Package amplee :: Package atompub :: Package member :: Module helper
[hide private]
[frames] | no frames]

Source Code for Module amplee.atompub.member.helper

  1  # -*- coding: utf-8 -*- 
  2   
  3  __doc__ = """ 
  4  Member classes have a common set of functionnalities that this module try 
  5  to centralize to make the code easier to ead and maintain 
  6  """ 
  7   
  8  from bridge import Element, Attribute 
  9  from bridge.common import ATOM10_PREFIX, ATOMPUB_PREFIX, XML_PREFIX, XML_NS, \ 
 10       ATOM10_NS, ATOMPUB_NS, XHTML1_NS, XHTML1_PREFIX, atom_as_attr, atom_as_list, \ 
 11       atom_attribute_of_element 
 12   
 13  from amplee.utils import generate_uuid_uri, get_isodate 
 14  from amplee.atompub.member import EntryMember 
 15   
16 -class MemberHelper(object):
17 - def __init__(self, collection):
18 self.collection = collection 19 self.entry = None
20
21 - def initiate(self, id):
22 """ 23 Creates a default Atom entry document with its id, published, updated and 24 edited children set. If the collection has a xml_base attribute set it will 25 also set it. 26 27 Keyword argument: 28 id -- the identifier of the entry (unicode) 29 """ 30 entry = Element(u'entry', prefix=ATOM10_PREFIX, namespace=ATOM10_NS) 31 Element(u'id', content=id, prefix=ATOM10_PREFIX, namespace=ATOM10_NS, parent=entry) 32 isodate = get_isodate() 33 Element(u'published', content=isodate, prefix=ATOM10_PREFIX, 34 namespace=ATOM10_NS, parent=entry) 35 Element(u'updated', content=isodate, prefix=ATOM10_PREFIX, 36 namespace=ATOM10_NS, parent=entry) 37 Element(u'edited', content=isodate, prefix=ATOMPUB_PREFIX, 38 namespace=ATOMPUB_NS, parent=entry) 39 40 if self.collection.xml_base: 41 Attribute(u'base', self.collection.xml_base, prefix=XML_PREFIX, 42 namespace=XML_NS, parent=entry) 43 44 self.entry = entry
45
46 - def add_element(self, name, content=None, attributes=None, 47 prefix=None, ns=None, parent=None):
48 """ 49 Add a child to an element 50 51 Keyword arguments: 52 name -- name of the element 53 content -- content of the element 54 attributes -- dictionnary of attributes to set 55 prefix -- XML prefix (if not provided defaults to self.entry.xml_prefix) 56 ns -- XML namespace (if not provided defaults to self.entry.xml_ns) 57 parent -- if provided the child will be attached to parent, 58 otherwiseparent will default to self.entry 59 """ 60 if not parent: 61 parent = self.entry 62 if not prefix: 63 prefix = parent.xml_prefix 64 if not ns: 65 ns = parent.xml_ns 66 return Element(name, content=content, attributes=attributes, 67 prefix=prefix, namespace=ns, parent=parent)
68 69
70 - def copy_element(self, name, source=None, destination=None, ns=None):
71 """ 72 Copy an element into another element 73 74 Keyword arguments: 75 name -- name of the element to copy 76 source -- element containing a child 'name' 77 destination -- element to which attached the copy 78 ns -- XML namespace to match (if not provided defaults to self.entry.xml_ns) 79 """ 80 if not destination: 81 destination = self.entry 82 if not ns: 83 ns = destination.xml_ns 84 if source.has_child(name, ns): 85 copy = source.get_child(name, ns).clone() 86 copy.xml_parent = destination 87 destination.xml_children.append(copy) 88 setattr(destination, name, copy) 89 return copy 90 91 return None
92
93 - def copy_elements(self, name, source=None, destination=None, ns=None):
94 """ 95 Copy a list of elements into another element 96 97 Keyword arguments: 98 name -- name of the element to copy 99 source -- element containing a child 'name' 100 destination -- element to which attached the copy 101 ns -- XML namespace to match (if not provided defaults to self.entry.xml_ns) 102 """ 103 if not destination: 104 destination = self.entry 105 if not ns: 106 ns = destination.xml_ns 107 handle = getattr(destination, name, []) 108 children = source.get_children(name, ns) 109 for child in children: 110 copy = child.clone() 111 copy.xml_parent = destination 112 destination.xml_children.append(copy) 113 handle.append(copy) 114 return copy 115 setattr(destination, name, handle)
116