1- import functools
2- import re
3- import tempfile
4-
51import cppyy
62
73
@@ -17,8 +13,8 @@ def __setitem__(self, key, item):
1713 def __getitem__ (self , key ):
1814 return self .getter (self .GA , key )
1915
20- def __call__ (self , key ):
21- return self .getter (self .GA , key )
16+ def __call__ (self , * args , ** kwargs ):
17+ return self .getter (self .GA , * args , ** kwargs )
2218
2319
2420class GraphAttributesDescriptor (object ):
@@ -96,98 +92,10 @@ def generate_GA_setters():
9692 cppyy .cppdef (DEFS + "};" )
9793
9894
99- SVGConf = None
100-
101-
102- def GraphAttributes_to_html (self ):
103- global SVGConf
104- if SVGConf == None :
105- SVGConf = cppyy .gbl .ogdf .GraphIO .SVGSettings ()
106- SVGConf .margin (50 )
107- SVGConf .bezierInterpolation (True )
108- SVGConf .curviness (0.3 )
109- with tempfile .NamedTemporaryFile ("w+t" , suffix = ".svg" , prefix = "ogdf-python-" ) as f :
110- # os = cppyy.gbl.std.ofstream(f.name)
111- # cppyy.bind_object(cppyy.addressof(os), "std::basic_ostream<char>")
112- cppyy .gbl .ogdf .GraphIO .drawSVG (self , f .name , SVGConf )
113- # os.close()
114- return f .read ()
115-
116-
11795def replace_GraphAttributes (klass , name ):
118- if not name .endswith ("GraphAttributes" ): return
11996 klass .directed = property (klass .directed , cppyy .gbl .ogdf_pythonization .GraphAttributes_set_directed )
12097 for field in (CGA_FIELD_NAMES if name .startswith ("Cluster" ) else GA_FIELD_NAMES ):
12198 setattr (klass , field , GraphAttributesDescriptor (
12299 getattr (klass , field ),
123100 getattr (cppyy .gbl .ogdf_pythonization , "GraphAttributes_set_%s" % field )
124- ))
125- klass ._repr_html_ = GraphAttributes_to_html
126-
127-
128- generate_GA_setters ()
129- # TODO use pythonization for this so that classes are loaded lazily
130- replace_GraphAttributes (cppyy .gbl .ogdf .GraphAttributes , "GraphAttributes" )
131- replace_GraphAttributes (cppyy .gbl .ogdf .ClusterGraphAttributes , "ClusterGraphAttributes" )
132- cppyy .gbl .ogdf .Graph ._repr_html_ = GraphAttributes_to_html # TODO layout
133- cppyy .gbl .ogdf .ClusterGraph ._repr_html_ = GraphAttributes_to_html
134-
135-
136- def GraphObjectContainer_getitem (self , idx ):
137- for e in self :
138- if e .index () == idx :
139- return e
140- raise IndexError ()
141-
142-
143- def pythonize_ogdf_internal (klass , name ):
144- if name .startswith ("GraphObjectContainer" ):
145- klass .__getitem__ = GraphObjectContainer_getitem
146-
147-
148- cppyy .py .add_pythonization (pythonize_ogdf_internal , "ogdf::internal" )
149-
150-
151- class StreamToStr (object ):
152- def __init__ (self , klass ):
153- self .klass = klass
154- self .old_str = klass .__str__
155-
156- def __get__ (self , obj , type = None ):
157- @functools .wraps (self .old_str )
158- def to_str ():
159- try :
160- return cppyy .gbl .ogdf_pythonization .to_string (obj )
161- except TypeError as e :
162- print (e )
163- return self .old_str (obj )
164-
165- return to_str
166-
167-
168- def generic_getitem (self , idx ):
169- # TODO more efficient implementation for random-access, reverse iteration
170- for i , e in enumerate (self ):
171- if i == idx :
172- return e
173- raise IndexError ()
174-
175-
176- def pythonize_ogdf (klass , name ):
177- if not isinstance (klass .__str__ , StreamToStr ):
178- klass .__str__ = StreamToStr (klass )
179- if re .match ("List(Const)?(Reverse)?Iterator(Base)?(<.+>)?" , name ):
180- def advance (self ):
181- if not self .valid ():
182- raise StopIteration ()
183- val = self .__deref__ ()
184- self .__preinc__ ()
185- return val
186-
187- klass .__next__ = advance
188- if re .match ("S?List(Pure)?" , name ):
189- klass .__getitem__ = generic_getitem
190- # TODO setitem?
191-
192-
193- cppyy .py .add_pythonization (pythonize_ogdf , "ogdf" )
101+ ))
0 commit comments