* class'in decode'unu da implement et, testini de yap

* birtakim temizlikler yap, down stupidity
* zibidi
This commit is contained in:
Eray Özkural
2005-09-21 23:52:45 +00:00
parent 495b3b6613
commit d064e8a940
3 changed files with 40 additions and 12 deletions
+24 -11
View File
@@ -163,6 +163,7 @@ class autoxml(type):
""" """
def __init__(cls, name, bases, dict): def __init__(cls, name, bases, dict):
print 'generating class', name
# add XmlFile as one of the superclasses, we're smart # add XmlFile as one of the superclasses, we're smart
bases = list(bases) bases = list(bases)
@@ -285,12 +286,12 @@ class autoxml(type):
def gen_tag(cls, tag, spec, readtext, writetext): def gen_tag(cls, tag, spec, readtext, writetext):
"""generate readers and writers for the tag""" """generate readers and writers for the tag"""
tag_type = spec[0] tag_type = spec[0]
if type(tag_type) == types.TypeType: if type(tag_type) is types.TypeType:
return cls.gen_anon_basic(tag, spec, readtext, writetext) return cls.gen_anon_basic(tag, spec, readtext, writetext)
elif type(tag_type) == types.ListType: elif type(tag_type) is types.ListType:
return cls.gen_list_tag(tag, spec, readtext, writetext) return cls.gen_list_tag(tag, spec)
elif type(tag_type) == types.ClassType: elif type(tag_type) is autoxml or type(tag_type) is types.ClassType:
return cls.gen_class_tag(tag, spec, readtext, writetext) return cls.gen_class_tag(tag, spec)
def mixed_case(cls, identifier): def mixed_case(cls, identifier):
"""helper function to turn token name into mixed case""" """helper function to turn token name into mixed case"""
@@ -334,7 +335,7 @@ class autoxml(type):
return value return value
else: else:
if req == mandatory: if req == mandatory:
raise Error('Mandatory argument not available') raise Error('Mandatory token %s not available' % token)
else: else:
return None return None
@@ -352,20 +353,32 @@ class autoxml(type):
return initialize, decode, encode, format return initialize, decode, encode, format
def gen_class_tag(cls, tag, spec, readtext, writetext): def gen_class_tag(cls, tag, spec):
"""generate a class datatype""" """generate a class datatype"""
name, tag_type, req, path = cls.parse_spec(tag, spec) name, tag_type, req, path = cls.parse_spec(tag, spec)
def make_object(): def make_object():
pass return tag_type.__new__(tag_type)
def init(): def init():
return None return make_object()
def decode(node): def decode(node):
node = getNode(node, path)
if node:
try:
obj = make_object()
obj.decode(node)
return obj
except Error:
raise Error('Type mismatch: DOM cannot be decoded')
else:
if req == mandatory:
raise Error('Mandatory argument not available')
else:
return None return None
def encode(xml, l): def encode(xml, value):
pass pass
def format(l): def format(l):
@@ -374,7 +387,7 @@ class autoxml(type):
return (init, decode, encode, format) return (init, decode, encode, format)
def gen_list_tag(cls, tag, spec, readtext, writetext): def gen_list_tag(cls, tag, spec):
"""generate a list datatype""" """generate a list datatype"""
name, tag_type, req, path = cls.parse_spec(tag, spec) name, tag_type, req, path = cls.parse_spec(tag, spec)
if len(tag_type) != 1: if len(tag_type) != 1:
+9
View File
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?> <?xml version="1.0" encoding="utf-8" standalone="no"?>
<A href="http://www.cs.bilkent.edu.tr/~erayo"> <A href="http://www.cs.bilkent.edu.tr/~erayo">
<Name>Eray Ozkural</Name> <Name>Eray Ozkural</Name>
<Number>868</Number> <Number>868</Number>
@@ -9,4 +10,12 @@
<Project>noatun</Project> <Project>noatun</Project>
<Project>kdevelop</Project> <Project>kdevelop</Project>
</Projects> </Projects>
<OtherInfo>
<BirthDate>18071976</BirthDate>
<Interest>AI</Interest>
<CodesWith>
<Person>Gurer</Person>
<Person>Meren</Person>
</CodesWith>
</OtherInfo>
</A> </A>
+6
View File
@@ -26,6 +26,11 @@ class XmlFileTestCase(unittest.TestCase):
pisi.api.init(False) pisi.api.init(False)
def testMetaClass(self): def testMetaClass(self):
class OtherInfo:
__metaclass__ = xmlfile.autoxml
t_BirthDate = [types.StringType, xmlfile.mandatory]
t_Interest = [types.StringType, xmlfile.mandatory]
class A: class A:
__metaclass__ = xmlfile.autoxml __metaclass__ = xmlfile.autoxml
t_Name = [types.StringType, xmlfile.mandatory] t_Name = [types.StringType, xmlfile.mandatory]
@@ -33,6 +38,7 @@ class XmlFileTestCase(unittest.TestCase):
t_Email = [types.StringType, xmlfile.optional] t_Email = [types.StringType, xmlfile.optional]
a_href = [types.StringType, xmlfile.mandatory] a_href = [types.StringType, xmlfile.mandatory]
t_Projects = [ [types.StringType], xmlfile.mandatory, 'Projects/Project'] t_Projects = [ [types.StringType], xmlfile.mandatory, 'Projects/Project']
t_OtherInfo = [ OtherInfo, xmlfile.optional ]
a = A() a = A()
self.assertEqual(a.href, None) self.assertEqual(a.href, None)
dom = mdom.parse('tests/a.xml') dom = mdom.parse('tests/a.xml')