From c807f154515ec23e9f65134682ccf9974c2007ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Louis=20V=C3=A9zina?= <5130500+morpheus65535@users.noreply.github.com> Date: Sun, 11 Aug 2019 13:06:36 -0400 Subject: [PATCH] Continuing development. --- bazarr/get_languages.py | 145 +++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 77 deletions(-) diff --git a/bazarr/get_languages.py b/bazarr/get_languages.py index f5e83a7cd..a58fbdc8a 100644 --- a/bazarr/get_languages.py +++ b/bazarr/get_languages.py @@ -6,121 +6,112 @@ import pycountry from get_args import args from subzero.language import Language +from database import TableSettingsLanguages def load_language_in_db(): # Get languages list in langs tuple - langs = [[lang.alpha_3, lang.alpha_2, lang.name] + langs = [{'code3': lang.alpha_3, 'code2': lang.alpha_2, 'name': lang.name} for lang in pycountry.languages if hasattr(lang, 'alpha_2')] - # Open database connection - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - # Insert languages in database table - c.executemany('''INSERT OR IGNORE INTO table_settings_languages(code3, code2, name) VALUES(?, ?, ?)''', langs) - c.execute('''INSERT OR IGNORE INTO table_settings_languages(code3, code2, name) VALUES(?, ?, ?)''', - ('pob', 'pb', 'Brazilian Portuguese')) - - langs = [[lang.bibliographic, lang.alpha_3] + for lang in langs: + TableSettingsLanguages.insert( + lang + ).on_conflict_ignore().execute() + + TableSettingsLanguages.insert( + { + TableSettingsLanguages.code3: 'pob', + TableSettingsLanguages.code2: 'pb', + TableSettingsLanguages.name: 'Brazilian Portuguese' + } + ).on_conflict_ignore().execute() + + langs = [{'code3b': lang.bibliographic, 'code3': lang.alpha_3} for lang in pycountry.languages if hasattr(lang, 'alpha_2') and hasattr(lang, 'bibliographic')] # Update languages in database table - c.executemany('''UPDATE table_settings_languages SET code3b = ? WHERE code3 = ?''', langs) - - # Commit changes to database table - db.commit() - - # Close database connection - db.close() + for lang in langs: + TableSettingsLanguages.update( + { + TableSettingsLanguages.code3b: lang['code3b'] + } + ).where( + TableSettingsLanguages.code3 == lang['code3'] + ).execute() def language_from_alpha2(lang): - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT name FROM table_settings_languages WHERE code2 = ?''', (lang,)).fetchone()[0] - except: - result = None - db.close() - return result + result = TableSettingsLanguages.select( + TableSettingsLanguages.name + ).where( + TableSettingsLanguages.code2 == lang + ).first() + return result.name def language_from_alpha3(lang): - if lang == 'fre': - lang = 'fra' - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT name FROM table_settings_languages WHERE code3 = ? OR code3b = ?''', - (lang, lang)).fetchone()[0] - except: - result = None - db.close() - return result + result = TableSettingsLanguages.select( + TableSettingsLanguages.name + ).where( + TableSettingsLanguages.code3 == lang | TableSettingsLanguages.code3b == lang + ).first() + return result.name def alpha2_from_alpha3(lang): - if lang == 'fre': - lang = 'fra' - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT code2 FROM table_settings_languages WHERE code3 = ? OR code3b = ?''', - (lang, lang)).fetchone()[0] - except: - result = None - db.close() - return result + result = TableSettingsLanguages.select( + TableSettingsLanguages.code2 + ).where( + TableSettingsLanguages.code3 == lang | TableSettingsLanguages.code3b == lang + ).first() + return result.code2 def alpha2_from_language(lang): - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT code2 FROM table_settings_languages WHERE name = ?''', (lang,)).fetchone()[0] - except: - result = None - db.close() - return result + result = TableSettingsLanguages.select( + TableSettingsLanguages.code2 + ).where( + TableSettingsLanguages.name == lang + ).first() + return result.code2 def alpha3_from_alpha2(lang): - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT code3 FROM table_settings_languages WHERE code2 = ?''', (lang,)).fetchone()[0] - except: - result = None - db.close() - return result + result = TableSettingsLanguages.select( + TableSettingsLanguages.code3 + ).where( + TableSettingsLanguages.code2 == lang + ).first() + return result.code3 def alpha3_from_language(lang): - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - try: - result = c.execute('''SELECT code3 FROM table_settings_languages WHERE name = ?''', (lang,)).fetchone()[0] - except: - result = None - db.close() - return result + result = TableSettingsLanguages.select( + TableSettingsLanguages.code3 + ).where( + TableSettingsLanguages.name == lang + ).first() + return result.code3 def get_language_set(): - db = sqlite3.connect(os.path.join(args.config_dir, 'db', 'bazarr.db'), timeout=30) - c = db.cursor() - languages = c.execute('''SELECT code3 FROM table_settings_languages WHERE enabled = 1''').fetchall() - db.close() + languages = TableSettingsLanguages.select( + TableSettingsLanguages.code3 + ).where( + TableSettingsLanguages.enabled == 1 + ) + language_set = set() for lang in languages: - if lang[0] == 'pob': + if lang.code3 == 'pob': language_set.add(Language('por', 'BR')) else: - language_set.add(Language(lang[0])) + language_set.add(Language(lang.code3)) return language_set