Advertisement
s243a

pearl.py (3)

Aug 13th, 2022
866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.12 KB | Source Code | 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. import inspect
  8. from types import MethodType
  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.  
  22. #<pt:Tree rdf:about="url_for_some_tree">
  23. #   <dcterms:title><![CDATA[tree_title]]></dcterms:title>
  24. #   <dcterms:creator rdf:resource="https://www.pearltrees.com/somebody#sioc" />
  25. #   <pt:treeId>tree_id</pt:treeId>
  26. #   <pt:assoId>tree_id</pt:assoId>
  27. #   <pt:lastUpdate>2012-05-23T20:00:32</pt:lastUpdate>
  28. #   <pt:privacy>0</pt:privacy>
  29. #</pt:Tree>
  30.  
  31. PREFIXES_FM_PROP={
  32.   'title' : 'dcterms',
  33.   'creator' : 'dcterms',
  34.   'treeID' : 'pt',
  35.   'assoId' : 'pt',
  36.   'lastupdate' : 'pt',
  37.   'privacy' : 'pt'}
  38.  
  39.  
  40.  
  41. DEFAULTS = {
  42.   'privacy': (lambda x : 1),
  43.   'lastUpdate' : lambda : "" + date.today() + "T" + time.now()
  44. }
  45. iff=lambda a,b,c: b if a else c  
  46.  
  47.  
  48. # See: Custom Claass Lookup
  49. #      https://lxml.de/element_classes.html
  50. class MyLookup(etree.CustomElementClassLookup):
  51.   def lookup(self, node_type, document, namespace, name):
  52.     print("namespace="+str(namespace))
  53.     if namespace == 'http://www.pearltrees.com/rdf/0.1/#':
  54.       if name == "Tree":
  55.         return PT_TreeLXML
  56.     else:
  57.      return None  # pass on to (default) fallback
  58.  
  59. def strip_ns(name):
  60.   if name[0]=='{':
  61.     return name.tag.split('}')[1]
  62.   else:
  63.     return name
  64. def get_verb(name):
  65.   for i in range(0,len(name)-1):
  66.     if name[i]!="_":
  67.       three_chars=name[i,i+3]
  68.       if three_chars in ('get','has','set','add'):
  69.         return three_chars
  70.       else:
  71.         return None
  72.   return None
  73. parser = etree.XMLParser()
  74. parser.set_element_class_lookup(MyLookup())
  75. root_str='''<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  76.         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  77.         xmlns:foaf="http://xmlns.com/foaf/0.1/"
  78.         xmlns:dcterms="http://purl.org/dc/elements/1.1/"
  79.         xmlns:pt="http://www.pearltrees.com/rdf/0.1/#"></rdf:RDF>'''
  80. root=etree.fromstring(root_str, parser)
  81. def asgn(name,val,default=None,defaults=None):
  82.   if default is None:
  83.     if defaults is None:
  84.       defaults=DEFAULTS
  85.     default=defaults[name]
  86.   try:
  87.     iff(name in val,val[name],default)
  88.   except:
  89.     if val is None:
  90.       val=default
  91.   return val
  92.  
  93. def add_boilerplate_meth(cls,verb,pred):
  94.   print("cls="+str(cls))
  95.   print("verb="+str(verb))
  96.   print("pred="+str(pred))
  97.   def new_meth(self,*prop,**kw):
  98.     return __getattr__(self,"_"+verb+"_"+"pred",*prop,*kw)
  99.   if str(verb) in ('set','get','add','has'):
  100.     setattr(cls,"_"+verb+"_"+pred,MethodType(new_meth, cls))
  101.   else:
  102.     raise Exception("unkonwn verb in add_boilerplate")
  103. def add_boilerplate_methods(cls,predicates,verbs=('set','get','add','has')):
  104.   for verb in verbs:
  105.     for pred in predicates:
  106.       add_boilerplate_meth(cls,verb,pred)
  107.  
  108. class PT_Obj(object):
  109.   def __add_item_helper__(tag,prefix=None,nsmap=NSMAP,ns=None):
  110.     print("tag="+str(tag))
  111.     print("prefix="+str(prefix))
  112.     print("ns="+str(ns))
  113.     if ns is None and tag[0] == "{":
  114.       return tag
  115.     if ns is None and prefix is None:
  116.       if tag in PREFIXES_FM_PROP:
  117.         try:
  118.           prefix=PREFIXES_FM_PROP[tag]
  119.         except:
  120.           raise Warning("no namespace for "+str(tag))
  121.     if ns is None and prefix is not None:
  122.       try:
  123.         ns=nsmap[prefix]
  124.       except:
  125.         raise Warning("no namespace mapping for"+prefix)
  126.     #if ns is None
  127.     #  PREFIXES_FM_PROP
  128.     #  try:
  129.     #    ns=PREFIXES_FM_PROP[prefix]
  130.     #  except:
  131.     #    raise warning("no namespace mapping for"+creator)      
  132.     if ns is not None:
  133.       tag="{" + ns + "}" + tag
  134.     #value=asgn(tag,value)
  135.     return tag
  136.   #This is a replacement for the python new clas
  137.   # See: https://www.geeksforgeeks.org/__new__-in-python/  
  138.   def _new2(cls, *args,**kw):
  139.     self.__init2__
  140.     return self
  141.   #This is a replacement for the python new clas
  142.   # See: https://www.geeksforgeeks.org/__new__-in-python/    
  143.   def __init2__(self, *args,**kw):
  144.     pass
  145.   def __getattr__(self, full_name):
  146.     verb,name=get_verb(full_name)  
  147.     if verb=='get':
  148.       return lambda : self._get_item(name)
  149.     elif verb=='set':
  150.       return lambda value : self._set_item(name,value)
  151.     elif verb=='has':
  152.       return lambda : self._has_item(name)
  153.     elif verb=='add':
  154.       def adder_method():
  155.         iff(len(p)>0,self._add_prop(name*p,**kw),
  156.                      self._add_item(name*p,**kw))        
  157.       return adder_method
  158.     # Need to code some functional programming piping to do this.
  159.     #Add if missing then set item
  160.     # elif verb=='aSet'
  161.     #   if Not self._has_item(name):
  162.     #     item=self._add_item(name)
  163.     #     return item._setText(value)
  164.     #   return self._set_item(value)  
  165.     #inspect.getmembers(self, predicate=inspect.ismethod)
  166.     else:
  167.      # https://docs.python.org/3/library/inspect.html#inspect.getmembers
  168.      # https://stackoverflow.com/questions/34439/finding-what-methods-a-python-object-has#comment115434232_28220562
  169.      for n,v in inspect.getmembers(self):
  170.        if n==full_name:
  171.          return v
  172.  
  173.   def _add_item(self,tag,nsmap=NSMAP,prefix=None):
  174.     pass
  175.   def _remove_item(self,about,text=None):
  176.     pass
  177.  
  178.   def _add_prop(self,prop,value=None,prefix=None,nsmap=NSMAP,ns=None):
  179.     self._set_item(prop,value,
  180.       item=self._add_item(prop,nsmap=nsmap))
  181.   def _set_item(self,prop,value,item=None):
  182.     pass
  183.   def _remove_prop():  
  184.     pass
  185. # - In the orginal pearltrees a tree once was analoges to a mind map for a given
  186. # topic. (e.g. ...)
  187. # - In PT2.0 and on the mindmap concept was replaced by a grid like strucures
  188. # and each node of the tree was replaced by a buch of cards.
  189. # - In either case the tree is the principle web page for each topic containing a number
  190. # of sub items. One major difference is that later version of pearltree lose much of the
  191. # tree struture  def __init__(self,**kw):
  192. #<pt:Tree rdf:about="url_for_some_tree">
  193. #   <dcterms:title><![CDATA[tree_title]]></dcterms:title>
  194. #   <dcterms:creator rdf:resource="https://www.pearltrees.com/somebody#sioc" />
  195. #   <pt:treeId>tree_id</pt:treeId>
  196. #   <pt:assoId>tree_id</pt:assoId>
  197. #   <pt:lastUpdate>2012-05-23T20:00:32</pt:lastUpdate>
  198. #   <pt:privacy>0</pt:privacy>
  199. #</pt:Tree>
  200.  
  201. class PT_Tree(PT_Obj):
  202.   def __init__(self,**kw):
  203.     self.__init2__
  204.   def _new2(cls,*args, **kw):
  205.     self.__init2__(*args,**kw)
  206.     return self
  207.   #This is a replacement for the python new clas
  208.   # See: https://www.geeksforgeeks.org/__new__-in-python/    
  209.   def __init2__(self,attrib=None, parent=None, nsmap=NSMAP, props=None, childern=None):
  210.     pass
  211.   def _remove_pearl(about):
  212.     pass
  213. add_boilerplate_methods(
  214.   PT_Tree,(
  215.     'title',
  216.     'creator',
  217.     'treeId',
  218.     'assoId',
  219.     'lastUpdate',
  220.     'privacy'))
  221.  
  222.  
  223. class PT_Objectify(ObjectifiedElement,PT_Obj):
  224.   #__init__(attrib=None, nsmap=None, *children, **_extra)
  225.   #x.__init__(...) initializes x; see help(type(x)) for signature
  226.   #def __init2__(self,attrib, nsmap=NSMAP, *children, **_extra):
  227.   #
  228.   #
  229.   #  print("attrib=" + str(attrib))
  230.   #  print("nsmap=" + str(nsmap))
  231.   #  print("childern=" + str(children))
  232.   #  print("_extra=" + str(_extra))
  233.   #  StringElement.__init__(self,attrib,nsmap,*children,**_extra)
  234.   #  print("Parent Intailized")
  235.   def __new__(cls,attr, parent=None,  nsmap=NSMAP, *args,**kw):
  236.     PT_Objectify.__new2__(cls,attr, parent, *args,**kw)
  237.   def __new2__(cls,attr, parent=None,  nsmap=NSMAP, *args,**kw):
  238.     # https://lxml.de/2.1/namespace_extensions.html
  239.     #etree.setDefaultElementClass(cls)
  240.     #print("attrib=" + str(attr))
  241.     #print("parent=" + str(parent))
  242.     attr2=PT_Obj.__add_item_helper__(attr,prefix=None,nsmap=NSMAP,ns=None)
  243.     if parent is None:
  244.       el=objectify.Element(attr2)
  245.     else:
  246.       el=objectify.SubElement(parent,attr2)
  247.     el.__class__.__init2__(el,parent,attr,*args,**kw)
  248.     return el #PT_Tree.__new__(el,*args,**kw)
  249.   def __init__(self,attrib, parent=None, nsmap=NSMAP, *children, **properties):
  250.     self.__init2__(self,attrib, parent=None, nsmap=NSMAP, *children, **properties)
  251.   def __init2__(self,attrib=None, parent=None, nsmap=NSMAP, props=None, childern=None):
  252.     #for child in childern:
  253.     #  objectify.SubElement(self,child)
  254.     print("PT_Objectify.__new2__")
  255.     print("attrib="+str(attrib))
  256.     print("props="+str(props))
  257.     print("childern="+str(childern))
  258.     for prop,val in props.items():
  259.       PROP=getattr(objectify.E,prop)
  260.       objectify.SubElement(self,PROP(val))
  261.     PT_Obj.__init2__(el,parent,attr,*args,**kw)  
  262.   def _get_item(name):    
  263.     name2=strip_ns(name) #TODO: Verify if this is necessary
  264.     find = objectify.ObjectPath("."+name2)
  265.     try:
  266.       return find(self)
  267.     except:
  268.       return None    
  269.   def _add_item(self,tag,nsmap=NSMAP,prefix=None,ns=None):
  270.     print("PT_Objectify._add_item")
  271.     print("tag="+str(tag))
  272.     print("prefix="+str(prefix))
  273.     tag2=PT_Obj.__add_item_helper__(tag,prefix=prefix,nsmap=nsmap,ns=None)
  274.     item=objectify.SubElement(self,tag2)
  275.     return item
  276.   def _has_item(name):
  277.     return self._get_item(name) is none
  278.   #Propbably redundent:
  279.   #def _add_prop(self,prop,value=None,prefix=None,nsmap=NSMAP,ns=None):
  280.   #  self.setItem(
  281.   #    self._add_item(prop,value,nsmap))
  282.   def _set_item(self,item_name,value,item=None):
  283.     print("PT_Objectify._set_item")
  284.     print("item_name="+str(item_name))
  285.     print("item_name_type"+str(type(Element)))
  286.     if item is not None:
  287.       pass
  288.     elif str(item_name)[0:8] == "<Element":
  289.        raise Warning("item_name should be string. Use named argument 'item' instead")
  290.        item=item_name
  291.     else: #Assume this is a string
  292.       item=self._get_item(item_name)
  293.     #TODO: Maybe throw warning if item text is already set
  294.     try:
  295.       item._setText(value)
  296.     except:
  297.       item.text=value
  298.   def __str(self):
  299.     return etree.tostring(self, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape")
  300.  
  301. class PT_TreeLXML(PT_Objectify,PT_Tree):
  302.   #def __init__(self,attrib, nsmap=NSMAP, *children, **_extra):
  303.   #  PT_Objectify.__init__(self,attrib, nsmap, *children, **_extra)
  304.   #self.__init__=
  305.   def __new__(*args,**kw):
  306.     return ObjectifiedElement.__new__(*args,**kw)
  307.   def __new2__(attr="Tree",parent=root,nsmap=NSMAP,props=None,prefix="pt",ns=None, childern=None):
  308.     # https://lxml.de/2.1/namespace_extensions.html
  309.    
  310.     #etree.setDefaultElementClass(cls)
  311.     #if attr is None:
  312.     #  return ElementBase.__new__(PT_TreeLXML)
  313.     #else:
  314.     attr2=PT_Obj.__add_item_helper__(attr,prefix=prefix,nsmap=nsmap,ns=None)    
  315.  
  316.     el=objectify.SubElement(parent,attr2,nsmap=nsmap)
  317.     print(etree.tostring(el, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  318.     el.__class__.__init2__(el,attr2,parent=parent,nsmap=nsmap,props=props,childern=childern)
  319.     return el #PT_Tree.__new__(el,*args,**kw)
  320.   def __init2__(self,attr=None,parent=None, nsmap=NSMAP, props=None, childern=None):
  321.     #for child in childern:
  322.     #  se=objectify.SubElement(self,child,nsmap=NSMAP)
  323.     #  print(etree.tostring(se, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  324.     print("PT_Objectify.__new2__")
  325.     print("attrib="+str(attr))
  326.     print("props="+str(props))
  327.     print("childern="+str(childern))
  328.     for prop,val in props.items():
  329.       #PROP=getattr(objectify.E,prop)
  330.       #se=objectify.SubElement(self,child,PROP(val),nsmap=NSMAP)
  331.  
  332.  
  333.       self._add_prop(prop,value=val,prefix=None,nsmap=NSMAP,ns=None)
  334.       #print(etree.tostring(se, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  335.   def __str__(self):
  336.     return PT_Objectify.__str__(self)
  337.   #def _init(self):
  338.   #  PT_Objectify._init()
  339.  
  340.     #PT_Objectify.__init__(self,attrib, nsmap, *children, **_extra)
  341.  
  342. #<pt:Tree rdf:about="url_for_some_tree">
  343. #   <dcterms:title><![CDATA[tree_title]]></dcterms:title>
  344. #   <dcterms:creator rdf:resource="https://www.pearltrees.com/somebody#sioc" />
  345. #   <pt:treeId>tree_id</pt:treeId>
  346. #   <pt:assoId>tree_id</pt:assoId>
  347. #   <pt:lastUpdate>2012-05-23T20:00:32</pt:lastUpdate>
  348. #   <pt:privacy>0</pt:privacy>
  349. #</pt:Tree>
  350. def treeTest(**kw):
  351.   tree=PT_TreeLXML.__new2__(
  352.     props={
  353.       'title' : 'my tree',
  354.       'creator' : 'bob',
  355.       'tree_id' : '1234',
  356.       'assoID' : '23',
  357.       'lastUpdate' :'yesterday',
  358.       'privacy' : '1'})
  359.   print(str(tree))
  360. #def main():
  361. treeTest()
  362. print(etree.tostring(root, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  363.  
  364. #if __name__ == "__main__" or 1 == 1: #For testing we will always run main.
  365. #    main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement