* autoxml: start implementing output with formatter
* minor improvements
This commit is contained in:
+4
-4
@@ -10,10 +10,10 @@
|
|||||||
# Please read the COPYING file.
|
# Please read the COPYING file.
|
||||||
#
|
#
|
||||||
|
|
||||||
# Yet another Pisi module for fetching files from various sources. Of
|
"""Yet another Pisi module for fetching files from various sources. Of
|
||||||
# course, this is not limited to just fetching source files. We fetch
|
course, this is not limited to just fetching source files. We fetch
|
||||||
# all kinds of things: source tarballs, index files, packages, and God
|
all kinds of things: source tarballs, index files, packages, and God
|
||||||
# knows what.
|
knows what."""
|
||||||
|
|
||||||
# python standard library modules
|
# python standard library modules
|
||||||
import urllib2
|
import urllib2
|
||||||
|
|||||||
@@ -23,3 +23,6 @@ class autosuper(type):
|
|||||||
|
|
||||||
class autosuprop(autosuper, autoprop):
|
class autosuprop(autosuper, autoprop):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
class Struct:
|
||||||
|
def __init__(self, **entries): self.__dict__.update(entries)
|
||||||
|
|||||||
+34
-41
@@ -47,7 +47,6 @@ import pisi
|
|||||||
from pisi.xmlext import *
|
from pisi.xmlext import *
|
||||||
import pisi.context as ctx
|
import pisi.context as ctx
|
||||||
|
|
||||||
|
|
||||||
class Error(pisi.Error):
|
class Error(pisi.Error):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -99,28 +98,18 @@ class LocalText(object):
|
|||||||
newnode.appendChild(newtext)
|
newnode.appendChild(newtext)
|
||||||
node.appendChild(newnode)
|
node.appendChild(newnode)
|
||||||
|
|
||||||
def format(self, errs):
|
def format(self, f, errs):
|
||||||
L = locale.getlocale()[0][0:2] # try to read language, pathetic isn't it?
|
L = locale.getlocale()[0][0:2] # try to read language, pathetic isn't it?
|
||||||
if self.locs.has_key(L):
|
if self.locs.has_key(L):
|
||||||
return self.locs[L]
|
f.add_flowing_data(self.locs[L])
|
||||||
elif self.locs.has_key('en'):
|
elif self.locs.has_key('en'):
|
||||||
# fallback to English, blah
|
# fallback to English, blah
|
||||||
return self.locs['en']
|
f.add_flowing_data(self.locs['en'])
|
||||||
elif self.locs.has_key('tr'):
|
elif self.locs.has_key('tr'):
|
||||||
# fallback to Turkish
|
# fallback to Turkish
|
||||||
return self.locs['tr']
|
f.add_flowing_data(self.locs['tr'])
|
||||||
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"))
|
||||||
return ''
|
|
||||||
|
|
||||||
# FIXME: use something like the below to return a default
|
|
||||||
#L = language
|
|
||||||
#if not self.locs.has_key(L):
|
|
||||||
# L = 'en'
|
|
||||||
#if not self.locs.has_key(L):
|
|
||||||
# #errs.append("Tag '%s' should have an English version\n" % d[2])
|
|
||||||
# return ""
|
|
||||||
#retur
|
|
||||||
|
|
||||||
class autoxml(type):
|
class autoxml(type):
|
||||||
"""High-level automatic XML transformation interface for xmlfile.
|
"""High-level automatic XML transformation interface for xmlfile.
|
||||||
@@ -192,6 +181,7 @@ class autoxml(type):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def __init__(cls, name, bases, dict):
|
def __init__(cls, name, bases, dict):
|
||||||
"""entry point for metaclass code"""
|
"""entry point for metaclass code"""
|
||||||
#print 'generating class', name
|
#print 'generating class', name
|
||||||
@@ -252,14 +242,22 @@ class autoxml(type):
|
|||||||
cls.encode = encode
|
cls.encode = encode
|
||||||
|
|
||||||
cls.formatters = formatters
|
cls.formatters = formatters
|
||||||
def format(self, errs):
|
def format(self, f, errs):
|
||||||
string = ''
|
|
||||||
for formatter in self.__class__.formatters:
|
for formatter in self.__class__.formatters:
|
||||||
string += formatter(self, errs)
|
formatter(self, f, errs)
|
||||||
return string
|
|
||||||
cls.format = format
|
cls.format = format
|
||||||
if not dict.has_key('__str__'):
|
def print_text(self):
|
||||||
cls.__str__ = format
|
import formatter
|
||||||
|
w = formatter.DumbWriter() # plain text
|
||||||
|
f = formatter.AbstractFormatter(w)
|
||||||
|
errs = []
|
||||||
|
self.format(f, errs)
|
||||||
|
if errs:
|
||||||
|
for x in errs:
|
||||||
|
ctx.ui.warning(x)
|
||||||
|
cls.print_text = print_text
|
||||||
|
#if not dict.has_key('__str__'):
|
||||||
|
# cls.__str__ = str
|
||||||
|
|
||||||
def gen_attr_member(cls, attr):
|
def gen_attr_member(cls, attr):
|
||||||
"""generate readers and writers for an attribute member"""
|
"""generate readers and writers for an attribute member"""
|
||||||
@@ -328,14 +326,13 @@ class autoxml(type):
|
|||||||
value = None
|
value = None
|
||||||
encode_a(xml, node, value, errs)
|
encode_a(xml, node, value, errs)
|
||||||
|
|
||||||
def format(self, errs):
|
def format(self, f, errs):
|
||||||
if hasattr(self, name):
|
if hasattr(self, name):
|
||||||
value = getattr(self,name)
|
value = getattr(self,name)
|
||||||
return '%s: %s\n' % (token, format_a(value, errs))
|
f.add_literal_data('%s: %s\n' % (token, format_a(value, f, errs)))
|
||||||
else:
|
else:
|
||||||
if req == mandatory:
|
if req == mandatory:
|
||||||
raise Error(_('Mandatory variable %s not available') % name)
|
raise Error(_('Mandatory variable %s not available') % name)
|
||||||
return ''
|
|
||||||
|
|
||||||
return (init, decode, encode, format)
|
return (init, decode, encode, format)
|
||||||
|
|
||||||
@@ -405,9 +402,9 @@ class autoxml(type):
|
|||||||
if req == mandatory:
|
if req == mandatory:
|
||||||
errs.append(_('Mandatory argument not available'))
|
errs.append(_('Mandatory argument not available'))
|
||||||
|
|
||||||
def format(value, errs):
|
def format(value, f, errs):
|
||||||
"""format value for pretty printing"""
|
"""format value for pretty printing"""
|
||||||
return unicode(value)
|
f.add_literal_data(unicode(value))
|
||||||
|
|
||||||
return initialize, decode, encode, format
|
return initialize, decode, encode, format
|
||||||
|
|
||||||
@@ -452,14 +449,12 @@ class autoxml(type):
|
|||||||
if req == mandatory:
|
if req == mandatory:
|
||||||
errs.append(_('Mandatory argument not available'))
|
errs.append(_('Mandatory argument not available'))
|
||||||
|
|
||||||
def format(obj, errs):
|
def format(obj, f, errs):
|
||||||
try:
|
try:
|
||||||
return obj.format(errs)
|
obj.format(f, errs)
|
||||||
except Error:
|
except Error:
|
||||||
if req == mandatory:
|
if req == mandatory:
|
||||||
errs.append(_('Mandatory argument not available'))
|
errs.append(_('Mandatory argument not available'))
|
||||||
else:
|
|
||||||
return ''
|
|
||||||
return (init, decode, encode, format)
|
return (init, decode, encode, format)
|
||||||
|
|
||||||
def gen_list_tag(cls, tag, spec):
|
def gen_list_tag(cls, tag, spec):
|
||||||
@@ -498,15 +493,14 @@ class autoxml(type):
|
|||||||
if req is mandatory:
|
if req is mandatory:
|
||||||
errs.append(_('Mandatory list empty'))
|
errs.append(_('Mandatory list empty'))
|
||||||
|
|
||||||
def format(l, errs):
|
def format(l, f, errs):
|
||||||
#print 'format:', name
|
# indent here
|
||||||
s = ''
|
|
||||||
for ix in range(len(l)):
|
for ix in range(len(l)):
|
||||||
s += str(ix+1) + ': ' + format_item(l[ix], errs)
|
f.add_flowing_data(str(ix+1) + ': ')
|
||||||
|
format_item(l[ix], f, errs)
|
||||||
if ix != len(l)-1:
|
if ix != len(l)-1:
|
||||||
s += ', '
|
f.add_flowing_data(', ')
|
||||||
return s
|
|
||||||
|
|
||||||
return (init, decode, encode, format)
|
return (init, decode, encode, format)
|
||||||
|
|
||||||
def gen_insetclass_tag(cls, tag, spec):
|
def gen_insetclass_tag(cls, tag, spec):
|
||||||
@@ -551,14 +545,13 @@ class autoxml(type):
|
|||||||
if req == mandatory:
|
if req == mandatory:
|
||||||
errs.append(_('Mandatory argument not available'))
|
errs.append(_('Mandatory argument not available'))
|
||||||
|
|
||||||
def format(obj, errs):
|
def format(obj, f, errs):
|
||||||
try:
|
try:
|
||||||
return obj.format(errs)
|
obj.format(f, errs)
|
||||||
except Error:
|
except Error:
|
||||||
if req == mandatory:
|
if req == mandatory:
|
||||||
errs.append(_('Mandatory argument not available'))
|
errs.append(_('Mandatory argument not available'))
|
||||||
else:
|
|
||||||
return ''
|
|
||||||
return (init, decode, encode, format)
|
return (init, decode, encode, format)
|
||||||
|
|
||||||
basic_cons_map = {
|
basic_cons_map = {
|
||||||
|
|||||||
@@ -55,8 +55,9 @@ class XmlFileTestCase(unittest.TestCase):
|
|||||||
self.assert_(a.href.startswith('http'))
|
self.assert_(a.href.startswith('http'))
|
||||||
self.assertEqual(a.number, 868)
|
self.assertEqual(a.number, 868)
|
||||||
self.assertEqual(a.name, 'Eray Ozkural')
|
self.assertEqual(a.name, 'Eray Ozkural')
|
||||||
string = a.format(errs)
|
#string = a.format(errs)
|
||||||
print '*', string
|
#print '*', string
|
||||||
|
a.print_text()
|
||||||
#self.assert_(string.startswith('Name'))
|
#self.assert_(string.startswith('Name'))
|
||||||
xml = xmlfile.XmlFile('A')
|
xml = xmlfile.XmlFile('A')
|
||||||
xml.newDOM()
|
xml.newDOM()
|
||||||
@@ -75,6 +76,6 @@ class XmlFileTestCase(unittest.TestCase):
|
|||||||
a2.encode(xml, xml.rootNode(), errs3)
|
a2.encode(xml, xml.rootNode(), errs3)
|
||||||
print 'errs3', errs3
|
print 'errs3', errs3
|
||||||
xml.writexml('/tmp/a2.xml')
|
xml.writexml('/tmp/a2.xml')
|
||||||
string = a2.format(errs3)
|
#string = a2.format(errs3)
|
||||||
|
|
||||||
suite = unittest.makeSuite(XmlFileTestCase)
|
suite = unittest.makeSuite(XmlFileTestCase)
|
||||||
|
|||||||
Reference in New Issue
Block a user