Commits

Martin von Löwis  committed 0b8f6a2

Add more schema. Support alternatives.

  • Participants
  • Parent commits adb5f18

Comments (0)

Files changed (1)

File cldr/ldml.py

         return None
 
 class MandatoryChild(XMLInfo):
-    pass
+    '''Mandatory child element.'''
 
 class OptionalChild(XMLInfo):
-    pass
+    '''Optional child element (DTD multiplicity ?). Value is None or object'''
 
 class MultipleChild(XMLInfo):
+    '''Child with DTD multiplicity *. Values is a list.'''
     def initialvalue(self):
         return []
 
+class AltChild(XMLInfo):
+    '''Child that is declared with * multiplicity, but only ever occurs more than once when because of the alt= attribute.'''
+
 class LDMLSingleKey(dict):
     '''Dictionary keyed with a single key, either specified using standard dict lookup
     or as a keyword argument to get'''
         for name, kind in self._fields.items():
             setattr(self, name, kind.initialvalue())
 
+    @property
+    def alternatives(self):
+        if 'alt' not in self._fields:
+            raise AttributeError('Alternatives not supported in schema for '+self.__class__.__name__)
+        try:
+            return self._alternatives
+        except AttributeError:
+            self._alternatives = r = {}
+            return r
+
 class Incomplete(LDMLBase):
     pass
 
+class Any(LDMLBase):
+    pass
+
 class Empty(LDMLBase):
     "Element is declared EMPTY in DTD"
 
     references = Attribute()
     alt = Attribute()
 
+class Script(PCDATA):
+    type = Attribute()
+    draft = DraftAttribute()
+    references = Attribute()
+    alt = Attribute()
+    
 class Territory(PCDATA):
     type = Attribute()
     draft = DraftAttribute()
     references = Attribute()
     alt = Attribute()
 
-# script
-# territory
-# variant
-# alias
-# default
-# fallback
+class Variant(PCDATA):
+    type = Attribute()
+    draft = DraftAttribute()
+    references = Attribute()
+    alt = Attribute()
+
+class Alias(LDMLBase):
+    special = MultipleChild()
+
+    source = Attribute()
+    path = Attribute()
+    draft = DraftAttribute()
+    alt = Attribute()
+    
+class Default(LDMLBase):
+    special = MultipleChild()
+
+    type = Attribute()
+    choice = Attribute()
+    draft = DraftAttribute()
+    references = Attribute()
+    alt = Attribute()
+
+class Special(Any):
+    pass
+
+class Cp(LDMLBase):
+    special = MultipleChild()
+
+    hex = Attribute()
+    
+class Fallback(PCDATA): # deprecated
+    draft = DraftAttribute()
+    references = Attribute()
+    alt = Attribute()    
 
 class LocaleDisplayNames(LDMLBase):
     _alias = True
     special = MultipleChild()
 
     draft = DraftAttribute()
-    
+    standard = Attribute() # deprecated
+    references = Attribute()
+    validSublocales = Attribute()    
 
 class Scripts(Incomplete):
-    pass
+    _alias = True
+    script = TypedChild()
+    special = MultipleChild()
+
+    draft = DraftAttribute()
+    standard = Attribute() # deprecated
+    references = Attribute()
+    validSublocales = Attribute()
 
 class Territories(LDMLBase):
     _alias = True
     validSublocales = Attribute()
 
 class Variants(Incomplete):
-    pass
+    _alias = True
+    variant = TypedChild()
+    special = MultipleChild()
+
+    draft = DraftAttribute()
+    standard = Attribute() # deprecated
+    references = Attribute()
+    validSublocales = Attribute()
 
 class Keys(Incomplete):
-    pass
+    _alias = True
+    key = TypedChild()
+    special = MultipleChild()
+
+    draft = DraftAttribute()
+    standard = Attribute() # deprecated
+    references = Attribute()
+    validSublocales = Attribute()
 
 class Key(Incomplete):
     pass
 
 class Types(Incomplete):
-    pass
+    _alias = True
+    type = TypedChild()
+    special = MultipleChild()
+
+    draft = DraftAttribute()
+    standard = Attribute() # deprecated
+    references = Attribute()
+    validSublocales = Attribute()
 
 class Type(Incomplete):
     pass
 
 class TransformNames(Incomplete):
-    pass
+    _alias = True
+    transformName = TypedChild()
+    special = MultipleChild()
+
+    draft = DraftAttribute()
+    references = Attribute()
 
 class TransformName(Incomplete):
     pass
 
 class CodePatterns(Incomplete):
-    pass
+    _alias = True
+    codePattern = TypedChild()
+    special = MultipleChild()
 
 class CodePattern(Incomplete):
     pass
 
 class MeasurementSystemNames(Incomplete):
-    pass
+    _alias = True
+    measurementSystemName = TypedChild()
+    special = MultipleChild()
+
+    draft = DraftAttribute()
+    references = Attribute()
+    validSublocales = Attribute()
 
 class MeasurementSystemName(Incomplete):
     pass
 
-# XXX Alias
 class LocaleDisplayPattern(LDMLBase):
+    _alias = True
     localePattern = MultipleChild()
     localeSeparator = MultipleChild()
     localeKeyTypePattern = MultipleChild()
 class Relative(Incomplete):
     pass
 
-class Numbers(Incomplete):
-    pass
+class Numbers(LDMLBase):
+    _alias = True
+    defaultNumberingSystem = MultipleChild()
+    otherNumberingSystems = MultipleChild()
+    symbols = DictChild(('numberSystem',))
+    decimalFormats = DictChild(('numberSystem',))
+    scientificFormats = DictChild(('numberSystem',))
+    percentFormats = DictChild(('numberSystem',))
+    currencyFormats = DictChild(('numberSystem',))
+    currencies = OptionalChild()
+    special = MultipleChild()
 
 class DefaultNumberingSystem(Incomplete):
     pass
 class Finance(Incomplete):
     pass
 
-class Symbols(Incomplete):
-    pass
+class Symbols(LDMLBase):
+    decimal = AltChild()
+    group = AltChild()
+    list = AltChild()
+    percentSign = AltChild()
+    nativeZeroDigit = AltChild()
+    patternDigit = AltChild()
+    plusSign = AltChild()
+    minusSign = AltChild()
+    exponential = AltChild()
+    perMille = AltChild()
+    infinity = AltChild()
+    nan = AltChild()
+    currencyDecimal = AltChild()
+    currencyGroup = AltChild()
+    special = MultipleChild()
+
+    draft = DraftAttribute()
+    standard = Attribute()
+    references = Attribute()
+    alt = Attribute()
+    validSubLocales = Attribute()
+    numberSystem = Attribute()
 
 class Decimal(Incomplete):
     pass
         field = node._fields[child.tag]
         cnode = _buildtree(child, interning)
         if isinstance(field, MultipleChild):
+            # multiplicity *
             getattr(node, child.tag).append(cnode)
         elif isinstance(field, DictChild):
+            # multiplicity *, with dedicated keys
             if len(field.keys) == 1:
                 key = child.attrib[field.keys[0]]
             else:
                 key = tuple(child.attrib[k] for k in field.keys)
-            getattr(node, child.tag)[key] = cnode
+            mapping = getattr(node, child.tag)
+            if 'alt' in child.attrib:
+                # Add to existing element as alternative
+               mapping[key].alternatives[cnode.alt] = cnode
+            else:
+                assert key not in getattr(node, child.tag), (node, key)
+                mapping[key] = cnode
         else:
+            assert getattr(node, child.tag, cnode) is None
             setattr(node, child.tag, cnode)
     for name, field in node._fields.items():
         if isinstance(field, MandatoryChild):