From 165c649f3198a638c4f33007d2bf8ca6416d645f Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sun, 20 Sep 2020 11:44:03 +0200 Subject: [PATCH] Fix "is not a valid language" on upload --- cps/comic.py | 8 +------- cps/editbooks.py | 5 ++++- cps/epub.py | 12 ++---------- cps/gdrive.py | 2 +- cps/isoLanguages.py | 22 ++++++++++++++++++++++ 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/cps/comic.py b/cps/comic.py index e788fc44..57f204cd 100644 --- a/cps/comic.py +++ b/cps/comic.py @@ -135,13 +135,7 @@ def get_comic_info(tmp_file_path, original_file_name, original_file_extension, r loadedMetadata = archive.readMetadata(style) lang = loadedMetadata.language - if lang: - if len(lang) == 2: - loadedMetadata.language = isoLanguages.get(part1=lang).name - elif len(lang) == 3: - loadedMetadata.language = isoLanguages.get(part3=lang).name - else: - loadedMetadata.language = "" + loadedMetadata.language = isoLanguages.get_lang3(lang) return BookMeta( file_path=tmp_file_path, diff --git a/cps/editbooks.py b/cps/editbooks.py index cf1eb4c3..23da1458 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -357,7 +357,10 @@ def edit_book_comments(comments, book): def edit_book_languages(languages, book, upload=False): input_languages = languages.split(',') unknown_languages = [] - input_l = isoLanguages.get_language_codes(get_locale(), input_languages, unknown_languages) + if not upload: + input_l = isoLanguages.get_language_codes(get_locale(), input_languages, unknown_languages) + else: + input_l = isoLanguages.get_valid_language_codes(get_locale(), input_languages, unknown_languages) for l in unknown_languages: log.error('%s is not a valid language', l) flash(_(u"%(langname)s is not a valid language", langname=l), category="warning") diff --git a/cps/epub.py b/cps/epub.py index bdba0607..a1f2b1f0 100644 --- a/cps/epub.py +++ b/cps/epub.py @@ -83,16 +83,8 @@ def get_epub_info(tmp_file_path, original_file_name, original_file_extension): else: epub_metadata['description'] = "" - if epub_metadata['language'] == u'Unknown': - epub_metadata['language'] = "" - else: - lang = epub_metadata['language'].split('-', 1)[0].lower() - if len(lang) == 2: - epub_metadata['language'] = isoLanguages.get(part1=lang).name - elif len(lang) == 3: - epub_metadata['language'] = isoLanguages.get(part3=lang).name - else: - epub_metadata['language'] = "" + lang = epub_metadata['language'].split('-', 1)[0].lower() + epub_metadata['language'] = isoLanguages.get_lang3(lang) series = tree.xpath("/pkg:package/pkg:metadata/pkg:meta[@name='calibre:series']/@content", namespaces=ns) if len(series) > 0: diff --git a/cps/gdrive.py b/cps/gdrive.py index 82a19890..74a45061 100644 --- a/cps/gdrive.py +++ b/cps/gdrive.py @@ -43,7 +43,7 @@ log = logger.create() try: from googleapiclient.errors import HttpError except ImportError as err: - log.debug(("Cannot import googleapiclient, using gdrive will not work: %s", err)) + log.debug("Cannot import googleapiclient, using GDrive will not work: %s", err) current_milli_time = lambda: int(round(time() * 1000)) diff --git a/cps/isoLanguages.py b/cps/isoLanguages.py index d8b7fa00..2a8391f5 100644 --- a/cps/isoLanguages.py +++ b/cps/isoLanguages.py @@ -66,3 +66,25 @@ def get_language_codes(locale, language_names, remainder=None): if remainder is not None: remainder.extend(language_names) return languages + +def get_valid_language_codes(locale, language_names, remainder=None): + languages = list() + for k, v in get_language_names(locale).items(): + if k in language_names: + languages.append(k) + language_names.remove(k) + if remainder is not None and len(language_names): + remainder.extend(language_names) + return languages + +def get_lang3(lang): + try: + if len(lang) == 2: + ret_value = get(part1=lang).part3 + elif len(lang) == 3: + ret_value = lang + else: + ret_value = "" + except KeyError: + ret_value = lang + return ret_value