* start writing an auto xml pickler

This commit is contained in:
Eray Özkural
2005-08-27 19:43:02 +00:00
parent 8e65a1fb74
commit 921567ced1
5 changed files with 90 additions and 7 deletions
+10 -6
View File
@@ -21,13 +21,17 @@
from os.path import basename from os.path import basename
# pisi modules # pisi modules
from xmlext import * from pisi.xmlext import *
from xmlfile import XmlFile import pisi.xmlfile as xmlfile
from ui import ui from pisi.xmlfile import XmlFile
from dependency import DepInfo from pisi.ui import ui
from util import Checks from pisi.dependency import DepInfo
from pisi.util import Checks
#class Packager:
# __metaclass__ = xmlfile.autoxml
class PackagerInfo: class PackagerInfo:
def __init__(self, node = None): def __init__(self, node = None):
if node: if node:
@@ -50,7 +54,7 @@ class PackagerInfo:
return err.list return err.list
def __str__(self): def __str__(self):
s = " ".join(self.name, self.email) s = "%s <%s>" % (self.name, self.email)
return s return s
class AdditionalFileInfo: class AdditionalFileInfo:
+49
View File
@@ -28,6 +28,55 @@ import codecs
from xmlext import * 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): class XmlFile(object):
"""A class for retrieving information from an XML file""" """A class for retrieving information from an XML file"""
+2 -1
View File
@@ -21,6 +21,7 @@ runTestSuite = lambda(x): unittest.TextTestRunner(verbosity=2).run(x)
def run_all(): def run_all():
import utiltests import utiltests
import xmlfile
import specfiletests import specfiletests
import metadatatests import metadatatests
import constantstests import constantstests
@@ -37,7 +38,7 @@ def run_all():
alltests = unittest.TestSuite(( alltests = unittest.TestSuite((
utiltests.suite, utiltests.suite,
specfiletests.suite, xmlfiletests.suite,
specfiletests.suite, specfiletests.suite,
metadatatests.suite, metadatatests.suite,
constantstests.suite, constantstests.suite,
+1
View File
@@ -76,4 +76,5 @@ class SpecFileTestCase(unittest.TestCase):
self.spec.read("tests/popt/pspec.xml") self.spec.read("tests/popt/pspec.xml")
self.spec.write(os.path.join(config.tmp_dir(), 'popt-copy.pspec.xml')) self.spec.write(os.path.join(config.tmp_dir(), 'popt-copy.pspec.xml'))
suite = unittest.makeSuite(SpecFileTestCase) suite = unittest.makeSuite(SpecFileTestCase)
+28
View File
@@ -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)