diff --git a/pisi/specfile.py b/pisi/specfile.py index f33972cd..4ba8b0bf 100644 --- a/pisi/specfile.py +++ b/pisi/specfile.py @@ -21,13 +21,17 @@ from os.path import basename # pisi modules -from xmlext import * -from xmlfile import XmlFile -from ui import ui -from dependency import DepInfo -from util import Checks +from pisi.xmlext import * +import pisi.xmlfile as xmlfile +from pisi.xmlfile import XmlFile +from pisi.ui import ui +from pisi.dependency import DepInfo +from pisi.util import Checks +#class Packager: +# __metaclass__ = xmlfile.autoxml + class PackagerInfo: def __init__(self, node = None): if node: @@ -50,7 +54,7 @@ class PackagerInfo: return err.list def __str__(self): - s = " ".join(self.name, self.email) + s = "%s <%s>" % (self.name, self.email) return s class AdditionalFileInfo: diff --git a/pisi/xmlfile.py b/pisi/xmlfile.py index 94feaac0..fa0ee2a8 100644 --- a/pisi/xmlfile.py +++ b/pisi/xmlfile.py @@ -28,6 +28,55 @@ import codecs from xmlext import * +import pisi + +class Error(pisi.Error): + pass + +import types + +mandatory, optional = range(2) + +class autoxml(type): + + def __init__(cls, name, bases, dict): + + # add XmlFile as one of the superclasses + bases = list(bases) + if not XmlFile in bases: + bases.append(XmlFile) + + # standard initialization + super(autoxml, cls).__init__(name, bases, dict) + + # initialize class attribute __xml_tags + setattr(cls, '__xml_tags', []) + + # find variables that start with x_ + for var in dict: + if var.startswith('t_'): + print 'xml tag', var + autoxml.gen_tag(cls, var[2:]) + + def gen_tag(cls, tag): + # generate readers and writers for the tag + val = getattr(cls, 't_' + tag) + tag_type = val[0] + if type(tag_type) == type(type): + if val[0] == types.IntType: + autoxml.gen_int_tag(cls, tag, val) + #gen_tag = staticmethod(gen_tag) + + def mixed_case(cls, identifier): + identifier_p = identifier[0].lower() + identifier[1:] + return identifier_p + + def gen_int_tag(cls, tag, val): + print tag, val + name = cls.mixed_case(tag) + def read_int(self): + self.name = getNodeText(getNode(node, tag)) + setattr(cls, 'decode' + tag, read_int) class XmlFile(object): """A class for retrieving information from an XML file""" diff --git a/tests/run.py b/tests/run.py index 6bc1f864..229da31e 100755 --- a/tests/run.py +++ b/tests/run.py @@ -21,6 +21,7 @@ runTestSuite = lambda(x): unittest.TextTestRunner(verbosity=2).run(x) def run_all(): import utiltests + import xmlfile import specfiletests import metadatatests import constantstests @@ -37,7 +38,7 @@ def run_all(): alltests = unittest.TestSuite(( utiltests.suite, - specfiletests.suite, + xmlfiletests.suite, specfiletests.suite, metadatatests.suite, constantstests.suite, diff --git a/tests/specfiletests.py b/tests/specfiletests.py index 5b233df2..58d644d3 100644 --- a/tests/specfiletests.py +++ b/tests/specfiletests.py @@ -76,4 +76,5 @@ class SpecFileTestCase(unittest.TestCase): self.spec.read("tests/popt/pspec.xml") self.spec.write(os.path.join(config.tmp_dir(), 'popt-copy.pspec.xml')) + suite = unittest.makeSuite(SpecFileTestCase) diff --git a/tests/xmlfiletests.py b/tests/xmlfiletests.py new file mode 100644 index 00000000..db0e15bf --- /dev/null +++ b/tests/xmlfiletests.py @@ -0,0 +1,28 @@ +# Copyright (C) 2005, TUBITAK/UEKAE +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation; either version 2 of the License, or (at your option) +# any later version. +# +# Please read the COPYING file. +# + +import unittest +import os + +from pisi import xmlfile +from pisi.config import config +import pisi.util as util +import types + +class XmlFileTestCase(unittest.TestCase): + def setUp(self): + pass + + def testMetaClass(self): + class A: + __metaclass__ = xmlfile.autoxml + t_Number = [types.IntType, xmlfile.mandatory] + +suite = unittest.makeSuite(XmlFileTestCase)