From 4cf71dd336569f103010c6bc90140a9c48d31762 Mon Sep 17 00:00:00 2001 From: Ozzieisaacs Date: Sun, 20 Sep 2020 11:41:44 +0200 Subject: [PATCH] Fix "is not a valid language" on upload --- cps/comic.py | 8 +------- cps/editbooks.py | 5 ++++- cps/epub.py | 13 +++---------- cps/gdrive.py | 2 +- cps/isoLanguages.py | 22 ++++++++++++++++++++++ 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/cps/comic.py b/cps/comic.py index d613be92..4f991129 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 63e8aa0c..f5494d36 100644 --- a/cps/editbooks.py +++ b/cps/editbooks.py @@ -393,7 +393,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..779c1ca2 100644 --- a/cps/epub.py +++ b/cps/epub.py @@ -26,6 +26,7 @@ from .helper import split_authors from .constants import BookMeta + def extractCover(zipFile, coverFile, coverpath, tmp_file_name): if coverFile is None: return None @@ -83,16 +84,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