make it possible to compile l20n on fly trunk
authorZbigniew Braniecki <gandalf@mozilla.com>
Thu, 19 Aug 2010 21:05:20 +0200
branchtrunk
changeset 437 b41b4e9f3692ae5a39a3d5831a4d37be2a99319a
parent 436 41c4bbad0c47e5feff391e164aff3fe76d7bf20d
child 438 b1dd85aefac7cb67ab1cec5f6edeb2191b87cfcc
push id22
push userzbraniecki@mozilla.com
push dateMon, 23 Aug 2010 14:01:03 +0000
make it possible to compile l20n on fly
scripts/convtol20n.py
--- a/scripts/convtol20n.py
+++ b/scripts/convtol20n.py
@@ -4,33 +4,34 @@ import os
 import sys
 from optparse import OptionParser
 
 sys.path.append('../silme/lib')
 import silme.core
 import silme.format
 import silme.io
 from silme.format.l20n import Serializer as L20nSerializer
+from silme.format.l20n import L20nFormatParser
 from silme.format.l20n.structure import Entity as L20nEntity
 from silme.format.l20n.structure import Comment as L20nComment
 from silme.format.l20n.structure import KeyValuePair
 
 
 import re
 
 def variable_replace(match):
     name = match.group(1).replace('.','_')
     return '${%(name)s}s' % {'name' : name}
 
 def upgrade(l10nobj):
     """ because the current silme.format.l20n.structure is 
     not inheriting from silme.core, we have to manually
     copy all Strings, Entities and Comments to L20nStructure to get
     L20n power. We can ignore this step if we just want to dump
-    plain L10nObject to L20n, but this time, we want to merge DTD entities
+    plain Structure to L20n, but this time, we want to merge DTD entities
     into L20n entities 
     """
     lol = silme.format.l20n.Structure()
     lol.id = l10nobj.id[:l10nobj.id.rfind('.')]+'.lol'
     for elem in l10nobj:
         if isinstance(elem, silme.core.Entity):
             entity = L20nEntity(elem.id.replace('.','_'))
             # DTD specific: replace &...; format for variables
@@ -46,82 +47,98 @@ def upgrade(l10nobj):
             lol.add(elem)
     return lol
 
 def standarize_key(arg):
     """standarize types of keys
     """
     keys = {'commandkey': ['commandkey', 'cmdkey', 'key'],
             'tooltip': ['tooltip'],
-            'accesskey': ['accesskey']}
+            'accesskey': ['accesskey'],
+            'label': ['label'],
+            'width': ['width'],
+            'height': ['height']}
     key = None
     for k, lst in keys.items():
         if arg in lst:
             key = k
     return key or False
 
 def merge_entities(lol):
     """we want to merge many entities from old format
     to one entity in a new format with KeyValuePairs"""
 
     entities = lol.entities()
     pattern = re.compile('(.*)_([a-zA-Z]+)(\d?)$', re.I)
 
     for i in entities:
-        if i.id.endswith('_label') or i.id.endswith('_title'): # clean the ID
+        if i.id.endswith('_title'): # clean the ID
             entity = lol.entity(i.id)
             entity.id = i.id[:-6]
             continue
+        print i.id
+        if i.id.endswith('_key') or i.id.endswith('_key2'): # make a commandkey a separate entity for now
+            i.kvplist['commandkey'] = KeyValuePair('commandkey', i.value)
+            i.value = "" # leave the value empty
+            continue
         m = pattern.match(i.id) #  check if the ID is in form of ssssss.sssd?
         if not m:
             continue
         key  = standarize_key(m.group(2).lower())
         if not key:
             continue
         if not lol.has_entity(m.group(1)): # we don't have entity for this key. Make it entity with type
             i.id = m.group(1)
-            i.kvplist['type'] = key
+            i.kvplist[key] = KeyValuePair(key, i.value)
+            i._value = "" # leave the value empty
             continue
         
         # we have a separate entity and we'll turn this entity into a key
         key  = standarize_key(m.group(2).lower())
         ent2 = lol.entity(m.group(1))
         found = False
-        for kvp in ent2.kvplist: # check if we already have such key
-            if kvp.key == key:
-                kvp.add(i.value)
+        for k, v in ent2.kvplist.items(): # check if we already have such key
+            if k == key:
+                v.add(i.value)
                 found = True
         if not found:
-            ent2.kvplist[key] = i.value
+            ent2.kvplist[key] = KeyValuePair(key, i.value)
         remove_entity(lol, i.id)
     return lol
 
 def remove_entity(lol, id):
     """it removes the entity and cleans the empty lines around it"""
     pos = lol.entity_pos(id)
     lol.remove_element(pos)
     if isinstance(lol[pos-1], unicode):
         if lol[pos-1]=='\n':
             lol.remove_element(pos-1) # remove its string
         elif lol[pos-1].startswith('\n\n'):
             lol[pos-1] = lol[pos-1][1:]
 
-def convtol20n(path1, path2):
+def convtol20n(path1, path2, compile=False):
     silme.format.Manager.register('dtd','l20n')
     ioc = silme.io.Manager.get('file')
     
     obj = ioc.get_structure(path1)
     
     lol = upgrade(obj) # optional
     lol = merge_entities(lol) # optional
-
-    ioc.write_structure(lol, path2)
+    print compile
+    if compile:
+      j20n = L20nFormatParser.compile(lol)
+      j20n = j20n.encode('utf8')
+      ioc.write_source(j20n, path2)
+    else:
+      ioc.write_structure(lol, path2)
 
 if __name__ == "__main__":
   usage = "usage: %prog path1 path2\n"
   usage += "Convert FILE1 to FILE2"
   parser = OptionParser(usage, version='%prog 0.1')
+  parser.add_option("-c", "--compile",
+                    action="store_true", dest="compile", default=False, help="compile to J20n")
   (options, args) = parser.parse_args(sys.argv[1:])
   if len(args)>=2:
-    convtol20n(args[0], args[1])
+    convtol20n(args[0], args[1], compile=options.compile)
   else:
     parser.error("input and output required")