Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorolpc user <olpc@xo-74-32-56.localdomain>2012-06-16 18:18:25 (GMT)
committer olpc user <olpc@xo-74-32-56.localdomain>2012-06-16 18:18:25 (GMT)
commit1c8cf1f18784f9db7d5fa980c71ed70297e2c548 (patch)
treef6b0f671fa23fb6d71536c7d0937836cc10ec49d
parent351ced843ead1dee29fb59f805815a60f3e203a6 (diff)
Added pynxc
-rw-r--r--pynxc/MyDefs.h69
-rw-r--r--pynxc/ast_template.py372
-rw-r--r--pynxc/nxc/.DS_Storebin0 -> 6148 bytes
-rw-r--r--pynxc/nxc/README10
-rw-r--r--pynxc/nxc/darwin/.DS_Storebin0 -> 6148 bytes
-rwxr-xr-xpynxc/nxc/darwin/nbcbin0 -> 5422736 bytes
-rwxr-xr-xpynxc/nxc/darwin/nxtcombin0 -> 113388 bytes
-rw-r--r--pynxc/nxc/darwin/place_nbc_and_nxtcom_here.txt0
-rw-r--r--pynxc/nxc/linux2/.DS_Storebin0 -> 6148 bytes
-rwxr-xr-xpynxc/nxc/linux2/nbcbin0 -> 1787240 bytes
-rwxr-xr-xpynxc/nxc/win32/nbc.exebin0 -> 1295360 bytes
-rw-r--r--pynxc/nxc/win32/place_nbc_exe_here.txt0
-rwxr-xr-xpynxc/pynxc.pyw1392
-rw-r--r--pynxc/pynxc.yaml1
-rw-r--r--pynxc/tutorial_samples/10_mutex.py25
-rw-r--r--pynxc/tutorial_samples/10_semaphore.py41
-rw-r--r--pynxc/tutorial_samples/11_DC_master.py37
-rw-r--r--pynxc/tutorial_samples/11_ack_master.py30
-rw-r--r--pynxc/tutorial_samples/11_ack_slave.py26
-rw-r--r--pynxc/tutorial_samples/11_msg_master.py32
-rw-r--r--pynxc/tutorial_samples/11_msg_slave.py31
-rw-r--r--pynxc/tutorial_samples/12_ASCII.py16
-rw-r--r--pynxc/tutorial_samples/12_ReadStr.py40
-rw-r--r--pynxc/tutorial_samples/12_WriteStr.py27
-rw-r--r--pynxc/tutorial_samples/12_fs_numbers_int.py20
-rw-r--r--pynxc/tutorial_samples/12_fs_numbers_long.py21
-rw-r--r--pynxc/tutorial_samples/12_graphics.py30
-rw-r--r--pynxc/tutorial_samples/12_timers.py14
-rw-r--r--pynxc/tutorial_samples/12_wait.py12
-rw-r--r--pynxc/tutorial_samples/1_errors.py8
-rw-r--r--pynxc/tutorial_samples/1_simple.nxc79
-rw-r--r--pynxc/tutorial_samples/1_simple.py9
-rw-r--r--pynxc/tutorial_samples/2_repeat_a.py15
-rw-r--r--pynxc/tutorial_samples/2_repeat_b.py14
-rw-r--r--pynxc/tutorial_samples/2_repeat_c.py23
-rw-r--r--pynxc/tutorial_samples/2_turn_b.py9
-rw-r--r--pynxc/tutorial_samples/3_random.py9
-rw-r--r--pynxc/tutorial_samples/3_spiral.py14
-rw-r--r--pynxc/tutorial_samples/3_variables.py15
-rw-r--r--pynxc/tutorial_samples/4_if.py16
-rw-r--r--pynxc/tutorial_samples/5_light.py14
-rw-r--r--pynxc/tutorial_samples/5_sound.py20
-rw-r--r--pynxc/tutorial_samples/5_touch.py10
-rw-r--r--pynxc/tutorial_samples/5_ultrasonic.py13
-rw-r--r--pynxc/tutorial_samples/5_wait.py6
-rw-r--r--pynxc/tutorial_samples/6_define_a.py13
-rw-r--r--pynxc/tutorial_samples/6_define_b.py16
-rw-r--r--pynxc/tutorial_samples/6_inline.py17
-rw-r--r--pynxc/tutorial_samples/6_inline2.py16
-rw-r--r--pynxc/tutorial_samples/6_subs.py13
-rw-r--r--pynxc/tutorial_samples/6_tasks.py23
-rw-r--r--pynxc/tutorial_samples/7_drive_music.py15
-rw-r--r--pynxc/tutorial_samples/7_music.py8
-rw-r--r--pynxc/tutorial_samples/7_sounds.py22
-rw-r--r--pynxc/tutorial_samples/8_OnReg.py17
-rw-r--r--pynxc/tutorial_samples/8_OnSync.py15
-rw-r--r--pynxc/tutorial_samples/8_PID.py8
-rw-r--r--pynxc/tutorial_samples/8_RotateMotor.py4
-rw-r--r--pynxc/tutorial_samples/8_RotateMotorEx.py7
-rw-r--r--pynxc/tutorial_samples/8_float.py8
-rw-r--r--pynxc/tutorial_samples/9_multiple.py38
-rw-r--r--pynxc/tutorial_samples/9_pulses.py14
-rw-r--r--pynxc/tutorial_samples/9_rotation.py17
-rw-r--r--pynxc/tutorial_samples/test1.py9
-rw-r--r--pynxc/tutorial_samples/test_string.py5
-rw-r--r--pynxc/waxy/.DS_Storebin0 -> 6148 bytes
-rw-r--r--pynxc/waxy/__init__.py95
-rw-r--r--pynxc/waxy/__init__.py~77
-rw-r--r--pynxc/waxy/aboutbox.py43
-rw-r--r--pynxc/waxy/aboutbox.py~43
-rw-r--r--pynxc/waxy/application.py32
-rw-r--r--pynxc/waxy/application.py~36
-rw-r--r--pynxc/waxy/artprovider.py126
-rw-r--r--pynxc/waxy/bitmap.py29
-rw-r--r--pynxc/waxy/bitmap.py~29
-rw-r--r--pynxc/waxy/bitmapbutton.py49
-rw-r--r--pynxc/waxy/bitmapbutton.py~50
-rw-r--r--pynxc/waxy/button.py54
-rw-r--r--pynxc/waxy/button.py~51
-rw-r--r--pynxc/waxy/canvas.py33
-rw-r--r--pynxc/waxy/checkbox.py71
-rw-r--r--pynxc/waxy/checkbox.py~72
-rw-r--r--pynxc/waxy/checklistbox.py27
-rw-r--r--pynxc/waxy/checklistbox.py~28
-rw-r--r--pynxc/waxy/colordb.py844
-rw-r--r--pynxc/waxy/colourdialog.py34
-rw-r--r--pynxc/waxy/colourdialog.py~34
-rw-r--r--pynxc/waxy/combobox.py50
-rw-r--r--pynxc/waxy/combobox.py~51
-rw-r--r--pynxc/waxy/constants.py3
-rw-r--r--pynxc/waxy/containers.py311
-rw-r--r--pynxc/waxy/containers.py~314
-rw-r--r--pynxc/waxy/core.py36
-rw-r--r--pynxc/waxy/core.py~2
-rw-r--r--pynxc/waxy/customdialog.py66
-rw-r--r--pynxc/waxy/customdialog.py~67
-rw-r--r--pynxc/waxy/demos/.DS_Storebin0 -> 12292 bytes
-rw-r--r--pynxc/waxy/demos/.jedrecent11
-rwxr-xr-xpynxc/waxy/demos/AboutBox.py89
-rwxr-xr-xpynxc/waxy/demos/AboutBox.py~89
-rwxr-xr-xpynxc/waxy/demos/Button.py43
-rwxr-xr-xpynxc/waxy/demos/Button.py~42
-rwxr-xr-xpynxc/waxy/demos/Calendar.py43
-rw-r--r--pynxc/waxy/demos/Canvas.py43
-rwxr-xr-xpynxc/waxy/demos/ColorDialog.py49
-rw-r--r--pynxc/waxy/demos/ColorDialog.py~49
-rwxr-xr-xpynxc/waxy/demos/ColourDialog.py49
-rwxr-xr-xpynxc/waxy/demos/ColourDialog.py~49
-rwxr-xr-xpynxc/waxy/demos/DirectoryDialog.py75
-rwxr-xr-xpynxc/waxy/demos/DirectoryDialog.py~75
-rwxr-xr-xpynxc/waxy/demos/Drag.py110
-rwxr-xr-xpynxc/waxy/demos/FileDialog.py104
-rwxr-xr-xpynxc/waxy/demos/FileDialog.py~104
-rwxr-xr-xpynxc/waxy/demos/Grid.py66
-rwxr-xr-xpynxc/waxy/demos/Grid2.py177
-rwxr-xr-xpynxc/waxy/demos/MessageDialog.py98
-rwxr-xr-xpynxc/waxy/demos/MessageDialog.py~83
-rwxr-xr-xpynxc/waxy/demos/MultiChoiceDialog.py56
-rw-r--r--pynxc/waxy/demos/MultiChoiceDialog.py~56
-rwxr-xr-xpynxc/waxy/demos/ProgressDialog.py76
-rwxr-xr-xpynxc/waxy/demos/ProgressDialog.py~76
-rwxr-xr-xpynxc/waxy/demos/Rubberband.py221
-rwxr-xr-xpynxc/waxy/demos/SingleChoiceDialog.py89
-rwxr-xr-xpynxc/waxy/demos/SingleChoiceDialog.py~89
-rwxr-xr-xpynxc/waxy/demos/Slider.py51
-rwxr-xr-xpynxc/waxy/demos/TreeListView.py51
-rwxr-xr-xpynxc/waxy/demos/TreeView.py91
-rw-r--r--pynxc/waxy/demos/heretic2.pngbin0 -> 612 bytes
-rw-r--r--pynxc/waxy/demos/heretic2rev.pngbin0 -> 4621 bytes
-rw-r--r--pynxc/waxy/demos/plotit.py151
-rw-r--r--pynxc/waxy/dialog.py115
-rw-r--r--pynxc/waxy/dialog.py~108
-rw-r--r--pynxc/waxy/directorydialog.py39
-rw-r--r--pynxc/waxy/directorydialog.py~40
-rw-r--r--pynxc/waxy/dragdrop.py63
-rw-r--r--pynxc/waxy/dropdownbox.py40
-rw-r--r--pynxc/waxy/dropdownbox.py~40
-rw-r--r--pynxc/waxy/events.py33
-rw-r--r--pynxc/waxy/filedialog.py40
-rw-r--r--pynxc/waxy/filedialog.py~35
-rw-r--r--pynxc/waxy/flexgridpanel.py21
-rw-r--r--pynxc/waxy/flexgridpanel.py~22
-rw-r--r--pynxc/waxy/font.py18
-rw-r--r--pynxc/waxy/fontdialog.py35
-rw-r--r--pynxc/waxy/fontdialog.py~35
-rw-r--r--pynxc/waxy/frame.py67
-rw-r--r--pynxc/waxy/frame.py~68
-rw-r--r--pynxc/waxy/grid.py52
-rw-r--r--pynxc/waxy/gridpanel.py24
-rw-r--r--pynxc/waxy/htmlwindow.py15
-rw-r--r--pynxc/waxy/htmlwindow.py~15
-rw-r--r--pynxc/waxy/image.py115
-rw-r--r--pynxc/waxy/image.py~116
-rw-r--r--pynxc/waxy/imagelist.py38
-rw-r--r--pynxc/waxy/imagepanel.py34
-rw-r--r--pynxc/waxy/imagepanel.py~33
-rw-r--r--pynxc/waxy/keys.py44
-rw-r--r--pynxc/waxy/label.py33
-rw-r--r--pynxc/waxy/label.py~33
-rw-r--r--pynxc/waxy/line.py18
-rw-r--r--pynxc/waxy/line.py~18
-rw-r--r--pynxc/waxy/listbox.py92
-rw-r--r--pynxc/waxy/listbox.py~92
-rw-r--r--pynxc/waxy/menu.py135
-rw-r--r--pynxc/waxy/menu.py~129
-rw-r--r--pynxc/waxy/messagedialog.py70
-rw-r--r--pynxc/waxy/messagedialog.py~63
-rw-r--r--pynxc/waxy/mousepointer.py95
-rw-r--r--pynxc/waxy/multichoicedialog.py29
-rw-r--r--pynxc/waxy/multichoicedialog.py~27
-rw-r--r--pynxc/waxy/notebook.py51
-rw-r--r--pynxc/waxy/notebook.py~51
-rw-r--r--pynxc/waxy/notes.txt21
-rw-r--r--pynxc/waxy/notes.txt~20
-rw-r--r--pynxc/waxy/panel.py27
-rw-r--r--pynxc/waxy/panel.py~28
-rw-r--r--pynxc/waxy/progressdialog.py28
-rw-r--r--pynxc/waxy/progressdialog.py~28
-rw-r--r--pynxc/waxy/singlechoicedialog.py29
-rw-r--r--pynxc/waxy/singlechoicedialog.py~29
-rw-r--r--pynxc/waxy/slider.py104
-rw-r--r--pynxc/waxy/splitter.py56
-rw-r--r--pynxc/waxy/splitter.py~62
-rw-r--r--pynxc/waxy/statusbar.py37
-rw-r--r--pynxc/waxy/styles.py160
-rw-r--r--pynxc/waxy/styles.py~160
-rw-r--r--pynxc/waxy/textbox.py128
-rw-r--r--pynxc/waxy/textbox.py~128
-rw-r--r--pynxc/waxy/textentrydialog.py179
-rw-r--r--pynxc/waxy/treelistview.py28
-rw-r--r--pynxc/waxy/treeview.py119
-rw-r--r--pynxc/waxy/utils.py29
-rw-r--r--pynxc/waxy/waxyobject.py246
-rw-r--r--pynxc/waxy/waxyobject.py~246
-rw-r--r--pynxc/yaml/__init__.py288
-rw-r--r--pynxc/yaml/composer.py139
-rw-r--r--pynxc/yaml/constructor.py684
-rw-r--r--pynxc/yaml/cyaml.py85
-rw-r--r--pynxc/yaml/dumper.py62
-rw-r--r--pynxc/yaml/emitter.py1135
-rw-r--r--pynxc/yaml/error.py75
-rw-r--r--pynxc/yaml/events.py86
-rw-r--r--pynxc/yaml/loader.py40
-rw-r--r--pynxc/yaml/nodes.py49
-rw-r--r--pynxc/yaml/parser.py584
-rw-r--r--pynxc/yaml/reader.py225
-rw-r--r--pynxc/yaml/representer.py489
-rw-r--r--pynxc/yaml/resolver.py224
-rw-r--r--pynxc/yaml/scanner.py1457
-rw-r--r--pynxc/yaml/serializer.py111
-rw-r--r--pynxc/yaml/tokens.py104
211 files changed, 17987 insertions, 0 deletions
diff --git a/pynxc/MyDefs.h b/pynxc/MyDefs.h
new file mode 100644
index 0000000..ebbdca6
--- /dev/null
+++ b/pynxc/MyDefs.h
@@ -0,0 +1,69 @@
+#define ULTRASONIC 1
+#define EYES 1
+#define LIGHT 2
+#define SOUND 3
+#define MIC 3
+#define TOUCH 4
+
+#define None 0
+
+
+int SensorTypes[]={0,0,0,0};
+
+
+
+inline int SensorVal(int s_) {
+
+ s_=s_-1;
+ int t_ = SensorTypes[s_];
+
+ if (t_==None) {
+ return -1;
+ } else if (t_==ULTRASONIC) {
+ Wait(100);
+ return SensorUS(s_);
+ } else {
+ return Sensor(s_);
+ }
+
+}
+
+
+
+void DefineSensors(int s1,int s2,int s3,int s4) {
+
+ int sa[];
+ int i;
+
+ ArrayInit(sa,0,4);
+ sa[0]=s1;
+ sa[1]=s2;
+ sa[2]=s3;
+ sa[3]=s4;
+
+ for (i=0; i<4; i++) {
+
+ if (sa[i]==None) {
+ SensorTypes[i]=None;
+ } else if (sa[i]==ULTRASONIC) {
+ SetSensorLowspeed(i);
+ SensorTypes[i]=ULTRASONIC;
+ } else if (sa[i]==LIGHT) {
+ SetSensorLight(i);
+ SensorTypes[i]=LIGHT;
+ } else if (sa[i]==SOUND) {
+ SetSensorSound(i);
+ SensorTypes[i]=SOUND;
+ } else if (sa[i]==TOUCH) {
+ SetSensor(i,SENSOR_TOUCH);
+ SensorTypes[i]=TOUCH;
+ }
+
+
+
+
+ }
+
+
+}
+
diff --git a/pynxc/ast_template.py b/pynxc/ast_template.py
new file mode 100644
index 0000000..d5af4d8
--- /dev/null
+++ b/pynxc/ast_template.py
@@ -0,0 +1,372 @@
+#!/usr/bin/env python
+__author__ = ''
+__version__ = (0,0)
+
+import sys
+import compiler.ast
+from compiler.visitor import ASTVisitor
+from compiler import parse, walk
+from compiler.consts import *
+
+class Visitor(ASTVisitor):
+ def __init__(self,stream=sys.stdout,debug=False):
+ self.v = lambda tree, visitor=self: walk(tree, visitor)
+ self.stream = stream
+ self.strcode = ""
+ self.debug=debug
+ self.indents = 0
+ ASTVisitor.__init__(self)
+
+ def __str__(self):
+ return self.strcode
+
+ def DEDENT(self):
+ self.indents -=1
+ self.NEWLINE()
+
+ def INDENT(self):
+ self.indents += 1
+ self.NEWLINE()
+
+ def NEWLINE(self):
+ self.write('\n')
+ self.write(' ' * 4 * self.indents )
+
+ def write(self, data):
+ if self.stream:
+ self.stream.write(data)
+ self.strcode += data
+
+ def visitBlock(self, block):
+ if self.debug:
+ print 'visitBlock'
+
+ def visitAdd(self, node):
+ if self.debug:
+ print 'visitAdd'
+
+ def visitAnd(self, node):
+ if self.debug:
+ print 'visitAnd'
+
+ def visitAssAttr(self, node):
+ if self.debug:
+ print 'visitAssAttr'
+
+ def visitAssList(self, node):
+ if self.debug:
+ print 'visitAssList'
+
+ def visitAssName(self, node):
+ if self.debug:
+ print 'visitAssName'
+ if node.flags == OP_DELETE:
+ print "del ",
+ print node.name
+
+
+ def visitAssTuple(self, node):
+ if self.debug:
+ print 'visitAssTuple'
+
+ def visitAssert(self, node):
+ if self.debug:
+ print 'visitAssert'
+
+ def visitAssign(self, node):
+ if self.debug:
+ print 'visitAssign'
+ for i in range(len(node.nodes)):
+ n = node.nodes[i]
+ if self.debug:
+ print " Node ",n
+ self.v(n)
+ self.v(node.expr)
+
+ def visitAugAssign(self, node):
+ if self.debug:
+ print 'visitAugAssign'
+ self.v(node.node)
+ self.v(node.expr)
+
+ def visitBackquote(self, node):
+ if self.debug:
+ print 'visitBackquote'
+
+ def visitBitand(self, node):
+ if self.debug:
+ print 'visitBitand'
+
+ def visitBitor(self, node):
+ if self.debug:
+ print 'visitBitor'
+
+ def visitBitxor(self, node):
+ if self.debug:
+ print 'visitBitxor'
+
+ def visitBreak(self, node):
+ if self.debug:
+ print 'visitBreak'
+
+ def visitCallFunc(self, node):
+ if self.debug:
+ print 'visitCallFunc'
+ print 'funcname: ',node.node.name
+
+
+ self.v(node.node)
+ for i in range(len(node.args)):
+ self.v(node.args[i])
+
+
+
+
+ def visitClass(self, node):
+ if self.debug:
+ print 'visitClass'
+
+ def visitCompare(self, node):
+ if self.debug:
+ print 'visitCompare'
+ self.v(node.expr)
+ for operator, operand in node.ops:
+ self.v(operand)
+
+ def visitConst(self, node):
+ if self.debug:
+ print 'visitConst'
+ print "Const",repr(node.value)
+
+ def visitContinue(self, node):
+ if self.debug:
+ print 'visitContinue'
+
+ def visitDecorators(self, node):
+ if self.debug:
+ print 'visitDecorators'
+
+ def visitDict(self, node):
+ if self.debug:
+ print 'visitDict'
+
+ def visitDiscard(self, node):
+ if self.debug:
+ print 'visitDiscard'
+ self.v(node.expr)
+
+ def visitDiv(self, node):
+ if self.debug:
+ print 'visitDiv'
+
+ def visitFloorDiv(self, node):
+ if self.debug:
+ print 'visitFloorDiv'
+
+ def visitEllipsis(self, node):
+ if self.debug:
+ print 'visitEllipsis'
+
+ def visitExec(self, node):
+ if self.debug:
+ print 'visitExec'
+
+ def visitFor(self, node):
+ if self.debug:
+ print 'visitFor'
+
+ def visitFrom(self, node):
+ if self.debug:
+ print 'visitFrom'
+
+ def visitFunction(self, node):
+ if self.debug:
+ print 'visitFunction'
+ self.v(node.code)
+
+ def visitGenExpr(self, node):
+ if self.debug:
+ print 'visitGenExpr'
+
+ def visitGenExprFor(self, node):
+ if self.debug:
+ print 'visitGenExprFor'
+
+ def visitGetattr(self, node):
+ if self.debug:
+ print 'visitGetattr'
+
+ def visitGlobal(self, node):
+ if self.debug:
+ print 'visitGlobal'
+
+ def visitIf(self, node):
+ if self.debug:
+ print 'visitIf'
+
+ (c, b) = node.tests[0]
+ self.v(c)
+ self.v(b)
+
+ for c, b in node.tests[1:]:
+ self.v(c)
+ self.v(b)
+ if node.else_:
+ self.v(node.else_)
+
+ def visitImport(self, node):
+ if self.debug:
+ print 'visitImport'
+
+ def visitInvert(self, node):
+ if self.debug:
+ print 'visitInvert'
+
+ def visitKeyword(self, node):
+ if self.debug:
+ print 'visitKeyword'
+ self.v(node.expr)
+
+ def visitLambda(self, node):
+ if self.debug:
+ print 'visitLambda'
+
+ def visitLeftShift(self, node):
+ if self.debug:
+ print 'visitLeftShift'
+
+ def visitList(self, node):
+ if self.debug:
+ print 'visitList'
+
+ def visitListComp(self, node):
+ if self.debug:
+ print 'visitListComp'
+
+ def visitListCompFor(self, node):
+ if self.debug:
+ print 'visitListCompFor'
+
+ def visitListCompIf(self, node):
+ if self.debug:
+ print 'visitListCompIf'
+
+ def visitMod(self, node):
+ if self.debug:
+ print 'visitMod'
+
+ def visitModule(self, node):
+ if self.debug:
+ print 'visitModule'
+ self.v(node.node)
+
+ def visitMul(self, node):
+ if self.debug:
+ print 'visitMul'
+
+ def visitName(self, node):
+ if self.debug:
+ print 'visitName'
+ print "Name",node.name
+
+ def visitNot(self, node):
+ if self.debug:
+ print 'visitNot'
+
+ def visitOr(self, node):
+ if self.debug:
+ print 'visitOr'
+
+ def visitPass(self, node):
+ if self.debug:
+ print 'visitPass'
+
+ def visitPower(self, node):
+ if self.debug:
+ print 'visitPower'
+
+ def visitPrint(self, node):
+ if self.debug:
+ print 'visitPrint'
+
+ def visitPrintnl(self, node):
+ if self.debug:
+ print 'visitPrintnl'
+
+ def visitRaise(self, node):
+ if self.debug:
+ print 'visitRaise'
+
+ def visitReturn(self, node):
+ if self.debug:
+ print 'visitReturn'
+
+ def visitRightShift(self, node):
+ if self.debug:
+ print 'visitRightShift'
+
+ def visitSlice(self, node):
+ if self.debug:
+ print 'visitSlice'
+
+ def visitStmt(self, node):
+ if self.debug:
+ print 'visitStmt'
+ for n in node.nodes:
+ self.v(n)
+
+ def visitSub(self, node):
+ if self.debug:
+ print 'visitSub'
+
+ def visitSubscript(self, node):
+ if self.debug:
+ print 'visitSubscript'
+
+ def visitTryExcept(self, node):
+ if self.debug:
+ print 'visitTryExcept'
+
+ def visitTryFinally(self, node):
+ if self.debug:
+ print 'visitTryFinally'
+
+ def visitTuple(self, node):
+ if self.debug:
+ print 'visitTuple'
+
+ def visitUnaryAdd(self, node):
+ if self.debug:
+ print 'visitUnaryAdd'
+
+ def visitUnarySub(self, node):
+ if self.debug:
+ print 'visitUnarySub'
+
+ def visitWhile(self, node):
+ if self.debug:
+ print 'visitWhile'
+ self.v(node.test)
+ self.v(node.body)
+ if node.else_:
+ self.v(node.else_)
+
+ def visitYield(self, node):
+ if self.debug:
+ print 'visitYield'
+
+
+
+def ast2py(ast,debug=True):
+ v = Visitor(debug)
+ v.v(ast)
+ return str(v)
+
+def main():
+ filename = 'test.py'
+ ast = parse(open(filename).read())
+ s = ast2py(ast)
+ return s
+
+if __name__ == "__main__":
+ print main()
diff --git a/pynxc/nxc/.DS_Store b/pynxc/nxc/.DS_Store
new file mode 100644
index 0000000..6e54a94
--- /dev/null
+++ b/pynxc/nxc/.DS_Store
Binary files differ
diff --git a/pynxc/nxc/README b/pynxc/nxc/README
new file mode 100644
index 0000000..f17ebad
--- /dev/null
+++ b/pynxc/nxc/README
@@ -0,0 +1,10 @@
+Latest source of NXC can be found here:
+
+http://bricxcc.svn.sourceforge.net/viewvc/bricxcc/
+
+under the MOZILLA PUBLIC LICENSE Version 1.1 (http://bricxcc.svn.sourceforge.net/viewvc/bricxcc/LICENSE)
+
+Latest binaries can be found here:
+
+http://bricxcc.sourceforge.net/nbc/
+
diff --git a/pynxc/nxc/darwin/.DS_Store b/pynxc/nxc/darwin/.DS_Store
new file mode 100644
index 0000000..9789e1c
--- /dev/null
+++ b/pynxc/nxc/darwin/.DS_Store
Binary files differ
diff --git a/pynxc/nxc/darwin/nbc b/pynxc/nxc/darwin/nbc
new file mode 100755
index 0000000..2268506
--- /dev/null
+++ b/pynxc/nxc/darwin/nbc
Binary files differ
diff --git a/pynxc/nxc/darwin/nxtcom b/pynxc/nxc/darwin/nxtcom
new file mode 100755
index 0000000..da43f95
--- /dev/null
+++ b/pynxc/nxc/darwin/nxtcom
Binary files differ
diff --git a/pynxc/nxc/darwin/place_nbc_and_nxtcom_here.txt b/pynxc/nxc/darwin/place_nbc_and_nxtcom_here.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/pynxc/nxc/darwin/place_nbc_and_nxtcom_here.txt
diff --git a/pynxc/nxc/linux2/.DS_Store b/pynxc/nxc/linux2/.DS_Store
new file mode 100644
index 0000000..fd25488
--- /dev/null
+++ b/pynxc/nxc/linux2/.DS_Store
Binary files differ
diff --git a/pynxc/nxc/linux2/nbc b/pynxc/nxc/linux2/nbc
new file mode 100755
index 0000000..0141c4e
--- /dev/null
+++ b/pynxc/nxc/linux2/nbc
Binary files differ
diff --git a/pynxc/nxc/win32/nbc.exe b/pynxc/nxc/win32/nbc.exe
new file mode 100755
index 0000000..0c7962e
--- /dev/null
+++ b/pynxc/nxc/win32/nbc.exe
Binary files differ
diff --git a/pynxc/nxc/win32/place_nbc_exe_here.txt b/pynxc/nxc/win32/place_nbc_exe_here.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/pynxc/nxc/win32/place_nbc_exe_here.txt
diff --git a/pynxc/pynxc.pyw b/pynxc/pynxc.pyw
new file mode 100755
index 0000000..820fb2a
--- /dev/null
+++ b/pynxc/pynxc.pyw
@@ -0,0 +1,1392 @@
+#!/usr/bin/env python
+from __future__ import with_statement
+
+__author__ = 'Brian Blais <bblais@bryant.edu>'
+__version__ = (0,1,6)
+
+
+import ast_template
+import sys
+from compiler import parse, walk
+from compiler.consts import *
+from waxy import *
+import os
+import subprocess
+from optparse import OptionParser
+import re
+import yaml
+
+
+pynxc_root,junk=os.path.split(sys.argv[0])
+#pynxc_root=os.getcwd()
+
+
+class MyObject(object):
+
+ def __init__(self,name,type='default'):
+
+ self.name=name
+ self.type=type
+ self.datatype=None
+ self.value=None
+
+
+ self.variables=[]
+
+ def __repr__(self):
+ return self.type+" "+self.name+" of datatype "+self.datatype.__repr__()+" with value "+self.value.__repr__()
+
+
+class Variable(MyObject):
+
+ def __init__(self,name,datatype='default'):
+
+
+ super(Variable,self).__init__(name,'variable')
+ self.datatype=datatype
+
+
+class Function(MyObject):
+
+ def __init__(self,name,variables=[]):
+
+
+ super(Function,self).__init__(name,'function')
+ self.variables=variables
+
+
+
+
+file_lines=[]
+
+class SecondPassVisitor(ast_template.Visitor):
+ """This object goes through and output the code"""
+
+ def __init__(self,fv,stream=sys.stdout,debug=False):
+
+ ast_template.Visitor.__init__(self,stream,debug)
+
+
+ self.fv=fv # first-pass visitor
+ self.stream=stream
+ self.erase_assign=False
+ self.buffer=[]
+ self.semicolon=True
+
+ self.writef('#include "NXCDefs.h"\n')
+ self.writef(open(os.path.join(pynxc_root,"MyDefs.h"),'rt').read())
+
+
+ for d in fv.defines:
+ self.writef('#define %s %s\n' % (d[0],d[1]))
+
+ self.print_structure_definitions(fv.struct_types)
+
+ self.print_typedefs(fv.typedefs)
+
+ if self.debug:
+ print "Module Variables Printing "
+
+ self.print_variable_definitions(fv.functions['module'].variables)
+ self.scope=['module']
+
+
+ def type2str(self,datatype):
+ if datatype=='Integer':
+ return 'int'
+ elif datatype=='IntegerPtr':
+ return 'int&'
+ elif datatype=='Word':
+ return 'word'
+ elif datatype=='Long':
+ return 'long'
+ elif datatype=='Byte':
+ return 'byte'
+ elif datatype=='Short':
+ return 'short'
+ elif datatype=='String':
+ return 'string'
+ elif datatype=='Mutex':
+ return 'mutex'
+ elif datatype in self.fv.struct_types:
+ return datatype
+ else:
+ return 'int'
+
+ def print_typedefs(self,typedefs):
+
+ for types in typedefs:
+ self.write('typedef %s %s;' % (typedefs[types],types))
+ self.NEWLINE()
+ self.flush()
+
+ def print_structure_definitions(self,struct_types):
+
+ for key in struct_types:
+ self.write('struct %s ' % key)
+ self.INDENT()
+ variables=struct_types[key]
+ for var in variables:
+ self.write(self.type2str(variables[var].datatype))
+ self.write(' %s' % variables[var].name)
+ self.write(';')
+ self.NEWLINE()
+ self.DEDENT()
+ self.write(';')
+ self.NEWLINE()
+
+
+ def print_variable_definitions(self,variables):
+ for var in variables:
+
+
+ if self.debug:
+ print " Variable ",variables[var]
+
+ self.write(self.type2str(variables[var].datatype))
+
+ self.write(' %s' % variables[var].name)
+
+ if not variables[var].value is None:
+ if isinstance(variables[var].value,list):
+ self.write('[]')
+ if not variables[var].value==[]:
+ self.write('={')
+ for v in variables[var].value:
+ self.write(v.__repr__())
+ if not v == variables[var].value[-1]:
+ self.write(',')
+ self.write('}')
+
+
+
+ else:
+ val=variables[var].value.__repr__()
+ self.write('= %s' % val.replace("'",'"'))
+ self.write(';')
+ self.NEWLINE()
+
+ if variables[var].datatype in self.fv.struct_types:
+ variables2=self.fv.struct_types[variables[var].datatype]
+ for var2 in variables2:
+ if variables2[var2].value:
+ val2=variables2[var2].value.__repr__()
+
+ self.write('%s.%s' % (var,variables2[var2].name))
+ self.write('= %s' % val2.replace("'",'"'))
+ self.write(';')
+ self.NEWLINE()
+
+
+
+ self.flush()
+
+
+ def flush(self):
+
+ if self.buffer:
+ for s in self.buffer:
+ s=s.replace("'",'"')
+ self.stream.write(s)
+
+ self.buffer=[]
+
+ def DEDENT(self,with_semicolon=False):
+ self.indents -=1
+ self.NEWLINE()
+ self.write('}')
+ if with_semicolon:
+ self.write(';')
+ self.NEWLINE()
+
+ def INDENT(self):
+ self.indents += 1
+ self.write(' {')
+ self.NEWLINE()
+
+ def NEWLINE(self):
+ self.write('\n')
+ self.write(' ' * 4 * self.indents )
+
+ def write(self, data):
+ self.buffer.append(data)
+
+
+ def writef(self, data):
+ self.write(data)
+ self.flush()
+
+
+ def visitBlock(self, block):
+ self.INDENT()
+ self.v(block)
+ self.DEDENT()
+
+ def visitAdd(self, node):
+ self.write("(")
+ self.v(node.left)
+ self.write(" + ")
+ self.v(node.right)
+ self.write(")")
+
+ def visitAnd(self, node):
+ self.write("(")
+ for i in range(len(node.nodes)):
+ self.write("(")
+ self.v(node.nodes[i])
+ self.write(")")
+ if i < (len(node.nodes) - 1):
+ self.write(" && ")
+ self.write(")")
+
+ def visitAssAttr(self, node):
+ if self.debug:
+ print 'visitSecondVisitorAssAttr'
+ self.v(node.expr, self)
+ self.write(".%s" % node.attrname)
+
+ def visitAssName(self, node):
+ self.write(node.name)
+
+ def visitAssign(self, node):
+ self.flush()
+ self.erase_assign=False
+ n=node.nodes[0]
+
+
+ if self.scope[-1]=='module':
+ return
+
+# try:
+# if (n.name == n.name.upper()) and (len(n.name)>2): # a definition
+# return
+# except AttributeError:
+# pass # probably a subscript?
+
+ for i in range(len(node.nodes)):
+ n = node.nodes[i]
+ self.v(n)
+ if i < len(node.nodes):
+ self.write(" = ")
+ self.v(node.expr)
+
+ self.write("; ")
+ self.NEWLINE()
+
+
+ if self.erase_assign:
+ self.buffer=[]
+
+ self.flush()
+
+
+ def visitAugAssign(self, node):
+ self.v(node.node)
+ self.write(" %s " % node.op)
+ self.v(node.expr)
+ self.write("; ")
+ self.NEWLINE()
+
+
+ def visitBitand(self, node):
+ for i in range(len(node.nodes)):
+ self.v(node.nodes[i])
+ if i < (len(node.nodes) - 1):
+ self.write(" & ")
+
+ def visitBitor(self, node):
+ for i in range(len(node.nodes)):
+ self.v(node.nodes[i])
+ if i < (len(node.nodes) - 1):
+ self.write(" | ")
+
+ def visitBitxor(self, node):
+ for i in range(len(node.nodes)):
+ self.v(node.nodes[i])
+ if i < (len(node.nodes) - 1):
+ self.write(" ^ ")
+
+ def visitBreak(self, node):
+ self.write("break; ")
+ self.NEWLINE()
+
+ def visitFunction(self, node):
+ self.scope.append('function')
+
+ hasvar = haskw = hasone = hasboth = False
+
+ ndefaults = len(node.defaults)
+
+ if node.flags & CO_VARARGS:
+ hasone = hasvar = True
+ if node.flags & CO_VARKEYWORDS:
+ hasone = haskw = True
+ hasboth = hasvar and haskw
+
+ kwarg = None
+ vararg = None
+ defargs = []
+ newargs = node.argnames[:]
+
+ if ndefaults:
+ for i in range(ndefaults):
+ defargs.append((newargs.pop(), node.defaults.pop()))
+ defargs.reverse()
+
+ func_type=self.fv.functions[node.name].datatype
+ if not func_type:
+ func_type='void'
+
+ if func_type=='default':
+ func_type='int'
+
+ if node.name=='main':
+ self.write("task %s(" % node.name)
+ elif node.name.find('task_')==0:
+ self.write("task %s(" % node.name)
+ elif node.name.find('sub_')==0:
+ self.write("sub %s(" % node.name)
+ elif node.name.find('inline_')==0:
+ self.write("inline %s %s(" % (func_type,node.name.replace('inline_','')))
+ else:
+ self.write("%s %s(" % (func_type,node.name))
+
+
+ for i in range(len(newargs)):
+ if isinstance(newargs[i], tuple):
+ self.write("(%s, %s)" % newargs[i])
+ else:
+ self.write("int "+newargs[i])
+ if i < len(newargs) - 1:
+ self.write(", ")
+ if defargs and len(newargs):
+ self.write(", ")
+
+ for i in range(len(defargs)):
+ name, default = defargs[i]
+ typename=default.node.name
+
+
+
+ self.write("%s %s" % (self.type2str(typename),name))
+ #self.v(default)
+ if i < len(defargs) - 1:
+ self.write(", ")
+
+ if vararg:
+ if (newargs or defargs):
+ self.write(", ")
+ self.write(vararg)
+ if kwarg:
+ if (newargs or defargs or vararg):
+ self.write(", ")
+ self.write(kwarg)
+
+ self.write(") ")
+ self.INDENT()
+
+ self.print_variable_definitions(self.fv.functions[node.name].variables)
+
+ self.v(node.code)
+ self.DEDENT()
+
+ self.flush()
+ self.scope.pop()
+
+
+ def visitCallFunc(self, node):
+
+ name=node.node.name
+
+ if name=="ASM": # raw ASM
+ s=node.args[0].value
+
+ self.write("asm{%s}" % s)
+
+ return
+
+ if name=="NXC":
+ s=node.args[0].value
+
+ self.write("%s" % s)
+
+ return
+
+ if name=="DEFINE":
+ s=node.args[1].value
+ d=node.args[0].value
+
+ self.write("#define %s %s" %(d,s))
+ self.NEWLINE()
+ self.semicolon=False
+ return
+
+ self.v(node.node)
+ self.write("(")
+ for i in range(len(node.args)):
+ self.v(node.args[i])
+ if i < (len(node.args) - 1):
+ self.write(", ")
+ if node.star_args:
+ if len(node.args):
+ self.write(", ")
+ self.write("*")
+ self.v(node.star_args)
+ if node.dstar_args:
+ if node.args or node.star_args:
+ self.write(", ")
+ self.write("**")
+ self.v(node.dstar_args)
+ self.write(")")
+
+ if name in self.fv.types:
+ self.erase_assign=True
+
+
+ def visitClass(self, node):
+ self.scope.append('class')
+
+ self.flush()
+ for i in range(len(node.bases)):
+ self.v(node.bases[i])
+ self.INDENT()
+ self.v(node.code)
+ self.DEDENT()
+
+ self.buffer=[] # get rid of all of the stuff in a class def
+ self.scope.pop()
+
+
+ def visitCompare(self, node):
+ self.write("(")
+ self.v(node.expr)
+
+ for operator, operand in node.ops:
+ self.write(" %s " % operator)
+ self.v(operand)
+ self.write(")")
+
+ def visitConst(self, node):
+ self.write(repr(node.value))
+
+ def visitContinue(self, node):
+ self.write("continue; ")
+
+ def visitDiscard(self, node):
+ self.semicolon=True
+
+ # deal with empty statements, so it doesn't print None
+ try:
+ if node.expr.value is None:
+ pass
+ else:
+ self.v(node.expr)
+ if self.semicolon:
+ self.write(";")
+ self.NEWLINE()
+ except AttributeError:
+ self.v(node.expr)
+ if self.semicolon:
+ self.write(";")
+ self.NEWLINE()
+
+ def visitDiv(self, node):
+ self.v(node.left)
+ self.write(" / ")
+ self.v(node.right)
+
+
+ def visitFor(self, node):
+
+
+ children=node.list.getChildNodes()
+ start='0'
+ end='1'
+ step='1'
+
+ if self.debug:
+ print node.assign.name
+ print dir(node.assign)
+
+ print node.list
+ print dir(node.list)
+
+ print node.list.getChildren()
+
+ print children
+
+ if children[0].name=='range':
+ vals=[v.asList()[0] for v in children[1:]]
+ if node.assign.name=='repeat': # keyword repeat
+ if len(vals)==1:
+ end=vals[0]
+ else:
+ raise ValueError,"Bad for-loop construction"
+
+ self.write("repeat(%s) " % (end))
+
+ else:
+ if len(vals)==1:
+ end=vals[0]
+ elif len(vals)==2:
+ start=vals[0]
+ end=vals[1]
+ elif len(vals)==3:
+ start=vals[0]
+ end=vals[1]
+ step=vals[2]
+ else:
+ raise ValueError,"Bad for-loop construction"
+
+ varname=node.assign.name
+ self.write("for (%s=%s; %s<%s; %s+=%s) " % (varname,start,
+ varname,end,
+ varname,step))
+
+ self.INDENT()
+ self.v(node.body)
+ self.DEDENT()
+ else:
+ raise ValueError,"For-loop construction not implemented"
+
+
+
+
+ def visitGenExpr(self, node):
+ self.write("(")
+ self.v(node.code)
+ self.write(")")
+
+ def visitGetattr(self, node):
+ self.v(node.expr)
+ self.write(".%s" % node.attrname)
+
+ def visitIf(self, node):
+ flag=False
+ for c, b in node.tests:
+ if not flag:
+ self.write("if (")
+ else:
+ self.write("else if (")
+ self.v(c)
+ self.write(') ')
+ self.INDENT()
+ self.v(b)
+ self.DEDENT()
+ flag=True
+ if node.else_:
+ self.write("else ")
+ self.INDENT()
+ self.v(node.else_)
+ self.DEDENT()
+
+ def visitKeyword(self, node):
+ self.write(node.name)
+ self.write("=")
+ self.v(node.expr)
+
+
+ def visitInvert(self, node):
+ self.write("~")
+ self.v(node.expr)
+
+ def visitLeftShift(self, node):
+ self.v(node.left)
+ self.write(" << ")
+ self.v(node.right)
+
+ def visitMod(self, node):
+ self.v(node.left)
+ self.write(" % ")
+ self.v(node.right)
+
+ def visitMul(self, node):
+ self.v(node.left)
+ self.write(" * ")
+ self.v(node.right)
+
+ def visitName(self, node):
+
+ if node.name=='False':
+ self.write("false")
+ elif node.name=='True':
+ self.write("true")
+ else:
+ self.write(node.name.replace('inline_',''))
+
+ def visitNot(self, node):
+ self.write(" !(")
+ self.v(node.expr)
+ self.write(")")
+
+ def visitOr(self, node):
+ self.write("(")
+ for i in range(len(node.nodes)):
+ self.write("(")
+ self.v(node.nodes[i])
+ self.write(")")
+ if i < len(node.nodes) - 1:
+ self.write(" || ")
+ self.write(")")
+
+
+ def visitPass(self, node):
+ self.write("// pass ")
+
+ def visitReturn(self, node):
+ try:
+ if node.value.value is None:
+ self.write('return;')
+ else:
+ self.write('return(%s);' % node.value.value.__repr__())
+
+ except TypeError:
+ pass
+ except AttributeError:
+
+ self.write("return(")
+ self.v(node.value)
+ self.write(");")
+
+ def visitRightShift(self, node):
+ self.v(node.left)
+ self.write(" >> ")
+ self.v(node.right)
+
+ def visitSubscript(self, node):
+ isdel = False
+ if node.flags == OP_DELETE: isdel = True
+ isdel and self.write("del ")
+ self.v(node.expr)
+ self.write("[")
+ for i in range(len(node.subs)):
+ self.v(node.subs[i])
+ if i == len(node.subs) - 1:
+ self.write("]")
+ node.flags == OP_DELETE and self.NEWLINE()
+
+ def visitSub(self, node):
+ self.write("(")
+ self.v(node.left)
+ self.write(" - ")
+ self.v(node.right)
+ self.write(")")
+
+ def visitUnaryAdd(self, node):
+ self.write("+")
+ self.v(node.expr)
+
+ def visitUnarySub(self, node):
+ self.write("-")
+ self.v(node.expr)
+
+ def visitWhile(self, node):
+ self.write("while (")
+ self.v(node.test)
+ self.write(") ")
+ self.INDENT()
+ self.v(node.body)
+ if node.else_:
+ self.DEDENT()
+ self.write("else:")
+ self.INDENT()
+ self.v(node.else_)
+ self.DEDENT()
+
+
+
+
+
+
+class FirstPassVisitor(ast_template.Visitor):
+ """This object goes through and gets all of the variables.
+ The second pass will output the code"""
+
+ def __init__(self,stream=sys.stdout,debug=False):
+
+ ast_template.Visitor.__init__(self,stream,debug)
+ self.variables={}
+
+ self.return_datatype=None
+ self.types=['Byte','Short','Word','String','Mutex','Integer','Long','Struct']
+ self.struct_types={}
+ self.functions={}
+ self.functions['module']=Function('module',self.variables)
+
+ self.variables_assign=[]
+ self.kwassign=[]
+ self.use_kwassign=False
+
+ self.typedefs={}
+
+ self.scope=['module']
+
+ self.use_typedef=False
+
+ def visitClass(self, node):
+ self.scope.append('class')
+
+ variables={}
+ old_self_variables=self.variables
+ self.variables=variables
+
+ if self.debug:
+ print "myvisitClass"
+ print node.name
+
+ basename=node.bases[0].name
+ for i in range(len(node.bases)):
+ self.v(node.bases[i])
+
+
+ self.use_typedef=False
+ self.v(node.code)
+
+ if self.use_typedef:
+ self.typedefs[node.name]=basename
+ self.types.append(node.name)
+ else:
+ self.struct_types[node.name]=variables
+ self.types.append(node.name)
+ self.variables=old_self_variables
+ self.scope.pop()
+
+
+
+ def visitPass(self, node):
+ if self.scope[-1]=='class':
+ self.use_typedef=True
+
+ def visitBlock(self, block):
+ if self.debug:
+ print "myvisitBlock"
+ self.v(block)
+
+ def visitAssName(self, node):
+ if self.debug:
+ print 'visitAssName'
+ if node.flags == OP_DELETE:
+ print "del ",
+ print node.name
+
+ n=node
+
+ self.variables_assign.append(n.name)
+
+ if n.name not in self.variables:
+ self.variables[n.name]=Variable(n.name)
+ if self.debug:
+ print "MyAddVar",n.name
+
+ def visitReturn(self, node):
+ #self.write("return ")
+ #self.v(node.value)
+ try:
+ if node.value.value is None:
+ self.return_datatype='void'
+ else:
+ if isinstance(node.value.value,int):
+ self.return_datatype='int'
+ else:
+ raise TypeError,"Unknown type for "+str(node.value.value)
+
+ except TypeError:
+ pass
+ except AttributeError: # a name?
+ name=node.value.name
+ if name in self.variables:
+ self.return_datatype=self.variables[name].datatype
+ else:
+ raise NameError, "Name"+name+"not found"
+
+ def visitFor(self, node):
+ if self.debug:
+ print 'myvisitFor'
+
+ if node.assign.name!='repeat': # keyword repeat
+ self.v(node.assign)
+
+ self.v(node.body)
+
+
+
+ def visitAssign(self, node):
+ if self.debug:
+ print 'MyvisitAssign'
+
+ self.variables_assign=[]
+ for i in range(len(node.nodes)):
+ n = node.nodes[i]
+ if self.debug:
+ print " Node ",n
+ self.v(n)
+
+ if self.debug:
+ print "varassign ",self.variables_assign
+ a=node.expr.asList()[0]
+ print "varassign expr",node.expr,node.expr.asList()[0],type(a)
+
+ if self.scope[-1]=='module':
+ if self.debug:
+ print "Module Variables"
+ for name in self.variables_assign:
+ val=node.expr.asList()[0]
+ self.variables[name].value=val
+ if isinstance(val,str):
+ self.variables[name].datatype='String'
+ if self.debug:
+ print " ",self.variables[name]
+
+
+ self.v(node.expr)
+
+
+ def visitKeyword(self, node):
+ if self.debug:
+ print 'myvisitKeyword'
+
+ if not self.use_kwassign:
+ self.v(node.expr)
+ return
+
+
+
+ def visitCallFunc(self, node):
+ if self.debug:
+ print 'myvisitCallFunc'
+ print 'funcname: ',node.node.name
+
+ name=node.node.name
+
+
+ if not name in self.types:
+ self.v(node.node)
+ for i in range(len(node.args)):
+ self.v(node.args[i])
+
+ return
+
+ for v in self.variables_assign:
+ if (name=='Byte' or name=='Word' or name=='Short' or
+ name=='String' or name=='Integer' or name=='Long' or
+ name=='Mutex'):
+
+ self.variables[v].datatype=name
+ try:
+ self.variables[v].value=node.args[0].value
+ except IndexError:
+ self.variables[v].value=None # to fix the mutex problem
+ # was: pass # use the default value
+ except AttributeError: # list? or mutex
+ nodelist=node.args[0].asList()
+ vallist=[]
+ for l in nodelist:
+ vallist.append(l.value)
+
+ self.variables[v].value=vallist
+ elif name=='Struct':
+
+
+ self.use_kwassign=True
+
+
+ struct_name=node.args[0].value
+
+ if not struct_name in self.struct_types:
+ self.struct_types.append(struct_name)
+
+ for i in range(1,len(node.args)):
+ if self.debug:
+ print node.args[i]
+ print dir(node.args[i])
+ fun=node.args[i].name
+ #self.v(node.args[i])
+ if self.debug:
+ print " fun",fun
+ elif name in self.types:
+ self.variables[v].datatype=name
+ try:
+ self.variables[v].value=node.args[0].value
+ except IndexError:
+ pass # use the default value
+ except AttributeError: # list?
+ self.variables[v].value=[]
+
+
+ def visitFunction(self, node):
+ self.scope.append('function')
+ self.return_datatype=None
+
+ variables={}
+ old_self_variables=self.variables
+ self.variables=variables
+
+ if self.debug:
+ print "myvisitFunction"
+ print node.name
+ hasvar = haskw = hasone = hasboth = False
+
+ ndefaults = len(node.defaults)
+
+ if node.flags & CO_VARARGS:
+ hasone = hasvar = True
+ if node.flags & CO_VARKEYWORDS:
+ hasone = haskw = True
+ hasboth = hasvar and haskw
+
+ kwarg = None
+ vararg = None
+ defargs = []
+ newargs = node.argnames[:]
+
+
+ self.v(node.code)
+
+ self.functions[node.name]=Function(node.name,variables)
+ self.functions[node.name].datatype=self.return_datatype
+ self.variables=old_self_variables
+ self.scope.pop()
+
+
+ # remove those variables that are global
+ remove_var=[]
+ for var in self.functions[node.name].variables:
+ if (var in self.variables and
+ self.functions[node.name].variables[var].datatype=='default'):
+
+ remove_var.append(var)
+
+ for r in remove_var:
+ self.functions[node.name].variables.pop(r)
+
+
+def python_to_nxc(pyfile,nxcfile=None,debug=False):
+ global file_lines
+
+ filename = pyfile
+
+ f = open(filename, 'U')
+ codestring = f.read()
+ f.close()
+ if codestring and codestring[-1] != '\n':
+ codestring = codestring + '\n'
+
+ file_lines=open(filename).readlines()
+ filestr=codestring
+
+
+ defines=re.findall('\s*DEFINE (.*?)=(.*)',filestr)
+ filestr=re.sub('\s*DEFINE (.*?)=(.*)',"",filestr)
+
+
+ if debug:
+ print "Filestr"
+ print filestr
+
+ ast = parse(filestr)
+ v = FirstPassVisitor(debug=debug)
+ v.v(ast)
+
+ v.defines=defines
+
+ if nxcfile:
+ fid=open(nxcfile,'wt')
+ else:
+ fid=sys.stdout
+
+ v2 = SecondPassVisitor(v,debug=debug,stream=fid)
+ v2.v(ast)
+ v2.flush()
+
+ if not fid==sys.stdout:
+ fid.close()
+
+def readconfig(fname):
+ config={'firmware':'105'}
+
+ if os.path.exists(fname):
+ data=yaml.load(open(fname))
+ config.update(data)
+
+ return config
+
+def main():
+
+ config=readconfig('pynxc.yaml')
+ nxc=os.path.join("nxc",sys.platform,'nbc')
+ if not os.path.exists(nxc):
+ nxc = 'nbc' # expect 'nbc' in the binary PATH
+
+ usage="usage: %prog [options] [filename]"
+ parser = OptionParser(usage=usage)
+
+ parser.add_option('-c', '--compile', dest="compile",
+ help='compile to nxc code only',default=False,
+ action="store_true")
+ parser.add_option('--debug', dest="debug",
+ help='show debug messages',default=False,
+ action="store_true")
+ parser.add_option('--show_nxc', dest="show_nxc",
+ help='show the nxc code',default=False,
+ action="store_true")
+ parser.add_option('-d', '--download', dest="download",
+ help='download program',default=False,
+ action="store_true")
+ parser.add_option('-B', '--bluetooth', dest="bluetooth",
+ help='enable bluetooth',default=False,
+ action="store_true")
+ parser.add_option('--firmware', dest="firmware",
+ help='firmware version (105, 107, or 128)',default=config['firmware'])
+ parser.add_option('--command', dest="nxc",
+ help='what is the nxc/nqc command',default=nxc,
+ metavar="<command>")
+
+
+ options, args = parser.parse_args()
+ if len(args) < 1:
+ parser.print_help()
+ raise SystemExit
+
+ options.firmware=config['firmware']
+
+ # sanity check on the options
+
+ if (options.download) and (options.compile):
+ print "conflicting options"
+ parser.print_help()
+ raise SystemExit
+
+
+ nxc_root,nxc=os.path.split(options.nxc)
+ s=nxc.lower()
+
+ filename = args[0]
+
+ for filename in args:
+
+ root,ext=os.path.splitext(filename)
+
+ nxc_filename=root+".nxc"
+ rxe_filename=root+".rxe"
+
+ python_to_nxc(filename,nxc_filename,debug=options.debug)
+ print "Wrote %s." % (nxc_filename)
+
+ if options.show_nxc:
+ fid=open(nxc_filename)
+ print fid.read()
+ fid.close()
+
+ if not options.compile:
+
+ cmd=options.nxc+" "
+ if options.bluetooth:
+ cmd+=' -BT '
+
+ cmd=cmd+ "'%s'" % nxc_filename+ " -I='%s' -I=%s/ -v=%s -O='%s'" % (nxc_root,
+ pynxc_root,
+ options.firmware,
+ rxe_filename)
+ print cmd
+ a=os.system(cmd)
+
+ if options.download:
+ print "Downloading...",
+ cmd=options.nxc+" "
+ cmd=cmd+ nxc_filename+ " -I='%s/' -I='%s/' v=%s -d" % (nxc_root,
+ pynxc_root,
+ options.firmware)
+ a=os.system(cmd)
+ nxtcom=os.path.join(nxc_root,'nxtcom')
+ print nxtcom
+ if os.path.exists(nxtcom):
+ cmd='%s %s' % (nxtcom,rxe_filename)
+ a=os.system(cmd)
+
+ print "done."
+
+
+ return
+
+
+class MainFrame(Frame):
+
+ def Body(self):
+ self.ReadConfig()
+
+ self.nxc=os.path.join("nxc",sys.platform,'nbc')
+ if not os.path.exists(self.nxc):
+ nxc = 'nbc' # expect 'nbc' in the binary PATH
+
+ self.prog=None
+
+ self.CreateMenu()
+
+ self.textbox = TextBox(self, multiline=1, readonly=1,
+ Font=Font("Courier New", 10), Size=(650,500),
+ Value='PyNXC Version '+str(__version__)+"\n" +
+ "Firmware Version "+self.firmware_version+"\n")
+ self.AddComponent(self.textbox, expand='both')
+
+ self.Pack()
+ self.CenterOnScreen()
+
+ cmdlist=[self.nxc]
+ self.DoCmd(cmdlist)
+
+ self.ResetTitle()
+
+ def ReadConfig(self):
+
+ config=readconfig('pynxc.yaml')
+ self.firmware_version=str(config['firmware'])
+
+ def UpdateConfig(self):
+
+ config={'firmware':self.firmware_version}
+ with open("pynxc.yaml",'w') as fid:
+ yaml.dump(config,fid,default_flow_style=False)
+
+ def CreateMenu(self):
+
+
+ menubar = MenuBar()
+
+ menu1 = Menu(self)
+ menu1.Append("L&oad Program File", self.Load, "Load a .py file",hotkey="Ctrl+O")
+ menu1.Append("&Quit", self.Quit, "Quit",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+ menu1 = Menu(self)
+ menu1.Append("Con&vert", self.OnlyConvert)
+ menu1.Append("&Compile", self.Compile,hotkey="Ctrl+C")
+ menu1.Append("Compile and &Download", self.Download,hotkey="Ctrl+D")
+ menubar.Append(menu1, "&Program")
+ self.menubar=menubar
+
+ self.nxt_menu = Menu(self)
+ self.nxt_menu.Append("&Info", self.NXT_Info)
+ self.bluetooth_menu=self.nxt_menu.Append("Enable &Bluetooth", type='check')
+
+ submenu=Menu(self)
+ self.firmware_menus=[
+ submenu.Append("Version 105",type='radio',event=self.FirmwareVersion),
+ submenu.Append("Version 107",type='radio',event=self.FirmwareVersion),
+ submenu.Append("Version 128",type='radio',event=self.FirmwareVersion),
+ ]
+ if self.firmware_version=='107':
+ self.firmware_menus[1].Check(True)
+ elif self.firmware_version=='128':
+ self.firmware_menus[2].Check(True)
+ else:
+ self.firmware_menus[0].Check(True)
+ self.firmware_version='105'
+ self.UpdateConfig()
+
+ self.nxt_menu.AppendMenu("Firmware",submenu)
+
+ self.menubar.Append(self.nxt_menu, "&NXT")
+
+
+ self.SetMenuBar(menubar)
+
+
+ def FirmwareVersion(self,event):
+ versions={'Version 105':'105',
+ 'Version 107':'107',
+ 'Version 128':'128',
+ }
+
+ for menu in self.firmware_menus:
+ if menu.IsChecked():
+ self.firmware_version=versions[menu.Label]
+ self.UpdateConfig()
+
+
+
+ def Load(self,event=None):
+ dlg = FileDialog(self, 'Select a Program File',default_dir=os.getcwd(),wildcard='*.py',open=1)
+ try:
+ result = dlg.ShowModal()
+ if result == 'ok':
+ self.prog = dlg.GetPaths()[0]
+ self.ResetTitle()
+ finally:
+ dlg.Destroy()
+
+ def ResetTitle(self,event=None):
+ if self.prog:
+ junk,fname=os.path.split(self.prog)
+ s='PyNXC: %s' % fname
+ else:
+ s='PyNXC'
+
+ self.SetTitle(s)
+
+
+ def NXT_Info(self,event=None):
+ pass
+
+ def Quit(self,event=None):
+ self.Close()
+
+ def DoCmd(self,cmdlist):
+
+ S=self.textbox.GetValue()
+ S=S+"#-> "+" ".join(cmdlist)+"\n"
+ self.textbox.SetValue(S)
+
+ try:
+
+ if sys.platform=='win32':
+ output=subprocess.Popen(
+ cmdlist,stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,stderr=subprocess.PIPE)
+ else:
+ output=subprocess.Popen(
+ cmdlist,stdin=subprocess.PIPE,
+ stdout=subprocess.PIPE,stderr=subprocess.PIPE,close_fds=True)
+
+ try: # there might be non-ASCII chars in the output
+ S += output.stdout.read() + output.stderr.read()
+ except UnicodeDecodeError:
+ S += (output.stdout.read() + output.stderr.read()
+ ).decode('utf-8', 'replace')
+
+ self.textbox.SetValue(S)
+
+ except OSError:
+
+ s=sys.exc_info()
+ S=S+"Error with NXC Executable: "+str(s[1])
+ self.textbox.SetValue(S)
+ return
+
+
+
+ def Convert(self):
+ filename=self.prog
+
+# path,fname=os.path.split(filename)
+ root,ext=os.path.splitext(filename)
+
+ nxc_filename=root+".nxc"
+
+ S=self.textbox.GetValue()
+
+ S=S+"Writing %s..." % (nxc_filename)
+ self.textbox.SetValue(S)
+
+ python_to_nxc(filename,nxc_filename)
+
+
+ S=S+".done\n"
+ self.textbox.SetValue(S)
+
+ return nxc_filename
+
+
+ def OnlyConvert(self,event=None):
+ self.textbox.SetValue("")
+
+ if not self.prog:
+ self.Load()
+
+ if not self.prog:
+ dlg = MessageDialog(self, "Error","No Program File Selected")
+ dlg.ShowModal()
+ dlg.Destroy()
+ return
+
+ try:
+ nxc_filename=self.Convert()
+ except:
+ s=sys.exc_info()
+ str1="Error in "+self.prog +": "+str(s[1])
+ self.textbox.SetValue(str1)
+ return
+
+ def Compile(self,event=None):
+ self.textbox.SetValue("")
+
+ if not self.prog:
+ self.Load()
+
+ if not self.prog:
+ dlg = MessageDialog(self, "Error","No Program File Selected")
+ dlg.ShowModal()
+ dlg.Destroy()
+ return
+
+ try:
+ nxc_filename=self.Convert()
+ except:
+ s=sys.exc_info()
+ str1="Error in "+self.prog +": "+str(s[1])
+ self.textbox.SetValue(str1)
+ return
+
+
+ root,ext=os.path.splitext(nxc_filename)
+ rxe_filename=root+".rxe"
+
+
+
+ cmdlist=[self.nxc,nxc_filename," -v=%s" % self.firmware_version," -O=%s" % rxe_filename]
+
+ self.DoCmd(cmdlist)
+
+
+
+ def Download(self,event=None):
+
+ self.textbox.SetValue("")
+
+ if not self.prog:
+ self.Load()
+
+ if not self.prog:
+ dlg = MessageDialog(self, "Error","No Program File Selected")
+ dlg.ShowModal()
+ dlg.Destroy()
+ return
+
+ try:
+ nxc_filename=self.Convert()
+ except:
+ s=sys.exc_info()
+ str1="Error in "+self.prog +": "+str(s[1])
+ self.textbox.SetValue(str1)
+ return
+
+ flags=['-d','-S=usb','-v=%s' % self.firmware_version]
+ if self.bluetooth_menu.IsChecked():
+ flags.append('-BT')
+
+ root,ext=os.path.splitext(nxc_filename)
+ rxe_filename=root+".rxe"
+ cmdlist=[self.nxc]
+ cmdlist.extend(flags)
+ cmdlist.append(nxc_filename)
+
+ self.DoCmd(cmdlist)
+
+ root,ext=os.path.split(self.nxc)
+ nxtcom=os.path.join(root,'nxtcom_scripts/nxtcom')
+ print nxtcom
+ if os.path.exists(nxtcom):
+ print "Downloading...",
+ cmd='%s %s' % (nxtcom,rxe_filename)
+ a=os.system(cmd)
+ print "done."
+
+
+if __name__ == "__main__":
+
+
+ if len(sys.argv)<2: # no args given, launch gui
+ app = Application(MainFrame, title="PyNXC")
+ app.Run()
+ else:
+ sys.exit(main())
+
diff --git a/pynxc/pynxc.yaml b/pynxc/pynxc.yaml
new file mode 100644
index 0000000..7600055
--- /dev/null
+++ b/pynxc/pynxc.yaml
@@ -0,0 +1 @@
+firmware: '105'
diff --git a/pynxc/tutorial_samples/10_mutex.py b/pynxc/tutorial_samples/10_mutex.py
new file mode 100644
index 0000000..25994b4
--- /dev/null
+++ b/pynxc/tutorial_samples/10_mutex.py
@@ -0,0 +1,25 @@
+moveMutex=Mutex()
+
+def task_move_square():
+
+ while True:
+ Acquire(moveMutex)
+ OnFwd(OUT_AC, 75); Wait(1000)
+ OnRev(OUT_C, 75); Wait(850)
+ Release(moveMutex)
+
+def task_check_sensors():
+
+ while True:
+ if SENSOR_1 == 1:
+
+ Acquire(moveMutex)
+ OnRev(OUT_AC, 75); Wait(500)
+ OnFwd(OUT_A, 75); Wait(850)
+ Release(moveMutex)
+
+def main():
+ SetSensor(IN_1,SENSOR_TOUCH)
+ Precedes(task_check_sensors, task_move_square)
+
+
diff --git a/pynxc/tutorial_samples/10_semaphore.py b/pynxc/tutorial_samples/10_semaphore.py
new file mode 100644
index 0000000..3b6a765
--- /dev/null
+++ b/pynxc/tutorial_samples/10_semaphore.py
@@ -0,0 +1,41 @@
+sem=0 # make sure this one is global
+
+def task_move_square():
+ while True:
+ while (sem == 1):
+ pass
+ sem = 1
+ OnFwd(OUT_AC, 75)
+ sem = 0
+ Wait(1000)
+
+ while sem==1:
+ pass
+
+ sem = 1
+
+
+ OnRev(OUT_C, 75)
+ sem = 0
+ Wait(850)
+
+def task_submain():
+
+ SetSensor(IN_1, SENSOR_TOUCH)
+ while True:
+
+ if SENSOR_1 == 1:
+ while (sem == 1):
+ pass
+ sem = 1
+ OnRev(OUT_AC, 75); Wait(500)
+ OnFwd(OUT_A, 75); Wait(850)
+ sem = 0
+
+def main():
+
+ sem = 0
+ Precedes(task_move_square, task_submain)
+
+
+
diff --git a/pynxc/tutorial_samples/11_DC_master.py b/pynxc/tutorial_samples/11_DC_master.py
new file mode 100644
index 0000000..28f85bf
--- /dev/null
+++ b/pynxc/tutorial_samples/11_DC_master.py
@@ -0,0 +1,37 @@
+#MASTER
+
+DEFINE BT_CONN=1
+
+def sub_BTCheck(conn):
+ if (BluetoothStatus(conn)!=NO_ERR):
+ TextOut(5,LCD_LINE2,"Error")
+ Wait(1000)
+ Stop(True)
+
+DEFINE('MOTOR(p,s)',"""RemoteSetOutputState(BT_CONN, p, s, \\
+ OUT_MODE_MOTORON+OUT_MODE_BRAKE+OUT_MODE_REGULATED, \\
+ OUT_REGMODE_SPEED, 0, OUT_RUNSTATE_RUNNING, 0)""")
+
+def main():
+ sub_BTCheck(BT_CONN)
+ RemotePlayTone(BT_CONN, 4000, 100)
+
+ while (BluetoothStatus(BT_CONN)!=NO_ERR):
+ pass
+
+ Wait(110)
+ RemotePlaySoundFile(BT_CONN, "! Click.rso", false)
+
+ while (BluetoothStatus(BT_CONN)!=NO_ERR):
+ pass
+
+ #Wait(500)
+ RemoteResetMotorPosition(BT_CONN,OUT_A,true)
+
+ while (BluetoothStatus(BT_CONN)!=NO_ERR):
+ pass
+
+ MOTOR(OUT_A,100)
+ Wait(1000)
+ MOTOR(OUT_A,0)
+
diff --git a/pynxc/tutorial_samples/11_ack_master.py b/pynxc/tutorial_samples/11_ack_master.py
new file mode 100644
index 0000000..3104f3d
--- /dev/null
+++ b/pynxc/tutorial_samples/11_ack_master.py
@@ -0,0 +1,30 @@
+#MASTER
+
+DEFINE BT_CONN=1
+DEFINE OUTBOX=5
+DEFINE INBOX=1
+
+def sub_BTCheck(conn):
+ if (BluetoothStatus(conn)!=NO_ERR):
+ TextOut(5,LCD_LINE2,"Error")
+ Wait(1000)
+ Stop(True)
+
+def main():
+ sub_BTCheck(BT_CONN)
+ TextOut(10,LCD_LINE1,"Master sending")
+ while True:
+ i = Random(512);
+ TextOut(0,LCD_LINE3," ")
+ NumOut(5,LCD_LINE3,i)
+ ack = 0
+ SendRemoteNumber(BT_CONN,OUTBOX,i)
+
+ while (ack!=0xFF):
+ pass
+
+ while (ReceiveRemoteNumber(INBOX,true,ack) != NO_ERR):
+ pass
+
+
+ Wait(250)
diff --git a/pynxc/tutorial_samples/11_ack_slave.py b/pynxc/tutorial_samples/11_ack_slave.py
new file mode 100644
index 0000000..9e8c619
--- /dev/null
+++ b/pynxc/tutorial_samples/11_ack_slave.py
@@ -0,0 +1,26 @@
+#SLAVE
+
+DEFINE BT_CONN=1
+DEFINE OUTBOX=5
+DEFINE INBOX=1
+
+def sub_BTCheck(conn):
+ if (BluetoothStatus(conn)!=NO_ERR):
+ TextOut(5,LCD_LINE2,"Error")
+ Wait(1000)
+ Stop(True)
+
+
+def main():
+ numin=Integer()
+
+ sub_BTCheck(0)
+ TextOut(5,LCD_LINE1,"Slave receiving")
+ SendResponseNumber(OUTBOX,0xFF) #unblock master
+ while True:
+ if (ReceiveRemoteNumber(INBOX,true,numin) != STAT_MSG_EMPTY_MAILBOX):
+ TextOut(0,LCD_LINE3," ");
+ NumOut(5,LCD_LINE3,numin);
+ SendResponseNumber(OUTBOX,0xFF);
+
+ Wait(10); #take breath (optional)
diff --git a/pynxc/tutorial_samples/11_msg_master.py b/pynxc/tutorial_samples/11_msg_master.py
new file mode 100644
index 0000000..65cb3fd
--- /dev/null
+++ b/pynxc/tutorial_samples/11_msg_master.py
@@ -0,0 +1,32 @@
+#MASTER
+
+DEFINE BT_CONN=1
+DEFINE OUTBOX=5
+DEFINE INBOX=1
+
+def sub_BTCheck(conn):
+ if (BluetoothStatus(conn)!=NO_ERR):
+ TextOut(5,LCD_LINE2,"Error")
+ Wait(1000)
+ Stop(True)
+
+def main():
+ instr=String()
+ outstr=String()
+ iStr=String()
+
+ i=Integer(0)
+
+ sub_BTCheck(BT_CONN) #check slave connection
+ while True:
+ iStr = NumToStr(i)
+ outstr = StrCat("M",iStr)
+ TextOut(10,LCD_LINE1,"Master Test")
+ TextOut(0,LCD_LINE2,"IN:")
+ TextOut(0,LCD_LINE4,"OUT:")
+ ReceiveRemoteString(INBOX, True,instr)
+ # SendRemoteString(BT_CONN,OUTBOX,outstr) # for some reason, this line doesn't work!
+ TextOut(10,LCD_LINE3,instr)
+ TextOut(10,LCD_LINE5,outstr)
+ Wait(100)
+ i+=1
diff --git a/pynxc/tutorial_samples/11_msg_slave.py b/pynxc/tutorial_samples/11_msg_slave.py
new file mode 100644
index 0000000..34c6c27
--- /dev/null
+++ b/pynxc/tutorial_samples/11_msg_slave.py
@@ -0,0 +1,31 @@
+DEFINE BT_CONN=1
+DEFINE INBOX=5
+DEFINE OUTBOX=1
+
+
+def sub_BTCheck(conn):
+
+ if (BluetoothStatus(conn)!=NO_ERR):
+ TextOut(5,LCD_LINE2,"Error")
+ Wait(1000)
+ Stop(True)
+
+def main():
+ instr=String('')
+ outstr=String('')
+ iStr=String('')
+
+ i=Integer(0)
+ sub_BTCheck(0) #check master connection
+ while True:
+ iStr = NumToStr(i)
+ outstr = StrCat("S",iStr)
+ TextOut(10,LCD_LINE1,"Slave Test")
+ TextOut(0,LCD_LINE2,"IN:")
+ TextOut(0,LCD_LINE4,"OUT:")
+ ReceiveRemoteString(INBOX, True, instr)
+ SendResponseString(OUTBOX,outstr)
+ TextOut(10,LCD_LINE3,instr)
+ TextOut(10,LCD_LINE5,outstr)
+ Wait(100)
+ i+=1
diff --git a/pynxc/tutorial_samples/12_ASCII.py b/pynxc/tutorial_samples/12_ASCII.py
new file mode 100644
index 0000000..68ae25d
--- /dev/null
+++ b/pynxc/tutorial_samples/12_ASCII.py
@@ -0,0 +1,16 @@
+def main():
+ handle=Byte()
+ i=Integer()
+ slen=Integer()
+ s=String()
+
+ if (CreateFile("ASCII.txt", 2048, handle) == NO_ERR):
+
+ for i in range(256):
+ s=NumToStr(i)
+ slen = StrLen(s)
+ WriteString(handle, s, slen)
+ WriteLn(handle, i)
+
+ CloseFile(handle)
+
diff --git a/pynxc/tutorial_samples/12_ReadStr.py b/pynxc/tutorial_samples/12_ReadStr.py
new file mode 100644
index 0000000..5e45145
--- /dev/null
+++ b/pynxc/tutorial_samples/12_ReadStr.py
@@ -0,0 +1,40 @@
+DEFINE FILE_LINES=10
+
+def sub_CreateRandomFile(fname=String(), lines=Integer()):
+ handle=Byte()
+ s=String()
+ n=bytesWritten=Integer()
+ DeleteFile(fname)
+ fsize=Integer()
+ fsize = lines*5
+ # create file with random data
+ if (CreateFile(fname, fsize, handle) == NO_ERR):
+ for repeat in range(FILE_LINES):
+ n = Random(0xFF)
+ s = NumToStr(n)
+ WriteLnString(handle,s,bytesWritten)
+
+ CloseFile(handle)
+
+
+def main():
+ handle=Byte()
+ buf=String()
+ fsize=Integer()
+ blah = Integer(0)
+
+
+
+ sub_CreateRandomFile("rand.txt",FILE_LINES)
+ if (OpenFileRead("rand.txt", fsize, handle) == NO_ERR):
+ TextOut(10,LCD_LINE2,"Filesize:")
+ NumOut(65,LCD_LINE2,fsize)
+ Wait(600)
+ while (not eof): # read the text file till the end
+ pass
+ if (ReadLnString(handle,buf) != NO_ERR):
+ eof = true
+ TextOut(20,LCD_LINE3,buf)
+ Wait(500)
+ CloseFile(handle)
+
diff --git a/pynxc/tutorial_samples/12_WriteStr.py b/pynxc/tutorial_samples/12_WriteStr.py
new file mode 100644
index 0000000..1dcc155
--- /dev/null
+++ b/pynxc/tutorial_samples/12_WriteStr.py
@@ -0,0 +1,27 @@
+DEFINE OK=LDR_SUCCESS
+
+
+def main():
+
+ fileHandle=Byte()
+ fileSize=Short()
+ bytesWritten=Short()
+
+ read=String()
+ write=String()
+ error=String()
+
+ tmp=String()
+
+ DeleteFile("Danny.txt")
+ DeleteFile("DannySays.txt")
+ CreateFile("Danny.txt", 512, fileHandle)
+ for i in range(2,10):
+ write = "NXT is cool "
+ tmp = NumToStr(i)
+ write = StrCat(write,tmp," times!")
+ WriteLnString(fileHandle,write, bytesWritten)
+
+ CloseFile(fileHandle)
+ RenameFile("Danny.txt","DannySays.txt")
+
diff --git a/pynxc/tutorial_samples/12_fs_numbers_int.py b/pynxc/tutorial_samples/12_fs_numbers_int.py
new file mode 100644
index 0000000..249c142
--- /dev/null
+++ b/pynxc/tutorial_samples/12_fs_numbers_int.py
@@ -0,0 +1,20 @@
+def main():
+ handle=time=Byte(0)
+ i=fsize=numin=Integer()
+
+ DeleteFile("int.txt")
+ CreateFile("int.txt",4096,handle)
+
+ for i in range(1000,10000,1000):
+ WriteLn(handle,i)
+
+ CloseFile(handle)
+ OpenFileRead("int.txt",fsize,handle);
+
+ while (ReadLn(handle,numin)==NO_ERR):
+ ClearScreen()
+ NumOut(30,LCD_LINE5,numin)
+ Wait(500)
+
+ CloseFile(handle)
+
diff --git a/pynxc/tutorial_samples/12_fs_numbers_long.py b/pynxc/tutorial_samples/12_fs_numbers_long.py
new file mode 100644
index 0000000..c23d47f
--- /dev/null
+++ b/pynxc/tutorial_samples/12_fs_numbers_long.py
@@ -0,0 +1,21 @@
+def main():
+ handle=time=Byte(0)
+ i=fsize=numin=Integer()
+ numin=Long()
+
+ DeleteFile("long.txt")
+ CreateFile("long.txt",4096,handle)
+
+ for i in range(100000,1000000,50000):
+ WriteLn(handle,i)
+
+ CloseFile(handle)
+ OpenFileRead("long.txt",fsize,handle);
+
+ while (ReadLn(handle,numin)==NO_ERR):
+ ClearScreen()
+ NumOut(30,LCD_LINE5,numin)
+ Wait(500)
+
+ CloseFile(handle)
+
diff --git a/pynxc/tutorial_samples/12_graphics.py b/pynxc/tutorial_samples/12_graphics.py
new file mode 100644
index 0000000..c80c90f
--- /dev/null
+++ b/pynxc/tutorial_samples/12_graphics.py
@@ -0,0 +1,30 @@
+DEFINE X_MAX=99
+DEFINE Y_MAX=63
+DEFINE X_MID=(X_MAX+1)/2
+DEFINE Y_MID=(Y_MAX+1)/2
+
+def main():
+ i=1234
+
+ TextOut(15,LCD_LINE1,"Display")
+ NumOut(60,LCD_LINE1, i)
+ PointOut(1,Y_MAX-1)
+ PointOut(X_MAX-1,Y_MAX-1)
+ PointOut(1,1)
+ PointOut(X_MAX-1,1)
+ Wait(200)
+ RectOut(5,5,90,50)
+ Wait(200)
+ LineOut(5,5,95,55)
+ Wait(200)
+ LineOut(5,55,95,5)
+ Wait(200)
+ CircleOut(X_MID,Y_MID-2,20)
+ Wait(800)
+ ClearScreen()
+ GraphicOut(30,10,"faceclosed.ric")
+ Wait(500)
+ ClearScreen()
+ GraphicOut(30,10,"faceopen.ric")
+ Wait(1000)
+
diff --git a/pynxc/tutorial_samples/12_timers.py b/pynxc/tutorial_samples/12_timers.py
new file mode 100644
index 0000000..e024c1c
--- /dev/null
+++ b/pynxc/tutorial_samples/12_timers.py
@@ -0,0 +1,14 @@
+def main():
+ t0 = CurrentTick()
+ time=0
+
+ while time<10000:
+ time = CurrentTick()-t0
+ OnFwd(OUT_AC, 75)
+ Wait(Random(1000))
+ OnRev(OUT_C, 75)
+ Wait(Random(1000))
+
+
+ Off(OUT_AC);
+
diff --git a/pynxc/tutorial_samples/12_wait.py b/pynxc/tutorial_samples/12_wait.py
new file mode 100644
index 0000000..87b9b6f
--- /dev/null
+++ b/pynxc/tutorial_samples/12_wait.py
@@ -0,0 +1,12 @@
+def main():
+
+ SetSensor(IN_1,SENSOR_TOUCH)
+ t3 = CurrentTick()
+ OnFwd(OUT_AC, 75)
+
+ while (SENSOR_1 != 1) and ((CurrentTick()-t3) <= 1000):
+ pass
+
+ Off(OUT_AC);
+
+
diff --git a/pynxc/tutorial_samples/1_errors.py b/pynxc/tutorial_samples/1_errors.py
new file mode 100644
index 0000000..77640bf
--- /dev/null
+++ b/pynxc/tutorial_samples/1_errors.py
@@ -0,0 +1,8 @@
+def main():
+ OnFwd(OUT_D, 75)
+ OnFwd(OUT_C, 75)
+ Wait(4000)
+ OnRev(OUT_AC, 75)
+ Wait(4000)
+ Of(OUT_AC)
+
diff --git a/pynxc/tutorial_samples/1_simple.nxc b/pynxc/tutorial_samples/1_simple.nxc
new file mode 100644
index 0000000..f425424
--- /dev/null
+++ b/pynxc/tutorial_samples/1_simple.nxc
@@ -0,0 +1,79 @@
+#include "NXCDefs.h"
+#define ULTRASONIC 1
+#define EYES 1
+#define LIGHT 2
+#define SOUND 3
+#define MIC 3
+#define TOUCH 4
+
+#define None 0
+
+
+int SensorTypes[]={0,0,0,0};
+
+
+
+inline int SensorVal(int s_) {
+
+ s_=s_-1;
+ int t_ = SensorTypes[s_];
+
+ if (t_==None) {
+ return -1;
+ } else if (t_==ULTRASONIC) {
+ Wait(100);
+ return SensorUS(s_);
+ } else {
+ return Sensor(s_);
+ }
+
+}
+
+
+
+void DefineSensors(int s1,int s2,int s3,int s4) {
+
+ int sa[];
+ int i;
+
+ ArrayInit(sa,0,4);
+ sa[0]=s1;
+ sa[1]=s2;
+ sa[2]=s3;
+ sa[3]=s4;
+
+ for (i=0; i<4; i++) {
+
+ if (sa[i]==None) {
+ SensorTypes[i]=None;
+ } else if (sa[i]==ULTRASONIC) {
+ SetSensorLowspeed(i);
+ SensorTypes[i]=ULTRASONIC;
+ } else if (sa[i]==LIGHT) {
+ SetSensorLight(i);
+ SensorTypes[i]=LIGHT;
+ } else if (sa[i]==SOUND) {
+ SetSensorSound(i);
+ SensorTypes[i]=SOUND;
+ } else if (sa[i]==TOUCH) {
+ SetSensor(i,SENSOR_TOUCH);
+ SensorTypes[i]=TOUCH;
+ }
+
+
+
+
+ }
+
+
+}
+
+task main() {
+ OnFwd(OUT_A, 75);
+ OnFwd(OUT_B, 75);
+ Wait(4000);
+ OnRev(OUT_AB, 75);
+ Wait(4000);
+ Off(OUT_AB);
+
+}
diff --git a/pynxc/tutorial_samples/1_simple.py b/pynxc/tutorial_samples/1_simple.py
new file mode 100644
index 0000000..eed7ecf
--- /dev/null
+++ b/pynxc/tutorial_samples/1_simple.py
@@ -0,0 +1,9 @@
+def main():
+
+ OnFwd(OUT_A, 75)
+ OnFwd(OUT_B, 75)
+ Wait(4000)
+ OnRev(OUT_AB, 75)
+ Wait(4000)
+ Off(OUT_AB)
+
diff --git a/pynxc/tutorial_samples/2_repeat_a.py b/pynxc/tutorial_samples/2_repeat_a.py
new file mode 100644
index 0000000..b7c5c99
--- /dev/null
+++ b/pynxc/tutorial_samples/2_repeat_a.py
@@ -0,0 +1,15 @@
+DEFINE MOVE_TIME=500
+DEFINE TURN_TIME=360
+
+def main():
+
+ a=5
+
+ for repeat in range(a):
+ OnFwd(OUT_AC, 75)
+ Wait(MOVE_TIME)
+ OnRev(OUT_C, 75)
+ Wait(TURN_TIME)
+
+ Off(OUT_AC)
+
diff --git a/pynxc/tutorial_samples/2_repeat_b.py b/pynxc/tutorial_samples/2_repeat_b.py
new file mode 100644
index 0000000..1400342
--- /dev/null
+++ b/pynxc/tutorial_samples/2_repeat_b.py
@@ -0,0 +1,14 @@
+DEFINE MOVE_TIME=500
+DEFINE TURN_TIME=360
+
+def main():
+
+ for repeat in range(10):
+ for repeat in range(4):
+
+ OnFwd(OUT_AC, 75)
+ Wait(MOVE_TIME)
+ OnRev(OUT_C, 75)
+ Wait(TURN_TIME)
+
+ Off(OUT_AC)
diff --git a/pynxc/tutorial_samples/2_repeat_c.py b/pynxc/tutorial_samples/2_repeat_c.py
new file mode 100644
index 0000000..a9988c4
--- /dev/null
+++ b/pynxc/tutorial_samples/2_repeat_c.py
@@ -0,0 +1,23 @@
+# 10 SQUARES
+#
+#This program make the robot run 10 squares
+#
+#
+
+# Time for a straight move
+DEFINE MOVE_TIME=500
+# Time for turning 90 degrees
+DEFINE TURN_TIME=360
+
+def main():
+
+ for repeat in range(10): # Make 10 squares
+ for repeat in range(4):
+
+ OnFwd(OUT_AC, 75)
+ Wait(MOVE_TIME)
+ OnRev(OUT_C, 75)
+ Wait(TURN_TIME)
+
+ Off(OUT_AC) # Now turn the motors off
+
diff --git a/pynxc/tutorial_samples/2_turn_b.py b/pynxc/tutorial_samples/2_turn_b.py
new file mode 100644
index 0000000..75f30c7
--- /dev/null
+++ b/pynxc/tutorial_samples/2_turn_b.py
@@ -0,0 +1,9 @@
+DEFINE MOVE_TIME=1000
+DEFINE TURN_TIME=360
+
+def main():
+ OnFwd(OUT_AC, 75)
+ Wait(MOVE_TIME)
+ OnRev(OUT_C, 75)
+ Wait(TURN_TIME)
+ Off(OUT_AC)
diff --git a/pynxc/tutorial_samples/3_random.py b/pynxc/tutorial_samples/3_random.py
new file mode 100644
index 0000000..9a5b9a0
--- /dev/null
+++ b/pynxc/tutorial_samples/3_random.py
@@ -0,0 +1,9 @@
+def main():
+
+ while True:
+ move_time = Random(600) # default type is an integer
+ turn_time = Random(400)
+ OnFwd(OUT_AC, 75)
+ Wait(move_time)
+ OnRev(OUT_A, 75)
+ Wait(turn_time)
diff --git a/pynxc/tutorial_samples/3_spiral.py b/pynxc/tutorial_samples/3_spiral.py
new file mode 100644
index 0000000..90f9f5e
--- /dev/null
+++ b/pynxc/tutorial_samples/3_spiral.py
@@ -0,0 +1,14 @@
+DEFINE TURN_TIME=360
+
+def main():
+
+ move_time = 200 # set the initial value
+ for repeat in range(50):
+
+ OnFwd(OUT_AC, 75)
+ Wait(move_time) # use the variable for sleeping
+ OnRev(OUT_C, 75)
+ Wait(TURN_TIME)
+ move_time += 200 # increase the variable
+
+ Off(OUT_AC)
diff --git a/pynxc/tutorial_samples/3_variables.py b/pynxc/tutorial_samples/3_variables.py
new file mode 100644
index 0000000..00ae642
--- /dev/null
+++ b/pynxc/tutorial_samples/3_variables.py
@@ -0,0 +1,15 @@
+bbb=ccc=5.0
+values=Integer([])
+
+def main():
+ aaa = 10
+ bbb = 20 * 5
+ ccc = bbb
+ ccc /= aaa
+ ccc -= 5
+ aaa = 10 * (ccc + 3) # aaa is now equal to 80
+ ArrayInit(values, 0, 10) # allocate 10 elements = 0
+ values[0] = aaa
+ values[1] = bbb
+ values[2] = aaa*bbb
+ values[3] = ccc
diff --git a/pynxc/tutorial_samples/4_if.py b/pynxc/tutorial_samples/4_if.py
new file mode 100644
index 0000000..3f1365f
--- /dev/null
+++ b/pynxc/tutorial_samples/4_if.py
@@ -0,0 +1,16 @@
+DEFINE MOVE_TIME=500
+DEFINE TURN_TIME=360
+
+a=0
+
+def main():
+ while True:
+
+ OnFwd(OUT_AC, 75)
+ Wait(MOVE_TIME)
+ if Random() > 0:
+ OnRev(OUT_C, 75)
+ else:
+ OnRev(OUT_A, 75)
+
+ Wait(TURN_TIME)
diff --git a/pynxc/tutorial_samples/5_light.py b/pynxc/tutorial_samples/5_light.py
new file mode 100644
index 0000000..0f2cbee
--- /dev/null
+++ b/pynxc/tutorial_samples/5_light.py
@@ -0,0 +1,14 @@
+DEFINE THRESHOLD=40
+
+def main():
+ DefineSensors(None,None,LIGHT,None)
+ OnFwd(OUT_AC, 75)
+ while True:
+ if SensorVal(2) > THRESHOLD:
+ OnRev(OUT_C, 75)
+ Wait(100)
+
+ while SensorVal(2) > THRESHOLD:
+ pass
+
+ OnFwd(OUT_AC, 75)
diff --git a/pynxc/tutorial_samples/5_sound.py b/pynxc/tutorial_samples/5_sound.py
new file mode 100644
index 0000000..ee8c146
--- /dev/null
+++ b/pynxc/tutorial_samples/5_sound.py
@@ -0,0 +1,20 @@
+DEFINE THRESHOLD=40
+DEFINE MIC=SensorVal(2)
+
+def main():
+ DefineSensors(None,SOUND,None,None)
+ while True:
+ while (MIC <= THRESHOLD):
+ pass
+
+ OnFwd(OUT_AC, 75)
+ Wait(300)
+
+
+ while (MIC <= THRESHOLD):
+ pass
+
+ Off(OUT_AC)
+ Wait(300)
+
+
diff --git a/pynxc/tutorial_samples/5_touch.py b/pynxc/tutorial_samples/5_touch.py
new file mode 100644
index 0000000..3c37439
--- /dev/null
+++ b/pynxc/tutorial_samples/5_touch.py
@@ -0,0 +1,10 @@
+
+def main():
+ DefineSensors(TOUCH,None,None,None)
+ OnFwd(OUT_AC, 75)
+ while True:
+ if SensorVal(1) == 1:
+
+ OnRev(OUT_AC, 75); Wait(300)
+ OnFwd(OUT_A, 75); Wait(300)
+ OnFwd(OUT_AC, 75)
diff --git a/pynxc/tutorial_samples/5_ultrasonic.py b/pynxc/tutorial_samples/5_ultrasonic.py
new file mode 100644
index 0000000..40503bf
--- /dev/null
+++ b/pynxc/tutorial_samples/5_ultrasonic.py
@@ -0,0 +1,13 @@
+# in cm
+DEFINE NEAR=15
+
+def main():
+ DefineSensors(None,None,None,EYES)
+ while True:
+ OnFwd(OUT_AC,50)
+ while SensorVal(4)>NEAR:
+ pass
+
+ Off(OUT_AC)
+ OnRev(OUT_C,100)
+ Wait(800)
diff --git a/pynxc/tutorial_samples/5_wait.py b/pynxc/tutorial_samples/5_wait.py
new file mode 100644
index 0000000..dcf93f1
--- /dev/null
+++ b/pynxc/tutorial_samples/5_wait.py
@@ -0,0 +1,6 @@
+def main():
+ DefineSensors(TOUCH,None,None,None)
+ OnFwd(OUT_AC, 75)
+ while SensorVal(1) != 1:
+ pass
+ Off(OUT_AC)
diff --git a/pynxc/tutorial_samples/6_define_a.py b/pynxc/tutorial_samples/6_define_a.py
new file mode 100644
index 0000000..2f08c50
--- /dev/null
+++ b/pynxc/tutorial_samples/6_define_a.py
@@ -0,0 +1,13 @@
+DEFINE turn_around=OnRev(OUT_B, 75); Wait(3400); a=5; OnFwd(OUT_AB, 75);
+
+def main():
+ a=0
+ OnFwd(OUT_AB, 75)
+ Wait(1000)
+ turn_around
+ Wait(2000)
+ turn_around
+ Wait(1000)
+ turn_around
+ Off(OUT_AB)
+
diff --git a/pynxc/tutorial_samples/6_define_b.py b/pynxc/tutorial_samples/6_define_b.py
new file mode 100644
index 0000000..0e21654
--- /dev/null
+++ b/pynxc/tutorial_samples/6_define_b.py
@@ -0,0 +1,16 @@
+DEFINE turn_right(s,t)=OnFwd(OUT_A, s);OnRev(OUT_B, s);Wait(t);
+DEFINE turn_left(s,t)=OnRev(OUT_A, s);OnFwd(OUT_B, s);Wait(t);
+DEFINE forwards(s,t)=OnFwd(OUT_AB, s);Wait(t);
+DEFINE backwards(s,t)=OnRev(OUT_AB, s);Wait(t);
+
+def main():
+
+ backwards(50,10000)
+ forwards(50,10000)
+ turn_left(75,750)
+ forwards(75,1000)
+ backwards(75,2000)
+ forwards(75,1000)
+ turn_right(75,750)
+ forwards(30,2000)
+ Off(OUT_AB)
diff --git a/pynxc/tutorial_samples/6_inline.py b/pynxc/tutorial_samples/6_inline.py
new file mode 100644
index 0000000..7465a6d
--- /dev/null
+++ b/pynxc/tutorial_samples/6_inline.py
@@ -0,0 +1,17 @@
+def inline_turn_around():
+ OnRev(OUT_C, 75)
+ Wait(900)
+ OnFwd(OUT_AC, 75)
+
+def main():
+
+ OnFwd(OUT_AC, 75)
+ Wait(1000)
+ inline_turn_around()
+ Wait(2000)
+ inline_turn_around()
+ Wait(1000)
+ inline_turn_around()
+ Off(OUT_AC)
+
+
diff --git a/pynxc/tutorial_samples/6_inline2.py b/pynxc/tutorial_samples/6_inline2.py
new file mode 100644
index 0000000..0447f11
--- /dev/null
+++ b/pynxc/tutorial_samples/6_inline2.py
@@ -0,0 +1,16 @@
+def inline_turn_around(pwr,turntime):
+
+ OnRev(OUT_C, pwr)
+ Wait(turntime)
+ OnFwd(OUT_AC, pwr)
+
+def main():
+
+ OnFwd(OUT_AC, 75)
+ Wait(1000)
+ inline_turn_around(75, 2000)
+ Wait(2000)
+ inline_turn_around(75, 500)
+ Wait(1000)
+ inline_turn_around(75, 3000)
+ Off(OUT_AC)
diff --git a/pynxc/tutorial_samples/6_subs.py b/pynxc/tutorial_samples/6_subs.py
new file mode 100644
index 0000000..b35313c
--- /dev/null
+++ b/pynxc/tutorial_samples/6_subs.py
@@ -0,0 +1,13 @@
+def sub_turn_around(pwr):
+ OnRev(OUT_C, pwr); Wait(900)
+ OnFwd(OUT_AC, pwr)
+
+def main():
+ OnFwd(OUT_AC, 75)
+ Wait(1000)
+ sub_turn_around(75)
+ Wait(2000)
+ sub_turn_around(75)
+ Wait(1000)
+ sub_turn_around(75)
+ Off(OUT_AC)
diff --git a/pynxc/tutorial_samples/6_tasks.py b/pynxc/tutorial_samples/6_tasks.py
new file mode 100644
index 0000000..8651d3c
--- /dev/null
+++ b/pynxc/tutorial_samples/6_tasks.py
@@ -0,0 +1,23 @@
+
+moveMutex=Mutex()
+
+def task_move_square():
+ while True:
+ Acquire(moveMutex)
+ OnFwd(OUT_AC, 75); Wait(1000)
+ OnRev(OUT_C, 75); Wait(500)
+ Release(moveMutex)
+
+def task_check_sensors():
+ while True:
+ if SENSOR_1 == 1:
+ Acquire(moveMutex)
+ OnRev(OUT_AC, 75); Wait(500)
+ OnFwd(OUT_A, 75); Wait(500)
+ Release(moveMutex)
+
+def main():
+ Precedes(task_move_square, task_check_sensors)
+ SetSensorTouch(IN_1)
+
+
diff --git a/pynxc/tutorial_samples/7_drive_music.py b/pynxc/tutorial_samples/7_drive_music.py
new file mode 100644
index 0000000..750be10
--- /dev/null
+++ b/pynxc/tutorial_samples/7_drive_music.py
@@ -0,0 +1,15 @@
+def task_music():
+ while True:
+ PlayTone(262,400); Wait(500);
+ PlayTone(294,400); Wait(500);
+ PlayTone(330,400); Wait(500);
+ PlayTone(294,400); Wait(500);
+
+def task_movement():
+
+ while True:
+ OnFwd(OUT_AC, 75); Wait(3000);
+ OnRev(OUT_AC, 75); Wait(3000);
+
+def main():
+ Precedes(task_music, task_movement);
diff --git a/pynxc/tutorial_samples/7_music.py b/pynxc/tutorial_samples/7_music.py
new file mode 100644
index 0000000..824647d
--- /dev/null
+++ b/pynxc/tutorial_samples/7_music.py
@@ -0,0 +1,8 @@
+DEFINE VOL=3
+
+def main():
+ PlayToneEx(262,400,VOL,False); Wait(500)
+ PlayToneEx(294,400,VOL,False); Wait(500)
+ PlayToneEx(330,400,VOL,False); Wait(500)
+ PlayToneEx(294,400,VOL,False); Wait(500)
+ PlayToneEx(262,1600,VOL,False); Wait(2000)
diff --git a/pynxc/tutorial_samples/7_sounds.py b/pynxc/tutorial_samples/7_sounds.py
new file mode 100644
index 0000000..90de9fb
--- /dev/null
+++ b/pynxc/tutorial_samples/7_sounds.py
@@ -0,0 +1,22 @@
+DEFINE TIME=300
+DEFINE MAXVOL=7
+DEFINE MINVOL=1
+DEFINE MIDVOL=3
+
+DEFINE pause_4th=Wait(TIME)
+DEFINE pause_8th=Wait(TIME/2)
+DEFINE note_4th=PlayFileEx("! Click.rso",MIDVOL,FALSE); pause_4th
+DEFINE note_8th=PlayFileEx("! Click.rso",MAXVOL,FALSE); pause_8th
+
+def main():
+ PlayFileEx("! Startup.rso",MINVOL,False)
+ Wait(2000)
+ note_4th
+ note_8th
+ note_8th
+ note_4th
+ note_4th
+ pause_4th
+ note_4th
+ note_4th
+ Wait(100)
diff --git a/pynxc/tutorial_samples/8_OnReg.py b/pynxc/tutorial_samples/8_OnReg.py
new file mode 100644
index 0000000..864fa83
--- /dev/null
+++ b/pynxc/tutorial_samples/8_OnReg.py
@@ -0,0 +1,17 @@
+def main():
+
+ OnFwdReg(OUT_AC,50,OUT_REGMODE_IDLE)
+ Wait(2000)
+ Off(OUT_AC)
+ PlayTone(4000,50)
+ Wait(1000)
+ ResetAllTachoCounts(OUT_AC)
+ OnFwdReg(OUT_AC,50,OUT_REGMODE_SPEED)
+ Wait(2000)
+ Off(OUT_AC)
+ PlayTone(4000,50)
+ Wait(1000)
+ OnFwdReg(OUT_AC,50,OUT_REGMODE_SYNC)
+ Wait(2000)
+ Off(OUT_AC)
+
diff --git a/pynxc/tutorial_samples/8_OnSync.py b/pynxc/tutorial_samples/8_OnSync.py
new file mode 100644
index 0000000..20ac0d9
--- /dev/null
+++ b/pynxc/tutorial_samples/8_OnSync.py
@@ -0,0 +1,15 @@
+def main():
+ PlayTone(5000,30)
+ OnFwdSync(OUT_AC,50,0)
+ Wait(1000)
+ PlayTone(5000,30)
+ OnFwdSync(OUT_AC,50,20)
+ Wait(1000)
+ PlayTone(5000,30)
+ OnFwdSync(OUT_AC,50,-40)
+ Wait(1000)
+ PlayTone(5000,30)
+ OnRevSync(OUT_AC,50,90)
+ Wait(1000)
+ Off(OUT_AC)
+
diff --git a/pynxc/tutorial_samples/8_PID.py b/pynxc/tutorial_samples/8_PID.py
new file mode 100644
index 0000000..a4c09a2
--- /dev/null
+++ b/pynxc/tutorial_samples/8_PID.py
@@ -0,0 +1,8 @@
+DEFINE P=40
+DEFINE I=40
+DEFINE D=70
+
+def main():
+ RotateMotorPID(OUT_A, 100, 180, P, I, D)
+ Wait(2000)
+
diff --git a/pynxc/tutorial_samples/8_RotateMotor.py b/pynxc/tutorial_samples/8_RotateMotor.py
new file mode 100644
index 0000000..10704f9
--- /dev/null
+++ b/pynxc/tutorial_samples/8_RotateMotor.py
@@ -0,0 +1,4 @@
+def main():
+ RotateMotor(OUT_AC, 50,360)
+ RotateMotor(OUT_C, 50,-360)
+
diff --git a/pynxc/tutorial_samples/8_RotateMotorEx.py b/pynxc/tutorial_samples/8_RotateMotorEx.py
new file mode 100644
index 0000000..c40d796
--- /dev/null
+++ b/pynxc/tutorial_samples/8_RotateMotorEx.py
@@ -0,0 +1,7 @@
+def main():
+
+ RotateMotorEx(OUT_AC, 50, 360, 0, True,False)
+ RotateMotorEx(OUT_AC, 50, 360, 40, True,False)
+ RotateMotorEx(OUT_AC, 50, 360, -40, True,False)
+ RotateMotorEx(OUT_AC, 50, 360, 100, True,True)
+
diff --git a/pynxc/tutorial_samples/8_float.py b/pynxc/tutorial_samples/8_float.py
new file mode 100644
index 0000000..678404b
--- /dev/null
+++ b/pynxc/tutorial_samples/8_float.py
@@ -0,0 +1,8 @@
+def main():
+ OnFwd(OUT_AC, 75)
+ Wait(500)
+ Off(OUT_AC)
+ Wait(1000)
+ OnFwd(OUT_AC, 75)
+ Wait(500)
+ Coast(OUT_AC)
diff --git a/pynxc/tutorial_samples/9_multiple.py b/pynxc/tutorial_samples/9_multiple.py
new file mode 100644
index 0000000..7ec8aa2
--- /dev/null
+++ b/pynxc/tutorial_samples/9_multiple.py
@@ -0,0 +1,38 @@
+moveMutex=Mutex()
+
+def task_moverandom():
+
+ while True:
+
+ ttt = Random(500) + 40
+ tt2 = Random(1)
+ Acquire(moveMutex)
+ if tt2 > 0:
+ OnRev(OUT_A, 75)
+ OnFwd(OUT_C, 75)
+ Wait(ttt)
+ else:
+ OnRev(OUT_C, 75)
+ OnFwd(OUT_A, 75)
+ Wait(ttt)
+
+ ttt = Random(1500) + 50
+ OnFwd(OUT_AC, 75)
+ Wait(ttt)
+ Release(moveMutex)
+
+def task_submain():
+
+ SetSensorType(IN_1, SENSOR_TYPE_LIGHT);
+ SetSensorMode(IN_1, SENSOR_MODE_RAW);
+ while True:
+
+ if (SENSOR_1 < 100) or (SENSOR_1 > 750):
+ Acquire(moveMutex)
+ OnRev(OUT_AC, 75); Wait(300)
+ Release(moveMutex)
+
+def main():
+ Precedes(task_moverandom, task_submain)
+
+
diff --git a/pynxc/tutorial_samples/9_pulses.py b/pynxc/tutorial_samples/9_pulses.py
new file mode 100644
index 0000000..81eef2e
--- /dev/null
+++ b/pynxc/tutorial_samples/9_pulses.py
@@ -0,0 +1,14 @@
+def main():
+ SetSensorType(IN_1, SENSOR_TYPE_TOUCH)
+ SetSensorMode(IN_1, SENSOR_MODE_PULSE)
+ while True:
+
+ ClearSensor(IN_1)
+ while SENSOR_1<=0:
+ pass
+
+ Wait(500)
+ if SENSOR_1 == 1:
+ Off(OUT_AC)
+ if SENSOR_1 == 2:
+ OnFwd(OUT_AC, 75)
diff --git a/pynxc/tutorial_samples/9_rotation.py b/pynxc/tutorial_samples/9_rotation.py
new file mode 100644
index 0000000..6d91afa
--- /dev/null
+++ b/pynxc/tutorial_samples/9_rotation.py
@@ -0,0 +1,17 @@
+def main():
+
+
+ SetSensor(IN_1, SENSOR_ROTATION); ClearSensor(IN_1)
+ SetSensor(IN_3, SENSOR_ROTATION); ClearSensor(IN_3)
+
+ while True:
+ if SENSOR_1 < SENSOR_3:
+ OnFwd(OUT_A, 75)
+ Float(OUT_C)
+ elif SENSOR_1 > SENSOR_3:
+ OnFwd(OUT_C, 75)
+ Float(OUT_A)
+ else:
+ OnFwd(OUT_AC, 75)
+
+
diff --git a/pynxc/tutorial_samples/test1.py b/pynxc/tutorial_samples/test1.py
new file mode 100644
index 0000000..27cc889
--- /dev/null
+++ b/pynxc/tutorial_samples/test1.py
@@ -0,0 +1,9 @@
+def main():
+
+ OnFwd(OUT_A,100)
+ OnFwd(OUT_B,100)
+
+ Wait(5000)
+
+ Off(OUT_A)
+ Off(OUT_B)
diff --git a/pynxc/tutorial_samples/test_string.py b/pynxc/tutorial_samples/test_string.py
new file mode 100644
index 0000000..2feef3f
--- /dev/null
+++ b/pynxc/tutorial_samples/test_string.py
@@ -0,0 +1,5 @@
+def main():
+
+ a='hello'
+
+ \ No newline at end of file
diff --git a/pynxc/waxy/.DS_Store b/pynxc/waxy/.DS_Store
new file mode 100644
index 0000000..e9b3bb0
--- /dev/null
+++ b/pynxc/waxy/.DS_Store
Binary files differ
diff --git a/pynxc/waxy/__init__.py b/pynxc/waxy/__init__.py
new file mode 100644
index 0000000..b9cae90
--- /dev/null
+++ b/pynxc/waxy/__init__.py
@@ -0,0 +1,95 @@
+WAXY_VERSION = "0.0.1"
+WAXY_VERSION_TUPLE = tuple(map(int, WAXY_VERSION.split(".")))
+
+__version__ = WAXY_VERSION
+
+__original_copyright__ = "Copyright 2003-2006 Hans Nowak"
+__original_author__ = "Hans Nowak (hans@zephyrfalcon.org)"
+
+# this code is lifted from the, now unsupported, project wax
+# i've cleaned things up a bit, taken out some things that
+# don't work in the new wx, and keep modifying it to suit
+# my needs
+
+__license__ = "BSD"
+__author__ = "Brian Blais (bblais@bryant.edu)"
+
+
+
+# updated for recent wx, and modified as needed by bblais
+
+import sys
+import core # builtin functions and such
+import wx
+
+from wx import Yield
+from constants import *
+from wx import Point
+
+from aboutbox import AboutBox
+from application import Application
+from artprovider import ArtProvider
+from bitmap import Bitmap, BitmapFromData, BitmapFromFile
+from bitmapbutton import BitmapButton
+from button import Button
+from canvas import Canvas
+from checkbox import CheckBox
+from checklistbox import CheckListBox
+from colordb import ColorDB
+from colourdialog import ColourDialog,ColorDialog
+from combobox import ComboBox
+from containers import Container # do we need to publish this?
+from customdialog import CustomDialog
+from dialog import Dialog, showdialog
+from directorydialog import DirectoryDialog,ChooseDirectory
+from dragdrop import FileDropTarget, TextDropTarget, URLDropTarget
+from dropdownbox import DropDownBox
+from filedialog import FileDialog
+##from filetreeview import FileTreeView
+##from findreplacedialog import FindReplaceDialog
+##from flexgridframe import FlexGridFrame
+from flexgridpanel import FlexGridPanel
+from font import Font
+##from fontdialog import FontDialog
+from frame import Frame, HorizontalFrame, VerticalFrame
+from grid import Grid
+##from gridframe import GridFrame
+from gridpanel import GridPanel
+##from groupbox import GroupBox
+from htmlwindow import HTMLWindow
+from image import Image, AddImageHandler, AddAllImageHandlers, ImageAsBitmap,ImagePanel
+from imagelist import ImageList
+from keys import keys
+from label import Label
+from line import Line
+from listbox import ListBox
+##from listview import ListView, ListItemAttr
+#from maskedtextbox import MaskedTextBox
+from menu import Menu, MenuBar
+from messagedialog import MessageDialog, ShowMessage, Error
+##from mdiframes import MDIChildFrame, MDIParentFrame
+from mousepointer import MousePointers
+from multichoicedialog import MultiChoiceDialog
+from notebook import NoteBook
+##from overlaypanel import OverlayPanel
+from panel import Panel, HorizontalPanel, VerticalPanel
+##from plainframe import PlainFrame
+##from plainpanel import PlainPanel
+from progressdialog import ProgressDialog
+##from radiobutton import RadioButton
+##from scrollframe import ScrollFrame
+##from shell import PyCrust, PyCrustFilling
+##from simpleeditor import SimpleEditor
+from singlechoicedialog import SingleChoiceDialog
+from slider import Slider,FloatSlider
+from splitter import Splitter
+from statusbar import StatusBar
+##from styledtextbox import StyledTextBox
+##from systemsettings import SystemSettings
+from textbox import TextBox
+from textentrydialog import TextEntryDialog,IntegerInputDialog,Input_Integer,FloatInputDialog,FloatInputDialog
+##from timer import Timer
+from treelistview import TreeListView
+from treeview import TreeView
+from waxyobject import WaxyObject
+
diff --git a/pynxc/waxy/__init__.py~ b/pynxc/waxy/__init__.py~
new file mode 100644
index 0000000..1964639
--- /dev/null
+++ b/pynxc/waxy/__init__.py~
@@ -0,0 +1,77 @@
+WAXY_VERSION = "0.0.1"
+WAXY_VERSION_TUPLE = tuple(map(int, WAXY_VERSION.split(".")))
+
+__version__ = WAXY_VERSION
+__license__ = "BSD"
+__author__ = "Brian Blais (bblais@bryant.edu)"
+
+import sys
+import core # builtin functions and such
+import wx
+
+from aboutbox import AboutBox
+from application import Application
+from artprovider import ArtProvider
+from bitmap import Bitmap, BitmapFromData, BitmapFromFile
+from bitmapbutton import BitmapButton
+from button import Button
+###from canvas import Canvas
+from checkbox import CheckBox
+from checklistbox import CheckListBox
+from colordb import ColorDB
+from colourdialog import ColourDialog,ColorDialog
+from combobox import ComboBox
+from containers import Container # do we need to publish this?
+from customdialog import CustomDialog
+from dialog import Dialog, showdialog
+from directorydialog import DirectoryDialog,ChooseDirectory
+###from dragdrop import FileDropTarget, TextDropTarget, URLDropTarget
+from dropdownbox import DropDownBox
+from filedialog import FileDialog
+##from filetreeview import FileTreeView
+##from findreplacedialog import FindReplaceDialog
+##from flexgridframe import FlexGridFrame
+from flexgridpanel import FlexGridPanel
+from font import Font
+##from fontdialog import FontDialog
+from frame import Frame, HorizontalFrame, VerticalFrame
+##from grid import Grid
+##from gridframe import GridFrame
+##from gridpanel import GridPanel
+##from groupbox import GroupBox
+from htmlwindow import HTMLWindow
+from image import Image, AddImageHandler, AddAllImageHandlers, ImageAsBitmap
+##from imagelist import ImageList
+from keys import keys
+from label import Label
+from line import Line
+from listbox import ListBox
+##from listview import ListView, ListItemAttr
+#from maskedtextbox import MaskedTextBox
+from menu import Menu, MenuBar
+from messagedialog import MessageDialog, ShowMessage
+##from mdiframes import MDIChildFrame, MDIParentFrame
+##from mousepointer import MousePointers
+from multichoicedialog import MultiChoiceDialog
+from notebook import NoteBook
+##from overlaypanel import OverlayPanel
+from panel import Panel, HorizontalPanel, VerticalPanel
+##from plainframe import PlainFrame
+##from plainpanel import PlainPanel
+from progressdialog import ProgressDialog
+##from radiobutton import RadioButton
+##from scrollframe import ScrollFrame
+##from shell import PyCrust, PyCrustFilling
+##from simpleeditor import SimpleEditor
+from singlechoicedialog import SingleChoiceDialog
+from splitter import Splitter
+##from statusbar import StatusBar
+##from styledtextbox import StyledTextBox
+##from systemsettings import SystemSettings
+from textbox import TextBox
+from textentrydialog import TextEntryDialog
+##from timer import Timer
+##from treelistview import TreeListView
+##from treeview import TreeView
+from waxyobject import WaxyObject
+
diff --git a/pynxc/waxy/aboutbox.py b/pynxc/waxy/aboutbox.py
new file mode 100644
index 0000000..884e406
--- /dev/null
+++ b/pynxc/waxy/aboutbox.py
@@ -0,0 +1,43 @@
+import wx
+from wx.lib.wordwrap import wordwrap
+
+def AboutBox(parent,info=None,
+ name='',version='',copyright='',description='',url='',
+ webtitle='',developers=[],license=''):
+
+
+ if not info:
+
+ info={}
+ info['name']=name
+ info['version']=version
+ info['copyright']=copyright
+ info['description']=description
+ info['url']=url
+ info['webtitle']=webtitle
+ info['developers']=developers
+ info['license']=license
+
+
+
+ ainfo = wx.AboutDialogInfo()
+ ainfo.Name = info['name']
+ ainfo.Version = info['version']
+ ainfo.Copyright = info['copyright']
+
+ if '(C)' not in ainfo.Copyright:
+ ainfo.Copyright='(C) '+info.Copyright
+
+ ainfo.Description = wordwrap(info['description'],
+ 350, wx.ClientDC(parent))
+ ainfo.WebSite = (info['url'],info['webtitle'])
+ ainfo.Developers = info['developers']
+
+ ainfo.License = wordwrap(info['license'], 500, wx.ClientDC(parent))
+
+ # Then we call wx.AboutBox giving it that info object
+
+
+
+ wx.AboutBox(ainfo)
+
diff --git a/pynxc/waxy/aboutbox.py~ b/pynxc/waxy/aboutbox.py~
new file mode 100644
index 0000000..d84fc97
--- /dev/null
+++ b/pynxc/waxy/aboutbox.py~
@@ -0,0 +1,43 @@
+import wx
+from wx.lib.wordwrap import wordwrap
+
+def AboutBox(parent,info=None,
+ name='',version='',copyright='',description='',url='',
+ webtitle='',developers=[],license=''):
+
+
+ if not info:
+
+ info={}
+ info['name']=name
+ info['version']=version
+ info['copyright']=copyright
+ info['description']=description
+ info['url']=url
+ info['webtitle']=webtitle
+ info['developers']=developers
+ info['license']=license
+
+
+
+ ainfo = wx.AboutDialogInfo()
+ ainfo.Name = info['name']
+ ainfo.Version = info['version']
+ ainfo.Copyright = info['copyright']
+
+ if '(C)' not in ainfo.Copyright:
+ ainfo.Copyright='(C) '+info.Copyright
+
+ ainfo.Description = wordwrap(info['description'],
+ 350, wx.ClientDC(parent))
+ ainfo.WebSite = (info['url'],info['webtitle'])
+ ainfo.Developers = info['developers']
+
+ ainfo.License = wordwrap(info['license'], 500, wx.ClientDC(parent))
+
+ # Then we call wx.AboutBox giving it that info object
+
+
+
+ wx.AboutBox(info)
+
diff --git a/pynxc/waxy/application.py b/pynxc/waxy/application.py
new file mode 100644
index 0000000..c6c01fd
--- /dev/null
+++ b/pynxc/waxy/application.py
@@ -0,0 +1,32 @@
+# application.py
+
+import wx
+import sys
+#from waxconfig import WaxConfig
+#import font
+
+class Application(wx.App):
+ def __init__(self, frameklass, *args, **kwargs):
+ # takes a frame *class* plus arbitrary options. these options will
+ # be passed to the frame constructor.
+ self.frameklass = frameklass
+ self.args = args
+ self.kwargs = kwargs
+
+ # when set, the app uses the stdout/stderr window; off by default
+ use_stdout_window = 0
+ if kwargs.has_key('use_stdout_window'):
+ use_stdout_window = kwargs['use_stdout_window']
+ del kwargs['use_stdout_window']
+ wx.App.__init__(self, use_stdout_window)
+
+ def OnInit(self):
+ self.mainframe = self.frameklass(*self.args, **self.kwargs)
+ if hasattr(self.mainframe.__class__, "__ExceptHook__"):
+ sys.excepthook = self.mainframe.__ExceptHook__
+ self.mainframe.Show(True)
+ self.SetTopWindow(self.mainframe)
+ return True
+
+ def Run(self):
+ self.MainLoop()
diff --git a/pynxc/waxy/application.py~ b/pynxc/waxy/application.py~
new file mode 100644
index 0000000..eab2e66
--- /dev/null
+++ b/pynxc/waxy/application.py~
@@ -0,0 +1,36 @@
+# application.py
+
+import wx
+import sys
+#from waxconfig import WaxConfig
+#import font
+
+class Application(wx.App):
+ def __init__(self, frameklass, *args, **kwargs):
+ # takes a frame *class* plus arbitrary options. these options will
+ # be passed to the frame constructor.
+ self.frameklass = frameklass
+ self.args = args
+ self.kwargs = kwargs
+
+ # when set, the app uses the stdout/stderr window; off by default
+ use_stdout_window = 0
+ if kwargs.has_key('use_stdout_window'):
+ use_stdout_window = kwargs['use_stdout_window']
+ del kwargs['use_stdout_window']
+ wx.App.__init__(self, use_stdout_window)
+
+ def OnInit(self):
+ if isinstance(WaxConfig.default_font, tuple):
+ WaxConfig.default_font = font.Font(*WaxConfig.default_font)
+ else:
+ print >> sys.stderr, "Warning: This construct will not work in future wxPython versions"
+ self.mainframe = self.frameklass(*self.args, **self.kwargs)
+ if hasattr(self.mainframe.__class__, "__ExceptHook__"):
+ sys.excepthook = self.mainframe.__ExceptHook__
+ self.mainframe.Show(True)
+ self.SetTopWindow(self.mainframe)
+ return True
+
+ def Run(self):
+ self.MainLoop()
diff --git a/pynxc/waxy/artprovider.py b/pynxc/waxy/artprovider.py
new file mode 100644
index 0000000..29014ad
--- /dev/null
+++ b/pynxc/waxy/artprovider.py
@@ -0,0 +1,126 @@
+# artprovider.py
+
+import wx, os
+import string
+import cStringIO
+
+ARTCLIENTS = [ "ART_TOOLBAR",
+ "ART_MENU",
+ "ART_FRAME_ICON",
+ "ART_CMN_DIALOG",
+ "ART_HELP_BROWSER",
+ "ART_MESSAGE_BOX",
+ "ART_OTHER",
+ ]
+
+ARTIDS = [ "ART_ADD_BOOKMARK",
+ "ART_DEL_BOOKMARK",
+ "ART_HELP_SIDE_PANEL",
+ "ART_HELP_SETTINGS",
+ "ART_HELP_BOOK",
+ "ART_HELP_FOLDER",
+ "ART_HELP_PAGE",
+ "ART_GO_BACK",
+ "ART_GO_FORWARD",
+ "ART_GO_UP",
+ "ART_GO_DOWN",
+ "ART_GO_TO_PARENT",
+ "ART_GO_HOME",
+ "ART_FILE_OPEN",
+ "ART_PRINT",
+ "ART_HELP",
+ "ART_TIP",
+ "ART_REPORT_VIEW",
+ "ART_LIST_VIEW",
+ "ART_NEW_DIR",
+ "ART_FOLDER",
+ "ART_GO_DIR_UP",
+ "ART_EXECUTABLE_FILE",
+ "ART_NORMAL_FILE",
+ "ART_TICK_MARK",
+ "ART_CROSS_MARK",
+ "ART_ERROR",
+ "ART_QUESTION",
+ "ART_WARNING",
+ "ART_INFORMATION",
+ ]
+
+class ArtProvider(object):
+ def __init__(self, size=wx.DefaultSize):
+ self.custom = {}
+ self.clients = []
+ for client in ARTCLIENTS:
+ self.clients.append(client[4:].lower())
+ self.images = []
+ for image in ARTIDS:
+ self.images.append(image[4:].lower())
+ self.size = size
+ self._usecustom = False
+
+ def _SetUseCustom(self, value):
+ if self._usecustom and not value:
+ wx.ArtProvider_PopProvider()
+ elif value and not self._usecustom:
+ wx.ArtProvider_PushProvider(ArtPushProvider(self))
+ self._usecustom = value
+
+ def _GetUseCustom(self):
+ return self.__usecustom
+
+ UseCustom = property(_GetUseCustom, _SetUseCustom, doc="Custom art enable")
+
+ def _GetWxArtIds(self, name, client):
+ imageid = "ART_" + name.upper()
+ clientid = "ART_" + client.upper()
+ wx_image = getattr(wx, imageid, name)
+ wx_client = getattr(wx, clientid, client)
+ return wx_image, wx_client
+
+ def GetBitmap(self, image, client='other', size=None):
+ wx_image, wx_client = self._GetWxArtIds(image, client)
+ if size == None:
+ size = self.size
+ return wx.ArtProvider_GetBitmap(wx_image, wx_client, size)
+
+ def GetIcon(self, name, client='other', size=None):
+ wx_image, wx_client = self._GetWxArtIds(name, client)
+ if size == None:
+ size = self.size
+ return wx.ArtProvider_GetIcon(wx_image, wx_client, size)
+
+ def RegisterImage(self, image, name, client='other', size=None):
+ wx_image, wx_client = self._GetWxArtIds(name, client)
+ if size == None:
+ size = self.size
+ keyword = string.join([wx_client,"::", wx_image, "::", str(size[0]),":", str(size[1])],'')
+ if size != (-1,-1):
+ image.Rescale(size[0], size[1])
+ self.custom[keyword] = wx.BitmapFromImage(image)
+ if wx_client not in self.clients:
+ if not wx_client.startswith('wxART'):
+ self.clients.append(wx_client)
+ if wx_image not in self.images:
+ if not wx_image.startswith('wxART'):
+ self.images.append(wx_image)
+
+ def RegisterFromFile(self, filename, name, client='other', size=None):
+ image = wx.Image(opj(filename), wx.BITMAP_TYPE_ANY)
+ self.RegisterImage(image, name, client, size)
+
+ def RegisterFromData(self, stream, name, client='other', size=None):
+ image = wx.ImageFromStream(stream)
+ self.RegisterImage(image, name, client, size)
+
+class ArtPushProvider(wx.ArtProvider):
+ # custom class for push_provider
+ def __init__(self, waxArtProvider):
+ wx.ArtProvider.__init__(self)
+ self.__AP = waxArtProvider
+
+ def CreateBitmap(self, artid, client, size):
+ keyword = string.join([client,"::", artid, "::", str(size[0]),":", str(size[1])],'')
+ bmp = wx.NullBitmap
+ if self.__AP.custom.has_key(keyword):
+ bmp = self.__AP.custom[keyword]
+ return bmp
+
diff --git a/pynxc/waxy/bitmap.py b/pynxc/waxy/bitmap.py
new file mode 100644
index 0000000..49f0f8a
--- /dev/null
+++ b/pynxc/waxy/bitmap.py
@@ -0,0 +1,29 @@
+# bitmap.py
+
+import wx
+import waxyobject
+import image
+import cStringIO
+
+# XXX not sure what to do with this
+class Bitmap(wx.StaticBitmap, waxyobject.WaxyObject):
+
+ def __init__(self, parent, bitmap):
+ if isinstance(bitmap, str) or isinstance(bitmap, unicode):
+ bitmap = BitmapFromFile(bitmap)
+ wx.StaticBitmap.__init__(self, parent, wx.NewId(), bitmap)
+ # XXX supposedly you can load this from file too?
+
+#
+# use these functions for convenience...
+# unfortunately, they return wxBitmaps, not Bitmaps
+
+def BitmapFromData(data):
+ stream = cStringIO.StringIO(data)
+ z = wx.ImageFromStream(stream)
+ return wx.BitmapFromImage(z)
+
+def BitmapFromFile(filename):
+ data = open(filename, 'rb').read()
+ return BitmapFromData(data)
+
diff --git a/pynxc/waxy/bitmap.py~ b/pynxc/waxy/bitmap.py~
new file mode 100644
index 0000000..7a3629c
--- /dev/null
+++ b/pynxc/waxy/bitmap.py~
@@ -0,0 +1,29 @@
+# bitmap.py
+
+import wx
+import waxobject
+import image
+import cStringIO
+
+# XXX not sure what to do with this
+class Bitmap(wx.StaticBitmap, waxobject.WaxObject):
+
+ def __init__(self, parent, bitmap):
+ if isinstance(bitmap, str) or isinstance(bitmap, unicode):
+ bitmap = BitmapFromFile(bitmap)
+ wx.StaticBitmap.__init__(self, parent, wx.NewId(), bitmap)
+ # XXX supposedly you can load this from file too?
+
+#
+# use these functions for convenience...
+# unfortunately, they return wxBitmaps, not Bitmaps
+
+def BitmapFromData(data):
+ stream = cStringIO.StringIO(data)
+ z = wx.ImageFromStream(stream)
+ return wx.BitmapFromImage(z)
+
+def BitmapFromFile(filename):
+ data = open(filename, 'rb').read()
+ return BitmapFromData(data)
+
diff --git a/pynxc/waxy/bitmapbutton.py b/pynxc/waxy/bitmapbutton.py
new file mode 100644
index 0000000..0955387
--- /dev/null
+++ b/pynxc/waxy/bitmapbutton.py
@@ -0,0 +1,49 @@
+# bitmapbutton.py
+
+import button
+import containers
+import waxyobject
+import wx
+import os
+import styles
+
+def opj(path):
+ """Convert paths to the platform-specific separator"""
+ return apply(os.path.join, tuple(path.split('/')))
+
+def loadbitmap(filename):
+ """Load a bitmap from an image file"""
+ return wx.Image(opj(filename), wx.BITMAP_TYPE_ANY).ConvertToBitmap()
+
+class BitmapButton(wx.BitmapButton, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Click': wx.EVT_BUTTON,
+ }
+
+ def __init__(self, parent, bmp, event=None, default_style=1, size=None, **kwargs):
+ if isinstance(bmp, str) or isinstance(bmp, unicode):
+ bmp = loadbitmap(bmp)
+ style = default_style and 4 or 0
+ style |= self._params(kwargs)
+ style |= self._params(kwargs, button.Button.__styles__)
+ style |= styles.window(kwargs)
+
+ wx.BitmapButton.__init__(self, parent, wx.NewId(), bmp,
+ size=size or (-1,-1), style=style)
+
+ self.BindEvents()
+ if event:
+ self.OnClick = event
+ styles.properties(self, kwargs)
+
+ __styles__ = {
+ 'align': ({
+ 'left': wx.BU_LEFT,
+ 'top': wx.BU_TOP,
+ 'right': wx.BU_RIGHT,
+ 'bottom': wx.BU_BOTTOM,
+ }, styles.DICTSTART),
+ 'autodraw': (wx.BU_AUTODRAW, styles.NORMAL),
+ }
+
diff --git a/pynxc/waxy/bitmapbutton.py~ b/pynxc/waxy/bitmapbutton.py~
new file mode 100644
index 0000000..2c4e286
--- /dev/null
+++ b/pynxc/waxy/bitmapbutton.py~
@@ -0,0 +1,50 @@
+# bitmapbutton.py
+
+import button
+import containers
+import waxyobject
+import wx
+import os
+import styles
+
+def opj(path):
+ """Convert paths to the platform-specific separator"""
+ return apply(os.path.join, tuple(path.split('/')))
+
+def loadbitmap(filename):
+ """Load a bitmap from an image file"""
+ return wx.Image(opj(filename), wx.BITMAP_TYPE_ANY).ConvertToBitmap()
+
+class BitmapButton(wx.BitmapButton, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Click': wx.EVT_BUTTON,
+ }
+
+ def __init__(self, parent, bmp, event=None, default_style=1, size=None, **kwargs):
+ if isinstance(bmp, str) or isinstance(bmp, unicode):
+ bmp = loadbitmap(bmp)
+ style = default_style and 4 or 0
+ style |= self._params(kwargs)
+ style |= self._params(kwargs, button.Button.__styles__)
+ style |= styles.window(kwargs)
+
+ wx.BitmapButton.__init__(self, parent, wx.NewId(), bmp,
+ size=size or (-1,-1), style=style)
+
+ self.SetDefaultFont()
+ self.BindEvents()
+ if event:
+ self.OnClick = event
+ styles.properties(self, kwargs)
+
+ __styles__ = {
+ 'align': ({
+ 'left': wx.BU_LEFT,
+ 'top': wx.BU_TOP,
+ 'right': wx.BU_RIGHT,
+ 'bottom': wx.BU_BOTTOM,
+ }, styles.DICTSTART),
+ 'autodraw': (wx.BU_AUTODRAW, styles.NORMAL),
+ }
+
diff --git a/pynxc/waxy/button.py b/pynxc/waxy/button.py
new file mode 100644
index 0000000..1538abc
--- /dev/null
+++ b/pynxc/waxy/button.py
@@ -0,0 +1,54 @@
+# button.py
+
+import containers
+import waxyobject
+import wx
+import styles
+
+
+class Button(wx.Button, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Click': wx.EVT_BUTTON,
+ }
+
+ def __init__(self, parent, text="", event=None, size=None,
+ tooltip="", default=False, disabled=False, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.Button.__init__(self, parent, wx.NewId(), text, size=size or (-1,-1),
+ style=style)
+
+ self.BindEvents()
+ if event:
+ self.OnClick = event
+
+ if tooltip:
+ self.SetToolTipString(tooltip)
+
+ if default:
+ self.SetDefault()
+
+ if disabled:
+ self.Enable(False)
+
+ styles.properties(self, kwargs)
+
+ #
+ # style parameters
+
+ __styles__ = {
+ 'align': ({
+ "left": wx.BU_LEFT,
+ "right": wx.BU_RIGHT,
+ "bottom": wx.BU_BOTTOM,
+ "top": wx.BU_TOP,
+ "exact": wx.BU_EXACTFIT,
+ }, styles.DICTSTART),
+ 'flat': (wx.NO_BORDER, styles.NORMAL),
+ # seems to have no effect on Windows XP
+ 'exactfit': (wx.BU_EXACTFIT, styles.NORMAL),
+ }
+
diff --git a/pynxc/waxy/button.py~ b/pynxc/waxy/button.py~
new file mode 100644
index 0000000..764674d
--- /dev/null
+++ b/pynxc/waxy/button.py~
@@ -0,0 +1,51 @@
+# button.py
+
+import containers
+import waxyobject
+import wx
+import styles
+
+
+class Button(wx.Button, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Click': wx.EVT_BUTTON,
+ }
+
+ def __init__(self, parent, text="", event=None, size=None,
+ tooltip="", default=False, disabled=False, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.Button.__init__(self, parent, wx.NewId(), text, size=size or (-1,-1),
+ style=style)
+
+ self.BindEvents()
+ if event:
+ self.OnClick = event
+
+ if tooltip:
+ self.SetToolTipString(tooltip)
+
+ if default:
+ self.SetDefault()
+
+ styles.properties(self, kwargs)
+
+ #
+ # style parameters
+
+ __styles__ = {
+ 'align': ({
+ "left": wx.BU_LEFT,
+ "right": wx.BU_RIGHT,
+ "bottom": wx.BU_BOTTOM,
+ "top": wx.BU_TOP,
+ "exact": wx.BU_EXACTFIT,
+ }, styles.DICTSTART),
+ 'flat': (wx.NO_BORDER, styles.NORMAL),
+ # seems to have no effect on Windows XP
+ 'exactfit': (wx.BU_EXACTFIT, styles.NORMAL),
+ }
+
diff --git a/pynxc/waxy/canvas.py b/pynxc/waxy/canvas.py
new file mode 100644
index 0000000..072fb85
--- /dev/null
+++ b/pynxc/waxy/canvas.py
@@ -0,0 +1,33 @@
+# canvas.py
+
+import wx
+import waxyobject
+
+class Canvas(wx.ScrolledWindow, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Paint': wx.EVT_PAINT,
+ }
+
+ def __init__(self, parent):
+ wx.ScrolledWindow.__init__(self, parent, wx.NewId())
+ self.Init()
+
+ self.BindEvents()
+
+ def _OnPaint(self, event=None):
+ self.OnPaint(event)
+ def OnPaint(self, event=None):
+ dc = wx.PaintDC(self)
+ self.PrepareDC(dc)
+ self.OnDraw(dc)
+ event.Skip()
+
+ def OnDraw(self, dc):
+ # override to draw on the canvas
+ pass
+
+ def Init(self):
+ # override to place scrollbars, set color, etc.
+ pass
+
diff --git a/pynxc/waxy/checkbox.py b/pynxc/waxy/checkbox.py
new file mode 100644
index 0000000..143a260
--- /dev/null
+++ b/pynxc/waxy/checkbox.py
@@ -0,0 +1,71 @@
+# checkbox.py
+
+import wx
+import waxyobject
+import styles
+
+class CheckBox(wx.CheckBox, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Check': wx.EVT_CHECKBOX,
+ }
+
+
+ def __init__(self, parent, text='', size=None, border=1, **kwargs):
+ flags = 0
+ if not border:
+ flags |= wx.NO_BORDER
+ # XXX not sure what border does... there doesn't seem to be a visible
+ # difference?
+ flags |= self._params(kwargs)
+ flags |= styles.window(kwargs)
+
+ wx.CheckBox.__init__(self, parent, wx.NewId(), text, None,
+ size or (-1, -1), flags)
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ _3states = {
+ 'checked': wx.CHK_CHECKED,
+ 'unchecked': wx.CHK_UNCHECKED,
+ 'undetermined': wx.CHK_UNDETERMINED,
+ }
+
+ def Set3StateValue(self, state):
+ for name, flag in self._3states.items():
+ if name.startswith(state):
+ wx.CheckBox.Set3StateValue(self, flag)
+ break
+ else:
+ raise KeyError, "Unknown state: %s" % (state,)
+
+ def Get3StateValue(self):
+ value = wx.CheckBox.Get3StateValue(self)
+ if value == wx.CHK_CHECKED:
+ return 'checked'
+ elif value == wx.CHK_UNCHECKED:
+ return 'unchecked'
+ elif value == wx.CHK_UNDETERMINED:
+ return 'undetermined'
+ else:
+ return '?'
+
+ #
+ # style parameters
+
+ _checkbox_states = {
+ 2: wx.CHK_2STATE,
+ 3: wx.CHK_3STATE,
+ }
+
+ _checkbox_align = {
+ 'left': 0,
+ 'right': wx.ALIGN_RIGHT,
+ }
+
+ def _params(self, kwargs):
+ flags = 0
+ flags |= styles.styledict('states', self._checkbox_states, kwargs, wx.CHK_2STATE)
+ flags |= styles.styledictstart('align', self._checkbox_align, kwargs, 0)
+ return flags
diff --git a/pynxc/waxy/checkbox.py~ b/pynxc/waxy/checkbox.py~
new file mode 100644
index 0000000..4a595f6
--- /dev/null
+++ b/pynxc/waxy/checkbox.py~
@@ -0,0 +1,72 @@
+# checkbox.py
+
+import wx
+import waxyobject
+import styles
+
+class CheckBox(wx.CheckBox, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Check': wx.EVT_CHECKBOX,
+ }
+
+
+ def __init__(self, parent, text='', size=None, border=1, **kwargs):
+ flags = 0
+ if not border:
+ flags |= wx.NO_BORDER
+ # XXX not sure what border does... there doesn't seem to be a visible
+ # difference?
+ flags |= self._params(kwargs)
+ flags |= styles.window(kwargs)
+
+ wx.CheckBox.__init__(self, parent, wx.NewId(), text, None,
+ size or (-1, -1), flags)
+
+ self.BindEvents()
+ self.SetDefaultFont()
+ styles.properties(self, kwargs)
+
+ _3states = {
+ 'checked': wx.CHK_CHECKED,
+ 'unchecked': wx.CHK_UNCHECKED,
+ 'undetermined': wx.CHK_UNDETERMINED,
+ }
+
+ def Set3StateValue(self, state):
+ for name, flag in self._3states.items():
+ if name.startswith(state):
+ wx.CheckBox.Set3StateValue(self, flag)
+ break
+ else:
+ raise KeyError, "Unknown state: %s" % (state,)
+
+ def Get3StateValue(self):
+ value = wx.CheckBox.Get3StateValue(self)
+ if value == wx.CHK_CHECKED:
+ return 'checked'
+ elif value == wx.CHK_UNCHECKED:
+ return 'unchecked'
+ elif value == wx.CHK_UNDETERMINED:
+ return 'undetermined'
+ else:
+ return '?'
+
+ #
+ # style parameters
+
+ _checkbox_states = {
+ 2: wx.CHK_2STATE,
+ 3: wx.CHK_3STATE,
+ }
+
+ _checkbox_align = {
+ 'left': 0,
+ 'right': wx.ALIGN_RIGHT,
+ }
+
+ def _params(self, kwargs):
+ flags = 0
+ flags |= styles.styledict('states', self._checkbox_states, kwargs, wx.CHK_2STATE)
+ flags |= styles.styledictstart('align', self._checkbox_align, kwargs, 0)
+ return flags
diff --git a/pynxc/waxy/checklistbox.py b/pynxc/waxy/checklistbox.py
new file mode 100644
index 0000000..67176a6
--- /dev/null
+++ b/pynxc/waxy/checklistbox.py
@@ -0,0 +1,27 @@
+# checklistbox.py
+# XXX doesn't work on Mac.
+
+# todo: styles (if any)
+
+import wx
+import waxyobject
+import styles
+
+class CheckListBox(wx.CheckListBox, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'CheckListBox': wx.EVT_CHECKLISTBOX,
+ }
+
+ def __init__(self, parent, choices=[], size=None, **kwargs):
+ style = 0
+ #style |= checklistbox_params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.CheckListBox.__init__(self, parent, wx.NewId(), choices=choices, style=style)
+
+ self.BindEvents()
+
+ styles.properties(self, kwargs)
+
+
diff --git a/pynxc/waxy/checklistbox.py~ b/pynxc/waxy/checklistbox.py~
new file mode 100644
index 0000000..34c7dd2
--- /dev/null
+++ b/pynxc/waxy/checklistbox.py~
@@ -0,0 +1,28 @@
+# checklistbox.py
+# XXX doesn't work on Mac.
+
+# todo: styles (if any)
+
+import wx
+import waxyobject
+import styles
+
+class CheckListBox(wx.CheckListBox, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'CheckListBox': wx.EVT_CHECKLISTBOX,
+ }
+
+ def __init__(self, parent, choices=[], size=None, **kwargs):
+ style = 0
+ #style |= checklistbox_params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.CheckListBox.__init__(self, parent, wx.NewId(), choices=choices, style=style)
+
+ self.SetDefaultFont()
+ self.BindEvents()
+
+ styles.properties(self, kwargs)
+
+
diff --git a/pynxc/waxy/colordb.py b/pynxc/waxy/colordb.py
new file mode 100644
index 0000000..b2cb6cd
--- /dev/null
+++ b/pynxc/waxy/colordb.py
@@ -0,0 +1,844 @@
+# colordb.py
+# Allows access to Tk's color names, plus adds some other functions.
+# You can now do obj.SetForegroundColor(colordb['papayawhip'])! ;-)
+# Or: obj.SetForegroundColor('papayawhip').
+# or even: obj.ForegroundColor = 'papayawhip'!
+
+import math
+import operator
+import string
+
+def convert_color(color):
+ if isinstance(color, str):
+ color = _colordb.byname(color)
+ # TODO: maybe colors in the format "#007FC0"?
+ return color
+
+data = {
+ 'alice blue': (240, 248, 255),
+ 'aliceblue': (240, 248, 255),
+ 'antique white': (250, 235, 215),
+ 'antiquewhite': (250, 235, 215),
+ 'antiquewhite1': (255, 239, 219),
+ 'antiquewhite2': (238, 223, 204),
+ 'antiquewhite3': (205, 192, 176),
+ 'antiquewhite4': (139, 131, 120),
+ 'aquamarine': (127, 255, 212),
+ 'aquamarine1': (127, 255, 212),
+ 'aquamarine2': (118, 238, 198),
+ 'aquamarine3': (102, 205, 170),
+ 'aquamarine4': (69, 139, 116),
+ 'azure': (240, 255, 255),
+ 'azure1': (240, 255, 255),
+ 'azure2': (224, 238, 238),
+ 'azure3': (193, 205, 205),
+ 'azure4': (131, 139, 139),
+ 'beige': (245, 245, 220),
+ 'bisque': (255, 228, 196),
+ 'bisque1': (255, 228, 196),
+ 'bisque2': (238, 213, 183),
+ 'bisque3': (205, 183, 158),
+ 'bisque4': (139, 125, 107),
+ 'black': (0, 0, 0),
+ 'blanched almond': (255, 235, 205),
+ 'blanchedalmond': (255, 235, 205),
+ 'blue': (0, 0, 255),
+ 'blue violet': (138, 43, 226),
+ 'blue1': (0, 0, 255),
+ 'blue2': (0, 0, 238),
+ 'blue3': (0, 0, 205),
+ 'blue4': (0, 0, 139),
+ 'blueviolet': (138, 43, 226),
+ 'brown': (165, 42, 42),
+ 'brown1': (255, 64, 64),
+ 'brown2': (238, 59, 59),
+ 'brown3': (205, 51, 51),
+ 'brown4': (139, 35, 35),
+ 'burlywood': (222, 184, 135),
+ 'burlywood1': (255, 211, 155),
+ 'burlywood2': (238, 197, 145),
+ 'burlywood3': (205, 170, 125),
+ 'burlywood4': (139, 115, 85),
+ 'cadet blue': (95, 158, 160),
+ 'cadetblue': (95, 158, 160),
+ 'cadetblue1': (152, 245, 255),
+ 'cadetblue2': (142, 229, 238),
+ 'cadetblue3': (122, 197, 205),
+ 'cadetblue4': (83, 134, 139),
+ 'chartreuse': (127, 255, 0),
+ 'chartreuse1': (127, 255, 0),
+ 'chartreuse2': (118, 238, 0),
+ 'chartreuse3': (102, 205, 0),
+ 'chartreuse4': (69, 139, 0),
+ 'chocolate': (210, 105, 30),
+ 'chocolate1': (255, 127, 36),
+ 'chocolate2': (238, 118, 33),
+ 'chocolate3': (205, 102, 29),
+ 'chocolate4': (139, 69, 19),
+ 'coral': (255, 127, 80),
+ 'coral1': (255, 114, 86),
+ 'coral2': (238, 106, 80),
+ 'coral3': (205, 91, 69),
+ 'coral4': (139, 62, 47),
+ 'cornflower blue': (100, 149, 237),
+ 'cornflowerblue': (100, 149, 237),
+ 'cornsilk': (255, 248, 220),
+ 'cornsilk1': (255, 248, 220),
+ 'cornsilk2': (238, 232, 205),
+ 'cornsilk3': (205, 200, 177),
+ 'cornsilk4': (139, 136, 120),
+ 'cyan': (0, 255, 255),
+ 'cyan1': (0, 255, 255),
+ 'cyan2': (0, 238, 238),
+ 'cyan3': (0, 205, 205),
+ 'cyan4': (0, 139, 139),
+ 'dark blue': (0, 0, 139),
+ 'dark cyan': (0, 139, 139),
+ 'dark goldenrod': (184, 134, 11),
+ 'dark gray': (169, 169, 169),
+ 'dark green': (0, 100, 0),
+ 'dark grey': (169, 169, 169),
+ 'dark khaki': (189, 183, 107),
+ 'dark magenta': (139, 0, 139),
+ 'dark olive green': (85, 107, 47),
+ 'dark orange': (255, 140, 0),
+ 'dark orchid': (153, 50, 204),
+ 'dark red': (139, 0, 0),
+ 'dark salmon': (233, 150, 122),
+ 'dark sea green': (143, 188, 143),
+ 'dark slate blue': (72, 61, 139),
+ 'dark slate gray': (47, 79, 79),
+ 'dark slate grey': (47, 79, 79),
+ 'dark turquoise': (0, 206, 209),
+ 'dark violet': (148, 0, 211),
+ 'darkblue': (0, 0, 139),
+ 'darkcyan': (0, 139, 139),
+ 'darkgoldenrod': (184, 134, 11),
+ 'darkgoldenrod1': (255, 185, 15),
+ 'darkgoldenrod2': (238, 173, 14),
+ 'darkgoldenrod3': (205, 149, 12),
+ 'darkgoldenrod4': (139, 101, 8),
+ 'darkgray': (169, 169, 169),
+ 'darkgreen': (0, 100, 0),
+ 'darkgrey': (169, 169, 169),
+ 'darkkhaki': (189, 183, 107),
+ 'darkmagenta': (139, 0, 139),
+ 'darkolivegreen': (85, 107, 47),
+ 'darkolivegreen1': (202, 255, 112),
+ 'darkolivegreen2': (188, 238, 104),
+ 'darkolivegreen3': (162, 205, 90),
+ 'darkolivegreen4': (110, 139, 61),
+ 'darkorange': (255, 140, 0),
+ 'darkorange1': (255, 127, 0),
+ 'darkorange2': (238, 118, 0),
+ 'darkorange3': (205, 102, 0),
+ 'darkorange4': (139, 69, 0),
+ 'darkorchid': (153, 50, 204),
+ 'darkorchid1': (191, 62, 255),
+ 'darkorchid2': (178, 58, 238),
+ 'darkorchid3': (154, 50, 205),
+ 'darkorchid4': (104, 34, 139),
+ 'darkred': (139, 0, 0),
+ 'darksalmon': (233, 150, 122),
+ 'darkseagreen': (143, 188, 143),
+ 'darkseagreen1': (193, 255, 193),
+ 'darkseagreen2': (180, 238, 180),
+ 'darkseagreen3': (155, 205, 155),
+ 'darkseagreen4': (105, 139, 105),
+ 'darkslateblue': (72, 61, 139),
+ 'darkslategray': (47, 79, 79),
+ 'darkslategray1': (151, 255, 255),
+ 'darkslategray2': (141, 238, 238),
+ 'darkslategray3': (121, 205, 205),
+ 'darkslategray4': (82, 139, 139),
+ 'darkslategrey': (47, 79, 79),
+ 'darkturquoise': (0, 206, 209),
+ 'darkviolet': (148, 0, 211),
+ 'deep pink': (255, 20, 147),
+ 'deep sky blue': (0, 191, 255),
+ 'deeppink': (255, 20, 147),
+ 'deeppink1': (255, 20, 147),
+ 'deeppink2': (238, 18, 137),
+ 'deeppink3': (205, 16, 118),
+ 'deeppink4': (139, 10, 80),
+ 'deepskyblue': (0, 191, 255),
+ 'deepskyblue1': (0, 191, 255),
+ 'deepskyblue2': (0, 178, 238),
+ 'deepskyblue3': (0, 154, 205),
+ 'deepskyblue4': (0, 104, 139),
+ 'dim gray': (105, 105, 105),
+ 'dim grey': (105, 105, 105),
+ 'dimgray': (105, 105, 105),
+ 'dimgrey': (105, 105, 105),
+ 'dodger blue': (30, 144, 255),
+ 'dodgerblue': (30, 144, 255),
+ 'dodgerblue1': (30, 144, 255),
+ 'dodgerblue2': (28, 134, 238),
+ 'dodgerblue3': (24, 116, 205),
+ 'dodgerblue4': (16, 78, 139),
+ 'firebrick': (178, 34, 34),
+ 'firebrick1': (255, 48, 48),
+ 'firebrick2': (238, 44, 44),
+ 'firebrick3': (205, 38, 38),
+ 'firebrick4': (139, 26, 26),
+ 'floral white': (255, 250, 240),
+ 'floralwhite': (255, 250, 240),
+ 'forest green': (34, 139, 34),
+ 'forestgreen': (34, 139, 34),
+ 'gainsboro': (220, 220, 220),
+ 'ghost white': (248, 248, 255),
+ 'ghostwhite': (248, 248, 255),
+ 'gold': (255, 215, 0),
+ 'gold1': (255, 215, 0),
+ 'gold2': (238, 201, 0),
+ 'gold3': (205, 173, 0),
+ 'gold4': (139, 117, 0),
+ 'goldenrod': (218, 165, 32),
+ 'goldenrod1': (255, 193, 37),
+ 'goldenrod2': (238, 180, 34),
+ 'goldenrod3': (205, 155, 29),
+ 'goldenrod4': (139, 105, 20),
+ 'gray': (190, 190, 190),
+ 'gray0': (0, 0, 0),
+ 'gray1': (3, 3, 3),
+ 'gray10': (26, 26, 26),
+ 'gray100': (255, 255, 255),
+ 'gray11': (28, 28, 28),
+ 'gray12': (31, 31, 31),
+ 'gray13': (33, 33, 33),
+ 'gray14': (36, 36, 36),
+ 'gray15': (38, 38, 38),
+ 'gray16': (41, 41, 41),
+ 'gray17': (43, 43, 43),
+ 'gray18': (46, 46, 46),
+ 'gray19': (48, 48, 48),
+ 'gray2': (5, 5, 5),
+ 'gray20': (51, 51, 51),
+ 'gray21': (54, 54, 54),
+ 'gray22': (56, 56, 56),
+ 'gray23': (59, 59, 59),
+ 'gray24': (61, 61, 61),
+ 'gray25': (64, 64, 64),
+ 'gray26': (66, 66, 66),
+ 'gray27': (69, 69, 69),
+ 'gray28': (71, 71, 71),
+ 'gray29': (74, 74, 74),
+ 'gray3': (8, 8, 8),
+ 'gray30': (77, 77, 77),
+ 'gray31': (79, 79, 79),
+ 'gray32': (82, 82, 82),
+ 'gray33': (84, 84, 84),
+ 'gray34': (87, 87, 87),
+ 'gray35': (89, 89, 89),
+ 'gray36': (92, 92, 92),
+ 'gray37': (94, 94, 94),
+ 'gray38': (97, 97, 97),
+ 'gray39': (99, 99, 99),
+ 'gray4': (10, 10, 10),
+ 'gray40': (102, 102, 102),
+ 'gray41': (105, 105, 105),
+ 'gray42': (107, 107, 107),
+ 'gray43': (110, 110, 110),
+ 'gray44': (112, 112, 112),
+ 'gray45': (115, 115, 115),
+ 'gray46': (117, 117, 117),
+ 'gray47': (120, 120, 120),
+ 'gray48': (122, 122, 122),
+ 'gray49': (125, 125, 125),
+ 'gray5': (13, 13, 13),
+ 'gray50': (127, 127, 127),
+ 'gray51': (130, 130, 130),
+ 'gray52': (133, 133, 133),
+ 'gray53': (135, 135, 135),
+ 'gray54': (138, 138, 138),
+ 'gray55': (140, 140, 140),
+ 'gray56': (143, 143, 143),
+ 'gray57': (145, 145, 145),
+ 'gray58': (148, 148, 148),
+ 'gray59': (150, 150, 150),
+ 'gray6': (15, 15, 15),
+ 'gray60': (153, 153, 153),
+ 'gray61': (156, 156, 156),
+ 'gray62': (158, 158, 158),
+ 'gray63': (161, 161, 161),
+ 'gray64': (163, 163, 163),
+ 'gray65': (166, 166, 166),
+ 'gray66': (168, 168, 168),
+ 'gray67': (171, 171, 171),
+ 'gray68': (173, 173, 173),
+ 'gray69': (176, 176, 176),
+ 'gray7': (18, 18, 18),
+ 'gray70': (179, 179, 179),
+ 'gray71': (181, 181, 181),
+ 'gray72': (184, 184, 184),
+ 'gray73': (186, 186, 186),
+ 'gray74': (189, 189, 189),
+ 'gray75': (191, 191, 191),
+ 'gray76': (194, 194, 194),
+ 'gray77': (196, 196, 196),
+ 'gray78': (199, 199, 199),
+ 'gray79': (201, 201, 201),
+ 'gray8': (20, 20, 20),
+ 'gray80': (204, 204, 204),
+ 'gray81': (207, 207, 207),
+ 'gray82': (209, 209, 209),
+ 'gray83': (212, 212, 212),
+ 'gray84': (214, 214, 214),
+ 'gray85': (217, 217, 217),
+ 'gray86': (219, 219, 219),
+ 'gray87': (222, 222, 222),
+ 'gray88': (224, 224, 224),
+ 'gray89': (227, 227, 227),
+ 'gray9': (23, 23, 23),
+ 'gray90': (229, 229, 229),
+ 'gray91': (232, 232, 232),
+ 'gray92': (235, 235, 235),
+ 'gray93': (237, 237, 237),
+ 'gray94': (240, 240, 240),
+ 'gray95': (242, 242, 242),
+ 'gray96': (245, 245, 245),
+ 'gray97': (247, 247, 247),
+ 'gray98': (250, 250, 250),
+ 'gray99': (252, 252, 252),
+ 'green': (0, 255, 0),
+ 'green yellow': (173, 255, 47),
+ 'green1': (0, 255, 0),
+ 'green2': (0, 238, 0),
+ 'green3': (0, 205, 0),
+ 'green4': (0, 139, 0),
+ 'greenyellow': (173, 255, 47),
+ 'grey': (190, 190, 190),
+ 'grey0': (0, 0, 0),
+ 'grey1': (3, 3, 3),
+ 'grey10': (26, 26, 26),
+ 'grey100': (255, 255, 255),
+ 'grey11': (28, 28, 28),
+ 'grey12': (31, 31, 31),
+ 'grey13': (33, 33, 33),
+ 'grey14': (36, 36, 36),
+ 'grey15': (38, 38, 38),
+ 'grey16': (41, 41, 41),
+ 'grey17': (43, 43, 43),
+ 'grey18': (46, 46, 46),
+ 'grey19': (48, 48, 48),
+ 'grey2': (5, 5, 5),
+ 'grey20': (51, 51, 51),
+ 'grey21': (54, 54, 54),
+ 'grey22': (56, 56, 56),
+ 'grey23': (59, 59, 59),
+ 'grey24': (61, 61, 61),
+ 'grey25': (64, 64, 64),
+ 'grey26': (66, 66, 66),
+ 'grey27': (69, 69, 69),
+ 'grey28': (71, 71, 71),
+ 'grey29': (74, 74, 74),
+ 'grey3': (8, 8, 8),
+ 'grey30': (77, 77, 77),
+ 'grey31': (79, 79, 79),
+ 'grey32': (82, 82, 82),
+ 'grey33': (84, 84, 84),
+ 'grey34': (87, 87, 87),
+ 'grey35': (89, 89, 89),
+ 'grey36': (92, 92, 92),
+ 'grey37': (94, 94, 94),
+ 'grey38': (97, 97, 97),
+ 'grey39': (99, 99, 99),
+ 'grey4': (10, 10, 10),
+ 'grey40': (102, 102, 102),
+ 'grey41': (105, 105, 105),
+ 'grey42': (107, 107, 107),
+ 'grey43': (110, 110, 110),
+ 'grey44': (112, 112, 112),
+ 'grey45': (115, 115, 115),
+ 'grey46': (117, 117, 117),
+ 'grey47': (120, 120, 120),
+ 'grey48': (122, 122, 122),
+ 'grey49': (125, 125, 125),
+ 'grey5': (13, 13, 13),
+ 'grey50': (127, 127, 127),
+ 'grey51': (130, 130, 130),
+ 'grey52': (133, 133, 133),
+ 'grey53': (135, 135, 135),
+ 'grey54': (138, 138, 138),
+ 'grey55': (140, 140, 140),
+ 'grey56': (143, 143, 143),
+ 'grey57': (145, 145, 145),
+ 'grey58': (148, 148, 148),
+ 'grey59': (150, 150, 150),
+ 'grey6': (15, 15, 15),
+ 'grey60': (153, 153, 153),
+ 'grey61': (156, 156, 156),
+ 'grey62': (158, 158, 158),
+ 'grey63': (161, 161, 161),
+ 'grey64': (163, 163, 163),
+ 'grey65': (166, 166, 166),
+ 'grey66': (168, 168, 168),
+ 'grey67': (171, 171, 171),
+ 'grey68': (173, 173, 173),
+ 'grey69': (176, 176, 176),
+ 'grey7': (18, 18, 18),
+ 'grey70': (179, 179, 179),
+ 'grey71': (181, 181, 181),
+ 'grey72': (184, 184, 184),
+ 'grey73': (186, 186, 186),
+ 'grey74': (189, 189, 189),
+ 'grey75': (191, 191, 191),
+ 'grey76': (194, 194, 194),
+ 'grey77': (196, 196, 196),
+ 'grey78': (199, 199, 199),
+ 'grey79': (201, 201, 201),
+ 'grey8': (20, 20, 20),
+ 'grey80': (204, 204, 204),
+ 'grey81': (207, 207, 207),
+ 'grey82': (209, 209, 209),
+ 'grey83': (212, 212, 212),
+ 'grey84': (214, 214, 214),
+ 'grey85': (217, 217, 217),
+ 'grey86': (219, 219, 219),
+ 'grey87': (222, 222, 222),
+ 'grey88': (224, 224, 224),
+ 'grey89': (227, 227, 227),
+ 'grey9': (23, 23, 23),
+ 'grey90': (229, 229, 229),
+ 'grey91': (232, 232, 232),
+ 'grey92': (235, 235, 235),
+ 'grey93': (237, 237, 237),
+ 'grey94': (240, 240, 240),
+ 'grey95': (242, 242, 242),
+ 'grey96': (245, 245, 245),
+ 'grey97': (247, 247, 247),
+ 'grey98': (250, 250, 250),
+ 'grey99': (252, 252, 252),
+ 'honeydew': (240, 255, 240),
+ 'honeydew1': (240, 255, 240),
+ 'honeydew2': (224, 238, 224),
+ 'honeydew3': (193, 205, 193),
+ 'honeydew4': (131, 139, 131),
+ 'hot pink': (255, 105, 180),
+ 'hotpink': (255, 105, 180),
+ 'hotpink1': (255, 110, 180),
+ 'hotpink2': (238, 106, 167),
+ 'hotpink3': (205, 96, 144),
+ 'hotpink4': (139, 58, 98),
+ 'indian red': (205, 92, 92),
+ 'indianred': (205, 92, 92),
+ 'indianred1': (255, 106, 106),
+ 'indianred2': (238, 99, 99),
+ 'indianred3': (205, 85, 85),
+ 'indianred4': (139, 58, 58),
+ 'ivory': (255, 255, 240),
+ 'ivory1': (255, 255, 240),
+ 'ivory2': (238, 238, 224),
+ 'ivory3': (205, 205, 193),
+ 'ivory4': (139, 139, 131),
+ 'khaki': (240, 230, 140),
+ 'khaki1': (255, 246, 143),
+ 'khaki2': (238, 230, 133),
+ 'khaki3': (205, 198, 115),
+ 'khaki4': (139, 134, 78),
+ 'lavender': (230, 230, 250),
+ 'lavender blush': (255, 240, 245),
+ 'lavenderblush': (255, 240, 245),
+ 'lavenderblush1': (255, 240, 245),
+ 'lavenderblush2': (238, 224, 229),
+ 'lavenderblush3': (205, 193, 197),
+ 'lavenderblush4': (139, 131, 134),
+ 'lawn green': (124, 252, 0),
+ 'lawngreen': (124, 252, 0),
+ 'lemon chiffon': (255, 250, 205),
+ 'lemonchiffon': (255, 250, 205),
+ 'lemonchiffon1': (255, 250, 205),
+ 'lemonchiffon2': (238, 233, 191),
+ 'lemonchiffon3': (205, 201, 165),
+ 'lemonchiffon4': (139, 137, 112),
+ 'light blue': (173, 216, 230),
+ 'light coral': (240, 128, 128),
+ 'light cyan': (224, 255, 255),
+ 'light goldenrod': (238, 221, 130),
+ 'light goldenrod yellow': (250, 250, 210),
+ 'light gray': (211, 211, 211),
+ 'light green': (144, 238, 144),
+ 'light grey': (211, 211, 211),
+ 'light pink': (255, 182, 193),
+ 'light salmon': (255, 160, 122),
+ 'light sea green': (32, 178, 170),
+ 'light sky blue': (135, 206, 250),
+ 'light slate blue': (132, 112, 255),
+ 'light slate gray': (119, 136, 153),
+ 'light slate grey': (119, 136, 153),
+ 'light steel blue': (176, 196, 222),
+ 'light yellow': (255, 255, 224),
+ 'lightblue': (173, 216, 230),
+ 'lightblue1': (191, 239, 255),
+ 'lightblue2': (178, 223, 238),
+ 'lightblue3': (154, 192, 205),
+ 'lightblue4': (104, 131, 139),
+ 'lightcoral': (240, 128, 128),
+ 'lightcyan': (224, 255, 255),
+ 'lightcyan1': (224, 255, 255),
+ 'lightcyan2': (209, 238, 238),
+ 'lightcyan3': (180, 205, 205),
+ 'lightcyan4': (122, 139, 139),
+ 'lightgoldenrod': (238, 221, 130),
+ 'lightgoldenrod1': (255, 236, 139),
+ 'lightgoldenrod2': (238, 220, 130),
+ 'lightgoldenrod3': (205, 190, 112),
+ 'lightgoldenrod4': (139, 129, 76),
+ 'lightgoldenrodyellow': (250, 250, 210),
+ 'lightgray': (211, 211, 211),
+ 'lightgreen': (144, 238, 144),
+ 'lightgrey': (211, 211, 211),
+ 'lightpink': (255, 182, 193),
+ 'lightpink1': (255, 174, 185),
+ 'lightpink2': (238, 162, 173),
+ 'lightpink3': (205, 140, 149),
+ 'lightpink4': (139, 95, 101),
+ 'lightsalmon': (255, 160, 122),
+ 'lightsalmon1': (255, 160, 122),
+ 'lightsalmon2': (238, 149, 114),
+ 'lightsalmon3': (205, 129, 98),
+ 'lightsalmon4': (139, 87, 66),
+ 'lightseagreen': (32, 178, 170),
+ 'lightskyblue': (135, 206, 250),
+ 'lightskyblue1': (176, 226, 255),
+ 'lightskyblue2': (164, 211, 238),
+ 'lightskyblue3': (141, 182, 205),
+ 'lightskyblue4': (96, 123, 139),
+ 'lightslateblue': (132, 112, 255),
+ 'lightslategray': (119, 136, 153),
+ 'lightslategrey': (119, 136, 153),
+ 'lightsteelblue': (176, 196, 222),
+ 'lightsteelblue1': (202, 225, 255),
+ 'lightsteelblue2': (188, 210, 238),
+ 'lightsteelblue3': (162, 181, 205),
+ 'lightsteelblue4': (110, 123, 139),
+ 'lightyellow': (255, 255, 224),
+ 'lightyellow1': (255, 255, 224),
+ 'lightyellow2': (238, 238, 209),
+ 'lightyellow3': (205, 205, 180),
+ 'lightyellow4': (139, 139, 122),
+ 'lime green': (50, 205, 50),
+ 'limegreen': (50, 205, 50),
+ 'linen': (250, 240, 230),
+ 'magenta': (255, 0, 255),
+ 'magenta1': (255, 0, 255),
+ 'magenta2': (238, 0, 238),
+ 'magenta3': (205, 0, 205),
+ 'magenta4': (139, 0, 139),
+ 'maroon': (176, 48, 96),
+ 'maroon1': (255, 52, 179),
+ 'maroon2': (238, 48, 167),
+ 'maroon3': (205, 41, 144),
+ 'maroon4': (139, 28, 98),
+ 'medium aquamarine': (102, 205, 170),
+ 'medium blue': (0, 0, 205),
+ 'medium orchid': (186, 85, 211),
+ 'medium purple': (147, 112, 219),
+ 'medium sea green': (60, 179, 113),
+ 'medium slate blue': (123, 104, 238),
+ 'medium spring green': (0, 250, 154),
+ 'medium turquoise': (72, 209, 204),
+ 'medium violet red': (199, 21, 133),
+ 'mediumaquamarine': (102, 205, 170),
+ 'mediumblue': (0, 0, 205),
+ 'mediumorchid': (186, 85, 211),
+ 'mediumorchid1': (224, 102, 255),
+ 'mediumorchid2': (209, 95, 238),
+ 'mediumorchid3': (180, 82, 205),
+ 'mediumorchid4': (122, 55, 139),
+ 'mediumpurple': (147, 112, 219),
+ 'mediumpurple1': (171, 130, 255),
+ 'mediumpurple2': (159, 121, 238),
+ 'mediumpurple3': (137, 104, 205),
+ 'mediumpurple4': (93, 71, 139),
+ 'mediumseagreen': (60, 179, 113),
+ 'mediumslateblue': (123, 104, 238),
+ 'mediumspringgreen': (0, 250, 154),
+ 'mediumturquoise': (72, 209, 204),
+ 'mediumvioletred': (199, 21, 133),
+ 'midnight blue': (25, 25, 112),
+ 'midnightblue': (25, 25, 112),
+ 'mint cream': (245, 255, 250),
+ 'mintcream': (245, 255, 250),
+ 'misty rose': (255, 228, 225),
+ 'mistyrose': (255, 228, 225),
+ 'mistyrose1': (255, 228, 225),
+ 'mistyrose2': (238, 213, 210),
+ 'mistyrose3': (205, 183, 181),
+ 'mistyrose4': (139, 125, 123),
+ 'moccasin': (255, 228, 181),
+ 'navajo white': (255, 222, 173),
+ 'navajowhite': (255, 222, 173),
+ 'navajowhite1': (255, 222, 173),
+ 'navajowhite2': (238, 207, 161),
+ 'navajowhite3': (205, 179, 139),
+ 'navajowhite4': (139, 121, 94),
+ 'navy': (0, 0, 128),
+ 'navy blue': (0, 0, 128),
+ 'navyblue': (0, 0, 128),
+ 'old lace': (253, 245, 230),
+ 'oldlace': (253, 245, 230),
+ 'olive drab': (107, 142, 35),
+ 'olivedrab': (107, 142, 35),
+ 'olivedrab1': (192, 255, 62),
+ 'olivedrab2': (179, 238, 58),
+ 'olivedrab3': (154, 205, 50),
+ 'olivedrab4': (105, 139, 34),
+ 'orange': (255, 165, 0),
+ 'orange red': (255, 69, 0),
+ 'orange1': (255, 165, 0),
+ 'orange2': (238, 154, 0),
+ 'orange3': (205, 133, 0),
+ 'orange4': (139, 90, 0),
+ 'orangered': (255, 69, 0),
+ 'orangered1': (255, 69, 0),
+ 'orangered2': (238, 64, 0),
+ 'orangered3': (205, 55, 0),
+ 'orangered4': (139, 37, 0),
+ 'orchid': (218, 112, 214),
+ 'orchid1': (255, 131, 250),
+ 'orchid2': (238, 122, 233),
+ 'orchid3': (205, 105, 201),
+ 'orchid4': (139, 71, 137),
+ 'pale goldenrod': (238, 232, 170),
+ 'pale green': (152, 251, 152),
+ 'pale turquoise': (175, 238, 238),
+ 'pale violet red': (219, 112, 147),
+ 'palegoldenrod': (238, 232, 170),
+ 'palegreen': (152, 251, 152),
+ 'palegreen1': (154, 255, 154),
+ 'palegreen2': (144, 238, 144),
+ 'palegreen3': (124, 205, 124),
+ 'palegreen4': (84, 139, 84),
+ 'paleturquoise': (175, 238, 238),
+ 'paleturquoise1': (187, 255, 255),
+ 'paleturquoise2': (174, 238, 238),
+ 'paleturquoise3': (150, 205, 205),
+ 'paleturquoise4': (102, 139, 139),
+ 'palevioletred': (219, 112, 147),
+ 'palevioletred1': (255, 130, 171),
+ 'palevioletred2': (238, 121, 159),
+ 'palevioletred3': (205, 104, 127),
+ 'palevioletred4': (139, 71, 93),
+ 'papaya whip': (255, 239, 213),
+ 'papayawhip': (255, 239, 213),
+ 'peach puff': (255, 218, 185),
+ 'peachpuff': (255, 218, 185),
+ 'peachpuff1': (255, 218, 185),
+ 'peachpuff2': (238, 203, 173),
+ 'peachpuff3': (205, 175, 149),
+ 'peachpuff4': (139, 119, 101),
+ 'peru': (205, 133, 63),
+ 'pink': (255, 192, 203),
+ 'pink1': (255, 181, 197),
+ 'pink2': (238, 169, 184),
+ 'pink3': (205, 145, 158),
+ 'pink4': (139, 99, 108),
+ 'plum': (221, 160, 221),
+ 'plum1': (255, 187, 255),
+ 'plum2': (238, 174, 238),
+ 'plum3': (205, 150, 205),
+ 'plum4': (139, 102, 139),
+ 'powder blue': (176, 224, 230),
+ 'powderblue': (176, 224, 230),
+ 'purple': (160, 32, 240),
+ 'purple1': (155, 48, 255),
+ 'purple2': (145, 44, 238),
+ 'purple3': (125, 38, 205),
+ 'purple4': (85, 26, 139),
+ 'red': (255, 0, 0),
+ 'red1': (255, 0, 0),
+ 'red2': (238, 0, 0),
+ 'red3': (205, 0, 0),
+ 'red4': (139, 0, 0),
+ 'rosy brown': (188, 143, 143),
+ 'rosybrown': (188, 143, 143),
+ 'rosybrown1': (255, 193, 193),
+ 'rosybrown2': (238, 180, 180),
+ 'rosybrown3': (205, 155, 155),
+ 'rosybrown4': (139, 105, 105),
+ 'royal blue': (65, 105, 225),
+ 'royalblue': (65, 105, 225),
+ 'royalblue1': (72, 118, 255),
+ 'royalblue2': (67, 110, 238),
+ 'royalblue3': (58, 95, 205),
+ 'royalblue4': (39, 64, 139),
+ 'saddle brown': (139, 69, 19),
+ 'saddlebrown': (139, 69, 19),
+ 'salmon': (250, 128, 114),
+ 'salmon1': (255, 140, 105),
+ 'salmon2': (238, 130, 98),
+ 'salmon3': (205, 112, 84),
+ 'salmon4': (139, 76, 57),
+ 'sandy brown': (244, 164, 96),
+ 'sandybrown': (244, 164, 96),
+ 'sea green': (46, 139, 87),
+ 'seagreen': (46, 139, 87),
+ 'seagreen1': (84, 255, 159),
+ 'seagreen2': (78, 238, 148),
+ 'seagreen3': (67, 205, 128),
+ 'seagreen4': (46, 139, 87),
+ 'seashell': (255, 245, 238),
+ 'seashell1': (255, 245, 238),
+ 'seashell2': (238, 229, 222),
+ 'seashell3': (205, 197, 191),
+ 'seashell4': (139, 134, 130),
+ 'sienna': (160, 82, 45),
+ 'sienna1': (255, 130, 71),
+ 'sienna2': (238, 121, 66),
+ 'sienna3': (205, 104, 57),
+ 'sienna4': (139, 71, 38),
+ 'sky blue': (135, 206, 235),
+ 'skyblue': (135, 206, 235),
+ 'skyblue1': (135, 206, 255),
+ 'skyblue2': (126, 192, 238),
+ 'skyblue3': (108, 166, 205),
+ 'skyblue4': (74, 112, 139),
+ 'slate blue': (106, 90, 205),
+ 'slate gray': (112, 128, 144),
+ 'slate grey': (112, 128, 144),
+ 'slateblue': (106, 90, 205),
+ 'slateblue1': (131, 111, 255),
+ 'slateblue2': (122, 103, 238),
+ 'slateblue3': (105, 89, 205),
+ 'slateblue4': (71, 60, 139),
+ 'slategray': (112, 128, 144),
+ 'slategray1': (198, 226, 255),
+ 'slategray2': (185, 211, 238),
+ 'slategray3': (159, 182, 205),
+ 'slategray4': (108, 123, 139),
+ 'slategrey': (112, 128, 144),
+ 'snow': (255, 250, 250),
+ 'snow1': (255, 250, 250),
+ 'snow2': (238, 233, 233),
+ 'snow3': (205, 201, 201),
+ 'snow4': (139, 137, 137),
+ 'spring green': (0, 255, 127),
+ 'springgreen': (0, 255, 127),
+ 'springgreen1': (0, 255, 127),
+ 'springgreen2': (0, 238, 118),
+ 'springgreen3': (0, 205, 102),
+ 'springgreen4': (0, 139, 69),
+ 'steel blue': (70, 130, 180),
+ 'steelblue': (70, 130, 180),
+ 'steelblue1': (99, 184, 255),
+ 'steelblue2': (92, 172, 238),
+ 'steelblue3': (79, 148, 205),
+ 'steelblue4': (54, 100, 139),
+ 'tan': (210, 180, 140),
+ 'tan1': (255, 165, 79),
+ 'tan2': (238, 154, 73),
+ 'tan3': (205, 133, 63),
+ 'tan4': (139, 90, 43),
+ 'thistle': (216, 191, 216),
+ 'thistle1': (255, 225, 255),
+ 'thistle2': (238, 210, 238),
+ 'thistle3': (205, 181, 205),
+ 'thistle4': (139, 123, 139),
+ 'tomato': (255, 99, 71),
+ 'tomato1': (255, 99, 71),
+ 'tomato2': (238, 92, 66),
+ 'tomato3': (205, 79, 57),
+ 'tomato4': (139, 54, 38),
+ 'turquoise': (64, 224, 208),
+ 'turquoise1': (0, 245, 255),
+ 'turquoise2': (0, 229, 238),
+ 'turquoise3': (0, 197, 205),
+ 'turquoise4': (0, 134, 139),
+ 'violet': (238, 130, 238),
+ 'violet red': (208, 32, 144),
+ 'violetred': (208, 32, 144),
+ 'violetred1': (255, 62, 150),
+ 'violetred2': (238, 58, 140),
+ 'violetred3': (205, 50, 120),
+ 'violetred4': (139, 34, 82),
+ 'wheat': (245, 222, 179),
+ 'wheat1': (255, 231, 186),
+ 'wheat2': (238, 216, 174),
+ 'wheat3': (205, 186, 150),
+ 'wheat4': (139, 126, 102),
+ 'white': (255, 255, 255),
+ 'white smoke': (245, 245, 245),
+ 'whitesmoke': (245, 245, 245),
+ 'yellow': (255, 255, 0),
+ 'yellow green': (154, 205, 50),
+ 'yellow1': (255, 255, 0),
+ 'yellow2': (238, 238, 0),
+ 'yellow3': (205, 205, 0),
+ 'yellow4': (139, 139, 0),
+ 'yellowgreen': (154, 205, 50),
+}
+
+class ColorDB:
+
+ def __init__(self):
+ self.colordata = data
+
+ def byname(self, name):
+ """ Find color by name. """
+ try:
+ return self.colordata[name]
+ except KeyError:
+ pass
+ try:
+ return self.colordata[name.lower()]
+ except KeyError:
+ pass
+ raise KeyError, name
+
+ def blend(self, color1, color2):
+ """ "Blend" two colors, i.e. take their average RGB values. """
+ if isinstance(color1, str):
+ color1 = self.byname(color1)
+ if isinstance(color2, str):
+ color2 = self.byname(color2)
+ return tuple([(v1+v2)/2 for (v1, v2) in zip(color1, color2)])
+
+ def blend_weighed(self, color1, weight1, color2, weight2):
+ """ Blend two colors, using a weighing factor. """
+ if isinstance(color1, str):
+ color1 = self.byname(color1)
+ if isinstance(color2, str):
+ color2 = self.byname(color2)
+ total_weight = weight1 + weight2
+ return tuple([(v1*weight1 + v2*weight2) / total_weight
+ for (v1, v2) in zip(color1, color2)])
+
+ def invert(self, color):
+ if isinstance(color, str):
+ color = self.byname(str)
+ return tuple([255 - v for v in color])
+
+ def findcolors(self, color, numresults=5):
+ """ Find the topmost <numresults> colors in the color database that
+ are closest to <color>. Useful for finding the name of a color.
+ <color> is a tuple (r, g, b).
+
+ Returns a list of tuples (distance, name, (r, g, b).
+ """
+ if isinstance(color, str):
+ color = self.byname(color)
+
+ def sqr(x): return x*x
+ def distance(color1, color2):
+ squares = [sqr(abs(v1-v2)) for (v1, v2) in zip(color1, color2)]
+ sum = reduce(operator.add, squares)
+ return math.sqrt(sum)
+
+ results = {}
+ for (name, acolor) in self.colordata.items():
+ dist = distance(color, acolor)
+ results[dist] = (dist, name, acolor)
+
+ results = results.values()
+ results.sort()
+ return results[:numresults]
+
+ def __getitem__(self, name):
+ return self.byname(name)
+
+ def hex(self, name):
+ rgb = self.byname(name)
+ return "%02x%02x%02x" % rgb
+
+_colordb = ColorDB()
diff --git a/pynxc/waxy/colourdialog.py b/pynxc/waxy/colourdialog.py
new file mode 100644
index 0000000..5ccd797
--- /dev/null
+++ b/pynxc/waxy/colourdialog.py
@@ -0,0 +1,34 @@
+import wx
+import waxyobject
+from font import Font
+
+class ColourDialog(wx.ColourDialog, waxyobject.WaxyObject):
+
+ def __init__(self, parent):
+ wx.ColourDialog.__init__(self, parent)
+ # Ensure the full colour dialog is displayed,
+ # not the abbreviated version.
+ self.GetColourData().SetChooseFull(True)
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.ColourDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+ def GetChosenColour(self):
+ """ Shorthand... """
+ data = self.GetColourData()
+ color = data.GetColour().Get()
+ return color
+
+class ColorDialog(ColourDialog):
+
+
+ def GetChosenColor(self):
+ """ Shorthand... """
+ data = self.GetColourData()
+ color = data.GetColour().Get()
+ return color
diff --git a/pynxc/waxy/colourdialog.py~ b/pynxc/waxy/colourdialog.py~
new file mode 100644
index 0000000..5ccd797
--- /dev/null
+++ b/pynxc/waxy/colourdialog.py~
@@ -0,0 +1,34 @@
+import wx
+import waxyobject
+from font import Font
+
+class ColourDialog(wx.ColourDialog, waxyobject.WaxyObject):
+
+ def __init__(self, parent):
+ wx.ColourDialog.__init__(self, parent)
+ # Ensure the full colour dialog is displayed,
+ # not the abbreviated version.
+ self.GetColourData().SetChooseFull(True)
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.ColourDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+ def GetChosenColour(self):
+ """ Shorthand... """
+ data = self.GetColourData()
+ color = data.GetColour().Get()
+ return color
+
+class ColorDialog(ColourDialog):
+
+
+ def GetChosenColor(self):
+ """ Shorthand... """
+ data = self.GetColourData()
+ color = data.GetColour().Get()
+ return color
diff --git a/pynxc/waxy/combobox.py b/pynxc/waxy/combobox.py
new file mode 100644
index 0000000..a39cff3
--- /dev/null
+++ b/pynxc/waxy/combobox.py
@@ -0,0 +1,50 @@
+# combobox.py
+
+import wx
+import containers
+import styles
+import waxyobject
+
+class ComboBox(wx.ComboBox, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Select': wx.EVT_COMBOBOX,
+ 'TextChanged': wx.EVT_TEXT,
+ }
+
+ def __init__(self, parent, choices=[], size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.ComboBox.__init__(self, parent, wx.NewId(), "",
+ size=size or (-1,-1), choices=choices, style=style)
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ def SetItems(self, items):
+ """ Clear the internal list of items, and set new items. <items> is
+ a list of 2-tuples (string, data). """
+ self.Clear()
+ for s, data in items:
+ self.Append(s, data)
+
+ def GetItems(self):
+ """ Return a list of 2-tuples (string, data). """
+ items = []
+ for i in range(self.GetCount()):
+ s = self.GetString(i)
+ data = self.GetClientData(i)
+ items.append((s, data))
+ return items
+
+ #
+ # style parameters
+
+ def _params(self, kwargs):
+ flags = wx.CB_DROPDOWN # default
+ flags |= styles.stylebool('simple', wx.CB_SIMPLE, kwargs)
+ flags |= styles.stylebool('readonly', wx.CB_READONLY, kwargs)
+ flags |= styles.stylebool('sort', wx.CB_SORT, kwargs)
+ return flags
diff --git a/pynxc/waxy/combobox.py~ b/pynxc/waxy/combobox.py~
new file mode 100644
index 0000000..853cdb4
--- /dev/null
+++ b/pynxc/waxy/combobox.py~
@@ -0,0 +1,51 @@
+# combobox.py
+
+import wx
+import containers
+import styles
+import waxyobject
+
+class ComboBox(wx.ComboBox, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Select': wx.EVT_COMBOBOX,
+ 'TextChanged': wx.EVT_TEXT,
+ }
+
+ def __init__(self, parent, choices=[], size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.ComboBox.__init__(self, parent, wx.NewId(), "",
+ size=size or (-1,-1), choices=choices, style=style)
+ self.SetDefaultFont()
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ def SetItems(self, items):
+ """ Clear the internal list of items, and set new items. <items> is
+ a list of 2-tuples (string, data). """
+ self.Clear()
+ for s, data in items:
+ self.Append(s, data)
+
+ def GetItems(self):
+ """ Return a list of 2-tuples (string, data). """
+ items = []
+ for i in range(self.GetCount()):
+ s = self.GetString(i)
+ data = self.GetClientData(i)
+ items.append((s, data))
+ return items
+
+ #
+ # style parameters
+
+ def _params(self, kwargs):
+ flags = wx.CB_DROPDOWN # default
+ flags |= styles.stylebool('simple', wx.CB_SIMPLE, kwargs)
+ flags |= styles.stylebool('readonly', wx.CB_READONLY, kwargs)
+ flags |= styles.stylebool('sort', wx.CB_SORT, kwargs)
+ return flags
diff --git a/pynxc/waxy/constants.py b/pynxc/waxy/constants.py
new file mode 100644
index 0000000..c2a448c
--- /dev/null
+++ b/pynxc/waxy/constants.py
@@ -0,0 +1,3 @@
+from wx import MEDIUM_GREY_PEN
+
+Medium_Grey_Pen=MEDIUM_GREY_PEN \ No newline at end of file
diff --git a/pynxc/waxy/containers.py b/pynxc/waxy/containers.py
new file mode 100644
index 0000000..b781ccc
--- /dev/null
+++ b/pynxc/waxy/containers.py
@@ -0,0 +1,311 @@
+# containers.py
+# Containers are mixin classes that have a sizer and a number of methods to
+# deal with that sizer (add components, etc). Used to implement fairly
+# "basic" controls like Panel and Frame. When in doubt, derive from those
+# two rather than making up your own container construct.
+
+import utils
+import waxyobject
+import wx
+import warnings
+
+class Container(waxyobject.WaxyObject):
+
+ def _create_sizer(self, direction):
+ if direction.lower().startswith("h"):
+ dir = wx.HORIZONTAL
+ elif direction.lower().startswith("v"):
+ dir = wx.VERTICAL
+ else:
+ raise ValueError, "Incorrect direction"
+ self.sizer = wx.BoxSizer(dir)
+ self._packed = 0
+
+ def Body(self):
+ """ Optionally override this to create components. Add them with the
+ AddComponent() method. """
+
+ def AddComponent(self, comp, expand=0, stretch=0, align=None, border=0):
+ # expand: expands a component in the direction of the panel/sizer
+ # stretch: expands a component in the other direction
+ # for example, if we have a horizontal panel, and resize the window
+ # both horizontally and vertically, then a component with 'expand'
+ # will expand horizontally, and one with 'stretch' will expand
+ # vertically. (See simplebuttons.py)
+
+ if isinstance(expand, str):
+ expand = expand.lower()
+ if expand.startswith("h"):
+ if self.GetOrientation() == 'horizontal':
+ expand, stretch = 1, 0
+ else:
+ expand, stretch = 0, 1
+ elif expand.startswith("v"):
+ if self.GetOrientation() == 'horizontal':
+ expand, stretch = 0, 1
+ else:
+ expand, stretch = 1, 0
+ elif expand.startswith("b"):
+ expand = stretch = 1
+ elif expand == '':
+ expand = stretch = 0
+ else:
+ raise ValueError, "Invalid value for expand: %r" % (expand,)
+
+ flags = 0
+ if stretch:
+ flags |= wx.EXPAND
+ if align:
+ flags |= {
+ "t": wx.ALIGN_TOP,
+ "c": wx.ALIGN_CENTER,
+ "b": wx.ALIGN_BOTTOM,
+ "r": wx.ALIGN_RIGHT,
+ "l": wx.ALIGN_LEFT,
+ }.get(align.lower()[:1], 0)
+ if border:
+ flags |= wx.ALL
+
+ flags |= wx.FIXED_MINSIZE
+ if 0 and hasattr(comp, 'sizer') and hasattr(comp, '_include_sizer'):
+ # ugly hack to add nested sizer rather than its frame
+ # only for certain quirky controls, like GroupBox!
+ self.sizer.Add(comp.sizer, expand, flags)
+ else:
+ self.sizer.Add(comp, expand, flags, border)
+ # comp can be a component or a tuple (width, height), but the
+ # Add() method is called just the same, as of wxPython 2.5.1.5
+
+ def AddSpace(self, width, height=None, *args, **kwargs):
+ if height is None:
+ height = width
+ return self.AddComponent((width, height), *args, **kwargs)
+ AddSeparator = AddSpace
+
+ def Pack(self):
+ if not self._packed:
+ self.SetSizerAndFit(self.sizer)
+ self._packed = 1
+
+ def Repack(self):
+ self.sizer.RecalcSizes()
+
+ def GetOrientation(self):
+ """ Return 'horizontal' or 'vertical'. """
+ orientation = self.sizer.GetOrientation()
+ if orientation == wx.HORIZONTAL:
+ return 'horizontal'
+ elif orientation == wx.VERTICAL:
+ return 'vertical'
+ else:
+ raise ValueError, "Unknown direction for sizer"
+
+#
+# GridContainer (used to implement GridPanel)
+
+class GridContainer(Container):
+
+ _sizerclass = wx.GridSizer
+
+ alignment = {
+ 'b': wx.ALIGN_BOTTOM,
+ 'r': wx.ALIGN_RIGHT,
+ 'l': wx.ALIGN_LEFT,
+ 'c': wx.ALIGN_CENTER,
+ 't': wx.ALIGN_TOP,
+ 'h': wx.ALIGN_CENTER_HORIZONTAL,
+ 'v': wx.ALIGN_CENTER_VERTICAL,
+ }
+
+ def _create_sizer(self, rows, cols, hgap=1, vgap=1):
+ self.sizer = self._sizerclass(rows, cols, vgap, hgap)
+ self.controls = {}
+ for row in range(rows):
+ for col in range(cols):
+ self.controls[col, row] = None
+ # (col, row) allows for (x, y)-like calling
+ self._packed = 0
+
+ def AddComponent(self, col, row, obj, expand=1, align='', border=0, stretch=0, proportion=0):
+ # make sure the parents are correct
+ if stretch:
+ warnings.warn("stretch is deprecated and has no effect here")
+
+ if self.controls[col, row]:
+ raise ValueError, "A control has already been set for position (%s, %s)" % (row, col)
+ self.controls[col, row] = {'obj': obj, 'expand': expand, 'align': align,
+ 'border': border, 'proportion': proportion}
+
+ def Pack(self):
+ if not self._packed:
+ controls = self._AllControls()
+ self.sizer.AddMany(controls)
+ self.SetSizerAndFit(self.sizer) # is this still necessary?
+ self._packed = 1
+
+ def __setitem__(self, index, value):
+ col, row = index
+ self.AddComponent(col, row, value)
+
+ def __getitem__(self, index):
+ col, row = index
+ return self.controls[col, row]['obj'] # may raise KeyError
+
+ def _AllControls(self):
+ controls = []
+ for row in range(self.sizer.GetRows()):
+ for col in range(self.sizer.GetCols()):
+ d = self.controls[col, row]
+ if d is None:
+ from panel import Panel
+ p = Panel(self) # hack up a dummy panel
+ controls.append((p, 0, wx.EXPAND|wx.FIXED_MINSIZE))
+ else:
+ obj = d['obj']
+
+ # set alignment
+ align = d['align'].lower()
+ alignment = 0
+ for key, value in self.__class__.alignment.items():
+ if key in align:
+ alignment |= value
+
+ z = d['expand'] and wx.EXPAND
+ z |= alignment
+ border = d['border']
+ #if border and not alignment:
+ if border:
+ z |= wx.ALL
+ z |= wx.FIXED_MINSIZE
+ proportion = d['proportion']
+ controls.append((obj, proportion, z, border))
+
+ return controls
+
+ # XXX maybe a Wax version of AddMany() would be useful?
+
+#
+# FlexGridContainer
+
+class FlexGridContainer(GridContainer):
+ _sizerclass = wx.FlexGridSizer
+
+ def AddGrowableRow(self, row):
+ self.sizer.AddGrowableRow(row)
+
+ def AddGrowableCol(self, col):
+ self.sizer.AddGrowableCol(col)
+
+#
+# OverlayContainer
+
+class OverlaySizer(wx.PySizer):
+ def __init__(self):
+ wx.PySizer.__init__(self)
+
+ def CalcMin(self):
+ maxx, maxy = 0, 0
+ for win in self.GetChildren():
+ x, y = win.CalcMin()
+ maxx = max(maxx, x)
+ maxy = max(maxy, y)
+ return wx.Size(maxx, maxy)
+
+ def RecalcSizes(self):
+ pos = self.GetPosition()
+ size = self.GetSize()
+ for win in self.GetChildren():
+ win.SetDimension(pos, size)
+
+
+class OverlayContainer(Container):
+ """ Container that takes an arbitrary number of windows, and stacks them
+ on top of each other. Controls are hidden by default. """
+
+ def _create_sizer(self):
+ self.sizer = OverlaySizer()
+ self.windows = []
+ self._packed = 0
+
+ def AddComponent(self, window, expand=0, stretch=0, align=None, border=0):
+ """
+ # make sure the parents are correct
+ if waxconfig.WaxConfig.check_parent:
+ if window.GetParent() is not self:
+ utils.parent_warning(window, self)
+
+ flags = 0
+ if stretch:
+ flags |= wx.EXPAND
+ if align:
+ flags |= {
+ "t": wx.ALIGN_TOP,
+ "c": wx.ALIGN_CENTER,
+ "b": wx.ALIGN_BOTTOM,
+ "r": wx.ALIGN_RIGHT,
+ "l": wx.ALIGN_LEFT,
+ }.get(align.lower()[:1], 0)
+ if border:
+ flags |= wx.ALL
+ flags |= wx.FIXED_MINSIZE
+
+ self.sizer.Add(window, expand, flags, border)
+ """
+ Container.AddComponent(self, window, expand, stretch, align, border)
+ self.windows.append(window)
+ window.Hide() # default is hiding
+
+
+
+#
+# GroupBoxContainer
+
+class GroupBoxContainer(Container):
+
+ def _create_sizer(self, groupbox, direction='h'):
+ direction = direction.lower()
+ if direction.startswith('v'):
+ dir = wx.VERTICAL
+ elif direction.startswith('h'):
+ dir = wx.HORIZONTAL
+ else:
+ raise ValueError, "Unknown direction: %s" % (direction,)
+
+ self.sizer = wx.StaticBoxSizer(groupbox, dir)
+ self._packed = 0
+
+#
+# PlainContainer
+
+class PlainContainer(Container):
+ """ A container without a sizer. Controls must be added at a given position.
+ Size can be specified as well.
+ """
+
+ def _create_sizer(self):
+ self.sizer = None
+ self._packed = 0 # included for compatibility
+
+ def AddComponent(self, x, y, comp):
+ # make sure the parents are correct
+
+ comp.SetPosition((x, y))
+
+ def AddComponentAndSize(self, x, y, sx, sy, comp):
+ # make sure the parents are correct
+
+ comp.SetPosition((x, y))
+ comp.SetSize((sx, sy))
+
+ def Pack(self):
+ self._packed = 1 # useless, but included for compatibility
+
+ if len(self.Children) == 1:
+ import panel
+ dummy = panel.Panel(self, size=(0,0))
+ dummy.Position = 0, 0
+ self.__dummy = dummy
+
+ def Repack():
+ pass
+
diff --git a/pynxc/waxy/containers.py~ b/pynxc/waxy/containers.py~
new file mode 100644
index 0000000..5c8c70d
--- /dev/null
+++ b/pynxc/waxy/containers.py~
@@ -0,0 +1,314 @@
+# containers.py
+# Containers are mixin classes that have a sizer and a number of methods to
+# deal with that sizer (add components, etc). Used to implement fairly
+# "basic" controls like Panel and Frame. When in doubt, derive from those
+# two rather than making up your own container construct.
+
+import utils
+import waxyobject
+import wx
+import warnings
+
+class Container(waxyobject.WaxyObject):
+
+ def _create_sizer(self, direction):
+ if direction.lower().startswith("h"):
+ dir = wx.HORIZONTAL
+ elif direction.lower().startswith("v"):
+ dir = wx.VERTICAL
+ else:
+ raise ValueError, "Incorrect direction"
+ self.sizer = wx.BoxSizer(dir)
+ self._packed = 0
+
+ def Body(self):
+ """ Optionally override this to create components. Add them with the
+ AddComponent() method. """
+
+ def AddComponent(self, comp, expand=0, stretch=0, align=None, border=0):
+ # expand: expands a component in the direction of the panel/sizer
+ # stretch: expands a component in the other direction
+ # for example, if we have a horizontal panel, and resize the window
+ # both horizontally and vertically, then a component with 'expand'
+ # will expand horizontally, and one with 'stretch' will expand
+ # vertically. (See simplebuttons.py)
+
+ if isinstance(expand, str):
+ expand = expand.lower()
+ if expand.startswith("h"):
+ if self.GetOrientation() == 'horizontal':
+ expand, stretch = 1, 0
+ else:
+ expand, stretch = 0, 1
+ elif expand.startswith("v"):
+ if self.GetOrientation() == 'horizontal':
+ expand, stretch = 0, 1
+ else:
+ expand, stretch = 1, 0
+ elif expand.startswith("b"):
+ expand = stretch = 1
+ elif expand == '':
+ expand = stretch = 0
+ else:
+ raise ValueError, "Invalid value for expand: %r" % (expand,)
+
+ flags = 0
+ if stretch:
+ flags |= wx.EXPAND
+ if align:
+ flags |= {
+ "t": wx.ALIGN_TOP,
+ "c": wx.ALIGN_CENTER,
+ "b": wx.ALIGN_BOTTOM,
+ "r": wx.ALIGN_RIGHT,
+ "l": wx.ALIGN_LEFT,
+ }.get(align.lower()[:1], 0)
+ if border:
+ flags |= wx.ALL
+
+ flags |= wx.FIXED_MINSIZE
+ if 0 and hasattr(comp, 'sizer') and hasattr(comp, '_include_sizer'):
+ # ugly hack to add nested sizer rather than its frame
+ # only for certain quirky controls, like GroupBox!
+ self.sizer.Add(comp.sizer, expand, flags)
+ else:
+ self.sizer.Add(comp, expand, flags, border)
+ # comp can be a component or a tuple (width, height), but the
+ # Add() method is called just the same, as of wxPython 2.5.1.5
+
+ def AddSpace(self, width, height=None, *args, **kwargs):
+ if height is None:
+ height = width
+ return self.AddComponent((width, height), *args, **kwargs)
+ AddSeparator = AddSpace
+
+ def Pack(self):
+ if not self._packed:
+ self.SetSizerAndFit(self.sizer)
+ self._packed = 1
+
+ def Repack(self):
+ self.sizer.RecalcSizes()
+
+ def GetOrientation(self):
+ """ Return 'horizontal' or 'vertical'. """
+ orientation = self.sizer.GetOrientation()
+ if orientation == wx.HORIZONTAL:
+ return 'horizontal'
+ elif orientation == wx.VERTICAL:
+ return 'vertical'
+ else:
+ raise ValueError, "Unknown direction for sizer"
+
+#
+# GridContainer (used to implement GridPanel)
+
+class GridContainer(Container):
+
+ _sizerclass = wx.GridSizer
+
+ alignment = {
+ 'b': wx.ALIGN_BOTTOM,
+ 'r': wx.ALIGN_RIGHT,
+ 'l': wx.ALIGN_LEFT,
+ 'c': wx.ALIGN_CENTER,
+ 't': wx.ALIGN_TOP,
+ 'h': wx.ALIGN_CENTER_HORIZONTAL,
+ 'v': wx.ALIGN_CENTER_VERTICAL,
+ }
+
+ def _create_sizer(self, rows, cols, hgap=1, vgap=1):
+ self.sizer = self._sizerclass(rows, cols, vgap, hgap)
+ self.controls = {}
+ for row in range(rows):
+ for col in range(cols):
+ self.controls[col, row] = None
+ # (col, row) allows for (x, y)-like calling
+ self._packed = 0
+
+ def AddComponent(self, col, row, obj, expand=1, align='', border=0, stretch=0, proportion=0):
+ # make sure the parents are correct
+ if stretch:
+ warnings.warn("stretch is deprecated and has no effect here")
+
+ if self.controls[col, row]:
+ raise ValueError, "A control has already been set for position (%s, %s)" % (row, col)
+ self.controls[col, row] = {'obj': obj, 'expand': expand, 'align': align,
+ 'border': border, 'proportion': proportion}
+
+ def Pack(self):
+ if not self._packed:
+ controls = self._AllControls()
+ self.sizer.AddMany(controls)
+ self.SetSizerAndFit(self.sizer) # is this still necessary?
+ self._packed = 1
+
+ def __setitem__(self, index, value):
+ col, row = index
+ self.AddComponent(col, row, value)
+
+ def __getitem__(self, index):
+ col, row = index
+ return self.controls[col, row]['obj'] # may raise KeyError
+
+ def _AllControls(self):
+ controls = []
+ for row in range(self.sizer.GetRows()):
+ for col in range(self.sizer.GetCols()):
+ d = self.controls[col, row]
+ if d is None:
+ from panel import Panel
+ p = Panel(self) # hack up a dummy panel
+ controls.append((p, 0, wx.EXPAND|wx.FIXED_MINSIZE))
+ else:
+ obj = d['obj']
+
+ # set alignment
+ align = d['align'].lower()
+ alignment = 0
+ for key, value in self.__class__.alignment.items():
+ if key in align:
+ alignment |= value
+
+ z = d['expand'] and wx.EXPAND
+ z |= alignment
+ border = d['border']
+ #if border and not alignment:
+ if border:
+ z |= wx.ALL
+ z |= wx.FIXED_MINSIZE
+ proportion = d['proportion']
+ controls.append((obj, proportion, z, border))
+
+ return controls
+
+ # XXX maybe a Wax version of AddMany() would be useful?
+
+#
+# FlexGridContainer
+
+class FlexGridContainer(GridContainer):
+ _sizerclass = wx.FlexGridSizer
+
+ def AddGrowableRow(self, row):
+ self.sizer.AddGrowableRow(row)
+
+ def AddGrowableCol(self, col):
+ self.sizer.AddGrowableCol(col)
+
+#
+# OverlayContainer
+
+class OverlaySizer(wx.PySizer):
+ def __init__(self):
+ wx.PySizer.__init__(self)
+
+ def CalcMin(self):
+ maxx, maxy = 0, 0
+ for win in self.GetChildren():
+ x, y = win.CalcMin()
+ maxx = max(maxx, x)
+ maxy = max(maxy, y)
+ return wx.Size(maxx, maxy)
+
+ def RecalcSizes(self):
+ pos = self.GetPosition()
+ size = self.GetSize()
+ for win in self.GetChildren():
+ win.SetDimension(pos, size)
+
+
+class OverlayContainer(Container):
+ """ Container that takes an arbitrary number of windows, and stacks them
+ on top of each other. Controls are hidden by default. """
+
+ def _create_sizer(self):
+ self.sizer = OverlaySizer()
+ self.windows = []
+ self._packed = 0
+
+ def AddComponent(self, window, expand=0, stretch=0, align=None, border=0):
+ """
+ # make sure the parents are correct
+ if waxconfig.WaxConfig.check_parent:
+ if window.GetParent() is not self:
+ utils.parent_warning(window, self)
+
+ flags = 0
+ if stretch:
+ flags |= wx.EXPAND
+ if align:
+ flags |= {
+ "t": wx.ALIGN_TOP,
+ "c": wx.ALIGN_CENTER,
+ "b": wx.ALIGN_BOTTOM,
+ "r": wx.ALIGN_RIGHT,
+ "l": wx.ALIGN_LEFT,
+ }.get(align.lower()[:1], 0)
+ if border:
+ flags |= wx.ALL
+ flags |= wx.FIXED_MINSIZE
+
+ self.sizer.Add(window, expand, flags, border)
+ """
+ Container.AddComponent(self, window, expand, stretch, align, border)
+ self.windows.append(window)
+ window.Hide() # default is hiding
+
+
+
+#
+# GroupBoxContainer
+
+class GroupBoxContainer(Container):
+
+ def _create_sizer(self, groupbox, direction='h'):
+ direction = direction.lower()
+ if direction.startswith('v'):
+ dir = wx.VERTICAL
+ elif direction.startswith('h'):
+ dir = wx.HORIZONTAL
+ else:
+ raise ValueError, "Unknown direction: %s" % (direction,)
+
+ self.sizer = wx.StaticBoxSizer(groupbox, dir)
+ self._packed = 0
+
+#
+# PlainContainer
+
+class PlainContainer(Container):
+ """ A container without a sizer. Controls must be added at a given position.
+ Size can be specified as well.
+ """
+
+ def _create_sizer(self):
+ self.sizer = None
+ self._packed = 0 # included for compatibility
+
+ def AddComponent(self, x, y, comp):
+ # make sure the parents are correct
+
+ comp.SetPosition((x, y))
+
+ def AddComponentAndSize(self, x, y, sx, sy, comp):
+ # make sure the parents are correct
+ if waxconfig.WaxConfig.check_parent:
+ if comp.GetParent() is not self:
+ utils.parent_warning(comp, self)
+
+ comp.SetPosition((x, y))
+ comp.SetSize((sx, sy))
+
+ def Pack(self):
+ self._packed = 1 # useless, but included for compatibility
+
+ if len(self.Children) == 1:
+ import panel
+ dummy = panel.Panel(self, size=(0,0))
+ dummy.Position = 0, 0
+ self.__dummy = dummy
+
+ def Repack():
+ pass
+
diff --git a/pynxc/waxy/core.py b/pynxc/waxy/core.py
new file mode 100644
index 0000000..0b58a48
--- /dev/null
+++ b/pynxc/waxy/core.py
@@ -0,0 +1,36 @@
+DEBUG = 0 # set to 1 to enable debugging messages
+
+from wx import BeginBusyCursor, CallAfter, EndBusyCursor, GetActiveWindow, \
+ GetApp, Platform, SafeYield, WakeUpIdle, Yield, YieldIfNeeded
+
+
+
+if False:
+ required = (2, 8, 0, 0) # minimum wxPython version
+ required_str = ".".join(map(str, required))
+
+ # if a multiversion wxPython is installed, attempt to get a required version
+ try:
+ # for wxversion, we only take the first two numbers
+ import wxversion
+ wxversion_req = ".".join(map(str, required[:2]))
+ try:
+ wxversion.ensureMinimal(wxversion_req)
+ except wxversion.VersionError, e:
+ # it should also be possible to import wax after wxPython is imported.
+ # this VersionError prevents this, so we work around it.
+ if e.args[0].find("must be called before wxPython is imported") < 0:
+ raise
+ except ImportError: # will fail if it's not a multiversion installation
+ pass
+
+ import wx
+
+ assert wx.VERSION >= required, \
+ "This version of Wax requires wxPython %s or later" % (required_str,)
+
+ DEBUG = 0 # set to 1 to enable debugging messages
+
+ from wx import BeginBusyCursor, CallAfter, EndBusyCursor, GetActiveWindow, \
+ GetApp, Platform, SafeYield, WakeUpIdle, Yield, YieldIfNeeded
+
diff --git a/pynxc/waxy/core.py~ b/pynxc/waxy/core.py~
new file mode 100644
index 0000000..ef04e1f
--- /dev/null
+++ b/pynxc/waxy/core.py~
@@ -0,0 +1,2 @@
+required = (2, 8, 0, 0) # minimum wxPython version
+required_str = ".".join(map(str, required))
diff --git a/pynxc/waxy/customdialog.py b/pynxc/waxy/customdialog.py
new file mode 100644
index 0000000..dfe73b9
--- /dev/null
+++ b/pynxc/waxy/customdialog.py
@@ -0,0 +1,66 @@
+# customdialog.py
+
+import wx
+import containers
+import dialog
+
+class CustomDialog(wx.Dialog, containers.Container):
+
+ __events__ = dialog.Dialog.__events__.copy()
+
+ def __init__(self, parent, title=""):
+ wx.Dialog.__init__(self, parent, wx.NewId(), title, wx.DefaultPosition)
+
+ # this should create self.sizer and give access to self.AddComponent
+ self._create_sizer('vertical')
+
+ # We use EndModal() internally, and that requires that we return
+ # IDs. _modalhooks is a mapping from ID to desired return value (as a
+ # string), used by CustomDialog.ShowModal. _count is used for
+ # generating the unique IDs.
+ self._modalhooks = {}
+ self._count = 0 # enumerator
+
+ self.Body()
+
+ self.BindEvents()
+ self.Pack()
+ self.Centre()
+
+ def Body(self):
+ pass
+
+ def _GetEnumValue(self):
+ self._count = self._count + 1
+ return self._count
+
+ def SetBehavior(self, button, result, validator=None, event=None):
+ """ Set the behavior for the given button. <result> is the desired
+ result, as a string ('ok', 'cancel', 'foo', etc). <event> is
+ called, if defined.
+ Note 1: EndModal() is always called, causing the dialog to be
+ closed. If you don't want this (i.e. if you want a "regular" button
+ on the dialog), don't call SetBehavior.
+ Note: Any OnClick-event that already exists on the button will
+ be overwritten. It will *not* be called.
+ """
+ id = self._GetEnumValue()
+ self._modalhooks[id] = result
+ def OnClick(zevent):
+ # XXX there's room for a "validator" here
+ if validator:
+ pass # not sure how to do this yet
+ if event:
+ event(zevent)
+ # note: if an exception occurs here, it is not caught
+ self.EndModal(id)
+ button.OnClick = OnClick
+
+ def OnClickButton(self, event):
+ button = event.GetEventObject()
+
+ def ShowModal(self):
+ r = wx.Dialog.ShowModal(self)
+ # here, we need to figure out which button was clicked!
+ return self._modalhooks.get(r, "?")
+
diff --git a/pynxc/waxy/customdialog.py~ b/pynxc/waxy/customdialog.py~
new file mode 100644
index 0000000..6af64c8
--- /dev/null
+++ b/pynxc/waxy/customdialog.py~
@@ -0,0 +1,67 @@
+# customdialog.py
+
+import wx
+import containers
+import dialog
+
+class CustomDialog(wx.Dialog, containers.Container):
+
+ __events__ = dialog.Dialog.__events__.copy()
+
+ def __init__(self, parent, title=""):
+ wx.Dialog.__init__(self, parent, wx.NewId(), title, wx.DefaultPosition)
+
+ # this should create self.sizer and give access to self.AddComponent
+ self._create_sizer('vertical')
+ self.SetDefaultFont()
+
+ # We use EndModal() internally, and that requires that we return
+ # IDs. _modalhooks is a mapping from ID to desired return value (as a
+ # string), used by CustomDialog.ShowModal. _count is used for
+ # generating the unique IDs.
+ self._modalhooks = {}
+ self._count = 0 # enumerator
+
+ self.Body()
+
+ self.BindEvents()
+ self.Pack()
+ self.Centre()
+
+ def Body(self):
+ pass
+
+ def _GetEnumValue(self):
+ self._count = self._count + 1
+ return self._count
+
+ def SetBehavior(self, button, result, validator=None, event=None):
+ """ Set the behavior for the given button. <result> is the desired
+ result, as a string ('ok', 'cancel', 'foo', etc). <event> is
+ called, if defined.
+ Note 1: EndModal() is always called, causing the dialog to be
+ closed. If you don't want this (i.e. if you want a "regular" button
+ on the dialog), don't call SetBehavior.
+ Note: Any OnClick-event that already exists on the button will
+ be overwritten. It will *not* be called.
+ """
+ id = self._GetEnumValue()
+ self._modalhooks[id] = result
+ def OnClick(zevent):
+ # XXX there's room for a "validator" here
+ if validator:
+ pass # not sure how to do this yet
+ if event:
+ event(zevent)
+ # note: if an exception occurs here, it is not caught
+ self.EndModal(id)
+ button.OnClick = OnClick
+
+ def OnClickButton(self, event):
+ button = event.GetEventObject()
+
+ def ShowModal(self):
+ r = wx.Dialog.ShowModal(self)
+ # here, we need to figure out which button was clicked!
+ return self._modalhooks.get(r, "?")
+
diff --git a/pynxc/waxy/demos/.DS_Store b/pynxc/waxy/demos/.DS_Store
new file mode 100644
index 0000000..e162bfd
--- /dev/null
+++ b/pynxc/waxy/demos/.DS_Store
Binary files differ
diff --git a/pynxc/waxy/demos/.jedrecent b/pynxc/waxy/demos/.jedrecent
new file mode 100644
index 0000000..f4f65e0
--- /dev/null
+++ b/pynxc/waxy/demos/.jedrecent
@@ -0,0 +1,11 @@
+/home/bblais/python/lib/waxy/progressdialog.py:17:31:0
+/home/bblais/python/work/waxy/demos/ProgressDialog.py:27:30:0
+/home/bblais/python/lib/waxy/multichoicedialog.py:1:1:0
+/home/bblais/python/lib/waxy/keys.py:1:1:0
+/home/bblais/python/lib/waxy/artprovider.py:1:1:0
+/home/bblais/python/lib/waxy/bitmapbutton.py:18:52:0
+/home/bblais/python/lib/waxy/notes.txt:17:9:0
+/home/bblais/python/lib/waxy/__init__.py:20:1:0
+/home/bblais/python/lib/waxy/singlechoicedialog.py:8:1:1
+/home/bblais/python/lib/waxy/listbox.py:5:12:1
+/home/bblais/python/lib/waxy/demos/SingleChoiceDialog.py:54:43:1
diff --git a/pynxc/waxy/demos/AboutBox.py b/pynxc/waxy/demos/AboutBox.py
new file mode 100755
index 0000000..7cdea24
--- /dev/null
+++ b/pynxc/waxy/demos/AboutBox.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(Frame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "About Box1",self.About1,default=True)
+ self.AddComponent(b)
+
+ b = Button(self, "About Box2",self.About2,default=True)
+ self.AddComponent(b)
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def About1(self,event):
+
+ licenseText = "blah " * 250 + "\n\n" +"yadda " * 100
+
+ AboutBox(self,name='Hello World',
+ version='1.2.3',
+ copyright="(C) 2006 Programmers and Coders Everywhere",
+ description=(
+ "A \"hello world\" program is a software program that prints out "
+ "\"Hello world!\" on a display device. It is used in many introductory "
+ "tutorials for teaching a programming language."
+
+ "\n\nSuch a program is typically one of the simplest programs possible "
+ "in a computer language. A \"hello world\" program can be a useful "
+ "sanity test to make sure that a language's compiler, development "
+ "environment, and run-time environment are correctly installed."),
+ url="http://en.wikipedia.org/wiki/Hello_world",
+ webtitle="Hello World home page",
+ developers=[ "Joe Programmer",
+ "Jane Coder",
+ "Vippy the Mascot" ],
+ license=licenseText)
+
+
+ def About2(self,event):
+
+ licenseText = "blah " * 250 + "\n\n" +"yadda " * 100
+
+ info={}
+ info['name']='Hello World'
+ info['version']='1.2.3'
+ info['copyright']="(C) 2006 Programmers and Coders Everywhere"
+ info['description']=(
+ "A \"hello world\" program is a software program that prints out "
+ "\"Hello world!\" on a display device. It is used in many introductory "
+ "tutorials for teaching a programming language."
+
+ "\n\nSuch a program is typically one of the simplest programs possible "
+ "in a computer language. A \"hello world\" program can be a useful "
+ "sanity test to make sure that a language's compiler, development "
+ "environment, and run-time environment are correctly installed.")
+ info['url']="http://en.wikipedia.org/wiki/Hello_world"
+ info['webtitle']="Hello World home page"
+ info['developers']=[ "Joe Programmer",
+ "Jane Coder",
+ "Vippy the Mascot" ]
+ info['license']=licenseText
+
+
+ AboutBox(self,info)
+
+
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="AboutBox")
+ app.Run()
diff --git a/pynxc/waxy/demos/AboutBox.py~ b/pynxc/waxy/demos/AboutBox.py~
new file mode 100755
index 0000000..eacd55a
--- /dev/null
+++ b/pynxc/waxy/demos/AboutBox.py~
@@ -0,0 +1,89 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(Frame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "About Box1",self.About1,default=True)
+ self.AddComponent(b)
+
+ b = Button(self, "About Box2",self.About2,default=True)
+ self.AddComponent(b)
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def About1(self):
+
+ licenseText = "blah " * 250 + "\n\n" +"yadda " * 100
+
+ AboutBox(self,name='Hello World',
+ version='1.2.3',
+ copyright="(C) 2006 Programmers and Coders Everywhere",
+ description=(
+ "A \"hello world\" program is a software program that prints out "
+ "\"Hello world!\" on a display device. It is used in many introductory "
+ "tutorials for teaching a programming language."
+
+ "\n\nSuch a program is typically one of the simplest programs possible "
+ "in a computer language. A \"hello world\" program can be a useful "
+ "sanity test to make sure that a language's compiler, development "
+ "environment, and run-time environment are correctly installed."),
+ url="http://en.wikipedia.org/wiki/Hello_world",
+ webtitle="Hello World home page",
+ developers=[ "Joe Programmer",
+ "Jane Coder",
+ "Vippy the Mascot" ],
+ license=licenseText)
+
+
+ def About2(self):
+
+ licenseText = "blah " * 250 + "\n\n" +"yadda " * 100
+
+ info={}
+ info['name']='Hello World'
+ info['version']='1.2.3'
+ info['copyright']="(C) 2006 Programmers and Coders Everywhere"
+ info['description']=(
+ "A \"hello world\" program is a software program that prints out "
+ "\"Hello world!\" on a display device. It is used in many introductory "
+ "tutorials for teaching a programming language."
+
+ "\n\nSuch a program is typically one of the simplest programs possible "
+ "in a computer language. A \"hello world\" program can be a useful "
+ "sanity test to make sure that a language's compiler, development "
+ "environment, and run-time environment are correctly installed.")
+ info['url']="http://en.wikipedia.org/wiki/Hello_world"
+ info['webtitle']="Hello World home page"
+ info['developers']=[ "Joe Programmer",
+ "Jane Coder",
+ "Vippy the Mascot" ]
+ info['license']=licenseText
+
+
+ AboutBox(self,info)
+
+
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="AboutBox")
+ app.Run()
diff --git a/pynxc/waxy/demos/Button.py b/pynxc/waxy/demos/Button.py
new file mode 100755
index 0000000..998ef2b
--- /dev/null
+++ b/pynxc/waxy/demos/Button.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "Default Button",self.OnClick,default=True)
+ self.AddComponent(b)
+
+ b = Button(self, "Hello Again",self.OnClick,
+ tooltip="This is a Hello button...")
+ self.AddComponent(b)
+
+ b = Button(self, "Flat Button?",self.OnClick,
+ tooltip="This button has a style flag of wx.NO_BORDER. On some platforms that will give it a flattened look.",flat=True)
+ self.AddComponent(b)
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+ def OnClick(self,event):
+ print "Click! (%d)\n" % (event.GetId())
+
+ def CloseWindow(self,event):
+ self.Close()
+
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="Some buttons")
+ app.Run()
diff --git a/pynxc/waxy/demos/Button.py~ b/pynxc/waxy/demos/Button.py~
new file mode 100755
index 0000000..7274a33
--- /dev/null
+++ b/pynxc/waxy/demos/Button.py~
@@ -0,0 +1,42 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "Default Button",self.OnClick,default=True)
+ self.AddComponent(b)
+
+ b = Button(self, "Hello Again",self.OnClick,
+ tooltip="This is a Hello button...")
+ self.AddComponent(b)
+
+ b = Button(self, "Flat Button?",self.OnClick,
+ tooltip="This button has a style flag of wx.NO_BORDER. On some platforms that will give it a flattened look.",flat=True)
+ self.AddComponent(b)
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+ def OnClick(self,event):
+ print "Click! (%d)\n" % (event.GetId())
+
+ def CloseWindow(self,event):
+ self.Close()
+
+
+app = Application(MainFrame, title="Some buttons")
+app.Run()
diff --git a/pynxc/waxy/demos/Calendar.py b/pynxc/waxy/demos/Calendar.py
new file mode 100755
index 0000000..1780b0d
--- /dev/null
+++ b/pynxc/waxy/demos/Calendar.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+
+from waxy import *
+import wx.lib.calendar
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+ self.calend = wx.lib.calendar.Calendar(self, -1)
+ self.calend.SetCurrentDay()
+ self.calend.grid_color = 'BLUE'
+ self.calend.SetBusType()
+ self.calend.Show(True)
+
+ self.Bind(wx.lib.calendar.EVT_CALENDAR, self.MouseClick, self.calend)
+
+
+ self.SetSize((640, 480))
+
+ def MouseClick(self, evt):
+ text = '%s CLICK %02d/%02d/%d' % (evt.click, evt.day, evt.month, evt.year) # format date
+ print 'Date Selected: ' + text + '\n'
+
+
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="Calendar")
+ app.Run()
diff --git a/pynxc/waxy/demos/Canvas.py b/pynxc/waxy/demos/Canvas.py
new file mode 100644
index 0000000..7822e62
--- /dev/null
+++ b/pynxc/waxy/demos/Canvas.py
@@ -0,0 +1,43 @@
+from waxy import *
+
+class MyCanvas(Canvas):
+
+ def Init(self):
+ self.SetBackgroundColour('WHITE')
+
+ def OnDraw(self, dc):
+ dc.SetTextForeground('BLACK')
+ dc.SetPen(MEDIUM_GREY_PEN)
+
+ self.DrawHexagons(dc, 300, 10, radius=40, max_number=11)
+
+ def DrawHexagons(self, dc, x, y, radius, max_number):
+ for i in range(1, max_number+1):
+ offset = i * -0.5 * radius
+ for j in range(i):
+ self.DrawHexagon(dc, x + offset + j * radius,
+ y + (i * 0.75 * radius), radius)
+
+ def DrawHexagon(self, dc, x, y, radius):
+ # does not seem like a 'real' hexagon, but this will do for now
+ p1 = (x, y)
+ p2 = (x + 0.5 * radius, y + 0.25 * radius)
+ p3 = (p2[0], p2[1] + 0.5 * radius)
+ p4 = (p1[0], p1[1] + radius)
+ p5 = (x - 0.5 * radius, p3[1])
+ p6 = (x - 0.5 * radius, y + 0.25 * radius)
+
+ dc.DrawLinePoint(p1, p2)
+ dc.DrawLinePoint(p2, p3)
+ dc.DrawLinePoint(p3, p4)
+ dc.DrawLinePoint(p4, p5)
+ dc.DrawLinePoint(p5, p6)
+ dc.DrawLinePoint(p6, p1)
+
+class MainFrame(Frame):
+ def Body(self):
+ canvas = MyCanvas(self)
+ self.SetSize((600, 500))
+
+app = Application(MainFrame)
+app.MainLoop()
diff --git a/pynxc/waxy/demos/ColorDialog.py b/pynxc/waxy/demos/ColorDialog.py
new file mode 100755
index 0000000..03c3234
--- /dev/null
+++ b/pynxc/waxy/demos/ColorDialog.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(Frame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "Color Dialog",self.Color,default=True)
+ self.AddComponent(b)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def Color(self,event):
+
+
+ dlg=ColorDialog(self)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenColor()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="ColorDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/ColorDialog.py~ b/pynxc/waxy/demos/ColorDialog.py~
new file mode 100644
index 0000000..bf26f0f
--- /dev/null
+++ b/pynxc/waxy/demos/ColorDialog.py~
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(Frame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "Colour Dialog",self.Colour,default=True)
+ self.AddComponent(b)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def Colour(self,event):
+
+
+ dlg=ColourDialog(self)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenColour()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="ColourDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/ColourDialog.py b/pynxc/waxy/demos/ColourDialog.py
new file mode 100755
index 0000000..bf26f0f
--- /dev/null
+++ b/pynxc/waxy/demos/ColourDialog.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(Frame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "Colour Dialog",self.Colour,default=True)
+ self.AddComponent(b)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def Colour(self,event):
+
+
+ dlg=ColourDialog(self)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenColour()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="ColourDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/ColourDialog.py~ b/pynxc/waxy/demos/ColourDialog.py~
new file mode 100755
index 0000000..761b610
--- /dev/null
+++ b/pynxc/waxy/demos/ColourDialog.py~
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(Frame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "Colour Dialog",self.Colour,default=True)
+ self.AddComponent(b)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def Colour(self,event):
+
+
+ dlg=ColourDialog(self)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Here: ",dlg.GetChosenColour()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="ColourDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/DirectoryDialog.py b/pynxc/waxy/demos/DirectoryDialog.py
new file mode 100755
index 0000000..1943b1f
--- /dev/null
+++ b/pynxc/waxy/demos/DirectoryDialog.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "Directory Dialog",self.Directory,default=True)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "Choose Directory",self.Directory2)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "Directory Dialog with New Button",self.Directory3,default=True)
+ self.AddComponent(b,border=10)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def Directory(self,event):
+
+
+ dlg=DirectoryDialog(self)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenDirectory()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def Directory2(self,event):
+
+
+ res=ChooseDirectory(self)
+
+ print "Chose: ",res
+
+
+ def Directory3(self,event):
+
+
+ dlg=DirectoryDialog(self,new_dir_button=True)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenDirectory()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="DirectoryDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/DirectoryDialog.py~ b/pynxc/waxy/demos/DirectoryDialog.py~
new file mode 100755
index 0000000..3d13c7d
--- /dev/null
+++ b/pynxc/waxy/demos/DirectoryDialog.py~
@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "Directory Dialog",self.Directory,default=True)
+ self.AddComponent(b,border=20)
+
+ b = Button(self, "Choose Directory",self.Directory2)
+ self.AddComponent(b,border=20)
+
+ b = Button(self, "Directory Dialog with New Button",self.Directory3,default=True)
+ self.AddComponent(b,border=20)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def Directory(self,event):
+
+
+ dlg=DirectoryDialog(self)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenDirectory()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def Directory2(self,event):
+
+
+ res=ChooseDirectory(self)
+
+ print "Chose: ",res
+
+
+ def Directory3(self,event):
+
+
+ dlg=DirectoryDialog(self,new_dir_button=True)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenDirectory()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="DirectoryDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/Drag.py b/pynxc/waxy/demos/Drag.py
new file mode 100755
index 0000000..42ae3bf
--- /dev/null
+++ b/pynxc/waxy/demos/Drag.py
@@ -0,0 +1,110 @@
+#!/usr/bin/env python
+
+from waxy import *
+class Sprite:
+ def __init__(self, fname):
+ self.bitmap = Image(fname+".png").ConvertToBitmap()
+ self.bitmap_rev = Image(fname+"rev.png").ConvertToBitmap()
+ self.background = None
+ self.pos=wx.Point(0,0)
+ self.reversed=False
+
+ def Draw(self, dc):
+ # copy background from dc to memory object
+ memdc = wx.MemoryDC()
+ memdc.Blit(0, 0, self.bitmap.GetWidth(), self.bitmap.GetHeight(), dc,
+ self.pos[0], self.pos[1], wx.COPY, 1)
+ self.background = memdc # keep around for later
+
+ bitmap = self.bitmap if not self.reversed else self.bitmap_rev
+ dc.DrawBitmap(bitmap, self.pos[0], self.pos[1], 1)
+
+ def HitTest(self, pt):
+ rect = self.GetRect()
+ return rect.InsideXY(pt.x, pt.y)
+
+
+ def GetRect(self):
+ return wx.Rect(self.pos[0], self.pos[1],
+ self.bitmap.GetWidth(), self.bitmap.GetHeight())
+
+class MyCanvas(Canvas):
+
+ def Init(self):
+ self.SetBackgroundColour('WHITE')
+ self.SetScrollbars(20, 20, 100, 100)
+ self.sprite = Sprite('heretic2')
+ self.sprite.pos=wx.Point(50,50)
+ self.selected=False
+ self.starting_mouse_pos=None
+ self.sprite_orig_pos=None
+
+ def OnLeftDown(self,event=None):
+
+ if self.sprite.HitTest(event.Position):
+ self.selected=True
+ self.starting_mouse_pos=event.Position
+ self.sprite_orig_pos=self.sprite.pos
+ self.sprite.reversed=not self.sprite.reversed
+ self.Refresh()
+
+ def OnLeftUp(self,event=None):
+ self.selected=False
+
+ def OnMotion(self,event=None):
+ if not event.Dragging():
+ return
+
+ if not self.selected:
+ return
+
+ print "move.",event.Position
+ self.sprite.pos=self.sprite_orig_pos+event.Position-self.starting_mouse_pos
+
+ self.Refresh()
+
+
+ def OnDraw(self, dc):
+ dc.SetTextForeground('BLUE')
+ dc.SetPen(wx.MEDIUM_GREY_PEN)
+ for i in range(50):
+ dc.DrawLine(0, i*10, i*10, 0)
+
+ self.sprite.Draw(dc)
+
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ self.canvas = MyCanvas(self)
+ self.canvas.SetSize((400,300))
+ self.AddComponent(self.canvas, expand=1, stretch=1)
+
+ self.Pack()
+
+
+
+ self.CenterOnScreen()
+
+ self.Show()
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+
+
+if __name__=="__main__":
+ app = Application(MainFrame, title=__file__)
+ app.Run()
diff --git a/pynxc/waxy/demos/FileDialog.py b/pynxc/waxy/demos/FileDialog.py
new file mode 100755
index 0000000..6c2bc8e
--- /dev/null
+++ b/pynxc/waxy/demos/FileDialog.py
@@ -0,0 +1,104 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+wildcard = "Python source (*.py)|*.py|" \
+ "Compiled Python (*.pyc)|*.pyc|" \
+ "SPAM files (*.spam)|*.spam|" \
+ "Egg file (*.egg)|*.egg|" \
+ "All files (*.*)|*.*"
+
+class MainFrame(Frame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "File OPEN Dialog",self.File1,default=True)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "File SAVE Dialog",self.File2)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "File MULTIPLE OPEN Dialog",self.File3)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "File Dialog (default?)",self.File0)
+ self.AddComponent(b,border=10)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def File1(self,event):
+
+
+ dlg=FileDialog(self,wildcard=wildcard,open=True)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenFile()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def File2(self,event):
+
+
+ dlg=FileDialog(self,wildcard=wildcard,save=True)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenFile()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def File3(self,event):
+
+
+ dlg=FileDialog(self,wildcard=wildcard,open=True,multiple=True)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenFile()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def File0(self,event):
+
+
+ dlg=FileDialog(self,wildcard=wildcard)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenFile()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="FileDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/FileDialog.py~ b/pynxc/waxy/demos/FileDialog.py~
new file mode 100755
index 0000000..4bf03bc
--- /dev/null
+++ b/pynxc/waxy/demos/FileDialog.py~
@@ -0,0 +1,104 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+wildcard = "Python source (*.py)|*.py|" \
+ "Compiled Python (*.pyc)|*.pyc|" \
+ "SPAM files (*.spam)|*.spam|" \
+ "Egg file (*.egg)|*.egg|" \
+ "All files (*.*)|*.*"
+
+class MainFrame(Frame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "File OPEN Dialog",self.File1,default=True)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "File SAVE Dialog",self.File2)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "File MULTIPLE OPEN Dialog",self.File3)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "File Dialog (default?)",self.File2)
+ self.AddComponent(b,border=10)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def File1(self,event):
+
+
+ dlg=FileDialog(self,wildcard=wildcard,open=True)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenFile()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def File2(self,event):
+
+
+ dlg=FileDialog(self,wildcard=wildcard,save=True)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenFile()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def File3(self,event):
+
+
+ dlg=FileDialog(self,wildcard=wildcard,open=True,multiple=True)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenFile()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def File0(self,event):
+
+
+ dlg=FileDialog(self,wildcard=wildcard)
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenFile()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="FileDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/Grid.py b/pynxc/waxy/demos/Grid.py
new file mode 100755
index 0000000..833d57d
--- /dev/null
+++ b/pynxc/waxy/demos/Grid.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ self.cellbox=TextBox(self)
+ self.AddComponent(self.cellbox,border=10,stretch=True)
+ self.cellbox.OnChar=self.EditCell
+
+ self.grid = Grid(self,100,40)
+ self.AddComponent(self.grid,border=10,expand='both')
+ self.grid.OnSelectCell=self.OnSelectCell
+
+ self.Pack()
+ self.SetSize((1000, 800))
+
+ self.CenterOnScreen()
+
+ self.Show()
+ self.grid.SetFocus()
+ self.cellbox.SetValue(self.grid[0,0])
+
+ def EditCell(self,event):
+
+ if event.KeyCode>0 and event.KeyCode<255: # a character
+
+ v=self.cellbox.GetValue()
+ r,c=self.grid.GetGridCursorRow(),self.grid.GetGridCursorCol()
+
+
+ if event.KeyCode==13: # return
+ self.grid[r,c]=v
+ self.grid.SetFocus()
+ elif event.KeyCode==27: # escape
+ self.cellbox.SetValue("")
+ self.grid.SetFocus()
+
+ event.Skip()
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+ def OnSelectCell(self,event):
+ r,c=event.Row,event.Col
+ self.cellbox.SetValue(self.grid[r,c])
+ event.Skip()
+
+
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="Grid")
+ app.Run()
diff --git a/pynxc/waxy/demos/Grid2.py b/pynxc/waxy/demos/Grid2.py
new file mode 100755
index 0000000..e8d8f5b
--- /dev/null
+++ b/pynxc/waxy/demos/Grid2.py
@@ -0,0 +1,177 @@
+#!/usr/bin/env python
+
+from waxy import *
+import wx
+
+
+class CustomDataTable(wx.grid.PyGridTableBase):
+ def __init__(self):
+ wx.grid.PyGridTableBase.__init__(self)
+
+ self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform',
+ 'Opened?', 'Fixed?', 'Tested?', 'TestFloat']
+
+ self.dataTypes = [wx.grid.GRID_VALUE_NUMBER,
+ wx.grid.GRID_VALUE_STRING,
+ wx.grid.GRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical',
+ wx.grid.GRID_VALUE_NUMBER + ':1,5',
+ wx.grid.GRID_VALUE_CHOICE + ':all,MSW,GTK,other',
+ wx.grid.GRID_VALUE_BOOL,
+ wx.grid.GRID_VALUE_BOOL,
+ wx.grid.GRID_VALUE_BOOL,
+ wx.grid.GRID_VALUE_FLOAT + ':6,2',
+ ]
+
+ self.data = [
+ [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1, 1.12],
+ [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0, 1.50],
+ [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0, 1.56]
+
+ ]
+
+
+ #--------------------------------------------------
+ # required methods for the wxPyGridTableBase interface
+
+ def GetNumberRows(self):
+ return len(self.data) + 1
+
+ def GetNumberCols(self):
+ return len(self.data[0])
+
+ def IsEmptyCell(self, row, col):
+ try:
+ return not self.data[row][col]
+ except IndexError:
+ return True
+
+ # Get/Set values in the table. The Python version of these
+ # methods can handle any data-type, (as long as the Editor and
+ # Renderer understands the type too,) not just strings as in the
+ # C++ version.
+ def GetValue(self, row, col):
+ try:
+ return self.data[row][col]
+ except IndexError:
+ return ''
+
+ def SetValue(self, row, col, value):
+ try:
+ self.data[row][col] = value
+ except IndexError:
+ # add a new row
+ self.data.append([''] * self.GetNumberCols())
+ self.SetValue(row, col, value)
+
+ # tell the grid we've added a row
+ msg = wx.grid.GridTableMessage(self, # The table
+ wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
+ 1 # how many
+ )
+
+ self.GetView().ProcessTableMessage(msg)
+
+
+ #--------------------------------------------------
+ # Some optional methods
+
+ # Called when the grid needs to display labels
+ def GetColLabelValue(self, col):
+ return self.colLabels[col]
+
+ # Called to determine the kind of editor/renderer to use by
+ # default, doesn't necessarily have to be the same type used
+ # natively by the editor/renderer if they know how to convert.
+ def GetTypeName(self, row, col):
+ return self.dataTypes[col]
+
+ # Called to determine how the data can be fetched and stored by the
+ # editor and renderer. This allows you to enforce some type-safety
+ # in the grid.
+ def CanGetValueAs(self, row, col, typeName):
+ colType = self.dataTypes[col].split(':')[0]
+ if typeName == colType:
+ return True
+ else:
+ return False
+
+ def CanSetValueAs(self, row, col, typeName):
+ return self.CanGetValueAs(row, col, typeName)
+
+
+
+class MyGrid(Grid):
+
+ def __init__(self, parent):
+ Grid.__init__(self, parent)
+ table = CustomDataTable()
+
+ self.SetTable(table, True)
+
+ self.SetRowLabelSize(0)
+ self.SetMargins(0,0)
+ self.AutoSizeColumns(False)
+
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ self.cellbox=TextBox(self)
+ self.AddComponent(self.cellbox,border=10,stretch=True)
+ self.cellbox.OnChar=self.EditCell
+
+ self.grid = MyGrid(self)
+ self.AddComponent(self.grid,border=10,expand='both')
+ self.grid.OnSelectCell=self.OnSelectCell
+
+ self.Pack()
+ self.SetSize((1000, 800))
+
+ self.CenterOnScreen()
+
+ self.Show()
+ self.grid.SetFocus()
+ self.cellbox.SetValue(self.grid[0,0])
+
+ def EditCell(self,event):
+
+ if event.KeyCode>0 and event.KeyCode<255: # a character
+
+ v=self.cellbox.GetValue()
+ r,c=self.grid.GetGridCursorRow(),self.grid.GetGridCursorCol()
+
+
+ if event.KeyCode==13: # return
+ self.grid[r,c]=v
+ self.grid.SetFocus()
+ elif event.KeyCode==27: # escape
+ self.cellbox.SetValue("")
+ self.grid.SetFocus()
+
+ event.Skip()
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+ def OnSelectCell(self,event):
+ r,c=event.Row,event.Col
+ self.cellbox.SetValue(self.grid[r,c])
+ event.Skip()
+
+
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="Grid")
+ app.Run()
diff --git a/pynxc/waxy/demos/MessageDialog.py b/pynxc/waxy/demos/MessageDialog.py
new file mode 100755
index 0000000..96f069a
--- /dev/null
+++ b/pynxc/waxy/demos/MessageDialog.py
@@ -0,0 +1,98 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "Message Dialog: Question",self.Message,default=True)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "Choose Message: Statement",self.Message2)
+ self.AddComponent(b,border=10)
+
+
+ panel=Panel(self,direction='h')
+
+ choices = MessageDialog._icons.keys()
+ choices.sort()
+
+ self.dd = DropDownBox(panel, choices)
+ panel.AddComponent(self.dd, border=10)
+
+ b = Button(panel, "Show message", self.Message3)
+ panel.AddComponent(b,border=10)
+
+
+
+ panel.Pack()
+
+
+ self.AddComponent(panel,border=20)
+
+ b = Button(self, "File Exists", self.Message4)
+ self.AddComponent(b,border=10)
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def Message(self,event):
+
+ dlg=MessageDialog(self,title="Holy cow", text="You wanna dance?",
+ ok=0, yes_no=1)
+ res=dlg.ShowModal()
+
+ print res
+
+ dlg.Destroy()
+
+ def Message2(self,event):
+
+ dlg=MessageDialog(self,text="Resistance is futile.")
+ res=dlg.ShowModal()
+
+ print res
+
+ dlg.Destroy()
+
+
+ def Message3(self,event):
+
+ choice = self.dd.GetStringSelection()
+ dlg = MessageDialog(self, "A message", "You chose: " + repr(choice),
+ icon=choice)
+ res=dlg.ShowModal()
+
+ print res
+
+ dlg.Destroy()
+
+ def Message4(self,event):
+ filename='hello.txt'
+ dlg = MessageDialog(self, '"%s" already exists. Do you want to replace it?' % filename,
+ 'A file or folder with the same name already exists in plasticity. Replacing it will overwrite its current contents.',icon='Warning',cancel=1)
+ result = dlg.ShowModal()
+
+ print result
+
+ dlg.Destroy()
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="MessageDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/MessageDialog.py~ b/pynxc/waxy/demos/MessageDialog.py~
new file mode 100755
index 0000000..5a42619
--- /dev/null
+++ b/pynxc/waxy/demos/MessageDialog.py~
@@ -0,0 +1,83 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "Message Dialog: Question",self.Message,default=True)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "Choose Message: Statement",self.Message2)
+ self.AddComponent(b,border=10)
+
+
+ panel=Panel(self,direction='h')
+
+ choices = MessageDialog._icons.keys()
+ choices.sort()
+
+ self.dd = DropDownBox(panel, choices)
+ panel.AddComponent(self.dd, border=10)
+
+ b = Button(panel, "Show message", self.Message3)
+ panel.AddComponent(b)
+ panel.Pack()
+
+ self.AddComponent(panel,border=20)
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def Message(self,event):
+
+ dlg=MessageDialog(self,title="Holy cow", text="You wanna dance?",
+ ok=0, yes_no=1)
+ res=dlg.ShowModal()
+
+ print res
+
+ dlg.Destroy()
+
+ def Message2(self,event):
+
+ dlg=MessageDialog(self,text="Resistance is futile.")
+ res=dlg.ShowModal()
+
+ print res
+
+ dlg.Destroy()
+
+
+ def Message3(self,event):
+
+ choice = self.dd.GetStringSelection()
+ dlg = MessageDialog(self, "A message", "You chose: " + repr(choice),
+ icon=choice)
+ res=dlg.ShowModal()
+
+ print res
+
+ dlg.Destroy()
+
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="MessageDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/MultiChoiceDialog.py b/pynxc/waxy/demos/MultiChoiceDialog.py
new file mode 100755
index 0000000..01644fb
--- /dev/null
+++ b/pynxc/waxy/demos/MultiChoiceDialog.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "MultiChoice Dialog",self.MultiChoice,default=True)
+ self.AddComponent(b,border=10)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def MultiChoice(self,event):
+
+
+ lst = [ 'apple', 'pear', 'banana', 'coconut', 'orange', 'grape', 'pineapple',
+ 'blueberry', 'raspberry', 'blackberry', 'snozzleberry',
+ 'etc', 'etc..', 'etc...' ]
+
+ dlg = MultiChoiceDialog( self, lst,
+ "Pick some fruit from\nthis list",
+ "MultiChoiceDialog")
+
+
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenItems()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="MultiChoiceDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/MultiChoiceDialog.py~ b/pynxc/waxy/demos/MultiChoiceDialog.py~
new file mode 100644
index 0000000..bf53055
--- /dev/null
+++ b/pynxc/waxy/demos/MultiChoiceDialog.py~
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "MultiChoice Dialog",self.MultiChoice,default=True)
+ self.AddComponent(b,border=10)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def MultiChoice(self,event):
+
+
+ lst = [ 'apple', 'pear', 'banana', 'coconut', 'orange', 'grape', 'pineapple',
+ 'blueberry', 'raspberry', 'blackberry', 'snozzleberry',
+ 'etc', 'etc..', 'etc...' ]
+
+ dlg = MultiChoiceDialog( self, lst,
+ "Pick some fruit from\nthis list",
+ "MultiChoiceDialog")
+
+
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenMultiChoice()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="MultiChoiceDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/ProgressDialog.py b/pynxc/waxy/demos/ProgressDialog.py
new file mode 100755
index 0000000..0847d4d
--- /dev/null
+++ b/pynxc/waxy/demos/ProgressDialog.py
@@ -0,0 +1,76 @@
+#!/usr/bin/env python
+
+from waxy import *
+import time
+
+class MainFrame(Frame):
+ def Body(self):
+ self.AddComponent(Button(self, "one", self.OnClick))
+ self.AddComponent(Button(self, "two", self.OnClick2))
+ self.AddComponent(Button(self, "three", self.OnClick3))
+ self.Pack()
+
+ def OnClick(self, event):
+ # note: does not have a Cancel button, so it cannot be interrupted
+ dlg = ProgressDialog(self, title="Progress Dialog Test",
+ message="Counting from 1 to 1000", maximum=1000, modal=0)
+ dlg.Show()
+ cancel = True
+ for i in range(1000):
+ if i % 100 == 0:
+ keep_going,skip = dlg.Update(i, "Counting from " + str(i) + " to 1000")
+ else:
+ keep_going,skip = dlg.Update(i)
+
+
+ time.sleep(.01)
+ if not keep_going:
+ break
+ dlg.Destroy()
+
+ def OnClick2(self, event):
+ dlg = ProgressDialog(self, title="Progress Dialog Test",
+ message="Counting from 1 to 1000", maximum=1000, abort=1)
+ dlg.Show()
+ cancel = True
+ for i in range(1000):
+ if i % 100 == 0:
+ cancel = dlg.Update(i, "Counting from " + str(i) + " to 1000")
+ else:
+ cancel = dlg.Update(i)
+ time.sleep(.01)
+ if not cancel[0]:
+ break
+ dlg.Destroy()
+
+ def OnClick3(self, event):
+ dlg = ProgressDialog(self, title="Progress Dialog Test",
+ message="Counting from 1 to 1000", maximum=1000, abort=1,
+ show_elapsed_time=1,
+ #show_estimated_time=1,
+ show_remaining_time=1)
+ dlg.Show()
+ cancel = True
+ for i in range(1000):
+ if i % 100 == 0:
+ cancel = dlg.Update(i, "Counting from " + str(i) + " to 1000")
+ else:
+ cancel = dlg.Update(i)
+ time.sleep(.01)
+ if not cancel[0]:
+ dlg2 = MessageDialog(self, title="Continue?", text="Do you want to abort?", yes_no=1)
+ result = dlg2.ShowModal()
+ if result == "yes":
+ dlg.Destroy()
+ break
+ else:
+ dlg.Resume()
+ dlg2.Destroy()
+ if dlg:
+ dlg.Destroy()
+
+
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="ProgressDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/ProgressDialog.py~ b/pynxc/waxy/demos/ProgressDialog.py~
new file mode 100755
index 0000000..5fc5157
--- /dev/null
+++ b/pynxc/waxy/demos/ProgressDialog.py~
@@ -0,0 +1,76 @@
+#!/usr/bin/env python
+
+from waxy import *
+import time
+
+class MainFrame(Frame):
+ def Body(self):
+ self.AddComponent(Button(self, "one", self.OnClick))
+ self.AddComponent(Button(self, "two", self.OnClick2))
+ self.AddComponent(Button(self, "three", self.OnClick3))
+ self.Pack()
+
+ def OnClick(self, event):
+ # note: does not have a Cancel button, so it cannot be interrupted
+ dlg = ProgressDialog(self, title="Progress Dialog Test",
+ message="Counting from 1 to 1000", maximum=1000, modal=0)
+ dlg.Show()
+ cancel = True
+ for i in range(1000):
+ if i % 100 == 0:
+ keep_going,skip = dlg.Update(i, "Counting from " + str(i) + " to 1000")
+ else:
+ keep_going,skip = dlg.Update(i)
+
+
+ time.sleep(.01)
+ if not cancel[0]:
+ break
+ dlg.Destroy()
+
+ def OnClick2(self, event):
+ dlg = ProgressDialog(self, title="Progress Dialog Test",
+ message="Counting from 1 to 1000", maximum=1000, abort=1)
+ dlg.Show()
+ cancel = True
+ for i in range(1000):
+ if i % 100 == 0:
+ cancel = dlg.Update(i, "Counting from " + str(i) + " to 1000")
+ else:
+ cancel = dlg.Update(i)
+ time.sleep(.01)
+ if not cancel[0]:
+ break
+ dlg.Destroy()
+
+ def OnClick3(self, event):
+ dlg = ProgressDialog(self, title="Progress Dialog Test",
+ message="Counting from 1 to 1000", maximum=1000, abort=1,
+ show_elapsed_time=1,
+ #show_estimated_time=1,
+ show_remaining_time=1)
+ dlg.Show()
+ cancel = True
+ for i in range(1000):
+ if i % 100 == 0:
+ cancel = dlg.Update(i, "Counting from " + str(i) + " to 1000")
+ else:
+ cancel = dlg.Update(i)
+ time.sleep(.01)
+ if not cancel[0]:
+ dlg2 = MessageDialog(self, title="Continue?", text="Do you want to abort?", yes_no=1)
+ result = dlg2.ShowModal()
+ if result == "yes":
+ dlg.Destroy()
+ break
+ else:
+ dlg.Resume()
+ dlg2.Destroy()
+ if dlg:
+ dlg.Destroy()
+
+
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="ProgressDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/Rubberband.py b/pynxc/waxy/demos/Rubberband.py
new file mode 100755
index 0000000..316850a
--- /dev/null
+++ b/pynxc/waxy/demos/Rubberband.py
@@ -0,0 +1,221 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+# Modified from:
+# Recipe 189744: wxPython Graphics - Drawing rubberbands over a canvas
+# http://code.activestate.com/recipes/189744/
+
+class Sprite:
+ def __init__(self, fname):
+ self.bitmap = Image(fname+".png").ConvertToBitmap()
+ self.bitmap_rev = Image(fname+"rev.png").ConvertToBitmap()
+ self.background = None
+ self.pos=wx.Point(0,0)
+ self.reversed=False
+
+ def Draw(self, dc):
+ # copy background from dc to memory object
+ memdc = wx.MemoryDC()
+ memdc.Blit(0, 0, self.bitmap.GetWidth(), self.bitmap.GetHeight(), dc,
+ self.pos[0], self.pos[1], wx.COPY, 1)
+ self.background = memdc # keep around for later
+
+ bitmap = self.bitmap if not self.reversed else self.bitmap_rev
+ dc.DrawBitmap(bitmap, self.pos[0], self.pos[1], 1)
+
+ def HitTest(self, pt):
+ rect = self.GetRect()
+ return rect.InsideXY(pt.x, pt.y)
+
+ def Intersects(self, rect):
+ myrect = self.GetRect()
+
+ # adjust for negative
+ arect=[val for val in rect]
+ if arect[2]<0:
+ arect[0]+=arect[2]
+ arect[2]=-arect[2]
+
+ if arect[3]<0:
+ arect[1]+=arect[3]
+ arect[3]=-arect[3]
+
+ return myrect.Intersects(arect)
+
+ def GetRect(self):
+ return wx.Rect(self.pos[0], self.pos[1],
+ self.bitmap.GetWidth(), self.bitmap.GetHeight())
+
+class MyCanvas(Canvas):
+
+ def Init(self):
+ self.SetBackgroundColour('WHITE')
+ self.SetScrollbars(20, 20, 100, 100)
+ self.sprite = Sprite('heretic2')
+ self.sprite.pos=wx.Point(50,50)
+
+ # mouse selection start point
+ self.m_stpoint=Point(0,0)
+ # mouse selection end point
+ self.m_endpoint=Point(0,0)
+ # mouse selection cache point
+ self.m_savepoint=Point(0,0)
+
+ # flags for left click/ selection
+ self._leftclicked=False
+ self._selected=False
+
+ def OnLeftDown(self,event):
+
+ # Left mouse button down, change cursor to
+ # something else to denote event capture
+ self.m_stpoint = event.GetPosition()
+ self.SetCursor('cross')
+
+ # invalidate current canvas
+ self.Refresh()
+ # cache current position
+ self.m_savepoint = self.m_stpoint
+ self.m_endpoint = self.m_savepoint
+ self._selected = False
+ self._leftclicked = True
+
+
+ def OnMotion(self,event):
+
+ if event.Dragging() and self._leftclicked:
+
+ # Draw new rectangle
+ self.m_endpoint = event.GetPosition()
+ self.m_savepoint = self.m_endpoint # cache current endpoint
+ self.Refresh()
+
+ def OnLeftUp(self,event):
+
+ # User released left button, change cursor back
+ self.SetCursor('arrow')
+ self._selected = True #selection is done
+ self._leftclicked = False # end of clicking
+ self.Refresh()
+
+ def OnDraw(self, dc):
+ dc.SetTextForeground('BLUE')
+ dc.SetPen(wx.MEDIUM_GREY_PEN)
+ for i in range(50):
+ dc.DrawLine(0, i*10, i*10, 0)
+
+ w = (self.m_endpoint.x - self.m_stpoint.x)
+ h = (self.m_endpoint.y - self.m_stpoint.y)
+ rect=self.m_stpoint.x, self.m_stpoint.y, w, h
+
+ if self.sprite.Intersects(rect):
+ self.sprite.reversed=True
+ else:
+ self.sprite.reversed=False
+
+ self.sprite.Draw(dc)
+
+ if not self._selected:
+ dc.SetLogicalFunction(wx.XOR)
+ wbrush = wx.Brush(wx.Colour(255,255,255), wx.TRANSPARENT)
+ wpen = wx.Pen(wx.Colour(200, 200, 200), 1, wx.SOLID)
+ dc.SetBrush(wbrush)
+ dc.SetPen(wpen)
+
+
+ # Set clipping region to rectangle corners
+ # dc.SetClippingRegion(self.m_stpoint.x, self.m_stpoint.y, w,h)
+ dc.DrawRectangle(*rect)
+
+
+
+ def GetCurrentSelection(self):
+ """ Return the current selected rectangle """
+
+ # if there is no selection, selection defaults to
+ # current viewport
+
+ left = Point(0,0)
+ right = Point(0,0)
+
+ # user dragged mouse to right
+ if self.m_endpoint.y > self.m_stpoint.y:
+ right = self.m_endpoint
+ left = self.m_stpoint
+ # user dragged mouse to left
+ elif self.m_endpoint.y < self.m_stpoint.y:
+ right = self.m_stpoint
+ left = self.m_endpoint
+
+ return (left.x, left.y, right.x, right.y)
+
+
+ def ClearCurrentSelection(self):
+ """ Clear the current selected rectangle """
+
+ box = self.GetCurrentSelection()
+
+ dc=wx.ClientDC(self)
+
+ w = box[2] - box[0]
+ h = box[3] - box[1]
+ dc.SetClippingRegion(box[0], box[1], w, h)
+ dc.SetLogicalFunction(wx.XOR)
+
+ # The brush is not really needed since we
+ # dont do any filling of the dc. It is set for
+ # sake of completion.
+
+ wbrush = wx.Brush(wx.Colour(255,255,255), wx.TRANSPARENT)
+ wpen = wx.Pen(wx.Colour(200, 200, 200), 1, wx.SOLID)
+ dc.SetBrush(wbrush)
+ dc.SetPen(wpen)
+ dc.DrawRectangle(box[0], box[1], w,h)
+ self._selected = false
+
+ # reset selection to canvas size
+ self.ResetSelection()
+
+ def ResetSelection(self):
+ """ Resets the mouse selection to entire canvas """
+
+ self.m_stpoint = Point(0,0)
+ sz=self._canvas.GetSize()
+ w,h=sz.GetWidth(), sz.GetHeight()
+ self.m_endpoint = wxPoint(w,h)
+
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ self.canvas = MyCanvas(self)
+ self.canvas.SetSize((400,300))
+ self.AddComponent(self.canvas, expand=1, stretch=1)
+
+ self.Pack()
+
+ self.CenterOnScreen()
+
+ self.Show()
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+
+
+if __name__=="__main__":
+ app = Application(MainFrame, title=__file__)
+ app.Run()
diff --git a/pynxc/waxy/demos/SingleChoiceDialog.py b/pynxc/waxy/demos/SingleChoiceDialog.py
new file mode 100755
index 0000000..0c0815d
--- /dev/null
+++ b/pynxc/waxy/demos/SingleChoiceDialog.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MyDialog(SingleChoiceDialog):
+
+ def OnClick(self):
+
+ print "hello!"
+
+
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "SingleChoice Dialog",self.SingleChoice,default=True)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "Custom SingleChoice Dialog",self.SingleChoice2)
+ self.AddComponent(b,border=10)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def SingleChoice(self,event):
+
+
+ lst = [ 'apple', 'pear', 'banana', 'coconut', 'orange', 'grape', 'pineapple',
+ 'blueberry', 'raspberry', 'blackberry', 'snozzleberry',
+ 'etc', 'etc..', 'etc...' ]
+
+ dlg = SingleChoiceDialog( self, lst,
+ "Pick some fruit from\nthis list",
+ "SingleChoiceDialog")
+
+
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenItem()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def SingleChoice2(self,event):
+
+
+ lst = [ 'apple', 'pear', 'banana', 'coconut', 'orange', 'grape', 'pineapple',
+ 'blueberry', 'raspberry', 'blackberry', 'snozzleberry',
+ 'etc', 'etc..', 'etc...' ]
+
+ dlg = MyDialog( self, lst,
+ "Pick some fruit from\nthis list",
+ "SingleChoiceDialog")
+
+
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenItem()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="SingleChoiceDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/SingleChoiceDialog.py~ b/pynxc/waxy/demos/SingleChoiceDialog.py~
new file mode 100755
index 0000000..90eab15
--- /dev/null
+++ b/pynxc/waxy/demos/SingleChoiceDialog.py~
@@ -0,0 +1,89 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+class MyDialog(SingleChoiceDialog):
+
+ def OnClick(self):
+
+ print "hello!"
+
+
+
+class MainFrame(VerticalFrame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ b = Button(self, "SingleChoice Dialog",self.SingleChoice,default=True)
+ self.AddComponent(b,border=10)
+
+ b = Button(self, "Custom SingleChoice Dialog",self.SingleChoice2)
+ self.AddComponent(b,border=10)
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+
+ def SingleChoice(self,event):
+
+
+ lst = [ 'apple', 'pear', 'banana', 'coconut', 'orange', 'grape', 'pineapple',
+ 'blueberry', 'raspberry', 'blackberry', 'snozzleberry',
+ 'etc', 'etc..', 'etc...' ]
+
+ dlg = SingleChoiceDialog( self, lst,
+ "Pick some fruit from\nthis list",
+ "SingleChoiceDialog")
+
+
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetString()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+ def SingleChoice2(self,event):
+
+
+ lst = [ 'apple', 'pear', 'banana', 'coconut', 'orange', 'grape', 'pineapple',
+ 'blueberry', 'raspberry', 'blackberry', 'snozzleberry',
+ 'etc', 'etc..', 'etc...' ]
+
+ dlg = MyDialog( self, lst,
+ "Pick some fruit from\nthis list",
+ "SingleChoiceDialog")
+
+
+ res=dlg.ShowModal()
+
+ if res=='ok':
+ print "Ok: ",dlg.GetChosenItem()
+
+ else:
+ print "Canceled!"
+
+ dlg.Destroy()
+
+
+ def CloseWindow(self,event):
+ self.Close()
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="SingleChoiceDialog")
+ app.Run()
diff --git a/pynxc/waxy/demos/Slider.py b/pynxc/waxy/demos/Slider.py
new file mode 100755
index 0000000..8b53d8e
--- /dev/null
+++ b/pynxc/waxy/demos/Slider.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+
+from waxy import *
+import pylab
+import numpy
+
+def myfunc(x,params):
+
+ y=params[0]+params[1]*x+params[2]*x**2
+
+ return y
+
+
+def myplot(x,y):
+
+ pylab(x,y,'-o')
+ title('This is a test')
+ xlabel('This')
+ ylabel('That')
+
+ show()
+
+class MainFrame(VerticalFrame):
+
+ def Body(self):
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+
+ s=Slider(self,min=-5.0,max=5.0,tickfreq=0.1,ticks='bottom',labels=True)
+ self.AddComponent(s,stretch=True)
+ s=FloatSlider(self,min=-5.0,max=5.0,tickfreq=0.1,ticks='bottom',labels=True)
+ self.AddComponent(s,stretch=True)
+ self.Pack()
+ self.SetSize((640, 480))
+
+ def CloseWindow(self,event):
+ self.Close()
+
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="Slider Example")
+ app.Run()
+
diff --git a/pynxc/waxy/demos/TreeListView.py b/pynxc/waxy/demos/TreeListView.py
new file mode 100755
index 0000000..bf4bb66
--- /dev/null
+++ b/pynxc/waxy/demos/TreeListView.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+
+# treelistview-1.py
+
+from waxy import *
+import wx
+# uses wx at the moment; will be fixed later
+
+class MainFrame(Frame):
+
+ def CreateTreeListView(self, parent):
+ treelistview = TreeListView(parent,
+ columns=["Main column", "Column 1", "Column 2"],
+ has_buttons=1, lines=1)
+ treelistview.Size = (400, 400)
+
+ # add columns
+ treelistview.SetMainColumn(0)
+ treelistview.SetColumnWidth(0, 175)
+
+ self.root = treelistview.AddRoot("the root item")
+ treelistview.SetItemText(self.root, "col 1 root", 1)
+ treelistview.SetItemText(self.root, "col 2 root", 2)
+
+ for x in range(15):
+ text = "Item %d" % (x,)
+ child = treelistview.AppendItem(self.root, text)
+ # child is a TreeItemId or something like that :-(
+ treelistview.SetItemText(child, text + "(c1)", 1)
+ treelistview.SetItemText(child, text + "(c2)", 2)
+ # this should really be something like:
+ # child.SetText(column, text)
+ # or even:
+ # child[column] = text
+ # ...can we use a wrapper object without messing up everything?
+
+ for y in range(5):
+ text = "Item %d-%d" % (x, y)
+ last = treelistview.AppendItem(child, text)
+ treelistview.SetItemText(last, text + "(c1)", 1)
+ treelistview.SetItemText(last, text + "(c1)", 1)
+
+ return treelistview
+
+ def Body(self):
+ self.treelistview = self.CreateTreeListView(self)
+ self.AddComponent(self.treelistview, expand='both')
+ self.Pack()
+
+app = Application(MainFrame)
+app.Run()
diff --git a/pynxc/waxy/demos/TreeView.py b/pynxc/waxy/demos/TreeView.py
new file mode 100755
index 0000000..50ebe10
--- /dev/null
+++ b/pynxc/waxy/demos/TreeView.py
@@ -0,0 +1,91 @@
+#!/usr/bin/env python
+
+from waxy import *
+
+def filltree(tree):
+ root = tree.AddRoot("the root item")
+ for i in range(10):
+ child = tree.AppendItem(root, "Item %d" % (i,))
+ for j in range(5):
+ grandchild = tree.AppendItem(child, "Item %d" % (i*10+j))
+
+ print [x for x in tree.GetChildNodes(root)]
+
+ d = {
+ "Hans": {
+ "age": 30,
+ "sign": "Aquarius",
+ "job": "programmer",
+ },
+ "Fred": {
+ "age": "unknown",
+ "sign": "unknown",
+ "shoe size": "unknown",
+ },
+ "Old Guy John": {
+ "age": "old",
+ "sign": "Aquarius",
+ },
+ "Bob": {
+ "sign": "Taurus",
+ "job": "proprietor",
+ },
+ "Christine": {
+ "age": 17,
+ "sign": "Aries",
+ "job": "cashier",
+ }
+ }
+
+ stuff = tree.AppendItem(root, "stuff")
+ tree.LoadFromDict(stuff, d)
+
+ return tree
+
+
+
+class MainFrame(Frame): # frame has a sizer built in
+
+ def Body(self):
+
+ self.CenterOnScreen()
+
+ self.CreateStatusBar()
+ self.SetStatusText("This is the statusbar")
+
+ menubar = MenuBar(self)
+ menu1 = Menu(self)
+ menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
+ menubar.Append(menu1, "&File")
+
+ splitter = Splitter(self)
+ self.treeview = TreeView(splitter, twist_buttons=1, has_buttons=1)
+ self.treeview.OnSelectionChanged = self.OnTreeSelectionChanged
+ self.textbox = TextBox(splitter, multiline=1)
+ splitter.Split(self.treeview, self.textbox, direction='v')
+ self.AddComponent(splitter, expand='both')
+ filltree(self.treeview)
+
+
+
+ self.Pack()
+ self.SetSize((640, 480))
+
+ def CloseWindow(self,event):
+ self.Close()
+
+ def OnTreeSelectionChanged(self, event):
+ item = event.GetItem()
+ data = self.treeview.GetPyData(item)
+ if data is None:
+ data = self.treeview.GetItemText(item)
+ self.textbox.Clear()
+ self.textbox.AppendText(str(data))
+ event.Skip()
+
+
+
+
+if __name__=="__main__":
+ app = Application(MainFrame, title="TreeView")
+ app.Run()
diff --git a/pynxc/waxy/demos/heretic2.png b/pynxc/waxy/demos/heretic2.png
new file mode 100644
index 0000000..a4ede33
--- /dev/null
+++ b/pynxc/waxy/demos/heretic2.png
Binary files differ
diff --git a/pynxc/waxy/demos/heretic2rev.png b/pynxc/waxy/demos/heretic2rev.png
new file mode 100644
index 0000000..fa92a54
--- /dev/null
+++ b/pynxc/waxy/demos/heretic2rev.png
Binary files differ
diff --git a/pynxc/waxy/demos/plotit.py b/pynxc/waxy/demos/plotit.py
new file mode 100644
index 0000000..f5eb429
--- /dev/null
+++ b/pynxc/waxy/demos/plotit.py
@@ -0,0 +1,151 @@
+from numpy import *
+
+from waxy import *
+
+from matplotlib.backends.backend_wxagg import FigureCanvasWx as FigureCanvas
+from matplotlib.backends.backend_wx import FigureManager
+from matplotlib.figure import Figure
+from matplotlib.axes import Subplot
+
+def export_fig(parent,filename=None):
+ from matplotlib.backends.backend_pdf import FigureCanvasPdf
+
+ if not filename:
+
+# parent.canvas.Show(False)
+
+ dlg = FileDialog(parent, "Export to...",default_dir=os.getcwd(),
+ wildcard='PNG Files|*.png|PDF Files|*.pdf|EPS Files|*.eps|SVG Files|*.svg|All Files|*.*',save=1)
+ result = dlg.ShowModal()
+ if result == 'ok':
+ filename = dlg.GetPaths()[0]
+ else:
+ filename=None
+
+ filter_index=dlg.GetFilterIndex()
+
+ dlg.Destroy()
+
+ parent.canvas.Show(True)
+
+
+ if filename:
+
+ nme,ext=os.path.splitext(filename)
+ if not ext: # do the filter index
+ ext='.pdf'
+
+ print filter_index
+
+ if filter_index==0:
+ ext='.png'
+ elif filter_index==1:
+ ext='.pdf'
+ elif filter_index==2:
+ ext='.eps'
+
+ filename=nme+ext
+
+ nme,ext=os.path.splitext(filename)
+
+ if os.path.exists(filename):
+ dlg = MessageDialog(parent, '"%s" already exists. Do you want to replace it?' % filename,
+ 'A file or folder with the same name already exists in plasticity. Replacing it will overwrite its current contents.',icon='warning',cancel=1)
+ result = dlg.ShowModal()
+
+ if result=='cancel':
+ return
+
+ if ext=='.pdf': # hack to fix a problem in the pdf backend
+ orig_dpi = parent.fig.dpi.get()
+ canvas_pdf = FigureCanvasPdf(parent.fig)
+ parent.fig.savefig(filename)
+ parent.fig.set_canvas(parent.canvas) # restore automagically switched attributes
+ parent.fig.dpi.set(orig_dpi)
+ else:
+ parent.fig.savefig(filename)
+
+
+
+class MainFrame(Frame):
+
+ def __init__(self,x,y,style,parent=None):
+ self.fig=None
+ Frame.__init__(self,parent,'Plot','H',size=(750,750))
+ self.x=x
+ self.y=y
+ self.style=style
+ self.Plot()
+
+ def Body(self):
+
+ self.CreateMenu()
+ self.CenterOnScreen()
+
+ self.fig = Figure(figsize=(7,5),dpi=100)
+ self.canvas = FigureCanvas(self, -1, self.fig)
+ self.figmgr = FigureManager(self.canvas, 1, self)
+
+ self.axes = [self.fig.add_subplot(111)]
+
+ def CreateMenu(self):
+
+ menubar = MenuBar()
+
+ menu = Menu(self)
+ menu.Append("&Run", self.Run, "Run",hotkey="Ctrl+R")
+ menu.Append("Export Figure...", self.Export, "Export the Screen")
+ menu.Append("&Quit", self.Quit, "Quit",hotkey="Ctrl+Q")
+
+ menubar.Append(menu, "&File")
+ self.running=False
+
+ self.SetMenuBar(menubar)
+ self.CreateStatusBar()
+
+ def Export(self,event=None):
+ export_fig(self)
+
+
+ def Run(self,event=None):
+
+ if self.running:
+ self.y=self.y_bak
+ self.UpdatePlot()
+
+ else:
+ t=0.0
+ self.y_bak=self.y
+
+ for t in linspace(0,2*pi*4,100):
+ self.y=y*cos(t)
+ self.UpdatePlot()
+
+ self.running=not self.running
+
+ def UpdatePlot(self):
+ self.h.set_ydata(self.y)
+ self.canvas.draw()
+ self.canvas.gui_repaint()
+ wx.Yield()
+
+ def Plot(self):
+ self.axes[0].clear()
+ self.h,=self.axes[0].plot(self.x,self.y,self.style,linewidth=3)
+
+ self.canvas.draw()
+ self.canvas.gui_repaint()
+
+
+ def Quit(self,event=None):
+ self.Close()
+
+def Plot(x,y,style='-'):
+ app = Application(MainFrame,x,y,style)
+ app.Run()
+
+if __name__=="__main__":
+
+ x=linspace(-10,10,100)
+ y=x*sin(x)+x
+ Plot(x,y)
diff --git a/pynxc/waxy/dialog.py b/pynxc/waxy/dialog.py
new file mode 100644
index 0000000..df3397d
--- /dev/null
+++ b/pynxc/waxy/dialog.py
@@ -0,0 +1,115 @@
+# dialog.py
+
+# TODO: Pressing Esc in a dialog should be the same as Cancel...
+# (but how? using EVT_CHAR doesn't work... neither does OnCharHook.)
+
+import wx
+
+from button import Button
+from containers import Container
+from line import Line
+from panel import Panel
+from keys import keys
+import frame
+
+# TODO: styles (same as Frame?)
+
+class Dialog(wx.Dialog, Container):
+
+ __events__ = frame.Frame.__events__.copy()
+ __events__ .update({
+ 'CharHook': wx.EVT_CHAR_HOOK,
+ })
+
+ def __init__(self, parent, title, cancel_button=1):
+ wx.Dialog.__init__(self, parent, wx.NewId(), title, wx.DefaultPosition)
+
+ # this should create self.sizer and give access to self.AddComponent
+ self._create_sizer('vertical')
+
+ # enter stuff here...
+ self.Body()
+
+ # add line and buttons pane...
+ line = Line(self, size=(20,-1), direction='horizontal')
+ self.AddComponent(line, align='center', stretch=1, border=5)
+
+ panel = self.AddButtonPanel(cancel_button)
+
+ self.BindEvents()
+
+ panel.Pack()
+ self.AddComponent(panel)
+
+ self.Pack()
+
+ self.Centre()
+
+ def AddButtonPanel(self, cancel_button=1):
+ panel = Panel(self, direction='horizontal')
+ self.okbutton = Button(panel, "OK", event=self.OnClickOKButton)
+ self.okbutton.SetDefault()
+ panel.AddComponent(self.okbutton, expand=1, border=5)
+
+ if cancel_button:
+ cancelbutton = Button(panel, "Cancel", event=self.OnClickCancelButton)
+ panel.AddComponent(cancelbutton, expand=1, border=5)
+ return panel
+
+ def OnClickOKButton(self, event=None):
+ if self.Validate():
+ event.Skip()
+ # only close the dialog if we validate:
+ self.EndModal(wx.ID_OK)
+ else:
+ self.OnValidateError(event)
+
+ def OnClickCancelButton(self, event=None):
+ self.EndModal(wx.ID_CANCEL)
+
+ def OnValidateError(self, event=None):
+ """ Override this to take action when the input does not validate.
+ (E.g. display an error message, etc.) """
+
+ def ShowModal(self):
+ """ Show the dialog modally. Returns 'ok' or 'cancel'. """
+ r = wx.Dialog.ShowModal(self)
+ return {
+ wx.ID_OK: "ok",
+ wx.ID_CANCEL: "cancel",
+ }.get(r, "?")
+
+ def Body(self):
+ # override this
+ # NOTE: do not call self.Pack() here, it's called automatically later
+ return None
+
+ def Validate(self):
+ """ Override this to validate input and return 0 if it's not correct.
+ Return 1 otherwise. """
+ return 1
+
+ # this doesn't work -- or does it?:
+ def OnCharHook(self, event=None):
+ if event.GetKeyCode() == keys.esc:
+ self.OnClickCancelButton()
+ event.Skip()
+
+
+def showdialog(dialogclass, *args, **kwargs):
+ """ Easy function to call dialogs and clean up when it's done. """
+ dlg = dialogclass(*args, **kwargs)
+ try:
+ result = dlg.ShowModal()
+ finally:
+ dlg.Destroy()
+
+ if result=='ok':
+ val=dlg.GetValue()
+ else:
+ val=None
+
+ dlg.Destroy()
+
+ return val
+
diff --git a/pynxc/waxy/dialog.py~ b/pynxc/waxy/dialog.py~
new file mode 100644
index 0000000..48444ab
--- /dev/null
+++ b/pynxc/waxy/dialog.py~
@@ -0,0 +1,108 @@
+# dialog.py
+
+# TODO: Pressing Esc in a dialog should be the same as Cancel...
+# (but how? using EVT_CHAR doesn't work... neither does OnCharHook.)
+
+import wx
+
+from button import Button
+from containers import Container
+from line import Line
+from panel import Panel
+from keys import keys
+import frame
+
+# TODO: styles (same as Frame?)
+
+class Dialog(wx.Dialog, Container):
+
+ __events__ = frame.Frame.__events__.copy()
+ __events__ .update({
+ 'CharHook': wx.EVT_CHAR_HOOK,
+ })
+
+ def __init__(self, parent, title, cancel_button=1):
+ wx.Dialog.__init__(self, parent, wx.NewId(), title, wx.DefaultPosition)
+
+ # this should create self.sizer and give access to self.AddComponent
+ self._create_sizer('vertical')
+ self.SetDefaultFont()
+
+ # enter stuff here...
+ self.Body()
+
+ # add line and buttons pane...
+ line = Line(self, size=(20,-1), direction='horizontal')
+ self.AddComponent(line, align='center', stretch=1, border=5)
+
+ panel = self.AddButtonPanel(cancel_button)
+
+ self.BindEvents()
+
+ panel.Pack()
+ self.AddComponent(panel)
+
+ self.Pack()
+
+ self.Centre()
+
+ def AddButtonPanel(self, cancel_button=1):
+ panel = Panel(self, direction='horizontal')
+ self.okbutton = Button(panel, "OK", event=self.OnClickOKButton)
+ self.okbutton.SetDefault()
+ panel.AddComponent(self.okbutton, expand=1, border=5)
+
+ if cancel_button:
+ cancelbutton = Button(panel, "Cancel", event=self.OnClickCancelButton)
+ panel.AddComponent(cancelbutton, expand=1, border=5)
+ return panel
+
+ def OnClickOKButton(self, event=None):
+ if self.Validate():
+ event.Skip()
+ # only close the dialog if we validate:
+ self.EndModal(wx.ID_OK)
+ else:
+ self.OnValidateError(event)
+
+ def OnClickCancelButton(self, event=None):
+ self.EndModal(wx.ID_CANCEL)
+
+ def OnValidateError(self, event=None):
+ """ Override this to take action when the input does not validate.
+ (E.g. display an error message, etc.) """
+
+ def ShowModal(self):
+ """ Show the dialog modally. Returns 'ok' or 'cancel'. """
+ r = wx.Dialog.ShowModal(self)
+ return {
+ wx.ID_OK: "ok",
+ wx.ID_CANCEL: "cancel",
+ }.get(r, "?")
+
+ def Body(self):
+ # override this
+ # NOTE: do not call self.Pack() here, it's called automatically later
+ return None
+
+ def Validate(self):
+ """ Override this to validate input and return 0 if it's not correct.
+ Return 1 otherwise. """
+ return 1
+
+ # this doesn't work -- or does it?:
+ def OnCharHook(self, event=None):
+ if event.GetKeyCode() == keys.esc:
+ self.OnClickCancelButton()
+ event.Skip()
+
+
+def showdialog(dialogclass, *args, **kwargs):
+ """ Easy function to call dialogs and clean up when it's done. """
+ dlg = dialogclass(*args, **kwargs)
+ try:
+ result = dlg.ShowModal()
+ finally:
+ dlg.Destroy()
+ return result
+
diff --git a/pynxc/waxy/directorydialog.py b/pynxc/waxy/directorydialog.py
new file mode 100644
index 0000000..5d1ee2a
--- /dev/null
+++ b/pynxc/waxy/directorydialog.py
@@ -0,0 +1,39 @@
+# choosedirectory.py
+
+import wx
+import waxyobject
+
+class DirectoryDialog(wx.DirDialog, waxyobject.WaxyObject):
+
+ def __init__(self, parent, title="Choose a directory", new_dir_button=0):
+ style = wx.DD_DEFAULT_STYLE
+ if new_dir_button:
+ style |= wx.DD_NEW_DIR_BUTTON
+ wx.DirDialog.__init__(self, parent, title, style=style)
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.DirDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+ def GetChosenDirectory(self):
+ """ Shorthand... """
+ path = self.GetPath()
+ return path
+
+
+# EXPERIMENTAL:
+# This dialog returns a list of filenames (dlg.GetPath()) upon success, and
+# an empty list upon cancel.
+
+def ChooseDirectory(parent, title="Choose a directory", new_dir_button=0):
+ path = None
+ dlg = DirectoryDialog(parent, title=title, new_dir_button=new_dir_button)
+ if dlg.ShowModal() == 'ok':
+ path = dlg.GetPath()
+ dlg.Destroy()
+ return path
+
diff --git a/pynxc/waxy/directorydialog.py~ b/pynxc/waxy/directorydialog.py~
new file mode 100644
index 0000000..866a238
--- /dev/null
+++ b/pynxc/waxy/directorydialog.py~
@@ -0,0 +1,40 @@
+# choosedirectory.py
+
+import wx
+import waxyobject
+
+class DirectoryDialog(wx.DirDialog, waxyobject.WaxyObject):
+
+ def __init__(self, parent, title="Choose a directory", new_dir_button=0):
+ style = wx.DD_DEFAULT_STYLE
+ if new_dir_button:
+ style |= wx.DD_NEW_DIR_BUTTON
+ wx.DirDialog.__init__(self, parent, title, style=style)
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.DirDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+ def GetChosenDirectory(self):
+ """ Shorthand... """
+ data = self.GetColourData()
+ color = data.GetColour().Get()
+ return color
+
+
+# EXPERIMENTAL:
+# This dialog returns a list of filenames (dlg.GetPath()) upon success, and
+# an empty list upon cancel.
+
+def ChooseDirectory(parent, title="Choose a directory", new_dir_button=0):
+ path = None
+ dlg = DirectoryDialog(parent, title=title, new_dir_button=new_dir_button)
+ if dlg.ShowModal() == 'ok':
+ path = dlg.GetPath()
+ dlg.Destroy()
+ return path
+
diff --git a/pynxc/waxy/dragdrop.py b/pynxc/waxy/dragdrop.py
new file mode 100644
index 0000000..05a5965
--- /dev/null
+++ b/pynxc/waxy/dragdrop.py
@@ -0,0 +1,63 @@
+# dragdrop.py
+
+import wx
+
+# These can be overridden if you need them:
+# def OnDropFiles(self, x, y, files)
+# def OnDragOver(self, x, y, d)
+# def OnEnter(self, x, y, d)
+# def OnLeave(self, d)
+#
+# However, for most purposes, it will probably suffice to pass in a function
+# to <event>.
+
+class FileDropTarget(wx.FileDropTarget):
+ # note that you can pass an event, or just override OnDropFiles.
+
+ def __init__(self, window, event=None):
+ wx.FileDropTarget.__init__(self)
+ self.window = window
+ self.event = event
+ self.window.SetDropTarget(self)
+
+ def OnDropFiles(self, x, y, filenames):
+ if self.event:
+ self.event(x, y, filenames)
+
+class TextDropTarget(wx.TextDropTarget):
+
+ def __init__(self, window, event=None):
+ wx.TextDropTarget.__init__(self)
+ self.window = window
+ self.event = event
+ self.window.SetDropTarget(self)
+
+ def OnDropText(self, x, y, text):
+ if self.event:
+ self.event(x, y, text)
+
+ def OnDragOver(self, x, y, d):
+ return wx.DragCopy
+ # XXX not sure what this does; copied from wxPython demo
+
+class URLDropTarget(wx.PyDropTarget):
+
+ def __init__(self, window, event=None):
+ # <event> is a function with signature (x, y, d, url).
+ wx.PyDropTarget.__init__(self)
+ self.window = window
+ self.event = event
+ self.window.SetDropTarget(self)
+ self.data = wx.URLDataObject()
+ self.SetDataObject(self.data)
+
+ def OnDragOver(self, x, y, d):
+ return wx.DragLink
+
+ def OnData(self, x, y, d):
+ if not self.GetData():
+ return wx.DragNone
+ url = self.data.GetURL()
+ if self.event:
+ self.event(x, y, d, url)
+ return d
diff --git a/pynxc/waxy/dropdownbox.py b/pynxc/waxy/dropdownbox.py
new file mode 100644
index 0000000..345351a
--- /dev/null
+++ b/pynxc/waxy/dropdownbox.py
@@ -0,0 +1,40 @@
+# dropdownbox.py
+
+# todo: styles... if any?
+
+import wx
+import containers
+import waxyobject
+import styles
+
+class DropDownBox(wx.Choice, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Select': wx.EVT_CHOICE,
+ }
+
+ def __init__(self, parent, choices=[], size=None, **kwargs):
+ style = 0
+ style |= styles.window(kwargs)
+ wx.Choice.__init__(self, parent, wx.NewId(), choices=choices,
+ size=size or (-1,-1), style=style)
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ def SetItems(self, items):
+ """ Clear the internal list of items, and set new items. <items> is
+ a list of 2-tuples (string, data). """
+ self.Clear()
+ for s, data in items:
+ self.Append(s, data)
+
+ def GetItems(self):
+ """ Return a list of 2-tuples (string, data). """
+ items = []
+ for i in range(self.GetCount()):
+ s = self.GetString(i)
+ data = self.GetClientData(i)
+ items.append((s, data))
+ return items
+
diff --git a/pynxc/waxy/dropdownbox.py~ b/pynxc/waxy/dropdownbox.py~
new file mode 100644
index 0000000..4598ff2
--- /dev/null
+++ b/pynxc/waxy/dropdownbox.py~
@@ -0,0 +1,40 @@
+# dropdownbox.py
+
+# todo: styles... if any?
+
+import wx
+import containers
+import waxyobject
+import styles
+
+class DropDownBox(wx.Choice, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Select': wx.EVT_CHOICE,
+ }
+
+ def __init__(self, parent, choices=[], size=None, **kwargs):
+ style = 0
+ style |= styles.window(kwargs)
+ wx.Choice.__init__(self, parent, wx.NewId(), choices=choices,
+ size=size or (-1,-1), style=style)
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ def SetItems(self, items):
+ """ Clear the internal list of items, and set new items. <items> is
+ a list of 2-tuples (string, data). """
+ self.Clear()
+ for s, data in items:
+ self.Append(s, data)
+
+ def GetItems(self):
+ """ Return a list of 2-tuples (string, data). """
+ items = []
+ for i in range(self.GetCount()):
+ s = self.GetString(i)
+ data = self.GetClientData(i)
+ items.append((s, data))
+ return items
+
diff --git a/pynxc/waxy/events.py b/pynxc/waxy/events.py
new file mode 100644
index 0000000..a9fef15
--- /dev/null
+++ b/pynxc/waxy/events.py
@@ -0,0 +1,33 @@
+# events.py
+
+import wx
+
+# place general events here... OnEnter, OnExit, mouse events, etc.
+
+events = {
+ 'Enter': wx.EVT_ENTER_WINDOW, # entering a control w/ mouse pointer
+ 'EnterWindow': wx.EVT_ENTER_WINDOW,
+ 'Exit': wx.EVT_LEAVE_WINDOW, # leaving a control w/ mouse pointer
+ 'ExitWindow': wx.EVT_LEAVE_WINDOW,
+ 'GetFocus': wx.EVT_SET_FOCUS, # control gets focus
+ 'HotKey': wx.EVT_HOTKEY,
+ 'KeyDown': wx.EVT_KEY_DOWN,
+ 'KeyUp': wx.EVT_KEY_UP,
+ 'Leave': wx.EVT_LEAVE_WINDOW,
+ 'LeaveWindow': wx.EVT_LEAVE_WINDOW,
+ 'LeftClick': wx.EVT_LEFT_DOWN, # alias for LeftDown
+ 'LeftDown': wx.EVT_LEFT_DOWN,
+ 'LeftDoubleClick': wx.EVT_LEFT_DCLICK,
+ 'LeftUp': wx.EVT_LEFT_UP,
+ 'LoseFocus': wx.EVT_KILL_FOCUS, # control loses focus
+ 'Motion':wx.EVT_MOTION,
+ 'Move': wx.EVT_MOVE, # window is moved. is this for frames only?
+ 'Resize': wx.EVT_SIZE, # same as 'Size'
+ 'RightClick': wx.EVT_RIGHT_DOWN, # alias for RightDown
+ 'RightDown': wx.EVT_RIGHT_DOWN,
+ 'RightUp': wx.EVT_RIGHT_UP,
+ 'Size': wx.EVT_SIZE, # window is resized
+
+ # NOTE: OnPaint should really be here, but this causes problems with the
+ # OnPaint method that many controls already have.
+}
diff --git a/pynxc/waxy/filedialog.py b/pynxc/waxy/filedialog.py
new file mode 100644
index 0000000..a11141e
--- /dev/null
+++ b/pynxc/waxy/filedialog.py
@@ -0,0 +1,40 @@
+# filedialog.py
+
+import wx
+import waxyobject
+
+class FileDialog(wx.FileDialog, waxyobject.WaxyObject):
+
+ def __init__(self, parent, title="Choose a file", default_dir="",
+ default_file="", wildcard="*.*", open=0, save=0, multiple=0):
+ style = 0
+ if open:
+ style |= wx.OPEN
+ elif save:
+ style |= wx.SAVE
+ style |= wx.OVERWRITE_PROMPT
+ if multiple:
+ style |= wx.MULTIPLE
+
+ self.multiple=multiple
+
+ wx.FileDialog.__init__(self, parent, title, default_dir, default_file,
+ wildcard, style)
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.FileDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+
+ def GetChosenFile(self):
+ """ Shorthand... """
+ data = self.GetPaths()
+
+ if not self.multiple:
+ data=data[0]
+
+ return data
diff --git a/pynxc/waxy/filedialog.py~ b/pynxc/waxy/filedialog.py~
new file mode 100644
index 0000000..2bfd2b6
--- /dev/null
+++ b/pynxc/waxy/filedialog.py~
@@ -0,0 +1,35 @@
+# filedialog.py
+
+import wx
+import waxyobject
+
+class FileDialog(wx.FileDialog, waxyobject.WaxyObject):
+
+ def __init__(self, parent, title="Choose a file", default_dir="",
+ default_file="", wildcard="*.*", open=0, save=0, multiple=0):
+ style = 0
+ if open:
+ style |= wx.OPEN
+ elif save:
+ style |= wx.SAVE
+ if multiple:
+ style |= wx.MULTIPLE
+
+ self.multiple=multiple
+
+ wx.FileDialog.__init__(self, parent, title, default_dir, default_file,
+ wildcard, style)
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.FileDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+
+ def GetChosenFile(self):
+ """ Shorthand... """
+ data = self.GetPaths()
+ return data
diff --git a/pynxc/waxy/flexgridpanel.py b/pynxc/waxy/flexgridpanel.py
new file mode 100644
index 0000000..b8e5a90
--- /dev/null
+++ b/pynxc/waxy/flexgridpanel.py
@@ -0,0 +1,21 @@
+# flexgridpanel.py
+
+# todo: styles
+
+import wx
+import containers
+import panel
+
+class FlexGridPanel(wx.Panel, containers.FlexGridContainer):
+ """ Sub-level containers inside a frame, used for layout. """
+ def __init__(self, parent=None, rows=3, cols=3, hgap=1, vgap=1,
+ growable_cols=(), growable_rows=()):
+ wx.Panel.__init__(self, parent or NULL, wx.NewId())
+
+ self._create_sizer(rows, cols, hgap, vgap)
+ self.Body()
+
+ for col in growable_cols:
+ self.AddGrowableCol(col)
+ for row in growable_rows:
+ self.AddGrowableRow(row)
diff --git a/pynxc/waxy/flexgridpanel.py~ b/pynxc/waxy/flexgridpanel.py~
new file mode 100644
index 0000000..751e1ae
--- /dev/null
+++ b/pynxc/waxy/flexgridpanel.py~
@@ -0,0 +1,22 @@
+# flexgridpanel.py
+
+# todo: styles
+
+import wx
+import containers
+import panel
+
+class FlexGridPanel(wx.Panel, containers.FlexGridContainer):
+ """ Sub-level containers inside a frame, used for layout. """
+ def __init__(self, parent=None, rows=3, cols=3, hgap=1, vgap=1,
+ growable_cols=(), growable_rows=()):
+ wx.Panel.__init__(self, parent or NULL, wx.NewId())
+
+ self._create_sizer(rows, cols, hgap, vgap)
+ self.SetDefaultFont()
+ self.Body()
+
+ for col in growable_cols:
+ self.AddGrowableCol(col)
+ for row in growable_rows:
+ self.AddGrowableRow(row)
diff --git a/pynxc/waxy/font.py b/pynxc/waxy/font.py
new file mode 100644
index 0000000..aefd228
--- /dev/null
+++ b/pynxc/waxy/font.py
@@ -0,0 +1,18 @@
+# font.py
+
+import wx
+
+class Font(wx.Font):
+ def __init__(self, name, size, italic=0, bold=0, underline=0):
+ wx.Font.__init__(self, size, wx.DEFAULT, italic and wx.ITALIC or wx.NORMAL,
+ bold and wx.BOLD or wx.NORMAL, underline, name)
+
+ def IsItalic(self):
+ return bool(self.GetStyle() & wx.ITALIC)
+
+ def IsBold(self):
+ return bool(self.GetWeight() & wx.BOLD)
+
+ def IsUnderlined(self):
+ return self.GetUnderlined()
+
diff --git a/pynxc/waxy/fontdialog.py b/pynxc/waxy/fontdialog.py
new file mode 100644
index 0000000..f4fbdca
--- /dev/null
+++ b/pynxc/waxy/fontdialog.py
@@ -0,0 +1,35 @@
+# fontdialog.py
+
+# Issues:
+# objects' GetFont() really should return a Font instance, not a wx.Font
+
+import wx
+import waxyobject
+from font import Font
+
+class FontDialog(wx.FontDialog, waxyobject.WaxyObject):
+
+ def __init__(self, parent, data=None):
+ if not data:
+ data = wx.FontData()
+ wx.FontDialog.__init__(self, parent, data)
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.FontDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+ def GetChosenFont(self):
+ """ Shorthand... """
+ data = self.GetFontData()
+ font = data.GetChosenFont()
+ font.__class__ = Font
+ return font
+
+# XXX
+# Problem: GetFontData() returns a wxFontData object, whose GetChosenFont()
+# still returns a wxFont, not a wax Font. No big deal really, but inconsistent.
+# Maybe Wax needs its own FontData class...?
diff --git a/pynxc/waxy/fontdialog.py~ b/pynxc/waxy/fontdialog.py~
new file mode 100644
index 0000000..e6959c0
--- /dev/null
+++ b/pynxc/waxy/fontdialog.py~
@@ -0,0 +1,35 @@
+# fontdialog.py
+
+# Issues:
+# objects' GetFont() really should return a Font instance, not a wx.Font
+
+import wx
+import waxyobject
+from font import Font
+
+class FontDialog(wx.FontDialog, waxobject.WaxObject):
+
+ def __init__(self, parent, data=None):
+ if not data:
+ data = wx.FontData()
+ wx.FontDialog.__init__(self, parent, data)
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.FontDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+ def GetChosenFont(self):
+ """ Shorthand... """
+ data = self.GetFontData()
+ font = data.GetChosenFont()
+ font.__class__ = Font
+ return font
+
+# XXX
+# Problem: GetFontData() returns a wxFontData object, whose GetChosenFont()
+# still returns a wxFont, not a wax Font. No big deal really, but inconsistent.
+# Maybe Wax needs its own FontData class...?
diff --git a/pynxc/waxy/frame.py b/pynxc/waxy/frame.py
new file mode 100644
index 0000000..c98e932
--- /dev/null
+++ b/pynxc/waxy/frame.py
@@ -0,0 +1,67 @@
+# frame.py
+
+import wx
+import containers
+import styles
+
+class Frame(wx.Frame, containers.Container):
+ """ Top-level frame (window) with built-in sizer. """
+
+ __events__ = {
+ 'Close': wx.EVT_CLOSE,
+ 'Iconize': wx.EVT_ICONIZE,
+ 'Show': wx.EVT_SHOW,
+ 'Activate': wx.EVT_ACTIVATE,
+ 'Idle': wx.EVT_IDLE,
+ # some of these might be better off in events.py?
+
+ 'MenuHighlight': wx.EVT_MENU_HIGHLIGHT,
+ }
+
+ def __init__(self, parent=None, title="", direction="H", size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.Frame.__init__(self, parent, wx.NewId(), title, size=size or (-1,-1),
+ style=style)
+
+ self.BindEvents()
+
+ self._create_sizer(direction)
+ styles.properties(self, kwargs)
+ self.Body()
+
+ def SetIcon(self, obj):
+ """ Like wx.Frame.SetIcon, but also accepts a path to an icon file. """
+ if isinstance(obj, str) or isinstance(obj, unicode):
+ obj = wx.Icon(obj, wx.BITMAP_TYPE_ICO) # FIXME
+ wx.Frame.SetIcon(self, obj)
+
+ #
+ # style parameters
+
+ def _params(self, kwargs):
+ flags = wx.DEFAULT_FRAME_STYLE
+
+ # REMOVE this flag if resize=0:
+ flags &= ~(styles.styleboolexclude('resize', wx.RESIZE_BORDER, kwargs, reverse=1))
+ flags &= ~(styles.styleboolexclude('close_box', wx.CLOSE_BOX, kwargs, reverse=1))
+ flags &= ~(styles.styleboolexclude('minimize_box', wx.MINIMIZE_BOX, kwargs, reverse=1))
+ flags &= ~(styles.styleboolexclude('maximize_box', wx.MAXIMIZE_BOX, kwargs, reverse=1))
+
+ flags |= styles.stylebool('shaped', wx.FRAME_SHAPED, kwargs)
+ flags |= styles.stylebool('stayontop', wx.STAY_ON_TOP, kwargs)
+ flags |= styles.stylebool('stay_on_top', wx.STAY_ON_TOP, kwargs)
+ return flags
+
+
+
+class HorizontalFrame(Frame):
+ def __init__(self, parent=None, title="", *args, **kwargs):
+ Frame.__init__(self, parent=parent, title=title, direction='h', *args, **kwargs)
+
+class VerticalFrame(Frame):
+ def __init__(self, parent=None, title="", *args, **kwargs):
+ Frame.__init__(self, parent=parent, title=title, direction='v', *args, **kwargs)
+
diff --git a/pynxc/waxy/frame.py~ b/pynxc/waxy/frame.py~
new file mode 100644
index 0000000..0758ca2
--- /dev/null
+++ b/pynxc/waxy/frame.py~
@@ -0,0 +1,68 @@
+# frame.py
+
+import wx
+import containers
+import styles
+
+class Frame(wx.Frame, containers.Container):
+ """ Top-level frame (window) with built-in sizer. """
+
+ __events__ = {
+ 'Close': wx.EVT_CLOSE,
+ 'Iconize': wx.EVT_ICONIZE,
+ 'Show': wx.EVT_SHOW,
+ 'Activate': wx.EVT_ACTIVATE,
+ 'Idle': wx.EVT_IDLE,
+ # some of these might be better off in events.py?
+
+ 'MenuHighlight': wx.EVT_MENU_HIGHLIGHT,
+ }
+
+ def __init__(self, parent=None, title="", direction="H", size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.Frame.__init__(self, parent, wx.NewId(), title, size=size or (-1,-1),
+ style=style)
+
+ self.BindEvents()
+
+ self._create_sizer(direction)
+# self.SetDefaultFont()
+ styles.properties(self, kwargs)
+ self.Body()
+
+ def SetIcon(self, obj):
+ """ Like wx.Frame.SetIcon, but also accepts a path to an icon file. """
+ if isinstance(obj, str) or isinstance(obj, unicode):
+ obj = wx.Icon(obj, wx.BITMAP_TYPE_ICO) # FIXME
+ wx.Frame.SetIcon(self, obj)
+
+ #
+ # style parameters
+
+ def _params(self, kwargs):
+ flags = wx.DEFAULT_FRAME_STYLE
+
+ # REMOVE this flag if resize=0:
+ flags &= ~(styles.styleboolexclude('resize', wx.RESIZE_BORDER, kwargs, reverse=1))
+ flags &= ~(styles.styleboolexclude('close_box', wx.CLOSE_BOX, kwargs, reverse=1))
+ flags &= ~(styles.styleboolexclude('minimize_box', wx.MINIMIZE_BOX, kwargs, reverse=1))
+ flags &= ~(styles.styleboolexclude('maximize_box', wx.MAXIMIZE_BOX, kwargs, reverse=1))
+
+ flags |= styles.stylebool('shaped', wx.FRAME_SHAPED, kwargs)
+ flags |= styles.stylebool('stayontop', wx.STAY_ON_TOP, kwargs)
+ flags |= styles.stylebool('stay_on_top', wx.STAY_ON_TOP, kwargs)
+ return flags
+
+
+
+class HorizontalFrame(Frame):
+ def __init__(self, parent=None, title="", *args, **kwargs):
+ Frame.__init__(self, parent=parent, title=title, direction='h', *args, **kwargs)
+
+class VerticalFrame(Frame):
+ def __init__(self, parent=None, title="", *args, **kwargs):
+ Frame.__init__(self, parent=parent, title=title, direction='v', *args, **kwargs)
+
diff --git a/pynxc/waxy/grid.py b/pynxc/waxy/grid.py
new file mode 100644
index 0000000..e246405
--- /dev/null
+++ b/pynxc/waxy/grid.py
@@ -0,0 +1,52 @@
+# grid.py
+
+# todo: styles
+
+import wx
+import wx.grid as gridlib
+import waxyobject
+
+class Grid(gridlib.Grid, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'CellLeftClick': gridlib.EVT_GRID_CELL_LEFT_CLICK,
+ 'CellRightClick': gridlib.EVT_GRID_CELL_RIGHT_CLICK,
+ 'CellLeftDoubleClick': gridlib.EVT_GRID_CELL_LEFT_DCLICK,
+ 'CellRightDoubleClick': gridlib.EVT_GRID_CELL_RIGHT_DCLICK,
+ 'LabelLeftClick': gridlib.EVT_GRID_LABEL_LEFT_CLICK,
+ 'LabelRightClick': gridlib.EVT_GRID_LABEL_RIGHT_CLICK,
+ 'LabelLeftDoubleClick': gridlib.EVT_GRID_LABEL_LEFT_DCLICK,
+ 'LabelRightDoubleClick': gridlib.EVT_GRID_LABEL_RIGHT_DCLICK,
+ 'RowSize': gridlib.EVT_GRID_ROW_SIZE,
+ 'ColSize': gridlib.EVT_GRID_COL_SIZE,
+ 'RangeSelect': gridlib.EVT_GRID_RANGE_SELECT,
+ 'CellChange':gridlib.EVT_GRID_CELL_CHANGE,
+ 'SelectCell':gridlib.EVT_GRID_SELECT_CELL,
+ 'EditorShown':gridlib.EVT_GRID_EDITOR_SHOWN,
+ 'EditorHidden':gridlib.EVT_GRID_EDITOR_HIDDEN,
+ 'EditorCreated':gridlib.EVT_GRID_EDITOR_CREATED,
+ }
+
+
+ def __init__(self, parent, numrows=10, numcolumns=10):
+ gridlib.Grid.__init__(self, parent, wx.NewId())
+ self.CreateGrid(numrows, numcolumns)
+ self.BindEvents()
+
+ def SetGlobalSize(self, rowsize, colsize):
+ """ Set all cells to the same size. """
+ for i in range(self.GetNumberRows()):
+ self.SetRowSize(i, rowsize)
+ for i in range(self.GetNumberCols()):
+ self.SetColSize(i, colsize)
+
+ def __setitem__(self, index, value):
+ assert isinstance(index, tuple) and len(index) == 2
+ row, column = index
+ self.SetCellValue(row, column, value)
+
+ def __getitem__(self, index):
+ assert isinstance(index, tuple) and len(index) == 2
+ row, column = index
+ return self.GetCellValue(row, column)
+
diff --git a/pynxc/waxy/gridpanel.py b/pynxc/waxy/gridpanel.py
new file mode 100644
index 0000000..ff010af
--- /dev/null
+++ b/pynxc/waxy/gridpanel.py
@@ -0,0 +1,24 @@
+# gridpanel.py
+
+# todo: styles
+
+import wx
+import containers
+import panel
+
+class GridPanel(wx.Panel, containers.GridContainer):
+ """ Sub-level containers inside a frame, used for layout. """
+ def __init__(self, parent=None, rows=3, cols=3, hgap=1, vgap=1):
+ wx.Panel.__init__(self, parent or NULL, wx.NewId())
+
+ self._create_sizer(rows, cols, hgap, vgap)
+# self.SetDefaultFont()
+ self.Body()
+
+'''
+NOTES
+
+1. Do not make the mistake of writing GridPanel(parent, 3, 3, 2, 2) or something
+like that. This will work, but set the *title* to 3, and cols to 2.
+Why does a Panel need a title anyway? Can we remove this?
+'''
diff --git a/pynxc/waxy/htmlwindow.py b/pynxc/waxy/htmlwindow.py
new file mode 100644
index 0000000..4453bcd
--- /dev/null
+++ b/pynxc/waxy/htmlwindow.py
@@ -0,0 +1,15 @@
+# htmlwindow.py
+
+# todo: styles
+
+import wx.html
+import waxyobject
+
+class HTMLWindow(wx.html.HtmlWindow, waxyobject.WaxyObject):
+
+ def __init__(self, parent, fullrepaint=1):
+ style = 0
+ if not fullrepaint:
+ style |= wx.NO_FULL_REPAINT_ON_RESIZE
+ wx.html.HtmlWindow.__init__(self, parent, wx.NewId(), style=style)
+
diff --git a/pynxc/waxy/htmlwindow.py~ b/pynxc/waxy/htmlwindow.py~
new file mode 100644
index 0000000..57a649d
--- /dev/null
+++ b/pynxc/waxy/htmlwindow.py~
@@ -0,0 +1,15 @@
+# htmlwindow.py
+
+# todo: styles
+
+import wx.html
+import waxobject
+
+class HTMLWindow(wx.html.HtmlWindow, waxobject.WaxObject):
+
+ def __init__(self, parent, fullrepaint=1):
+ style = 0
+ if not fullrepaint:
+ style |= wx.NO_FULL_REPAINT_ON_RESIZE
+ wx.html.HtmlWindow.__init__(self, parent, wx.NewId(), style=style)
+
diff --git a/pynxc/waxy/image.py b/pynxc/waxy/image.py
new file mode 100644
index 0000000..e18bfc8
--- /dev/null
+++ b/pynxc/waxy/image.py
@@ -0,0 +1,115 @@
+# image.py
+
+import wx
+import waxyobject
+
+_handlers = {}
+
+def AddImageHandler(type):
+ d = {
+ "bmp": wx.BMPHandler,
+ "png": wx.PNGHandler,
+ "jpg": wx.JPEGHandler,
+ "gif": wx.GIFHandler,
+ "pcx": wx.PCXHandler,
+ "pnm": wx.PNMHandler,
+ "tiff": wx.TIFFHandler,
+ #"iff": wx.IFFHandler,
+ #"xpm": wx.XPMHandler,
+ "ico": wx.ICOHandler,
+ "cur": wx.CURHandler,
+ "ani": wx.ANIHandler,
+ }
+ key = type.lower()
+ handler = d[key]
+ wx.Image_AddHandler(handler())
+ _handlers[key] = 1
+
+def AddAllImageHandlers():
+ wx.InitAllImageHandlers()
+
+class Image(wx.Image, waxyobject.WaxyObject):
+
+ def __init__(self, filename, type=None, autoinstall=False):
+ lfilename = filename.lower()
+ t = 0
+
+ # if type isn't set, try to grok it from the filename.
+ if not type:
+ if lfilename.endswith(".bmp"):
+ type = 'bmp'
+ elif lfilename.endswith(".gif"):
+ type = 'gif'
+ elif lfilename.endswith(".png"):
+ type = 'png'
+ elif lfilename.endswith(".jpg") or lfilename.endswith(".jpeg"):
+ type = 'jpg'
+ elif lfilename.endswith(".pcx"):
+ type = 'pcx'
+ elif lfilename.endswith(".ico"):
+ type = 'ico'
+
+ t = {
+ "bmp": wx.BITMAP_TYPE_BMP,
+ "gif": wx.BITMAP_TYPE_GIF,
+ "png": wx.BITMAP_TYPE_PNG,
+ "jpg": wx.BITMAP_TYPE_JPEG,
+ "jpeg": wx.BITMAP_TYPE_JPEG,
+ "pcx": wx.BITMAP_TYPE_PCX,
+ "ico": wx.BITMAP_TYPE_ICO,
+ }.get(type.lower(), 0)
+
+ if not t:
+ raise ValueError, "Could not determine bitmap type of '%s'" % (
+ filename,)
+
+ # if autoinstall is true, install handler on demand
+ if autoinstall:
+ if not _handlers.has_key(type):
+ AddImageHandler(type)
+
+ wx.Image.__init__(self, filename, t)
+
+
+def ImageAsBitmap(filename, *args, **kwargs):
+ return Image(filename, *args, **kwargs).ConvertToBitmap()
+
+class ImagePanel(wx.Panel, waxyobject.WaxyObject):
+
+ def __init__(self,parent,image,size=(200,200),pos=(0,0)):
+
+ wx.Panel.__init__(self,parent, size=size,pos=pos)
+
+ if isinstance(image,str):
+ self.image=Image(image)
+ else:
+ self.image=image
+
+ self.bitmap=wx.StaticBitmap(self, -1, self.image.ConvertToBitmap(),
+ style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
+ self.update()
+
+ def SetImage(self,image):
+
+ if isinstance(image,str):
+ image=Image(image)
+
+ self.image=image
+ self.update()
+
+ def update(self):
+
+ size=self.GetSizeTuple()
+
+ imsize=(self.image.GetWidth(),self.image.GetHeight())
+
+ if (float(imsize[0])/float(size[0]))>(float(imsize[1])/float(size[1])):
+ scale=(float(imsize[0])/float(size[0]))
+ else:
+ scale=(float(imsize[1])/float(size[1]))
+
+ newsize=(int(imsize[0]/scale),int(imsize[1]/scale))
+ newpos=( (size[0]-newsize[0])/2, (size[1]-newsize[1])/2)
+ self.bitmap.SetBitmap(self.image.Rescale(newsize[0],newsize[1]).ConvertToBitmap())
+ self.bitmap.SetPosition(newpos);
+
diff --git a/pynxc/waxy/image.py~ b/pynxc/waxy/image.py~
new file mode 100644
index 0000000..7b0d062
--- /dev/null
+++ b/pynxc/waxy/image.py~
@@ -0,0 +1,116 @@
+# image.py
+
+import wx
+import waxyobject
+
+_handlers = {}
+
+def AddImageHandler(type):
+ d = {
+ "bmp": wx.BMPHandler,
+ "png": wx.PNGHandler,
+ "jpg": wx.JPEGHandler,
+ "gif": wx.GIFHandler,
+ "pcx": wx.PCXHandler,
+ "pnm": wx.PNMHandler,
+ "tiff": wx.TIFFHandler,
+ #"iff": wx.IFFHandler,
+ #"xpm": wx.XPMHandler,
+ "ico": wx.ICOHandler,
+ "cur": wx.CURHandler,
+ "ani": wx.ANIHandler,
+ }
+ key = type.lower()
+ handler = d[key]
+ wx.Image_AddHandler(handler())
+ _handlers[key] = 1
+
+def AddAllImageHandlers():
+ wx.InitAllImageHandlers()
+
+class Image(wx.Image, waxyobject.WaxyObject):
+
+ def __init__(self, filename, type=None, autoinstall=False):
+ lfilename = filename.lower()
+ t = 0
+
+ # if type isn't set, try to grok it from the filename.
+ if not type:
+ if lfilename.endswith(".bmp"):
+ type = 'bmp'
+ elif lfilename.endswith(".gif"):
+ type = 'gif'
+ elif lfilename.endswith(".png"):
+ type = 'png'
+ elif lfilename.endswith(".jpg") or lfilename.endswith(".jpeg"):
+ type = 'jpg'
+ elif lfilename.endswith(".pcx"):
+ type = 'pcx'
+ elif lfilename.endswith(".ico"):
+ type = 'ico'
+
+ t = {
+ "bmp": wx.BITMAP_TYPE_BMP,
+ "gif": wx.BITMAP_TYPE_GIF,
+ "png": wx.BITMAP_TYPE_PNG,
+ "jpg": wx.BITMAP_TYPE_JPEG,
+ "jpeg": wx.BITMAP_TYPE_JPEG,
+ "pcx": wx.BITMAP_TYPE_PCX,
+ "ico": wx.BITMAP_TYPE_ICO,
+ }.get(type.lower(), 0)
+
+ if not t:
+ raise ValueError, "Could not determine bitmap type of '%s'" % (
+ filename,)
+
+ # if autoinstall is true, install handler on demand
+ if autoinstall:
+ if not _handlers.has_key(type):
+ AddImageHandler(type)
+
+ wx.Image.__init__(self, filename, t)
+
+
+def ImageAsBitmap(filename, *args, **kwargs):
+ return Image(filename, *args, **kwargs).ConvertToBitmap()
+
+class ImagePanel(wx.Panel, waxyobject.WaxyObject):
+
+ def __init__(self,parent,image_filename,size=(200,200),pos=(0,0)):
+
+ wx.Panel.__init__(self,parent, size=size,pos=pos)
+
+ if isinstance(image_filename,str):
+
+ self.image=Image(image_filename)
+ else:
+ self.image=image_filename
+
+ self.bitmap=wx.StaticBitmap(self, -1, self.image.ConvertToBitmap(),
+ style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
+ self.update()
+
+ def SetImage(self,image):
+
+ if isinstance(image,str):
+ image=Image(image)
+
+ self.image=image
+ self.update()
+
+ def update(self):
+
+ size=self.GetSizeTuple()
+
+ imsize=(self.image.GetWidth(),self.image.GetHeight())
+
+ if (float(imsize[0])/float(size[0]))>(float(imsize[1])/float(size[1])):
+ scale=(float(imsize[0])/float(size[0]))
+ else:
+ scale=(float(imsize[1])/float(size[1]))
+
+ newsize=(int(imsize[0]/scale),int(imsize[1]/scale))
+ newpos=( (size[0]-newsize[0])/2, (size[1]-newsize[1])/2)
+ self.bitmap.SetBitmap(self.image.Rescale(newsize[0],newsize[1]).ConvertToBitmap())
+ self.bitmap.SetPosition(newpos);
+
diff --git a/pynxc/waxy/imagelist.py b/pynxc/waxy/imagelist.py
new file mode 100644
index 0000000..f23c91d
--- /dev/null
+++ b/pynxc/waxy/imagelist.py
@@ -0,0 +1,38 @@
+"""\
+imagelist.py
+
+Wax's version of the wxImageList is a bit more user-friendly:
+
+Rather than forcing you to keep the indexes around or know them by heart,
+you can (optionally) add names for the images you add, then look them up to
+retrieve the index:
+
+ imagelist.Add(bitmap, 'folder')
+ imagelist.Add(bitmap, 'folder_open')
+
+ somecontrol.SetItemImage(node, imagelist['folder'])
+
+"""
+
+import wx
+
+class ImageList(wx.ImageList):
+
+ def __init__(self, *args, **kwargs):
+ wx.ImageList.__init__(self, *args, **kwargs)
+ self._names = {}
+
+ def Add(self, bitmap, name=None):
+ if isinstance(bitmap, str) or isinstance(bitmap, unicode):
+ bitmap = wx.BitmapFromImage(wx.Image(bitmap))
+
+ index = wx.ImageList.Add(self, bitmap)
+ if name:
+ self._names[name] = index
+
+ return index
+ def GetIndexByName(self, name):
+ return self._names[name]
+
+ def __getitem__(self, name):
+ return self.GetIndexByName(name)
diff --git a/pynxc/waxy/imagepanel.py b/pynxc/waxy/imagepanel.py
new file mode 100644
index 0000000..d04609c
--- /dev/null
+++ b/pynxc/waxy/imagepanel.py
@@ -0,0 +1,34 @@
+import wx
+
+class ImagePanel(wx.Panel):
+
+ def __init__(self,parent,id,size=(200,200),image=None,pos=(0,0)):
+
+ wx.Panel.__init__(self,parent, id,size=size,pos=pos)
+
+ self.image=image
+
+ self.bitmap=wx.StaticBitmap(self, -1, image.ConvertToBitmap(),
+ style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
+ self.update()
+
+ def SetImage(self,image):
+ self.image=image
+ self.update()
+
+ def update(self):
+
+ size=self.GetSizeTuple()
+ imsize=(self.image.GetWidth(),self.image.GetHeight())
+
+ if (float(imsize[0])/float(size[0]))>(float(imsize[1])/float(size[1])):
+ scale=(float(imsize[0])/float(size[0]))
+ else:
+ scale=(float(imsize[1])/float(size[1]))
+
+ newsize=(int(imsize[0]/scale),int(imsize[1]/scale))
+ newpos=( (size[0]-newsize[0])/2, (size[1]-newsize[1])/2)
+ self.bitmap.SetBitmap(self.image.Rescale(newsize[0],newsize[1]).ConvertToBitmap())
+ self.bitmap.SetPosition(newpos);
+
+
diff --git a/pynxc/waxy/imagepanel.py~ b/pynxc/waxy/imagepanel.py~
new file mode 100644
index 0000000..af2081f
--- /dev/null
+++ b/pynxc/waxy/imagepanel.py~
@@ -0,0 +1,33 @@
+import wx
+
+class ImagePanel(wx.Panel):
+
+ def __init__(self,parent,id,size=(200,200),image=None,pos=(0,0)):
+
+ wx.Panel.__init__(self,parent, id,size=size,pos=pos)
+
+ self.image=image
+
+ self.bitmap=wx.StaticBitmap(self, -1, image.ConvertToBitmap(),
+ style=wx.TAB_TRAVERSAL|wx.SIMPLE_BORDER)
+ self.update()
+
+ def SetImage(self,image):
+ self.image=image
+ self.update()
+
+ def update(self):
+
+ size=self.GetSizeTuple()
+ imsize=(self.image.GetWidth(),self.image.GetHeight())
+
+ if (float(imsize[0])/float(size[0]))>(float(imsize[1])/float(size[1])):
+ scale=(float(imsize[0])/float(size[0]))
+ else:
+ scale=(float(imsize[1])/float(size[1]))
+
+ newsize=(int(imsize[0]/scale),int(imsize[1]/scale))
+ newpos=( (size[0]-newsize[0])/2, (size[1]-newsize[1])/2)
+ self.bitmap.SetBitmap(self.image.Rescale(newsize[0],newsize[1]).ConvertToBitmap())
+ self.bitmap.SetPosition(newpos);
+
diff --git a/pynxc/waxy/keys.py b/pynxc/waxy/keys.py
new file mode 100644
index 0000000..dc3b355
--- /dev/null
+++ b/pynxc/waxy/keys.py
@@ -0,0 +1,44 @@
+# keys.py
+
+import wx
+
+class keys:
+ enter = wx.WXK_RETURN
+ # 'return' is not valid; reserved word
+
+ alt = wx.WXK_ALT
+ control = ctrl = wx.WXK_CONTROL
+ shift = wx.WXK_SHIFT
+
+ f1 = F1 = wx.WXK_F1
+ f2 = F2 = wx.WXK_F2
+ f3 = F3 = wx.WXK_F3
+ f4 = F4 = wx.WXK_F4
+ f5 = F5 = wx.WXK_F5
+ f6 = F6 = wx.WXK_F6
+ f7 = F7 = wx.WXK_F7
+ f8 = F8 = wx.WXK_F8
+ f9 = F9 = wx.WXK_F9
+ f10 = F10 = wx.WXK_F10
+ f11 = F11 = wx.WXK_F11
+ f12 = F12 = wx.WXK_F12
+
+ insert = wx.WXK_INSERT
+ delete = wx.WXK_DELETE
+ home = wx.WXK_HOME
+ end = wx.WXK_END
+
+ up = cursor_up = wx.WXK_UP
+ down = cursor_down = wx.WXK_DOWN
+ left = cursor_left = wx.WXK_LEFT
+ right = cursor_right = wx.WXK_RIGHT
+
+ pageup = pgup = wx.WXK_PRIOR # not: wx.WXK_PAGEUP
+ pagedown = pgdown = pgdn = wx.WXK_NEXT # not: wx.WXK_PAGEDOWN
+
+ tab = wx.WXK_TAB
+ backspace = bsp = wx.WXK_BACK
+ esc = escape = wx.WXK_ESCAPE
+
+ # XXX more later...?
+
diff --git a/pynxc/waxy/label.py b/pynxc/waxy/label.py
new file mode 100644
index 0000000..ed77117
--- /dev/null
+++ b/pynxc/waxy/label.py
@@ -0,0 +1,33 @@
+# label.py
+
+import wx
+import containers
+import waxyobject
+import styles
+
+class Label(wx.StaticText, waxyobject.WaxyObject):
+
+ def __init__(self, parent, text="", size=None, **kwargs):
+ #assert isinstance(parent, containers.Container)
+
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.StaticText.__init__(self, parent, wx.NewId(), text,
+ size=size or (-1,-1), style=style)
+ styles.properties(self, kwargs)
+
+ #
+ # style parameters
+
+ __styles__ = {
+ 'align': ({
+ 'left': wx.ALIGN_LEFT,
+ 'right': wx.ALIGN_RIGHT,
+ 'center': wx.ALIGN_CENTRE,
+ 'centre': wx.ALIGN_CENTRE,
+ }, styles.DICTSTART),
+ 'noresize': (wx.ST_NO_AUTORESIZE, styles.NORMAL),
+ }
+
diff --git a/pynxc/waxy/label.py~ b/pynxc/waxy/label.py~
new file mode 100644
index 0000000..eb2e266
--- /dev/null
+++ b/pynxc/waxy/label.py~
@@ -0,0 +1,33 @@
+# label.py
+
+import wx
+import containers
+import waxyobject
+import styles
+
+class Label(wx.StaticText, waxyobject.WaxObject):
+
+ def __init__(self, parent, text="", size=None, **kwargs):
+ #assert isinstance(parent, containers.Container)
+
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.StaticText.__init__(self, parent, wx.NewId(), text,
+ size=size or (-1,-1), style=style)
+ styles.properties(self, kwargs)
+
+ #
+ # style parameters
+
+ __styles__ = {
+ 'align': ({
+ 'left': wx.ALIGN_LEFT,
+ 'right': wx.ALIGN_RIGHT,
+ 'center': wx.ALIGN_CENTRE,
+ 'centre': wx.ALIGN_CENTRE,
+ }, styles.DICTSTART),
+ 'noresize': (wx.ST_NO_AUTORESIZE, styles.NORMAL),
+ }
+
diff --git a/pynxc/waxy/line.py b/pynxc/waxy/line.py
new file mode 100644
index 0000000..f25448c
--- /dev/null
+++ b/pynxc/waxy/line.py
@@ -0,0 +1,18 @@
+# line.py
+
+import wx
+import waxyobject
+
+class Line(wx.StaticLine, waxyobject.WaxyObject):
+
+ def __init__(self, parent, size=(20,-1), direction='horizontal'):
+ style = 0
+ if direction.lower().startswith('h'):
+ style |= wx.LI_HORIZONTAL
+ elif direction.lower().startswith('v'):
+ style |= wx.LI_HORIZONTAL
+ else:
+ raise ValueError, "direction should be horizontal or vertical"
+
+ wx.StaticLine.__init__(self, parent, wx.NewId(), size=size, style=style)
+
diff --git a/pynxc/waxy/line.py~ b/pynxc/waxy/line.py~
new file mode 100644
index 0000000..6653a3e
--- /dev/null
+++ b/pynxc/waxy/line.py~
@@ -0,0 +1,18 @@
+# line.py
+
+import wx
+import waxyobject
+
+class Line(wx.StaticLine, waxyobject.WaxObject):
+
+ def __init__(self, parent, size=(20,-1), direction='horizontal'):
+ style = 0
+ if direction.lower().startswith('h'):
+ style |= wx.LI_HORIZONTAL
+ elif direction.lower().startswith('v'):
+ style |= wx.LI_HORIZONTAL
+ else:
+ raise ValueError, "direction should be horizontal or vertical"
+
+ wx.StaticLine.__init__(self, parent, wx.NewId(), size=size, style=style)
+
diff --git a/pynxc/waxy/listbox.py b/pynxc/waxy/listbox.py
new file mode 100644
index 0000000..c1283cc
--- /dev/null
+++ b/pynxc/waxy/listbox.py
@@ -0,0 +1,92 @@
+# listbox.py
+
+import wx
+import containers
+import waxyobject
+import styles
+
+class ListBox(wx.ListBox, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Click': wx.EVT_LISTBOX,
+ 'DoubleClick': wx.EVT_LISTBOX_DCLICK,
+ }
+
+ def __init__(self, parent, choices=[], size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.ListBox.__init__(self, parent, wx.NewId(), size=size or (-1,-1),
+ choices=choices, style=style)
+
+ self.BindEvents()
+
+ styles.properties(self, kwargs)
+
+ def SetSelectionType(self, selection):
+ style = 0
+ selection = selection.lower()
+ if selection == "single":
+ style |= wx.LB_SINGLE
+ elif selection == "multiple":
+ style |= wx.LB_MULTIPLE
+ elif selection == "extended":
+ style |= wx.LB_EXTENDED
+ else:
+ raise ValueError, "selection must be single, multiple or extended"
+ return style
+
+ def SetItems(self, items):
+ """ Clear the internal list of items, and set new items. <items> is
+ a list of 2-tuples (string, data). """
+ self.Clear()
+ for s, data in items:
+ self.Append(s, data)
+
+ def Fill(self, items):
+ """ Like SetItems, but just uses a list of strings. """
+ self.Clear()
+ for s in items:
+ self.Append(s)
+
+ def GetItems(self):
+ """ Return a list of 2-tuples (string, data). """
+ items = []
+ for i in range(self.GetCount()):
+ s = self.GetString(i)
+ data = self.GetClientData(i)
+ items.append((s, data))
+ return items
+
+ # TODO:
+ # Add __getitem__, __setitem__, __delitem__
+ # These should probably work with tuples (string, data).
+ # 1-tuples can be used, in which case data are left alone.
+ # Non-tuples are not allowed.
+
+ # We should also be able to iterate over a ListBox...
+ # Although for (s, data) in listbox.GetItems() would work too.
+
+ #
+ # style parameters
+
+ _listbox_selection = {
+ 'single': wx.LB_SINGLE,
+ 'multiple': wx.LB_MULTIPLE,
+ 'extended': wx.LB_EXTENDED,
+ }
+
+ _listbox_scrollbar = {
+ 'always': wx.LB_ALWAYS_SB,
+ 'needed': wx.LB_NEEDED_SB,
+ }
+
+ def _params(self, kwargs):
+ flags = 0
+ flags |= styles.styledictstart('selection', self._listbox_selection, kwargs)
+ flags |= styles.styledictstart('scrollbar', self._listbox_scrollbar, kwargs)
+ flags |= styles.stylebool('sort', wx.LB_SORT, kwargs)
+ flags |= styles.stylebool('horizontal_scrollbar', wx.LB_HSCROLL, kwargs)
+
+ return flags
diff --git a/pynxc/waxy/listbox.py~ b/pynxc/waxy/listbox.py~
new file mode 100644
index 0000000..377694a
--- /dev/null
+++ b/pynxc/waxy/listbox.py~
@@ -0,0 +1,92 @@
+# listbox.py
+
+import wx
+import containers
+import waxobject
+import styles
+
+class ListBox(wx.ListBox, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Click': wx.EVT_LISTBOX,
+ 'DoubleClick': wx.EVT_LISTBOX_DCLICK,
+ }
+
+ def __init__(self, parent, choices=[], size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.ListBox.__init__(self, parent, wx.NewId(), size=size or (-1,-1),
+ choices=choices, style=style)
+
+ self.BindEvents()
+
+ styles.properties(self, kwargs)
+
+ def SetSelectionType(self, selection):
+ style = 0
+ selection = selection.lower()
+ if selection == "single":
+ style |= wx.LB_SINGLE
+ elif selection == "multiple":
+ style |= wx.LB_MULTIPLE
+ elif selection == "extended":
+ style |= wx.LB_EXTENDED
+ else:
+ raise ValueError, "selection must be single, multiple or extended"
+ return style
+
+ def SetItems(self, items):
+ """ Clear the internal list of items, and set new items. <items> is
+ a list of 2-tuples (string, data). """
+ self.Clear()
+ for s, data in items:
+ self.Append(s, data)
+
+ def Fill(self, items):
+ """ Like SetItems, but just uses a list of strings. """
+ self.Clear()
+ for s in items:
+ self.Append(s)
+
+ def GetItems(self):
+ """ Return a list of 2-tuples (string, data). """
+ items = []
+ for i in range(self.GetCount()):
+ s = self.GetString(i)
+ data = self.GetClientData(i)
+ items.append((s, data))
+ return items
+
+ # TODO:
+ # Add __getitem__, __setitem__, __delitem__
+ # These should probably work with tuples (string, data).
+ # 1-tuples can be used, in which case data are left alone.
+ # Non-tuples are not allowed.
+
+ # We should also be able to iterate over a ListBox...
+ # Although for (s, data) in listbox.GetItems() would work too.
+
+ #
+ # style parameters
+
+ _listbox_selection = {
+ 'single': wx.LB_SINGLE,
+ 'multiple': wx.LB_MULTIPLE,
+ 'extended': wx.LB_EXTENDED,
+ }
+
+ _listbox_scrollbar = {
+ 'always': wx.LB_ALWAYS_SB,
+ 'needed': wx.LB_NEEDED_SB,
+ }
+
+ def _params(self, kwargs):
+ flags = 0
+ flags |= styles.styledictstart('selection', self._listbox_selection, kwargs)
+ flags |= styles.styledictstart('scrollbar', self._listbox_scrollbar, kwargs)
+ flags |= styles.stylebool('sort', wx.LB_SORT, kwargs)
+ flags |= styles.stylebool('horizontal_scrollbar', wx.LB_HSCROLL, kwargs)
+
+ return flags
diff --git a/pynxc/waxy/menu.py b/pynxc/waxy/menu.py
new file mode 100644
index 0000000..ef587fc
--- /dev/null
+++ b/pynxc/waxy/menu.py
@@ -0,0 +1,135 @@
+# menu.py
+
+import wx
+import waxyobject
+import string
+
+class Menu(wx.Menu, waxyobject.WaxyObject):
+
+ def __init__(self, parent, autoevents=1):
+ wx.Menu.__init__(self)
+ self.parent = parent # necessary for hooking up events
+ self.autoevents = autoevents
+
+ def Append(self, title, event=None, tooltip="", type="", hotkey="",special=''):
+ # Note: the order is different from wx.Menu.Append!
+ style = 0
+ style |= {
+ "r": wx.ITEM_RADIO,
+ "c": wx.ITEM_CHECK,
+ }.get(type.lower()[:1], wx.ITEM_NORMAL)
+
+ if hotkey:
+ title = title + "\t" + hotkey
+
+ if special.lower()=='about':
+ id=wx.ID_ABOUT
+ elif special.lower()=='exit':
+ id=wx.ID_EXIT
+ else:
+ id = wx.NewId()
+
+ item = wx.Menu.Append(self, id, title, tooltip, style)
+ if event:
+ # you *can* choose to specify no event, but that doesn't seem very
+ # useful
+ wx.EVT_MENU(self.parent, id, event)
+ elif self.autoevents:
+ wx.EVT_MENU(self.parent, id, self.HandleAutoEvent)
+
+ return item
+
+ def AppendMenu(self, title, menu):
+ id = wx.NewId()
+ return wx.Menu.AppendMenu(self, id, title, menu)
+ # I suppose there's room for an event here, but why bother...?
+
+ def GetItem(self, title):
+ """ Find item by title, bypassing the internal id. """
+ id = self.FindItem(title)
+ item = self.FindItemById(id)
+ return item
+
+ def Delete(self, obj):
+ # according to the wxWindows reference, wx.Menu.Delete() also accepts
+ # MenuItems... but apparently that isn't true, at least not in wxPython.
+ # So let's fix it:
+ if isinstance(obj, wx.MenuItem):
+ id = obj.GetId()
+ return wx.Menu.Delete(self, id)
+ else:
+ # assume it's a number
+ return wx.Menu.Delete(self, obj)
+
+ def HandleAutoEvent(self, event):
+ id = event.GetId()
+ menuitem = self.FindItemById(id)
+
+ def GetFullTitle(id):
+ menubar = self.parent.GetMenuBar()
+ if menubar:
+ for mwitem in menubar.Walk():
+ if mwitem.items and mwitem.items[-1].GetId() == id:
+ title = mwitem.name
+ for menuitem in mwitem.items:
+ title = title + " " + menuitem.GetLabel()
+ return title
+ return None
+
+ title = GetFullTitle(id)
+ if title:
+ methodname = "Menu_" + genmethodname(title)
+ #print "Looking for:", methodname
+ if hasattr(self.parent, methodname):
+ f = getattr(self.parent, methodname)
+ f(event)
+
+ # these aliases make sense...
+ Add = Append
+ AddMenu = AppendMenu
+
+ def Walk(self):
+ for menuitem in self.GetMenuItems():
+ yield [menuitem]
+ submenu = menuitem.GetSubMenu()
+ if submenu:
+ for subitem in submenu.Walk():
+ yield [menuitem] + subitem
+
+ # TODO: Is it possible to write a better GetTitle() or GetRealTitle()
+ # or something, that gives us the actual label of the menu?
+
+class MenuBar(wx.MenuBar, waxyobject.WaxyObject):
+ def __init__(self, parent=None, *args, **kwargs):
+ wx.MenuBar.__init__(self, *args, **kwargs)
+ if parent: parent.SetMenuBar(self)
+
+ def Append(self, menu, text):
+ wx.MenuBar.Append(self, menu, text)
+
+ def Walk(self):
+ for i in range(self.GetMenuCount()):
+ menu = self.GetMenu(i)
+ for items in menu.Walk():
+ menuwalkitem = _MenuWalkItem()
+ menuwalkitem.name = self.GetLabelTop(i)
+ menuwalkitem.menu = menu
+ menuwalkitem.items = items
+ yield menuwalkitem
+
+class _MenuWalkItem:
+ pass
+
+#
+# auxiliary methods
+
+def genmethodname(title):
+ allowed = string.letters + string.digits + "_"
+ name = ""
+ for char in title:
+ if char in allowed:
+ name = name + char
+ elif char == ' ':
+ name = name + '_'
+ return name
+
diff --git a/pynxc/waxy/menu.py~ b/pynxc/waxy/menu.py~
new file mode 100644
index 0000000..2cab266
--- /dev/null
+++ b/pynxc/waxy/menu.py~
@@ -0,0 +1,129 @@
+# menu.py
+
+import wx
+import waxyobject
+import string
+
+class Menu(wx.Menu, waxyobject.WaxyObject):
+
+ def __init__(self, parent, autoevents=1):
+ wx.Menu.__init__(self)
+ self.parent = parent # necessary for hooking up events
+ self.autoevents = autoevents
+
+ def Append(self, title, event=None, tooltip="", type="", hotkey=""):
+ # Note: the order is different from wx.Menu.Append!
+ style = 0
+ style |= {
+ "r": wx.ITEM_RADIO,
+ "c": wx.ITEM_CHECK,
+ }.get(type.lower()[:1], wx.ITEM_NORMAL)
+
+ if hotkey:
+ title = title + "\t" + hotkey
+
+ id = wx.NewId()
+ item = wx.Menu.Append(self, id, title, tooltip, style)
+ if event:
+ # you *can* choose to specify no event, but that doesn't seem very
+ # useful
+ wx.EVT_MENU(self.parent, id, event)
+ elif self.autoevents:
+ wx.EVT_MENU(self.parent, id, self.HandleAutoEvent)
+
+ return item
+
+ def AppendMenu(self, title, menu):
+ id = wx.NewId()
+ return wx.Menu.AppendMenu(self, id, title, menu)
+ # I suppose there's room for an event here, but why bother...?
+
+ def GetItem(self, title):
+ """ Find item by title, bypassing the internal id. """
+ id = self.FindItem(title)
+ item = self.FindItemById(id)
+ return item
+
+ def Delete(self, obj):
+ # according to the wxWindows reference, wx.Menu.Delete() also accepts
+ # MenuItems... but apparently that isn't true, at least not in wxPython.
+ # So let's fix it:
+ if isinstance(obj, wx.MenuItem):
+ id = obj.GetId()
+ return wx.Menu.Delete(self, id)
+ else:
+ # assume it's a number
+ return wx.Menu.Delete(self, obj)
+
+ def HandleAutoEvent(self, event):
+ id = event.GetId()
+ menuitem = self.FindItemById(id)
+
+ def GetFullTitle(id):
+ menubar = self.parent.GetMenuBar()
+ if menubar:
+ for mwitem in menubar.Walk():
+ if mwitem.items and mwitem.items[-1].GetId() == id:
+ title = mwitem.name
+ for menuitem in mwitem.items:
+ title = title + " " + menuitem.GetLabel()
+ return title
+ return None
+
+ title = GetFullTitle(id)
+ if title:
+ methodname = "Menu_" + genmethodname(title)
+ #print "Looking for:", methodname
+ if hasattr(self.parent, methodname):
+ f = getattr(self.parent, methodname)
+ f(event)
+
+ # these aliases make sense...
+ Add = Append
+ AddMenu = AppendMenu
+
+ def Walk(self):
+ for menuitem in self.GetMenuItems():
+ yield [menuitem]
+ submenu = menuitem.GetSubMenu()
+ if submenu:
+ for subitem in submenu.Walk():
+ yield [menuitem] + subitem
+
+ # TODO: Is it possible to write a better GetTitle() or GetRealTitle()
+ # or something, that gives us the actual label of the menu?
+
+class MenuBar(wx.MenuBar, waxobject.WaxObject):
+ def __init__(self, parent=None, *args, **kwargs):
+ wx.MenuBar.__init__(self, *args, **kwargs)
+ if parent: parent.SetMenuBar(self)
+
+ def Append(self, menu, text):
+ wx.MenuBar.Append(self, menu, text)
+
+ def Walk(self):
+ for i in range(self.GetMenuCount()):
+ menu = self.GetMenu(i)
+ for items in menu.Walk():
+ menuwalkitem = _MenuWalkItem()
+ menuwalkitem.name = self.GetLabelTop(i)
+ menuwalkitem.menu = menu
+ menuwalkitem.items = items
+ yield menuwalkitem
+
+class _MenuWalkItem:
+ pass
+
+#
+# auxiliary methods
+
+def genmethodname(title):
+ allowed = string.letters + string.digits + "_"
+ name = ""
+ for char in title:
+ if char in allowed:
+ name = name + char
+ elif char == ' ':
+ name = name + '_'
+ return name
+
diff --git a/pynxc/waxy/messagedialog.py b/pynxc/waxy/messagedialog.py
new file mode 100644
index 0000000..534878b
--- /dev/null
+++ b/pynxc/waxy/messagedialog.py
@@ -0,0 +1,70 @@
+# messagedialog.py
+
+import wx
+import waxyobject
+import core
+
+class MessageDialog(wx.MessageDialog, waxyobject.WaxyObject):
+ """Displays a message dialog. Title, text, buttons (OK/Cancel/Yes/No),
+ and icon are configurable.
+ """
+
+ _icons = {
+ 'asterisk': wx.ICON_ASTERISK,
+ 'error': wx.ICON_ERROR,
+ 'exclamation': wx.ICON_EXCLAMATION,
+ 'hand': wx.ICON_HAND,
+ 'information': wx.ICON_INFORMATION,
+ 'mask': wx.ICON_MASK,
+ 'question': wx.ICON_QUESTION,
+ 'stop': wx.ICON_STOP,
+ 'warning': wx.ICON_WARNING,
+ }
+
+ def __init__(self, parent, title="Message", text="A message", ok=0,
+ cancel=0, yes_no=0, icon=""):
+ style = 0
+ if ok: style |= wx.OK
+ if cancel: style |= wx.CANCEL
+ if yes_no: style |= wx.YES_NO
+
+ # set icon... some of these values show the same icon, at least on
+ # Windows.
+ icon = self._icons.get(icon.lower(), None)
+ if icon:
+ style |= icon
+
+ # use a sensible default
+ if not (ok or cancel or yes_no):
+ style |= wx.OK
+
+ wx.MessageDialog.__init__(self, parent, text, title, style)
+
+ def ShowModal(self):
+ r = wx.MessageDialog.ShowModal(self)
+ return {
+ wx.ID_OK: 'ok',
+ wx.ID_CANCEL: 'cancel',
+ wx.ID_YES: 'yes',
+ wx.ID_NO: 'no',
+ }.get(r, "?")
+
+
+def ShowMessage(title, text, icon=""):
+ """ Displays a message with an OK button. A bit like Delphi's ShowMessage
+ procedure. """
+ parent = core.GetActiveWindow()
+ dlg = MessageDialog(parent, title, text, ok=1, icon=icon)
+ try:
+ dlg.ShowModal()
+ finally:
+ dlg.Destroy()
+
+
+def Error(msg,parent=None):
+
+ dlg = MessageDialog(parent, "Error",
+ msg,icon='error')
+ dlg.ShowModal()
+ dlg.Destroy()
+
diff --git a/pynxc/waxy/messagedialog.py~ b/pynxc/waxy/messagedialog.py~
new file mode 100644
index 0000000..896d33b
--- /dev/null
+++ b/pynxc/waxy/messagedialog.py~
@@ -0,0 +1,63 @@
+# messagedialog.py
+
+import wx
+import waxyobject
+import core
+
+class MessageDialog(wx.MessageDialog, waxyobject.WaxyObject):
+ """Displays a message dialog. Title, text, buttons (OK/Cancel/Yes/No),
+ and icon are configurable.
+ """
+
+ _icons = {
+ 'asterisk': wx.ICON_ASTERISK,
+ 'error': wx.ICON_ERROR,
+ 'exclamation': wx.ICON_EXCLAMATION,
+ 'hand': wx.ICON_HAND,
+ 'information': wx.ICON_INFORMATION,
+ 'mask': wx.ICON_MASK,
+ 'question': wx.ICON_QUESTION,
+ 'stop': wx.ICON_STOP,
+ 'warning': wx.ICON_WARNING,
+ }
+
+ def __init__(self, parent, title="Message", text="A message", ok=0,
+ cancel=0, yes_no=0, icon=""):
+ style = 0
+ if ok: style |= wx.OK
+ if cancel: style |= wx.CANCEL
+ if yes_no: style |= wx.YES_NO
+
+ # set icon... some of these values show the same icon, at least on
+ # Windows.
+ icon = self._icons.get(icon.lower(), None)
+ if icon:
+ style |= icon
+
+ # use a sensible default
+ if not (ok or cancel or yes_no):
+ style |= wx.OK
+
+ wx.MessageDialog.__init__(self, parent, text, title, style)
+ self.SetDefaultFont()
+
+ def ShowModal(self):
+ r = wx.MessageDialog.ShowModal(self)
+ return {
+ wx.ID_OK: 'ok',
+ wx.ID_CANCEL: 'cancel',
+ wx.ID_YES: 'yes',
+ wx.ID_NO: 'no',
+ }.get(r, "?")
+
+
+def ShowMessage(title, text, icon=""):
+ """ Displays a message with an OK button. A bit like Delphi's ShowMessage
+ procedure. """
+ parent = core.GetActiveWindow()
+ dlg = MessageDialog(parent, title, text, ok=1, icon=icon)
+ try:
+ dlg.ShowModal()
+ finally:
+ dlg.Destroy()
+
diff --git a/pynxc/waxy/mousepointer.py b/pynxc/waxy/mousepointer.py
new file mode 100644
index 0000000..7809cc3
--- /dev/null
+++ b/pynxc/waxy/mousepointer.py
@@ -0,0 +1,95 @@
+# mousepointer.py
+
+# TODO:
+# - remove code duplication in LoadImage/LoadStream
+
+import wx
+import colordb
+import os
+import sys
+import utils
+
+POINTERS = {
+ "arrow" : wx.CURSOR_ARROW,
+ "arrowright" : wx.CURSOR_RIGHT_ARROW,
+ "bullseye" : wx.CURSOR_BULLSEYE,
+ "char" : wx.CURSOR_CHAR,
+ "cross" : wx.CURSOR_CROSS,
+ "hand" : wx.CURSOR_HAND,
+ "ibeam" : wx.CURSOR_IBEAM,
+ "buttonleft" : wx.CURSOR_LEFT_BUTTON,
+ "magnifier" : wx.CURSOR_MAGNIFIER,
+ "buttonmiddle" : wx.CURSOR_MIDDLE_BUTTON,
+ "noentry" : wx.CURSOR_NO_ENTRY,
+ "paintbrush" : wx.CURSOR_PAINT_BRUSH,
+ "pencil" : wx.CURSOR_PENCIL,
+ "pointleft" : wx.CURSOR_POINT_LEFT,
+ "pointright" : wx.CURSOR_POINT_RIGHT,
+ "arrowquestion" : wx.CURSOR_QUESTION_ARROW,
+ "buttonright" : wx.CURSOR_RIGHT_BUTTON,
+ "sizenesw" : wx.CURSOR_SIZENESW,
+ "sizens" : wx.CURSOR_SIZENS,
+ "sizenwse" : wx.CURSOR_SIZENWSE,
+ "sizewe" : wx.CURSOR_SIZEWE,
+ "sizing" : wx.CURSOR_SIZING,
+ "spraycan" : wx.CURSOR_SPRAYCAN,
+ "wait" : wx.CURSOR_WAIT,
+ "watch" : wx.CURSOR_WATCH,
+ "blank" : wx.CURSOR_BLANK,
+ "default" : wx.CURSOR_DEFAULT,
+ "arrowcopy" : wx.CURSOR_COPY_ARROW,
+ "arrowwait" : wx.CURSOR_ARROWWAIT,
+ }
+
+class MousePointerRegistry:
+ def __init__(self):
+ self.custom = {}
+ def Get(self, name):
+ if POINTERS.has_key(name):
+ return wx.StockCursor(POINTERS[name]) # return a cursor
+ elif self.custom.has_key(name):
+ return self.custom[name] # return an image-as-cursor
+ else:
+ raise KeyError, "Unknown pointer name: %s" % (name,)
+ def Set(self, name, value):
+ self.custom[name] = value
+ Register = Set
+ def GetBuiltinNames(self):
+ return POINTERS.keys()
+ def GetCustomNames(self):
+ return self.custom.keys()
+ def GetNames(self):
+ return self.GetBuiltinNames() + self.GetCustomNames()
+
+ def _RegisterImage(self, name, image, maskcolor='white', hotx=None, hoty=None):
+ """ Register a wx.Image as a cursor. """
+ if sys.platform =='win32':
+ #cursors fixed size 32,32
+ xratio = image.GetWidth() / 32
+ yratio = image.GetHeight() / 32
+ else:
+ xratio = yratio = 1
+ if not image.HasMask():
+ c = colordb.convert_color(maskcolor)
+ image.SetMaskColour(c[0], c[1], c[2])
+ if not hotx:
+ hotx = image.GetWidth() / 2
+ hotx = hotx / xratio
+ image.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_X, hotx)
+ if not hoty:
+ hoty = image.GetHeight() / 2
+ hoty = hoty / yratio
+ image.SetOptionInt(wx.IMAGE_OPTION_CUR_HOTSPOT_Y, hoty)
+ self.custom[name] = wx.CursorFromImage(image)
+
+ def RegisterImage(self, name, filename, maskcolor='white', hotx=None, hoty=None):
+ i = wx.Image(utils.opj(filename), wx.BITMAP_TYPE_ANY)
+ self._RegisterImage(name, i, maskcolor, hotx, hoty)
+
+ def RegisterStream(self, name, stream, maskcolor='white', hotx=None, hoty=None):
+ i = wx.ImageFromStream(stream)
+ self._RegisterImage(name, i, maskcolor, hotx, hoty)
+
+
+# global registry
+MousePointers = MousePointerRegistry()
diff --git a/pynxc/waxy/multichoicedialog.py b/pynxc/waxy/multichoicedialog.py
new file mode 100644
index 0000000..e0ec608
--- /dev/null
+++ b/pynxc/waxy/multichoicedialog.py
@@ -0,0 +1,29 @@
+import wx
+import waxyobject
+from font import Font
+
+# would be nice with a select all or something
+
+class MultiChoiceDialog(wx.MultiChoiceDialog, waxyobject.WaxyObject):
+
+ def __init__(self, parent,lst,text='Choose',title='Multi Choice Dialog'):
+ wx.MultiChoiceDialog.__init__(self, parent,text,title,lst)
+
+ self.lst=lst
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.MultiChoiceDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+ def GetChosenItems(self):
+ selections = self.GetSelections()
+ strings = [self.lst[x] for x in selections]
+
+ return strings
+
+
+
diff --git a/pynxc/waxy/multichoicedialog.py~ b/pynxc/waxy/multichoicedialog.py~
new file mode 100644
index 0000000..564e0fc
--- /dev/null
+++ b/pynxc/waxy/multichoicedialog.py~
@@ -0,0 +1,27 @@
+import wx
+import waxyobject
+from font import Font
+
+class MultiChoiceDialog(wx.MultiChoiceDialog, waxyobject.WaxyObject):
+
+ def __init__(self, parent,lst,text='Choose',title='Multi Choice Dialog'):
+ wx.MultiChoiceDialog.__init__(self, parent,text,title,lst)
+
+ self.lst=lst
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.MultiChoiceDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+ def GetChosenItems(self):
+ selections = self.GetSelections()
+ strings = [self.lst[x] for x in selections]
+
+ return strings
+
+
+
diff --git a/pynxc/waxy/notebook.py b/pynxc/waxy/notebook.py
new file mode 100644
index 0000000..5ef5515
--- /dev/null
+++ b/pynxc/waxy/notebook.py
@@ -0,0 +1,51 @@
+# notebook.py
+
+import waxyobject
+import wx
+import styles
+import utils
+
+class NoteBook(wx.Notebook, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'PageChanging': wx.EVT_NOTEBOOK_PAGE_CHANGING,
+ 'PageChanged': wx.EVT_NOTEBOOK_PAGE_CHANGED,
+ }
+
+ def __init__(self, parent, size=(640,480), **kwargs):
+
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ self.id = wx.NewId()
+ wx.Notebook.__init__(self, parent, self.id, style=style, size=size)
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+# def AddPage(self, window, *args, **kwargs):
+# wx.Notebook.AddPage(self, window, *args, **kwargs)
+
+ def GetCurrentPage(self):
+ idx = self.GetSelection()
+ if idx == -1:
+ raise ValueError, "NoteBook currently has no pages"
+ else:
+ return self.GetPage(idx)
+
+ #
+ # style parameters
+
+ _notebook_orientation = {
+ "left": wx.NB_LEFT,
+ "right": wx.NB_RIGHT,
+ "bottom": wx.NB_BOTTOM,
+ "top": 0,
+ }
+
+ def _params(self, kwargs):
+ flags = 0
+ flags |= styles.styledictstart('orientation', self._notebook_orientation, kwargs)
+ flags |= styles.stylebool('fixedwidth', wx.NB_FIXEDWIDTH, kwargs)
+ flags |= styles.stylebool('multiline', wx.NB_MULTILINE, kwargs)
+ return flags
diff --git a/pynxc/waxy/notebook.py~ b/pynxc/waxy/notebook.py~
new file mode 100644
index 0000000..66024a0
--- /dev/null
+++ b/pynxc/waxy/notebook.py~
@@ -0,0 +1,51 @@
+# notebook.py
+
+import waxyobject
+import wx
+import styles
+import utils
+
+class NoteBook(wx.Notebook, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'PageChanging': wx.EVT_NOTEBOOK_PAGE_CHANGING,
+ 'PageChanged': wx.EVT_NOTEBOOK_PAGE_CHANGED,
+ }
+
+ def __init__(self, parent, size=(640,480), **kwargs):
+
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ self.id = wx.NewId()
+ wx.Notebook.__init__(self, parent, self.id, style=style, size=size)
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ def AddPage(self, window, *args, **kwargs):
+ wx.Notebook.AddPage(self, window, *args, **kwargs)
+
+ def GetCurrentPage(self):
+ idx = self.GetSelection()
+ if idx == -1:
+ raise ValueError, "NoteBook currently has no pages"
+ else:
+ return self.GetPage(idx)
+
+ #
+ # style parameters
+
+ _notebook_orientation = {
+ "left": wx.NB_LEFT,
+ "right": wx.NB_RIGHT,
+ "bottom": wx.NB_BOTTOM,
+ "top": 0,
+ }
+
+ def _params(self, kwargs):
+ flags = 0
+ flags |= styles.styledictstart('orientation', self._notebook_orientation, kwargs)
+ flags |= styles.stylebool('fixedwidth', wx.NB_FIXEDWIDTH, kwargs)
+ flags |= styles.stylebool('multiline', wx.NB_MULTILINE, kwargs)
+ return flags
diff --git a/pynxc/waxy/notes.txt b/pynxc/waxy/notes.txt
new file mode 100644
index 0000000..bc6a7be
--- /dev/null
+++ b/pynxc/waxy/notes.txt
@@ -0,0 +1,21 @@
+removing references to fonts
+removed the autoinstall on the Image type (memory leak?)
+added aboutbox
+added colourdialog, and colordialog
+added multichoicedialog
+added singlechoicedialog
+
+code to look at:
+canvas.py
+
+
+demos to do:
+
+artprovider
+bitmap
+bitmapbutton
+checkbox
+checklistbox
+combobox
+customdialog
+listbox
diff --git a/pynxc/waxy/notes.txt~ b/pynxc/waxy/notes.txt~
new file mode 100644
index 0000000..df69f54
--- /dev/null
+++ b/pynxc/waxy/notes.txt~
@@ -0,0 +1,20 @@
+removing references to fonts
+removed the autoinstall on the Image type (memory leak?)
+added aboutbox
+added colourdialog, and colordialog
+added multichoicedialog
+added singlechoicedialog
+
+code to look at:
+canvas.py
+
+
+demos to do:
+
+artprovider
+bitmap
+bitmapbutton
+checkbox
+checklistbox
+combobox
+customdialog
diff --git a/pynxc/waxy/panel.py b/pynxc/waxy/panel.py
new file mode 100644
index 0000000..a4222cb
--- /dev/null
+++ b/pynxc/waxy/panel.py
@@ -0,0 +1,27 @@
+# panel.py
+
+import wx
+import containers
+import styles
+
+class Panel(wx.Panel, containers.Container):
+ """ Sub-level containers inside a frame, used for layout. """
+ def __init__(self, parent, direction="H", size=None, **kwargs):
+ style = 0
+ style |= styles.window(kwargs)
+ wx.Panel.__init__(self, parent, wx.NewId(), size=size or (-1,-1),
+ style=style)
+
+ self.BindEvents()
+ self._create_sizer(direction)
+ styles.properties(self, kwargs)
+ self.Body()
+
+class HorizontalPanel(Panel):
+ def __init__(self, parent, **kwargs):
+ Panel.__init__(self, parent, direction='h', **kwargs)
+
+class VerticalPanel(Panel):
+ def __init__(self, parent, **kwargs):
+ Panel.__init__(self, parent, direction='v', **kwargs)
+
diff --git a/pynxc/waxy/panel.py~ b/pynxc/waxy/panel.py~
new file mode 100644
index 0000000..c6992fa
--- /dev/null
+++ b/pynxc/waxy/panel.py~
@@ -0,0 +1,28 @@
+# panel.py
+
+import wx
+import containers
+import styles
+
+class Panel(wx.Panel, containers.Container):
+ """ Sub-level containers inside a frame, used for layout. """
+ def __init__(self, parent, direction="H", size=None, **kwargs):
+ style = 0
+ style |= styles.window(kwargs)
+ wx.Panel.__init__(self, parent, wx.NewId(), size=size or (-1,-1),
+ style=style)
+
+ self.BindEvents()
+ self._create_sizer(direction)
+ self.SetDefaultFont()
+ styles.properties(self, kwargs)
+ self.Body()
+
+class HorizontalPanel(Panel):
+ def __init__(self, parent, **kwargs):
+ Panel.__init__(self, parent, direction='h', **kwargs)
+
+class VerticalPanel(Panel):
+ def __init__(self, parent, **kwargs):
+ Panel.__init__(self, parent, direction='v', **kwargs)
+
diff --git a/pynxc/waxy/progressdialog.py b/pynxc/waxy/progressdialog.py
new file mode 100644
index 0000000..e43f96e
--- /dev/null
+++ b/pynxc/waxy/progressdialog.py
@@ -0,0 +1,28 @@
+# progressdialog.py
+
+import wx
+import waxyobject
+
+class ProgressDialog(wx.ProgressDialog, waxyobject.WaxyObject):
+
+ def __init__(self,parent=None,title="Progress", message="Progress",
+ maximum=100,abort=1,modal=1,show_elapsed_time=1,show_remaining_time=1):
+ style = 0
+ if abort:
+ style |= wx.PD_CAN_ABORT
+ if modal:
+ style |= wx.PD_APP_MODAL
+ if show_elapsed_time:
+ style |= wx.PD_ELAPSED_TIME
+ if show_remaining_time:
+ style |= wx.PD_REMAINING_TIME
+ wx.ProgressDialog.__init__(self, title, message,
+ maximum, parent,style)
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.ProgressDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
diff --git a/pynxc/waxy/progressdialog.py~ b/pynxc/waxy/progressdialog.py~
new file mode 100644
index 0000000..17f44a5
--- /dev/null
+++ b/pynxc/waxy/progressdialog.py~
@@ -0,0 +1,28 @@
+# progressdialog.py
+
+import wx
+import waxyobject
+
+class ProgressDialog(wx.ProgressDialog, waxyobject.WaxyObject):
+
+ def __init__(self,parent=None,title="Progress", message="Progress",
+ maximum=100,abort=1,modal=1,show_elapsed_time=1,show_remaining_time=1):
+ style = 0
+ if abort:
+ style |= wx.PD_CAN_ABORT
+ if modal:
+ style |= wx.PD_APP_MODAL
+ if elapsed:
+ style |= wx.PD_ELAPSED_TIME
+ if remaining:
+ style |= wx.PD_REMAINING_TIME
+ wx.ProgressDialog.__init__(self, title, message,
+ maximum, parent,style)
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.ProgressDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
diff --git a/pynxc/waxy/singlechoicedialog.py b/pynxc/waxy/singlechoicedialog.py
new file mode 100644
index 0000000..e6dffdc
--- /dev/null
+++ b/pynxc/waxy/singlechoicedialog.py
@@ -0,0 +1,29 @@
+import wx
+import waxyobject
+from font import Font
+
+# would be nice with a select all or something
+
+class SingleChoiceDialog(wx.SingleChoiceDialog, waxyobject.WaxyObject):
+
+ def __init__(self, parent,lst,text='Choose',title='Single Choice Dialog'):
+ wx.SingleChoiceDialog.__init__(self, parent,text,title,lst)
+
+ self.lst=lst
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.SingleChoiceDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+ def GetChosenItem(self):
+ s = self.GetStringSelection()
+
+
+ return s
+
+
+
diff --git a/pynxc/waxy/singlechoicedialog.py~ b/pynxc/waxy/singlechoicedialog.py~
new file mode 100644
index 0000000..2941dbc
--- /dev/null
+++ b/pynxc/waxy/singlechoicedialog.py~
@@ -0,0 +1,29 @@
+import wx
+import waxyobject
+from font import Font
+
+# would be nice with a select all or something
+
+class SingleChoiceDialog(wx.SingleChoiceDialog, waxyobject.WaxyObject):
+
+ def __init__(self, parent,lst,text='Choose',title='Single Choice Dialog'):
+ wx.SingleChoiceDialog.__init__(self, parent,text,title,lst)
+
+ self.lst=lst
+
+ def ShowModal(self):
+ """ Simplified ShowModal(), returning strings 'ok' or 'cancel'. """
+ result = wx.SingleChoiceDialog.ShowModal(self)
+ if result == wx.ID_OK:
+ return 'ok'
+ else:
+ return 'cancel'
+
+ def GetChosenItems(self):
+ selections = self.GetSelections()
+ strings = [self.lst[x] for x in selections]
+
+ return strings
+
+
+
diff --git a/pynxc/waxy/slider.py b/pynxc/waxy/slider.py
new file mode 100644
index 0000000..1f70e64
--- /dev/null
+++ b/pynxc/waxy/slider.py
@@ -0,0 +1,104 @@
+# slider.py
+
+import wx
+import containers
+import waxyobject
+import styles
+from panel import Panel
+from label import Label
+
+class Slider(wx.Slider, waxyobject.WaxyObject):
+ __events__ = {
+ 'Scroll': wx.EVT_SCROLL,
+ }
+
+ def __init__(self, parent, tickfreq=5, min=0, max=100, event=None, size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.Slider.__init__(self, parent, wx.NewId(), size=size or (-1,-1), style=style)
+ self.SetTickFreq(tickfreq)
+ self.SetRange(min, max)
+
+ self.BindEvents()
+ if event:
+ self.OnScroll = event
+ styles.properties(self, kwargs)
+
+ #
+ # style parameters
+
+ __styles__ = {
+ 'labels': (wx.SL_LABELS, styles.NORMAL),
+ 'ticks': ({
+ "left": wx.SL_LEFT | wx.SL_AUTOTICKS,
+ "right": wx.SL_RIGHT | wx.SL_AUTOTICKS,
+ "top": wx.SL_TOP | wx.SL_AUTOTICKS,
+ "bottom": wx.SL_BOTTOM | wx.SL_AUTOTICKS,
+ }, styles.DICTSTART),
+ 'orientation': ({
+ "horizontal": wx.SL_HORIZONTAL,
+ "vertical": wx.SL_VERTICAL,
+ }, styles.DICTSTART),
+ }
+
+
+class FloatSlider(Panel):
+
+ def __init__(self, parent, tickfreq=0.5, min=-5.0, max=5.0, format='%.1f',event=None, size=None, **kwargs):
+
+ if 'labels' in kwargs:
+ labels=kwargs['labels']
+ kwargs['labels']=False
+
+ Panel.__init__(self,parent,direction='v')
+
+
+ intmin=0
+ intmax=int(float(max-min)/tickfreq)
+ inttickfreq=1
+
+ self.min=min
+ self.max=max
+ self.tickfreq=tickfreq
+ self.format=format
+
+ self.slider=Slider(self, inttickfreq,intmin,intmax,event,size,**kwargs)
+ self.slider.SetValue(self.slider.GetMax()/2)
+ self.slider.OnScroll=self.OnScroll
+ self.AddComponent(self.slider,stretch=True,border=10)
+
+ if labels:
+ p=Panel(self,direction='h')
+ self.text1=Label(p,self.format % self.min,align='left')
+ self.text2=Label(p,self.format % self.max,align='right')
+ self.text3=Label(p,self.format % self.GetValue(),align='center')
+ p.AddComponent(self.text1,expand=True,border=3)
+ p.AddComponent(self.text3,expand=False,border=3)
+ p.AddComponent(self.text2,expand=True,border=3)
+ p.Pack()
+
+ self.AddComponent(p,stretch=True)
+
+
+
+ self.Pack()
+
+ def OnScroll(self,event):
+ self.text3.SetLabel(self.format % self.GetValue())
+ self.text3.SetExtraStyle(wx.ALIGN_CENTER)
+ self.OnUpdate()
+
+ def OnUpdate(self):
+ pass
+
+ def GetValue(self):
+
+ intval=float(self.slider.GetValue())
+ intmax=float(self.slider.GetMax())
+
+ val=(intval*self.tickfreq)+self.min
+
+ return val
+
diff --git a/pynxc/waxy/splitter.py b/pynxc/waxy/splitter.py
new file mode 100644
index 0000000..4afcb4b
--- /dev/null
+++ b/pynxc/waxy/splitter.py
@@ -0,0 +1,56 @@
+# splitter.py
+
+import wx
+import waxyobject
+import styles
+import utils
+
+class Splitter(wx.SplitterWindow, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'SashPosChanging': wx.EVT_SPLITTER_SASH_POS_CHANGING,
+ 'SashPosChanged': wx.EVT_SPLITTER_SASH_POS_CHANGED,
+ 'Unsplit': wx.EVT_SPLITTER_UNSPLIT,
+ 'DoubleClick': wx.EVT_SPLITTER_DCLICK,
+ }
+
+ def __init__(self, parent, size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+ wx.SplitterWindow.__init__(self, parent, wx.NewId(), style=style)
+
+ if size:
+ self.SetSize(size)
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ # I would have liked to add the windows to the constructor, but it's
+ # not possible because the Splitter needs to be present as the
+ # windows' parent... hmm...
+
+ def Split(self, window1, window2, direction="horizontal", sashposition=100,
+ minsize=20):
+
+ if direction.lower().startswith("h"):
+ self.SplitHorizontally(window1, window2, sashposition)
+ elif direction.lower().startswith("v"):
+ self.SplitVertically(window1, window2, sashposition)
+ else:
+ raise ValueError, "direction must be horizontal or vertical"
+
+ self.SetMinimumPaneSize(minsize)
+
+ #
+ # style parameters
+
+ def _params(self, kwargs):
+ flags = 0
+ flags |= styles.stylebool('permit_unsplit', wx.SP_PERMIT_UNSPLIT, kwargs)
+ flags |= styles.stylebool('live_update', wx.SP_LIVE_UPDATE, kwargs)
+ flags |= styles.stylebool('no_xp_theme', wx.SP_NO_XP_THEME, kwargs)
+ flags |= styles.stylebool('border', wx.SP_BORDER, kwargs)
+ flags |= styles.stylebool('sash3d', wx.SP_3DSASH, kwargs)
+ flags |= styles.stylebool('all3d', wx.SP_3D, kwargs)
+ return flags
diff --git a/pynxc/waxy/splitter.py~ b/pynxc/waxy/splitter.py~
new file mode 100644
index 0000000..e1fac80
--- /dev/null
+++ b/pynxc/waxy/splitter.py~
@@ -0,0 +1,62 @@
+# splitter.py
+
+import wx
+import waxyobject
+import styles
+import utils
+
+class Splitter(wx.SplitterWindow, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'SashPosChanging': wx.EVT_SPLITTER_SASH_POS_CHANGING,
+ 'SashPosChanged': wx.EVT_SPLITTER_SASH_POS_CHANGED,
+ 'Unsplit': wx.EVT_SPLITTER_UNSPLIT,
+ 'DoubleClick': wx.EVT_SPLITTER_DCLICK,
+ }
+
+ def __init__(self, parent, size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+ wx.SplitterWindow.__init__(self, parent, wx.NewId(), style=style)
+
+ if size:
+ self.SetSize(size)
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ # I would have liked to add the windows to the constructor, but it's
+ # not possible because the Splitter needs to be present as the
+ # windows' parent... hmm...
+
+ def Split(self, window1, window2, direction="horizontal", sashposition=100,
+ minsize=20):
+ # check parents
+ if waxconfig.WaxConfig.check_parent:
+ if window1.GetParent() is not self:
+ utils.parent_warning(window1, self)
+ if window2.GetParent() is not self:
+ utils.parent_warning(window2, self)
+
+ if direction.lower().startswith("h"):
+ self.SplitHorizontally(window1, window2, sashposition)
+ elif direction.lower().startswith("v"):
+ self.SplitVertically(window1, window2, sashposition)
+ else:
+ raise ValueError, "direction must be horizontal or vertical"
+
+ self.SetMinimumPaneSize(minsize)
+
+ #
+ # style parameters
+
+ def _params(self, kwargs):
+ flags = 0
+ flags |= styles.stylebool('permit_unsplit', wx.SP_PERMIT_UNSPLIT, kwargs)
+ flags |= styles.stylebool('live_update', wx.SP_LIVE_UPDATE, kwargs)
+ flags |= styles.stylebool('no_xp_theme', wx.SP_NO_XP_THEME, kwargs)
+ flags |= styles.stylebool('border', wx.SP_BORDER, kwargs)
+ flags |= styles.stylebool('sash3d', wx.SP_3DSASH, kwargs)
+ flags |= styles.stylebool('all3d', wx.SP_3D, kwargs)
+ return flags
diff --git a/pynxc/waxy/statusbar.py b/pynxc/waxy/statusbar.py
new file mode 100644
index 0000000..ba9643c
--- /dev/null
+++ b/pynxc/waxy/statusbar.py
@@ -0,0 +1,37 @@
+# statusbar.py
+
+import wx
+import waxyobject
+import styles
+
+class StatusBar(wx.StatusBar, waxyobject.WaxyObject):
+
+ def __init__(self, parent, numpanels=1, add=1, **kwargs):
+ # note: does not support the 'size' parameter
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.StatusBar.__init__(self, parent, wx.NewId(), style=style)
+
+# self.SetDefaultFont()
+ self.SetFieldsCount(numpanels)
+ if add:
+ parent.SetStatusBar(self)
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+
+ # allows for: statusbar[0] = "text"
+ def __setitem__(self, index, text):
+ self.SetStatusText(text, index)
+
+ #
+ # style parameters
+
+ def _params(self, kwargs):
+ flags = 0
+ flags |= styles.stylebool('sizegrip', wx.ST_SIZEGRIP, kwargs)
+ return flags
+
diff --git a/pynxc/waxy/styles.py b/pynxc/waxy/styles.py
new file mode 100644
index 0000000..d89b0f2
--- /dev/null
+++ b/pynxc/waxy/styles.py
@@ -0,0 +1,160 @@
+# styles.py
+
+import wx
+
+
+NORMAL = 0
+EXCLUDE = 1
+DICT = 2
+DICTSTART = 3
+REVERSE = 32
+
+
+def styledict(name, dict, kwargs, default=0):
+ flags = 0
+ if kwargs.has_key(name):
+ value = kwargs[name]
+ try:
+ value = value.lower()
+ except AttributeError:
+ pass
+ flags |= dict.get(value, default)
+ del kwargs[name]
+ return flags
+
+def styledictstart(name, dict, kwargs, default=0):
+ """ Like styledict, but matches the first letter(s) rather than the
+ complete value. For example, if "lower" is the only key in the dict
+ starting with "l", then by passing the value "l" we match "lower".
+ Used e.g. in _button_align.
+ """
+ flags = 0
+ if kwargs.has_key(name):
+ value = kwargs[name].lower()
+ # note that we traverse the dict pair by pair, so ambiguous matches
+ # *are* possible
+ for key, flag in dict.items():
+ if key.startswith(value):
+ flags |= flag
+ break
+ else:
+ flags |= default
+ del kwargs[name]
+ return flags
+
+def stylebool(name, flag, kwargs, reverse=0):
+ """ If <name> is in kwargs and is true, then return <flag>.
+ If <reverse> is set, then the condition needs to be false in order
+ for the flag to be returned.
+ (E.g. stylebool('lines', wx.BLAH_NOLINES, kwargs, reverse=1)
+ will return wx.BLAH_NOLINES if lines==0, not if lines==1.)
+ """
+ flags = 0
+ if kwargs.has_key(name):
+ value = kwargs[name]
+ if reverse:
+ value = not value
+ if value:
+ flags |= flag
+ del kwargs[name]
+ return flags
+
+def styleboolexclude(name, flag, kwargs, reverse=0):
+ flags = 0
+ if kwargs.has_key(name):
+ value = kwargs[name]
+ if reverse:
+ value = not value
+ if value:
+ flags |= flag
+ del kwargs[name]
+ return flags
+
+def stylebooleither(name, flagtrue, flagfalse, kwargs):
+ flags = 0
+ if kwargs.has_key(name):
+ value = kwargs[name]
+ if value:
+ flags |= flagtrue
+ else:
+ flags |= flagfalse
+ del kwargs[name]
+ return flags
+
+# not sure if this can be used...
+def stylefunc(name, func, kwargs):
+ if kwargs.has_key(name):
+ value = kwargs[name]
+ func(value)
+ del kwargs[value]
+ return 0
+
+#
+#
+#
+def dostyle(styledict, kwargs):
+ """ Set styles for a given control, using a "style dict". Return the
+ eventual flag value. Mostly for internal use. """
+ flags = 0
+ for k, (style, type) in styledict.items():
+ #style, type = styledict[k]
+ # First check for reversing
+ reverse = 0
+ if (type & REVERSE) == REVERSE:
+ reverse = 1
+ # Get everything but the `reverse` bits
+ type = type & (reverse - 1)
+ # Now see what we're dealing with
+ if type == NORMAL:
+ flags |= stylebool(k, style, kwargs, reverse=reverse)
+ elif type == DICT:
+ flags |= styledict(k, style, kwargs)
+ elif type == DICTSTART:
+ flags |= styledictstart(k, style, kwargs)
+ elif type == EXCLUDE:
+ flags &= ~styleboolexclude(k, style, kwargs, reverse=reverse)
+
+ return flags
+#
+# the following functions do not handle flags
+
+def properties(obj, kwargs):
+ """ Called by constructors to set properties from a dict. Must be called
+ *after* the parent's constructor (unlike most of the other styles
+ functions). """
+ if kwargs.has_key('properties'):
+ d = kwargs['properties']
+ for key, value in d.items():
+ setattr(obj, key, value) # attempt to set property
+ del kwargs['properties']
+
+ # attempt to set the remaining attributes as properties (this will raise
+ # an error if an invalid property name is specified).
+ obj.SetProperties(**kwargs)
+
+ # XXX could be a method in WaxyObject... if it doesn't already exist?
+
+
+#
+# window styles
+
+_window_border = {
+ "simple": wx.SIMPLE_BORDER,
+ "double": wx.DOUBLE_BORDER,
+ "sunken": wx.SUNKEN_BORDER,
+ "raised": wx.RAISED_BORDER,
+ "static": wx.STATIC_BORDER,
+ "no": wx.NO_BORDER,
+ "none": wx.NO_BORDER,
+}
+
+def window(kwargs):
+ """ Styles applicable to any wx.Window descendant. """
+ flags = 0
+ flags |= styledict('border', _window_border, kwargs)
+ flags |= stylebool('transparent', wx.TRANSPARENT_WINDOW, kwargs)
+ flags |= stylebool('tab_traversal', wx.TAB_TRAVERSAL, kwargs)
+ flags |= stylebool('wants_chars', wx.WANTS_CHARS, kwargs)
+
+ return flags
+
diff --git a/pynxc/waxy/styles.py~ b/pynxc/waxy/styles.py~
new file mode 100644
index 0000000..ed39f77
--- /dev/null
+++ b/pynxc/waxy/styles.py~
@@ -0,0 +1,160 @@
+# styles.py
+
+import wx
+
+
+NORMAL = 0
+EXCLUDE = 1
+DICT = 2
+DICTSTART = 3
+REVERSE = 32
+
+
+def styledict(name, dict, kwargs, default=0):
+ flags = 0
+ if kwargs.has_key(name):
+ value = kwargs[name]
+ try:
+ value = value.lower()
+ except AttributeError:
+ pass
+ flags |= dict.get(value, default)
+ del kwargs[name]
+ return flags
+
+def styledictstart(name, dict, kwargs, default=0):
+ """ Like styledict, but matches the first letter(s) rather than the
+ complete value. For example, if "lower" is the only key in the dict
+ starting with "l", then by passing the value "l" we match "lower".
+ Used e.g. in _button_align.
+ """
+ flags = 0
+ if kwargs.has_key(name):
+ value = kwargs[name].lower()
+ # note that we traverse the dict pair by pair, so ambiguous matches
+ # *are* possible
+ for key, flag in dict.items():
+ if key.startswith(value):
+ flags |= flag
+ break
+ else:
+ flags |= default
+ del kwargs[name]
+ return flags
+
+def stylebool(name, flag, kwargs, reverse=0):
+ """ If <name> is in kwargs and is true, then return <flag>.
+ If <reverse> is set, then the condition needs to be false in order
+ for the flag to be returned.
+ (E.g. stylebool('lines', wx.BLAH_NOLINES, kwargs, reverse=1)
+ will return wx.BLAH_NOLINES if lines==0, not if lines==1.)
+ """
+ flags = 0
+ if kwargs.has_key(name):
+ value = kwargs[name]
+ if reverse:
+ value = not value
+ if value:
+ flags |= flag
+ del kwargs[name]
+ return flags
+
+def styleboolexclude(name, flag, kwargs, reverse=0):
+ flags = 0
+ if kwargs.has_key(name):
+ value = kwargs[name]
+ if reverse:
+ value = not value
+ if value:
+ flags |= flag
+ del kwargs[name]
+ return flags
+
+def stylebooleither(name, flagtrue, flagfalse, kwargs):
+ flags = 0
+ if kwargs.has_key(name):
+ value = kwargs[name]
+ if value:
+ flags |= flagtrue
+ else:
+ flags |= flagfalse
+ del kwargs[name]
+ return flags
+
+# not sure if this can be used...
+def stylefunc(name, func, kwargs):
+ if kwargs.has_key(name):
+ value = kwargs[name]
+ func(value)
+ del kwargs[value]
+ return 0
+
+#
+#
+#
+def dostyle(styledict, kwargs):
+ """ Set styles for a given control, using a "style dict". Return the
+ eventual flag value. Mostly for internal use. """
+ flags = 0
+ for k, (style, type) in styledict.items():
+ #style, type = styledict[k]
+ # First check for reversing
+ reverse = 0
+ if (type & REVERSE) == REVERSE:
+ reverse = 1
+ # Get everything but the `reverse` bits
+ type = type & (reverse - 1)
+ # Now see what we're dealing with
+ if type == NORMAL:
+ flags |= stylebool(k, style, kwargs, reverse=reverse)
+ elif type == DICT:
+ flags |= styledict(k, style, kwargs)
+ elif type == DICTSTART:
+ flags |= styledictstart(k, style, kwargs)
+ elif type == EXCLUDE:
+ flags &= ~styleboolexclude(k, style, kwargs, reverse=reverse)
+
+ return flags
+#
+# the following functions do not handle flags
+
+def properties(obj, kwargs):
+ """ Called by constructors to set properties from a dict. Must be called
+ *after* the parent's constructor (unlike most of the other styles
+ functions). """
+ if kwargs.has_key('properties'):
+ d = kwargs['properties']
+ for key, value in d.items():
+ setattr(obj, key, value) # attempt to set property
+ del kwargs['properties']
+
+ # attempt to set the remaining attributes as properties (this will raise
+ # an error if an invalid property name is specified).
+ obj.SetProperties(**kwargs)
+
+ # XXX could be a method in WaxObject... if it doesn't already exist?
+
+
+#
+# window styles
+
+_window_border = {
+ "simple": wx.SIMPLE_BORDER,
+ "double": wx.DOUBLE_BORDER,
+ "sunken": wx.SUNKEN_BORDER,
+ "raised": wx.RAISED_BORDER,
+ "static": wx.STATIC_BORDER,
+ "no": wx.NO_BORDER,
+ "none": wx.NO_BORDER,
+}
+
+def window(kwargs):
+ """ Styles applicable to any wx.Window descendant. """
+ flags = 0
+ flags |= styledict('border', _window_border, kwargs)
+ flags |= stylebool('transparent', wx.TRANSPARENT_WINDOW, kwargs)
+ flags |= stylebool('tab_traversal', wx.TAB_TRAVERSAL, kwargs)
+ flags |= stylebool('wants_chars', wx.WANTS_CHARS, kwargs)
+
+ return flags
+
diff --git a/pynxc/waxy/textbox.py b/pynxc/waxy/textbox.py
new file mode 100644
index 0000000..7595b5a
--- /dev/null
+++ b/pynxc/waxy/textbox.py
@@ -0,0 +1,128 @@
+# textbox.py
+
+import waxyobject
+import wx
+import core
+import styles
+
+class TextBox(wx.TextCtrl, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'Char': wx.EVT_CHAR, # do all controls have this?
+ 'MaxLength': wx.EVT_TEXT_MAXLEN, # alias for TextMaxLen
+ 'Text': wx.EVT_TEXT,
+ 'TextEnter': wx.EVT_TEXT_ENTER,
+ 'TextMaxLen': wx.EVT_TEXT_MAXLEN,
+ 'TextURL': wx.EVT_TEXT_URL,
+ }
+
+ def __init__(self, parent, text="", size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.TextCtrl.__init__(self, parent, wx.NewId(), text,
+ size=size or (125,-1), style=style)
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ def write(self, s):
+ # Added so we can use a TextBox as a file-like object and redirect
+ # stdout to it.
+ self.AppendText(s)
+ try:
+ core.Yield()
+ except:
+ pass
+
+ def GetCurrentLineNumber(self):
+ """ Return the current line number (i.e. the number of the line the
+ cursor is on). """
+ pos = self.GetInsertionPoint()
+ x, y = self.PositionToXY(pos)
+ return y
+
+ def GetLines(self):
+ """ Return the current text as a list of lines. (Changing the list
+ does not affect the contents of the TextBox.) """
+ text = self.GetValue()
+ lines = text.split("\n")
+ return lines
+
+ def SetModified(self, modified):
+ if modified:
+ # set to modified by appending a dummy space and removing it again
+ self.AppendText(' ')
+ lastpos = self.GetLastPosition()
+ self.Remove(lastpos-1, lastpos)
+ else:
+ self.DiscardEdits()
+
+ def GetModified(self):
+ """ Returns true if the contents of the control were modified. (Alias
+ for IsModified(). """
+ return self.IsModified()
+
+ def InsertText(self, pos, text):
+ """ Insert text at the given position. """
+ old_insertion_point = self.GetInsertionPoint()
+ self.SetInsertionPoint(pos)
+ self.WriteText(text)
+ # put cursor at original insertion point
+ if old_insertion_point <= pos:
+ self.SetInsertionPoint(old_insertion_point)
+ else:
+ self.SetInsertionPoint(old_insertion_point + len(text))
+
+ # ideas:
+ # should Remove support negative indexes? (like slices)
+ # should it support slicing? e.g. del atextbox[10:20]
+
+ #
+ # style parameters
+
+ #_textbox_justify = {
+ # "left": wx.TE_LEFT,
+ # "center": wx.TE_CENTRE,
+ # "centre": wx.TE_CENTRE,
+ # "middle": wx.TE_CENTRE,
+ # "right": wx.TE_RIGHT,
+ #}
+
+ __styles__ = {
+ 'justify': ({
+ 'left': wx.TE_LEFT,
+ 'center': wx.TE_CENTRE,
+ 'centre': wx.TE_CENTRE,
+ 'middle': wx.TE_CENTRE,
+ 'right': wx.TE_RIGHT,
+ }, styles.DICTSTART),
+ 'multiline': (wx.TE_MULTILINE, styles.NORMAL),
+ 'password': (wx.TE_PASSWORD, styles.NORMAL),
+ 'readonly': (wx.TE_READONLY, styles.NORMAL),
+ 'wrap': (wx.TE_DONTWRAP, styles.NORMAL | styles.REVERSE),
+ 'process_enter': (wx.TE_PROCESS_ENTER, styles.NORMAL),
+ 'process_tab': (wx.TE_PROCESS_TAB, styles.NORMAL),
+ 'rich': (wx.TE_RICH, styles.NORMAL),
+ 'rich2': (wx.TE_RICH2, styles.NORMAL),
+ 'auto_url': (wx.TE_AUTO_URL, styles.NORMAL),
+ 'hscroll': (wx.HSCROLL, styles.NORMAL),
+ }
+
+ def _params(self, kwargs):
+ flags = 0 | wx.TE_NOHIDESEL # maybe add the option of changing this one
+ #flags |= styles.stylebool('multiline', wx.TE_MULTILINE, kwargs)
+ #flags |= styles.stylebool('password', wx.TE_PASSWORD, kwargs)
+ #flags |= styles.stylebool('readonly', wx.TE_READONLY, kwargs)
+ #flags |= styles.stylebool('wrap', wx.TE_DONTWRAP, kwargs, reverse=1)
+ #flags |= styles.stylebool('process_enter', wx.TE_PROCESS_ENTER, kwargs)
+ #flags |= styles.stylebool('process_tab', wx.TE_PROCESS_TAB, kwargs)
+ #flags |= styles.stylebool('rich', wx.TE_RICH, kwargs)
+ #flags |= styles.stylebool('rich2', wx.TE_RICH2, kwargs)
+ #flags |= styles.stylebool('auto_url', wx.TE_AUTO_URL, kwargs)
+ #flags |= styles.stylebool('hscroll', wx.HSCROLL, kwargs)
+ #flags |= styles.styledictstart('justify', self._textbox_justify, kwargs, 0)
+ flags |= styles.dostyle(self.__styles__, kwargs)
+ return flags
+
diff --git a/pynxc/waxy/textbox.py~ b/pynxc/waxy/textbox.py~
new file mode 100644
index 0000000..88d42b4
--- /dev/null
+++ b/pynxc/waxy/textbox.py~
@@ -0,0 +1,128 @@
+# textbox.py
+
+import waxyobject
+import wx
+import core
+import styles
+
+class TextBox(wx.TextCtrl, waxyobject.WaxObject):
+
+ __events__ = {
+ 'Char': wx.EVT_CHAR, # do all controls have this?
+ 'MaxLength': wx.EVT_TEXT_MAXLEN, # alias for TextMaxLen
+ 'Text': wx.EVT_TEXT,
+ 'TextEnter': wx.EVT_TEXT_ENTER,
+ 'TextMaxLen': wx.EVT_TEXT_MAXLEN,
+ 'TextURL': wx.EVT_TEXT_URL,
+ }
+
+ def __init__(self, parent, text="", size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.TextCtrl.__init__(self, parent, wx.NewId(), text,
+ size=size or (125,-1), style=style)
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ def write(self, s):
+ # Added so we can use a TextBox as a file-like object and redirect
+ # stdout to it.
+ self.AppendText(s)
+ try:
+ core.Yield()
+ except:
+ pass
+
+ def GetCurrentLineNumber(self):
+ """ Return the current line number (i.e. the number of the line the
+ cursor is on). """
+ pos = self.GetInsertionPoint()
+ x, y = self.PositionToXY(pos)
+ return y
+
+ def GetLines(self):
+ """ Return the current text as a list of lines. (Changing the list
+ does not affect the contents of the TextBox.) """
+ text = self.GetValue()
+ lines = text.split("\n")
+ return lines
+
+ def SetModified(self, modified):
+ if modified:
+ # set to modified by appending a dummy space and removing it again
+ self.AppendText(' ')
+ lastpos = self.GetLastPosition()
+ self.Remove(lastpos-1, lastpos)
+ else:
+ self.DiscardEdits()
+
+ def GetModified(self):
+ """ Returns true if the contents of the control were modified. (Alias
+ for IsModified(). """
+ return self.IsModified()
+
+ def InsertText(self, pos, text):
+ """ Insert text at the given position. """
+ old_insertion_point = self.GetInsertionPoint()
+ self.SetInsertionPoint(pos)
+ self.WriteText(text)
+ # put cursor at original insertion point
+ if old_insertion_point <= pos:
+ self.SetInsertionPoint(old_insertion_point)
+ else:
+ self.SetInsertionPoint(old_insertion_point + len(text))
+
+ # ideas:
+ # should Remove support negative indexes? (like slices)
+ # should it support slicing? e.g. del atextbox[10:20]
+
+ #
+ # style parameters
+
+ #_textbox_justify = {
+ # "left": wx.TE_LEFT,
+ # "center": wx.TE_CENTRE,
+ # "centre": wx.TE_CENTRE,
+ # "middle": wx.TE_CENTRE,
+ # "right": wx.TE_RIGHT,
+ #}
+
+ __styles__ = {
+ 'justify': ({
+ 'left': wx.TE_LEFT,
+ 'center': wx.TE_CENTRE,
+ 'centre': wx.TE_CENTRE,
+ 'middle': wx.TE_CENTRE,
+ 'right': wx.TE_RIGHT,
+ }, styles.DICTSTART),
+ 'multiline': (wx.TE_MULTILINE, styles.NORMAL),
+ 'password': (wx.TE_PASSWORD, styles.NORMAL),
+ 'readonly': (wx.TE_READONLY, styles.NORMAL),
+ 'wrap': (wx.TE_DONTWRAP, styles.NORMAL | styles.REVERSE),
+ 'process_enter': (wx.TE_PROCESS_ENTER, styles.NORMAL),
+ 'process_tab': (wx.TE_PROCESS_TAB, styles.NORMAL),
+ 'rich': (wx.TE_RICH, styles.NORMAL),
+ 'rich2': (wx.TE_RICH2, styles.NORMAL),
+ 'auto_url': (wx.TE_AUTO_URL, styles.NORMAL),
+ 'hscroll': (wx.HSCROLL, styles.NORMAL),
+ }
+
+ def _params(self, kwargs):
+ flags = 0 | wx.TE_NOHIDESEL # maybe add the option of changing this one
+ #flags |= styles.stylebool('multiline', wx.TE_MULTILINE, kwargs)
+ #flags |= styles.stylebool('password', wx.TE_PASSWORD, kwargs)
+ #flags |= styles.stylebool('readonly', wx.TE_READONLY, kwargs)
+ #flags |= styles.stylebool('wrap', wx.TE_DONTWRAP, kwargs, reverse=1)
+ #flags |= styles.stylebool('process_enter', wx.TE_PROCESS_ENTER, kwargs)
+ #flags |= styles.stylebool('process_tab', wx.TE_PROCESS_TAB, kwargs)
+ #flags |= styles.stylebool('rich', wx.TE_RICH, kwargs)
+ #flags |= styles.stylebool('rich2', wx.TE_RICH2, kwargs)
+ #flags |= styles.stylebool('auto_url', wx.TE_AUTO_URL, kwargs)
+ #flags |= styles.stylebool('hscroll', wx.HSCROLL, kwargs)
+ #flags |= styles.styledictstart('justify', self._textbox_justify, kwargs, 0)
+ flags |= styles.dostyle(self.__styles__, kwargs)
+ return flags
+
diff --git a/pynxc/waxy/textentrydialog.py b/pynxc/waxy/textentrydialog.py
new file mode 100644
index 0000000..afe31ca
--- /dev/null
+++ b/pynxc/waxy/textentrydialog.py
@@ -0,0 +1,179 @@
+# textentrydialog.py
+# Simple dialog for entering a string.
+# Note: Not based on wxPython's TextEntryDialog.
+
+from dialog import Dialog
+from textbox import TextBox
+from label import Label
+from keys import keys
+import string
+
+class TextEntryDialog(Dialog):
+
+ def __init__(self, parent, title="Enter some text", prompt="Enter some text",
+ default="", cancel_button=1):
+ self.prompt = prompt
+ self.default = default
+ Dialog.__init__(self, parent, title, cancel_button=cancel_button)
+
+ def Body(self):
+ label = Label(self, self.prompt)
+ self.AddComponent(label, expand='h', border=7)
+
+ self.text = TextBox(self, size=(100,25), process_enter=1)
+ self.text.SetValue(self.default)
+ self.text.OnChar = self.OnTextBoxChar
+ self.AddComponent(self.text, expand='h', border=5)
+
+ def OnTextBoxChar(self, event=None):
+ # pressing Enter in the TextBox is the same as clicking OK
+ if event.GetKeyCode() == keys.enter:
+ self.OnClickOKButton(event)
+ else:
+ event.Skip()
+
+ def GetValue(self):
+ return self.text.GetValue()
+
+
+
+
+class IntegerInputDialog(TextEntryDialog):
+
+ def __init__(self, parent, title='Enter some text',
+ prompt='Enter some text', default='', cancel_button=1):
+
+ TextEntryDialog.__init__(self, parent, title=title,
+ prompt=prompt, default=str(default),
+ cancel_button=cancel_button)
+
+
+ def GetValue(self):
+
+ try:
+ val=TextEntryDialog.GetValue(self)
+ except ValueError:
+ return None
+
+ try:
+ int_val=eval("int("+val+")")
+ except (SyntaxError,NameError):
+ int_val=None
+
+ return int_val
+
+
+ def OnCharHook(self, event):
+ key = event.KeyCode()
+
+ if key == wx.WXK_ESCAPE:
+ self.OnClickCancelButton()
+
+ if key < wx.WXK_SPACE or key == wx.WXK_DELETE or key > 255:
+ event.Skip()
+ return
+
+
+ good_chars=string.digits+'+*-()'
+ if chr(key) in good_chars:
+ event.Skip()
+ return
+
+ if not wx.Validator_IsSilent():
+ wx.Bell()
+
+ # Returning without calling even.Skip eats the event before it
+ # gets to the text control
+ return
+
+
+
+ def Validate(self):
+ val = TextEntryDialog.GetValue(self) # make sure the get a string
+
+ good_chars=string.digits+'+*-()'
+ for x in val:
+ if x not in good_chars:
+ return False
+
+ try:
+ int_val=eval("int("+val+")")
+ except (SyntaxError,NameError):
+ Error("Syntax Error")
+ return False
+
+ return True
+
+
+def Input_Integer(title,
+ prompt, default=None,parent=None):
+
+
+ a=IntegerInputDialog(parent,title,prompt,default)
+ result=a.ShowModal()
+
+ if result=='ok':
+ r=a.GetValue()
+ else:
+ r=None
+
+ a.Destroy()
+
+ return r
+
+
+class FloatInputDialog(TextEntryDialog):
+
+ def __init__(self, parent, title='Enter some text',
+ prompt='Enter some text', default=None, cancel_button=1):
+
+ TextEntryDialog.__init__(self, parent, title=title,
+ prompt=prompt, default=str(default),
+ cancel_button=cancel_button)
+
+
+ def GetValue(self):
+
+ try:
+ val=TextEntryDialog.GetValue(self)
+ except ValueError:
+ return None
+
+ try:
+ float_val=eval("float("+val+")")
+ except SyntaxError:
+ float_val=None
+
+ return float_val
+
+
+
+ def Validate(self):
+ val = TextEntryDialog.GetValue(self) # make sure the get a string
+
+ try:
+ float_val=eval("float("+val+")")
+ except (SyntaxError,NameError):
+ Error("Syntax Error")
+ return False
+
+ return True
+
+
+
+def Input_Float(title,
+ prompt, default=None,parent=None):
+
+ a=FloatInputDialog(parent,title,prompt,default)
+ result=a.ShowModal()
+
+ if result=='ok':
+ r=a.GetValue()
+ else:
+ r=None
+
+ a.Destroy()
+
+ return r
+
+
diff --git a/pynxc/waxy/treelistview.py b/pynxc/waxy/treelistview.py
new file mode 100644
index 0000000..e4d3d40
--- /dev/null
+++ b/pynxc/waxy/treelistview.py
@@ -0,0 +1,28 @@
+# treelistview.py
+
+import styles
+import wx
+import wx.gizmos as gizmos
+import waxyobject
+import treeview
+
+class TreeListView(gizmos.TreeListCtrl, waxyobject.WaxyObject):
+
+ def __init__(self, parent, columns=(), size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+ gizmos.TreeListCtrl.__init__(self, parent, wx.NewId(),
+ size=size or (-1,-1), style=style)
+
+ for name in columns:
+ self.AddColumn(name)
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ # some deviltry to copy styles stuff from TreeView...
+ _treeview_selection = treeview.TreeView._treeview_selection
+ _params = treeview.TreeView._params.im_func
+ # I'll come up with a better solution later.
+
diff --git a/pynxc/waxy/treeview.py b/pynxc/waxy/treeview.py
new file mode 100644
index 0000000..eea90ca
--- /dev/null
+++ b/pynxc/waxy/treeview.py
@@ -0,0 +1,119 @@
+# treeview.py
+
+# todo: can we access tree like a dict?
+
+from __future__ import generators
+import wx
+import waxyobject
+import styles
+import types
+import utils
+
+class TreeView(wx.TreeCtrl, waxyobject.WaxyObject):
+
+ __events__ = {
+ 'ItemExpanded': wx.EVT_TREE_ITEM_EXPANDED,
+ 'ItemExpanding': wx.EVT_TREE_ITEM_EXPANDING,
+ 'ItemCollapsed': wx.EVT_TREE_ITEM_COLLAPSED,
+ 'ItemCollapsing': wx.EVT_TREE_ITEM_COLLAPSING,
+ 'SelectionChanged': wx.EVT_TREE_SEL_CHANGED,
+ 'BeginEdit': wx.EVT_TREE_BEGIN_LABEL_EDIT,
+ 'EndEdit': wx.EVT_TREE_END_LABEL_EDIT,
+ 'ItemActivated': wx.EVT_TREE_ITEM_ACTIVATED,
+ 'BeginDrag': wx.EVT_TREE_BEGIN_DRAG,
+ 'BeginRightDrag': wx.EVT_TREE_BEGIN_RDRAG,
+ 'DeleteItem': wx.EVT_TREE_DELETE_ITEM,
+ }
+
+ def __init__(self, parent, size=None, **kwargs):
+ style = 0
+ style |= self._params(kwargs)
+ style |= styles.window(kwargs)
+
+ wx.TreeCtrl.__init__(self, parent, wx.NewId(), size=size or (-1,-1),
+ style=style)
+
+ self.BindEvents()
+ styles.properties(self, kwargs)
+
+ def GetChildNodes(self, node):
+ """ Generator returning a node's children. <node> has to be a tree
+ node, e.g. the root, or any item. """
+ child, cookie = self.GetFirstChild(node)
+ while child.IsOk():
+ yield child
+ child, cookie = self.GetNextChild(node, cookie)
+
+ def HasChildren(self, node):
+ child, cookie = self.GetFirstChild(node)
+ return child.IsOk()
+
+ def Clear(self):
+ self.DeleteAllItems()
+ # Clear() is used for other controls, so it makes sense to use it here
+ # as well.
+
+ def LoadFromDict(self, node, adict):
+ # XXX extend this to work with objects
+ items = adict.items()
+ items.sort()
+ for key, value in items:
+ if isinstance(value, dict):
+ child = self.AppendItem(node, utils.asstring(key))
+ self.SetPyData(child, None) # hmm...
+ self.LoadFromDict(child, value)
+ else:
+ child = self.AppendItem(node, utils.asstring(key))
+ self.SetPyData(child, value)
+
+ def LoadFromNestedList(self, root, list):
+ """
+ loads a nested list of (key, value, [children]) tuples (or lists)
+ as children of the given root node. preserves the
+ order of elements as they appear in the list.
+ """
+ for item in list:
+ if (type(item) not in (types.TupleType, types.ListType)
+ or len(item) not in (0, 3)):
+ raise Exception, "LoadFromNestedList requires a list of (key, value, children) tuples"
+ (key, value, children) = item
+ node = self.AppendItem(root, utils.asstring(key))
+ self.SetPyData(node, value)
+ if type(children) == types.ListType:
+ self.LoadFromNestedList(node, children)
+
+ def SetImageList(self, imagelist):
+ wx.TreeCtrl.SetImageList(self, imagelist)
+ self._imagelist = imagelist
+ # a copy must be kept around, or the images will be freed
+
+ def SetItemImage(self, node, index, expanded=0):
+ return wx.TreeCtrl.SetItemImage(self, node, index, [wx.TreeItemIcon_Normal, wx.TreeItemIcon_Expanded][expanded])
+
+ #
+ # style parameters
+
+ _treeview_selection = {
+ 'single': wx.TR_SINGLE,
+ 'multiple': wx.TR_MULTIPLE,
+ 'extended': wx.TR_EXTENDED,
+ }
+
+ def _params(self, kwargs):
+ flags = wx.TR_DEFAULT_STYLE
+ flags |= styles.stylebool('edit_labels', wx.TR_EDIT_LABELS, kwargs)
+ flags |= styles.stylebool('twist_buttons', wx.TR_TWIST_BUTTONS, kwargs)
+ flags |= styles.stylebool('lines', wx.TR_NO_LINES, kwargs, reverse=1)
+ flags |= styles.stylebool('hide_root', wx.TR_HIDE_ROOT, kwargs)
+ flags |= styles.styledictstart('selection', self._treeview_selection, kwargs)
+
+ # has_buttons requires special code:
+ if kwargs.has_key('has_buttons'):
+ if kwargs['has_buttons']:
+ flags |= wx.TR_HAS_BUTTONS
+ else:
+ flags &= ~wx.TR_HAS_BUTTONS # make sure flag is NOT set
+ del kwargs['has_buttons']
+ # Note that TR_NO_BUTTONS is useless... it's 0!
+
+ return flags
diff --git a/pynxc/waxy/utils.py b/pynxc/waxy/utils.py
new file mode 100644
index 0000000..467b325
--- /dev/null
+++ b/pynxc/waxy/utils.py
@@ -0,0 +1,29 @@
+# tools.py
+# Miscellaneous utility functions.
+
+import os
+import warnings
+
+def asstring(x):
+ """ Ensure that x is a string, i.e. if it's not of the str or unicode
+ types, convert it with str. Otherwise, leave it unchanged. """
+ if not (isinstance(x, str) or isinstance(x, unicode)):
+ x = str(x)
+ return x
+
+def opj(path):
+ """Convert paths to the platform-specific separator"""
+ str = apply(os.path.join, tuple(path.split('/')))
+ # HACK: on Linux, a leading / gets lost...
+ if path.startswith('/'):
+ str = '/' + str
+ return str
+
+class CheckParentWarning(Warning):
+ pass
+
+def parent_warning(obj, container):
+ s = "obj %r has parent %r, but is added to container %r" % (
+ obj, obj.GetParent(), container)
+ warnings.warn(s, CheckParentWarning)
+
diff --git a/pynxc/waxy/waxyobject.py b/pynxc/waxy/waxyobject.py
new file mode 100644
index 0000000..952dd27
--- /dev/null
+++ b/pynxc/waxy/waxyobject.py
@@ -0,0 +1,246 @@
+# waxyobject.py
+
+import wx
+import core
+import colordb
+import events
+import mousepointer
+import styles
+
+class MetaWaxyObject(type):
+
+## def inject_SetFont(cls):
+## if hasattr(cls, "SetFont"):
+## real_SetFont = getattr(cls, "SetFont")
+## def SetFont(self, obj):
+## if isinstance(obj, tuple):
+## import font
+## obj = font.Font(*obj)
+## real_SetFont(self, obj)
+## SetFont.__doc__ = real_SetFont.__doc__
+## setattr(cls, "SetFont", SetFont)
+## if core.DEBUG:
+## print "%s: SetFont replaced" % (cls,)
+##
+## def inject_GetFont(cls):
+## if hasattr(cls, "GetFont"):
+## real_GetFont = getattr(cls, "GetFont")
+## import font
+## def GetFont(self):
+## wxfont = real_GetFont(self)
+## wxfont.__class__ = font.Font
+## return wxfont
+## GetFont.__doc__ = real_GetFont.__doc__
+## setattr(cls, "GetFont", GetFont)
+## if core.DEBUG:
+## print "%s: GetFont replaced" % (cls,)
+
+ def inject_SetCursor(cls):
+ if hasattr(cls, "SetCursor"):
+ real_SetCursor = getattr(cls, "SetCursor")
+ def SetCursor(self, x):
+ if isinstance(x, basestring):
+ c = mousepointer.MousePointers.Get(x)
+ real_SetCursor(self, c)
+ else:
+ real_SetCursor(self, x)
+ SetCursor.__doc__ = real_SetCursor.__doc__
+ setattr(cls, "SetCursor", SetCursor)
+ if core.DEBUG:
+ print "%s: SetCursor replaced" % (cls,)
+
+ def inject_SetWindowStyle(cls):
+ if hasattr(cls, "SetWindowStyle"):
+ real_SetWindowStyle = getattr(cls, "SetWindowStyle")
+ def SetWindowStyle(self, __default__=0, **kwargs):
+ """ Wax-style SetWindowStyle that supports the old way of
+ setting styles (using an integer flag value), and/or
+ named keywords, depending on the class's _params method.
+ """
+ flags = __default__
+ flags |= self._params(kwargs)
+ if isinstance(self, wx.Window):
+ flags |= styles.window(kwargs)
+ real_SetWindowStyle(self, flags)
+ setattr(cls, "SetWindowStyle", SetWindowStyle)
+ if core.DEBUG:
+ print "%s: SetWindowStyle replaced" % (cls,)
+
+ def inject_ColorMethods(cls):
+ if hasattr(cls, "SetBackgroundColour"):
+ real_SetBackgroundColour = getattr(cls, "SetBackgroundColour")
+ real_SetForegroundColour = getattr(cls, "SetForegroundColour")
+
+ def SetForegroundColour(self, color):
+ color = colordb.convert_color(color)
+ real_SetForegroundColour(self, color)
+ SetForegroundColour.__doc__ = real_SetForegroundColour.__doc__
+ setattr(cls, "SetForegroundColour", SetForegroundColour)
+
+ def SetBackgroundColour(self, color):
+ color = colordb.convert_color(color)
+ real_SetBackgroundColour(self, color)
+ SetBackgroundColour.__doc__ = real_SetBackgroundColour.__doc__
+ setattr(cls, "SetBackgroundColour", SetBackgroundColour)
+
+ # make aliases for "Color"
+ setattr(cls, "SetBackgroundColor", cls.SetBackgroundColour)
+ setattr(cls, "SetForegroundColor", cls.SetForegroundColour)
+ setattr(cls, "GetBackgroundColor", cls.GetBackgroundColour)
+ setattr(cls, "GetForegroundColor", cls.GetForegroundColour)
+
+ if core.DEBUG:
+ print "%s: SetForegroundColour/SetBackgroundColour replaced" % (cls,)
+
+ def __init__(cls, name, bases, dict):
+# cls.inject_SetFont()
+# cls.inject_GetFont()
+ cls.inject_SetCursor()
+ cls.inject_ColorMethods()
+ cls.inject_SetWindowStyle()
+
+
+class WaxyObject:
+ """ Mixin class for Wax controls.
+ Stick attributes and methods here that every Wax control should have.
+ """
+ __metaclass__ = MetaWaxyObject
+ # yes, I don't like metaclasses, but they're actually useful here :-)
+
+ __events__ = {}
+ __styles__ = {}
+
+## def SetDefaultFont(self):
+## if hasattr(self, 'SetFont'):
+## self.SetFont(waxconfig.WaxConfig.default_font)
+
+ def SetSizeX(self, ix):
+ x, y = self.GetSize()
+ self.SetSize((ix, y))
+
+ def SetSizeY(self, iy):
+ x, y = self.GetSize()
+ self.SetSize((x, iy))
+
+ def GetSizeX(self):
+ return self.GetSize()[0]
+
+ def GetSizeY(self):
+ return self.GetSize()[1]
+
+ def BindEvents(self):
+ items = []
+ if hasattr(self, "__events__"):
+ items = getattr(self, "__events__").items()
+ items.extend(events.events.items())
+ for name, wxevent in items:
+ if hasattr(self, "On" + name):
+ f = getattr(self, "On" + name)
+ self.Bind(wxevent, f)
+ if core.DEBUG:
+ print "Binding %s to %s" % (name, f)
+
+ def GetAllChildren(self):
+ """ Return a generator returning all children, grandchildren, etc,
+ of the given widget. The widgets are traversed depth-first.
+ """
+ if hasattr(self, 'GetChildren'):
+ for child in self.GetChildren():
+ yield child
+ for grandchild in child.GetAllChildren():
+ yield grandchild
+
+ #
+ # pseudo-properties
+
+ def __getattr__(self, name):
+ if hasattr(self.__class__, "Get" + name):
+ # use self.__class__ rather than self to avoid recursion
+ f = getattr(self, "Get" + name)
+ return f()
+ else:
+ raise AttributeError, name
+
+ def __setattr__(self, name, value):
+ if hasattr(self, "Set" + name):
+ f = getattr(self, "Set" + name)
+ return f(value)
+ elif name.startswith("On"):
+ shortname = name[2:]
+ if self.__events__.has_key(shortname):
+ wxevent = self.__events__[shortname]
+ elif events.events.has_key(shortname):
+ wxevent = events.events[shortname]
+ else:
+ wxevent = None
+ self.__dict__[name] = value
+ if wxevent:
+ self.Bind(wxevent, value)
+ if core.DEBUG:
+ print "Binding %s to %s" % (name, value)
+ else:
+ self.__dict__[name] = value
+
+ def SetAttributes(self, **kwargs):
+ """ Set a number of attributes at once. E.g.
+ widget.SetAttributes(Font=MYFONT, Size=(100,200))
+ """
+ for (key, value) in kwargs.items():
+ self.__setattr__(key, value)
+
+ def SetProperties(self, **kwargs):
+ """ As SetAttributes, but raises an error if an unknown property name
+ is specified. """
+ for (key, value) in kwargs.items():
+ f = getattr(self, "Set" + key, None)
+ if f:
+ f(value)
+ else:
+ raise AttributeError, "Unknown property: %s" % (key,)
+
+ def _params(self, kwargs, thesestyles={}):
+ # default method for those controls that don't have a _params()
+ if thesestyles:
+ return styles.dostyle(thesestyles, kwargs)
+ else:
+ return styles.dostyle(self.__styles__, kwargs)
+
+ def HasStyle(self, style, substyle=None):
+ """ Returns whether or not a style is set for this object. """
+ try:
+ sval = self.__styles__[style][0]
+ if isinstance(sval, dict):
+ # The style returned is a complex style so...
+ if substyle:
+ # ...checking for a specific substyle, return if it's set
+ subval = sval[substyle]
+ ws = self.GetWindowStyle()
+ # If we're dealing with a zero based style, things
+ # get a bit trickier
+ if subval == 0:
+ for k in sval:
+ temp = sval[k]
+ if k != substyle and (ws & temp) == temp:
+ return False
+ return (ws & subval) == subval
+ else:
+ # ...not checking for specific, so see if either is set and
+ # return the key value associated with what is set
+ mystyle = self.GetWindowStyle()
+ for k in sval:
+ v = sval[k]
+ if (mystyle & v) == v:
+ return k
+ return False
+ else:
+ # Style is not complex, return if it is set or not
+ return (self.GetWindowStyle() & sval) == sval
+ except:
+ return False
+
+ def GetStyleDict(self):
+ """ Returns a dictionary with style info for this object. """
+ ret = {}
+ for k in self.__styles__:
+ ret[k] = self.HasStyle(k)
+ return ret
diff --git a/pynxc/waxy/waxyobject.py~ b/pynxc/waxy/waxyobject.py~
new file mode 100644
index 0000000..a11937e
--- /dev/null
+++ b/pynxc/waxy/waxyobject.py~
@@ -0,0 +1,246 @@
+# waxobject.py
+
+import wx
+import core
+import colordb
+import events
+import mousepointer
+import styles
+
+class MetaWaxyObject(type):
+
+## def inject_SetFont(cls):
+## if hasattr(cls, "SetFont"):
+## real_SetFont = getattr(cls, "SetFont")
+## def SetFont(self, obj):
+## if isinstance(obj, tuple):
+## import font
+## obj = font.Font(*obj)
+## real_SetFont(self, obj)
+## SetFont.__doc__ = real_SetFont.__doc__
+## setattr(cls, "SetFont", SetFont)
+## if core.DEBUG:
+## print "%s: SetFont replaced" % (cls,)
+##
+## def inject_GetFont(cls):
+## if hasattr(cls, "GetFont"):
+## real_GetFont = getattr(cls, "GetFont")
+## import font
+## def GetFont(self):
+## wxfont = real_GetFont(self)
+## wxfont.__class__ = font.Font
+## return wxfont
+## GetFont.__doc__ = real_GetFont.__doc__
+## setattr(cls, "GetFont", GetFont)
+## if core.DEBUG:
+## print "%s: GetFont replaced" % (cls,)
+
+ def inject_SetCursor(cls):
+ if hasattr(cls, "SetCursor"):
+ real_SetCursor = getattr(cls, "SetCursor")
+ def SetCursor(self, x):
+ if isinstance(x, basestring):
+ c = mousepointer.MousePointers.Get(x)
+ real_SetCursor(self, c)
+ else:
+ real_SetCursor(self, x)
+ SetCursor.__doc__ = real_SetCursor.__doc__
+ setattr(cls, "SetCursor", SetCursor)
+ if core.DEBUG:
+ print "%s: SetCursor replaced" % (cls,)
+
+ def inject_SetWindowStyle(cls):
+ if hasattr(cls, "SetWindowStyle"):
+ real_SetWindowStyle = getattr(cls, "SetWindowStyle")
+ def SetWindowStyle(self, __default__=0, **kwargs):
+ """ Wax-style SetWindowStyle that supports the old way of
+ setting styles (using an integer flag value), and/or
+ named keywords, depending on the class's _params method.
+ """
+ flags = __default__
+ flags |= self._params(kwargs)
+ if isinstance(self, wx.Window):
+ flags |= styles.window(kwargs)
+ real_SetWindowStyle(self, flags)
+ setattr(cls, "SetWindowStyle", SetWindowStyle)
+ if core.DEBUG:
+ print "%s: SetWindowStyle replaced" % (cls,)
+
+ def inject_ColorMethods(cls):
+ if hasattr(cls, "SetBackgroundColour"):
+ real_SetBackgroundColour = getattr(cls, "SetBackgroundColour")
+ real_SetForegroundColour = getattr(cls, "SetForegroundColour")
+
+ def SetForegroundColour(self, color):
+ color = colordb.convert_color(color)
+ real_SetForegroundColour(self, color)
+ SetForegroundColour.__doc__ = real_SetForegroundColour.__doc__
+ setattr(cls, "SetForegroundColour", SetForegroundColour)
+
+ def SetBackgroundColour(self, color):
+ color = colordb.convert_color(color)
+ real_SetBackgroundColour(self, color)
+ SetBackgroundColour.__doc__ = real_SetBackgroundColour.__doc__
+ setattr(cls, "SetBackgroundColour", SetBackgroundColour)
+
+ # make aliases for "Color"
+ setattr(cls, "SetBackgroundColor", cls.SetBackgroundColour)
+ setattr(cls, "SetForegroundColor", cls.SetForegroundColour)
+ setattr(cls, "GetBackgroundColor", cls.GetBackgroundColour)
+ setattr(cls, "GetForegroundColor", cls.GetForegroundColour)
+
+ if core.DEBUG:
+ print "%s: SetForegroundColour/SetBackgroundColour replaced" % (cls,)
+
+ def __init__(cls, name, bases, dict):
+# cls.inject_SetFont()
+# cls.inject_GetFont()
+ cls.inject_SetCursor()
+ cls.inject_ColorMethods()
+ cls.inject_SetWindowStyle()
+
+
+class WaxyObject:
+ """ Mixin class for Wax controls.
+ Stick attributes and methods here that every Wax control should have.
+ """
+ __metaclass__ = MetaWaxyObject
+ # yes, I don't like metaclasses, but they're actually useful here :-)
+
+ __events__ = {}
+ __styles__ = {}
+
+## def SetDefaultFont(self):
+## if hasattr(self, 'SetFont'):
+## self.SetFont(waxconfig.WaxConfig.default_font)
+
+ def SetSizeX(self, ix):
+ x, y = self.GetSize()
+ self.SetSize((ix, y))
+
+ def SetSizeY(self, iy):
+ x, y = self.GetSize()
+ self.SetSize((x, iy))
+
+ def GetSizeX(self):
+ return self.GetSize()[0]
+
+ def GetSizeY(self):
+ return self.GetSize()[1]
+
+ def BindEvents(self):
+ items = []
+ if hasattr(self, "__events__"):
+ items = getattr(self, "__events__").items()
+ items.extend(events.events.items())
+ for name, wxevent in items:
+ if hasattr(self, "On" + name):
+ f = getattr(self, "On" + name)
+ self.Bind(wxevent, f)
+ if core.DEBUG:
+ print "Binding %s to %s" % (name, f)
+
+ def GetAllChildren(self):
+ """ Return a generator returning all children, grandchildren, etc,
+ of the given widget. The widgets are traversed depth-first.
+ """
+ if hasattr(self, 'GetChildren'):
+ for child in self.GetChildren():
+ yield child
+ for grandchild in child.GetAllChildren():
+ yield grandchild
+
+ #
+ # pseudo-properties
+
+ def __getattr__(self, name):
+ if hasattr(self.__class__, "Get" + name):
+ # use self.__class__ rather than self to avoid recursion
+ f = getattr(self, "Get" + name)
+ return f()
+ else:
+ raise AttributeError, name
+
+ def __setattr__(self, name, value):
+ if hasattr(self, "Set" + name):
+ f = getattr(self, "Set" + name)
+ return f(value)
+ elif name.startswith("On"):
+ shortname = name[2:]
+ if self.__events__.has_key(shortname):
+ wxevent = self.__events__[shortname]
+ elif events.events.has_key(shortname):
+ wxevent = events.events[shortname]
+ else:
+ wxevent = None
+ self.__dict__[name] = value
+ if wxevent:
+ self.Bind(wxevent, value)
+ if core.DEBUG:
+ print "Binding %s to %s" % (name, value)
+ else:
+ self.__dict__[name] = value
+
+ def SetAttributes(self, **kwargs):
+ """ Set a number of attributes at once. E.g.
+ widget.SetAttributes(Font=MYFONT, Size=(100,200))
+ """
+ for (key, value) in kwargs.items():
+ self.__setattr__(key, value)
+
+ def SetProperties(self, **kwargs):
+ """ As SetAttributes, but raises an error if an unknown property name
+ is specified. """
+ for (key, value) in kwargs.items():
+ f = getattr(self, "Set" + key, None)
+ if f:
+ f(value)
+ else:
+ raise AttributeError, "Unknown property: %s" % (key,)
+
+ def _params(self, kwargs, thesestyles={}):
+ # default method for those controls that don't have a _params()
+ if thesestyles:
+ return styles.dostyle(thesestyles, kwargs)
+ else:
+ return styles.dostyle(self.__styles__, kwargs)
+
+ def HasStyle(self, style, substyle=None):
+ """ Returns whether or not a style is set for this object. """
+ try:
+ sval = self.__styles__[style][0]
+ if isinstance(sval, dict):
+ # The style returned is a complex style so...
+ if substyle:
+ # ...checking for a specific substyle, return if it's set
+ subval = sval[substyle]
+ ws = self.GetWindowStyle()
+ # If we're dealing with a zero based style, things
+ # get a bit trickier
+ if subval == 0:
+ for k in sval:
+ temp = sval[k]
+ if k != substyle and (ws & temp) == temp:
+ return False
+ return (ws & subval) == subval
+ else:
+ # ...not checking for specific, so see if either is set and
+ # return the key value associated with what is set
+ mystyle = self.GetWindowStyle()
+ for k in sval:
+ v = sval[k]
+ if (mystyle & v) == v:
+ return k
+ return False
+ else:
+ # Style is not complex, return if it is set or not
+ return (self.GetWindowStyle() & sval) == sval
+ except:
+ return False
+
+ def GetStyleDict(self):
+ """ Returns a dictionary with style info for this object. """
+ ret = {}
+ for k in self.__styles__:
+ ret[k] = self.HasStyle(k)
+ return ret
diff --git a/pynxc/yaml/__init__.py b/pynxc/yaml/__init__.py
new file mode 100644
index 0000000..c0fd1f3
--- /dev/null
+++ b/pynxc/yaml/__init__.py
@@ -0,0 +1,288 @@
+
+from error import *
+
+from tokens import *
+from events import *
+from nodes import *
+
+from loader import *
+from dumper import *
+
+__version__ = '3.09'
+
+try:
+ from cyaml import *
+ __with_libyaml__ = True
+except ImportError:
+ __with_libyaml__ = False
+
+def scan(stream, Loader=Loader):
+ """
+ Scan a YAML stream and produce scanning tokens.
+ """
+ loader = Loader(stream)
+ while loader.check_token():
+ yield loader.get_token()
+
+def parse(stream, Loader=Loader):
+ """
+ Parse a YAML stream and produce parsing events.
+ """
+ loader = Loader(stream)
+ while loader.check_event():
+ yield loader.get_event()
+
+def compose(stream, Loader=Loader):
+ """
+ Parse the first YAML document in a stream
+ and produce the corresponding representation tree.
+ """
+ loader = Loader(stream)
+ return loader.get_single_node()
+
+def compose_all(stream, Loader=Loader):
+ """
+ Parse all YAML documents in a stream
+ and produce corresponding representation trees.
+ """
+ loader = Loader(stream)
+ while loader.check_node():
+ yield loader.get_node()
+
+def load(stream, Loader=Loader):
+ """
+ Parse the first YAML document in a stream
+ and produce the corresponding Python object.
+ """
+ loader = Loader(stream)
+ return loader.get_single_data()
+
+def load_all(stream, Loader=Loader):
+ """
+ Parse all YAML documents in a stream
+ and produce corresponding Python objects.
+ """
+ loader = Loader(stream)
+ while loader.check_data():
+ yield loader.get_data()
+
+def safe_load(stream):
+ """
+ Parse the first YAML document in a stream
+ and produce the corresponding Python object.
+ Resolve only basic YAML tags.
+ """
+ return load(stream, SafeLoader)
+
+def safe_load_all(stream):
+ """
+ Parse all YAML documents in a stream
+ and produce corresponding Python objects.
+ Resolve only basic YAML tags.
+ """
+ return load_all(stream, SafeLoader)
+
+def emit(events, stream=None, Dumper=Dumper,
+ canonical=None, indent=None, width=None,
+ allow_unicode=None, line_break=None):
+ """
+ Emit YAML parsing events into a stream.
+ If stream is None, return the produced string instead.
+ """
+ getvalue = None
+ if stream is None:
+ from StringIO import StringIO
+ stream = StringIO()
+ getvalue = stream.getvalue
+ dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
+ allow_unicode=allow_unicode, line_break=line_break)
+ for event in events:
+ dumper.emit(event)
+ if getvalue:
+ return getvalue()
+
+def serialize_all(nodes, stream=None, Dumper=Dumper,
+ canonical=None, indent=None, width=None,
+ allow_unicode=None, line_break=None,
+ encoding='utf-8', explicit_start=None, explicit_end=None,
+ version=None, tags=None):
+ """
+ Serialize a sequence of representation trees into a YAML stream.
+ If stream is None, return the produced string instead.
+ """
+ getvalue = None
+ if stream is None:
+ if encoding is None:
+ from StringIO import StringIO
+ else:
+ from cStringIO import StringIO
+ stream = StringIO()
+ getvalue = stream.getvalue
+ dumper = Dumper(stream, canonical=canonical, indent=indent, width=width,
+ allow_unicode=allow_unicode, line_break=line_break,
+ encoding=encoding, version=version, tags=tags,
+ explicit_start=explicit_start, explicit_end=explicit_end)
+ dumper.open()
+ for node in nodes:
+ dumper.serialize(node)
+ dumper.close()
+ if getvalue:
+ return getvalue()
+
+def serialize(node, stream=None, Dumper=Dumper, **kwds):
+ """
+ Serialize a representation tree into a YAML stream.
+ If stream is None, return the produced string instead.
+ """
+ return serialize_all([node], stream, Dumper=Dumper, **kwds)
+
+def dump_all(documents, stream=None, Dumper=Dumper,
+ default_style=None, default_flow_style=None,
+ canonical=None, indent=None, width=None,
+ allow_unicode=None, line_break=None,
+ encoding='utf-8', explicit_start=None, explicit_end=None,
+ version=None, tags=None):
+ """
+ Serialize a sequence of Python objects into a YAML stream.
+ If stream is None, return the produced string instead.
+ """
+ getvalue = None
+ if stream is None:
+ if encoding is None:
+ from StringIO import StringIO
+ else:
+ from cStringIO import StringIO
+ stream = StringIO()
+ getvalue = stream.getvalue
+ dumper = Dumper(stream, default_style=default_style,
+ default_flow_style=default_flow_style,
+ canonical=canonical, indent=indent, width=width,
+ allow_unicode=allow_unicode, line_break=line_break,
+ encoding=encoding, version=version, tags=tags,
+ explicit_start=explicit_start, explicit_end=explicit_end)
+ dumper.open()
+ for data in documents:
+ dumper.represent(data)
+ dumper.close()
+ if getvalue:
+ return getvalue()
+
+def dump(data, stream=None, Dumper=Dumper, **kwds):
+ """
+ Serialize a Python object into a YAML stream.
+ If stream is None, return the produced string instead.
+ """
+ return dump_all([data], stream, Dumper=Dumper, **kwds)
+
+def safe_dump_all(documents, stream=None, **kwds):
+ """
+ Serialize a sequence of Python objects into a YAML stream.
+ Produce only basic YAML tags.
+ If stream is None, return the produced string instead.
+ """
+ return dump_all(documents, stream, Dumper=SafeDumper, **kwds)
+
+def safe_dump(data, stream=None, **kwds):
+ """
+ Serialize a Python object into a YAML stream.
+ Produce only basic YAML tags.
+ If stream is None, return the produced string instead.
+ """
+ return dump_all([data], stream, Dumper=SafeDumper, **kwds)
+
+def add_implicit_resolver(tag, regexp, first=None,
+ Loader=Loader, Dumper=Dumper):
+ """
+ Add an implicit scalar detector.
+ If an implicit scalar value matches the given regexp,
+ the corresponding tag is assigned to the scalar.
+ first is a sequence of possible initial characters or None.
+ """
+ Loader.add_implicit_resolver(tag, regexp, first)
+ Dumper.add_implicit_resolver(tag, regexp, first)
+
+def add_path_resolver(tag, path, kind=None, Loader=Loader, Dumper=Dumper):
+ """
+ Add a path based resolver for the given tag.
+ A path is a list of keys that forms a path
+ to a node in the representation tree.
+ Keys can be string values, integers, or None.
+ """
+ Loader.add_path_resolver(tag, path, kind)
+ Dumper.add_path_resolver(tag, path, kind)
+
+def add_constructor(tag, constructor, Loader=Loader):
+ """
+ Add a constructor for the given tag.
+ Constructor is a function that accepts a Loader instance
+ and a node object and produces the corresponding Python object.
+ """
+ Loader.add_constructor(tag, constructor)
+
+def add_multi_constructor(tag_prefix, multi_constructor, Loader=Loader):
+ """
+ Add a multi-constructor for the given tag prefix.
+ Multi-constructor is called for a node if its tag starts with tag_prefix.
+ Multi-constructor accepts a Loader instance, a tag suffix,
+ and a node object and produces the corresponding Python object.
+ """
+ Loader.add_multi_constructor(tag_prefix, multi_constructor)
+
+def add_representer(data_type, representer, Dumper=Dumper):
+ """
+ Add a representer for the given type.
+ Representer is a function accepting a Dumper instance
+ and an instance of the given data type
+ and producing the corresponding representation node.
+ """
+ Dumper.add_representer(data_type, representer)
+
+def add_multi_representer(data_type, multi_representer, Dumper=Dumper):
+ """
+ Add a representer for the given type.
+ Multi-representer is a function accepting a Dumper instance
+ and an instance of the given data type or subtype
+ and producing the corresponding representation node.
+ """
+ Dumper.add_multi_representer(data_type, multi_representer)
+
+class YAMLObjectMetaclass(type):
+ """
+ The metaclass for YAMLObject.
+ """
+ def __init__(cls, name, bases, kwds):
+ super(YAMLObjectMetaclass, cls).__init__(name, bases, kwds)
+ if 'yaml_tag' in kwds and kwds['yaml_tag'] is not None:
+ cls.yaml_loader.add_constructor(cls.yaml_tag, cls.from_yaml)
+ cls.yaml_dumper.add_representer(cls, cls.to_yaml)
+
+class YAMLObject(object):
+ """
+ An object that can dump itself to a YAML stream
+ and load itself from a YAML stream.
+ """
+
+ __metaclass__ = YAMLObjectMetaclass
+ __slots__ = () # no direct instantiation, so allow immutable subclasses
+
+ yaml_loader = Loader
+ yaml_dumper = Dumper
+
+ yaml_tag = None
+ yaml_flow_style = None
+
+ def from_yaml(cls, loader, node):
+ """
+ Convert a representation node to a Python object.
+ """
+ return loader.construct_yaml_object(node, cls)
+ from_yaml = classmethod(from_yaml)
+
+ def to_yaml(cls, dumper, data):
+ """
+ Convert a Python object to a representation node.
+ """
+ return dumper.represent_yaml_object(cls.yaml_tag, data, cls,
+ flow_style=cls.yaml_flow_style)
+ to_yaml = classmethod(to_yaml)
+
diff --git a/pynxc/yaml/composer.py b/pynxc/yaml/composer.py
new file mode 100644
index 0000000..06e5ac7
--- /dev/null
+++ b/pynxc/yaml/composer.py
@@ -0,0 +1,139 @@
+
+__all__ = ['Composer', 'ComposerError']
+
+from error import MarkedYAMLError
+from events import *
+from nodes import *
+
+class ComposerError(MarkedYAMLError):
+ pass
+
+class Composer(object):
+
+ def __init__(self):
+ self.anchors = {}
+
+ def check_node(self):
+ # Drop the STREAM-START event.
+ if self.check_event(StreamStartEvent):
+ self.get_event()
+
+ # If there are more documents available?
+ return not self.check_event(StreamEndEvent)
+
+ def get_node(self):
+ # Get the root node of the next document.
+ if not self.check_event(StreamEndEvent):
+ return self.compose_document()
+
+ def get_single_node(self):
+ # Drop the STREAM-START event.
+ self.get_event()
+
+ # Compose a document if the stream is not empty.
+ document = None
+ if not self.check_event(StreamEndEvent):
+ document = self.compose_document()
+
+ # Ensure that the stream contains no more documents.
+ if not self.check_event(StreamEndEvent):
+ event = self.get_event()
+ raise ComposerError("expected a single document in the stream",
+ document.start_mark, "but found another document",
+ event.start_mark)
+
+ # Drop the STREAM-END event.
+ self.get_event()
+
+ return document
+
+ def compose_document(self):
+ # Drop the DOCUMENT-START event.
+ self.get_event()
+
+ # Compose the root node.
+ node = self.compose_node(None, None)
+
+ # Drop the DOCUMENT-END event.
+ self.get_event()
+
+ self.anchors = {}
+ return node
+
+ def compose_node(self, parent, index):
+ if self.check_event(AliasEvent):
+ event = self.get_event()
+ anchor = event.anchor
+ if anchor not in self.anchors:
+ raise ComposerError(None, None, "found undefined alias %r"
+ % anchor.encode('utf-8'), event.start_mark)
+ return self.anchors[anchor]
+ event = self.peek_event()
+ anchor = event.anchor
+ if anchor is not None:
+ if anchor in self.anchors:
+ raise ComposerError("found duplicate anchor %r; first occurence"
+ % anchor.encode('utf-8'), self.anchors[anchor].start_mark,
+ "second occurence", event.start_mark)
+ self.descend_resolver(parent, index)
+ if self.check_event(ScalarEvent):
+ node = self.compose_scalar_node(anchor)
+ elif self.check_event(SequenceStartEvent):
+ node = self.compose_sequence_node(anchor)
+ elif self.check_event(MappingStartEvent):
+ node = self.compose_mapping_node(anchor)
+ self.ascend_resolver()
+ return node
+
+ def compose_scalar_node(self, anchor):
+ event = self.get_event()
+ tag = event.tag
+ if tag is None or tag == u'!':
+ tag = self.resolve(ScalarNode, event.value, event.implicit)
+ node = ScalarNode(tag, event.value,
+ event.start_mark, event.end_mark, style=event.style)
+ if anchor is not None:
+ self.anchors[anchor] = node
+ return node
+
+ def compose_sequence_node(self, anchor):
+ start_event = self.get_event()
+ tag = start_event.tag
+ if tag is None or tag == u'!':
+ tag = self.resolve(SequenceNode, None, start_event.implicit)
+ node = SequenceNode(tag, [],
+ start_event.start_mark, None,
+ flow_style=start_event.flow_style)
+ if anchor is not None:
+ self.anchors[anchor] = node
+ index = 0
+ while not self.check_event(SequenceEndEvent):
+ node.value.append(self.compose_node(node, index))
+ index += 1
+ end_event = self.get_event()
+ node.end_mark = end_event.end_mark
+ return node
+
+ def compose_mapping_node(self, anchor):
+ start_event = self.get_event()
+ tag = start_event.tag
+ if tag is None or tag == u'!':
+ tag = self.resolve(MappingNode, None, start_event.implicit)
+ node = MappingNode(tag, [],
+ start_event.start_mark, None,
+ flow_style=start_event.flow_style)
+ if anchor is not None:
+ self.anchors[anchor] = node
+ while not self.check_event(MappingEndEvent):
+ #key_event = self.peek_event()
+ item_key = self.compose_node(node, None)
+ #if item_key in node.value:
+ # raise ComposerError("while composing a mapping", start_event.start_mark,
+ # "found duplicate key", key_event.start_mark)
+ item_value = self.compose_node(node, item_key)
+ #node.value[item_key] = item_value
+ node.value.append((item_key, item_value))
+ end_event = self.get_event()
+ node.end_mark = end_event.end_mark
+ return node
+
diff --git a/pynxc/yaml/constructor.py b/pynxc/yaml/constructor.py
new file mode 100644
index 0000000..420c434
--- /dev/null
+++ b/pynxc/yaml/constructor.py
@@ -0,0 +1,684 @@
+
+__all__ = ['BaseConstructor', 'SafeConstructor', 'Constructor',
+ 'ConstructorError']
+
+from error import *
+from nodes import *
+
+import datetime
+
+try:
+ set
+except NameError:
+ from sets import Set as set
+
+import binascii, re, sys, types
+
+class ConstructorError(MarkedYAMLError):
+ pass
+
+class BaseConstructor(object):
+
+ yaml_constructors = {}
+ yaml_multi_constructors = {}
+
+ def __init__(self):
+ self.constructed_objects = {}
+ self.recursive_objects = {}
+ self.state_generators = []
+ self.deep_construct = False
+
+ def check_data(self):
+ # If there are more documents available?
+ return self.check_node()
+
+ def get_data(self):
+ # Construct and return the next document.
+ if self.check_node():
+ return self.construct_document(self.get_node())
+
+ def get_single_data(self):
+ # Ensure that the stream contains a single document and construct it.
+ node = self.get_single_node()
+ if node is not None:
+ return self.construct_document(node)
+ return None
+
+ def construct_document(self, node):
+ data = self.construct_object(node)
+ while self.state_generators:
+ state_generators = self.state_generators
+ self.state_generators = []
+ for generator in state_generators:
+ for dummy in generator:
+ pass
+ self.constructed_objects = {}
+ self.recursive_objects = {}
+ self.deep_construct = False
+ return data
+
+ def construct_object(self, node, deep=False):
+ if deep:
+ old_deep = self.deep_construct
+ self.deep_construct = True
+ if node in self.constructed_objects:
+ return self.constructed_objects[node]
+ if node in self.recursive_objects:
+ raise ConstructorError(None, None,
+ "found unconstructable recursive node", node.start_mark)
+ self.recursive_objects[node] = None
+ constructor = None
+ tag_suffix = None
+ if node.tag in self.yaml_constructors:
+ constructor = self.yaml_constructors[node.tag]
+ else:
+ for tag_prefix in self.yaml_multi_constructors:
+ if node.tag.startswith(tag_prefix):
+ tag_suffix = node.tag[len(tag_prefix):]
+ constructor = self.yaml_multi_constructors[tag_prefix]
+ break
+ else:
+ if None in self.yaml_multi_constructors:
+ tag_suffix = node.tag
+ constructor = self.yaml_multi_constructors[None]
+ elif None in self.yaml_constructors:
+ constructor = self.yaml_constructors[None]
+ elif isinstance(node, ScalarNode):
+ constructor = self.__class__.construct_scalar
+ elif isinstance(node, SequenceNode):
+ constructor = self.__class__.construct_sequence
+ elif isinstance(node, MappingNode):
+ constructor = self.__class__.construct_mapping
+ if tag_suffix is None:
+ data = constructor(self, node)
+ else:
+ data = constructor(self, tag_suffix, node)
+ if isinstance(data, types.GeneratorType):
+ generator = data
+ data = generator.next()
+ if self.deep_construct:
+ for dummy in generator:
+ pass
+ else:
+ self.state_generators.append(generator)
+ self.constructed_objects[node] = data
+ del self.recursive_objects[node]
+ if deep:
+ self.deep_construct = old_deep
+ return data
+
+ def construct_scalar(self, node):
+ if not isinstance(node, ScalarNode):
+ raise ConstructorError(None, None,
+ "expected a scalar node, but found %s" % node.id,
+ node.start_mark)
+ return node.value
+
+ def construct_sequence(self, node, deep=False):
+ if not isinstance(node, SequenceNode):
+ raise ConstructorError(None, None,
+ "expected a sequence node, but found %s" % node.id,
+ node.start_mark)
+ return [self.construct_object(child, deep=deep)
+ for child in node.value]
+
+ def construct_mapping(self, node, deep=False):
+ if not isinstance(node, MappingNode):
+ raise ConstructorError(None, None,
+ "expected a mapping node, but found %s" % node.id,
+ node.start_mark)
+ mapping = {}
+ for key_node, value_node in node.value:
+ key = self.construct_object(key_node, deep=deep)
+ try:
+ hash(key)
+ except TypeError, exc:
+ raise ConstructorError("while constructing a mapping", node.start_mark,
+ "found unacceptable key (%s)" % exc, key_node.start_mark)
+ value = self.construct_object(value_node, deep=deep)
+ mapping[key] = value
+ return mapping
+
+ def construct_pairs(self, node, deep=False):
+ if not isinstance(node, MappingNode):
+ raise ConstructorError(None, None,
+ "expected a mapping node, but found %s" % node.id,
+ node.start_mark)
+ pairs = []
+ for key_node, value_node in node.value:
+ key = self.construct_object(key_node, deep=deep)
+ value = self.construct_object(value_node, deep=deep)
+ pairs.append((key, value))
+ return pairs
+
+ def add_constructor(cls, tag, constructor):
+ if not 'yaml_constructors' in cls.__dict__:
+ cls.yaml_constructors = cls.yaml_constructors.copy()
+ cls.yaml_constructors[tag] = constructor
+ add_constructor = classmethod(add_constructor)
+
+ def add_multi_constructor(cls, tag_prefix, multi_constructor):
+ if not 'yaml_multi_constructors' in cls.__dict__:
+ cls.yaml_multi_constructors = cls.yaml_multi_constructors.copy()
+ cls.yaml_multi_constructors[tag_prefix] = multi_constructor
+ add_multi_constructor = classmethod(add_multi_constructor)
+
+class SafeConstructor(BaseConstructor):
+
+ def construct_scalar(self, node):
+ if isinstance(node, MappingNode):
+ for key_node, value_node in node.value:
+ if key_node.tag == u'tag:yaml.org,2002:value':
+ return self.construct_scalar(value_node)
+ return BaseConstructor.construct_scalar(self, node)
+
+ def flatten_mapping(self, node):
+ merge = []
+ index = 0
+ while index < len(node.value):
+ key_node, value_node = node.value[index]
+ if key_node.tag == u'tag:yaml.org,2002:merge':
+ del node.value[index]
+ if isinstance(value_node, MappingNode):
+ self.flatten_mapping(value_node)
+ merge.extend(value_node.value)
+ elif isinstance(value_node, SequenceNode):
+ submerge = []
+ for subnode in value_node.value:
+ if not isinstance(subnode, MappingNode):
+ raise ConstructorError("while constructing a mapping",
+ node.start_mark,
+ "expected a mapping for merging, but found %s"
+ % subnode.id, subnode.start_mark)
+ self.flatten_mapping(subnode)
+ submerge.append(subnode.value)
+ submerge.reverse()
+ for value in submerge:
+ merge.extend(value)
+ else:
+ raise ConstructorError("while constructing a mapping", node.start_mark,
+ "expected a mapping or list of mappings for merging, but found %s"
+ % value_node.id, value_node.start_mark)
+ elif key_node.tag == u'tag:yaml.org,2002:value':
+ key_node.tag = u'tag:yaml.org,2002:str'
+ index += 1
+ else:
+ index += 1
+ if merge:
+ node.value = merge + node.value
+
+ def construct_mapping(self, node, deep=False):
+ if isinstance(node, MappingNode):
+ self.flatten_mapping(node)
+ return BaseConstructor.construct_mapping(self, node, deep=deep)
+
+ def construct_yaml_null(self, node):
+ self.construct_scalar(node)
+ return None
+
+ bool_values = {
+ u'yes': True,
+ u'no': False,
+ u'true': True,
+ u'false': False,
+ u'on': True,
+ u'off': False,
+ }
+
+ def construct_yaml_bool(self, node):
+ value = self.construct_scalar(node)
+ return self.bool_values[value.lower()]
+
+ def construct_yaml_int(self, node):
+ value = str(self.construct_scalar(node))
+ value = value.replace('_', '')
+ sign = +1
+ if value[0] == '-':
+ sign = -1
+ if value[0] in '+-':
+ value = value[1:]
+ if value == '0':
+ return 0
+ elif value.startswith('0b'):
+ return sign*int(value[2:], 2)
+ elif value.startswith('0x'):
+ return sign*int(value[2:], 16)
+ elif value[0] == '0':
+ return sign*int(value, 8)
+ elif ':' in value:
+ digits = [int(part) for part in value.split(':')]
+ digits.reverse()
+ base = 1
+ value = 0
+ for digit in digits:
+ value += digit*base
+ base *= 60
+ return sign*value
+ else:
+ return sign*int(value)
+
+ inf_value = 1e300
+ while inf_value != inf_value*inf_value:
+ inf_value *= inf_value
+ nan_value = -inf_value/inf_value # Trying to make a quiet NaN (like C99).
+
+ def construct_yaml_float(self, node):
+ value = str(self.construct_scalar(node))
+ value = value.replace('_', '').lower()
+ sign = +1
+ if value[0] == '-':
+ sign = -1
+ if value[0] in '+-':
+ value = value[1:]
+ if value == '.inf':
+ return sign*self.inf_value
+ elif value == '.nan':
+ return self.nan_value
+ elif ':' in value:
+ digits = [float(part) for part in value.split(':')]
+ digits.reverse()
+ base = 1
+ value = 0.0
+ for digit in digits:
+ value += digit*base
+ base *= 60
+ return sign*value
+ else:
+ return sign*float(value)
+
+ def construct_yaml_binary(self, node):
+ value = self.construct_scalar(node)
+ try:
+ return str(value).decode('base64')
+ except (binascii.Error, UnicodeEncodeError), exc:
+ raise ConstructorError(None, None,
+ "failed to decode base64 data: %s" % exc, node.start_mark)
+
+ timestamp_regexp = re.compile(
+ ur'''^(?P<year>[0-9][0-9][0-9][0-9])
+ -(?P<month>[0-9][0-9]?)
+ -(?P<day>[0-9][0-9]?)
+ (?:(?:[Tt]|[ \t]+)
+ (?P<hour>[0-9][0-9]?)
+ :(?P<minute>[0-9][0-9])
+ :(?P<second>[0-9][0-9])
+ (?:\.(?P<fraction>[0-9]*))?
+ (?:[ \t]*(?P<tz>Z|(?P<tz_sign>[-+])(?P<tz_hour>[0-9][0-9]?)
+ (?::(?P<tz_minute>[0-9][0-9]))?))?)?$''', re.X)
+
+ def construct_yaml_timestamp(self, node):
+ value = self.construct_scalar(node)
+ match = self.timestamp_regexp.match(node.value)
+ values = match.groupdict()
+ year = int(values['year'])
+ month = int(values['month'])
+ day = int(values['day'])
+ if not values['hour']:
+ return datetime.date(year, month, day)
+ hour = int(values['hour'])
+ minute = int(values['minute'])
+ second = int(values['second'])
+ fraction = 0
+ if values['fraction']:
+ fraction = values['fraction'][:6]
+ while len(fraction) < 6:
+ fraction += '0'
+ fraction = int(fraction)
+ delta = None
+ if values['tz_sign']:
+ tz_hour = int(values['tz_hour'])
+ tz_minute = int(values['tz_minute'] or 0)
+ delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute)
+ if values['tz_sign'] == '-':
+ delta = -delta
+ data = datetime.datetime(year, month, day, hour, minute, second, fraction)
+ if delta:
+ data -= delta
+ return data
+
+ def construct_yaml_omap(self, node):
+ # Note: we do not check for duplicate keys, because it's too
+ # CPU-expensive.
+ omap = []
+ yield omap
+ if not isinstance(node, SequenceNode):
+ raise ConstructorError("while constructing an ordered map", node.start_mark,
+ "expected a sequence, but found %s" % node.id, node.start_mark)
+ for subnode in node.value:
+ if not isinstance(subnode, MappingNode):
+ raise ConstructorError("while constructing an ordered map", node.start_mark,
+ "expected a mapping of length 1, but found %s" % subnode.id,
+ subnode.start_mark)
+ if len(subnode.value) != 1:
+ raise ConstructorError("while constructing an ordered map", node.start_mark,
+ "expected a single mapping item, but found %d items" % len(subnode.value),
+ subnode.start_mark)
+ key_node, value_node = subnode.value[0]
+ key = self.construct_object(key_node)
+ value = self.construct_object(value_node)
+ omap.append((key, value))
+
+ def construct_yaml_pairs(self, node):
+ # Note: the same code as `construct_yaml_omap`.
+ pairs = []
+ yield pairs
+ if not isinstance(node, SequenceNode):
+ raise ConstructorError("while constructing pairs", node.start_mark,
+ "expected a sequence, but found %s" % node.id, node.start_mark)
+ for subnode in node.value:
+ if not isinstance(subnode, MappingNode):
+ raise ConstructorError("while constructing pairs", node.start_mark,
+ "expected a mapping of length 1, but found %s" % subnode.id,
+ subnode.start_mark)
+ if len(subnode.value) != 1:
+ raise ConstructorError("while constructing pairs", node.start_mark,
+ "expected a single mapping item, but found %d items" % len(subnode.value),
+ subnode.start_mark)
+ key_node, value_node = subnode.value[0]
+ key = self.construct_object(key_node)
+ value = self.construct_object(value_node)
+ pairs.append((key, value))
+
+ def construct_yaml_set(self, node):
+ data = set()
+ yield data
+ value = self.construct_mapping(node)
+ data.update(value)
+
+ def construct_yaml_str(self, node):
+ value = self.construct_scalar(node)
+ try:
+ return value.encode('ascii')
+ except UnicodeEncodeError:
+ return value
+
+ def construct_yaml_seq(self, node):
+ data = []
+ yield data
+ data.extend(self.construct_sequence(node))
+
+ def construct_yaml_map(self, node):
+ data = {}
+ yield data
+ value = self.construct_mapping(node)
+ data.update(value)
+
+ def construct_yaml_object(self, node, cls):
+ data = cls.__new__(cls)
+ yield data
+ if hasattr(data, '__setstate__'):
+ state = self.construct_mapping(node, deep=True)
+ data.__setstate__(state)
+ else:
+ state = self.construct_mapping(node)
+ data.__dict__.update(state)
+
+ def construct_undefined(self, node):
+ raise ConstructorError(None, None,
+ "could not determine a constructor for the tag %r" % node.tag.encode('utf-8'),
+ node.start_mark)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:null',
+ SafeConstructor.construct_yaml_null)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:bool',
+ SafeConstructor.construct_yaml_bool)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:int',
+ SafeConstructor.construct_yaml_int)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:float',
+ SafeConstructor.construct_yaml_float)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:binary',
+ SafeConstructor.construct_yaml_binary)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:timestamp',
+ SafeConstructor.construct_yaml_timestamp)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:omap',
+ SafeConstructor.construct_yaml_omap)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:pairs',
+ SafeConstructor.construct_yaml_pairs)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:set',
+ SafeConstructor.construct_yaml_set)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:str',
+ SafeConstructor.construct_yaml_str)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:seq',
+ SafeConstructor.construct_yaml_seq)
+
+SafeConstructor.add_constructor(
+ u'tag:yaml.org,2002:map',
+ SafeConstructor.construct_yaml_map)
+
+SafeConstructor.add_constructor(None,
+ SafeConstructor.construct_undefined)
+
+class Constructor(SafeConstructor):
+
+ def construct_python_str(self, node):
+ return self.construct_scalar(node).encode('utf-8')
+
+ def construct_python_unicode(self, node):
+ return self.construct_scalar(node)
+
+ def construct_python_long(self, node):
+ return long(self.construct_yaml_int(node))
+
+ def construct_python_complex(self, node):
+ return complex(self.construct_scalar(node))
+
+ def construct_python_tuple(self, node):
+ return tuple(self.construct_sequence(node))
+
+ def find_python_module(self, name, mark):
+ if not name:
+ raise ConstructorError("while constructing a Python module", mark,
+ "expected non-empty name appended to the tag", mark)
+ try:
+ __import__(name)
+ except ImportError, exc:
+ raise ConstructorError("while constructing a Python module", mark,
+ "cannot find module %r (%s)" % (name.encode('utf-8'), exc), mark)
+ return sys.modules[name]
+
+ def find_python_name(self, name, mark):
+ if not name:
+ raise ConstructorError("while constructing a Python object", mark,
+ "expected non-empty name appended to the tag", mark)
+ if u'.' in name:
+ # Python 2.4 only
+ #module_name, object_name = name.rsplit('.', 1)
+ items = name.split('.')
+ object_name = items.pop()
+ module_name = '.'.join(items)
+ else:
+ module_name = '__builtin__'
+ object_name = name
+ try:
+ __import__(module_name)
+ except ImportError, exc:
+ raise ConstructorError("while constructing a Python object", mark,
+ "cannot find module %r (%s)" % (module_name.encode('utf-8'), exc), mark)
+ module = sys.modules[module_name]
+ if not hasattr(module, object_name):
+ raise ConstructorError("while constructing a Python object", mark,
+ "cannot find %r in the module %r" % (object_name.encode('utf-8'),
+ module.__name__), mark)
+ return getattr(module, object_name)
+
+ def construct_python_name(self, suffix, node):
+ value = self.construct_scalar(node)
+ if value:
+ raise ConstructorError("while constructing a Python name", node.start_mark,
+ "expected the empty value, but found %r" % value.encode('utf-8'),
+ node.start_mark)
+ return self.find_python_name(suffix, node.start_mark)
+
+ def construct_python_module(self, suffix, node):
+ value = self.construct_scalar(node)
+ if value:
+ raise ConstructorError("while constructing a Python module", node.start_mark,
+ "expected the empty value, but found %r" % value.encode('utf-8'),
+ node.start_mark)
+ return self.find_python_module(suffix, node.start_mark)
+
+ class classobj: pass
+
+ def make_python_instance(self, suffix, node,
+ args=None, kwds=None, newobj=False):
+ if not args:
+ args = []
+ if not kwds:
+ kwds = {}
+ cls = self.find_python_name(suffix, node.start_mark)
+ if newobj and isinstance(cls, type(self.classobj)) \
+ and not args and not kwds:
+ instance = self.classobj()
+ instance.__class__ = cls
+ return instance
+ elif newobj and isinstance(cls, type):
+ return cls.__new__(cls, *args, **kwds)
+ else:
+ return cls(*args, **kwds)
+
+ def set_python_instance_state(self, instance, state):
+ if hasattr(instance, '__setstate__'):
+ instance.__setstate__(state)
+ else:
+ slotstate = {}
+ if isinstance(state, tuple) and len(state) == 2:
+ state, slotstate = state
+ if hasattr(instance, '__dict__'):
+ instance.__dict__.update(state)
+ elif state:
+ slotstate.update(state)
+ for key, value in slotstate.items():
+ setattr(object, key, value)
+
+ def construct_python_object(self, suffix, node):
+ # Format:
+ # !!python/object:module.name { ... state ... }
+ instance = self.make_python_instance(suffix, node, newobj=True)
+ yield instance
+ deep = hasattr(instance, '__setstate__')
+ state = self.construct_mapping(node, deep=deep)
+ self.set_python_instance_state(instance, state)
+
+ def construct_python_object_apply(self, suffix, node, newobj=False):
+ # Format:
+ # !!python/object/apply # (or !!python/object/new)
+ # args: [ ... arguments ... ]
+ # kwds: { ... keywords ... }
+ # state: ... state ...
+ # listitems: [ ... listitems ... ]
+ # dictitems: { ... dictitems ... }
+ # or short format:
+ # !!python/object/apply [ ... arguments ... ]
+ # The difference between !!python/object/apply and !!python/object/new
+ # is how an object is created, check make_python_instance for details.
+ if isinstance(node, SequenceNode):
+ args = self.construct_sequence(node, deep=True)
+ kwds = {}
+ state = {}
+ listitems = []
+ dictitems = {}
+ else:
+ value = self.construct_mapping(node, deep=True)
+ args = value.get('args', [])
+ kwds = value.get('kwds', {})
+ state = value.get('state', {})
+ listitems = value.get('listitems', [])
+ dictitems = value.get('dictitems', {})
+ instance = self.make_python_instance(suffix, node, args, kwds, newobj)
+ if state:
+ self.set_python_instance_state(instance, state)
+ if listitems:
+ instance.extend(listitems)
+ if dictitems:
+ for key in dictitems:
+ instance[key] = dictitems[key]
+ return instance
+
+ def construct_python_object_new(self, suffix, node):
+ return self.construct_python_object_apply(suffix, node, newobj=True)
+
+Constructor.add_constructor(
+ u'tag:yaml.org,2002:python/none',
+ Constructor.construct_yaml_null)
+
+Constructor.add_constructor(
+ u'tag:yaml.org,2002:python/bool',
+ Constructor.construct_yaml_bool)
+
+Constructor.add_constructor(
+ u'tag:yaml.org,2002:python/str',
+ Constructor.construct_python_str)
+
+Constructor.add_constructor(
+ u'tag:yaml.org,2002:python/unicode',
+ Constructor.construct_python_unicode)
+
+Constructor.add_constructor(
+ u'tag:yaml.org,2002:python/int',
+ Constructor.construct_yaml_int)
+
+Constructor.add_constructor(
+ u'tag:yaml.org,2002:python/long',
+ Constructor.construct_python_long)
+
+Constructor.add_constructor(
+ u'tag:yaml.org,2002:python/float',
+ Constructor.construct_yaml_float)
+
+Constructor.add_constructor(
+ u'tag:yaml.org,2002:python/complex',
+ Constructor.construct_python_complex)
+
+Constructor.add_constructor(
+ u'tag:yaml.org,2002:python/list',
+ Constructor.construct_yaml_seq)
+
+Constructor.add_constructor(
+ u'tag:yaml.org,2002:python/tuple',
+ Constructor.construct_python_tuple)
+
+Constructor.add_constructor(
+ u'tag:yaml.org,2002:python/dict',
+ Constructor.construct_yaml_map)
+
+Constructor.add_multi_constructor(
+ u'tag:yaml.org,2002:python/name:',
+ Constructor.construct_python_name)
+
+Constructor.add_multi_constructor(
+ u'tag:yaml.org,2002:python/module:',
+ Constructor.construct_python_module)
+
+Constructor.add_multi_constructor(
+ u'tag:yaml.org,2002:python/object:',
+ Constructor.construct_python_object)
+
+Constructor.add_multi_constructor(
+ u'tag:yaml.org,2002:python/object/apply:',
+ Constructor.construct_python_object_apply)
+
+Constructor.add_multi_constructor(
+ u'tag:yaml.org,2002:python/object/new:',
+ Constructor.construct_python_object_new)
+
diff --git a/pynxc/yaml/cyaml.py b/pynxc/yaml/cyaml.py
new file mode 100644
index 0000000..68dcd75
--- /dev/null
+++ b/pynxc/yaml/cyaml.py
@@ -0,0 +1,85 @@
+
+__all__ = ['CBaseLoader', 'CSafeLoader', 'CLoader',
+ 'CBaseDumper', 'CSafeDumper', 'CDumper']
+
+from _yaml import CParser, CEmitter
+
+from constructor import *
+
+from serializer import *
+from representer import *
+
+from resolver import *
+
+class CBaseLoader(CParser, BaseConstructor, BaseResolver):
+
+ def __init__(self, stream):
+ CParser.__init__(self, stream)
+ BaseConstructor.__init__(self)
+ BaseResolver.__init__(self)
+
+class CSafeLoader(CParser, SafeConstructor, Resolver):
+
+ def __init__(self, stream):
+ CParser.__init__(self, stream)
+ SafeConstructor.__init__(self)
+ Resolver.__init__(self)
+
+class CLoader(CParser, Constructor, Resolver):
+
+ def __init__(self, stream):
+ CParser.__init__(self, stream)
+ Constructor.__init__(self)
+ Resolver.__init__(self)
+
+class CBaseDumper(CEmitter, BaseRepresenter, BaseResolver):
+
+ def __init__(self, stream,
+ default_style=None, default_flow_style=None,
+ canonical=None, indent=None, width=None,
+ allow_unicode=None, line_break=None,
+ encoding=None, explicit_start=None, explicit_end=None,
+ version=None, tags=None):
+ CEmitter.__init__(self, stream, canonical=canonical,
+ indent=indent, width=width, encoding=encoding,
+ allow_unicode=allow_unicode, line_break=line_break,
+ explicit_start=explicit_start, explicit_end=explicit_end,
+ version=version, tags=tags)
+ Representer.__init__(self, default_style=default_style,
+ default_flow_style=default_flow_style)
+ Resolver.__init__(self)
+
+class CSafeDumper(CEmitter, SafeRepresenter, Resolver):
+
+ def __init__(self, stream,
+ default_style=None, default_flow_style=None,
+ canonical=None, indent=None, width=None,
+ allow_unicode=None, line_break=None,
+ encoding=None, explicit_start=None, explicit_end=None,
+ version=None, tags=None):
+ CEmitter.__init__(self, stream, canonical=canonical,
+ indent=indent, width=width, encoding=encoding,
+ allow_unicode=allow_unicode, line_break=line_break,
+ explicit_start=explicit_start, explicit_end=explicit_end,
+ version=version, tags=tags)
+ SafeRepresenter.__init__(self, default_style=default_style,
+ default_flow_style=default_flow_style)
+ Resolver.__init__(self)
+
+class CDumper(CEmitter, Serializer, Representer, Resolver):
+
+ def __init__(self, stream,
+ default_style=None, default_flow_style=None,
+ canonical=None, indent=None, width=None,
+ allow_unicode=None, line_break=None,
+ encoding=None, explicit_start=None, explicit_end=None,
+ version=None, tags=None):
+ CEmitter.__init__(self, stream, canonical=canonical,
+ indent=indent, width=width, encoding=encoding,
+ allow_unicode=allow_unicode, line_break=line_break,
+ explicit_start=explicit_start, explicit_end=explicit_end,
+ version=version, tags=tags)
+ Representer.__init__(self, default_style=default_style,
+ default_flow_style=default_flow_style)
+ Resolver.__init__(self)
+
diff --git a/pynxc/yaml/dumper.py b/pynxc/yaml/dumper.py
new file mode 100644
index 0000000..f811d2c
--- /dev/null
+++ b/pynxc/yaml/dumper.py
@@ -0,0 +1,62 @@
+
+__all__ = ['BaseDumper', 'SafeDumper', 'Dumper']
+
+from emitter import *
+from serializer import *
+from representer import *
+from resolver import *
+
+class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver):
+
+ def __init__(self, stream,
+ default_style=None, default_flow_style=None,
+ canonical=None, indent=None, width=None,
+ allow_unicode=None, line_break=None,
+ encoding=None, explicit_start=None, explicit_end=None,
+ version=None, tags=None):
+ Emitter.__init__(self, stream, canonical=canonical,
+ indent=indent, width=width,
+ allow_unicode=allow_unicode, line_break=line_break)
+ Serializer.__init__(self, encoding=encoding,
+ explicit_start=explicit_start, explicit_end=explicit_end,
+ version=version, tags=tags)
+ Representer.__init__(self, default_style=default_style,
+ default_flow_style=default_flow_style)
+ Resolver.__init__(self)
+
+class SafeDumper(Emitter, Serializer, SafeRepresenter, Resolver):
+
+ def __init__(self, stream,
+ default_style=None, default_flow_style=None,
+ canonical=None, indent=None, width=None,
+ allow_unicode=None, line_break=None,
+ encoding=None, explicit_start=None, explicit_end=None,
+ version=None, tags=None):
+ Emitter.__init__(self, stream, canonical=canonical,
+ indent=indent, width=width,
+ allow_unicode=allow_unicode, line_break=line_break)
+ Serializer.__init__(self, encoding=encoding,
+ explicit_start=explicit_start, explicit_end=explicit_end,
+ version=version, tags=tags)
+ SafeRepresenter.__init__(self, default_style=default_style,
+ default_flow_style=default_flow_style)
+ Resolver.__init__(self)
+
+class Dumper(Emitter, Serializer, Representer, Resolver):
+
+ def __init__(self, stream,
+ default_style=None, default_flow_style=None,
+ canonical=None, indent=None, width=None,
+ allow_unicode=None, line_break=None,
+ encoding=None, explicit_start=None, explicit_end=None,
+ version=None, tags=None):
+ Emitter.__init__(self, stream, canonical=canonical,
+ indent=indent, width=width,
+ allow_unicode=allow_unicode, line_break=line_break)
+ Serializer.__init__(self, encoding=encoding,
+ explicit_start=explicit_start, explicit_end=explicit_end,
+ version=version, tags=tags)
+ Representer.__init__(self, default_style=default_style,
+ default_flow_style=default_flow_style)
+ Resolver.__init__(self)
+
diff --git a/pynxc/yaml/emitter.py b/pynxc/yaml/emitter.py
new file mode 100644
index 0000000..4cb2c8a
--- /dev/null
+++ b/pynxc/yaml/emitter.py
@@ -0,0 +1,1135 @@
+
+# Emitter expects events obeying the following grammar:
+# stream ::= STREAM-START document* STREAM-END
+# document ::= DOCUMENT-START node DOCUMENT-END
+# node ::= SCALAR | sequence | mapping
+# sequence ::= SEQUENCE-START node* SEQUENCE-END
+# mapping ::= MAPPING-START (node node)* MAPPING-END
+
+__all__ = ['Emitter', 'EmitterError']
+
+from error import YAMLError
+from events import *
+
+class EmitterError(YAMLError):
+ pass
+
+class ScalarAnalysis(object):
+ def __init__(self, scalar, empty, multiline,
+ allow_flow_plain, allow_block_plain,
+ allow_single_quoted, allow_double_quoted,
+ allow_block):
+ self.scalar = scalar
+ self.empty = empty
+ self.multiline = multiline
+ self.allow_flow_plain = allow_flow_plain
+ self.allow_block_plain = allow_block_plain
+ self.allow_single_quoted = allow_single_quoted
+ self.allow_double_quoted = allow_double_quoted
+ self.allow_block = allow_block
+
+class Emitter(object):
+
+ DEFAULT_TAG_PREFIXES = {
+ u'!' : u'!',
+ u'tag:yaml.org,2002:' : u'!!',
+ }
+
+ def __init__(self, stream, canonical=None, indent=None, width=None,
+ allow_unicode=None, line_break=None):
+
+ # The stream should have the methods `write` and possibly `flush`.
+ self.stream = stream
+
+ # Encoding can be overriden by STREAM-START.
+ self.encoding = None
+
+ # Emitter is a state machine with a stack of states to handle nested
+ # structures.
+ self.states = []
+ self.state = self.expect_stream_start
+
+ # Current event and the event queue.
+ self.events = []
+ self.event = None
+
+ # The current indentation level and the stack of previous indents.
+ self.indents = []
+ self.indent = None
+
+ # Flow level.
+ self.flow_level = 0
+
+ # Contexts.
+ self.root_context = False
+ self.sequence_context = False
+ self.mapping_context = False
+ self.simple_key_context = False
+
+ # Characteristics of the last emitted character:
+ # - current position.
+ # - is it a whitespace?
+ # - is it an indention character
+ # (indentation space, '-', '?', or ':')?
+ self.line = 0
+ self.column = 0
+ self.whitespace = True
+ self.indention = True
+
+ # Whether the document requires an explicit document indicator
+ self.open_ended = False
+
+ # Formatting details.
+ self.canonical = canonical
+ self.allow_unicode = allow_unicode
+ self.best_indent = 2
+ if indent and 1 < indent < 10:
+ self.best_indent = indent
+ self.best_width = 80
+ if width and width > self.best_indent*2:
+ self.best_width = width
+ self.best_line_break = u'\n'
+ if line_break in [u'\r', u'\n', u'\r\n']:
+ self.best_line_break = line_break
+
+ # Tag prefixes.
+ self.tag_prefixes = None
+
+ # Prepared anchor and tag.
+ self.prepared_anchor = None
+ self.prepared_tag = None
+
+ # Scalar analysis and style.
+ self.analysis = None
+ self.style = None
+
+ def emit(self, event):
+ self.events.append(event)
+ while not self.need_more_events():
+ self.event = self.events.pop(0)
+ self.state()
+ self.event = None
+
+ # In some cases, we wait for a few next events before emitting.
+
+ def need_more_events(self):
+ if not self.events:
+ return True
+ event = self.events[0]
+ if isinstance(event, DocumentStartEvent):
+ return self.need_events(1)
+ elif isinstance(event, SequenceStartEvent):
+ return self.need_events(2)
+ elif isinstance(event, MappingStartEvent):
+ return self.need_events(3)
+ else:
+ return False
+
+ def need_events(self, count):
+ level = 0
+ for event in self.events[1:]:
+ if isinstance(event, (DocumentStartEvent, CollectionStartEvent)):
+ level += 1
+ elif isinstance(event, (DocumentEndEvent, CollectionEndEvent)):
+ level -= 1
+ elif isinstance(event, StreamEndEvent):
+ level = -1
+ if level < 0:
+ return False
+ return (len(self.events) < count+1)
+
+ def increase_indent(self, flow=False, indentless=False):
+ self.indents.append(self.indent)
+ if self.indent is None:
+ if flow:
+ self.indent = self.best_indent
+ else:
+ self.indent = 0
+ elif not indentless:
+ self.indent += self.best_indent
+
+ # States.
+
+ # Stream handlers.
+
+ def expect_stream_start(self):
+ if isinstance(self.event, StreamStartEvent):
+ if self.event.encoding and not getattr(self.stream, 'encoding', None):
+ self.encoding = self.event.encoding
+ self.write_stream_start()
+ self.state = self.expect_first_document_start
+ else:
+ raise EmitterError("expected StreamStartEvent, but got %s"
+ % self.event)
+
+ def expect_nothing(self):
+ raise EmitterError("expected nothing, but got %s" % self.event)
+
+ # Document handlers.
+
+ def expect_first_document_start(self):
+ return self.expect_document_start(first=True)
+
+ def expect_document_start(self, first=False):
+ if isinstance(self.event, DocumentStartEvent):
+ if (self.event.version or self.event.tags) and self.open_ended:
+ self.write_indicator(u'...', True)
+ self.write_indent()
+ if self.event.version:
+ version_text = self.prepare_version(self.event.version)
+ self.write_version_directive(version_text)
+ self.tag_prefixes = self.DEFAULT_TAG_PREFIXES.copy()
+ if self.event.tags:
+ handles = self.event.tags.keys()
+ handles.sort()
+ for handle in handles:
+ prefix = self.event.tags[handle]
+ self.tag_prefixes[prefix] = handle
+ handle_text = self.prepare_tag_handle(handle)
+ prefix_text = self.prepare_tag_prefix(prefix)
+ self.write_tag_directive(handle_text, prefix_text)
+ implicit = (first and not self.event.explicit and not self.canonical
+ and not self.event.version and not self.event.tags
+ and not self.check_empty_document())
+ if not implicit:
+ self.write_indent()
+ self.write_indicator(u'---', True)
+ if self.canonical:
+ self.write_indent()
+ self.state = self.expect_document_root
+ elif isinstance(self.event, StreamEndEvent):
+ if self.open_ended:
+ self.write_indicator(u'...', True)
+ self.write_indent()
+ self.write_stream_end()
+ self.state = self.expect_nothing
+ else:
+ raise EmitterError("expected DocumentStartEvent, but got %s"
+ % self.event)
+
+ def expect_document_end(self):
+ if isinstance(self.event, DocumentEndEvent):
+ self.write_indent()
+ if self.event.explicit:
+ self.write_indicator(u'...', True)
+ self.write_indent()
+ self.flush_stream()
+ self.state = self.expect_document_start
+ else:
+ raise EmitterError("expected DocumentEndEvent, but got %s"
+ % self.event)
+
+ def expect_document_root(self):
+ self.states.append(self.expect_document_end)
+ self.expect_node(root=True)
+
+ # Node handlers.
+
+ def expect_node(self, root=False, sequence=False, mapping=False,
+ simple_key=False):
+ self.root_context = root
+ self.sequence_context = sequence
+ self.mapping_context = mapping
+ self.simple_key_context = simple_key
+ if isinstance(self.event, AliasEvent):
+ self.expect_alias()
+ elif isinstance(self.event, (ScalarEvent, CollectionStartEvent)):
+ self.process_anchor(u'&')
+ self.process_tag()
+ if isinstance(self.event, ScalarEvent):
+ self.expect_scalar()
+ elif isinstance(self.event, SequenceStartEvent):
+ if self.flow_level or self.canonical or self.event.flow_style \
+ or self.check_empty_sequence():
+ self.expect_flow_sequence()
+ else:
+ self.expect_block_sequence()
+ elif isinstance(self.event, MappingStartEvent):
+ if self.flow_level or self.canonical or self.event.flow_style \
+ or self.check_empty_mapping():
+ self.expect_flow_mapping()
+ else:
+ self.expect_block_mapping()
+ else:
+ raise EmitterError("expected NodeEvent, but got %s" % self.event)
+
+ def expect_alias(self):
+ if self.event.anchor is None:
+ raise EmitterError("anchor is not specified for alias")
+ self.process_anchor(u'*')
+ self.state = self.states.pop()
+
+ def expect_scalar(self):
+ self.increase_indent(flow=True)
+ self.process_scalar()
+ self.indent = self.indents.pop()
+ self.state = self.states.pop()
+
+ # Flow sequence handlers.
+
+ def expect_flow_sequence(self):
+ self.write_indicator(u'[', True, whitespace=True)
+ self.flow_level += 1
+ self.increase_indent(flow=True)
+ self.state = self.expect_first_flow_sequence_item
+
+ def expect_first_flow_sequence_item(self):
+ if isinstance(self.event, SequenceEndEvent):
+ self.indent = self.indents.pop()
+ self.flow_level -= 1
+ self.write_indicator(u']', False)
+ self.state = self.states.pop()
+ else:
+ if self.canonical or self.column > self.best_width:
+ self.write_indent()
+ self.states.append(self.expect_flow_sequence_item)
+ self.expect_node(sequence=True)
+
+ def expect_flow_sequence_item(self):
+ if isinstance(self.event, SequenceEndEvent):
+ self.indent = self.indents.pop()
+ self.flow_level -= 1
+ if self.canonical:
+ self.write_indicator(u',', False)
+ self.write_indent()
+ self.write_indicator(u']', False)
+ self.state = self.states.pop()
+ else:
+ self.write_indicator(u',', False)
+ if self.canonical or self.column > self.best_width:
+ self.write_indent()
+ self.states.append(self.expect_flow_sequence_item)
+ self.expect_node(sequence=True)
+
+ # Flow mapping handlers.
+
+ def expect_flow_mapping(self):
+ self.write_indicator(u'{', True, whitespace=True)
+ self.flow_level += 1
+ self.increase_indent(flow=True)
+ self.state = self.expect_first_flow_mapping_key
+
+ def expect_first_flow_mapping_key(self):
+ if isinstance(self.event, MappingEndEvent):
+ self.indent = self.indents.pop()
+ self.flow_level -= 1
+ self.write_indicator(u'}', False)
+ self.state = self.states.pop()
+ else:
+ if self.canonical or self.column > self.best_width:
+ self.write_indent()
+ if not self.canonical and self.check_simple_key():
+ self.states.append(self.expect_flow_mapping_simple_value)
+ self.expect_node(mapping=True, simple_key=True)
+ else:
+ self.write_indicator(u'?', True)
+ self.states.append(self.expect_flow_mapping_value)
+ self.expect_node(mapping=True)
+
+ def expect_flow_mapping_key(self):
+ if isinstance(self.event, MappingEndEvent):
+ self.indent = self.indents.pop()
+ self.flow_level -= 1
+ if self.canonical:
+ self.write_indicator(u',', False)
+ self.write_indent()
+ self.write_indicator(u'}', False)
+ self.state = self.states.pop()
+ else:
+ self.write_indicator(u',', False)
+ if self.canonical or self.column > self.best_width:
+ self.write_indent()
+ if not self.canonical and self.check_simple_key():
+ self.states.append(self.expect_flow_mapping_simple_value)
+ self.expect_node(mapping=True, simple_key=True)
+ else:
+ self.write_indicator(u'?', True)
+ self.states.append(self.expect_flow_mapping_value)
+ self.expect_node(mapping=True)
+
+ def expect_flow_mapping_simple_value(self):
+ self.write_indicator(u':', False)
+ self.states.append(self.expect_flow_mapping_key)
+ self.expect_node(mapping=True)
+
+ def expect_flow_mapping_value(self):
+ if self.canonical or self.column > self.best_width:
+ self.write_indent()
+ self.write_indicator(u':', True)
+ self.states.append(self.expect_flow_mapping_key)
+ self.expect_node(mapping=True)
+
+ # Block sequence handlers.
+
+ def expect_block_sequence(self):
+ indentless = (self.mapping_context and not self.indention)
+ self.increase_indent(flow=False, indentless=indentless)
+ self.state = self.expect_first_block_sequence_item
+
+ def expect_first_block_sequence_item(self):
+ return self.expect_block_sequence_item(first=True)
+
+ def expect_block_sequence_item(self, first=False):
+ if not first and isinstance(self.event, SequenceEndEvent):
+ self.indent = self.indents.pop()
+ self.state = self.states.pop()
+ else:
+ self.write_indent()
+ self.write_indicator(u'-', True, indention=True)
+ self.states.append(self.expect_block_sequence_item)
+ self.expect_node(sequence=True)
+
+ # Block mapping handlers.
+
+ def expect_block_mapping(self):
+ self.increase_indent(flow=False)
+ self.state = self.expect_first_block_mapping_key
+
+ def expect_first_block_mapping_key(self):
+ return self.expect_block_mapping_key(first=True)
+
+ def expect_block_mapping_key(self, first=False):
+ if not first and isinstance(self.event, MappingEndEvent):
+ self.indent = self.indents.pop()
+ self.state = self.states.pop()
+ else:
+ self.write_indent()
+ if self.check_simple_key():
+ self.states.append(self.expect_block_mapping_simple_value)
+ self.expect_node(mapping=True, simple_key=True)
+ else:
+ self.write_indicator(u'?', True, indention=True)
+ self.states.append(self.expect_block_mapping_value)
+ self.expect_node(mapping=True)
+
+ def expect_block_mapping_simple_value(self):
+ self.write_indicator(u':', False)
+ self.states.append(self.expect_block_mapping_key)
+ self.expect_node(mapping=True)
+
+ def expect_block_mapping_value(self):
+ self.write_indent()
+ self.write_indicator(u':', True, indention=True)
+ self.states.append(self.expect_block_mapping_key)
+ self.expect_node(mapping=True)
+
+ # Checkers.
+
+ def check_empty_sequence(self):
+ return (isinstance(self.event, SequenceStartEvent) and self.events
+ and isinstance(self.events[0], SequenceEndEvent))
+
+ def check_empty_mapping(self):
+ return (isinstance(self.event, MappingStartEvent) and self.events
+ and isinstance(self.events[0], MappingEndEvent))
+
+ def check_empty_document(self):
+ if not isinstance(self.event, DocumentStartEvent) or not self.events:
+ return False
+ event = self.events[0]
+ return (isinstance(event, ScalarEvent) and event.anchor is None
+ and event.tag is None and event.implicit and event.value == u'')
+
+ def check_simple_key(self):
+ length = 0
+ if isinstance(self.event, NodeEvent) and self.event.anchor is not None:
+ if self.prepared_anchor is None:
+ self.prepared_anchor = self.prepare_anchor(self.event.anchor)
+ length += len(self.prepared_anchor)
+ if isinstance(self.event, (ScalarEvent, CollectionStartEvent)) \
+ and self.event.tag is not None:
+ if self.prepared_tag is None:
+ self.prepared_tag = self.prepare_tag(self.event.tag)
+ length += len(self.prepared_tag)
+ if isinstance(self.event, ScalarEvent):
+ if self.analysis is None:
+ self.analysis = self.analyze_scalar(self.event.value)
+ length += len(self.analysis.scalar)
+ return (length < 128 and (isinstance(self.event, AliasEvent)
+ or (isinstance(self.event, ScalarEvent)
+ and not self.analysis.empty and not self.analysis.multiline)
+ or self.check_empty_sequence() or self.check_empty_mapping()))
+
+ # Anchor, Tag, and Scalar processors.
+
+ def process_anchor(self, indicator):
+ if self.event.anchor is None:
+ self.prepared_anchor = None
+ return
+ if self.prepared_anchor is None:
+ self.prepared_anchor = self.prepare_anchor(self.event.anchor)
+ if self.prepared_anchor:
+ self.write_indicator(indicator+self.prepared_anchor, True)
+ self.prepared_anchor = None
+
+ def process_tag(self):
+ tag = self.event.tag
+ if isinstance(self.event, ScalarEvent):
+ if self.style is None:
+ self.style = self.choose_scalar_style()
+ if ((not self.canonical or tag is None) and
+ ((self.style == '' and self.event.implicit[0])
+ or (self.style != '' and self.event.implicit[1]))):
+ self.prepared_tag = None
+ return
+ if self.event.implicit[0] and tag is None:
+ tag = u'!'
+ self.prepared_tag = None
+ else:
+ if (not self.canonical or tag is None) and self.event.implicit:
+ self.prepared_tag = None
+ return
+ if tag is None:
+ raise EmitterError("tag is not specified")
+ if self.prepared_tag is None:
+ self.prepared_tag = self.prepare_tag(tag)
+ if self.prepared_tag:
+ self.write_indicator(self.prepared_tag, True)
+ self.prepared_tag = None
+
+ def choose_scalar_style(self):
+ if self.analysis is None:
+ self.analysis = self.analyze_scalar(self.event.value)
+ if self.event.style == '"' or self.canonical:
+ return '"'
+ if not self.event.style and self.event.implicit[0]:
+ if (not (self.simple_key_context and
+ (self.analysis.empty or self.analysis.multiline))
+ and (self.flow_level and self.analysis.allow_flow_plain
+ or (not self.flow_level and self.analysis.allow_block_plain))):
+ return ''
+ if self.event.style and self.event.style in '|>':
+ if (not self.flow_level and not self.simple_key_context
+ and self.analysis.allow_block):
+ return self.event.style
+ if not self.event.style or self.event.style == '\'':
+ if (self.analysis.allow_single_quoted and
+ not (self.simple_key_context and self.analysis.multiline)):
+ return '\''
+ return '"'
+
+ def process_scalar(self):
+ if self.analysis is None:
+ self.analysis = self.analyze_scalar(self.event.value)
+ if self.style is None:
+ self.style = self.choose_scalar_style()
+ split = (not self.simple_key_context)
+ #if self.analysis.multiline and split \
+ # and (not self.style or self.style in '\'\"'):
+ # self.write_indent()
+ if self.style == '"':
+ self.write_double_quoted(self.analysis.scalar, split)
+ elif self.style == '\'':
+ self.write_single_quoted(self.analysis.scalar, split)
+ elif self.style == '>':
+ self.write_folded(self.analysis.scalar)
+ elif self.style == '|':
+ self.write_literal(self.analysis.scalar)
+ else:
+ self.write_plain(self.analysis.scalar, split)
+ self.analysis = None
+ self.style = None
+
+ # Analyzers.
+
+ def prepare_version(self, version):
+ major, minor = version
+ if major != 1:
+ raise EmitterError("unsupported YAML version: %d.%d" % (major, minor))
+ return u'%d.%d' % (major, minor)
+
+ def prepare_tag_handle(self, handle):
+ if not handle:
+ raise EmitterError("tag handle must not be empty")
+ if handle[0] != u'!' or handle[-1] != u'!':
+ raise EmitterError("tag handle must start and end with '!': %r"
+ % (handle.encode('utf-8')))
+ for ch in handle[1:-1]:
+ if not (u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \
+ or ch in u'-_'):
+ raise EmitterError("invalid character %r in the tag handle: %r"
+ % (ch.encode('utf-8'), handle.encode('utf-8')))
+ return handle
+
+ def prepare_tag_prefix(self, prefix):
+ if not prefix:
+ raise EmitterError("tag prefix must not be empty")
+ chunks = []
+ start = end = 0
+ if prefix[0] == u'!':
+ end = 1
+ while end < len(prefix):
+ ch = prefix[end]
+ if u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \
+ or ch in u'-;/?!:@&=+$,_.~*\'()[]':
+ end += 1
+ else:
+ if start < end:
+ chunks.append(prefix[start:end])
+ start = end = end+1
+ data = ch.encode('utf-8')
+ for ch in data:
+ chunks.append(u'%%%02X' % ord(ch))
+ if start < end:
+ chunks.append(prefix[start:end])
+ return u''.join(chunks)
+
+ def prepare_tag(self, tag):
+ if not tag:
+ raise EmitterError("tag must not be empty")
+ if tag == u'!':
+ return tag
+ handle = None
+ suffix = tag
+ prefixes = self.tag_prefixes.keys()
+ prefixes.sort()
+ for prefix in prefixes:
+ if tag.startswith(prefix) \
+ and (prefix == u'!' or len(prefix) < len(tag)):
+ handle = self.tag_prefixes[prefix]
+ suffix = tag[len(prefix):]
+ chunks = []
+ start = end = 0
+ while end < len(suffix):
+ ch = suffix[end]
+ if u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \
+ or ch in u'-;/?:@&=+$,_.~*\'()[]' \
+ or (ch == u'!' and handle != u'!'):
+ end += 1
+ else:
+ if start < end:
+ chunks.append(suffix[start:end])
+ start = end = end+1
+ data = ch.encode('utf-8')
+ for ch in data:
+ chunks.append(u'%%%02X' % ord(ch))
+ if start < end:
+ chunks.append(suffix[start:end])
+ suffix_text = u''.join(chunks)
+ if handle:
+ return u'%s%s' % (handle, suffix_text)
+ else:
+ return u'!<%s>' % suffix_text
+
+ def prepare_anchor(self, anchor):
+ if not anchor:
+ raise EmitterError("anchor must not be empty")
+ for ch in anchor:
+ if not (u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \
+ or ch in u'-_'):
+ raise EmitterError("invalid character %r in the anchor: %r"
+ % (ch.encode('utf-8'), anchor.encode('utf-8')))
+ return anchor
+
+ def analyze_scalar(self, scalar):
+
+ # Empty scalar is a special case.
+ if not scalar:
+ return ScalarAnalysis(scalar=scalar, empty=True, multiline=False,
+ allow_flow_plain=False, allow_block_plain=True,
+ allow_single_quoted=True, allow_double_quoted=True,
+ allow_block=False)
+
+ # Indicators and special characters.
+ block_indicators = False
+ flow_indicators = False
+ line_breaks = False
+ special_characters = False
+
+ # Important whitespace combinations.
+ leading_space = False
+ leading_break = False
+ trailing_space = False
+ trailing_break = False
+ break_space = False
+ space_break = False
+
+ # Check document indicators.
+ if scalar.startswith(u'---') or scalar.startswith(u'...'):
+ block_indicators = True
+ flow_indicators = True
+
+ # First character or preceded by a whitespace.
+ preceeded_by_whitespace = True
+
+ # Last character or followed by a whitespace.
+ followed_by_whitespace = (len(scalar) == 1 or
+ scalar[1] in u'\0 \t\r\n\x85\u2028\u2029')
+
+ # The previous character is a space.
+ previous_space = False
+
+ # The previous character is a break.
+ previous_break = False
+
+ index = 0
+ while index < len(scalar):
+ ch = scalar[index]
+
+ # Check for indicators.
+ if index == 0:
+ # Leading indicators are special characters.
+ if ch in u'#,[]{}&*!|>\'\"%@`':
+ flow_indicators = True
+ block_indicators = True
+ if ch in u'?:':
+ flow_indicators = True
+ if followed_by_whitespace:
+ block_indicators = True
+ if ch == u'-' and followed_by_whitespace:
+ flow_indicators = True
+ block_indicators = True
+ else:
+ # Some indicators cannot appear within a scalar as well.
+ if ch in u',?[]{}':
+ flow_indicators = True
+ if ch == u':':
+ flow_indicators = True
+ if followed_by_whitespace:
+ block_indicators = True
+ if ch == u'#' and preceeded_by_whitespace:
+ flow_indicators = True
+ block_indicators = True
+
+ # Check for line breaks, special, and unicode characters.
+ if ch in u'\n\x85\u2028\u2029':
+ line_breaks = True
+ if not (ch == u'\n' or u'\x20' <= ch <= u'\x7E'):
+ if (ch == u'\x85' or u'\xA0' <= ch <= u'\uD7FF'
+ or u'\uE000' <= ch <= u'\uFFFD') and ch != u'\uFEFF':
+ unicode_characters = True
+ if not self.allow_unicode:
+ special_characters = True
+ else:
+ special_characters = True
+
+ # Detect important whitespace combinations.
+ if ch == u' ':
+ if index == 0:
+ leading_space = True
+ if index == len(scalar)-1:
+ trailing_space = True
+ if previous_break:
+ break_space = True
+ previous_space = True
+ previous_break = False
+ elif ch in u'\n\x85\u2028\u2029':
+ if index == 0:
+ leading_break = True
+ if index == len(scalar)-1:
+ trailing_break = True
+ if previous_space:
+ space_break = True
+ previous_space = False
+ previous_break = True
+ else:
+ previous_space = False
+ previous_break = False
+
+ # Prepare for the next character.
+ index += 1
+ preceeded_by_whitespace = (ch in u'\0 \t\r\n\x85\u2028\u2029')
+ followed_by_whitespace = (index+1 >= len(scalar) or
+ scalar[index+1] in u'\0 \t\r\n\x85\u2028\u2029')
+
+ # Let's decide what styles are allowed.
+ allow_flow_plain = True
+ allow_block_plain = True
+ allow_single_quoted = True
+ allow_double_quoted = True
+ allow_block = True
+
+ # Leading and trailing whitespaces are bad for plain scalars.
+ if (leading_space or leading_break
+ or trailing_space or trailing_break):
+ allow_flow_plain = allow_block_plain = False
+
+ # We do not permit trailing spaces for block scalars.
+ if trailing_space:
+ allow_block = False
+
+ # Spaces at the beginning of a new line are only acceptable for block
+ # scalars.
+ if break_space:
+ allow_flow_plain = allow_block_plain = allow_single_quoted = False
+
+ # Spaces followed by breaks, as well as special character are only
+ # allowed for double quoted scalars.
+ if space_break or special_characters:
+ allow_flow_plain = allow_block_plain = \
+ allow_single_quoted = allow_block = False
+
+ # Although the plain scalar writer supports breaks, we never emit
+ # multiline plain scalars.
+ if line_breaks:
+ allow_flow_plain = allow_block_plain = False
+
+ # Flow indicators are forbidden for flow plain scalars.
+ if flow_indicators:
+ allow_flow_plain = False
+
+ # Block indicators are forbidden for block plain scalars.
+ if block_indicators:
+ allow_block_plain = False
+
+ return ScalarAnalysis(scalar=scalar,
+ empty=False, multiline=line_breaks,
+ allow_flow_plain=allow_flow_plain,
+ allow_block_plain=allow_block_plain,
+ allow_single_quoted=allow_single_quoted,
+ allow_double_quoted=allow_double_quoted,
+ allow_block=allow_block)
+
+ # Writers.
+
+ def flush_stream(self):
+ if hasattr(self.stream, 'flush'):
+ self.stream.flush()
+
+ def write_stream_start(self):
+ # Write BOM if needed.
+ if self.encoding and self.encoding.startswith('utf-16'):
+ self.stream.write(u'\uFEFF'.encode(self.encoding))
+
+ def write_stream_end(self):
+ self.flush_stream()
+
+ def write_indicator(self, indicator, need_whitespace,
+ whitespace=False, indention=False):
+ if self.whitespace or not need_whitespace:
+ data = indicator
+ else:
+ data = u' '+indicator
+ self.whitespace = whitespace
+ self.indention = self.indention and indention
+ self.column += len(data)
+ self.open_ended = False
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+
+ def write_indent(self):
+ indent = self.indent or 0
+ if not self.indention or self.column > indent \
+ or (self.column == indent and not self.whitespace):
+ self.write_line_break()
+ if self.column < indent:
+ self.whitespace = True
+ data = u' '*(indent-self.column)
+ self.column = indent
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+
+ def write_line_break(self, data=None):
+ if data is None:
+ data = self.best_line_break
+ self.whitespace = True
+ self.indention = True
+ self.line += 1
+ self.column = 0
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+
+ def write_version_directive(self, version_text):
+ data = u'%%YAML %s' % version_text
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ self.write_line_break()
+
+ def write_tag_directive(self, handle_text, prefix_text):
+ data = u'%%TAG %s %s' % (handle_text, prefix_text)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ self.write_line_break()
+
+ # Scalar streams.
+
+ def write_single_quoted(self, text, split=True):
+ self.write_indicator(u'\'', True)
+ spaces = False
+ breaks = False
+ start = end = 0
+ while end <= len(text):
+ ch = None
+ if end < len(text):
+ ch = text[end]
+ if spaces:
+ if ch is None or ch != u' ':
+ if start+1 == end and self.column > self.best_width and split \
+ and start != 0 and end != len(text):
+ self.write_indent()
+ else:
+ data = text[start:end]
+ self.column += len(data)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ start = end
+ elif breaks:
+ if ch is None or ch not in u'\n\x85\u2028\u2029':
+ if text[start] == u'\n':
+ self.write_line_break()
+ for br in text[start:end]:
+ if br == u'\n':
+ self.write_line_break()
+ else:
+ self.write_line_break(br)
+ self.write_indent()
+ start = end
+ else:
+ if ch is None or ch in u' \n\x85\u2028\u2029' or ch == u'\'':
+ if start < end:
+ data = text[start:end]
+ self.column += len(data)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ start = end
+ if ch == u'\'':
+ data = u'\'\''
+ self.column += 2
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ start = end + 1
+ if ch is not None:
+ spaces = (ch == u' ')
+ breaks = (ch in u'\n\x85\u2028\u2029')
+ end += 1
+ self.write_indicator(u'\'', False)
+
+ ESCAPE_REPLACEMENTS = {
+ u'\0': u'0',
+ u'\x07': u'a',
+ u'\x08': u'b',
+ u'\x09': u't',
+ u'\x0A': u'n',
+ u'\x0B': u'v',
+ u'\x0C': u'f',
+ u'\x0D': u'r',
+ u'\x1B': u'e',
+ u'\"': u'\"',
+ u'\\': u'\\',
+ u'\x85': u'N',
+ u'\xA0': u'_',
+ u'\u2028': u'L',
+ u'\u2029': u'P',
+ }
+
+ def write_double_quoted(self, text, split=True):
+ self.write_indicator(u'"', True)
+ start = end = 0
+ while end <= len(text):
+ ch = None
+ if end < len(text):
+ ch = text[end]
+ if ch is None or ch in u'"\\\x85\u2028\u2029\uFEFF' \
+ or not (u'\x20' <= ch <= u'\x7E'
+ or (self.allow_unicode
+ and (u'\xA0' <= ch <= u'\uD7FF'
+ or u'\uE000' <= ch <= u'\uFFFD'))):
+ if start < end:
+ data = text[start:end]
+ self.column += len(data)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ start = end
+ if ch is not None:
+ if ch in self.ESCAPE_REPLACEMENTS:
+ data = u'\\'+self.ESCAPE_REPLACEMENTS[ch]
+ elif ch <= u'\xFF':
+ data = u'\\x%02X' % ord(ch)
+ elif ch <= u'\uFFFF':
+ data = u'\\u%04X' % ord(ch)
+ else:
+ data = u'\\U%08X' % ord(ch)
+ self.column += len(data)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ start = end+1
+ if 0 < end < len(text)-1 and (ch == u' ' or start >= end) \
+ and self.column+(end-start) > self.best_width and split:
+ data = text[start:end]+u'\\'
+ if start < end:
+ start = end
+ self.column += len(data)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ self.write_indent()
+ self.whitespace = False
+ self.indention = False
+ if text[start] == u' ':
+ data = u'\\'
+ self.column += len(data)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ end += 1
+ self.write_indicator(u'"', False)
+
+ def determine_block_hints(self, text):
+ hints = u''
+ if text:
+ if text[0] in u' \n\x85\u2028\u2029':
+ hints += unicode(self.best_indent)
+ if text[-1] not in u'\n\x85\u2028\u2029':
+ hints += u'-'
+ elif len(text) == 1 or text[-2] in u'\n\x85\u2028\u2029':
+ hints += u'+'
+ return hints
+
+ def write_folded(self, text):
+ hints = self.determine_block_hints(text)
+ self.write_indicator(u'>'+hints, True)
+ if hints[-1:] == u'+':
+ self.open_ended = True
+ self.write_line_break()
+ leading_space = True
+ spaces = False
+ breaks = True
+ start = end = 0
+ while end <= len(text):
+ ch = None
+ if end < len(text):
+ ch = text[end]
+ if breaks:
+ if ch is None or ch not in u'\n\x85\u2028\u2029':
+ if not leading_space and ch is not None and ch != u' ' \
+ and text[start] == u'\n':
+ self.write_line_break()
+ leading_space = (ch == u' ')
+ for br in text[start:end]:
+ if br == u'\n':
+ self.write_line_break()
+ else:
+ self.write_line_break(br)
+ if ch is not None:
+ self.write_indent()
+ start = end
+ elif spaces:
+ if ch != u' ':
+ if start+1 == end and self.column > self.best_width:
+ self.write_indent()
+ else:
+ data = text[start:end]
+ self.column += len(data)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ start = end
+ else:
+ if ch is None or ch in u' \n\x85\u2028\u2029':
+ data = text[start:end]
+ self.column += len(data)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ if ch is None:
+ self.write_line_break()
+ start = end
+ if ch is not None:
+ breaks = (ch in u'\n\x85\u2028\u2029')
+ spaces = (ch == u' ')
+ end += 1
+
+ def write_literal(self, text):
+ hints = self.determine_block_hints(text)
+ self.write_indicator(u'|'+hints, True)
+ if hints[-1:] == u'+':
+ self.open_ended = True
+ self.write_line_break()
+ breaks = True
+ start = end = 0
+ while end <= len(text):
+ ch = None
+ if end < len(text):
+ ch = text[end]
+ if breaks:
+ if ch is None or ch not in u'\n\x85\u2028\u2029':
+ for br in text[start:end]:
+ if br == u'\n':
+ self.write_line_break()
+ else:
+ self.write_line_break(br)
+ if ch is not None:
+ self.write_indent()
+ start = end
+ else:
+ if ch is None or ch in u'\n\x85\u2028\u2029':
+ data = text[start:end]
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ if ch is None:
+ self.write_line_break()
+ start = end
+ if ch is not None:
+ breaks = (ch in u'\n\x85\u2028\u2029')
+ end += 1
+
+ def write_plain(self, text, split=True):
+ if self.root_context:
+ self.open_ended = True
+ if not text:
+ return
+ if not self.whitespace:
+ data = u' '
+ self.column += len(data)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ self.whitespace = False
+ self.indention = False
+ spaces = False
+ breaks = False
+ start = end = 0
+ while end <= len(text):
+ ch = None
+ if end < len(text):
+ ch = text[end]
+ if spaces:
+ if ch != u' ':
+ if start+1 == end and self.column > self.best_width and split:
+ self.write_indent()
+ self.whitespace = False
+ self.indention = False
+ else:
+ data = text[start:end]
+ self.column += len(data)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ start = end
+ elif breaks:
+ if ch not in u'\n\x85\u2028\u2029':
+ if text[start] == u'\n':
+ self.write_line_break()
+ for br in text[start:end]:
+ if br == u'\n':
+ self.write_line_break()
+ else:
+ self.write_line_break(br)
+ self.write_indent()
+ self.whitespace = False
+ self.indention = False
+ start = end
+ else:
+ if ch is None or ch in u' \n\x85\u2028\u2029':
+ data = text[start:end]
+ self.column += len(data)
+ if self.encoding:
+ data = data.encode(self.encoding)
+ self.stream.write(data)
+ start = end
+ if ch is not None:
+ spaces = (ch == u' ')
+ breaks = (ch in u'\n\x85\u2028\u2029')
+ end += 1
+
diff --git a/pynxc/yaml/error.py b/pynxc/yaml/error.py
new file mode 100644
index 0000000..577686d
--- /dev/null
+++ b/pynxc/yaml/error.py
@@ -0,0 +1,75 @@
+
+__all__ = ['Mark', 'YAMLError', 'MarkedYAMLError']
+
+class Mark(object):
+
+ def __init__(self, name, index, line, column, buffer, pointer):
+ self.name = name
+ self.index = index
+ self.line = line
+ self.column = column
+ self.buffer = buffer
+ self.pointer = pointer
+
+ def get_snippet(self, indent=4, max_length=75):
+ if self.buffer is None:
+ return None
+ head = ''
+ start = self.pointer
+ while start > 0 and self.buffer[start-1] not in u'\0\r\n\x85\u2028\u2029':
+ start -= 1
+ if self.pointer-start > max_length/2-1:
+ head = ' ... '
+ start += 5
+ break
+ tail = ''
+ end = self.pointer
+ while end < len(self.buffer) and self.buffer[end] not in u'\0\r\n\x85\u2028\u2029':
+ end += 1
+ if end-self.pointer > max_length/2-1:
+ tail = ' ... '
+ end -= 5
+ break
+ snippet = self.buffer[start:end].encode('utf-8')
+ return ' '*indent + head + snippet + tail + '\n' \
+ + ' '*(indent+self.pointer-start+len(head)) + '^'
+
+ def __str__(self):
+ snippet = self.get_snippet()
+ where = " in \"%s\", line %d, column %d" \
+ % (self.name, self.line+1, self.column+1)
+ if snippet is not None:
+ where += ":\n"+snippet
+ return where
+
+class YAMLError(Exception):
+ pass
+
+class MarkedYAMLError(YAMLError):
+
+ def __init__(self, context=None, context_mark=None,
+ problem=None, problem_mark=None, note=None):
+ self.context = context
+ self.context_mark = context_mark
+ self.problem = problem
+ self.problem_mark = problem_mark
+ self.note = note
+
+ def __str__(self):
+ lines = []
+ if self.context is not None:
+ lines.append(self.context)
+ if self.context_mark is not None \
+ and (self.problem is None or self.problem_mark is None
+ or self.context_mark.name != self.problem_mark.name
+ or self.context_mark.line != self.problem_mark.line
+ or self.context_mark.column != self.problem_mark.column):
+ lines.append(str(self.context_mark))
+ if self.problem is not None:
+ lines.append(self.problem)
+ if self.problem_mark is not None:
+ lines.append(str(self.problem_mark))
+ if self.note is not None:
+ lines.append(self.note)
+ return '\n'.join(lines)
+
diff --git a/pynxc/yaml/events.py b/pynxc/yaml/events.py
new file mode 100644
index 0000000..f79ad38
--- /dev/null
+++ b/pynxc/yaml/events.py
@@ -0,0 +1,86 @@
+
+# Abstract classes.
+
+class Event(object):
+ def __init__(self, start_mark=None, end_mark=None):
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ def __repr__(self):
+ attributes = [key for key in ['anchor', 'tag', 'implicit', 'value']
+ if hasattr(self, key)]
+ arguments = ', '.join(['%s=%r' % (key, getattr(self, key))
+ for key in attributes])
+ return '%s(%s)' % (self.__class__.__name__, arguments)
+
+class NodeEvent(Event):
+ def __init__(self, anchor, start_mark=None, end_mark=None):
+ self.anchor = anchor
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+
+class CollectionStartEvent(NodeEvent):
+ def __init__(self, anchor, tag, implicit, start_mark=None, end_mark=None,
+ flow_style=None):
+ self.anchor = anchor
+ self.tag = tag
+ self.implicit = implicit
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ self.flow_style = flow_style
+
+class CollectionEndEvent(Event):
+ pass
+
+# Implementations.
+
+class StreamStartEvent(Event):
+ def __init__(self, start_mark=None, end_mark=None, encoding=None):
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ self.encoding = encoding
+
+class StreamEndEvent(Event):
+ pass
+
+class DocumentStartEvent(Event):
+ def __init__(self, start_mark=None, end_mark=None,
+ explicit=None, version=None, tags=None):
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ self.explicit = explicit
+ self.version = version
+ self.tags = tags
+
+class DocumentEndEvent(Event):
+ def __init__(self, start_mark=None, end_mark=None,
+ explicit=None):
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ self.explicit = explicit
+
+class AliasEvent(NodeEvent):
+ pass
+
+class ScalarEvent(NodeEvent):
+ def __init__(self, anchor, tag, implicit, value,
+ start_mark=None, end_mark=None, style=None):
+ self.anchor = anchor
+ self.tag = tag
+ self.implicit = implicit
+ self.value = value
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ self.style = style
+
+class SequenceStartEvent(CollectionStartEvent):
+ pass
+
+class SequenceEndEvent(CollectionEndEvent):
+ pass
+
+class MappingStartEvent(CollectionStartEvent):
+ pass
+
+class MappingEndEvent(CollectionEndEvent):
+ pass
+
diff --git a/pynxc/yaml/loader.py b/pynxc/yaml/loader.py
new file mode 100644
index 0000000..293ff46
--- /dev/null
+++ b/pynxc/yaml/loader.py
@@ -0,0 +1,40 @@
+
+__all__ = ['BaseLoader', 'SafeLoader', 'Loader']
+
+from reader import *
+from scanner import *
+from parser import *
+from composer import *
+from constructor import *
+from resolver import *
+
+class BaseLoader(Reader, Scanner, Parser, Composer, BaseConstructor, BaseResolver):
+
+ def __init__(self, stream):
+ Reader.__init__(self, stream)
+ Scanner.__init__(self)
+ Parser.__init__(self)
+ Composer.__init__(self)
+ BaseConstructor.__init__(self)
+ BaseResolver.__init__(self)
+
+class SafeLoader(Reader, Scanner, Parser, Composer, SafeConstructor, Resolver):
+
+ def __init__(self, stream):
+ Reader.__init__(self, stream)
+ Scanner.__init__(self)
+ Parser.__init__(self)
+ Composer.__init__(self)
+ SafeConstructor.__init__(self)
+ Resolver.__init__(self)
+
+class Loader(Reader, Scanner, Parser, Composer, Constructor, Resolver):
+
+ def __init__(self, stream):
+ Reader.__init__(self, stream)
+ Scanner.__init__(self)
+ Parser.__init__(self)
+ Composer.__init__(self)
+ Constructor.__init__(self)
+ Resolver.__init__(self)
+
diff --git a/pynxc/yaml/nodes.py b/pynxc/yaml/nodes.py
new file mode 100644
index 0000000..c4f070c
--- /dev/null
+++ b/pynxc/yaml/nodes.py
@@ -0,0 +1,49 @@
+
+class Node(object):
+ def __init__(self, tag, value, start_mark, end_mark):
+ self.tag = tag
+ self.value = value
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ def __repr__(self):
+ value = self.value
+ #if isinstance(value, list):
+ # if len(value) == 0:
+ # value = '<empty>'
+ # elif len(value) == 1:
+ # value = '<1 item>'
+ # else:
+ # value = '<%d items>' % len(value)
+ #else:
+ # if len(value) > 75:
+ # value = repr(value[:70]+u' ... ')
+ # else:
+ # value = repr(value)
+ value = repr(value)
+ return '%s(tag=%r, value=%s)' % (self.__class__.__name__, self.tag, value)
+
+class ScalarNode(Node):
+ id = 'scalar'
+ def __init__(self, tag, value,
+ start_mark=None, end_mark=None, style=None):
+ self.tag = tag
+ self.value = value
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ self.style = style
+
+class CollectionNode(Node):
+ def __init__(self, tag, value,
+ start_mark=None, end_mark=None, flow_style=None):
+ self.tag = tag
+ self.value = value
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ self.flow_style = flow_style
+
+class SequenceNode(CollectionNode):
+ id = 'sequence'
+
+class MappingNode(CollectionNode):
+ id = 'mapping'
+
diff --git a/pynxc/yaml/parser.py b/pynxc/yaml/parser.py
new file mode 100644
index 0000000..b6a7416
--- /dev/null
+++ b/pynxc/yaml/parser.py
@@ -0,0 +1,584 @@
+
+# The following YAML grammar is LL(1) and is parsed by a recursive descent
+# parser.
+#
+# stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
+# implicit_document ::= block_node DOCUMENT-END*
+# explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
+# block_node_or_indentless_sequence ::=
+# ALIAS
+# | properties (block_content | indentless_block_sequence)?
+# | block_content
+# | indentless_block_sequence
+# block_node ::= ALIAS
+# | properties block_content?
+# | block_content
+# flow_node ::= ALIAS
+# | properties flow_content?
+# | flow_content
+# properties ::= TAG ANCHOR? | ANCHOR TAG?
+# block_content ::= block_collection | flow_collection | SCALAR
+# flow_content ::= flow_collection | SCALAR
+# block_collection ::= block_sequence | block_mapping
+# flow_collection ::= flow_sequence | flow_mapping
+# block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
+# indentless_sequence ::= (BLOCK-ENTRY block_node?)+
+# block_mapping ::= BLOCK-MAPPING_START
+# ((KEY block_node_or_indentless_sequence?)?
+# (VALUE block_node_or_indentless_sequence?)?)*
+# BLOCK-END
+# flow_sequence ::= FLOW-SEQUENCE-START
+# (flow_sequence_entry FLOW-ENTRY)*
+# flow_sequence_entry?
+# FLOW-SEQUENCE-END
+# flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
+# flow_mapping ::= FLOW-MAPPING-START
+# (flow_mapping_entry FLOW-ENTRY)*
+# flow_mapping_entry?
+# FLOW-MAPPING-END
+# flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
+#
+# FIRST sets:
+#
+# stream: { STREAM-START }
+# explicit_document: { DIRECTIVE DOCUMENT-START }
+# implicit_document: FIRST(block_node)
+# block_node: { ALIAS TAG ANCHOR SCALAR BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START }
+# flow_node: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START }
+# block_content: { BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START SCALAR }
+# flow_content: { FLOW-SEQUENCE-START FLOW-MAPPING-START SCALAR }
+# block_collection: { BLOCK-SEQUENCE-START BLOCK-MAPPING-START }
+# flow_collection: { FLOW-SEQUENCE-START FLOW-MAPPING-START }
+# block_sequence: { BLOCK-SEQUENCE-START }
+# block_mapping: { BLOCK-MAPPING-START }
+# block_node_or_indentless_sequence: { ALIAS ANCHOR TAG SCALAR BLOCK-SEQUENCE-START BLOCK-MAPPING-START FLOW-SEQUENCE-START FLOW-MAPPING-START BLOCK-ENTRY }
+# indentless_sequence: { ENTRY }
+# flow_collection: { FLOW-SEQUENCE-START FLOW-MAPPING-START }
+# flow_sequence: { FLOW-SEQUENCE-START }
+# flow_mapping: { FLOW-MAPPING-START }
+# flow_sequence_entry: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START KEY }
+# flow_mapping_entry: { ALIAS ANCHOR TAG SCALAR FLOW-SEQUENCE-START FLOW-MAPPING-START KEY }
+
+__all__ = ['Parser', 'ParserError']
+
+from error import MarkedYAMLError
+from tokens import *
+from events import *
+from scanner import *
+
+class ParserError(MarkedYAMLError):
+ pass
+
+class Parser(object):
+ # Since writing a recursive-descendant parser is a straightforward task, we
+ # do not give many comments here.
+
+ DEFAULT_TAGS = {
+ u'!': u'!',
+ u'!!': u'tag:yaml.org,2002:',
+ }
+
+ def __init__(self):
+ self.current_event = None
+ self.yaml_version = None
+ self.tag_handles = {}
+ self.states = []
+ self.marks = []
+ self.state = self.parse_stream_start
+
+ def check_event(self, *choices):
+ # Check the type of the next event.
+ if self.current_event is None:
+ if self.state:
+ self.current_event = self.state()
+ if self.current_event is not None:
+ if not choices:
+ return True
+ for choice in choices:
+ if isinstance(self.current_event, choice):
+ return True
+ return False
+
+ def peek_event(self):
+ # Get the next event.
+ if self.current_event is None:
+ if self.state:
+ self.current_event = self.state()
+ return self.current_event
+
+ def get_event(self):
+ # Get the next event and proceed further.
+ if self.current_event is None:
+ if self.state:
+ self.current_event = self.state()
+ value = self.current_event
+ self.current_event = None
+ return value
+
+ # stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
+ # implicit_document ::= block_node DOCUMENT-END*
+ # explicit_document ::= DIRECTIVE* DOCUMENT-START block_node? DOCUMENT-END*
+
+ def parse_stream_start(self):
+
+ # Parse the stream start.
+ token = self.get_token()
+ event = StreamStartEvent(token.start_mark, token.end_mark,
+ encoding=token.encoding)
+
+ # Prepare the next state.
+ self.state = self.parse_implicit_document_start
+
+ return event
+
+ def parse_implicit_document_start(self):
+
+ # Parse an implicit document.
+ if not self.check_token(DirectiveToken, DocumentStartToken,
+ StreamEndToken):
+ self.tag_handles = self.DEFAULT_TAGS
+ token = self.peek_token()
+ start_mark = end_mark = token.start_mark
+ event = DocumentStartEvent(start_mark, end_mark,
+ explicit=False)
+
+ # Prepare the next state.
+ self.states.append(self.parse_document_end)
+ self.state = self.parse_block_node
+
+ return event
+
+ else:
+ return self.parse_document_start()
+
+ def parse_document_start(self):
+
+ # Parse any extra document end indicators.
+ while self.check_token(DocumentEndToken):
+ self.get_token()
+
+ # Parse an explicit document.
+ if not self.check_token(StreamEndToken):
+ token = self.peek_token()
+ start_mark = token.start_mark
+ version, tags = self.process_directives()
+ if not self.check_token(DocumentStartToken):
+ raise ParserError(None, None,
+ "expected '<document start>', but found %r"
+ % self.peek_token().id,
+ self.peek_token().start_mark)
+ token = self.get_token()
+ end_mark = token.end_mark
+ event = DocumentStartEvent(start_mark, end_mark,
+ explicit=True, version=version, tags=tags)
+ self.states.append(self.parse_document_end)
+ self.state = self.parse_document_content
+ else:
+ # Parse the end of the stream.
+ token = self.get_token()
+ event = StreamEndEvent(token.start_mark, token.end_mark)
+ assert not self.states
+ assert not self.marks
+ self.state = None
+ return event
+
+ def parse_document_end(self):
+
+ # Parse the document end.
+ token = self.peek_token()
+ start_mark = end_mark = token.start_mark
+ explicit = False
+ if self.check_token(DocumentEndToken):
+ token = self.get_token()
+ end_mark = token.end_mark
+ explicit = True
+ event = DocumentEndEvent(start_mark, end_mark,
+ explicit=explicit)
+
+ # Prepare the next state.
+ self.state = self.parse_document_start
+
+ return event
+
+ def parse_document_content(self):
+ if self.check_token(DirectiveToken,
+ DocumentStartToken, DocumentEndToken, StreamEndToken):
+ event = self.process_empty_scalar(self.peek_token().start_mark)
+ self.state = self.states.pop()
+ return event
+ else:
+ return self.parse_block_node()
+
+ def process_directives(self):
+ self.yaml_version = None
+ self.tag_handles = {}
+ while self.check_token(DirectiveToken):
+ token = self.get_token()
+ if token.name == u'YAML':
+ if self.yaml_version is not None:
+ raise ParserError(None, None,
+ "found duplicate YAML directive", token.start_mark)
+ major, minor = token.value
+ if major != 1:
+ raise ParserError(None, None,
+ "found incompatible YAML document (version 1.* is required)",
+ token.start_mark)
+ self.yaml_version = token.value
+ elif token.name == u'TAG':
+ handle, prefix = token.value
+ if handle in self.tag_handles:
+ raise ParserError(None, None,
+ "duplicate tag handle %r" % handle.encode('utf-8'),
+ token.start_mark)
+ self.tag_handles[handle] = prefix
+ if self.tag_handles:
+ value = self.yaml_version, self.tag_handles.copy()
+ else:
+ value = self.yaml_version, None
+ for key in self.DEFAULT_TAGS:
+ if key not in self.tag_handles:
+ self.tag_handles[key] = self.DEFAULT_TAGS[key]
+ return value
+
+ # block_node_or_indentless_sequence ::= ALIAS
+ # | properties (block_content | indentless_block_sequence)?
+ # | block_content
+ # | indentless_block_sequence
+ # block_node ::= ALIAS
+ # | properties block_content?
+ # | block_content
+ # flow_node ::= ALIAS
+ # | properties flow_content?
+ # | flow_content
+ # properties ::= TAG ANCHOR? | ANCHOR TAG?
+ # block_content ::= block_collection | flow_collection | SCALAR
+ # flow_content ::= flow_collection | SCALAR
+ # block_collection ::= block_sequence | block_mapping
+ # flow_collection ::= flow_sequence | flow_mapping
+
+ def parse_block_node(self):
+ return self.parse_node(block=True)
+
+ def parse_flow_node(self):
+ return self.parse_node()
+
+ def parse_block_node_or_indentless_sequence(self):
+ return self.parse_node(block=True, indentless_sequence=True)
+
+ def parse_node(self, block=False, indentless_sequence=False):
+ if self.check_token(AliasToken):
+ token = self.get_token()
+ event = AliasEvent(token.value, token.start_mark, token.end_mark)
+ self.state = self.states.pop()
+ else:
+ anchor = None
+ tag = None
+ start_mark = end_mark = tag_mark = None
+ if self.check_token(AnchorToken):
+ token = self.get_token()
+ start_mark = token.start_mark
+ end_mark = token.end_mark
+ anchor = token.value
+ if self.check_token(TagToken):
+ token = self.get_token()
+ tag_mark = token.start_mark
+ end_mark = token.end_mark
+ tag = token.value
+ elif self.check_token(TagToken):
+ token = self.get_token()
+ start_mark = tag_mark = token.start_mark
+ end_mark = token.end_mark
+ tag = token.value
+ if self.check_token(AnchorToken):
+ token = self.get_token()
+ end_mark = token.end_mark
+ anchor = token.value
+ if tag is not None:
+ handle, suffix = tag
+ if handle is not None:
+ if handle not in self.tag_handles:
+ raise ParserError("while parsing a node", start_mark,
+ "found undefined tag handle %r" % handle.encode('utf-8'),
+ tag_mark)
+ tag = self.tag_handles[handle]+suffix
+ else:
+ tag = suffix
+ #if tag == u'!':
+ # raise ParserError("while parsing a node", start_mark,
+ # "found non-specific tag '!'", tag_mark,
+ # "Please check 'http://pyyaml.org/wiki/YAMLNonSpecificTag' and share your opinion.")
+ if start_mark is None:
+ start_mark = end_mark = self.peek_token().start_mark
+ event = None
+ implicit = (tag is None or tag == u'!')
+ if indentless_sequence and self.check_token(BlockEntryToken):
+ end_mark = self.peek_token().end_mark
+ event = SequenceStartEvent(anchor, tag, implicit,
+ start_mark, end_mark)
+ self.state = self.parse_indentless_sequence_entry
+ else:
+ if self.check_token(ScalarToken):
+ token = self.get_token()
+ end_mark = token.end_mark
+ if (token.plain and tag is None) or tag == u'!':
+ implicit = (True, False)
+ elif tag is None:
+ implicit = (False, True)
+ else:
+ implicit = (False, False)
+ event = ScalarEvent(anchor, tag, implicit, token.value,
+ start_mark, end_mark, style=token.style)
+ self.state = self.states.pop()
+ elif self.check_token(FlowSequenceStartToken):
+ end_mark = self.peek_token().end_mark
+ event = SequenceStartEvent(anchor, tag, implicit,
+ start_mark, end_mark, flow_style=True)
+ self.state = self.parse_flow_sequence_first_entry
+ elif self.check_token(FlowMappingStartToken):
+ end_mark = self.peek_token().end_mark
+ event = MappingStartEvent(anchor, tag, implicit,
+ start_mark, end_mark, flow_style=True)
+ self.state = self.parse_flow_mapping_first_key
+ elif block and self.check_token(BlockSequenceStartToken):
+ end_mark = self.peek_token().start_mark
+ event = SequenceStartEvent(anchor, tag, implicit,
+ start_mark, end_mark, flow_style=False)
+ self.state = self.parse_block_sequence_first_entry
+ elif block and self.check_token(BlockMappingStartToken):
+ end_mark = self.peek_token().start_mark
+ event = MappingStartEvent(anchor, tag, implicit,
+ start_mark, end_mark, flow_style=False)
+ self.state = self.parse_block_mapping_first_key
+ elif anchor is not None or tag is not None:
+ # Empty scalars are allowed even if a tag or an anchor is
+ # specified.
+ event = ScalarEvent(anchor, tag, (implicit, False), u'',
+ start_mark, end_mark)
+ self.state = self.states.pop()
+ else:
+ if block:
+ node = 'block'
+ else:
+ node = 'flow'
+ token = self.peek_token()
+ raise ParserError("while parsing a %s node" % node, start_mark,
+ "expected the node content, but found %r" % token.id,
+ token.start_mark)
+ return event
+
+ # block_sequence ::= BLOCK-SEQUENCE-START (BLOCK-ENTRY block_node?)* BLOCK-END
+
+ def parse_block_sequence_first_entry(self):
+ token = self.get_token()
+ self.marks.append(token.start_mark)
+ return self.parse_block_sequence_entry()
+
+ def parse_block_sequence_entry(self):
+ if self.check_token(BlockEntryToken):
+ token = self.get_token()
+ if not self.check_token(BlockEntryToken, BlockEndToken):
+ self.states.append(self.parse_block_sequence_entry)
+ return self.parse_block_node()
+ else:
+ self.state = self.parse_block_sequence_entry
+ return self.process_empty_scalar(token.end_mark)
+ if not self.check_token(BlockEndToken):
+ token = self.peek_token()
+ raise ParserError("while parsing a block collection", self.marks[-1],
+ "expected <block end>, but found %r" % token.id, token.start_mark)
+ token = self.get_token()
+ event = SequenceEndEvent(token.start_mark, token.end_mark)
+ self.state = self.states.pop()
+ self.marks.pop()
+ return event
+
+ # indentless_sequence ::= (BLOCK-ENTRY block_node?)+
+
+ def parse_indentless_sequence_entry(self):
+ if self.check_token(BlockEntryToken):
+ token = self.get_token()
+ if not self.check_token(BlockEntryToken,
+ KeyToken, ValueToken, BlockEndToken):
+ self.states.append(self.parse_indentless_sequence_entry)
+ return self.parse_block_node()
+ else:
+ self.state = self.parse_indentless_sequence_entry
+ return self.process_empty_scalar(token.end_mark)
+ token = self.peek_token()
+ event = SequenceEndEvent(token.start_mark, token.start_mark)
+ self.state = self.states.pop()
+ return event
+
+ # block_mapping ::= BLOCK-MAPPING_START
+ # ((KEY block_node_or_indentless_sequence?)?
+ # (VALUE block_node_or_indentless_sequence?)?)*
+ # BLOCK-END
+
+ def parse_block_mapping_first_key(self):
+ token = self.get_token()
+ self.marks.append(token.start_mark)
+ return self.parse_block_mapping_key()
+
+ def parse_block_mapping_key(self):
+ if self.check_token(KeyToken):
+ token = self.get_token()
+ if not self.check_token(KeyToken, ValueToken, BlockEndToken):
+ self.states.append(self.parse_block_mapping_value)
+ return self.parse_block_node_or_indentless_sequence()
+ else:
+ self.state = self.parse_block_mapping_value
+ return self.process_empty_scalar(token.end_mark)
+ if not self.check_token(BlockEndToken):
+ token = self.peek_token()
+ raise ParserError("while parsing a block mapping", self.marks[-1],
+ "expected <block end>, but found %r" % token.id, token.start_mark)
+ token = self.get_token()
+ event = MappingEndEvent(token.start_mark, token.end_mark)
+ self.state = self.states.pop()
+ self.marks.pop()
+ return event
+
+ def parse_block_mapping_value(self):
+ if self.check_token(ValueToken):
+ token = self.get_token()
+ if not self.check_token(KeyToken, ValueToken, BlockEndToken):
+ self.states.append(self.parse_block_mapping_key)
+ return self.parse_block_node_or_indentless_sequence()
+ else:
+ self.state = self.parse_block_mapping_key
+ return self.process_empty_scalar(token.end_mark)
+ else:
+ self.state = self.parse_block_mapping_key
+ token = self.peek_token()
+ return self.process_empty_scalar(token.start_mark)
+
+ # flow_sequence ::= FLOW-SEQUENCE-START
+ # (flow_sequence_entry FLOW-ENTRY)*
+ # flow_sequence_entry?
+ # FLOW-SEQUENCE-END
+ # flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
+ #
+ # Note that while production rules for both flow_sequence_entry and
+ # flow_mapping_entry are equal, their interpretations are different.
+ # For `flow_sequence_entry`, the part `KEY flow_node? (VALUE flow_node?)?`
+ # generate an inline mapping (set syntax).
+
+ def parse_flow_sequence_first_entry(self):
+ token = self.get_token()
+ self.marks.append(token.start_mark)
+ return self.parse_flow_sequence_entry(first=True)
+
+ def parse_flow_sequence_entry(self, first=False):
+ if not self.check_token(FlowSequenceEndToken):
+ if not first:
+ if self.check_token(FlowEntryToken):
+ self.get_token()
+ else:
+ token = self.peek_token()
+ raise ParserError("while parsing a flow sequence", self.marks[-1],
+ "expected ',' or ']', but got %r" % token.id, token.start_mark)
+
+ if self.check_token(KeyToken):
+ token = self.peek_token()
+ event = MappingStartEvent(None, None, True,
+ token.start_mark, token.end_mark,
+ flow_style=True)
+ self.state = self.parse_flow_sequence_entry_mapping_key
+ return event
+ elif not self.check_token(FlowSequenceEndToken):
+ self.states.append(self.parse_flow_sequence_entry)
+ return self.parse_flow_node()
+ token = self.get_token()
+ event = SequenceEndEvent(token.start_mark, token.end_mark)
+ self.state = self.states.pop()
+ self.marks.pop()
+ return event
+
+ def parse_flow_sequence_entry_mapping_key(self):
+ token = self.get_token()
+ if not self.check_token(ValueToken,
+ FlowEntryToken, FlowSequenceEndToken):
+ self.states.append(self.parse_flow_sequence_entry_mapping_value)
+ return self.parse_flow_node()
+ else:
+ self.state = self.parse_flow_sequence_entry_mapping_value
+ return self.process_empty_scalar(token.end_mark)
+
+ def parse_flow_sequence_entry_mapping_value(self):
+ if self.check_token(ValueToken):
+ token = self.get_token()
+ if not self.check_token(FlowEntryToken, FlowSequenceEndToken):
+ self.states.append(self.parse_flow_sequence_entry_mapping_end)
+ return self.parse_flow_node()
+ else:
+ self.state = self.parse_flow_sequence_entry_mapping_end
+ return self.process_empty_scalar(token.end_mark)
+ else:
+ self.state = self.parse_flow_sequence_entry_mapping_end
+ token = self.peek_token()
+ return self.process_empty_scalar(token.start_mark)
+
+ def parse_flow_sequence_entry_mapping_end(self):
+ self.state = self.parse_flow_sequence_entry
+ token = self.peek_token()
+ return MappingEndEvent(token.start_mark, token.start_mark)
+
+ # flow_mapping ::= FLOW-MAPPING-START
+ # (flow_mapping_entry FLOW-ENTRY)*
+ # flow_mapping_entry?
+ # FLOW-MAPPING-END
+ # flow_mapping_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
+
+ def parse_flow_mapping_first_key(self):
+ token = self.get_token()
+ self.marks.append(token.start_mark)
+ return self.parse_flow_mapping_key(first=True)
+
+ def parse_flow_mapping_key(self, first=False):
+ if not self.check_token(FlowMappingEndToken):
+ if not first:
+ if self.check_token(FlowEntryToken):
+ self.get_token()
+ else:
+ token = self.peek_token()
+ raise ParserError("while parsing a flow mapping", self.marks[-1],
+ "expected ',' or '}', but got %r" % token.id, token.start_mark)
+ if self.check_token(KeyToken):
+ token = self.get_token()
+ if not self.check_token(ValueToken,
+ FlowEntryToken, FlowMappingEndToken):
+ self.states.append(self.parse_flow_mapping_value)
+ return self.parse_flow_node()
+ else:
+ self.state = self.parse_flow_mapping_value
+ return self.process_empty_scalar(token.end_mark)
+ elif not self.check_token(FlowMappingEndToken):
+ self.states.append(self.parse_flow_mapping_empty_value)
+ return self.parse_flow_node()
+ token = self.get_token()
+ event = MappingEndEvent(token.start_mark, token.end_mark)
+ self.state = self.states.pop()
+ self.marks.pop()
+ return event
+
+ def parse_flow_mapping_value(self):
+ if self.check_token(ValueToken):
+ token = self.get_token()
+ if not self.check_token(FlowEntryToken, FlowMappingEndToken):
+ self.states.append(self.parse_flow_mapping_key)
+ return self.parse_flow_node()
+ else:
+ self.state = self.parse_flow_mapping_key
+ return self.process_empty_scalar(token.end_mark)
+ else:
+ self.state = self.parse_flow_mapping_key
+ token = self.peek_token()
+ return self.process_empty_scalar(token.start_mark)
+
+ def parse_flow_mapping_empty_value(self):
+ self.state = self.parse_flow_mapping_key
+ return self.process_empty_scalar(self.peek_token().start_mark)
+
+ def process_empty_scalar(self, mark):
+ return ScalarEvent(None, None, (True, False), u'', mark, mark)
+
diff --git a/pynxc/yaml/reader.py b/pynxc/yaml/reader.py
new file mode 100644
index 0000000..1e7a4db
--- /dev/null
+++ b/pynxc/yaml/reader.py
@@ -0,0 +1,225 @@
+# This module contains abstractions for the input stream. You don't have to
+# looks further, there are no pretty code.
+#
+# We define two classes here.
+#
+# Mark(source, line, column)
+# It's just a record and its only use is producing nice error messages.
+# Parser does not use it for any other purposes.
+#
+# Reader(source, data)
+# Reader determines the encoding of `data` and converts it to unicode.
+# Reader provides the following methods and attributes:
+# reader.peek(length=1) - return the next `length` characters
+# reader.forward(length=1) - move the current position to `length` characters.
+# reader.index - the number of the current character.
+# reader.line, stream.column - the line and the column of the current character.
+
+__all__ = ['Reader', 'ReaderError']
+
+from error import YAMLError, Mark
+
+import codecs, re
+
+# Unfortunately, codec functions in Python 2.3 does not support the `finish`
+# arguments, so we have to write our own wrappers.
+
+try:
+ codecs.utf_8_decode('', 'strict', False)
+ from codecs import utf_8_decode, utf_16_le_decode, utf_16_be_decode
+
+except TypeError:
+
+ def utf_16_le_decode(data, errors, finish=False):
+ if not finish and len(data) % 2 == 1:
+ data = data[:-1]
+ return codecs.utf_16_le_decode(data, errors)
+
+ def utf_16_be_decode(data, errors, finish=False):
+ if not finish and len(data) % 2 == 1:
+ data = data[:-1]
+ return codecs.utf_16_be_decode(data, errors)
+
+ def utf_8_decode(data, errors, finish=False):
+ if not finish:
+ # We are trying to remove a possible incomplete multibyte character
+ # from the suffix of the data.
+ # The first byte of a multi-byte sequence is in the range 0xc0 to 0xfd.
+ # All further bytes are in the range 0x80 to 0xbf.
+ # UTF-8 encoded UCS characters may be up to six bytes long.
+ count = 0
+ while count < 5 and count < len(data) \
+ and '\x80' <= data[-count-1] <= '\xBF':
+ count -= 1
+ if count < 5 and count < len(data) \
+ and '\xC0' <= data[-count-1] <= '\xFD':
+ data = data[:-count-1]
+ return codecs.utf_8_decode(data, errors)
+
+class ReaderError(YAMLError):
+
+ def __init__(self, name, position, character, encoding, reason):
+ self.name = name
+ self.character = character
+ self.position = position
+ self.encoding = encoding
+ self.reason = reason
+
+ def __str__(self):
+ if isinstance(self.character, str):
+ return "'%s' codec can't decode byte #x%02x: %s\n" \
+ " in \"%s\", position %d" \
+ % (self.encoding, ord(self.character), self.reason,
+ self.name, self.position)
+ else:
+ return "unacceptable character #x%04x: %s\n" \
+ " in \"%s\", position %d" \
+ % (self.character, self.reason,
+ self.name, self.position)
+
+class Reader(object):
+ # Reader:
+ # - determines the data encoding and converts it to unicode,
+ # - checks if characters are in allowed range,
+ # - adds '\0' to the end.
+
+ # Reader accepts
+ # - a `str` object,
+ # - a `unicode` object,
+ # - a file-like object with its `read` method returning `str`,
+ # - a file-like object with its `read` method returning `unicode`.
+
+ # Yeah, it's ugly and slow.
+
+ def __init__(self, stream):
+ self.name = None
+ self.stream = None
+ self.stream_pointer = 0
+ self.eof = True
+ self.buffer = u''
+ self.pointer = 0
+ self.raw_buffer = None
+ self.raw_decode = None
+ self.encoding = None
+ self.index = 0
+ self.line = 0
+ self.column = 0
+ if isinstance(stream, unicode):
+ self.name = "<unicode string>"
+ self.check_printable(stream)
+ self.buffer = stream+u'\0'
+ elif isinstance(stream, str):
+ self.name = "<string>"
+ self.raw_buffer = stream
+ self.determine_encoding()
+ else:
+ self.stream = stream
+ self.name = getattr(stream, 'name', "<file>")
+ self.eof = False
+ self.raw_buffer = ''
+ self.determine_encoding()
+
+ def peek(self, index=0):
+ try:
+ return self.buffer[self.pointer+index]
+ except IndexError:
+ self.update(index+1)
+ return self.buffer[self.pointer+index]
+
+ def prefix(self, length=1):
+ if self.pointer+length >= len(self.buffer):
+ self.update(length)
+ return self.buffer[self.pointer:self.pointer+length]
+
+ def forward(self, length=1):
+ if self.pointer+length+1 >= len(self.buffer):
+ self.update(length+1)
+ while length:
+ ch = self.buffer[self.pointer]
+ self.pointer += 1
+ self.index += 1
+ if ch in u'\n\x85\u2028\u2029' \
+ or (ch == u'\r' and self.buffer[self.pointer] != u'\n'):
+ self.line += 1
+ self.column = 0
+ elif ch != u'\uFEFF':
+ self.column += 1
+ length -= 1
+
+ def get_mark(self):
+ if self.stream is None:
+ return Mark(self.name, self.index, self.line, self.column,
+ self.buffer, self.pointer)
+ else:
+ return Mark(self.name, self.index, self.line, self.column,
+ None, None)
+
+ def determine_encoding(self):
+ while not self.eof and len(self.raw_buffer) < 2:
+ self.update_raw()
+ if not isinstance(self.raw_buffer, unicode):
+ if self.raw_buffer.startswith(codecs.BOM_UTF16_LE):
+ self.raw_decode = utf_16_le_decode
+ self.encoding = 'utf-16-le'
+ elif self.raw_buffer.startswith(codecs.BOM_UTF16_BE):
+ self.raw_decode = utf_16_be_decode
+ self.encoding = 'utf-16-be'
+ else:
+ self.raw_decode = utf_8_decode
+ self.encoding = 'utf-8'
+ self.update(1)
+
+ NON_PRINTABLE = re.compile(u'[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\uD7FF\uE000-\uFFFD]')
+ def check_printable(self, data):
+ match = self.NON_PRINTABLE.search(data)
+ if match:
+ character = match.group()
+ position = self.index+(len(self.buffer)-self.pointer)+match.start()
+ raise ReaderError(self.name, position, ord(character),
+ 'unicode', "special characters are not allowed")
+
+ def update(self, length):
+ if self.raw_buffer is None:
+ return
+ self.buffer = self.buffer[self.pointer:]
+ self.pointer = 0
+ while len(self.buffer) < length:
+ if not self.eof:
+ self.update_raw()
+ if self.raw_decode is not None:
+ try:
+ data, converted = self.raw_decode(self.raw_buffer,
+ 'strict', self.eof)
+ except UnicodeDecodeError, exc:
+ character = exc.object[exc.start]
+ if self.stream is not None:
+ position = self.stream_pointer-len(self.raw_buffer)+exc.start
+ else:
+ position = exc.start
+ raise ReaderError(self.name, position, character,
+ exc.encoding, exc.reason)
+ else:
+ data = self.raw_buffer
+ converted = len(data)
+ self.check_printable(data)
+ self.buffer += data
+ self.raw_buffer = self.raw_buffer[converted:]
+ if self.eof:
+ self.buffer += u'\0'
+ self.raw_buffer = None
+ break
+
+ def update_raw(self, size=1024):
+ data = self.stream.read(size)
+ if data:
+ self.raw_buffer += data
+ self.stream_pointer += len(data)
+ else:
+ self.eof = True
+
+#try:
+# import psyco
+# psyco.bind(Reader)
+#except ImportError:
+# pass
+
diff --git a/pynxc/yaml/representer.py b/pynxc/yaml/representer.py
new file mode 100644
index 0000000..f5606ec
--- /dev/null
+++ b/pynxc/yaml/representer.py
@@ -0,0 +1,489 @@
+
+__all__ = ['BaseRepresenter', 'SafeRepresenter', 'Representer',
+ 'RepresenterError']
+
+from error import *
+from nodes import *
+
+import datetime
+
+try:
+ set
+except NameError:
+ from sets import Set as set
+
+import sys, copy_reg, types
+
+class RepresenterError(YAMLError):
+ pass
+
+class BaseRepresenter(object):
+
+ yaml_representers = {}
+ yaml_multi_representers = {}
+
+ def __init__(self, default_style=None, default_flow_style=None):
+ self.default_style = default_style
+ self.default_flow_style = default_flow_style
+ self.represented_objects = {}
+ self.object_keeper = []
+ self.alias_key = None
+
+ def represent(self, data):
+ node = self.represent_data(data)
+ self.serialize(node)
+ self.represented_objects = {}
+ self.object_keeper = []
+ self.alias_key = None
+
+ def get_classobj_bases(self, cls):
+ bases = [cls]
+ for base in cls.__bases__:
+ bases.extend(self.get_classobj_bases(base))
+ return bases
+
+ def represent_data(self, data):
+ if self.ignore_aliases(data):
+ self.alias_key = None
+ else:
+ self.alias_key = id(data)
+ if self.alias_key is not None:
+ if self.alias_key in self.represented_objects:
+ node = self.represented_objects[self.alias_key]
+ #if node is None:
+ # raise RepresenterError("recursive objects are not allowed: %r" % data)
+ return node
+ #self.represented_objects[alias_key] = None
+ self.object_keeper.append(data)
+ data_types = type(data).__mro__
+ if type(data) is types.InstanceType:
+ data_types = self.get_classobj_bases(data.__class__)+list(data_types)
+ if data_types[0] in self.yaml_representers:
+ node = self.yaml_representers[data_types[0]](self, data)
+ else:
+ for data_type in data_types:
+ if data_type in self.yaml_multi_representers:
+ node = self.yaml_multi_representers[data_type](self, data)
+ break
+ else:
+ if None in self.yaml_multi_representers:
+ node = self.yaml_multi_representers[None](self, data)
+ elif None in self.yaml_representers:
+ node = self.yaml_representers[None](self, data)
+ else:
+ node = ScalarNode(None, unicode(data))
+ #if alias_key is not None:
+ # self.represented_objects[alias_key] = node
+ return node
+
+ def add_representer(cls, data_type, representer):
+ if not 'yaml_representers' in cls.__dict__:
+ cls.yaml_representers = cls.yaml_representers.copy()
+ cls.yaml_representers[data_type] = representer
+ add_representer = classmethod(add_representer)
+
+ def add_multi_representer(cls, data_type, representer):
+ if not 'yaml_multi_representers' in cls.__dict__:
+ cls.yaml_multi_representers = cls.yaml_multi_representers.copy()
+ cls.yaml_multi_representers[data_type] = representer
+ add_multi_representer = classmethod(add_multi_representer)
+
+ def represent_scalar(self, tag, value, style=None):
+ if style is None:
+ style = self.default_style
+ node = ScalarNode(tag, value, style=style)
+ if self.alias_key is not None:
+ self.represented_objects[self.alias_key] = node
+ return node
+
+ def represent_sequence(self, tag, sequence, flow_style=None):
+ value = []
+ node = SequenceNode(tag, value, flow_style=flow_style)
+ if self.alias_key is not None:
+ self.represented_objects[self.alias_key] = node
+ best_style = True
+ for item in sequence:
+ node_item = self.represent_data(item)
+ if not (isinstance(node_item, ScalarNode) and not node_item.style):
+ best_style = False
+ value.append(node_item)
+ if flow_style is None:
+ if self.default_flow_style is not None:
+ node.flow_style = self.default_flow_style
+ else:
+ node.flow_style = best_style
+ return node
+
+ def represent_mapping(self, tag, mapping, flow_style=None):
+ value = []
+ node = MappingNode(tag, value, flow_style=flow_style)
+ if self.alias_key is not None:
+ self.represented_objects[self.alias_key] = node
+ best_style = True
+ if hasattr(mapping, 'items'):
+ mapping = mapping.items()
+ mapping.sort()
+ for item_key, item_value in mapping:
+ node_key = self.represent_data(item_key)
+ node_value = self.represent_data(item_value)
+ if not (isinstance(node_key, ScalarNode) and not node_key.style):
+ best_style = False
+ if not (isinstance(node_value, ScalarNode) and not node_value.style):
+ best_style = False
+ value.append((node_key, node_value))
+ if flow_style is None:
+ if self.default_flow_style is not None:
+ node.flow_style = self.default_flow_style
+ else:
+ node.flow_style = best_style
+ return node
+
+ def ignore_aliases(self, data):
+ return False
+
+class SafeRepresenter(BaseRepresenter):
+
+ def ignore_aliases(self, data):
+ if data in [None, ()]:
+ return True
+ if isinstance(data, (str, unicode, bool, int, float)):
+ return True
+
+ def represent_none(self, data):
+ return self.represent_scalar(u'tag:yaml.org,2002:null',
+ u'null')
+
+ def represent_str(self, data):
+ tag = None
+ style = None
+ try:
+ data = unicode(data, 'ascii')
+ tag = u'tag:yaml.org,2002:str'
+ except UnicodeDecodeError:
+ try:
+ data = unicode(data, 'utf-8')
+ tag = u'tag:yaml.org,2002:str'
+ except UnicodeDecodeError:
+ data = data.encode('base64')
+ tag = u'tag:yaml.org,2002:binary'
+ style = '|'
+ return self.represent_scalar(tag, data, style=style)
+
+ def represent_unicode(self, data):
+ return self.represent_scalar(u'tag:yaml.org,2002:str', data)
+
+ def represent_bool(self, data):
+ if data:
+ value = u'true'
+ else:
+ value = u'false'
+ return self.represent_scalar(u'tag:yaml.org,2002:bool', value)
+
+ def represent_int(self, data):
+ return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data))
+
+ def represent_long(self, data):
+ return self.represent_scalar(u'tag:yaml.org,2002:int', unicode(data))
+
+ inf_value = 1e300
+ while repr(inf_value) != repr(inf_value*inf_value):
+ inf_value *= inf_value
+
+ def represent_float(self, data):
+ if data != data or (data == 0.0 and data == 1.0):
+ value = u'.nan'
+ elif data == self.inf_value:
+ value = u'.inf'
+ elif data == -self.inf_value:
+ value = u'-.inf'
+ else:
+ value = unicode(repr(data)).lower()
+ # Note that in some cases `repr(data)` represents a float number
+ # without the decimal parts. For instance:
+ # >>> repr(1e17)
+ # '1e17'
+ # Unfortunately, this is not a valid float representation according
+ # to the definition of the `!!float` tag. We fix this by adding
+ # '.0' before the 'e' symbol.
+ if u'.' not in value and u'e' in value:
+ value = value.replace(u'e', u'.0e', 1)
+ return self.represent_scalar(u'tag:yaml.org,2002:float', value)
+
+ def represent_list(self, data):
+ #pairs = (len(data) > 0 and isinstance(data, list))
+ #if pairs:
+ # for item in data:
+ # if not isinstance(item, tuple) or len(item) != 2:
+ # pairs = False
+ # break
+ #if not pairs:
+ return self.represent_sequence(u'tag:yaml.org,2002:seq', data)
+ #value = []
+ #for item_key, item_value in data:
+ # value.append(self.represent_mapping(u'tag:yaml.org,2002:map',
+ # [(item_key, item_value)]))
+ #return SequenceNode(u'tag:yaml.org,2002:pairs', value)
+
+ def represent_dict(self, data):
+ return self.represent_mapping(u'tag:yaml.org,2002:map', data)
+
+ def represent_set(self, data):
+ value = {}
+ for key in data:
+ value[key] = None
+ return self.represent_mapping(u'tag:yaml.org,2002:set', value)
+
+ def represent_date(self, data):
+ value = unicode(data.isoformat())
+ return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
+
+ def represent_datetime(self, data):
+ value = unicode(data.isoformat(' '))
+ return self.represent_scalar(u'tag:yaml.org,2002:timestamp', value)
+
+ def represent_yaml_object(self, tag, data, cls, flow_style=None):
+ if hasattr(data, '__getstate__'):
+ state = data.__getstate__()
+ else:
+ state = data.__dict__.copy()
+ return self.represent_mapping(tag, state, flow_style=flow_style)
+
+ def represent_undefined(self, data):
+ raise RepresenterError("cannot represent an object: %s" % data)
+
+SafeRepresenter.add_representer(type(None),
+ SafeRepresenter.represent_none)
+
+SafeRepresenter.add_representer(str,
+ SafeRepresenter.represent_str)
+
+SafeRepresenter.add_representer(unicode,
+ SafeRepresenter.represent_unicode)
+
+SafeRepresenter.add_representer(bool,
+ SafeRepresenter.represent_bool)
+
+SafeRepresenter.add_representer(int,
+ SafeRepresenter.represent_int)
+
+SafeRepresenter.add_representer(long,
+ SafeRepresenter.represent_long)
+
+SafeRepresenter.add_representer(float,
+ SafeRepresenter.represent_float)
+
+SafeRepresenter.add_representer(list,
+ SafeRepresenter.represent_list)
+
+SafeRepresenter.add_representer(tuple,
+ SafeRepresenter.represent_list)
+
+SafeRepresenter.add_representer(dict,
+ SafeRepresenter.represent_dict)
+
+SafeRepresenter.add_representer(set,
+ SafeRepresenter.represent_set)
+
+SafeRepresenter.add_representer(datetime.date,
+ SafeRepresenter.represent_date)
+
+SafeRepresenter.add_representer(datetime.datetime,
+ SafeRepresenter.represent_datetime)
+
+SafeRepresenter.add_representer(None,
+ SafeRepresenter.represent_undefined)
+
+class Representer(SafeRepresenter):
+
+ def represent_str(self, data):
+ tag = None
+ style = None
+ try:
+ data = unicode(data, 'ascii')
+ tag = u'tag:yaml.org,2002:str'
+ except UnicodeDecodeError:
+ try:
+ data = unicode(data, 'utf-8')
+ tag = u'tag:yaml.org,2002:python/str'
+ except UnicodeDecodeError:
+ data = data.encode('base64')
+ tag = u'tag:yaml.org,2002:binary'
+ style = '|'
+ return self.represent_scalar(tag, data, style=style)
+
+ def represent_unicode(self, data):
+ tag = None
+ try:
+ data.encode('ascii')
+ tag = u'tag:yaml.org,2002:python/unicode'
+ except UnicodeEncodeError:
+ tag = u'tag:yaml.org,2002:str'
+ return self.represent_scalar(tag, data)
+
+ def represent_long(self, data):
+ tag = u'tag:yaml.org,2002:int'
+ if int(data) is not data:
+ tag = u'tag:yaml.org,2002:python/long'
+ return self.represent_scalar(tag, unicode(data))
+
+ def represent_complex(self, data):
+ if data.imag == 0.0:
+ data = u'%r' % data.real
+ elif data.real == 0.0:
+ data = u'%rj' % data.imag
+ elif data.imag > 0:
+ data = u'%r+%rj' % (data.real, data.imag)
+ else:
+ data = u'%r%rj' % (data.real, data.imag)
+ return self.represent_scalar(u'tag:yaml.org,2002:python/complex', data)
+
+ def represent_tuple(self, data):
+ return self.represent_sequence(u'tag:yaml.org,2002:python/tuple', data)
+
+ def represent_name(self, data):
+ name = u'%s.%s' % (data.__module__, data.__name__)
+ return self.represent_scalar(u'tag:yaml.org,2002:python/name:'+name, u'')
+
+ def represent_module(self, data):
+ return self.represent_scalar(
+ u'tag:yaml.org,2002:python/module:'+data.__name__, u'')
+
+ def represent_instance(self, data):
+ # For instances of classic classes, we use __getinitargs__ and
+ # __getstate__ to serialize the data.
+
+ # If data.__getinitargs__ exists, the object must be reconstructed by
+ # calling cls(**args), where args is a tuple returned by
+ # __getinitargs__. Otherwise, the cls.__init__ method should never be
+ # called and the class instance is created by instantiating a trivial
+ # class and assigning to the instance's __class__ variable.
+
+ # If data.__getstate__ exists, it returns the state of the object.
+ # Otherwise, the state of the object is data.__dict__.
+
+ # We produce either a !!python/object or !!python/object/new node.
+ # If data.__getinitargs__ does not exist and state is a dictionary, we
+ # produce a !!python/object node . Otherwise we produce a
+ # !!python/object/new node.
+
+ cls = data.__class__
+ class_name = u'%s.%s' % (cls.__module__, cls.__name__)
+ args = None
+ state = None
+ if hasattr(data, '__getinitargs__'):
+ args = list(data.__getinitargs__())
+ if hasattr(data, '__getstate__'):
+ state = data.__getstate__()
+ else:
+ state = data.__dict__
+ if args is None and isinstance(state, dict):
+ return self.represent_mapping(
+ u'tag:yaml.org,2002:python/object:'+class_name, state)
+ if isinstance(state, dict) and not state:
+ return self.represent_sequence(
+ u'tag:yaml.org,2002:python/object/new:'+class_name, args)
+ value = {}
+ if args:
+ value['args'] = args
+ value['state'] = state
+ return self.represent_mapping(
+ u'tag:yaml.org,2002:python/object/new:'+class_name, value)
+
+ def represent_object(self, data):
+ # We use __reduce__ API to save the data. data.__reduce__ returns
+ # a tuple of length 2-5:
+ # (function, args, state, listitems, dictitems)
+
+ # For reconstructing, we calls function(*args), then set its state,
+ # listitems, and dictitems if they are not None.
+
+ # A special case is when function.__name__ == '__newobj__'. In this
+ # case we create the object with args[0].__new__(*args).
+
+ # Another special case is when __reduce__ returns a string - we don't
+ # support it.
+
+ # We produce a !!python/object, !!python/object/new or
+ # !!python/object/apply node.
+
+ cls = type(data)
+ if cls in copy_reg.dispatch_table:
+ reduce = copy_reg.dispatch_table[cls](data)
+ elif hasattr(data, '__reduce_ex__'):
+ reduce = data.__reduce_ex__(2)
+ elif hasattr(data, '__reduce__'):
+ reduce = data.__reduce__()
+ else:
+ raise RepresenterError("cannot represent object: %r" % data)
+ reduce = (list(reduce)+[None]*5)[:5]
+ function, args, state, listitems, dictitems = reduce
+ args = list(args)
+ if state is None:
+ state = {}
+ if listitems is not None:
+ listitems = list(listitems)
+ if dictitems is not None:
+ dictitems = dict(dictitems)
+ if function.__name__ == '__newobj__':
+ function = args[0]
+ args = args[1:]
+ tag = u'tag:yaml.org,2002:python/object/new:'
+ newobj = True
+ else:
+ tag = u'tag:yaml.org,2002:python/object/apply:'
+ newobj = False
+ function_name = u'%s.%s' % (function.__module__, function.__name__)
+ if not args and not listitems and not dictitems \
+ and isinstance(state, dict) and newobj:
+ return self.represent_mapping(
+ u'tag:yaml.org,2002:python/object:'+function_name, state)
+ if not listitems and not dictitems \
+ and isinstance(state, dict) and not state:
+ return self.represent_sequence(tag+function_name, args)
+ value = {}
+ if args:
+ value['args'] = args
+ if state or not isinstance(state, dict):
+ value['state'] = state
+ if listitems:
+ value['listitems'] = listitems
+ if dictitems:
+ value['dictitems'] = dictitems
+ return self.represent_mapping(tag+function_name, value)
+
+Representer.add_representer(str,
+ Representer.represent_str)
+
+Representer.add_representer(unicode,
+ Representer.represent_unicode)
+
+Representer.add_representer(long,
+ Representer.represent_long)
+
+Representer.add_representer(complex,
+ Representer.represent_complex)
+
+Representer.add_representer(tuple,
+ Representer.represent_tuple)
+
+Representer.add_representer(type,
+ Representer.represent_name)
+
+Representer.add_representer(types.ClassType,
+ Representer.represent_name)
+
+Representer.add_representer(types.FunctionType,
+ Representer.represent_name)
+
+Representer.add_representer(types.BuiltinFunctionType,
+ Representer.represent_name)
+
+Representer.add_representer(types.ModuleType,
+ Representer.represent_module)
+
+Representer.add_multi_representer(types.InstanceType,
+ Representer.represent_instance)
+
+Representer.add_multi_representer(object,
+ Representer.represent_object)
+
diff --git a/pynxc/yaml/resolver.py b/pynxc/yaml/resolver.py
new file mode 100644
index 0000000..6b5ab87
--- /dev/null
+++ b/pynxc/yaml/resolver.py
@@ -0,0 +1,224 @@
+
+__all__ = ['BaseResolver', 'Resolver']
+
+from error import *
+from nodes import *
+
+import re
+
+class ResolverError(YAMLError):
+ pass
+
+class BaseResolver(object):
+
+ DEFAULT_SCALAR_TAG = u'tag:yaml.org,2002:str'
+ DEFAULT_SEQUENCE_TAG = u'tag:yaml.org,2002:seq'
+ DEFAULT_MAPPING_TAG = u'tag:yaml.org,2002:map'
+
+ yaml_implicit_resolvers = {}
+ yaml_path_resolvers = {}
+
+ def __init__(self):
+ self.resolver_exact_paths = []
+ self.resolver_prefix_paths = []
+
+ def add_implicit_resolver(cls, tag, regexp, first):
+ if not 'yaml_implicit_resolvers' in cls.__dict__:
+ cls.yaml_implicit_resolvers = cls.yaml_implicit_resolvers.copy()
+ if first is None:
+ first = [None]
+ for ch in first:
+ cls.yaml_implicit_resolvers.setdefault(ch, []).append((tag, regexp))
+ add_implicit_resolver = classmethod(add_implicit_resolver)
+
+ def add_path_resolver(cls, tag, path, kind=None):
+ # Note: `add_path_resolver` is experimental. The API could be changed.
+ # `new_path` is a pattern that is matched against the path from the
+ # root to the node that is being considered. `node_path` elements are
+ # tuples `(node_check, index_check)`. `node_check` is a node class:
+ # `ScalarNode`, `SequenceNode`, `MappingNode` or `None`. `None`
+ # matches any kind of a node. `index_check` could be `None`, a boolean
+ # value, a string value, or a number. `None` and `False` match against
+ # any _value_ of sequence and mapping nodes. `True` matches against
+ # any _key_ of a mapping node. A string `index_check` matches against
+ # a mapping value that corresponds to a scalar key which content is
+ # equal to the `index_check` value. An integer `index_check` matches
+ # against a sequence value with the index equal to `index_check`.
+ if not 'yaml_path_resolvers' in cls.__dict__:
+ cls.yaml_path_resolvers = cls.yaml_path_resolvers.copy()
+ new_path = []
+ for element in path:
+ if isinstance(element, (list, tuple)):
+ if len(element) == 2:
+ node_check, index_check = element
+ elif len(element) == 1:
+ node_check = element[0]
+ index_check = True
+ else:
+ raise ResolverError("Invalid path element: %s" % element)
+ else:
+ node_check = None
+ index_check = element
+ if node_check is str:
+ node_check = ScalarNode
+ elif node_check is list:
+ node_check = SequenceNode
+ elif node_check is dict:
+ node_check = MappingNode
+ elif node_check not in [ScalarNode, SequenceNode, MappingNode] \
+ and not isinstance(node_check, basestring) \
+ and node_check is not None:
+ raise ResolverError("Invalid node checker: %s" % node_check)
+ if not isinstance(index_check, (basestring, int)) \
+ and index_check is not None:
+ raise ResolverError("Invalid index checker: %s" % index_check)
+ new_path.append((node_check, index_check))
+ if kind is str:
+ kind = ScalarNode
+ elif kind is list:
+ kind = SequenceNode
+ elif kind is dict:
+ kind = MappingNode
+ elif kind not in [ScalarNode, SequenceNode, MappingNode] \
+ and kind is not None:
+ raise ResolverError("Invalid node kind: %s" % kind)
+ cls.yaml_path_resolvers[tuple(new_path), kind] = tag
+ add_path_resolver = classmethod(add_path_resolver)
+
+ def descend_resolver(self, current_node, current_index):
+ if not self.yaml_path_resolvers:
+ return
+ exact_paths = {}
+ prefix_paths = []
+ if current_node:
+ depth = len(self.resolver_prefix_paths)
+ for path, kind in self.resolver_prefix_paths[-1]:
+ if self.check_resolver_prefix(depth, path, kind,
+ current_node, current_index):
+ if len(path) > depth:
+ prefix_paths.append((path, kind))
+ else:
+ exact_paths[kind] = self.yaml_path_resolvers[path, kind]
+ else:
+ for path, kind in self.yaml_path_resolvers:
+ if not path:
+ exact_paths[kind] = self.yaml_path_resolvers[path, kind]
+ else:
+ prefix_paths.append((path, kind))
+ self.resolver_exact_paths.append(exact_paths)
+ self.resolver_prefix_paths.append(prefix_paths)
+
+ def ascend_resolver(self):
+ if not self.yaml_path_resolvers:
+ return
+ self.resolver_exact_paths.pop()
+ self.resolver_prefix_paths.pop()
+
+ def check_resolver_prefix(self, depth, path, kind,
+ current_node, current_index):
+ node_check, index_check = path[depth-1]
+ if isinstance(node_check, basestring):
+ if current_node.tag != node_check:
+ return
+ elif node_check is not None:
+ if not isinstance(current_node, node_check):
+ return
+ if index_check is True and current_index is not None:
+ return
+ if (index_check is False or index_check is None) \
+ and current_index is None:
+ return
+ if isinstance(index_check, basestring):
+ if not (isinstance(current_index, ScalarNode)
+ and index_check == current_index.value):
+ return
+ elif isinstance(index_check, int) and not isinstance(index_check, bool):
+ if index_check != current_index:
+ return
+ return True
+
+ def resolve(self, kind, value, implicit):
+ if kind is ScalarNode and implicit[0]:
+ if value == u'':
+ resolvers = self.yaml_implicit_resolvers.get(u'', [])
+ else:
+ resolvers = self.yaml_implicit_resolvers.get(value[0], [])
+ resolvers += self.yaml_implicit_resolvers.get(None, [])
+ for tag, regexp in resolvers:
+ if regexp.match(value):
+ return tag
+ implicit = implicit[1]
+ if self.yaml_path_resolvers:
+ exact_paths = self.resolver_exact_paths[-1]
+ if kind in exact_paths:
+ return exact_paths[kind]
+ if None in exact_paths:
+ return exact_paths[None]
+ if kind is ScalarNode:
+ return self.DEFAULT_SCALAR_TAG
+ elif kind is SequenceNode:
+ return self.DEFAULT_SEQUENCE_TAG
+ elif kind is MappingNode:
+ return self.DEFAULT_MAPPING_TAG
+
+class Resolver(BaseResolver):
+ pass
+
+Resolver.add_implicit_resolver(
+ u'tag:yaml.org,2002:bool',
+ re.compile(ur'''^(?:yes|Yes|YES|no|No|NO
+ |true|True|TRUE|false|False|FALSE
+ |on|On|ON|off|Off|OFF)$''', re.X),
+ list(u'yYnNtTfFoO'))
+
+Resolver.add_implicit_resolver(
+ u'tag:yaml.org,2002:float',
+ re.compile(ur'''^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+][0-9]+)?
+ |\.[0-9_]+(?:[eE][-+][0-9]+)?
+ |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*
+ |[-+]?\.(?:inf|Inf|INF)
+ |\.(?:nan|NaN|NAN))$''', re.X),
+ list(u'-+0123456789.'))
+
+Resolver.add_implicit_resolver(
+ u'tag:yaml.org,2002:int',
+ re.compile(ur'''^(?:[-+]?0b[0-1_]+
+ |[-+]?0[0-7_]+
+ |[-+]?(?:0|[1-9][0-9_]*)
+ |[-+]?0x[0-9a-fA-F_]+
+ |[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$''', re.X),
+ list(u'-+0123456789'))
+
+Resolver.add_implicit_resolver(
+ u'tag:yaml.org,2002:merge',
+ re.compile(ur'^(?:<<)$'),
+ [u'<'])
+
+Resolver.add_implicit_resolver(
+ u'tag:yaml.org,2002:null',
+ re.compile(ur'''^(?: ~
+ |null|Null|NULL
+ | )$''', re.X),
+ [u'~', u'n', u'N', u''])
+
+Resolver.add_implicit_resolver(
+ u'tag:yaml.org,2002:timestamp',
+ re.compile(ur'''^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
+ |[0-9][0-9][0-9][0-9] -[0-9][0-9]? -[0-9][0-9]?
+ (?:[Tt]|[ \t]+)[0-9][0-9]?
+ :[0-9][0-9] :[0-9][0-9] (?:\.[0-9]*)?
+ (?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$''', re.X),
+ list(u'0123456789'))
+
+Resolver.add_implicit_resolver(
+ u'tag:yaml.org,2002:value',
+ re.compile(ur'^(?:=)$'),
+ [u'='])
+
+# The following resolver is only for documentation purposes. It cannot work
+# because plain scalars cannot start with '!', '&', or '*'.
+Resolver.add_implicit_resolver(
+ u'tag:yaml.org,2002:yaml',
+ re.compile(ur'^(?:!|&|\*)$'),
+ list(u'!&*'))
+
diff --git a/pynxc/yaml/scanner.py b/pynxc/yaml/scanner.py
new file mode 100644
index 0000000..5228fad
--- /dev/null
+++ b/pynxc/yaml/scanner.py
@@ -0,0 +1,1457 @@
+
+# Scanner produces tokens of the following types:
+# STREAM-START
+# STREAM-END
+# DIRECTIVE(name, value)
+# DOCUMENT-START
+# DOCUMENT-END
+# BLOCK-SEQUENCE-START
+# BLOCK-MAPPING-START
+# BLOCK-END
+# FLOW-SEQUENCE-START
+# FLOW-MAPPING-START
+# FLOW-SEQUENCE-END
+# FLOW-MAPPING-END
+# BLOCK-ENTRY
+# FLOW-ENTRY
+# KEY
+# VALUE
+# ALIAS(value)
+# ANCHOR(value)
+# TAG(value)
+# SCALAR(value, plain, style)
+#
+# Read comments in the Scanner code for more details.
+#
+
+__all__ = ['Scanner', 'ScannerError']
+
+from error import MarkedYAMLError
+from tokens import *
+
+class ScannerError(MarkedYAMLError):
+ pass
+
+class SimpleKey(object):
+ # See below simple keys treatment.
+
+ def __init__(self, token_number, required, index, line, column, mark):
+ self.token_number = token_number
+ self.required = required
+ self.index = index
+ self.line = line
+ self.column = column
+ self.mark = mark
+
+class Scanner(object):
+
+ def __init__(self):
+ """Initialize the scanner."""
+ # It is assumed that Scanner and Reader will have a common descendant.
+ # Reader do the dirty work of checking for BOM and converting the
+ # input data to Unicode. It also adds NUL to the end.
+ #
+ # Reader supports the following methods
+ # self.peek(i=0) # peek the next i-th character
+ # self.prefix(l=1) # peek the next l characters
+ # self.forward(l=1) # read the next l characters and move the pointer.
+
+ # Had we reached the end of the stream?
+ self.done = False
+
+ # The number of unclosed '{' and '['. `flow_level == 0` means block
+ # context.
+ self.flow_level = 0
+
+ # List of processed tokens that are not yet emitted.
+ self.tokens = []
+
+ # Add the STREAM-START token.
+ self.fetch_stream_start()
+
+ # Number of tokens that were emitted through the `get_token` method.
+ self.tokens_taken = 0
+
+ # The current indentation level.
+ self.indent = -1
+
+ # Past indentation levels.
+ self.indents = []
+
+ # Variables related to simple keys treatment.
+
+ # A simple key is a key that is not denoted by the '?' indicator.
+ # Example of simple keys:
+ # ---
+ # block simple key: value
+ # ? not a simple key:
+ # : { flow simple key: value }
+ # We emit the KEY token before all keys, so when we find a potential
+ # simple key, we try to locate the corresponding ':' indicator.
+ # Simple keys should be limited to a single line and 1024 characters.
+
+ # Can a simple key start at the current position? A simple key may
+ # start:
+ # - at the beginning of the line, not counting indentation spaces
+ # (in block context),
+ # - after '{', '[', ',' (in the flow context),
+ # - after '?', ':', '-' (in the block context).
+ # In the block context, this flag also signifies if a block collection
+ # may start at the current position.
+ self.allow_simple_key = True
+
+ # Keep track of possible simple keys. This is a dictionary. The key
+ # is `flow_level`; there can be no more that one possible simple key
+ # for each level. The value is a SimpleKey record:
+ # (token_number, required, index, line, column, mark)
+ # A simple key may start with ALIAS, ANCHOR, TAG, SCALAR(flow),
+ # '[', or '{' tokens.
+ self.possible_simple_keys = {}
+
+ # Public methods.
+
+ def check_token(self, *choices):
+ # Check if the next token is one of the given types.
+ while self.need_more_tokens():
+ self.fetch_more_tokens()
+ if self.tokens:
+ if not choices:
+ return True
+ for choice in choices:
+ if isinstance(self.tokens[0], choice):
+ return True
+ return False
+
+ def peek_token(self):
+ # Return the next token, but do not delete if from the queue.
+ while self.need_more_tokens():
+ self.fetch_more_tokens()
+ if self.tokens:
+ return self.tokens[0]
+
+ def get_token(self):
+ # Return the next token.
+ while self.need_more_tokens():
+ self.fetch_more_tokens()
+ if self.tokens:
+ self.tokens_taken += 1
+ return self.tokens.pop(0)
+
+ # Private methods.
+
+ def need_more_tokens(self):
+ if self.done:
+ return False
+ if not self.tokens:
+ return True
+ # The current token may be a potential simple key, so we
+ # need to look further.
+ self.stale_possible_simple_keys()
+ if self.next_possible_simple_key() == self.tokens_taken:
+ return True
+
+ def fetch_more_tokens(self):
+
+ # Eat whitespaces and comments until we reach the next token.
+ self.scan_to_next_token()
+
+ # Remove obsolete possible simple keys.
+ self.stale_possible_simple_keys()
+
+ # Compare the current indentation and column. It may add some tokens
+ # and decrease the current indentation level.
+ self.unwind_indent(self.column)
+
+ # Peek the next character.
+ ch = self.peek()
+
+ # Is it the end of stream?
+ if ch == u'\0':
+ return self.fetch_stream_end()
+
+ # Is it a directive?
+ if ch == u'%' and self.check_directive():
+ return self.fetch_directive()
+
+ # Is it the document start?
+ if ch == u'-' and self.check_document_start():
+ return self.fetch_document_start()
+
+ # Is it the document end?
+ if ch == u'.' and self.check_document_end():
+ return self.fetch_document_end()
+
+ # TODO: support for BOM within a stream.
+ #if ch == u'\uFEFF':
+ # return self.fetch_bom() <-- issue BOMToken
+
+ # Note: the order of the following checks is NOT significant.
+
+ # Is it the flow sequence start indicator?
+ if ch == u'[':
+ return self.fetch_flow_sequence_start()
+
+ # Is it the flow mapping start indicator?
+ if ch == u'{':
+ return self.fetch_flow_mapping_start()
+
+ # Is it the flow sequence end indicator?
+ if ch == u']':
+ return self.fetch_flow_sequence_end()
+
+ # Is it the flow mapping end indicator?
+ if ch == u'}':
+ return self.fetch_flow_mapping_end()
+
+ # Is it the flow entry indicator?
+ if ch == u',':
+ return self.fetch_flow_entry()
+
+ # Is it the block entry indicator?
+ if ch == u'-' and self.check_block_entry():
+ return self.fetch_block_entry()
+
+ # Is it the key indicator?
+ if ch == u'?' and self.check_key():
+ return self.fetch_key()
+
+ # Is it the value indicator?
+ if ch == u':' and self.check_value():
+ return self.fetch_value()
+
+ # Is it an alias?
+ if ch == u'*':
+ return self.fetch_alias()
+
+ # Is it an anchor?
+ if ch == u'&':
+ return self.fetch_anchor()
+
+ # Is it a tag?
+ if ch == u'!':
+ return self.fetch_tag()
+
+ # Is it a literal scalar?
+ if ch == u'|' and not self.flow_level:
+ return self.fetch_literal()
+
+ # Is it a folded scalar?
+ if ch == u'>' and not self.flow_level:
+ return self.fetch_folded()
+
+ # Is it a single quoted scalar?
+ if ch == u'\'':
+ return self.fetch_single()
+
+ # Is it a double quoted scalar?
+ if ch == u'\"':
+ return self.fetch_double()
+
+ # It must be a plain scalar then.
+ if self.check_plain():
+ return self.fetch_plain()
+
+ # No? It's an error. Let's produce a nice error message.
+ raise ScannerError("while scanning for the next token", None,
+ "found character %r that cannot start any token"
+ % ch.encode('utf-8'), self.get_mark())
+
+ # Simple keys treatment.
+
+ def next_possible_simple_key(self):
+ # Return the number of the nearest possible simple key. Actually we
+ # don't need to loop through the whole dictionary. We may replace it
+ # with the following code:
+ # if not self.possible_simple_keys:
+ # return None
+ # return self.possible_simple_keys[
+ # min(self.possible_simple_keys.keys())].token_number
+ min_token_number = None
+ for level in self.possible_simple_keys:
+ key = self.possible_simple_keys[level]
+ if min_token_number is None or key.token_number < min_token_number:
+ min_token_number = key.token_number
+ return min_token_number
+
+ def stale_possible_simple_keys(self):
+ # Remove entries that are no longer possible simple keys. According to
+ # the YAML specification, simple keys
+ # - should be limited to a single line,
+ # - should be no longer than 1024 characters.
+ # Disabling this procedure will allow simple keys of any length and
+ # height (may cause problems if indentation is broken though).
+ for level in self.possible_simple_keys.keys():
+ key = self.possible_simple_keys[level]
+ if key.line != self.line \
+ or self.index-key.index > 1024:
+ if key.required:
+ raise ScannerError("while scanning a simple key", key.mark,
+ "could not found expected ':'", self.get_mark())
+ del self.possible_simple_keys[level]
+
+ def save_possible_simple_key(self):
+ # The next token may start a simple key. We check if it's possible
+ # and save its position. This function is called for
+ # ALIAS, ANCHOR, TAG, SCALAR(flow), '[', and '{'.
+
+ # Check if a simple key is required at the current position.
+ required = not self.flow_level and self.indent == self.column
+
+ # A simple key is required only if it is the first token in the current
+ # line. Therefore it is always allowed.
+ assert self.allow_simple_key or not required
+
+ # The next token might be a simple key. Let's save it's number and
+ # position.
+ if self.allow_simple_key:
+ self.remove_possible_simple_key()
+ token_number = self.tokens_taken+len(self.tokens)
+ key = SimpleKey(token_number, required,
+ self.index, self.line, self.column, self.get_mark())
+ self.possible_simple_keys[self.flow_level] = key
+
+ def remove_possible_simple_key(self):
+ # Remove the saved possible key position at the current flow level.
+ if self.flow_level in self.possible_simple_keys:
+ key = self.possible_simple_keys[self.flow_level]
+
+ if key.required:
+ raise ScannerError("while scanning a simple key", key.mark,
+ "could not found expected ':'", self.get_mark())
+
+ del self.possible_simple_keys[self.flow_level]
+
+ # Indentation functions.
+
+ def unwind_indent(self, column):
+
+ ## In flow context, tokens should respect indentation.
+ ## Actually the condition should be `self.indent >= column` according to
+ ## the spec. But this condition will prohibit intuitively correct
+ ## constructions such as
+ ## key : {
+ ## }
+ #if self.flow_level and self.indent > column:
+ # raise ScannerError(None, None,
+ # "invalid intendation or unclosed '[' or '{'",
+ # self.get_mark())
+
+ # In the flow context, indentation is ignored. We make the scanner less
+ # restrictive then specification requires.
+ if self.flow_level:
+ return
+
+ # In block context, we may need to issue the BLOCK-END tokens.
+ while self.indent > column:
+ mark = self.get_mark()
+ self.indent = self.indents.pop()
+ self.tokens.append(BlockEndToken(mark, mark))
+
+ def add_indent(self, column):
+ # Check if we need to increase indentation.
+ if self.indent < column:
+ self.indents.append(self.indent)
+ self.indent = column
+ return True
+ return False
+
+ # Fetchers.
+
+ def fetch_stream_start(self):
+ # We always add STREAM-START as the first token and STREAM-END as the
+ # last token.
+
+ # Read the token.
+ mark = self.get_mark()
+
+ # Add STREAM-START.
+ self.tokens.append(StreamStartToken(mark, mark,
+ encoding=self.encoding))
+
+
+ def fetch_stream_end(self):
+
+ # Set the current intendation to -1.
+ self.unwind_indent(-1)
+
+ # Reset simple keys.
+ self.remove_possible_simple_key()
+ self.allow_simple_key = False
+ self.possible_simple_keys = {}
+
+ # Read the token.
+ mark = self.get_mark()
+
+ # Add STREAM-END.
+ self.tokens.append(StreamEndToken(mark, mark))
+
+ # The steam is finished.
+ self.done = True
+
+ def fetch_directive(self):
+
+ # Set the current intendation to -1.
+ self.unwind_indent(-1)
+
+ # Reset simple keys.
+ self.remove_possible_simple_key()
+ self.allow_simple_key = False
+
+ # Scan and add DIRECTIVE.
+ self.tokens.append(self.scan_directive())
+
+ def fetch_document_start(self):
+ self.fetch_document_indicator(DocumentStartToken)
+
+ def fetch_document_end(self):
+ self.fetch_document_indicator(DocumentEndToken)
+
+ def fetch_document_indicator(self, TokenClass):
+
+ # Set the current intendation to -1.
+ self.unwind_indent(-1)
+
+ # Reset simple keys. Note that there could not be a block collection
+ # after '---'.
+ self.remove_possible_simple_key()
+ self.allow_simple_key = False
+
+ # Add DOCUMENT-START or DOCUMENT-END.
+ start_mark = self.get_mark()
+ self.forward(3)
+ end_mark = self.get_mark()
+ self.tokens.append(TokenClass(start_mark, end_mark))
+
+ def fetch_flow_sequence_start(self):
+ self.fetch_flow_collection_start(FlowSequenceStartToken)
+
+ def fetch_flow_mapping_start(self):
+ self.fetch_flow_collection_start(FlowMappingStartToken)
+
+ def fetch_flow_collection_start(self, TokenClass):
+
+ # '[' and '{' may start a simple key.
+ self.save_possible_simple_key()
+
+ # Increase the flow level.
+ self.flow_level += 1
+
+ # Simple keys are allowed after '[' and '{'.
+ self.allow_simple_key = True
+
+ # Add FLOW-SEQUENCE-START or FLOW-MAPPING-START.
+ start_mark = self.get_mark()
+ self.forward()
+ end_mark = self.get_mark()
+ self.tokens.append(TokenClass(start_mark, end_mark))
+
+ def fetch_flow_sequence_end(self):
+ self.fetch_flow_collection_end(FlowSequenceEndToken)
+
+ def fetch_flow_mapping_end(self):
+ self.fetch_flow_collection_end(FlowMappingEndToken)
+
+ def fetch_flow_collection_end(self, TokenClass):
+
+ # Reset possible simple key on the current level.
+ self.remove_possible_simple_key()
+
+ # Decrease the flow level.
+ self.flow_level -= 1
+
+ # No simple keys after ']' or '}'.
+ self.allow_simple_key = False
+
+ # Add FLOW-SEQUENCE-END or FLOW-MAPPING-END.
+ start_mark = self.get_mark()
+ self.forward()
+ end_mark = self.get_mark()
+ self.tokens.append(TokenClass(start_mark, end_mark))
+
+ def fetch_flow_entry(self):
+
+ # Simple keys are allowed after ','.
+ self.allow_simple_key = True
+
+ # Reset possible simple key on the current level.
+ self.remove_possible_simple_key()
+
+ # Add FLOW-ENTRY.
+ start_mark = self.get_mark()
+ self.forward()
+ end_mark = self.get_mark()
+ self.tokens.append(FlowEntryToken(start_mark, end_mark))
+
+ def fetch_block_entry(self):
+
+ # Block context needs additional checks.
+ if not self.flow_level:
+
+ # Are we allowed to start a new entry?
+ if not self.allow_simple_key:
+ raise ScannerError(None, None,
+ "sequence entries are not allowed here",
+ self.get_mark())
+
+ # We may need to add BLOCK-SEQUENCE-START.
+ if self.add_indent(self.column):
+ mark = self.get_mark()
+ self.tokens.append(BlockSequenceStartToken(mark, mark))
+
+ # It's an error for the block entry to occur in the flow context,
+ # but we let the parser detect this.
+ else:
+ pass
+
+ # Simple keys are allowed after '-'.
+ self.allow_simple_key = True
+
+ # Reset possible simple key on the current level.
+ self.remove_possible_simple_key()
+
+ # Add BLOCK-ENTRY.
+ start_mark = self.get_mark()
+ self.forward()
+ end_mark = self.get_mark()
+ self.tokens.append(BlockEntryToken(start_mark, end_mark))
+
+ def fetch_key(self):
+
+ # Block context needs additional checks.
+ if not self.flow_level:
+
+ # Are we allowed to start a key (not nessesary a simple)?
+ if not self.allow_simple_key:
+ raise ScannerError(None, None,
+ "mapping keys are not allowed here",
+ self.get_mark())
+
+ # We may need to add BLOCK-MAPPING-START.
+ if self.add_indent(self.column):
+ mark = self.get_mark()
+ self.tokens.append(BlockMappingStartToken(mark, mark))
+
+ # Simple keys are allowed after '?' in the block context.
+ self.allow_simple_key = not self.flow_level
+
+ # Reset possible simple key on the current level.
+ self.remove_possible_simple_key()
+
+ # Add KEY.
+ start_mark = self.get_mark()
+ self.forward()
+ end_mark = self.get_mark()
+ self.tokens.append(KeyToken(start_mark, end_mark))
+
+ def fetch_value(self):
+
+ # Do we determine a simple key?
+ if self.flow_level in self.possible_simple_keys:
+
+ # Add KEY.
+ key = self.possible_simple_keys[self.flow_level]
+ del self.possible_simple_keys[self.flow_level]
+ self.tokens.insert(key.token_number-self.tokens_taken,
+ KeyToken(key.mark, key.mark))
+
+ # If this key starts a new block mapping, we need to add
+ # BLOCK-MAPPING-START.
+ if not self.flow_level:
+ if self.add_indent(key.column):
+ self.tokens.insert(key.token_number-self.tokens_taken,
+ BlockMappingStartToken(key.mark, key.mark))
+
+ # There cannot be two simple keys one after another.
+ self.allow_simple_key = False
+
+ # It must be a part of a complex key.
+ else:
+
+ # Block context needs additional checks.
+ # (Do we really need them? They will be catched by the parser
+ # anyway.)
+ if not self.flow_level:
+
+ # We are allowed to start a complex value if and only if
+ # we can start a simple key.
+ if not self.allow_simple_key:
+ raise ScannerError(None, None,
+ "mapping values are not allowed here",
+ self.get_mark())
+
+ # If this value starts a new block mapping, we need to add
+ # BLOCK-MAPPING-START. It will be detected as an error later by
+ # the parser.
+ if not self.flow_level:
+ if self.add_indent(self.column):
+ mark = self.get_mark()
+ self.tokens.append(BlockMappingStartToken(mark, mark))
+
+ # Simple keys are allowed after ':' in the block context.
+ self.allow_simple_key = not self.flow_level
+
+ # Reset possible simple key on the current level.
+ self.remove_possible_simple_key()
+
+ # Add VALUE.
+ start_mark = self.get_mark()
+ self.forward()
+ end_mark = self.get_mark()
+ self.tokens.append(ValueToken(start_mark, end_mark))
+
+ def fetch_alias(self):
+
+ # ALIAS could be a simple key.
+ self.save_possible_simple_key()
+
+ # No simple keys after ALIAS.
+ self.allow_simple_key = False
+
+ # Scan and add ALIAS.
+ self.tokens.append(self.scan_anchor(AliasToken))
+
+ def fetch_anchor(self):
+
+ # ANCHOR could start a simple key.
+ self.save_possible_simple_key()
+
+ # No simple keys after ANCHOR.
+ self.allow_simple_key = False
+
+ # Scan and add ANCHOR.
+ self.tokens.append(self.scan_anchor(AnchorToken))
+
+ def fetch_tag(self):
+
+ # TAG could start a simple key.
+ self.save_possible_simple_key()
+
+ # No simple keys after TAG.
+ self.allow_simple_key = False
+
+ # Scan and add TAG.
+ self.tokens.append(self.scan_tag())
+
+ def fetch_literal(self):
+ self.fetch_block_scalar(style='|')
+
+ def fetch_folded(self):
+ self.fetch_block_scalar(style='>')
+
+ def fetch_block_scalar(self, style):
+
+ # A simple key may follow a block scalar.
+ self.allow_simple_key = True
+
+ # Reset possible simple key on the current level.
+ self.remove_possible_simple_key()
+
+ # Scan and add SCALAR.
+ self.tokens.append(self.scan_block_scalar(style))
+
+ def fetch_single(self):
+ self.fetch_flow_scalar(style='\'')
+
+ def fetch_double(self):
+ self.fetch_flow_scalar(style='"')
+
+ def fetch_flow_scalar(self, style):
+
+ # A flow scalar could be a simple key.
+ self.save_possible_simple_key()
+
+ # No simple keys after flow scalars.
+ self.allow_simple_key = False
+
+ # Scan and add SCALAR.
+ self.tokens.append(self.scan_flow_scalar(style))
+
+ def fetch_plain(self):
+
+ # A plain scalar could be a simple key.
+ self.save_possible_simple_key()
+
+ # No simple keys after plain scalars. But note that `scan_plain` will
+ # change this flag if the scan is finished at the beginning of the
+ # line.
+ self.allow_simple_key = False
+
+ # Scan and add SCALAR. May change `allow_simple_key`.
+ self.tokens.append(self.scan_plain())
+
+ # Checkers.
+
+ def check_directive(self):
+
+ # DIRECTIVE: ^ '%' ...
+ # The '%' indicator is already checked.
+ if self.column == 0:
+ return True
+
+ def check_document_start(self):
+
+ # DOCUMENT-START: ^ '---' (' '|'\n')
+ if self.column == 0:
+ if self.prefix(3) == u'---' \
+ and self.peek(3) in u'\0 \t\r\n\x85\u2028\u2029':
+ return True
+
+ def check_document_end(self):
+
+ # DOCUMENT-END: ^ '...' (' '|'\n')
+ if self.column == 0:
+ if self.prefix(3) == u'...' \
+ and self.peek(3) in u'\0 \t\r\n\x85\u2028\u2029':
+ return True
+
+ def check_block_entry(self):
+
+ # BLOCK-ENTRY: '-' (' '|'\n')
+ return self.peek(1) in u'\0 \t\r\n\x85\u2028\u2029'
+
+ def check_key(self):
+
+ # KEY(flow context): '?'
+ if self.flow_level:
+ return True
+
+ # KEY(block context): '?' (' '|'\n')
+ else:
+ return self.peek(1) in u'\0 \t\r\n\x85\u2028\u2029'
+
+ def check_value(self):
+
+ # VALUE(flow context): ':'
+ if self.flow_level:
+ return True
+
+ # VALUE(block context): ':' (' '|'\n')
+ else:
+ return self.peek(1) in u'\0 \t\r\n\x85\u2028\u2029'
+
+ def check_plain(self):
+
+ # A plain scalar may start with any non-space character except:
+ # '-', '?', ':', ',', '[', ']', '{', '}',
+ # '#', '&', '*', '!', '|', '>', '\'', '\"',
+ # '%', '@', '`'.
+ #
+ # It may also start with
+ # '-', '?', ':'
+ # if it is followed by a non-space character.
+ #
+ # Note that we limit the last rule to the block context (except the
+ # '-' character) because we want the flow context to be space
+ # independent.
+ ch = self.peek()
+ return ch not in u'\0 \t\r\n\x85\u2028\u2029-?:,[]{}#&*!|>\'\"%@`' \
+ or (self.peek(1) not in u'\0 \t\r\n\x85\u2028\u2029'
+ and (ch == u'-' or (not self.flow_level and ch in u'?:')))
+
+ # Scanners.
+
+ def scan_to_next_token(self):
+ # We ignore spaces, line breaks and comments.
+ # If we find a line break in the block context, we set the flag
+ # `allow_simple_key` on.
+ # The byte order mark is stripped if it's the first character in the
+ # stream. We do not yet support BOM inside the stream as the
+ # specification requires. Any such mark will be considered as a part
+ # of the document.
+ #
+ # TODO: We need to make tab handling rules more sane. A good rule is
+ # Tabs cannot precede tokens
+ # BLOCK-SEQUENCE-START, BLOCK-MAPPING-START, BLOCK-END,
+ # KEY(block), VALUE(block), BLOCK-ENTRY
+ # So the checking code is
+ # if <TAB>:
+ # self.allow_simple_keys = False
+ # We also need to add the check for `allow_simple_keys == True` to
+ # `unwind_indent` before issuing BLOCK-END.
+ # Scanners for block, flow, and plain scalars need to be modified.
+
+ if self.index == 0 and self.peek() == u'\uFEFF':
+ self.forward()
+ found = False
+ while not found:
+ while self.peek() == u' ':
+ self.forward()
+ if self.peek() == u'#':
+ while self.peek() not in u'\0\r\n\x85\u2028\u2029':
+ self.forward()
+ if self.scan_line_break():
+ if not self.flow_level:
+ self.allow_simple_key = True
+ else:
+ found = True
+
+ def scan_directive(self):
+ # See the specification for details.
+ start_mark = self.get_mark()
+ self.forward()
+ name = self.scan_directive_name(start_mark)
+ value = None
+ if name == u'YAML':
+ value = self.scan_yaml_directive_value(start_mark)
+ end_mark = self.get_mark()
+ elif name == u'TAG':
+ value = self.scan_tag_directive_value(start_mark)
+ end_mark = self.get_mark()
+ else:
+ end_mark = self.get_mark()
+ while self.peek() not in u'\0\r\n\x85\u2028\u2029':
+ self.forward()
+ self.scan_directive_ignored_line(start_mark)
+ return DirectiveToken(name, value, start_mark, end_mark)
+
+ def scan_directive_name(self, start_mark):
+ # See the specification for details.
+ length = 0
+ ch = self.peek(length)
+ while u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \
+ or ch in u'-_':
+ length += 1
+ ch = self.peek(length)
+ if not length:
+ raise ScannerError("while scanning a directive", start_mark,
+ "expected alphabetic or numeric character, but found %r"
+ % ch.encode('utf-8'), self.get_mark())
+ value = self.prefix(length)
+ self.forward(length)
+ ch = self.peek()
+ if ch not in u'\0 \r\n\x85\u2028\u2029':
+ raise ScannerError("while scanning a directive", start_mark,
+ "expected alphabetic or numeric character, but found %r"
+ % ch.encode('utf-8'), self.get_mark())
+ return value
+
+ def scan_yaml_directive_value(self, start_mark):
+ # See the specification for details.
+ while self.peek() == u' ':
+ self.forward()
+ major = self.scan_yaml_directive_number(start_mark)
+ if self.peek() != '.':
+ raise ScannerError("while scanning a directive", start_mark,
+ "expected a digit or '.', but found %r"
+ % self.peek().encode('utf-8'),
+ self.get_mark())
+ self.forward()
+ minor = self.scan_yaml_directive_number(start_mark)
+ if self.peek() not in u'\0 \r\n\x85\u2028\u2029':
+ raise ScannerError("while scanning a directive", start_mark,
+ "expected a digit or ' ', but found %r"
+ % self.peek().encode('utf-8'),
+ self.get_mark())
+ return (major, minor)
+
+ def scan_yaml_directive_number(self, start_mark):
+ # See the specification for details.
+ ch = self.peek()
+ if not (u'0' <= ch <= u'9'):
+ raise ScannerError("while scanning a directive", start_mark,
+ "expected a digit, but found %r" % ch.encode('utf-8'),
+ self.get_mark())
+ length = 0
+ while u'0' <= self.peek(length) <= u'9':
+ length += 1
+ value = int(self.prefix(length))
+ self.forward(length)
+ return value
+
+ def scan_tag_directive_value(self, start_mark):
+ # See the specification for details.
+ while self.peek() == u' ':
+ self.forward()
+ handle = self.scan_tag_directive_handle(start_mark)
+ while self.peek() == u' ':
+ self.forward()
+ prefix = self.scan_tag_directive_prefix(start_mark)
+ return (handle, prefix)
+
+ def scan_tag_directive_handle(self, start_mark):
+ # See the specification for details.
+ value = self.scan_tag_handle('directive', start_mark)
+ ch = self.peek()
+ if ch != u' ':
+ raise ScannerError("while scanning a directive", start_mark,
+ "expected ' ', but found %r" % ch.encode('utf-8'),
+ self.get_mark())
+ return value
+
+ def scan_tag_directive_prefix(self, start_mark):
+ # See the specification for details.
+ value = self.scan_tag_uri('directive', start_mark)
+ ch = self.peek()
+ if ch not in u'\0 \r\n\x85\u2028\u2029':
+ raise ScannerError("while scanning a directive", start_mark,
+ "expected ' ', but found %r" % ch.encode('utf-8'),
+ self.get_mark())
+ return value
+
+ def scan_directive_ignored_line(self, start_mark):
+ # See the specification for details.
+ while self.peek() == u' ':
+ self.forward()
+ if self.peek() == u'#':
+ while self.peek() not in u'\0\r\n\x85\u2028\u2029':
+ self.forward()
+ ch = self.peek()
+ if ch not in u'\0\r\n\x85\u2028\u2029':
+ raise ScannerError("while scanning a directive", start_mark,
+ "expected a comment or a line break, but found %r"
+ % ch.encode('utf-8'), self.get_mark())
+ self.scan_line_break()
+
+ def scan_anchor(self, TokenClass):
+ # The specification does not restrict characters for anchors and
+ # aliases. This may lead to problems, for instance, the document:
+ # [ *alias, value ]
+ # can be interpteted in two ways, as
+ # [ "value" ]
+ # and
+ # [ *alias , "value" ]
+ # Therefore we restrict aliases to numbers and ASCII letters.
+ start_mark = self.get_mark()
+ indicator = self.peek()
+ if indicator == u'*':
+ name = 'alias'
+ else:
+ name = 'anchor'
+ self.forward()
+ length = 0
+ ch = self.peek(length)
+ while u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \
+ or ch in u'-_':
+ length += 1
+ ch = self.peek(length)
+ if not length:
+ raise ScannerError("while scanning an %s" % name, start_mark,
+ "expected alphabetic or numeric character, but found %r"
+ % ch.encode('utf-8'), self.get_mark())
+ value = self.prefix(length)
+ self.forward(length)
+ ch = self.peek()
+ if ch not in u'\0 \t\r\n\x85\u2028\u2029?:,]}%@`':
+ raise ScannerError("while scanning an %s" % name, start_mark,
+ "expected alphabetic or numeric character, but found %r"
+ % ch.encode('utf-8'), self.get_mark())
+ end_mark = self.get_mark()
+ return TokenClass(value, start_mark, end_mark)
+
+ def scan_tag(self):
+ # See the specification for details.
+ start_mark = self.get_mark()
+ ch = self.peek(1)
+ if ch == u'<':
+ handle = None
+ self.forward(2)
+ suffix = self.scan_tag_uri('tag', start_mark)
+ if self.peek() != u'>':
+ raise ScannerError("while parsing a tag", start_mark,
+ "expected '>', but found %r" % self.peek().encode('utf-8'),
+ self.get_mark())
+ self.forward()
+ elif ch in u'\0 \t\r\n\x85\u2028\u2029':
+ handle = None
+ suffix = u'!'
+ self.forward()
+ else:
+ length = 1
+ use_handle = False
+ while ch not in u'\0 \r\n\x85\u2028\u2029':
+ if ch == u'!':
+ use_handle = True
+ break
+ length += 1
+ ch = self.peek(length)
+ handle = u'!'
+ if use_handle:
+ handle = self.scan_tag_handle('tag', start_mark)
+ else:
+ handle = u'!'
+ self.forward()
+ suffix = self.scan_tag_uri('tag', start_mark)
+ ch = self.peek()
+ if ch not in u'\0 \r\n\x85\u2028\u2029':
+ raise ScannerError("while scanning a tag", start_mark,
+ "expected ' ', but found %r" % ch.encode('utf-8'),
+ self.get_mark())
+ value = (handle, suffix)
+ end_mark = self.get_mark()
+ return TagToken(value, start_mark, end_mark)
+
+ def scan_block_scalar(self, style):
+ # See the specification for details.
+
+ if style == '>':
+ folded = True
+ else:
+ folded = False
+
+ chunks = []
+ start_mark = self.get_mark()
+
+ # Scan the header.
+ self.forward()
+ chomping, increment = self.scan_block_scalar_indicators(start_mark)
+ self.scan_block_scalar_ignored_line(start_mark)
+
+ # Determine the indentation level and go to the first non-empty line.
+ min_indent = self.indent+1
+ if min_indent < 1:
+ min_indent = 1
+ if increment is None:
+ breaks, max_indent, end_mark = self.scan_block_scalar_indentation()
+ indent = max(min_indent, max_indent)
+ else:
+ indent = min_indent+increment-1
+ breaks, end_mark = self.scan_block_scalar_breaks(indent)
+ line_break = u''
+
+ # Scan the inner part of the block scalar.
+ while self.column == indent and self.peek() != u'\0':
+ chunks.extend(breaks)
+ leading_non_space = self.peek() not in u' \t'
+ length = 0
+ while self.peek(length) not in u'\0\r\n\x85\u2028\u2029':
+ length += 1
+ chunks.append(self.prefix(length))
+ self.forward(length)
+ line_break = self.scan_line_break()
+ breaks, end_mark = self.scan_block_scalar_breaks(indent)
+ if self.column == indent and self.peek() != u'\0':
+
+ # Unfortunately, folding rules are ambiguous.
+ #
+ # This is the folding according to the specification:
+
+ if folded and line_break == u'\n' \
+ and leading_non_space and self.peek() not in u' \t':
+ if not breaks:
+ chunks.append(u' ')
+ else:
+ chunks.append(line_break)
+
+ # This is Clark Evans's interpretation (also in the spec
+ # examples):
+ #
+ #if folded and line_break == u'\n':
+ # if not breaks:
+ # if self.peek() not in ' \t':
+ # chunks.append(u' ')
+ # else:
+ # chunks.append(line_break)
+ #else:
+ # chunks.append(line_break)
+ else:
+ break
+
+ # Chomp the tail.
+ if chomping is not False:
+ chunks.append(line_break)
+ if chomping is True:
+ chunks.extend(breaks)
+
+ # We are done.
+ return ScalarToken(u''.join(chunks), False, start_mark, end_mark,
+ style)
+
+ def scan_block_scalar_indicators(self, start_mark):
+ # See the specification for details.
+ chomping = None
+ increment = None
+ ch = self.peek()
+ if ch in u'+-':
+ if ch == '+':
+ chomping = True
+ else:
+ chomping = False
+ self.forward()
+ ch = self.peek()
+ if ch in u'0123456789':
+ increment = int(ch)
+ if increment == 0:
+ raise ScannerError("while scanning a block scalar", start_mark,
+ "expected indentation indicator in the range 1-9, but found 0",
+ self.get_mark())
+ self.forward()
+ elif ch in u'0123456789':
+ increment = int(ch)
+ if increment == 0:
+ raise ScannerError("while scanning a block scalar", start_mark,
+ "expected indentation indicator in the range 1-9, but found 0",
+ self.get_mark())
+ self.forward()
+ ch = self.peek()
+ if ch in u'+-':
+ if ch == '+':
+ chomping = True
+ else:
+ chomping = False
+ self.forward()
+ ch = self.peek()
+ if ch not in u'\0 \r\n\x85\u2028\u2029':
+ raise ScannerError("while scanning a block scalar", start_mark,
+ "expected chomping or indentation indicators, but found %r"
+ % ch.encode('utf-8'), self.get_mark())
+ return chomping, increment
+
+ def scan_block_scalar_ignored_line(self, start_mark):
+ # See the specification for details.
+ while self.peek() == u' ':
+ self.forward()
+ if self.peek() == u'#':
+ while self.peek() not in u'\0\r\n\x85\u2028\u2029':
+ self.forward()
+ ch = self.peek()
+ if ch not in u'\0\r\n\x85\u2028\u2029':
+ raise ScannerError("while scanning a block scalar", start_mark,
+ "expected a comment or a line break, but found %r"
+ % ch.encode('utf-8'), self.get_mark())
+ self.scan_line_break()
+
+ def scan_block_scalar_indentation(self):
+ # See the specification for details.
+ chunks = []
+ max_indent = 0
+ end_mark = self.get_mark()
+ while self.peek() in u' \r\n\x85\u2028\u2029':
+ if self.peek() != u' ':
+ chunks.append(self.scan_line_break())
+ end_mark = self.get_mark()
+ else:
+ self.forward()
+ if self.column > max_indent:
+ max_indent = self.column
+ return chunks, max_indent, end_mark
+
+ def scan_block_scalar_breaks(self, indent):
+ # See the specification for details.
+ chunks = []
+ end_mark = self.get_mark()
+ while self.column < indent and self.peek() == u' ':
+ self.forward()
+ while self.peek() in u'\r\n\x85\u2028\u2029':
+ chunks.append(self.scan_line_break())
+ end_mark = self.get_mark()
+ while self.column < indent and self.peek() == u' ':
+ self.forward()
+ return chunks, end_mark
+
+ def scan_flow_scalar(self, style):
+ # See the specification for details.
+ # Note that we loose indentation rules for quoted scalars. Quoted
+ # scalars don't need to adhere indentation because " and ' clearly
+ # mark the beginning and the end of them. Therefore we are less
+ # restrictive then the specification requires. We only need to check
+ # that document separators are not included in scalars.
+ if style == '"':
+ double = True
+ else:
+ double = False
+ chunks = []
+ start_mark = self.get_mark()
+ quote = self.peek()
+ self.forward()
+ chunks.extend(self.scan_flow_scalar_non_spaces(double, start_mark))
+ while self.peek() != quote:
+ chunks.extend(self.scan_flow_scalar_spaces(double, start_mark))
+ chunks.extend(self.scan_flow_scalar_non_spaces(double, start_mark))
+ self.forward()
+ end_mark = self.get_mark()
+ return ScalarToken(u''.join(chunks), False, start_mark, end_mark,
+ style)
+
+ ESCAPE_REPLACEMENTS = {
+ u'0': u'\0',
+ u'a': u'\x07',
+ u'b': u'\x08',
+ u't': u'\x09',
+ u'\t': u'\x09',
+ u'n': u'\x0A',
+ u'v': u'\x0B',
+ u'f': u'\x0C',
+ u'r': u'\x0D',
+ u'e': u'\x1B',
+ u' ': u'\x20',
+ u'\"': u'\"',
+ u'\\': u'\\',
+ u'N': u'\x85',
+ u'_': u'\xA0',
+ u'L': u'\u2028',
+ u'P': u'\u2029',
+ }
+
+ ESCAPE_CODES = {
+ u'x': 2,
+ u'u': 4,
+ u'U': 8,
+ }
+
+ def scan_flow_scalar_non_spaces(self, double, start_mark):
+ # See the specification for details.
+ chunks = []
+ while True:
+ length = 0
+ while self.peek(length) not in u'\'\"\\\0 \t\r\n\x85\u2028\u2029':
+ length += 1
+ if length:
+ chunks.append(self.prefix(length))
+ self.forward(length)
+ ch = self.peek()
+ if not double and ch == u'\'' and self.peek(1) == u'\'':
+ chunks.append(u'\'')
+ self.forward(2)
+ elif (double and ch == u'\'') or (not double and ch in u'\"\\'):
+ chunks.append(ch)
+ self.forward()
+ elif double and ch == u'\\':
+ self.forward()
+ ch = self.peek()
+ if ch in self.ESCAPE_REPLACEMENTS:
+ chunks.append(self.ESCAPE_REPLACEMENTS[ch])
+ self.forward()
+ elif ch in self.ESCAPE_CODES:
+ length = self.ESCAPE_CODES[ch]
+ self.forward()
+ for k in range(length):
+ if self.peek(k) not in u'0123456789ABCDEFabcdef':
+ raise ScannerError("while scanning a double-quoted scalar", start_mark,
+ "expected escape sequence of %d hexdecimal numbers, but found %r" %
+ (length, self.peek(k).encode('utf-8')), self.get_mark())
+ code = int(self.prefix(length), 16)
+ chunks.append(unichr(code))
+ self.forward(length)
+ elif ch in u'\r\n\x85\u2028\u2029':
+ self.scan_line_break()
+ chunks.extend(self.scan_flow_scalar_breaks(double, start_mark))
+ else:
+ raise ScannerError("while scanning a double-quoted scalar", start_mark,
+ "found unknown escape character %r" % ch.encode('utf-8'), self.get_mark())
+ else:
+ return chunks
+
+ def scan_flow_scalar_spaces(self, double, start_mark):
+ # See the specification for details.
+ chunks = []
+ length = 0
+ while self.peek(length) in u' \t':
+ length += 1
+ whitespaces = self.prefix(length)
+ self.forward(length)
+ ch = self.peek()
+ if ch == u'\0':
+ raise ScannerError("while scanning a quoted scalar", start_mark,
+ "found unexpected end of stream", self.get_mark())
+ elif ch in u'\r\n\x85\u2028\u2029':
+ line_break = self.scan_line_break()
+ breaks = self.scan_flow_scalar_breaks(double, start_mark)
+ if line_break != u'\n':
+ chunks.append(line_break)
+ elif not breaks:
+ chunks.append(u' ')
+ chunks.extend(breaks)
+ else:
+ chunks.append(whitespaces)
+ return chunks
+
+ def scan_flow_scalar_breaks(self, double, start_mark):
+ # See the specification for details.
+ chunks = []
+ while True:
+ # Instead of checking indentation, we check for document
+ # separators.
+ prefix = self.prefix(3)
+ if (prefix == u'---' or prefix == u'...') \
+ and self.peek(3) in u'\0 \t\r\n\x85\u2028\u2029':
+ raise ScannerError("while scanning a quoted scalar", start_mark,
+ "found unexpected document separator", self.get_mark())
+ while self.peek() in u' \t':
+ self.forward()
+ if self.peek() in u'\r\n\x85\u2028\u2029':
+ chunks.append(self.scan_line_break())
+ else:
+ return chunks
+
+ def scan_plain(self):
+ # See the specification for details.
+ # We add an additional restriction for the flow context:
+ # plain scalars in the flow context cannot contain ',', ':' and '?'.
+ # We also keep track of the `allow_simple_key` flag here.
+ # Indentation rules are loosed for the flow context.
+ chunks = []
+ start_mark = self.get_mark()
+ end_mark = start_mark
+ indent = self.indent+1
+ # We allow zero indentation for scalars, but then we need to check for
+ # document separators at the beginning of the line.
+ #if indent == 0:
+ # indent = 1
+ spaces = []
+ while True:
+ length = 0
+ if self.peek() == u'#':
+ break
+ while True:
+ ch = self.peek(length)
+ if ch in u'\0 \t\r\n\x85\u2028\u2029' \
+ or (not self.flow_level and ch == u':' and
+ self.peek(length+1) in u'\0 \t\r\n\x85\u2028\u2029') \
+ or (self.flow_level and ch in u',:?[]{}'):
+ break
+ length += 1
+ # It's not clear what we should do with ':' in the flow context.
+ if (self.flow_level and ch == u':'
+ and self.peek(length+1) not in u'\0 \t\r\n\x85\u2028\u2029,[]{}'):
+ self.forward(length)
+ raise ScannerError("while scanning a plain scalar", start_mark,
+ "found unexpected ':'", self.get_mark(),
+ "Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details.")
+ if length == 0:
+ break
+ self.allow_simple_key = False
+ chunks.extend(spaces)
+ chunks.append(self.prefix(length))
+ self.forward(length)
+ end_mark = self.get_mark()
+ spaces = self.scan_plain_spaces(indent, start_mark)
+ if not spaces or self.peek() == u'#' \
+ or (not self.flow_level and self.column < indent):
+ break
+ return ScalarToken(u''.join(chunks), True, start_mark, end_mark)
+
+ def scan_plain_spaces(self, indent, start_mark):
+ # See the specification for details.
+ # The specification is really confusing about tabs in plain scalars.
+ # We just forbid them completely. Do not use tabs in YAML!
+ chunks = []
+ length = 0
+ while self.peek(length) in u' ':
+ length += 1
+ whitespaces = self.prefix(length)
+ self.forward(length)
+ ch = self.peek()
+ if ch in u'\r\n\x85\u2028\u2029':
+ line_break = self.scan_line_break()
+ self.allow_simple_key = True
+ prefix = self.prefix(3)
+ if (prefix == u'---' or prefix == u'...') \
+ and self.peek(3) in u'\0 \t\r\n\x85\u2028\u2029':
+ return
+ breaks = []
+ while self.peek() in u' \r\n\x85\u2028\u2029':
+ if self.peek() == ' ':
+ self.forward()
+ else:
+ breaks.append(self.scan_line_break())
+ prefix = self.prefix(3)
+ if (prefix == u'---' or prefix == u'...') \
+ and self.peek(3) in u'\0 \t\r\n\x85\u2028\u2029':
+ return
+ if line_break != u'\n':
+ chunks.append(line_break)
+ elif not breaks:
+ chunks.append(u' ')
+ chunks.extend(breaks)
+ elif whitespaces:
+ chunks.append(whitespaces)
+ return chunks
+
+ def scan_tag_handle(self, name, start_mark):
+ # See the specification for details.
+ # For some strange reasons, the specification does not allow '_' in
+ # tag handles. I have allowed it anyway.
+ ch = self.peek()
+ if ch != u'!':
+ raise ScannerError("while scanning a %s" % name, start_mark,
+ "expected '!', but found %r" % ch.encode('utf-8'),
+ self.get_mark())
+ length = 1
+ ch = self.peek(length)
+ if ch != u' ':
+ while u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \
+ or ch in u'-_':
+ length += 1
+ ch = self.peek(length)
+ if ch != u'!':
+ self.forward(length)
+ raise ScannerError("while scanning a %s" % name, start_mark,
+ "expected '!', but found %r" % ch.encode('utf-8'),
+ self.get_mark())
+ length += 1
+ value = self.prefix(length)
+ self.forward(length)
+ return value
+
+ def scan_tag_uri(self, name, start_mark):
+ # See the specification for details.
+ # Note: we do not check if URI is well-formed.
+ chunks = []
+ length = 0
+ ch = self.peek(length)
+ while u'0' <= ch <= u'9' or u'A' <= ch <= u'Z' or u'a' <= ch <= u'z' \
+ or ch in u'-;/?:@&=+$,_.!~*\'()[]%':
+ if ch == u'%':
+ chunks.append(self.prefix(length))
+ self.forward(length)
+ length = 0
+ chunks.append(self.scan_uri_escapes(name, start_mark))
+ else:
+ length += 1
+ ch = self.peek(length)
+ if length:
+ chunks.append(self.prefix(length))
+ self.forward(length)
+ length = 0
+ if not chunks:
+ raise ScannerError("while parsing a %s" % name, start_mark,
+ "expected URI, but found %r" % ch.encode('utf-8'),
+ self.get_mark())
+ return u''.join(chunks)
+
+ def scan_uri_escapes(self, name, start_mark):
+ # See the specification for details.
+ bytes = []
+ mark = self.get_mark()
+ while self.peek() == u'%':
+ self.forward()
+ for k in range(2):
+ if self.peek(k) not in u'0123456789ABCDEFabcdef':
+ raise ScannerError("while scanning a %s" % name, start_mark,
+ "expected URI escape sequence of 2 hexdecimal numbers, but found %r" %
+ (self.peek(k).encode('utf-8')), self.get_mark())
+ bytes.append(chr(int(self.prefix(2), 16)))
+ self.forward(2)
+ try:
+ value = unicode(''.join(bytes), 'utf-8')
+ except UnicodeDecodeError, exc:
+ raise ScannerError("while scanning a %s" % name, start_mark, str(exc), mark)
+ return value
+
+ def scan_line_break(self):
+ # Transforms:
+ # '\r\n' : '\n'
+ # '\r' : '\n'
+ # '\n' : '\n'
+ # '\x85' : '\n'
+ # '\u2028' : '\u2028'
+ # '\u2029 : '\u2029'
+ # default : ''
+ ch = self.peek()
+ if ch in u'\r\n\x85':
+ if self.prefix(2) == u'\r\n':
+ self.forward(2)
+ else:
+ self.forward()
+ return u'\n'
+ elif ch in u'\u2028\u2029':
+ self.forward()
+ return ch
+ return u''
+
+#try:
+# import psyco
+# psyco.bind(Scanner)
+#except ImportError:
+# pass
+
diff --git a/pynxc/yaml/serializer.py b/pynxc/yaml/serializer.py
new file mode 100644
index 0000000..0bf1e96
--- /dev/null
+++ b/pynxc/yaml/serializer.py
@@ -0,0 +1,111 @@
+
+__all__ = ['Serializer', 'SerializerError']
+
+from error import YAMLError
+from events import *
+from nodes import *
+
+class SerializerError(YAMLError):
+ pass
+
+class Serializer(object):
+
+ ANCHOR_TEMPLATE = u'id%03d'
+
+ def __init__(self, encoding=None,
+ explicit_start=None, explicit_end=None, version=None, tags=None):
+ self.use_encoding = encoding
+ self.use_explicit_start = explicit_start
+ self.use_explicit_end = explicit_end
+ self.use_version = version
+ self.use_tags = tags
+ self.serialized_nodes = {}
+ self.anchors = {}
+ self.last_anchor_id = 0
+ self.closed = None
+
+ def open(self):
+ if self.closed is None:
+ self.emit(StreamStartEvent(encoding=self.use_encoding))
+ self.closed = False
+ elif self.closed:
+ raise SerializerError("serializer is closed")
+ else:
+ raise SerializerError("serializer is already opened")
+
+ def close(self):
+ if self.closed is None:
+ raise SerializerError("serializer is not opened")
+ elif not self.closed:
+ self.emit(StreamEndEvent())
+ self.closed = True
+
+ #def __del__(self):
+ # self.close()
+
+ def serialize(self, node):
+ if self.closed is None:
+ raise SerializerError("serializer is not opened")
+ elif self.closed:
+ raise SerializerError("serializer is closed")
+ self.emit(DocumentStartEvent(explicit=self.use_explicit_start,
+ version=self.use_version, tags=self.use_tags))
+ self.anchor_node(node)
+ self.serialize_node(node, None, None)
+ self.emit(DocumentEndEvent(explicit=self.use_explicit_end))
+ self.serialized_nodes = {}
+ self.anchors = {}
+ self.last_anchor_id = 0
+
+ def anchor_node(self, node):
+ if node in self.anchors:
+ if self.anchors[node] is None:
+ self.anchors[node] = self.generate_anchor(node)
+ else:
+ self.anchors[node] = None
+ if isinstance(node, SequenceNode):
+ for item in node.value:
+ self.anchor_node(item)
+ elif isinstance(node, MappingNode):
+ for key, value in node.value:
+ self.anchor_node(key)
+ self.anchor_node(value)
+
+ def generate_anchor(self, node):
+ self.last_anchor_id += 1
+ return self.ANCHOR_TEMPLATE % self.last_anchor_id
+
+ def serialize_node(self, node, parent, index):
+ alias = self.anchors[node]
+ if node in self.serialized_nodes:
+ self.emit(AliasEvent(alias))
+ else:
+ self.serialized_nodes[node] = True
+ self.descend_resolver(parent, index)
+ if isinstance(node, ScalarNode):
+ detected_tag = self.resolve(ScalarNode, node.value, (True, False))
+ default_tag = self.resolve(ScalarNode, node.value, (False, True))
+ implicit = (node.tag == detected_tag), (node.tag == default_tag)
+ self.emit(ScalarEvent(alias, node.tag, implicit, node.value,
+ style=node.style))
+ elif isinstance(node, SequenceNode):
+ implicit = (node.tag
+ == self.resolve(SequenceNode, node.value, True))
+ self.emit(SequenceStartEvent(alias, node.tag, implicit,
+ flow_style=node.flow_style))
+ index = 0
+ for item in node.value:
+ self.serialize_node(item, node, index)
+ index += 1
+ self.emit(SequenceEndEvent())
+ elif isinstance(node, MappingNode):
+ implicit = (node.tag
+ == self.resolve(MappingNode, node.value, True))
+ self.emit(MappingStartEvent(alias, node.tag, implicit,
+ flow_style=node.flow_style))
+ for key, value in node.value:
+ self.serialize_node(key, node, None)
+ self.serialize_node(value, node, key)
+ self.emit(MappingEndEvent())
+ self.ascend_resolver()
+
diff --git a/pynxc/yaml/tokens.py b/pynxc/yaml/tokens.py
new file mode 100644
index 0000000..4d0b48a
--- /dev/null
+++ b/pynxc/yaml/tokens.py
@@ -0,0 +1,104 @@
+
+class Token(object):
+ def __init__(self, start_mark, end_mark):
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ def __repr__(self):
+ attributes = [key for key in self.__dict__
+ if not key.endswith('_mark')]
+ attributes.sort()
+ arguments = ', '.join(['%s=%r' % (key, getattr(self, key))
+ for key in attributes])
+ return '%s(%s)' % (self.__class__.__name__, arguments)
+
+#class BOMToken(Token):
+# id = '<byte order mark>'
+
+class DirectiveToken(Token):
+ id = '<directive>'
+ def __init__(self, name, value, start_mark, end_mark):
+ self.name = name
+ self.value = value
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+
+class DocumentStartToken(Token):
+ id = '<document start>'
+
+class DocumentEndToken(Token):
+ id = '<document end>'
+
+class StreamStartToken(Token):
+ id = '<stream start>'
+ def __init__(self, start_mark=None, end_mark=None,
+ encoding=None):
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ self.encoding = encoding
+
+class StreamEndToken(Token):
+ id = '<stream end>'
+
+class BlockSequenceStartToken(Token):
+ id = '<block sequence start>'
+
+class BlockMappingStartToken(Token):
+ id = '<block mapping start>'
+
+class BlockEndToken(Token):
+ id = '<block end>'
+
+class FlowSequenceStartToken(Token):
+ id = '['
+
+class FlowMappingStartToken(Token):
+ id = '{'
+
+class FlowSequenceEndToken(Token):
+ id = ']'
+
+class FlowMappingEndToken(Token):
+ id = '}'
+
+class KeyToken(Token):
+ id = '?'
+
+class ValueToken(Token):
+ id = ':'
+
+class BlockEntryToken(Token):
+ id = '-'
+
+class FlowEntryToken(Token):
+ id = ','
+
+class AliasToken(Token):
+ id = '<alias>'
+ def __init__(self, value, start_mark, end_mark):
+ self.value = value
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+
+class AnchorToken(Token):
+ id = '<anchor>'
+ def __init__(self, value, start_mark, end_mark):
+ self.value = value
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+
+class TagToken(Token):
+ id = '<tag>'
+ def __init__(self, value, start_mark, end_mark):
+ self.value = value
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+
+class ScalarToken(Token):
+ id = '<scalar>'
+ def __init__(self, value, plain, start_mark, end_mark, style=None):
+ self.value = value
+ self.plain = plain
+ self.start_mark = start_mark
+ self.end_mark = end_mark
+ self.style = style
+