* eliminate some redundancy with specification of list datatype
now we simply have a component tag for the list item
This commit is contained in:
+15
-20
@@ -356,7 +356,7 @@ class autoxml(type):
|
|||||||
|
|
||||||
def decode(node):
|
def decode(node):
|
||||||
"""decode from DOM node, the value, watching the spec"""
|
"""decode from DOM node, the value, watching the spec"""
|
||||||
text = readtext(node, tagpath)
|
text = readtext(node, token)
|
||||||
print 'read text ', text
|
print 'read text ', text
|
||||||
if text:
|
if text:
|
||||||
try:
|
try:
|
||||||
@@ -373,9 +373,8 @@ class autoxml(type):
|
|||||||
def encode(xml, value):
|
def encode(xml, value):
|
||||||
"""encode given value inside DOM node"""
|
"""encode given value inside DOM node"""
|
||||||
if value:
|
if value:
|
||||||
head, last = cls.tagpath_head_last(tagpath)
|
node = createnode(xml, token)
|
||||||
node = createnode(xml, last)
|
writetext(xml, node, token, str(value))
|
||||||
writetext(xml, node, head, str(value))
|
|
||||||
return node
|
return node
|
||||||
else:
|
else:
|
||||||
if req == mandatory:
|
if req == mandatory:
|
||||||
@@ -398,7 +397,7 @@ class autoxml(type):
|
|||||||
return make_object()
|
return make_object()
|
||||||
|
|
||||||
def decode(node):
|
def decode(node):
|
||||||
node = getNode(node, path)
|
node = getNode(node, tag)
|
||||||
if node:
|
if node:
|
||||||
try:
|
try:
|
||||||
obj = make_object()
|
obj = make_object()
|
||||||
@@ -415,7 +414,9 @@ class autoxml(type):
|
|||||||
def encode(xml, obj):
|
def encode(xml, obj):
|
||||||
if obj:
|
if obj:
|
||||||
try:
|
try:
|
||||||
return obj.encode(xml)
|
node = obj.encode(xml)
|
||||||
|
#FIXME: change node's tag?
|
||||||
|
return
|
||||||
except Error:
|
except Error:
|
||||||
raise Error('Object cannot be encoded')
|
raise Error('Object cannot be encoded')
|
||||||
else:
|
else:
|
||||||
@@ -428,21 +429,14 @@ class autoxml(type):
|
|||||||
return (init, decode, encode, format)
|
return (init, decode, encode, format)
|
||||||
|
|
||||||
def gen_list_tag(cls, tag, spec):
|
def gen_list_tag(cls, tag, spec):
|
||||||
"""generate a list datatype"""
|
"""generate a list datatype. stores comps in tag/comp_tag"""
|
||||||
name, tag_type, req, path = cls.parse_spec(tag, spec)
|
name, tag_type, req, comp_tag = cls.parse_spec(tag, spec)
|
||||||
head, last = cls.tagpath_head_last(path)
|
#head, last = cls.tagpath_head_last(path)
|
||||||
|
|
||||||
if len(tag_type) != 1:
|
if len(tag_type) != 1:
|
||||||
raise Error('List type must contain only one element')
|
raise Error('List type must contain only one element')
|
||||||
|
|
||||||
# def readtext(node, tagpath):
|
x = cls.gen_tag(comp_tag, [tag_type[0], mandatory])
|
||||||
# print 'shit', node, tagpath
|
|
||||||
# return getNodeText(node)
|
|
||||||
# def createnode(xml, tag):
|
|
||||||
# return xml.newNode(last)
|
|
||||||
# def writetext(node, text):
|
|
||||||
# node.addText(node, text)
|
|
||||||
x = cls.gen_tag(last, [tag_type[0], mandatory])
|
|
||||||
(init_item, decode_item, encode_item, format_item) = x
|
(init_item, decode_item, encode_item, format_item) = x
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
@@ -450,12 +444,13 @@ class autoxml(type):
|
|||||||
|
|
||||||
def decode(node):
|
def decode(node):
|
||||||
l = []
|
l = []
|
||||||
nodes = getAllNodes(node, path)
|
nodes = getAllNodes(node, tag + '/' + comp_tag)
|
||||||
|
print node, tag + '/' + comp_tag
|
||||||
print 'F U', nodes
|
print 'F U', nodes
|
||||||
if len(nodes) is 0 and req is mandatory:
|
if len(nodes) is 0 and req is mandatory:
|
||||||
raise Error('Mandatory list empty')
|
raise Error('Mandatory list empty')
|
||||||
for node in nodes:
|
for node in nodes:
|
||||||
dummy = node.ownerDocument.createElement(last)
|
dummy = node.ownerDocument.createElement("Dummy")
|
||||||
dummy.appendChild(node)
|
dummy.appendChild(node)
|
||||||
l.append(decode_item(dummy))
|
l.append(decode_item(dummy))
|
||||||
return l
|
return l
|
||||||
@@ -465,7 +460,7 @@ class autoxml(type):
|
|||||||
if len(l) > 0:
|
if len(l) > 0:
|
||||||
for item in l:
|
for item in l:
|
||||||
item_node = encode_item(xml, item)
|
item_node = encode_item(xml, item)
|
||||||
xml.addNodeUnder(dummy, last, item_node)
|
xml.addNodeUnder(dummy, comp_tag, item_node)
|
||||||
return getNode(dummy, "Dummy")
|
return getNode(dummy, "Dummy")
|
||||||
else:
|
else:
|
||||||
if req is mandatory:
|
if req is mandatory:
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ class XmlFileTestCase(unittest.TestCase):
|
|||||||
__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, 'CodesWith/Person']
|
t_CodesWith = [ [types.StringType], xmlfile.optional, 'Person']
|
||||||
|
|
||||||
class A:
|
class A:
|
||||||
__metaclass__ = xmlfile.autoxml
|
__metaclass__ = xmlfile.autoxml
|
||||||
@@ -40,7 +40,7 @@ class XmlFileTestCase(unittest.TestCase):
|
|||||||
t_Number = [types.IntType, xmlfile.optional]
|
t_Number = [types.IntType, xmlfile.optional]
|
||||||
t_Email = [types.StringType, xmlfile.optional]
|
t_Email = [types.StringType, xmlfile.optional]
|
||||||
a_href = [types.StringType, xmlfile.mandatory]
|
a_href = [types.StringType, xmlfile.mandatory]
|
||||||
t_Projects = [ [types.StringType], xmlfile.mandatory, 'Projects/Project']
|
t_Projects = [ [types.StringType], xmlfile.mandatory, 'Project']
|
||||||
t_OtherInfo = [ OtherInfo, xmlfile.optional ]
|
t_OtherInfo = [ OtherInfo, xmlfile.optional ]
|
||||||
|
|
||||||
a = A()
|
a = A()
|
||||||
|
|||||||
Reference in New Issue
Block a user