extend l20n format support with various minor improvements trunk
authorZbigniew Braniecki <gandalf@mozilla.com>
Tue, 17 Aug 2010 19:13:53 +0200
branchtrunk
changeset 562 610bf8335651f01e916c99275f152117487414cc
parent 561 d82be00bdbe2c86e8158ecd695fbae32c54ad616
child 563 baa676ecf45d44ce9cb1e865cdae8130a9994a3a
push id267
push userzbraniecki@mozilla.com
push dateWed, 18 Aug 2010 21:52:40 +0000
extend l20n format support with various minor improvements
lib/silme/format/l20n/__init__.py
lib/silme/format/l20n/compiler.py
lib/silme/format/l20n/serializer.py
--- a/lib/silme/format/l20n/__init__.py
+++ b/lib/silme/format/l20n/__init__.py
@@ -1,14 +1,14 @@
 from .parser import L20nParser as Parser
 from .structure import LOL as Structure, Entity
 from .serializer import L20nSerializer as Serializer
 from .compiler import L20nCompiler as Compiler
 
-class L20nFormatParser ():
+class L20nFormatParser():
     name = 'l20n'
     desc = "L20n reader/writer"
     extensions = ['lol']
     encoding = 'utf_8' # allowed encoding
     fallback = None
 
     @classmethod
     def dump_structure(cls, l10nobject):
--- a/lib/silme/format/l20n/compiler.py
+++ b/lib/silme/format/l20n/compiler.py
@@ -1,10 +1,11 @@
 from .structure import *
-from ...core import Entity, Comment
+from ...core import Entity
+from silme.format.l20n.structure import Comment
 from silme.core.entity import is_entity, is_string
 import re
 
 class L20nCompiler(object):
     patterns = {
         'complex_string': re.compile('\$\{([^\}]+)\}[is]', re.S|re.U)
     }
     
@@ -25,37 +26,40 @@ class L20nCompiler(object):
 
     @classmethod
     def dump_entity(cls, entity):
         if isinstance(entity.values, list):
           string = u'this.%s = [%s]\n' % (entity.id, ','.join(map(cls.dump_string,entity.values)))
           if cls.has_params(entity):
               for k,v in entity.kvplist.items():
                   string += u'this.%s.%s = "%s"\n' % (entity.id, k,v)
-        elif is_string(entity.value):
+        elif is_string(entity.value) and len(entity.value): # if string is empty, skip it
             if cls.is_complex(entity):
                 if cls.has_params(entity):
                     string = u'this.__defineGetter__(\'%s\',function() {\n' % entity.id
                     string += u'  var x = new String("%s")\n' % entity.value
                     for k,v in entity.kvplist.items():
-                        string += u'  x.%s = "%s"\n' % (k,v)
+                        string += u'  x.%s = "%s"\n' % (k,v.value)
                     string += u'  return x\n'
                     string += u'})\n'
                 else:
                     string = u'this.%s=function() {return %s}\n' % (entity.id, cls.dump_value(entity.value));
                     string += 'this.%s.toString=function() {return self.%s()}\n' % (entity.id, entity.id);
             else:
                 string = u'this.%s=%s\n' % (entity.id, cls.dump_value(entity.value))
         elif entity.value.__class__.__name__=='Macro':
             macro = entity.value
             string = u'this.%s=function(%s) {\n  return %s\n}\n' % (entity.id,
                                                                     cls.dump_idlist(macro.idlist),
                                                                     cls.dump_structure(macro.structure))
         else:
-            string = u'foo'
+            if is_string(entity.value) and len(entity.value) == 0:
+                string = u'this.%s={%s}' % (entity.id, ', '.join("'%s': '%s'" % (k,v.value) for k,v in entity.kvplist.items()))
+            else:
+                string = u'foo'
         return string
 
     @classmethod
     def dump_structure(cls,s):
         t = s.__class__
         string = u''
         if is_string(s) or isinstance(s,int):
             string += unicode(s)
@@ -80,18 +84,17 @@ class L20nCompiler(object):
     def dump_idlist(cls,l):
         string = ','.join(l)
         return string
 
 
     @classmethod
     def dump_comment(cls, comment):
         string = u'/*'
-        for element in comment:
-            string += cls.dump_element(element)
+        string += comment.content
         string += u'*/'
         return string
 
     @classmethod
     def dump_kwpair(cls, key, value):
         return u'"%s": %s' % (key, value)
     
     @classmethod
--- a/lib/silme/format/l20n/serializer.py
+++ b/lib/silme/format/l20n/serializer.py
@@ -1,51 +1,49 @@
 import silme.core
 from structure import Entity as L20nEntity,Comment as L20nComment
 from parser import L20nParser as Parser
 
 import re
 
 class L20nSerializer():
     @classmethod
-    def serialize(cls, l10nobject, fallback=None):
-        if not fallback:
-            fallback = l10nobject.fallback
-        string = u''.join([cls.dump_element(element, fallback) for element in l10nobject])
+    def serialize(cls, l10nobject):
+        string = u''.join([cls.dump_element(element) for element in l10nobject])
         return string
 
     @classmethod
-    def dump_element(cls, element, fallback=None):
+    def dump_element(cls, element):
         if isinstance(element, silme.core.Entity) or \
            isinstance(element, L20nEntity):
-            return cls.dump_entity(element, fallback=fallback)
+            return cls.dump_entity(element)
         elif isinstance(element, silme.core.Comment) or\
              isinstance(element, L20nComment):
             return cls.dump_comment(element)
         else:
             return unicode(element)
 
     @classmethod
-    def dump_entity(cls, entity, fallback=None):
+    def dump_entity(cls, entity):
         if isinstance(entity, L20nEntity):
-            string = u'<'+entity.id+u': "'+unicode(entity.get_value(fallback))+u'"'
+            string = u'<'+entity.id+u': "'+unicode(entity.value)+u'"'
             if entity.kvplist:
                 string += u'\n '
-                string += u'\n '.join([cls.dump_kvp(kvp) for kvp in entity.kvplist])
+                string += u'\n '.join([cls.dump_kvp(k,v) for k,v in entity.kvplist.items()])
             string += u'>'
         else:
-            string = u'<'+entity.id+u': "'+unicode(entity.get_value(fallback))+u'">'
+            string = u'<'+entity.id+u': "'+unicode(entity.value)+u'">'
         return string
 
     @classmethod
-    def dump_kvp(cls, kvp):
-        if isinstance(kvp.value, list):
-            return u'%s: [%s]' % (kvp.key, u','.join(["\"%s\"" % v for v in kvp.value]))
+    def dump_kvp(cls, key, value):
+        if isinstance(value, list):
+            return u'%s: [%s]' % (key, u','.join(["\"%s\"" % v for v in value]))
         else:
-            return u'%s: "%s"' % (kvp.key, unicode(kvp.value))
+            return u'%s: "%s"' % (key, unicode(value))
 
     @classmethod
     def dump_entitylist(cls, elist, fallback=None):
         if not fallback:
             fallback = elist.fallback
         string = u''.join([cls.dump_entity(entity, fallback)+'\n' for entity in elist.get_entities()])
         return string