make it possible to compile l20n on fly trunk
authorZbigniew Braniecki <>
Thu, 19 Aug 2010 21:05:20 +0200
changeset 437 b41b4e9f3692ae5a39a3d5831a4d37be2a99319a
parent 436 41c4bbad0c47e5feff391e164aff3fe76d7bf20d
child 438 b1dd85aefac7cb67ab1cec5f6edeb2191b87cfcc
push id22
push dateMon, 23 Aug 2010 14:01:03 +0000
make it possible to compile l20n on fly
--- a/scripts/
+++ b/scripts/
@@ -4,33 +4,34 @@ import os
 import sys
 from optparse import OptionParser
 import silme.core
 import silme.format
 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 ='.','_')
     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'
     for elem in l10nobj:
         if isinstance(elem, silme.core.Entity):
             entity = L20nEntity('.','_'))
             # DTD specific: replace &...; format for variables
@@ -46,82 +47,98 @@ def upgrade(l10nobj):
     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'_label') or'_title'): # clean the ID
+        if'_title'): # clean the ID
             entity = lol.entity(
+        print
+        if'_key') or'_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( #  check if the ID is in form of ssssss.sssd?
         if not m:
         key  = standarize_key(
         if not key:
         if not lol.has_entity( # we don't have entity for this key. Make it entity with type
-            i.kvplist['type'] = key
+            i.kvplist[key] = KeyValuePair(key, i.value)
+            i._value = "" # leave the value empty
         # we have a separate entity and we'll turn this entity into a key
         key  = standarize_key(
         ent2 = lol.entity(
         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)
     return lol
 def remove_entity(lol, id):
     """it removes the entity and cleans the empty lines around it"""
     pos = lol.entity_pos(id)
     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):
     ioc ='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)
     parser.error("input and output required")