From 8ac7014dfeea6f31cf5b7ec7325768b157da65bf Mon Sep 17 00:00:00 2001 From: Laurent Monin Date: Wed, 9 Apr 2014 12:08:02 +0200 Subject: [PATCH] Add `update_constants` command to setup.py It retrieves and parses attributes.pot and countries.pot files from transifex to extract various constants needed in Picard. It helps to keep those in sync with MusicBrainz website. It then generates: - picard/attributes.py containing cover art types, medium formats, and release group types - picard/countries.py containing countries `update_constants` command replaces `update_countries` --- .gitignore | 1 + .tx/config | 6 ++++ NEWS.txt | 2 +- po/attributes/.gitignore | 0 setup.py | 59 ++++++++++++++++++++++++++++++++++------ 5 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 po/attributes/.gitignore diff --git a/.gitignore b/.gitignore index 20756da8a..4456c8063 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ locale *.exe .DS_Store po/countries/countries.pot +po/attributes/attributes.pot diff --git a/.tx/config b/.tx/config index c767707e5..405ed3983 100644 --- a/.tx/config +++ b/.tx/config @@ -12,3 +12,9 @@ file_filter = po/countries/.po source_file = po/countries/countries.pot source_lang = en type = PO + +[musicbrainz.attributes] +file_filter = po/attributes/.po +source_file = po/attributes/attributes.pot +source_lang = en +type = PO diff --git a/NEWS.txt b/NEWS.txt index 4395ea09c..c65e0c4c9 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -24,7 +24,6 @@ * Added "_artists_sort", "_albumartists", "_albumartists_sort" variables for scripts and plugins. * Made Picard use the country names also used on the MusicBrainz website (PICARD-205) * New setup.py command `get_po_files` (Retrieve po files from transifex) - * New setup.py command `update_countries` (Regenerate countries.py) * New setup.py command `regen_pot_file` (Regenerate po/picard.pot) * New Work tag (which for Classical music is often different from the track title) saved as ID3 TOAL tag. * New Composer Sort Order tag (variable %composersort%). @@ -38,6 +37,7 @@ * Show the ID3 version of the file in the Info... dialog (Ctrl-I) (PICARD-218) * Fixed a bug where Picard crashed if a MP3 file had malformed TRCK or TPOS tags (PICARD-112) * Add --files option to setup.py build_ui, used to force .ui to .py regeneration (PICARD-566) + * New setup.py command `update_constants` (Regenerate countries.py and attributes.py) diff --git a/po/attributes/.gitignore b/po/attributes/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/setup.py b/setup.py index 1236dc4ef..3cac6da50 100755 --- a/setup.py +++ b/setup.py @@ -424,8 +424,8 @@ def _get_option_name(obj): raise Exception("No such command class") -class picard_update_countries(Command): - description = "Regenerate countries.py" +class picard_update_constants(Command): + description = "Regenerate attributes.py and countries.py" user_options = [ ('skip-pull', None, "skip the tx pull steps"), ] @@ -443,22 +443,22 @@ class picard_update_countries(Command): from babel.messages import pofile - countries = dict() if not self.skip_pull: txpull_cmd = [ tx_executable, 'pull', '--force', - '--resource=musicbrainz.countries', + '--resource=musicbrainz.attributes,musicbrainz.countries', '--source', '--language=none', ] self.spawn(txpull_cmd) - potfile = os.path.join('po', 'countries', 'countries.pot') + countries = dict() + countries_potfile = os.path.join('po', 'countries', 'countries.pot') isocode_comment = u'iso.code:' - with open(potfile, 'rb') as f: - log.info('Parsing %s' % potfile) + with open(countries_potfile, 'rb') as f: + log.info('Parsing %s' % countries_potfile) po = pofile.read_po(f) for message in po: if not message.id or not isinstance(message.id, unicode): @@ -472,6 +472,28 @@ class picard_update_countries(Command): else: sys.exit('Failed to extract any country code/name !') + attributes = dict() + attributes_potfile = os.path.join('po', 'attributes', 'attributes.pot') + extract_attributes = ( + u'DB:cover_art_archive.art_type/name', + u'DB:medium_format/name', + u'DB:release_group_primary_type/name', + u'DB:release_group_secondary_type/name', + ) + with open(attributes_potfile, 'rb') as f: + log.info('Parsing %s' % attributes_potfile) + po = pofile.read_po(f) + for message in po: + if not message.id or not isinstance(message.id, unicode): + continue + for loc, pos in message.locations: + if loc in extract_attributes: + attributes[u"%s:%03d" % (loc,pos)] = message.id + if attributes: + self.attributes_py_file(attributes) + else: + sys.exit('Failed to extract any attribute !') + def countries_py_file(self, countries): header = (u"# -*- coding: utf-8 -*-\n" u"# Automatically generated - don't edit.\n" @@ -492,6 +514,26 @@ class picard_update_countries(Command): log.info("%s was rewritten (%d countries)" % (filename, len(countries))) + def attributes_py_file(self, attributes): + header = (u"# -*- coding: utf-8 -*-\n" + u"# Automatically generated - don't edit.\n" + u"# Use `python setup.py {option}` to update it.\n" + u"\n" + u"MB_ATTRIBUTES = {{\n") + line = u" u'{key}': u'{value}',\n" + footer = u"}}\n" + filename = os.path.join('picard', 'attributes.py') + with open(filename, 'w') as attributes_py: + def write_utf8(s, **kwargs): + attributes_py.write(s.format(**kwargs).encode('utf-8')) + + write_utf8(header, option=_get_option_name(self)) + for key, value in sorted(attributes.items(), key=lambda (k,v): k): + write_utf8(line, key=key, value=value.replace("'", "\\'")) + write_utf8(footer) + log.info("%s was rewritten (%d attributes)" % (filename, + len(attributes))) + def cflags_to_include_dirs(cflags): cflags = cflags.split() @@ -507,6 +549,7 @@ def _picard_get_locale_files(): path_domain = { 'po': 'picard', os.path.join('po', 'countries'): 'picard-countries', + os.path.join('po', 'attributes'): 'picard-attributes', } for path, domain in path_domain.iteritems(): for filepath in glob.glob(os.path.join(path, '*.po')): @@ -537,7 +580,7 @@ args2 = { 'clean_ui': picard_clean_ui, 'install': picard_install, 'install_locales': picard_install_locales, - 'update_countries': picard_update_countries, + 'update_constants': picard_update_constants, 'get_po_files': picard_get_po_files, 'regen_pot_file': picard_regen_pot_file, },