Changeset View
Changeset View
Standalone View
Standalone View
pykig/pykig.py
1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python3 | ||
---|---|---|---|---|---|
2 | # -*- coding: utf-8 -*- | 2 | # -*- coding: utf-8 -*- | ||
3 | #-------------------------------python------------------------pykig.py--# | 3 | #-------------------------------python------------------------pykig.py--# | ||
4 | # # | 4 | # # | ||
5 | # From Python to Kig # | 5 | # From Python to Kig # | ||
6 | # # | 6 | # # | ||
7 | #--Maurizio Paolini-Daniele Zambelli-----------------------------2005---# | 7 | #--Maurizio Paolini-Daniele Zambelli-----------------------------2005---# | ||
8 | # | 8 | # | ||
9 | # Some comments translated to English by Bartosz Dziewoński | 9 | # Some comments translated to English by Bartosz Dziewoński | ||
▲ Show 20 Lines • Show All 155 Lines • ▼ Show 20 Line(s) | |||||
165 | # print "KigDocument.__init__()" | 165 | # print "KigDocument.__init__()" | ||
166 | self.axes = "1" | 166 | self.axes = "1" | ||
167 | self.grid = "1" | 167 | self.grid = "1" | ||
168 | self.outfilename=outfilename | 168 | self.outfilename=outfilename | ||
169 | self.callkig=callkig | 169 | self.callkig=callkig | ||
170 | self.of=of | 170 | self.of=of | ||
171 | try: | 171 | try: | ||
172 | self.outfile = open(outfilename, 'w') | 172 | self.outfile = open(outfilename, 'w') | ||
173 | except IOError, value: | 173 | except IOError as value: | ||
174 | # print >> sys.stderr, outfilename, 'unwritable' | 174 | # print >> sys.stderr, outfilename, 'unwritable' | ||
175 | print >> sys.stderr, value | 175 | print(value, file=sys.stderr) | ||
176 | sys.exit(2) | 176 | sys.exit(2) | ||
177 | # KigOut._kigdocument=self | 177 | # KigOut._kigdocument=self | ||
178 | KigDOP._kd=self | 178 | KigDOP._kd=self | ||
179 | KigView._kd=self | 179 | KigView._kd=self | ||
180 | self.viewkig=[] | 180 | self.viewkig=[] | ||
181 | self.hierarchy=[] | 181 | self.hierarchy=[] | ||
182 | # Default values | 182 | # Default values | ||
183 | self.internal=False | 183 | self.internal=False | ||
Show All 23 Lines | 206 | try: | |||
207 | self.outfile.write(self.str_open()) | 207 | self.outfile.write(self.str_open()) | ||
208 | self.outfile.writelines(self.hierarchy) | 208 | self.outfile.writelines(self.hierarchy) | ||
209 | self.outfile.write(" </Hierarchy>\n <View>\n") | 209 | self.outfile.write(" </Hierarchy>\n <View>\n") | ||
210 | for f in self.viewkig: | 210 | for f in self.viewkig: | ||
211 | self.outfile.write(f.str_view()) | 211 | self.outfile.write(f.str_view()) | ||
212 | self.outfile.write(" </View>\n</KigDocument>\n") | 212 | self.outfile.write(" </View>\n</KigDocument>\n") | ||
213 | if self.outfile != sys.stdout: | 213 | if self.outfile != sys.stdout: | ||
214 | self.outfile.close() | 214 | self.outfile.close() | ||
215 | except IOError, value: | 215 | except IOError as value: | ||
216 | print >> sys.stderr, value | 216 | print(value, file=sys.stderr) | ||
217 | sys.exit(2) | 217 | sys.exit(2) | ||
218 | try: | 218 | try: | ||
219 | if self.callkig: | 219 | if self.callkig: | ||
220 | err = os.system('kig ' + self.outfilename) | 220 | err = os.system('kig ' + self.outfilename) | ||
221 | except Exception, value: | 221 | except Exception as value: | ||
222 | print >> sys.stderr, value | 222 | print(value, file=sys.stderr) | ||
223 | if not self.of: | 223 | if not self.of: | ||
224 | os.system('rm ' + self.outfilename) | 224 | os.system('rm ' + self.outfilename) | ||
225 | 225 | | |||
226 | def noaxes(self): self.axes="0" | 226 | def noaxes(self): self.axes="0" | ||
227 | def nogrid(self): self.grid="0" | 227 | def nogrid(self): self.grid="0" | ||
228 | def hideobjects(self): self.shown=HIDDEN | 228 | def hideobjects(self): self.shown=HIDDEN | ||
229 | def showobjects(self): self.shown=VISIBLE | 229 | def showobjects(self): self.shown=VISIBLE | ||
230 | def setwidth(self, w): self.width=w | 230 | def setwidth(self, w): self.width=w | ||
▲ Show 20 Lines • Show All 265 Lines • ▼ Show 20 Line(s) | 494 | def databuild(nomeclasse, nomekig, v="val"): | |||
496 | return """class %s(Data): | 496 | return """class %s(Data): | ||
497 | 497 | | |||
498 | def __init__(self, val): | 498 | def __init__(self, val): | ||
499 | Data.__init__(self, "%s", %s) | 499 | Data.__init__(self, "%s", %s) | ||
500 | """ % (nomeclasse, nomekig, v) | 500 | """ % (nomeclasse, nomekig, v) | ||
501 | 501 | | |||
502 | for d in data: | 502 | for d in data: | ||
503 | p1, p2, p3 = d | 503 | p1, p2, p3 = d | ||
504 | exec databuild(p1, p2, p3) | 504 | exec (databuild(p1, p2, p3)) | ||
505 | 505 | | |||
506 | ##### | 506 | ##### | ||
507 | # Objects | 507 | # Objects | ||
508 | ##### | 508 | ##### | ||
509 | """To add: | 509 | """To add: | ||
510 | ("ConvexHull", "ConvexHull", "polygon,", "(polygon,),"), | 510 | ("ConvexHull", "ConvexHull", "polygon,", "(polygon,),"), | ||
511 | ("EllipseByFocusFocusPoint", "EllipseBFFP", "p1, p2, p3,", "(p1, p2, p3,),"), | 511 | ("EllipseByFocusFocusPoint", "EllipseBFFP", "p1, p2, p3,", "(p1, p2, p3,),"), | ||
512 | ("HyperbolaByFocusFocusPoint", "HyperbolaBFFP", "p1, p2, p3,", "(p1, p2, p3,),"), | 512 | ("HyperbolaByFocusFocusPoint", "HyperbolaBFFP", "p1, p2, p3,", "(p1, p2, p3,),"), | ||
▲ Show 20 Lines • Show All 127 Lines • ▼ Show 20 Line(s) | 636 | def objectbuild(nameclass, namekig, params, objparams): | |||
640 | def __init__(self, %s shown=None, name=None, internal=None, | 640 | def __init__(self, %s shown=None, name=None, internal=None, | ||
641 | width=None, pointstyle=None, linestyle=None, color=None): | 641 | width=None, pointstyle=None, linestyle=None, color=None): | ||
642 | Object.__init__(self, "%s", %s shown, name, internal, | 642 | Object.__init__(self, "%s", %s shown, name, internal, | ||
643 | width, pointstyle, linestyle, color) | 643 | width, pointstyle, linestyle, color) | ||
644 | """ % (nameclass, params, namekig, objparams) | 644 | """ % (nameclass, params, namekig, objparams) | ||
645 | 645 | | |||
646 | for o in objects: | 646 | for o in objects: | ||
647 | p1, p2, p3, p4 = o | 647 | p1, p2, p3, p4 = o | ||
648 | exec objectbuild(p1, p2, p3, p4) | 648 | exec (objectbuild(p1, p2, p3, p4)) | ||
649 | 649 | | |||
650 | ##### | 650 | ##### | ||
651 | # Properties | 651 | # Properties | ||
652 | ##### | 652 | ##### | ||
653 | 653 | | |||
654 | property=(\ | 654 | property=(\ | ||
655 | ("Type", "base-object-type", "o,", "o,"), | 655 | ("Type", "base-object-type", "o,", "o,"), | ||
656 | ("Coordinate", "coordinate", "point,", "point,"), | 656 | ("Coordinate", "coordinate", "point,", "point,"), | ||
Show All 26 Lines | 679 | def propertybuild(nameclass, namekig, params, objparams): | |||
683 | def __init__(self, %s shown=None, name=None, internal=False, | 683 | def __init__(self, %s shown=None, name=None, internal=False, | ||
684 | width=None, pointstyle=None, linestyle=None, color=None): | 684 | width=None, pointstyle=None, linestyle=None, color=None): | ||
685 | Property.__init__(self, "%s", %s shown, name, internal, | 685 | Property.__init__(self, "%s", %s shown, name, internal, | ||
686 | width, pointstyle, linestyle, color) | 686 | width, pointstyle, linestyle, color) | ||
687 | """ % (nameclass, params, namekig, objparams) | 687 | """ % (nameclass, params, namekig, objparams) | ||
688 | 688 | | |||
689 | for p in property: | 689 | for p in property: | ||
690 | p1, p2, p3, p4 = p | 690 | p1, p2, p3, p4 = p | ||
691 | exec propertybuild(p1, p2, p3, p4) | 691 | exec (propertybuild(p1, p2, p3, p4)) | ||
692 | 692 | | |||
693 | ##### | 693 | ##### | ||
694 | # Start of properties definitions as Object's methods | 694 | # Start of properties definitions as Object's methods | ||
695 | ##### | 695 | ##### | ||
696 | # to be cleaned up! | 696 | # to be cleaned up! | ||
697 | points =(Point, ConstrainedPoint, RelativePoint, PolygonVertex) | 697 | points =(Point, ConstrainedPoint, RelativePoint, PolygonVertex) | ||
698 | lines=(Segment, Ray, Vector, InvertLine) | 698 | lines=(Segment, Ray, Vector, InvertLine) | ||
699 | segments=(Segment, Vector, PolygonSide, InvertSegment) | 699 | segments=(Segment, Vector, PolygonSide, InvertSegment) | ||
Show All 29 Lines | 727 | def methodsbuild(namekig): | |||
729 | return """def method(self,shown=None, name=None, internal=False, | 729 | return """def method(self,shown=None, name=None, internal=False, | ||
730 | width=None, pointstyle=None, linestyle=None, color=None): | 730 | width=None, pointstyle=None, linestyle=None, color=None): | ||
731 | return Property("%s", self, shown, name, internal, | 731 | return Property("%s", self, shown, name, internal, | ||
732 | width, pointstyle, linestyle, color) | 732 | width, pointstyle, linestyle, color) | ||
733 | """ % (namekig, ) | 733 | """ % (namekig, ) | ||
734 | 734 | | |||
735 | for p in methods: | 735 | for p in methods: | ||
736 | p1, p2, cl = p | 736 | p1, p2, cl = p | ||
737 | exec methodsbuild(p1) | 737 | exec (methodsbuild(p1)) | ||
738 | for c in cl: | 738 | for c in cl: | ||
739 | setattr(c, p2, method) | 739 | setattr(c, p2, method) | ||
740 | 740 | | |||
741 | ##### | 741 | ##### | ||
742 | # Usage | 742 | # Usage | ||
743 | ##### | 743 | ##### | ||
744 | 744 | | |||
745 | def usage(codexit): | 745 | def usage(codexit): | ||
746 | print >> sys.stderr, """ | 746 | print (""" | ||
747 | usage: pykig.py [options...] file ... | 747 | usage: pykig.py [options...] file ... | ||
748 | 748 | | |||
749 | Options: | 749 | Options: | ||
750 | -h, --help Show this text. | 750 | -h, --help Show this text. | ||
751 | -o <kig_file> | 751 | -o <kig_file> | ||
752 | --output <kig_file> Output to <kig_file> and don't call Kig. | 752 | --output <kig_file> Output to <kig_file> and don't call Kig. | ||
753 | -v, --version Output version and exit. | 753 | -v, --version Output version and exit. | ||
754 | -n, --nokig Don't call Kig. | 754 | -n, --nokig Don't call Kig. | ||
755 | 755 | | |||
756 | examples: | 756 | examples: | ||
757 | $ pykig.py my_file.kpy | 757 | $ pykig.py my_file.kpy | ||
758 | $ pykig.py -o output_file.kig my_file.kpy | 758 | $ pykig.py -o output_file.kig my_file.kpy | ||
759 | $ ... | 759 | $ ... | ||
760 | """ | 760 | """, file=sys.stderr) | ||
761 | sys.exit(codexit) | 761 | sys.exit(codexit) | ||
762 | 762 | | |||
763 | ##### | 763 | ##### | ||
764 | # Main body | 764 | # Main body | ||
765 | ##### | 765 | ##### | ||
766 | 766 | | |||
767 | import sys, traceback, os | 767 | import sys, traceback, os | ||
768 | #from math import * # for user's programs | 768 | #from math import * # for user's programs | ||
769 | import math # for user's programs | 769 | import math # for user's programs | ||
770 | import getopt | 770 | import getopt | ||
771 | import atexit | 771 | import atexit | ||
772 | import xml.sax.saxutils | 772 | import xml.sax.saxutils | ||
773 | 773 | | |||
774 | def prog(): | 774 | def prog(): | ||
775 | try: | 775 | try: | ||
776 | _opts, _args = getopt.getopt(sys.argv[1:], "hvno:",\ | 776 | _opts, _args = getopt.getopt(sys.argv[1:], "hvno:",\ | ||
777 | ["help", "version", "nokig", "output="]) | 777 | ["help", "version", "nokig", "output="]) | ||
778 | except getopt.GetoptError: | 778 | except getopt.GetoptError: | ||
779 | print "GetoptError" | 779 | print ("GetoptError") | ||
780 | usage(2) | 780 | usage(2) | ||
781 | _callKig=True | 781 | _callKig=True | ||
782 | _of=False | 782 | _of=False | ||
783 | for _opt, _arg in _opts: | 783 | for _opt, _arg in _opts: | ||
784 | if _opt in ("-h", "--help"): | 784 | if _opt in ("-h", "--help"): | ||
785 | usage(0) | 785 | usage(0) | ||
786 | if _opt in ("-v", "--version"): | 786 | if _opt in ("-v", "--version"): | ||
787 | print "Version:", version | 787 | print ("Version:", version) | ||
788 | sys.exit(0) | 788 | sys.exit(0) | ||
789 | if _opt in ("-n", "--nokig"): | 789 | if _opt in ("-n", "--nokig"): | ||
790 | _callKig=False | 790 | _callKig=False | ||
791 | elif _opt in ("-o", "--output"): | 791 | elif _opt in ("-o", "--output"): | ||
792 | _outfilename=_arg | 792 | _outfilename=_arg | ||
793 | _of=True | 793 | _of=True | ||
794 | _callKig=False # if there's an output file, don't call Kig | 794 | _callKig=False # if there's an output file, don't call Kig | ||
795 | if len(_args)==0: | 795 | if len(_args)==0: | ||
796 | _infilename=raw_input("Input file name: ") | 796 | _infilename=input("Input file name: ") | ||
797 | if not _infilename: | 797 | if not _infilename: | ||
798 | print "No input file name." | 798 | print ("No input file name.") | ||
799 | usage(2) | 799 | usage(2) | ||
800 | elif len(_args)==1: | 800 | elif len(_args)==1: | ||
801 | _infilename=_args[0] | 801 | _infilename=_args[0] | ||
802 | else: | 802 | else: | ||
803 | print "No input file name." | 803 | print ("No input file name.") | ||
804 | usage(2) | 804 | usage(2) | ||
805 | try: | 805 | try: | ||
806 | _infile = open(_infilename, 'r') | 806 | _infile = open(_infilename, 'r') | ||
807 | except: | 807 | except: | ||
808 | print >> sys.stderr, _infilename, "input file can't be read." | 808 | print (_infilename, "input file can't be read.", file=sys.stderr) | ||
yurchor: Why is this commented out? | |||||
arojas: Unintended, I probably pressed CTRL+D instead of CTRL+C | |||||
809 | sys.exit(2) | 809 | sys.exit(2) | ||
810 | if _of: | 810 | if _of: | ||
811 | if _outfilename=="-": | 811 | if _outfilename=="-": | ||
812 | _n, _e = os.path.splitext(_infilename) | 812 | _n, _e = os.path.splitext(_infilename) | ||
813 | _outfilename=_n+'.kig' | 813 | _outfilename=_n+'.kig' | ||
814 | else: | 814 | else: | ||
815 | _outfilename="/tmp/pykig" + str(os.getpid()) + ".kig" | 815 | _outfilename="/tmp/pykig" + str(os.getpid()) + ".kig" | ||
816 | global kigdocument | 816 | global kigdocument | ||
817 | kigdocument=KigDocument(_outfilename, _callKig, _of) | 817 | kigdocument=KigDocument(_outfilename, _callKig, _of) | ||
818 | kd=kigdocument | 818 | kd=kigdocument | ||
819 | try: | 819 | try: | ||
820 | execfile(_infilename, globals()) | 820 | exec(open(_infilename).read(), globals()) | ||
821 | except: | 821 | except: | ||
822 | print >> sys.stderr, 'Syntax error in', _infilename | 822 | print ('Syntax error in', _infilename, file=sys.stderr) | ||
823 | _info = sys.exc_info() # print out the traceback | 823 | _info = sys.exc_info() # print out the traceback | ||
824 | traceback.print_exc() | 824 | traceback.print_exc() | ||
825 | sys.exit(3) | 825 | sys.exit(3) | ||
826 | kigdocument.close() | 826 | kigdocument.close() | ||
827 | 827 | | |||
828 | if _infile != sys.stdin: | 828 | if _infile != sys.stdin: | ||
829 | _infile.close() | 829 | _infile.close() | ||
830 | 830 | | |||
Show All 11 Lines |
Why is this commented out?