Advertisement
s243a

pearl.py

Aug 9th, 2022 (edited)
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.08 KB | None | 0 0
  1. from lxml import etree, objectify
  2. from lxml.objectify import Element as Element
  3. from lxml.objectify import SubElement as SubElement
  4. from lxml.objectify import StringElement as StringElement
  5. from lxml.objectify import ObjectifiedElement as ObjectifiedElement
  6. from lxml.etree import ElementBase as ElementBase
  7.  
  8.  
  9. from datetime import date
  10. from datetime import time
  11. iff=lambda a,b,c: b if a else c
  12. NSMAP  = {
  13.     "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
  14.     "rdfs" : "http://www.w3.org/2000/01/rdf-schema#",
  15.     "dcterms" : "http://purl.org/dc/elements/1.1/",
  16.     "foaf" : "http://xmlns.com/foaf/0.1/",
  17.     "owl" : "http://www.w3.org/2002/07/owl#",
  18.     "sioc" : "http://rdfs.org/sioc/ns#",
  19.     "pt" : "http://www.pearltrees.com/rdf/0.1/#"
  20.     }
  21. DEFAULTS = {
  22.   'privacy': (lambda x : 1),
  23.   'lastUpdate' : lambda : "" + date.today() + "T" + time.now()
  24. }
  25. iff=lambda a,b,c: b if a else c  
  26.  
  27.  
  28. # See: Custom Claass Lookup
  29. #      https://lxml.de/element_classes.html
  30. class MyLookup(etree.CustomElementClassLookup):
  31.   def lookup(self, node_type, document, namespace, name):
  32.     print("namespace="+str(namespace))
  33.     if namespace == 'http://www.pearltrees.com/rdf/0.1/#':
  34.       if name == "Tree":
  35.         return PT_TreeLXML
  36.     else:
  37.      return None  # pass on to (default) fallback
  38.  
  39. parser = etree.XMLParser()
  40. parser.set_element_class_lookup(MyLookup())
  41. root_str='''<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  42.         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  43.         xmlns:foaf="http://xmlns.com/foaf/0.1/"
  44.         xmlns:dcterms="http://purl.org/dc/elements/1.1/"
  45.         xmlns:pt="http://www.pearltrees.com/rdf/0.1/#"></rdf:RDF>'''
  46. root=etree.fromstring(root_str, parser)
  47. def asgn(name,val,default=None,defaults=None):
  48.   if default is None:
  49.     if defaults is None:
  50.       defaults=DEFAULTS
  51.     default=defaults[name]
  52.   try:
  53.     iff(name in val,val[name],default)
  54.   except:
  55.     if val is None:
  56.       val=default
  57.   return val
  58.  
  59. class PT_Obj(object):
  60.   def __add_item_helper__(tag,prefix=None,nsmap=NSMAP,ns=None):
  61.     if ns is None and prefix is not None:
  62.       ns=nsmap[prefix]
  63.     if ns is not None:
  64.       tag="{" + ns + "}" + tag
  65.     #value=asgn(tag,value)
  66.     return tag
  67.   #This is a replacement for the python new clas
  68.   # See: https://www.geeksforgeeks.org/__new__-in-python/  
  69.   def _new2(cls, *args,**kw):
  70.     self.__init2__
  71.     return self
  72.   #This is a replacement for the python new clas
  73.   # See: https://www.geeksforgeeks.org/__new__-in-python/    
  74.   def __init2__(self, *args,**kw):
  75.     pass
  76.   def _add_item(self,value,nsmap=NSMAP,prefix=None):
  77.     pass
  78.   def _remove_item(self,about,text=None):
  79.     pass
  80.   def _remove_item(self,about,text=None):
  81.     pass
  82.   def _add_prop(self,prop,value=None,prefix=None,nsmap=NSMAP,ns=None):
  83.     self.setItem(
  84.       self._add_item(prop,value,nsmap))
  85.   def _setItem(self,item,value):
  86.     pass
  87.   def _remove_prop():  
  88.     pass
  89. # - In the orginal pearltrees a tree once was analoges to a mind map for a given
  90. # topic. (e.g. ...)
  91. # - In PT2.0 and on the mindmap concept was replaced by a grid like strucures
  92. # and each node of the tree was replaced by a buch of cards.
  93. # - In either case the tree is the principle web page for each topic containing a number
  94. # of sub items. One major difference is that later version of pearltree lose much of the
  95. # tree struture  def __init__(self,**kw):
  96. #<pt:Tree rdf:about="url_for_some_tree">
  97. #   <dcterms:title><![CDATA[tree_title]]></dcterms:title>
  98. #   <dcterms:creator rdf:resource="https://www.pearltrees.com/somebody#sioc" />
  99. #   <pt:treeId>tree_id</pt:treeId>
  100. #   <pt:assoId>tree_id</pt:assoId>
  101. #   <pt:lastUpdate>2012-05-23T20:00:32</pt:lastUpdate>
  102. #   <pt:privacy>0</pt:privacy>
  103. #</pt:Tree>
  104.  
  105. class PT_Tree(PT_Obj):
  106.   def __init__(self,**kw):
  107.     self.__init2__
  108.   def _new2(cls,*args, **kw):
  109.     self.__init2__(*args,**kw)
  110.     return self
  111.   #This is a replacement for the python new clas
  112.   # See: https://www.geeksforgeeks.org/__new__-in-python/    
  113.   def __init2__(self,*args,**kw):
  114.     self._add_title(**kw)
  115.     self._add_creator(**kw)
  116.     self._add_treeId(**kw)
  117.     self._add_assoId(**kw)
  118.     self._add_lastUpdate(**kw)
  119.     self._privacy(**kw)  
  120.   def _remove_pearl(about):
  121.     pass
  122.   def _add_title(self,**kw):
  123.     _add_prop("title",kw)
  124.   def _add_creator(self,**kw):
  125.     _add_prop("creator",kw)
  126.   def _add_treeId(self,**kw):
  127.     _add_prop("treeId",kw)    
  128.   def _add_assoId(self,**kw):
  129.     _add_prop("assoId",kw)    
  130.   def _add_assoId(self,**kw):
  131.     _add_prop("assoId",kw)    
  132.   def _lastUpdate(self,**kw):
  133.     _add_prop("lastUpdate",kw)    
  134.   def _privacy(self,**kw):
  135.     _add_prop("privacy",kw)    
  136.  
  137. class PT_Objectify(ObjectifiedElement,PT_Obj):
  138.   #__init__(attrib=None, nsmap=None, *children, **_extra)
  139.   #x.__init__(...) initializes x; see help(type(x)) for signature
  140.   #def __init2__(self,attrib, nsmap=NSMAP, *children, **_extra):
  141.   #
  142.   #
  143.   #  print("attrib=" + str(attrib))
  144.   #  print("nsmap=" + str(nsmap))
  145.   #  print("childern=" + str(children))
  146.   #  print("_extra=" + str(_extra))
  147.   #  StringElement.__init__(self,attrib,nsmap,*children,**_extra)
  148.   #  print("Parent Intailized")
  149.   def __new__(cls,attr, parent=None,  nsmap=NSMAP, *args,**kw):
  150.     PT_Objectify.__new2__(cls,attr, parent, *args,**kw)
  151.   def __new2__(cls,attr, parent=None,  nsmap=NSMAP, *args,**kw):
  152.     # https://lxml.de/2.1/namespace_extensions.html
  153.     #etree.setDefaultElementClass(cls)
  154.     print("attrib=" + str(attr))
  155.     print("parent=" + str(parent))
  156.     attr2=PT_Obj.__add_item_helper__(attr,prefix=None,nsmap=NSMAP,ns=None)
  157.     if parent is None:
  158.       el=objectify.Element(attr2)
  159.     else:
  160.       el=objectify.SubElement(parent,attr2)
  161.     PT_Obj.__init2__(el,parent,attr,*args,**kw)
  162.     return el #PT_Tree.__new__(el,*args,**kw)
  163.   def __init__(self,attrib, parent=None, nsmap=NSMAP, *children, **properties):
  164.     self.__init2__(self,attrib, parent=None, nsmap=NSMAP, *children, **properties)
  165.   def __init2__(self,attrib, parent=None, nsmap=NSMAP, *children, **properties):
  166.     for child in childern:
  167.       objectify.SubElement(self,child)
  168.     for prop,val in properties.items():
  169.       PROP=getattr(objectify.E,prop)
  170.       objectify.SubElement(self,PROP(val))
  171.   def _add_item(self,tag,nsmap=NSMAP,prefix=None):
  172.     tag2=__add_item_helper__(self,tag,prefix=None,nsmap=NSMAP,ns=None)
  173.     item=objectify.SubElement(self,tag2)
  174.     return child_ell
  175.   #Propbably redundent:
  176.   #def _add_prop(self,prop,value=None,prefix=None,nsmap=NSMAP,ns=None):
  177.   #  self.setItem(
  178.   #    self._add_item(prop,value,nsmap))
  179.   def _setItem(self,item,value):
  180.     item._setText(value)
  181.   def __str(self):
  182.     return etree.tostring(self, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape")
  183.  
  184. class PT_TreeLXML(PT_Objectify,PT_Tree):
  185.   #def __init__(self,attrib, nsmap=NSMAP, *children, **_extra):
  186.   #  PT_Objectify.__init__(self,attrib, nsmap, *children, **_extra)
  187.   #self.__init__=
  188.   def __new__(*args,**kw):
  189.     return ObjectifiedElement.__new__(*args,**kw)
  190.   def __new2__(attr="Tree",parent=root,nsmap=NSMAP,props=None,prefix="pt",ns=None):
  191.     # https://lxml.de/2.1/namespace_extensions.html
  192.    
  193.     #etree.setDefaultElementClass(cls)
  194.     if attr is None:
  195.       return ElementBase.__new__(PT_TreeLXML)
  196.     else:
  197.       attr2=PT_Obj.__add_item_helper__(attr,prefix=prefix,nsmap=nsmap,ns=None)    
  198.  
  199.       el=objectify.SubElement(parent,attr2,nsmap=nsmap)
  200.       print(etree.tostring(el, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  201.       PT_Obj.__init2__(el,attr2,parent,nsmap,props)
  202.       return el #PT_Tree.__new__(el,*args,**kw)
  203.   def __init2__(self,attr,parent, nsmap=NSMAP, props=None):
  204.     #for child in childern:
  205.     #  se=objectify.SubElement(self,child,nsmap=NSMAP)
  206.     #  print(etree.tostring(se, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  207.     for prop,val in props.items():
  208.       PROP=getattr(objectify.E,prop)
  209.       se=objectify.SubElement(self,child,PROP(val),nsmap=NSMAP)
  210.       print(etree.tostring(se, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  211.   def __str__(self):
  212.     return PT_Objectify.__str__(self)
  213.   #def _init(self):
  214.   #  PT_Objectify._init()
  215.  
  216.     #PT_Objectify.__init__(self,attrib, nsmap, *children, **_extra)
  217.  
  218. #<pt:Tree rdf:about="url_for_some_tree">
  219. #   <dcterms:title><![CDATA[tree_title]]></dcterms:title>
  220. #   <dcterms:creator rdf:resource="https://www.pearltrees.com/somebody#sioc" />
  221. #   <pt:treeId>tree_id</pt:treeId>
  222. #   <pt:assoId>tree_id</pt:assoId>
  223. #   <pt:lastUpdate>2012-05-23T20:00:32</pt:lastUpdate>
  224. #   <pt:privacy>0</pt:privacy>
  225. #</pt:Tree>
  226. def treeTest(**kw):
  227.   tree=PT_TreeLXML.__new2__(
  228.     props={
  229.       'title' : 'my tree',
  230.       'creator' : 'bob',
  231.       'tree_id' : '1234',
  232.       'assoID' : '23',
  233.       'lastUpdate' :'yesterday',
  234.       'privacy' : '1'})
  235.   print(str(tree))
  236. #def main():
  237. treeTest()
  238. print(etree.tostring(root, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  239.  
  240. #if __name__ == "__main__" or 1 == 1: #For testing we will always run main.
  241. #    main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement