1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
|
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
#-------------------------------python------------------------pykig.py--#
# #
# Da Python a Kig #
# #
#--Maurizio Paolini-Daniele Zambelli-----------------------------2005---#
#
# (licenza GPL)
version="0.2.11"
#####
# Type constant
#####
TI=type(0)
TF=type(0.)
TS=type("")
TT=type((0, 0))
#####
# Constants: Point Style, Line Style, defaults values...
#####
PS=("Round", "RoundEmpty", "Rectangular", "RectangularEmpty", "Cross")
LS=("SolidLine", "DashLine", "DashDotLine", "DashDotDotLine", "DotLine")
KIGTRUE="true"
KIGFALSE="false"
DEFWIDTH=-1
DEFCOLOR="#0000ff"
DEFNAME="none"
PROPERTY_INI="Property which"
OBJECT_INI="Object type"
PROPERTY_END="Property"
OBJECT_END="Object"
DICT=(("&","&"), ("<","<"), (">",">"),
("à","à "), ("è","è"), ("ì","ì"), ("ò","ò"), ("ù","ù"), ("é","é"))
#
# this is a trick to allow definitions like "p=Point(0,0,HIDDEN)"
#
HIDDEN=KIGFALSE
VISIBLE=KIGTRUE
#####
# Validation parameters
#####
def parameter(val, defval):
if val==None: return defval
else: return val
def validshown(shown):
if shown==KIGTRUE or shown==KIGFALSE: return shown
def validwidth(width):
if type(width)==TI: return width
def validpointstyle(ps):
if ps in PS: return ps
def validname(name):
if type(name)==TS: return name
def validlinestyle(ls):
if ls in LS: return ls
def validcolor(color):
if type(color)==TS: return color
#####
# if as function
#####
def rif(condition, val1, val2):
"""Return val1 if condition is True else return val2."""
if condition: return val1
else: return val2
#####
# Force some Python variables as kig variables
#####
def kig_double(val):
tp=type(val)
if tp==TI or tp==TF: return Double(val)
else: return val
def kig_int(val):
tp=type(val)
if tp==TI: return Int(val)
else: return val
def kig_string(val):
tp=type(val)
if tp==TS: return String(val)
else: return val
def kig_point(val):
tp=type(val)
if tp==TT:
x, y = val
return Point(x, y, internal=True)
else:
return val
def kig_relpoint(obj, displ):
x, y = displ
return RelativePoint(x, y, obj, internal=True)
#####
# base classes
#####
#####
# Classe KigDocument
#####
class KigDocument(object):
""" Classe che produce il documento kig.
genealogia:
KigDocument <- object
attributi di classe:
attributi:
axes
grid
outfilename
outfile
callkig
of
viewkig
hierarchy
internal
width
pointstyle
name
linestyle
shown
color
metodi:
viewappend
hierarchyappend
setcallkig
setof
str_open
close
noaxes
nogrid
hideobjects
showobjects
setwidth
setpointstyle
setname
setlinestyle
setshown
setcolor
setinternal
"""
def __init__(self, outfilename, callkig=True, of=False):
# print "KigDocument.__init__()"
self.axes = "1"
self.grid = "1"
self.outfilename=outfilename
self.callkig=callkig
self.of=of
try:
self.outfile = open(outfilename, 'w')
except IOError, value:
# print >> sys.stderr, outfilename, 'unwritable'
print >> sys.stderr, value
sys.exit(2)
# KigOut._kigdocument=self
KigDOP._kd=self
KigView._kd=self
self.viewkig=[]
self.hierarchy=[]
# Defaults values
self.internal=False
self.width=DEFWIDTH
self.pointstyle=PS[0]
self.name=DEFNAME
self.linestyle=LS[0]
self.shown=VISIBLE
self.color=DEFCOLOR
def viewappend(self, e): self.viewkig.append(e)
def hierarchyappend(self, e): self.hierarchy.append(e)
def setcallkig(v): self.callkig=v
def setof(v): self.of=v
def str_open(self):
return """<!DOCTYPE KigDocument>
<KigDocument axes="%s" grid="%s" CompatibilityVersion="0.7.0" Version="0.9.1" >
<CoordinateSystem>Euclidean</CoordinateSystem>
<Hierarchy>
""" % (self.axes, self.grid)
def close(self):
try:
self.outfile.write(self.str_open())
self.outfile.writelines(self.hierarchy)
self.outfile.write(" </Hierarchy>\n <View>\n")
for f in self.viewkig:
self.outfile.write(f.str_view())
self.outfile.write(" </View>\n</KigDocument>\n")
if self.outfile != sys.stdout:
self.outfile.close()
except IOError, value:
print >> sys.stderr, value
sys.exit(2)
try:
if self.callkig:
err = os.system('kig --nofork ' + self.outfilename)
except Exception, value:
print >> sys.stderr, value
if not self.of:
os.system('rm ' + self.outfilename)
def noaxes(self): self.axes="0"
def nogrid(self): self.grid="0"
def hideobjects(self): self.shown=HIDDEN
def showobjects(self): self.shown=VISIBLE
def setwidth(self, w): self.width=w
def setpointstyle(self, ps): self.pointstyle=ps
def setname(self, n): self.name=n
def setlinestyle(self, ls): self.linestyle=ls
def setshown(self, s): self.shown=s
def setcolor(self, c): self.color=c
def setinternal(self, v): self.internal=v
#####
# Classe KigDOP
#####
#class KigDOP(KigOut):
class KigDOP(object):
"""Classe da cui deriva ogni elemento che ha un id: Data, Object, Property.
genealogia:
kigDOP <- object
attributo di classe:
id-counter
attributi:
id
type
metodi:
getid
str_hierarchy
"""
_kd=None
_id_counter=0
def __init__(self, type):
KigDOP._id_counter+=1
self.id=KigDOP._id_counter
self._type=type
# self.getkigdocument().hierarchyappend(self.str_hierarchy())
KigDOP._kd.hierarchyappend(self.str_hierarchy())
def getid(self): return str(self.id)
def str_hierarchy(self): pass
#####
# Classe KigView
#####
#class KigView(KigOut):
class KigView(object):
""" Classe con i dati di visualizzazione
genealogia:
KigView <- object
attributi di classe:
_kd
attributi:
shown
width
style
color
name
pointstyle
metodi:
str_view
show
hide
"""
_kd=None
def __init__(self, object, shown, name, width, pointstyle, linestyle, color):
self.object=object
self.shown = parameter(shown, KigView._kd.shown)
self.width = parameter(width, KigView._kd.width)
self.pointstyle = parameter(pointstyle, KigView._kd.pointstyle)
self.linestyle = parameter(linestyle, KigView._kd.linestyle)
self.color = parameter(color, KigView._kd.color)
self.name = parameter(name, KigView._kd.name)
KigView._kd.viewappend(self)
def str_view(self):
"""Produce la stringa che viene scritta sotto <View>.
esempio:
<Draw width="-1" point-style="Round" namecalcer="none"
style="SolidLine" shown=None color="#0000ff" object="3" />
"""
return ' <Draw width="%s" point-style="%s" namecalcer="%s"\
style="%s" shown="%s" color="%s" object="%s" />\n' %\
(self.width, self.pointstyle, self.name,
self.linestyle, self.shown, self.color, self.object.getid())
#####
# Classe Data
#####
class Data(KigDOP):
""" Classe da cui deriva ogni elemento Data
genealogia:
Data <- KigDOP <- object
attributi:
val
metodi:
str_hierarchy
"""
def __init__(self, type, val):
self.val=val
KigDOP.__init__(self, type)
def str_hierarchy(self):
"""Produce la stringa che viene scritta sotto <Data>.
esempio:
<Data type="double" id="170" >0.1</Data>
"""
return ' <Data type="%s" id="%s" >%s</Data>\n' % \
(self._type, self.getid(), self.val)
#####
# Classe PropObj
#####
class PropObj(KigDOP):
""" Classe da cui deriva ogni elemento visibile
genealogia:
PropObj <- KigDOP <- object
attributi di classe:
attributi:
prop
objvec
view
metodi:
str_hierarchy
showname(self, n)
show(self)
hide(self)
setwidth(self, width)
setcolor(self, color)
setlinestyle(self, linestyle)
setpointstyle(self, pointstyle)
setname(self, n)
setshown(self, s)
getwidth(self)
getcolor(self)
getlinestyle(self)
getpointstyle(self)
"""
def __init__(self, prop, type, objvec, shown, name, internal,
width, pointstyle, linestyle, color):
self.prop=prop
self.objvec=objvec
self.n_lb=None
KigDOP.__init__(self, type)
internal=parameter(internal, KigDOP._kd.internal)
if internal:
self.view = None
else:
# Qui si assume che, se viene dato un nome ad un oggetto,
# si voglia anche visualizzare questo nome
if name: n_id=self.showname(name, shown, width, pointstyle, linestyle,
color)
else: n_id=None
self.view = KigView(self, shown, n_id, width, pointstyle, linestyle,
color)
def str_hierarchy(self):
"""Produce la stringa che viene scritta sotto <Data>.
esempio:
<Property which="mid-point" id="170" >
<Parent id="..." />
</Property>
oppure:
<Object type="ConstrainedPoint" id="14" >
<Parent id="13" />
<Parent id="10" />
</Object>
"""
retstring = ' <%s="%s" id="%s" >' %\
((self.prop and PROPERTY_INI or OBJECT_INI),
self._type, self.getid())
for p in self.objvec:
retstring = retstring + '\n <Parent id="%s" />' % p.getid()
retstring = retstring + '\n </%s>\n' % (self.prop and PROPERTY_END or
OBJECT_END)
return retstring
def showname(self, name, shown, width, pointstyle, linestyle, color):
n=String(name)
self.n_lb=Label(self, (0, 0), n, 0, shown, None, False,
width, pointstyle, linestyle, color)
return n.getid()
def show(self):
if self.view: self.view.shown=None
def hide(self):
if self.view: self.view.shown=KIGFALSE
def setwidth(self, width): self.view.width=width
def setcolor(self, color): self.view.color=color
def setlinestyle(self, linestyle):
if linestyle in LS: self.view.linestyle=linestyle
def setpointstyle(self, pointstyle):
if pointstyle in PS: self.view.pointstyle=pointstyle
def type(self): return Type(self)
def setname(self, n):
v=self.view
v.name=self.showname(n, v.shown, v.width, v.pointstyle, v.linestyle,
v.color)
def setshown(self, s): self.view.shown=s
#####
# Classe Property
#####
class Property(PropObj):
""" Classe da cui deriva ogni elemento Property
genealogia:
Property <- PropObj <- KigDOP <- object
"""
def __init__(self, type, parent, shown, name, internal,
width, pointstyle, linestyle, color):
PropObj.__init__(self, True, type, (parent,), shown, name, internal,
width, pointstyle, linestyle, color)
# print shown
#####
# Classe Object
#####
class Object(PropObj):
""" Classe da cui deriva ogni elemento Oggetto
genealogia:
Object <- PropObj <- KigDOP <- object
"""
def __init__(self, type, objvec, shown, name, internal,
width, pointstyle, linestyle, color):
PropObj.__init__(self, False, type, objvec, shown, name, internal,
width, pointstyle, linestyle, color)
#####
# Data
#####
data=(\
("Int", "int", "val"),
("Double", "double", "val"),
("String", "string", "convstr(val)"),
)
def convstr(s):
for o, n in DICT:
s=s.replace(o, n)
return s
def databuild(nomeclasse, nomekig, v="val"):
"""Create string with a Data class definition."""
return """class %s(Data):
def __init__(self, val):
Data.__init__(self, "%s", %s)
""" % (nomeclasse, nomekig, v)
for d in data:
p1, p2, p3 = d
exec databuild(p1, p2, p3)
#####
# Objects
#####
"""Da aggiungere:
("ConvexHall", "ConvexHall", "polygon,", "(polygon,),"),
("EllipseByFocusFocusPoint", "EllipseBFFP", "p1, p2, p3,", "(p1, p2, p3,),"),
("HyperbolaByFocusFocusPoint", "HyperbolaBFFP", "p1, p2, p3,", "(p1, p2, p3,),"),
(ConicsBy5Points", "ConicB5P", "p1, p2, p3, p4, p5,", "(p1, p2, p3, p4, p5),"),
("ParabolaBy3Points", "ParabolaBTP", "p1, p2, p3,", "(p1, p2, p3,),"),
("CocCurve", "CocCurve", "line, point,", "(line, point,),"),
"""
objects=(\
###### Points class
("Point", "FixedPoint", "x, y,", "(kig_double(x), kig_double(y)),"),
("ConstrainedPoint", "ConstrainedPoint",
"t, curve,", "(kig_double(t), curve),"),
("RelativePoint", "RelativePoint",
"x, y, p,", "(kig_double(x), kig_double(y), p),"),
###### segments, rays, lines
("Line", "LineAB", "p1, p2,", "(p1, p2),"),
("Segment", "SegmentAB", "p1, p2,", "(p1, p2),"),
("Ray", "RayAB", "p1, p2,", "(p1, p2),"),
("Orthogonal", "LinePerpend", "line, point,", "(line, point,),"),
("Parallel", "LineParallel", "line, point,", "(line, point,),"),
###### Circles, arcs, ...
("Circle", "CircleBCP", "center, point,", "(center, point,),"),
("CircleByCenterRadius", "CircleBPR", "center, radius,", "(center, radius,),"),
("CircleBy3Points", "CircleBTP", "p1, p2, p3,", "(p1, p2, p3,),"),
("ArcBy3Points", "ArcBTP", "p1, p2, p3,", "(p1, p2, p3,),"),
("ArcByCenterPointAngle", "ArcBCPA",
"center, point, angle,", "(center, point, angle),"),
###### Conics...
("ParabolaByDirectrixFocus", "ParabolaBDP", "line, point,", "(line, point,),"),
("VerticalCubic", "VerticalCubicB4P", "p1, p2, p3, p4,", "(p1, p2, p3, p4),"),
("ConicArc", "ConicArcBTPC", "p1, p2, p3, center,", "(p1, p2, p3, center),"),
#####
# intersections. The only standard object is the intersection
# of two lines, which always gives one single point
#####
("LineLineIntersection", "LineLineIntersection", "l1, l2,", "(l1, l2),"),
#####
# Classe CircleCircleIntersection e ConicLineIntersection
# l'intero "which" puo' assumere i valori 1 o -1 per indicare quale
# delle due intersezioni si desidera ottenere
# si potrebbe mettere un controllo...
#####
("CircleCircleIntersection", "CircleCircleIntersection",
"c1, c2, witch,", "(c1, c2, Int(witch),),"),
("ConicLineIntersection", "ConicLineIntersection",
"conic, line, witch,", "(conic, line, Int(witch),),"),
###### Classe Triangle
("Triangle", "TriangleB3P", "p1, p2, p3,", "(p1, p2, p3),"),
###### Classe Polygon (the only argument is a points vect)
("Polygon", "PolygonBNP", "pvec,", "pvec,"),
###### Classe PolygonBCV
# Poligono regolare dati il centro e un vertice; il terzo argomento
# e' un intero contenente il numero di lati
("PolygonBCV", "PoligonBCV",
"center, vertex, n,", "(center, vertex, Int(n)),"),
##### Classe PolygonVertex (poligono, intero >= 0)
("PolygonVertex", "PolygonVertex",
"polygon, i,", "(polygon, Int(i)),"),
##### Classe PolygonSide (poligono, intero >= 0)
("PolygonSide", "PolygonSide",
"polygon, i,", "(polygon, Int(i)),"),
###### vector, angle,...
("Vector", "Vector", "p1, p2,", "(p1, p2),"),
("Angle", "Angle", "p1, v, p2,", "(p1, v, p2),"),
###### Transformations
("Translate", "Translation", "obj, vector,", "(obj, vector),"),
("CentralSymmetry", "PointReflection", "obj, center,", "(obj, center),"),
("AxialSymmetry", "LineReflection", "obj, center,", "(obj, center),"),
("Rotate", "Rotation",
"obj, center, angle,", "(obj, center, angle),"),
("Scale", "ScalingOverCenter",
"obj, center, segment,", "(obj, center, segment),"),
("Scale2", "ScalingOverCenter2",
"obj, center, s1, s2,", "(obj, center, s1, s2),"),
("InvertPoint", "InvertPoint",
"point, circle,", "(point, circle),"),
("CircularInversion", "CircularInversion",
"objecttoinvert, circle,", "(objecttoinvert, circle),"),
("InvertLine", "InvertLine",
"line, circle,", "(line, circle),"),
("InvertCircle", "InvertCircle",
"circletoinvert, circle,", "(circletoinvert, circle),"),
("InvertArc", "InvertArc",
"arctoinvert, circle,", "(arctoinvert, circle),"),
("InvertSegment", "InvertSegment",
"segment, circle,", "(segment, circle),"),
###### Text, Label, ...
("Text", "Label",
"point, string, boxed=0,",
"(Int(boxed), kig_point(point), kig_string(string)),"),
("Label", "Label",
"obj, displ, string, boxed=0,",
"(Int(boxed),kig_relpoint(obj, displ),kig_string(string)),"),
("VarText", "Label",
"point, string, vars, boxed=0,",
"(Int(boxed), kig_point(point), \
kig_string(string))+tuple(vars),"),
("VarLabel", "Label",
"obj, displ, string, vars, boxed=0,",
"(Int(boxed), kig_relpoint(obj, displ), \
kig_string(string))+tuple(vars),"),
###### Python scripting... we need some work here...
("PythonScript", "PythonExecuteType",
"script, argvec,",
'(Object("PythonCompileType", (kig_string(script),), shown,\
name, internal, width, pointstyle, linestyle,\
color),)+tuple(argvec),'),
)
def objectbuild(nameclass, namekig, params, objparams):
"""Create string with a Object class definition."""
return """class %s(Object):
def __init__(self, %s shown=None, name=None, internal=None,
width=None, pointstyle=None, linestyle=None, color=None):
Object.__init__(self, "%s", %s shown, name, internal,
width, pointstyle, linestyle, color)
""" % (nameclass, params, namekig, objparams)
for o in objects:
p1, p2, p3, p4 = o
exec objectbuild(p1, p2, p3, p4)
#####
# Propertys
#####
property=(\
("Type", "base-object-type", "o,", "o,"),
("Coordinate", "coordinate", "point,", "point,"),
("XCoord", "coordinate-x", "point,", "point,"),
("YCoord", "coordinate-y", "point,", "point,"),
("MidPoints", "mid-point", "a, b,", "Segment(a, b, internal=True),"),
("MidPoint", "mid-point", "segment,", "segment,"),
("EndPointA", "end-point-A", "segment,", "segment,"),
("EndPointB", "end-point-B", "segment,", "segment,"),
("Length", "length", "segment,", "segment,"),
("Equation", "equation", "segment,", "segment,"),
("Slope", "slope", "segment,", "segment,"),
("NumOfSides", "polygon-number-of-sides", "poly,", "poly,"),
("Perimeter", "polygon-perimeter", "poly,", "poly,"),
("Surface", "polygon-surface", "poly,", "poly,"),
("CenterOfMass", "polygon-center-of-mass", "poly,", "poly,"),
("WindingNumber", "polygon-winding-number", "poly,", "poly,"),
("Radius", "radius", "circle,", "circle,"),
("Center", "center", "circle,", "circle,"),
("Bisector", "angle-bisector", "angle,", "angle,"),
("Support", "support", "object,", "object,"),
)
def propertybuild(nameclass, namekig, params, objparams):
"""Create string with a Property class definition."""
return """class %s(Property):
def __init__(self, %s shown=None, name=None, internal=False,
width=None, pointstyle=None, linestyle=None, color=None):
Property.__init__(self, "%s", %s shown, name, internal,
width, pointstyle, linestyle, color)
""" % (nameclass, params, namekig, objparams)
for p in property:
p1, p2, p3, p4 = p
exec propertybuild(p1, p2, p3, p4)
#####
# Start of properties definitions as Object's metod
#####
# da sistemare!
points =(Point, ConstrainedPoint, RelativePoint, PolygonVertex)
lines=(Segment, Ray, Vector, InvertLine)
segments=(Segment, Vector, PolygonSide, InvertSegment)
circles =(Circle, CircleBy3Points, CircularInversion, ArcBy3Points,
ArcByCenterPointAngle, InvertCircle)
polygons=(Polygon, PolygonBCV, Triangle)
angles =(Angle,)
supp = circles+lines
methods=(\
("coordinate", "coordinate", points),
("coordinate-x", "xcoord", points),
("coordinate-y", "ycoord", points),
("mid-point", "midpoint", segments),
("end-point-A", "endpointA", segments),
("end-point-B", "endpointB", segments),
("length", "length", segments),
("equation", "equation", lines),
("slope", "slope", lines),
("polygon-number-of-sides", "numofsides", polygons),
("polygon-perimeter", "perimeter", polygons),
("polygon-surface", "surface", polygons),
("polygon-center-of-mass", "centerofmass", polygons),
("polygon-winding-number", "windingnumber", polygons),
("center", "center", polygons),
("center", "center", circles),
("angle-bisector", "bisector", angles),
("support", "support", supp),
)
def methodsbuild(namekig):
"""Create string with a method class definition."""
return """def method(self,shown=None, name=None, internal=False,
width=None, pointstyle=None, linestyle=None, color=None):
return Property("%s", self, shown, name, internal,
width, pointstyle, linestyle, color)
""" % (namekig, )
for p in methods:
p1, p2, cl = p
exec methodsbuild(p1)
for c in cl:
setattr(c, p2, method)
#####
# Usage
#####
def usage(codexit):
print >> sys.stderr, """
usage: pykig.py [options...] file ...
Options:
-h, --help Show this text.
-o <kig_file>
--output <kig_file> output <kig_file> no call Kig
-v, --version output version
-n, --nokig no call Kig
examples:
$ pykig.py my_file.kpy
$ pykig.py -o output_file.kig my_file.kpy
$ ...
"""
sys.exit(codexit)
#####
# Main body
#####
import sys, traceback, os
#from math import * # for user's programs
import math # for user's programs
import getopt
import atexit
def prog():
try:
_opts, _args = getopt.getopt(sys.argv[1:], "hvno:",\
["help", "version", "nokig", "output="])
except getopt.GetoptError:
print "GetoptError"
usage(2)
_callKig=True
_of=False
for _opt, _arg in _opts:
if _opt in ("-h", "--help"):
usage(0)
if _opt in ("-v", "--version"):
print "version:", version
sys.exit(0)
if _opt in ("-n", "--nokig"):
_callKig=False
elif _opt in ("-o", "--output"):
_outfilename=_arg
_of=True
_callKig=False # se c'è il file di output, non viene chiamato Kig
if len(_args)==0:
_infilename=raw_input("Nome del file di input: ")
if not _infilename:
print "No Input filename"
usage(2)
elif len(_args)==1:
_infilename=_args[0]
else:
print "No infilename"
usage(2)
try:
_infile = open(_infilename, 'r')
except:
print >> sys.stderr, _infilename, 'unreadable'
sys.exit(2)
if _of:
if _outfilename=="-":
_n, _e = os.path.splitext(_infilename)
_outfilename=_n+'.kig'
else:
_outfilename="/tmp/pykig" + str(os.getpid()) + ".kig"
global kigdocument
kigdocument=KigDocument(_outfilename, _callKig, _of)
kd=kigdocument
try:
execfile(_infilename, globals())
except:
print >> sys.stderr, 'syntax error in', _infilename
_info = sys.exc_info() # vorrei stampare il traceback...
traceback.print_exc()
sys.exit(3)
kigdocument.close()
if _infile != sys.stdin:
_infile.close()
def lib():
_outfilename="/tmp/pykig" + str(os.getpid()) + ".kig"
global kigdocument
kigdocument=KigDocument(_outfilename)
kd=kigdocument
atexit.register(kigdocument.close)
if __name__ == "__main__":
prog()
else:
lib()
|