From d064e8a94018202c79c97a4dceea87ebda7b5d65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Wed, 21 Sep 2005 23:52:45 +0000 Subject: [PATCH] * class'in decode'unu da implement et, testini de yap * birtakim temizlikler yap, down stupidity * zibidi --- pisi/xmlfile.py | 37 +++++++++++++++++++++++++------------ tests/a.xml | 9 +++++++++ tests/xmlfiletests.py | 6 ++++++ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/pisi/xmlfile.py b/pisi/xmlfile.py index f394c2cd..04c8213b 100644 --- a/pisi/xmlfile.py +++ b/pisi/xmlfile.py @@ -163,6 +163,7 @@ class autoxml(type): """ def __init__(cls, name, bases, dict): + print 'generating class', name # add XmlFile as one of the superclasses, we're smart bases = list(bases) @@ -285,12 +286,12 @@ class autoxml(type): def gen_tag(cls, tag, spec, readtext, writetext): """generate readers and writers for the tag""" 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) - elif type(tag_type) == types.ListType: - return cls.gen_list_tag(tag, spec, readtext, writetext) - elif type(tag_type) == types.ClassType: - return cls.gen_class_tag(tag, spec, readtext, writetext) + elif type(tag_type) is types.ListType: + return cls.gen_list_tag(tag, spec) + elif type(tag_type) is autoxml or type(tag_type) is types.ClassType: + return cls.gen_class_tag(tag, spec) def mixed_case(cls, identifier): """helper function to turn token name into mixed case""" @@ -334,7 +335,7 @@ class autoxml(type): return value else: if req == mandatory: - raise Error('Mandatory argument not available') + raise Error('Mandatory token %s not available' % token) else: return None @@ -352,20 +353,32 @@ class autoxml(type): 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""" name, tag_type, req, path = cls.parse_spec(tag, spec) def make_object(): - pass + return tag_type.__new__(tag_type) def init(): - return None + return make_object() def decode(node): - return None + 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 - def encode(xml, l): + def encode(xml, value): pass def format(l): @@ -374,7 +387,7 @@ class autoxml(type): 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""" name, tag_type, req, path = cls.parse_spec(tag, spec) if len(tag_type) != 1: diff --git a/tests/a.xml b/tests/a.xml index 4245a7d5..b4cfc2cc 100644 --- a/tests/a.xml +++ b/tests/a.xml @@ -1,4 +1,5 @@ + Eray Ozkural 868 @@ -9,4 +10,12 @@ noatun kdevelop + + 18071976 + AI + + Gurer + Meren + + diff --git a/tests/xmlfiletests.py b/tests/xmlfiletests.py index 0d023c67..37662bd1 100644 --- a/tests/xmlfiletests.py +++ b/tests/xmlfiletests.py @@ -26,6 +26,11 @@ class XmlFileTestCase(unittest.TestCase): pisi.api.init(False) def testMetaClass(self): + class OtherInfo: + __metaclass__ = xmlfile.autoxml + t_BirthDate = [types.StringType, xmlfile.mandatory] + t_Interest = [types.StringType, xmlfile.mandatory] + class A: __metaclass__ = xmlfile.autoxml t_Name = [types.StringType, xmlfile.mandatory] @@ -33,6 +38,7 @@ class XmlFileTestCase(unittest.TestCase): t_Email = [types.StringType, xmlfile.optional] a_href = [types.StringType, xmlfile.mandatory] t_Projects = [ [types.StringType], xmlfile.mandatory, 'Projects/Project'] + t_OtherInfo = [ OtherInfo, xmlfile.optional ] a = A() self.assertEqual(a.href, None) dom = mdom.parse('tests/a.xml')