Advertisement
s243a

pearl.py (4)

Aug 14th, 2022
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 21.34 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.   'identifier' : 'dcterms',
  39.   'seeAlso' : 'rdfs',
  40.   'date' : 'dcterms',
  41.   'noteText' : 'pt',
  42.   'parentPearl' : 'pt',
  43.   'posOrder' : 'pt'}
  44.  
  45.  
  46.  
  47. DEFAULTS = {
  48.   'privacy': (lambda x : 1),
  49.   'lastUpdate' : lambda : "" + date.today() + "T" + time.now()
  50. }
  51. iff=lambda a,b,c: b if a else c  
  52.  
  53.  
  54. # See: Custom Claass Lookup
  55. #      https://lxml.de/element_classes.html
  56. class MyLookup(etree.CustomElementClassLookup):
  57.   def lookup(self, node_type, document, namespace, name):
  58.     print("namespace="+str(namespace))
  59.     if namespace == 'http://www.pearltrees.com/rdf/0.1/#':
  60.       if name == "Tree":
  61.         return PT_TreeLXML
  62.     else:
  63.      return None  # pass on to (default) fallback
  64.  
  65. def strip_ns(name):
  66.   if name[0]=='{':
  67.     return name.tag.split('}')[1]
  68.   else:
  69.     return name
  70. def get_verb(name):
  71.   for i in range(0,len(name)-1):
  72.     if name[i]!="_":
  73.       three_chars=name[i,i+3]
  74.       if three_chars in ('get','has','set','add'):
  75.         return three_chars
  76.       else:
  77.         return None
  78.   return None
  79. parser = etree.XMLParser()
  80. parser.set_element_class_lookup(MyLookup())
  81. root_str='''<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  82.         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  83.         xmlns:foaf="http://xmlns.com/foaf/0.1/"
  84.         xmlns:dcterms="http://purl.org/dc/elements/1.1/"
  85.         xmlns:pt="http://www.pearltrees.com/rdf/0.1/#"></rdf:RDF>'''
  86. root=etree.fromstring(root_str, parser)
  87. def asgn(name,val,default=None,defaults=None):
  88.   if default is None:
  89.     if defaults is None:
  90.       defaults=DEFAULTS
  91.     default=defaults[name]
  92.   try:
  93.     iff(name in val,val[name],default)
  94.   except:
  95.     if val is None:
  96.       val=default
  97.   return val
  98.  
  99. def add_boilerplate_meth(cls,verb,pred):
  100.   print("cls="+str(cls))
  101.   print("verb="+str(verb))
  102.   print("pred="+str(pred))
  103.   def new_meth(self,*prop,**kw):
  104.     return __getattr__(self,"_"+verb+"_"+"pred",*prop,*kw)
  105.   if str(verb) in ('set','get','add','has'):
  106.     setattr(cls,"_"+verb+"_"+pred,MethodType(new_meth, cls))
  107.   else:
  108.     raise Exception("unkonwn verb in add_boilerplate")
  109. def add_boilerplate_methods(cls,predicates,verbs=('set','get','add','has')):
  110.   for verb in verbs:
  111.     for pred in predicates:
  112.       add_boilerplate_meth(cls,verb,pred)
  113.  
  114. class PT_Obj(object):
  115.   def __add_item_helper__(tag,prefix=None,nsmap=NSMAP,ns=None):
  116.     print("tag="+str(tag))
  117.     print("prefix="+str(prefix))
  118.     print("ns="+str(ns))
  119.     if ns is None and tag[0] == "{":
  120.       return tag
  121.     if ns is None and prefix is None:
  122.       if tag in PREFIXES_FM_PROP:
  123.         try:
  124.           prefix=PREFIXES_FM_PROP[tag]
  125.         except:
  126.           raise Warning("no namespace for "+str(tag))
  127.     if ns is None and prefix is not None:
  128.       try:
  129.         ns=nsmap[prefix]
  130.       except:
  131.         raise Warning("no namespace mapping for"+prefix)
  132.     #if ns is None
  133.     #  PREFIXES_FM_PROP
  134.     #  try:
  135.     #    ns=PREFIXES_FM_PROP[prefix]
  136.     #  except:
  137.     #    raise warning("no namespace mapping for"+creator)      
  138.     if ns is not None:
  139.       tag="{" + ns + "}" + tag
  140.     #value=asgn(tag,value)
  141.     return tag
  142.   #This is a replacement for the python new clas
  143.   # See: https://www.geeksforgeeks.org/__new__-in-python/  
  144.   def _new2(cls, *args,**kw):
  145.     self.__init2__
  146.     return self
  147.   #This is a replacement for the python new clas
  148.   # See: https://www.geeksforgeeks.org/__new__-in-python/    
  149.   def __init2__(self, *args,**kw):
  150.     pass
  151.   def __getattr__(self, full_name):
  152.     verb,name=get_verb(full_name)  
  153.     if verb=='get':
  154.       return lambda : self._get_item(name)
  155.     elif verb=='set':
  156.       return lambda value : self._set_item(name,value)
  157.     elif verb=='has':
  158.       return lambda : self._has_item(name)
  159.     elif verb=='add':
  160.       def adder_method():
  161.         iff(len(p)>0,self._add_prop(name*p,**kw),
  162.                      self._add_item(name*p,**kw))        
  163.       return adder_method
  164.     # Need to code some functional programming piping to do this.
  165.     #Add if missing then set item
  166.     # elif verb=='aSet'
  167.     #   if Not self._has_item(name):
  168.     #     item=self._add_item(name)
  169.     #     return item._setText(value)
  170.     #   return self._set_item(value)  
  171.     #inspect.getmembers(self, predicate=inspect.ismethod)
  172.     else:
  173.      # https://docs.python.org/3/library/inspect.html#inspect.getmembers
  174.      # https://stackoverflow.com/questions/34439/finding-what-methods-a-python-object-has#comment115434232_28220562
  175.      for n,v in inspect.getmembers(self):
  176.        if n==full_name:
  177.          return v
  178.  
  179.   def _add_item(self,tag,nsmap=NSMAP,prefix=None):
  180.     pass
  181.   def _remove_item(self,about,text=None):
  182.     pass
  183.  
  184.   def _add_prop(self,prop,value=None,prefix=None,nsmap=NSMAP,ns=None):
  185.     self._set_item(prop,value,
  186.       item=self._add_item(prop,nsmap=nsmap))
  187.   def _set_item(self,prop,value,item=None):
  188.     pass
  189.   def _remove_prop():  
  190.     pass
  191. # - In the orginal pearltrees a tree once was analoges to a mind map for a given
  192. # topic. (e.g. ...)
  193. # - In PT2.0 and on the mindmap concept was replaced by a grid like strucures
  194. # and each node of the tree was replaced by a buch of cards.
  195. # - In either case the tree is the principle web page for each topic containing a number
  196. # of sub items. One major difference is that later version of pearltree lose much of the
  197. # tree struture  def __init__(self,**kw):
  198. #<pt:Tree rdf:about="url_for_some_tree">
  199. #   <dcterms:title><![CDATA[tree_title]]></dcterms:title>
  200. #   <dcterms:creator rdf:resource="https://www.pearltrees.com/somebody#sioc" />
  201. #   <pt:treeId>tree_id</pt:treeId>
  202. #   <pt:assoId>tree_id</pt:assoId>
  203. #   <pt:lastUpdate>2012-05-23T20:00:32</pt:lastUpdate>
  204. #   <pt:privacy>0</pt:privacy>
  205. #</pt:Tree>
  206.  
  207. class PT_Tree(PT_Obj):
  208.   def __init__(self,**kw):
  209.     self.__init2__
  210.   def _new2(cls,*args, **kw):
  211.     self.__init2__(*args,**kw)
  212.     return self
  213.   #This is a replacement for the python new clas
  214.   # See: https://www.geeksforgeeks.org/__new__-in-python/    
  215.   def __init2__(self,attrib=None, parent=None, nsmap=NSMAP, props=None, childern=None):
  216.     pass
  217.   def _remove_pearl(about):
  218.     pass
  219. add_boilerplate_methods(
  220.   PT_Tree,(
  221.     'title',
  222.     'creator',
  223.     'treeId',
  224.     'assoId',
  225.     'lastUpdate',
  226.     'privacy'))
  227.  
  228.  
  229. class PT_Objectify(ObjectifiedElement,PT_Obj):
  230.   #__init__(attrib=None, nsmap=None, *children, **_extra)
  231.   #x.__init__(...) initializes x; see help(type(x)) for signature
  232.   #def __init2__(self,attrib, nsmap=NSMAP, *children, **_extra):
  233.   #
  234.   #
  235.   #  print("attrib=" + str(attrib))
  236.   #  print("nsmap=" + str(nsmap))
  237.   #  print("childern=" + str(children))
  238.   #  print("_extra=" + str(_extra))
  239.   #  StringElement.__init__(self,attrib,nsmap,*children,**_extra)
  240.   #  print("Parent Intailized")
  241.   def __new__(cls,attr, parent=None,  nsmap=NSMAP, *args,**kw):
  242.     PT_Objectify.__new2__(cls,attr, parent, *args,**kw)
  243.   def __new2__(cls,attr, parent=None,  nsmap=NSMAP, *args,**kw):
  244.     # https://lxml.de/2.1/namespace_extensions.html
  245.     #etree.setDefaultElementClass(cls)
  246.     #print("attrib=" + str(attr))
  247.     #print("parent=" + str(parent))
  248.     attr2=PT_Obj.__add_item_helper__(attr,prefix=None,nsmap=NSMAP,ns=None)
  249.     if parent is None:
  250.       el=objectify.Element(attr2)
  251.     else:
  252.       el=objectify.SubElement(parent,attr2)
  253.     el.__class__.__init2__(el,parent,attr,*args,**kw)
  254.     return el #PT_Tree.__new__(el,*args,**kw)
  255.   def __init__(self,attrib, parent=None, nsmap=NSMAP, *children, **properties):
  256.     self.__init2__(self,attrib, parent=None, nsmap=NSMAP, *children, **properties)
  257.   def __init2__(self,attrib=None, parent=None, nsmap=NSMAP, props=None, childern=None):
  258.     #for child in childern:
  259.     #  objectify.SubElement(self,child)
  260.     print("PT_Objectify.__new2__")
  261.     print("attrib="+str(attrib))
  262.     print("props="+str(props))
  263.     print("childern="+str(childern))
  264.     for prop,val in props.items():
  265.       PROP=getattr(objectify.E,prop)
  266.       objectify.SubElement(self,PROP(val))
  267.     PT_Obj.__init2__(el,parent,attr,*args,**kw)  
  268.   def _get_item(name):    
  269.     name2=strip_ns(name) #TODO: Verify if this is necessary
  270.     find = objectify.ObjectPath("."+name2)
  271.     try:
  272.       return find(self)
  273.     except:
  274.       return None    
  275.   def _add_item(self,tag,nsmap=NSMAP,prefix=None,ns=None):
  276.     print("PT_Objectify._add_item")
  277.     print("tag="+str(tag))
  278.     print("prefix="+str(prefix))
  279.     tag2=PT_Obj.__add_item_helper__(tag,prefix=prefix,nsmap=nsmap,ns=None)
  280.     item=objectify.SubElement(self,tag2)
  281.     return item
  282.   def _has_item(name):
  283.     return self._get_item(name) is none
  284.   #Propbably redundent:
  285.   #def _add_prop(self,prop,value=None,prefix=None,nsmap=NSMAP,ns=None):
  286.   #  self.setItem(
  287.   #    self._add_item(prop,value,nsmap))
  288.   def _set_item(self,item_name,value,item=None):
  289.     print("PT_Objectify._set_item")
  290.     print("item_name="+str(item_name))
  291.     print("item_name_type"+str(type(Element)))
  292.     if item is not None:
  293.       pass
  294.     elif str(item_name)[0:8] == "<Element":
  295.        raise Warning("item_name should be string. Use named argument 'item' instead")
  296.        item=item_name
  297.     else: #Assume this is a string
  298.       item=self._get_item(item_name)
  299.     #TODO: Maybe throw warning if item text is already set
  300.     try:
  301.       item._setText(value)
  302.     except:
  303.       item.text=value
  304.   def __str(self):
  305.     return etree.tostring(self, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape")
  306.  
  307. class PT_TreeLXML(PT_Objectify,PT_Tree):
  308.   #def __init__(self,attrib, nsmap=NSMAP, *children, **_extra):
  309.   #  PT_Objectify.__init__(self,attrib, nsmap, *children, **_extra)
  310.   #self.__init__=
  311.   def __new__(*args,**kw):
  312.     return ObjectifiedElement.__new__(*args,**kw)
  313.   def __new2__(attr="Tree",parent=root,nsmap=NSMAP,props=None,prefix="pt",ns=None, childern=None):
  314.     # https://lxml.de/2.1/namespace_extensions.html
  315.    
  316.     #etree.setDefaultElementClass(cls)
  317.     #if attr is None:
  318.     #  return ElementBase.__new__(PT_TreeLXML)
  319.     #else:
  320.     attr2=PT_Obj.__add_item_helper__(attr,prefix=prefix,nsmap=nsmap,ns=None)    
  321.  
  322.     el=objectify.SubElement(parent,attr2,nsmap=nsmap)
  323.     print(etree.tostring(el, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  324.     el.__class__.__init2__(el,attr2,parent=parent,nsmap=nsmap,props=props,childern=childern)
  325.     return el #PT_Tree.__new__(el,*args,**kw)
  326.   def __init2__(self,attr=None,parent=None, nsmap=NSMAP, props=None, childern=None):
  327.     #for child in childern:
  328.     #  se=objectify.SubElement(self,child,nsmap=NSMAP)
  329.     #  print(etree.tostring(se, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  330.     print("PT_Objectify.__new2__")
  331.     print("attrib="+str(attr))
  332.     print("props="+str(props))
  333.     print("childern="+str(childern))
  334.     for prop,val in props.items():
  335.       #PROP=getattr(objectify.E,prop)
  336.       #se=objectify.SubElement(self,child,PROP(val),nsmap=NSMAP)
  337.  
  338.  
  339.       self._add_prop(prop,value=val,prefix=None,nsmap=NSMAP,ns=None)
  340.       #print(etree.tostring(se, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  341.   def __str__(self):
  342.     return PT_Objectify.__str__(self)
  343.   #def _init(self):
  344.   #  PT_Objectify._init()
  345.  
  346.     #PT_Objectify.__init__(self,attrib, nsmap, *children, **_extra)
  347.  
  348. #<pt:Tree rdf:about="url_for_some_tree">
  349. #   <dcterms:title><![CDATA[tree_title]]></dcterms:title>
  350. #   <dcterms:creator rdf:resource="https://www.pearltrees.com/somebody#sioc" />
  351. #   <pt:treeId>tree_id</pt:treeId>
  352. #   <pt:assoId>tree_id</pt:assoId>
  353. #   <pt:lastUpdate>2012-05-23T20:00:32</pt:lastUpdate>
  354. #   <pt:privacy>0</pt:privacy>
  355. #</pt:Tree>
  356. def treeTest(**kw):
  357.   tree=PT_TreeLXML.__new2__(
  358.     props={
  359.       'title' : 'my tree',
  360.       'creator' : 'bob',
  361.       'tree_id' : '1234',
  362.       'assoID' : '23',
  363.       'lastUpdate' :'yesterday',
  364.       'privacy' : '1'})
  365.   print(str(tree))
  366. #def main():
  367. treeTest()
  368. print(etree.tostring(root, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  369.  
  370. class PT_Pearl(PT_Obj):
  371.   def __init__(self,**kw):
  372.     self.__init2__
  373.   def _new2(cls,*args, **kw):
  374.     self.__init2__(*args,**kw)
  375.     return self
  376.   #This is a replacement for the python new clas
  377.   # See: https://www.geeksforgeeks.org/__new__-in-python/    
  378.   def __init2__(self,attrib=None, parent=None, nsmap=NSMAP, props=None, childern=None):
  379.     pass
  380.   def _remove_pearl(about):
  381.     pass
  382. add_boilerplate_methods(
  383.   PT_Pearl,(
  384.     'parentTree',
  385.     'leftPos',
  386.     'Pearl',
  387.     'rightPos',
  388.     'inTreeSinceDate',
  389.     'privacy',
  390.     'posOrder'))
  391. class PT_Pearl_LXML(PT_Objectify,PT_Pearl):
  392.   #def __init__(self,attrib, nsmap=NSMAP, *children, **_extra):
  393.   #  PT_Objectify.__init__(self,attrib, nsmap, *children, **_extra)
  394.   #self.__init__=
  395.   def __new__(*args,**kw):
  396.     return ObjectifiedElement.__new__(*args,**kw)
  397.   def __new2__(attr="Tree",parent=root,nsmap=NSMAP,props=None,prefix="pt",ns=None, childern=None):
  398.     # https://lxml.de/2.1/namespace_extensions.html
  399.    
  400.     #etree.setDefaultElementClass(cls)
  401.     #if attr is None:
  402.     #  return ElementBase.__new__(PT_TreeLXML)
  403.     #else:
  404.     attr2=PT_Obj.__add_item_helper__(attr,prefix=prefix,nsmap=nsmap,ns=None)    
  405.  
  406.     el=objectify.SubElement(parent,attr2,nsmap=nsmap)
  407.     print(etree.tostring(el, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  408.     el.__class__.__init2__(el,attr2,parent=parent,nsmap=nsmap,props=props,childern=childern)
  409.     return el #PT_Tree.__new__(el,*args,**kw)
  410.   def __init2__(self,attr=None,parent=None, nsmap=NSMAP, props=None, childern=None):
  411.     #for child in childern:
  412.     #  se=objectify.SubElement(self,child,nsmap=NSMAP)
  413.     #  print(etree.tostring(se, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  414.     print("PT_Objectify.__new2__")
  415.     print("attrib="+str(attr))
  416.     print("props="+str(props))
  417.     print("childern="+str(childern))
  418.     for prop,val in props.items():
  419.       #PROP=getattr(objectify.E,prop)
  420.       #se=objectify.SubElement(self,child,PROP(val),nsmap=NSMAP)
  421.  
  422.  
  423.       self._add_prop(prop,value=val,prefix=None,nsmap=NSMAP,ns=None)
  424.       #print(etree.tostring(se, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  425.   def __str__(self):
  426.     return PT_Objectify.__str__(self)
  427.  
  428. #<pt:RootPearl rdf:about="https://url_for_some_tree#rootPearl">
  429. #   <dcterms:title><![CDATA[tree_title]]></dcterms:title>
  430. #   <pt:parentTree rdf:resource="https://url_for_some_tree" />
  431. #   <pt:inTreeSinceDate>2012-05-23T20:00:32</pt:inTreeSinceDate>
  432. #   <pt:posOrder>1</pt:posOrder>
  433. #</pt:RootPearl>
  434.  
  435. class PT_RootPearl(PT_Pearl):
  436.   pass
  437.  
  438. class PT_RootPearl_LXML(PT_Pearl_LXML,PT_RootPearl):
  439.   pass
  440.  
  441. #<pt:PagePearl rdf:about="https://www.pearltrees.com/t/hacktivism/irl-hacktivists-groups/id2492374#item18082864">
  442. #   <dcterms:title><![CDATA[/tmp/lab | Creative use of technology]]></dcterms:title>
  443. #   <dcterms:identifier>http://www.tmplab.org/</dcterms:identifier>
  444. #   <pt:parentTree rdf:resource="https://www.pearltrees.com/t/hacktivism/irl-hacktivists-groups/id2492374" />
  445. #   <pt:inTreeSinceDate>2011-02-07T02:42:18</pt:inTreeSinceDate>
  446. #   <pt:posOrder>2</pt:posOrder>
  447. #</pt:PagePearl>
  448.  
  449. class PT_PagePearl(PT_Pearl):
  450.   pass
  451.  
  452. add_boilerplate_methods(
  453.   PT_PagePearl,(
  454.     'identifier'))
  455.  
  456. class PT_PagePearl_LXML(PT_Pearl_LXML,PT_PagePearl):
  457.   pass
  458.  
  459. # <pt:AliasPearl rdf:about="https://www.pearltrees.com/t/hacktivism/id2492215#item18110176&amp;show=item,18110176">
  460. #    <dcterms:title><![CDATA[Anonymous Culture]]></dcterms:title>
  461. #    <rdfs:seeAlso rdf:resource="https://www.pearltrees.com/anon/anonymous-culture/id2421304" />
  462. #    <pt:parentTree rdf:resource="https://www.pearltrees.com/t/hacktivism/id2492215" />
  463. #    <pt:inTreeSinceDate>2011-02-07T14:40:33</pt:inTreeSinceDate>
  464. #    <pt:posOrder>2</pt:posOrder>
  465. # </pt:AliasPearl>
  466.  
  467. class PT_AliasPearl(PT_Pearl):
  468.   pass
  469. add_boilerplate_methods(
  470.   PT_AliasPearl,(
  471.     'seeAlso'))
  472.  
  473. class PT_AliasPearl_LXML(PT_Pearl_LXML,PT_AliasPearl):
  474.   pass
  475.  
  476. # <pt:RefPearl rdf:about="https://www.pearltrees.com/t/hacktivism-s-litteracy/id2492416?show=item,18806157">
  477. #   <dcterms:title><![CDATA[EN]]></dcterms:title>
  478. #   <rdfs:seeAlso rdf:resource="https://www.pearltrees.com/t/hacktivism-s-litteracy/en/id2595429" />
  479. #   <pt:parentTree rdf:resource="https://www.pearltrees.com/t/hacktivism-s-litteracy/id2492416" />
  480. #   <pt:inTreeSinceDate>2011-02-28T19:42:18</pt:inTreeSinceDate>
  481. #   <pt:posOrder>2</pt:posOrder>
  482. # </pt:RefPearl>  
  483.  
  484. class PT_RefPearl(PT_Pearl):
  485.   pass
  486. add_boilerplate_methods(
  487.   PT_AliasPearl,(
  488.     'seeAlso'))
  489.  
  490. class PT_RefPearl_LXML(PT_Pearl_LXML,PT_RefPearl):
  491.   pass
  492.  
  493. def pearlTest(**kw):
  494.   tree=PT_TreeLXML.__new2__(
  495.     props={
  496.       'title' : 'my tree',
  497.       'creator' : 'bob',
  498.       'tree_id' : '1234',
  499.       'assoID' : '23',
  500.       'lastUpdate' :'yesterday',
  501.       'privacy' : '1'})
  502.   print(str(pearl))
  503. #def main():
  504. treeTest()
  505.  
  506. print(etree.tostring(root, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  507. #if __name__ == "__main__" or 1 == 1: #For testing we will always run main.
  508. #    main()
  509.  
  510. # <pt:Note>
  511. #    <dcterms:creator rdf:resource="https://www.pearltrees.com/zarby#sioc" />
  512. #    <dcterms:date>2011-03-01T11:57:52</dcterms:date>
  513. #    <pt:noteText><![CDATA[En effet, les Pearl directs aux articles ne peuvent être changé. Mais merci! C'est une bonne idée!]]></pt:noteText>
  514. #    <pt:parentPearl rdf:resource="https://www.pearltrees.com/t/hacktivism-s-litteracy/pdf-livres/id2595431" />
  515. # </pt:Note>
  516.  
  517. class PT_Note(PT_Obj):
  518.   def __init__(self,**kw):
  519.     self.__init2__
  520.   def _new2(cls,*args, **kw):
  521.     self.__init2__(*args,**kw)
  522.     return self
  523.   #This is a replacement for the python new clas
  524.   # See: https://www.geeksforgeeks.org/__new__-in-python/    
  525.   def __init2__(self,attrib=None, parent=None, nsmap=NSMAP, props=None, childern=None):
  526.     pass
  527.   def _remove_pearl(about):
  528.     pass
  529. add_boilerplate_methods(
  530.   PT_Tree,(
  531.     'creator',
  532.     'date',
  533.     'noteText',
  534.     'parentPearl'))
  535.  
  536. class PT_NoteLXML(PT_Objectify,PT_Note):
  537.   #def __init__(self,attrib, nsmap=NSMAP, *children, **_extra):
  538.   #  PT_Objectify.__init__(self,attrib, nsmap, *children, **_extra)
  539.   #self.__init__=
  540.   def __new__(*args,**kw):
  541.     return ObjectifiedElement.__new__(*args,**kw)
  542.   def __new2__(attr="Tree",parent=root,nsmap=NSMAP,props=None,prefix="pt",ns=None, childern=None):
  543.     # https://lxml.de/2.1/namespace_extensions.html
  544.    
  545.     #etree.setDefaultElementClass(cls)
  546.     #if attr is None:
  547.     #  return ElementBase.__new__(PT_TreeLXML)
  548.     #else:
  549.     attr2=PT_Obj.__add_item_helper__(attr,prefix=prefix,nsmap=nsmap,ns=None)    
  550.  
  551.     el=objectify.SubElement(parent,attr2,nsmap=nsmap)
  552.     print(etree.tostring(el, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  553.     el.__class__.__init2__(el,attr2,parent=parent,nsmap=nsmap,props=props,childern=childern)
  554.     return el #PT_Tree.__new__(el,*args,**kw)
  555.   def __init2__(self,attr=None,parent=None, nsmap=NSMAP, props=None, childern=None):
  556.     #for child in childern:
  557.     #  se=objectify.SubElement(self,child,nsmap=NSMAP)
  558.     #  print(etree.tostring(se, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  559.     print("PT_Objectify.__new2__")
  560.     print("attrib="+str(attr))
  561.     print("props="+str(props))
  562.     print("childern="+str(childern))
  563.     for prop,val in props.items():
  564.       #PROP=getattr(objectify.E,prop)
  565.       #se=objectify.SubElement(self,child,PROP(val),nsmap=NSMAP)
  566.  
  567.  
  568.       self._add_prop(prop,value=val,prefix=None,nsmap=NSMAP,ns=None)
  569.       #print(etree.tostring(se, pretty_print=True, xml_declaration=True,encoding='UTF8').decode("unicode_escape"))
  570.   def __str__(self):
  571.     return PT_Objectify.__str__(self)
  572.  
  573. #<pt:SectionPearl rdf:about="https://www.pearltrees.com/t/users-for-ideas/id5369609#item243408253">
  574. #   <pt:parentTree rdf:resource="https://www.pearltrees.com/t/users-for-ideas/id5369609" />
  575. #   <pt:inTreeSinceDate>2018-12-09T11:54:31</pt:inTreeSinceDate>
  576. #   <pt:posOrder>59</pt:posOrder>
  577. #</pt:SectionPearl>
  578.  
  579.  
  580. class PT_SectionPearl(PT_Pearl):
  581.   pass
  582.  
  583. class PT_SectionPearl_LXML(PT_Pearl_LXML,PT_SectionPearl):
  584.   pass
  585.  
  586.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement