* we're in business, use XmlFile as a baseclass
now in a somewhat kludgey way, for testing purposes (the automatic addition of XmlFile as a base class doesn't work yet)
This commit is contained in:
+6
-2
@@ -31,7 +31,6 @@ from pisi.util import Checks
|
|||||||
|
|
||||||
__metaclass__ = xmlfile.autoxml
|
__metaclass__ = xmlfile.autoxml
|
||||||
|
|
||||||
|
|
||||||
class Packager:
|
class Packager:
|
||||||
t_Name = [xmlfile.String, xmlfile.mandatory]
|
t_Name = [xmlfile.String, xmlfile.mandatory]
|
||||||
t_Email = [xmlfile.String, xmlfile.mandatory]
|
t_Email = [xmlfile.String, xmlfile.mandatory]
|
||||||
@@ -164,14 +163,19 @@ class Package:
|
|||||||
|
|
||||||
|
|
||||||
class SpecFile(XmlFile):
|
class SpecFile(XmlFile):
|
||||||
|
__metaclass__ = xmlfile.autoxml #needed when we specify a superclass
|
||||||
|
|
||||||
t_Source = [ [Source], xmlfile.mandatory, "Source"]
|
t_Source = [ [Source], xmlfile.mandatory, "Source"]
|
||||||
t_Packages = [ [Package], xmlfile.mandatory, "Package"]
|
t_Packages = [ [Package], xmlfile.mandatory, "Package"]
|
||||||
t_History = [ [Update], xmlfile.mandatory, "Update"]
|
t_History = [ [Update], xmlfile.mandatory, "Update"]
|
||||||
|
|
||||||
|
#def init(self, tag = "PISI"):
|
||||||
|
#ignore tag
|
||||||
|
#XmlFile.__init__(self, tag)
|
||||||
|
|
||||||
def read(self, filename):
|
def read(self, filename):
|
||||||
"""Read PSPEC file"""
|
"""Read PSPEC file"""
|
||||||
|
|
||||||
XmlFile.__init__(self,"PISI")
|
|
||||||
self.readxml(filename)
|
self.readxml(filename)
|
||||||
|
|
||||||
errs = []
|
errs = []
|
||||||
|
|||||||
+14
-6
@@ -47,6 +47,7 @@ import pisi
|
|||||||
from pisi.xmlext import *
|
from pisi.xmlext import *
|
||||||
import pisi.context as ctx
|
import pisi.context as ctx
|
||||||
import pisi.util as util
|
import pisi.util as util
|
||||||
|
import pisi.oo as oo
|
||||||
|
|
||||||
class Error(pisi.Error):
|
class Error(pisi.Error):
|
||||||
pass
|
pass
|
||||||
@@ -122,7 +123,7 @@ class LocalText(object):
|
|||||||
else:
|
else:
|
||||||
errs.append(_("Tag should have at least an English or Turkish version"))
|
errs.append(_("Tag should have at least an English or Turkish version"))
|
||||||
|
|
||||||
class autoxml(type):
|
class autoxml(oo.autosuper):
|
||||||
"""High-level automatic XML transformation interface for xmlfile.
|
"""High-level automatic XML transformation interface for xmlfile.
|
||||||
The idea is to declare a class for each XML tag. Inside the
|
The idea is to declare a class for each XML tag. Inside the
|
||||||
class the tags and attributes nested in the tag are further
|
class the tags and attributes nested in the tag are further
|
||||||
@@ -197,7 +198,7 @@ class autoxml(type):
|
|||||||
"""entry point for metaclass code"""
|
"""entry point for metaclass code"""
|
||||||
#print 'generating class', name
|
#print 'generating class', name
|
||||||
|
|
||||||
# add XmlFile as one of the superclasses, we're smart
|
# add XmlFile as one of the superclasses of cls
|
||||||
bases = list(bases)
|
bases = list(bases)
|
||||||
if not XmlFile in bases:
|
if not XmlFile in bases:
|
||||||
bases.append(XmlFile)
|
bases.append(XmlFile)
|
||||||
@@ -237,10 +238,17 @@ class autoxml(type):
|
|||||||
# generate top-level helper functions
|
# generate top-level helper functions
|
||||||
cls.initializers = inits
|
cls.initializers = inits
|
||||||
def initialize(self, tag = None, spec = None):
|
def initialize(self, tag = None, spec = None):
|
||||||
XmlFile.__init__(self, cls.tag)
|
#FIXME: what the hell is spec? :(
|
||||||
|
if not tag:
|
||||||
|
tag = cls.tag
|
||||||
|
#self.__super.__init__(tag = tag) #FIXME: doesn't work, why? :(
|
||||||
|
super(cls, self).__init__(tag = tag)
|
||||||
for init in self.__class__.initializers:
|
for init in self.__class__.initializers:
|
||||||
init(self)
|
init(self)
|
||||||
|
# init hook
|
||||||
|
if hasattr(self, 'init'):
|
||||||
|
self.init(tag)
|
||||||
|
|
||||||
cls.__init__ = initialize
|
cls.__init__ = initialize
|
||||||
|
|
||||||
cls.decoders = decoders
|
cls.decoders = decoders
|
||||||
@@ -614,8 +622,8 @@ class autoxml(type):
|
|||||||
class XmlFile(object):
|
class XmlFile(object):
|
||||||
"""A class to help reading and writing an XML file"""
|
"""A class to help reading and writing an XML file"""
|
||||||
|
|
||||||
def __init__(self, rootTag):
|
def __init__(self, tag):
|
||||||
self.rootTag = rootTag
|
self.rootTag = tag
|
||||||
self.newDOM()
|
self.newDOM()
|
||||||
|
|
||||||
def newDOM(self):
|
def newDOM(self):
|
||||||
|
|||||||
@@ -29,12 +29,9 @@ def run_all():
|
|||||||
testsources = filter(lambda x:x.endswith('tests.py'), files)
|
testsources = filter(lambda x:x.endswith('tests.py'), files)
|
||||||
for testsource in testsources:
|
for testsource in testsources:
|
||||||
module = __import__(testsource[:len(testsource)-3])
|
module = __import__(testsource[:len(testsource)-3])
|
||||||
#testsuite.add(module.suite)
|
|
||||||
print '\n* Running tests in', testsource
|
print '\n* Running tests in', testsource
|
||||||
run_test_suite(module.suite)
|
run_test_suite(module.suite)
|
||||||
|
|
||||||
#run_test_suite(unittest.TestSuite(tests))
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
locale.setlocale(locale.LC_ALL, '')
|
locale.setlocale(locale.LC_ALL, '')
|
||||||
|
|||||||
@@ -28,13 +28,13 @@ class XmlFileTestCase(unittest.TestCase):
|
|||||||
|
|
||||||
def testMetaClass(self):
|
def testMetaClass(self):
|
||||||
|
|
||||||
class OtherInfo:
|
class OtherInfo(object):
|
||||||
__metaclass__ = xmlfile.autoxml
|
__metaclass__ = xmlfile.autoxml
|
||||||
t_BirthDate = [types.StringType, xmlfile.mandatory]
|
t_BirthDate = [types.StringType, xmlfile.mandatory]
|
||||||
t_Interest = [types.StringType, xmlfile.optional]
|
t_Interest = [types.StringType, xmlfile.optional]
|
||||||
t_CodesWith = [ [types.StringType], xmlfile.optional, 'Person']
|
t_CodesWith = [ [types.StringType], xmlfile.optional, 'Person']
|
||||||
|
|
||||||
class A:
|
class A(xmlfile.XmlFile):
|
||||||
__metaclass__ = xmlfile.autoxml
|
__metaclass__ = xmlfile.autoxml
|
||||||
t_Name = [types.StringType, xmlfile.mandatory]
|
t_Name = [types.StringType, xmlfile.mandatory]
|
||||||
t_Description = [xmlfile.LocalText, xmlfile.mandatory]
|
t_Description = [xmlfile.LocalText, xmlfile.mandatory]
|
||||||
@@ -45,6 +45,7 @@ class XmlFileTestCase(unittest.TestCase):
|
|||||||
t_OtherInfo = [ OtherInfo, xmlfile.optional ]
|
t_OtherInfo = [ OtherInfo, xmlfile.optional ]
|
||||||
|
|
||||||
a = A()
|
a = A()
|
||||||
|
print A.__class__
|
||||||
self.assertEqual(a.href, None)
|
self.assertEqual(a.href, None)
|
||||||
dom = mdom.parse('tests/a.xml')
|
dom = mdom.parse('tests/a.xml')
|
||||||
node = getNode(dom, 'A')
|
node = getNode(dom, 'A')
|
||||||
|
|||||||
Reference in New Issue
Block a user