attrpath.py
Go to the documentation of this file.
1 # This module define the three functions:
2 # - getattrpath
3 # - setattrpath
4 # - existattrpath
5 # that work similarly as the get/setattr, but given path ("a.b.c.d")
6 # in input. Consider the following example:
7 # >>> setattrpath( e.__class__,"a.b.c.d",fun)
8 # with fun a function (self,*arg). Then, it is next possible to launch
9 # >>> e.a.b.c.d( ...)
10 # as if it was a classical member function of e.
11 
12 from __future__ import print_function
13 
14 
15 class CommandPath(object):
16  """
17  This class is only defined to implement a path of attribute
18  to store entity commands. It has no members except those automatically
19  defined at run time (which should be CommandPath or functions).
20  """
21 
22  mother = None
23 
24  def __getattr__(self, name):
25  privateName = name + "_obj"
26  if privateName in self.__dict__:
27  obj = getattr(self, privateName)
28  obj.mother = self.mother
29  return obj
30  return object.__getattr__(self, name)
31 
32 
33 def createCommandModule(target, name):
34  def createGetter(name):
35  def __(self):
36  obj = getattr(self, name)
37  obj.mother = self
38  return obj
39 
40  return __
41 
42  privateName = name + "_obj"
43  setattr(target, privateName, CommandPath())
44 
45  if not isinstance(target, CommandPath):
46  setattr(target, name, property(createGetter(privateName)))
47 
48 
49 class CommandLauncher(object):
50  """ """
51 
52  mother = None
53  fun = None
54 
55  def __init__(self, fun):
56  self.fun = fun
57 
58  def __call__(self, *arg):
59  return self.fun(self.mother, *arg)
60 
61 
62 def createCommandLauncher(target, name, fun):
63  if isinstance(target, CommandPath):
64  privateName = name + "_obj"
65  setattr(target, privateName, CommandLauncher(fun))
66  else:
67  setattr(target, name, fun)
68 
69 
70 def setattrpath(target, path, attribute):
71  """
72  Create in target an new attribute with value path (available at
73  target.path1. ... .pathn).
74  """
75  pathk = target
76  read = True
77  if isinstance(path, str):
78  path = path.split(".")
79  for tokenk in path[0:-1]:
80  if (not read) | (tokenk not in pathk.__dict__):
81  read = False
82  createCommandModule(pathk, tokenk)
83  pathk = getattr(pathk, tokenk + "_obj")
84  if callable(attribute):
85  createCommandLauncher(pathk, path[-1], attribute)
86  else:
87  print("Should not happen")
88  setattr(pathk, path[-1], attribute)
89 
90 
91 def getattrpath(target, path):
92  """
93  Get in target the value located at path (available at
94  target.path1. ... .pathn).
95  """
96  pathk = target
97  if isinstance(path, str):
98  path = path.split(".")
99  for tokenk in path:
100  privateName = tokenk + "_obj"
101  if hasattr(pathk, privateName):
102  pathk = getattr(pathk, privateName)
103  else:
104  if hasattr(pathk, tokenk):
105  pathk = getattr(pathk, tokenk)
106  else:
107  raise Exception(
108  'Path does not exist -- while accessing "'
109  + tokenk
110  + '" in '
111  + ".".join(path)
112  )
113  return pathk
114 
115 
116 def existattrpath(target, path):
117  """
118  Check for the existence in target of a value located at path (available at
119  target.path1. ... .pathn).
120  """
121  pathk = target
122  if isinstance(path, str):
123  path = path.split(".")
124  for tokenk in path[0:-1]:
125  print("check ", tokenk)
126  privateName = tokenk + "_obj"
127  if privateName not in pathk.__dict__:
128  return False
129  pathk = getattr(pathk, privateName)
130  name = path[-1]
131  privateName = name + "_obj"
132  return (name in pathk.__dict__) | (privateName in pathk.__dict__)
dynamic_graph.attrpath.createCommandModule
def createCommandModule(target, name)
Definition: attrpath.py:33
dynamic_graph.attrpath.CommandPath.mother
mother
Definition: attrpath.py:22
dynamic_graph.attrpath.setattrpath
def setattrpath(target, path, attribute)
Definition: attrpath.py:70
dynamic_graph.attrpath.CommandPath.__getattr__
def __getattr__(self, name)
Definition: attrpath.py:24
dynamic_graph.attrpath.existattrpath
def existattrpath(target, path)
Definition: attrpath.py:116
dynamic_graph.attrpath.CommandLauncher.__init__
def __init__(self, fun)
Definition: attrpath.py:55
dynamic_graph.attrpath.getattrpath
def getattrpath(target, path)
Definition: attrpath.py:91
dynamic_graph.attrpath.CommandPath
Definition: attrpath.py:15
dynamic_graph.attrpath.CommandLauncher.mother
mother
Definition: attrpath.py:52
dynamic_graph.attrpath.CommandLauncher
Definition: attrpath.py:49
dynamic_graph.attrpath.createCommandLauncher
def createCommandLauncher(target, name, fun)
Definition: attrpath.py:62
dynamic_graph.attrpath.CommandLauncher.__call__
def __call__(self, *arg)
Definition: attrpath.py:58
dynamic_graph.attrpath.CommandLauncher.fun
fun
Definition: attrpath.py:53


dynamic-graph-python
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Oct 27 2023 02:16:36