Bug 1562642 - add missing license in python/l10n/convert_xul_to_fluent r=zbraniecki
authorSylvestre Ledru <sledru@mozilla.com>
Mon, 11 Nov 2019 22:45:02 +0000
changeset 501713 0ec55dcb63d9a141b276bfefb777d150fadd17a4
parent 501712 291e0963223b2c3fdd821bb0ee88db944801a1d0
child 501714 7ce4cf2b6d61a4241708fec5e22f634192a1fe90
push id36798
push useropoprus@mozilla.com
push dateWed, 13 Nov 2019 17:01:43 +0000
treeherdermozilla-central@7a55cb6e7aec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerszbraniecki
bugs1562642
milestone72.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1562642 - add missing license in python/l10n/convert_xul_to_fluent r=zbraniecki Depends on D52590 Differential Revision: https://phabricator.services.mozilla.com/D52591
python/l10n/convert_xul_to_fluent/convert.py
python/l10n/convert_xul_to_fluent/lib/__init__.py
python/l10n/convert_xul_to_fluent/lib/dtd.py
python/l10n/convert_xul_to_fluent/lib/fluent.py
python/l10n/convert_xul_to_fluent/lib/utils.py
python/l10n/convert_xul_to_fluent/lib/xul.py
--- a/python/l10n/convert_xul_to_fluent/convert.py
+++ b/python/l10n/convert_xul_to_fluent/convert.py
@@ -1,100 +1,104 @@
-from __future__ import absolute_import
-from __future__ import print_function
-from lib.xul import collect_messages
-from lib.dtd import get_dtds
-from lib.utils import read_file, write_file
-from lib.migration import build_migration
-from lib.fluent import build_ftl
-import json
-import argparse
-import sys
-# import re
-
-# To run this, you'll need to have lxml installed:
-#   `pip install lxml`
-
-# default migration directions
-data = {
-    'migration': 'python/l10n/fluent_migrations',
-    'description': 'Migrate l10n strings',
-    'prefix': ''
-}
-
-
-def parse_inputs():
-    sys_args = sys.argv[1:]
-
-    parser = argparse.ArgumentParser()
-    parser.add_argument('bug_id', type=str,
-                        help='Id number for bug tracking')
-    parser.add_argument('xul', type=str,
-                        help='POSIX path from mozilla-central to the XML to be updated')
-    parser.add_argument('ftl', type=str,
-                        help='Case sensitive POSIX path from mozilla-central to desired ftl file')
-    parser.add_argument('mozilla_central', type=str,
-                        help='Case sensitive absolute POSIX path to current mozilla-central repo')
-    parser.add_argument('dtd', type=str,
-                        help='comma delimited list of case sensitive POSIX dtd file paths')
-    parser.add_argument('description', type=str,
-                        help='string enclosed in quotes')
-    parser.add_argument('--dry-run', action='store_true',
-                        help='Tell if running dry run or not')
-    parser.add_argument('--prefix', type=str,
-                        help='a keyword string to be added to all l10n ids')
-
-    parsed_args = parser.parse_args(sys_args)
-
-    data['description'] = parsed_args.description
-    data['bug_id'] = parsed_args.bug_id
-    data['xul'] = parsed_args.xul
-    data['ftl'] = parsed_args.ftl
-    data['mozilla-central'] = parsed_args.mozilla_central
-    data['dtd'] = parsed_args.dtd.split(',')
-    data['dry-run'] = parsed_args.dry_run
-    data['prefix'] = parsed_args.prefix
-    data['recipe'] = "bug_{}_{}.py".format(data['bug_id'],
-                                           data['xul'].split('/')[-1].split('.')[0])
-
-    main()
-
-
-def main():
-    dry_run = data['dry-run']
-    dtds = get_dtds(data['dtd'], data['mozilla-central'])
-
-    print('======== DTDs ========')
-    print(json.dumps(dtds, sort_keys=True, indent=2))
-
-    s = read_file(data['xul'], data['mozilla-central'])
-
-    print('======== INPUT ========')
-    print(s)
-
-    print('======== OUTPUT ========')
-    (new_xul, messages) = collect_messages(s, data['prefix'])
-    print(new_xul)
-    if not dry_run:
-        write_file(data['xul'], new_xul, data['mozilla-central'])
-
-    print('======== L10N ========')
-
-    print(json.dumps(messages, sort_keys=True, indent=2))
-
-    migration = build_migration(messages, dtds, data)
-
-    print('======== MIGRATION ========')
-    print(migration)
-    recipe_path = "{}/{}".format(data['migration'], data['recipe'])
-    if not dry_run:
-        write_file(recipe_path, migration, data['mozilla-central'])
-
-    ftl = build_ftl(messages, dtds, data)
-
-    print('======== Fluent ========')
-    print(ftl.encode("utf-8"))
-    if not dry_run:
-        write_file(data['ftl'], ftl.encode("utf-8"), data['mozilla-central'], append=True)
-
-
-if __name__ == '__main__':
-    parse_inputs()
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from __future__ import absolute_import
+from __future__ import print_function
+from lib.xul import collect_messages
+from lib.dtd import get_dtds
+from lib.utils import read_file, write_file
+from lib.migration import build_migration
+from lib.fluent import build_ftl
+import json
+import argparse
+import sys
+# import re
+
+# To run this, you'll need to have lxml installed:
+#   `pip install lxml`
+
+# default migration directions
+data = {
+    'migration': 'python/l10n/fluent_migrations',
+    'description': 'Migrate l10n strings',
+    'prefix': ''
+}
+
+
+def parse_inputs():
+    sys_args = sys.argv[1:]
+
+    parser = argparse.ArgumentParser()
+    parser.add_argument('bug_id', type=str,
+                        help='Id number for bug tracking')
+    parser.add_argument('xul', type=str,
+                        help='POSIX path from mozilla-central to the XML to be updated')
+    parser.add_argument('ftl', type=str,
+                        help='Case sensitive POSIX path from mozilla-central to desired ftl file')
+    parser.add_argument('mozilla_central', type=str,
+                        help='Case sensitive absolute POSIX path to current mozilla-central repo')
+    parser.add_argument('dtd', type=str,
+                        help='comma delimited list of case sensitive POSIX dtd file paths')
+    parser.add_argument('description', type=str,
+                        help='string enclosed in quotes')
+    parser.add_argument('--dry-run', action='store_true',
+                        help='Tell if running dry run or not')
+    parser.add_argument('--prefix', type=str,
+                        help='a keyword string to be added to all l10n ids')
+
+    parsed_args = parser.parse_args(sys_args)
+
+    data['description'] = parsed_args.description
+    data['bug_id'] = parsed_args.bug_id
+    data['xul'] = parsed_args.xul
+    data['ftl'] = parsed_args.ftl
+    data['mozilla-central'] = parsed_args.mozilla_central
+    data['dtd'] = parsed_args.dtd.split(',')
+    data['dry-run'] = parsed_args.dry_run
+    data['prefix'] = parsed_args.prefix
+    data['recipe'] = "bug_{}_{}.py".format(data['bug_id'],
+                                           data['xul'].split('/')[-1].split('.')[0])
+
+    main()
+
+
+def main():
+    dry_run = data['dry-run']
+    dtds = get_dtds(data['dtd'], data['mozilla-central'])
+
+    print('======== DTDs ========')
+    print(json.dumps(dtds, sort_keys=True, indent=2))
+
+    s = read_file(data['xul'], data['mozilla-central'])
+
+    print('======== INPUT ========')
+    print(s)
+
+    print('======== OUTPUT ========')
+    (new_xul, messages) = collect_messages(s, data['prefix'])
+    print(new_xul)
+    if not dry_run:
+        write_file(data['xul'], new_xul, data['mozilla-central'])
+
+    print('======== L10N ========')
+
+    print(json.dumps(messages, sort_keys=True, indent=2))
+
+    migration = build_migration(messages, dtds, data)
+
+    print('======== MIGRATION ========')
+    print(migration)
+    recipe_path = "{}/{}".format(data['migration'], data['recipe'])
+    if not dry_run:
+        write_file(recipe_path, migration, data['mozilla-central'])
+
+    ftl = build_ftl(messages, dtds, data)
+
+    print('======== Fluent ========')
+    print(ftl.encode("utf-8"))
+    if not dry_run:
+        write_file(data['ftl'], ftl.encode("utf-8"), data['mozilla-central'], append=True)
+
+
+if __name__ == '__main__':
+    parse_inputs()
--- a/python/l10n/convert_xul_to_fluent/lib/__init__.py
+++ b/python/l10n/convert_xul_to_fluent/lib/__init__.py
@@ -1,2 +1,6 @@
-from __future__ import absolute_import
-# from .xul import collect_messages
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from __future__ import absolute_import
+# from .xul import collect_messages
--- a/python/l10n/convert_xul_to_fluent/lib/dtd.py
+++ b/python/l10n/convert_xul_to_fluent/lib/dtd.py
@@ -1,27 +1,31 @@
-from __future__ import absolute_import
-from io import StringIO
-from lxml import etree
-from .utils import read_file
-
-
-def get_dtds(sources, base_path):
-    entries = {}
-    for source in sources:
-        dtd = get_dtd(source, base_path)
-        for entry in dtd:
-            entries[entry] = {
-                "value": dtd[entry],
-                "file": source
-            }
-    return entries
-
-
-def get_dtd(dtd_source, base_path):
-    entries = {}
-
-    source = read_file(dtd_source, base_path)
-
-    dtd = etree.DTD(StringIO(source.decode("utf-8")))
-    for entity in dtd.entities():
-        entries[entity.name] = entity.content
-    return entries
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from __future__ import absolute_import
+from io import StringIO
+from lxml import etree
+from .utils import read_file
+
+
+def get_dtds(sources, base_path):
+    entries = {}
+    for source in sources:
+        dtd = get_dtd(source, base_path)
+        for entry in dtd:
+            entries[entry] = {
+                "value": dtd[entry],
+                "file": source
+            }
+    return entries
+
+
+def get_dtd(dtd_source, base_path):
+    entries = {}
+
+    source = read_file(dtd_source, base_path)
+
+    dtd = etree.DTD(StringIO(source.decode("utf-8")))
+    for entity in dtd.entities():
+        entries[entity.name] = entity.content
+    return entries
--- a/python/l10n/convert_xul_to_fluent/lib/fluent.py
+++ b/python/l10n/convert_xul_to_fluent/lib/fluent.py
@@ -1,30 +1,34 @@
-from __future__ import absolute_import
-from fluent.syntax import ast
-from fluent.syntax.serializer import FluentSerializer
-
-
-def get_value_from_dtd(name, dtd):
-    return dtd[name[1:-1]]['value']
-
-
-def build_ftl(messages, dtd, data):
-    res = ast.Resource()
-
-    for id_str in messages:
-        msg = messages[id_str]
-        l10n_id = ast.Identifier(id_str)
-        val = None
-        attrs = []
-        if msg['value']:
-            dtd_val = get_value_from_dtd(msg['value'], dtd)
-            val = ast.Pattern([ast.TextElement(dtd_val)])
-        for attr_name in msg['attrs']:
-            dtd_val = get_value_from_dtd(msg['attrs'][attr_name], dtd)
-            attr_val = ast.Pattern([ast.TextElement(dtd_val)])
-            attrs.append(ast.Attribute(ast.Identifier(attr_name), attr_val))
-
-        m = ast.Message(l10n_id, val, attrs)
-        res.body.append(m)
-
-    serializer = FluentSerializer()
-    return serializer.serialize(res)
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from __future__ import absolute_import
+from fluent.syntax import ast
+from fluent.syntax.serializer import FluentSerializer
+
+
+def get_value_from_dtd(name, dtd):
+    return dtd[name[1:-1]]['value']
+
+
+def build_ftl(messages, dtd, data):
+    res = ast.Resource()
+
+    for id_str in messages:
+        msg = messages[id_str]
+        l10n_id = ast.Identifier(id_str)
+        val = None
+        attrs = []
+        if msg['value']:
+            dtd_val = get_value_from_dtd(msg['value'], dtd)
+            val = ast.Pattern([ast.TextElement(dtd_val)])
+        for attr_name in msg['attrs']:
+            dtd_val = get_value_from_dtd(msg['attrs'][attr_name], dtd)
+            attr_val = ast.Pattern([ast.TextElement(dtd_val)])
+            attrs.append(ast.Attribute(ast.Identifier(attr_name), attr_val))
+
+        m = ast.Message(l10n_id, val, attrs)
+        res.body.append(m)
+
+    serializer = FluentSerializer()
+    return serializer.serialize(res)
--- a/python/l10n/convert_xul_to_fluent/lib/utils.py
+++ b/python/l10n/convert_xul_to_fluent/lib/utils.py
@@ -1,19 +1,23 @@
-from __future__ import absolute_import
-import os
-
-
-def read_file(path, base_path=None):
-    if base_path is not None:
-        path = os.path.join(base_path, path)
-        path = path.replace('\\', '/')
-    with open(path) as fptr:
-        return fptr.read()
-
-
-def write_file(path, text, base_path=None, append=False):
-    if base_path is not None:
-        path = os.path.join(base_path, path)
-        path = path.replace('\\', '/')
-    mode = "a" if append and os.path.exists(path) else "w"
-    with open(path, mode) as text_file:
-        text_file.write(text)
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from __future__ import absolute_import
+import os
+
+
+def read_file(path, base_path=None):
+    if base_path is not None:
+        path = os.path.join(base_path, path)
+        path = path.replace('\\', '/')
+    with open(path) as fptr:
+        return fptr.read()
+
+
+def write_file(path, text, base_path=None, append=False):
+    if base_path is not None:
+        path = os.path.join(base_path, path)
+        path = path.replace('\\', '/')
+    mode = "a" if append and os.path.exists(path) else "w"
+    with open(path, mode) as text_file:
+        text_file.write(text)
--- a/python/l10n/convert_xul_to_fluent/lib/xul.py
+++ b/python/l10n/convert_xul_to_fluent/lib/xul.py
@@ -1,116 +1,120 @@
-from __future__ import absolute_import
-import re
-import string
-
-tag_re = r'<([a-z]+[^>/]*)(/?>)([^<]*)(?:</[a-z]+>)?'
-attr_re = r'\s+([a-z]+)="([\&\;\.a-zA-Z0-9]+)"'
-prefix = ""
-
-messages = {}
-
-
-def is_entity(s):
-    return s.startswith('&') and s.endswith(';')
-
-
-def convert_camel_case(name):
-    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1-\2', name)
-    return re.sub('([a-z0-9])([A-Z])', r'\1-\2', s1).lower()
-
-
-def construct_l10n_id(val, attrs):
-    global prefix
-    id = None
-    if val:
-        id = val[1:-1].strip(string.digits)
-    else:
-        core = None
-        for k in attrs:
-            v = attrs[k][1:-1].strip(string.digits).split('.')
-            if not core:
-                if v[-1].lower() == k:
-                    core = '.'.join(v[:-1])
-                else:
-                    core = '.'.join(v)
-        if core:
-            id = core
-    id = id.replace('.', '-')
-    id = convert_camel_case(id)
-    if prefix:
-        id = "{}-{}".format(prefix, id)
-    return id
-
-
-vector = 0
-is_l10n = False
-
-
-def get_indent(pre_tag):
-    if "\n" not in pre_tag:
-        return " "
-    last_bl = pre_tag.rindex("\n")
-    indent = 0
-    for ch in pre_tag[last_bl:]:
-        if ch == " ":
-            indent += 1
-    return "\n" + " " * indent
-
-
-def tagrepl(m):
-    global vector
-    global is_l10n
-    vector = 0
-
-    is_l10n = False
-    l10n_val = None
-    l10n_attrs = {}
-    if is_entity(m.group(3)):
-        is_l10n = True
-        l10n_val = m.group(3)
-
-    def attrrepl(m2):
-        global vector
-        global is_l10n
-        attr_l10n = False
-        if is_entity(m2.group(2)):
-            attr_l10n = True
-            l10n_attrs[m2.group(1)] = m2.group(2)
-        if attr_l10n:
-            is_l10n = True
-            vector = vector + len(m2.group(0))
-            return ""
-        return m2.group(0)
-
-    tag = re.sub(attr_re, attrrepl, m.group(0))
-    if is_l10n:
-        l10n_id = construct_l10n_id(l10n_val, l10n_attrs)
-        messages[l10n_id] = {
-            "value": l10n_val,
-            "attrs": l10n_attrs
-        }
-        indent = get_indent(tag[0:len(m.group(1)) + 1 - vector])
-        tag = \
-            tag[0:len(m.group(1)) + 1 - vector] + \
-            indent + \
-            'data-l10n-id="' + \
-            l10n_id + \
-            '"' + \
-            m.group(2) + \
-            (m.group(3) if not l10n_val else "") + \
-            tag[len(m.group(1)) + 1 + len(m.group(2)) +
-                len(m.group(3)) - vector:]
-    return tag
-
-
-def collect_messages(xul_source, in_prefix):
-    global messages
-    global prefix
-    messages = {}
-    prefix = in_prefix
-
-    new_source = re.sub(tag_re, tagrepl, xul_source)
-    return (new_source, messages)
-
-
-if __name__ == '__main__':
-    pass
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+from __future__ import absolute_import
+import re
+import string
+
+tag_re = r'<([a-z]+[^>/]*)(/?>)([^<]*)(?:</[a-z]+>)?'
+attr_re = r'\s+([a-z]+)="([\&\;\.a-zA-Z0-9]+)"'
+prefix = ""
+
+messages = {}
+
+
+def is_entity(s):
+    return s.startswith('&') and s.endswith(';')
+
+
+def convert_camel_case(name):
+    s1 = re.sub('(.)([A-Z][a-z]+)', r'\1-\2', name)
+    return re.sub('([a-z0-9])([A-Z])', r'\1-\2', s1).lower()
+
+
+def construct_l10n_id(val, attrs):
+    global prefix
+    id = None
+    if val:
+        id = val[1:-1].strip(string.digits)
+    else:
+        core = None
+        for k in attrs:
+            v = attrs[k][1:-1].strip(string.digits).split('.')
+            if not core:
+                if v[-1].lower() == k:
+                    core = '.'.join(v[:-1])
+                else:
+                    core = '.'.join(v)
+        if core:
+            id = core
+    id = id.replace('.', '-')
+    id = convert_camel_case(id)
+    if prefix:
+        id = "{}-{}".format(prefix, id)
+    return id
+
+
+vector = 0
+is_l10n = False
+
+
+def get_indent(pre_tag):
+    if "\n" not in pre_tag:
+        return " "
+    last_bl = pre_tag.rindex("\n")
+    indent = 0
+    for ch in pre_tag[last_bl:]:
+        if ch == " ":
+            indent += 1
+    return "\n" + " " * indent
+
+
+def tagrepl(m):
+    global vector
+    global is_l10n
+    vector = 0
+
+    is_l10n = False
+    l10n_val = None
+    l10n_attrs = {}
+    if is_entity(m.group(3)):
+        is_l10n = True
+        l10n_val = m.group(3)
+
+    def attrrepl(m2):
+        global vector
+        global is_l10n
+        attr_l10n = False
+        if is_entity(m2.group(2)):
+            attr_l10n = True
+            l10n_attrs[m2.group(1)] = m2.group(2)
+        if attr_l10n:
+            is_l10n = True
+            vector = vector + len(m2.group(0))
+            return ""
+        return m2.group(0)
+
+    tag = re.sub(attr_re, attrrepl, m.group(0))
+    if is_l10n:
+        l10n_id = construct_l10n_id(l10n_val, l10n_attrs)
+        messages[l10n_id] = {
+            "value": l10n_val,
+            "attrs": l10n_attrs
+        }
+        indent = get_indent(tag[0:len(m.group(1)) + 1 - vector])
+        tag = \
+            tag[0:len(m.group(1)) + 1 - vector] + \
+            indent + \
+            'data-l10n-id="' + \
+            l10n_id + \
+            '"' + \
+            m.group(2) + \
+            (m.group(3) if not l10n_val else "") + \
+            tag[len(m.group(1)) + 1 + len(m.group(2)) +
+                len(m.group(3)) - vector:]
+    return tag
+
+
+def collect_messages(xul_source, in_prefix):
+    global messages
+    global prefix
+    messages = {}
+    prefix = in_prefix
+
+    new_source = re.sub(tag_re, tagrepl, xul_source)
+    return (new_source, messages)
+
+
+if __name__ == '__main__':
+    pass