diff options
author | Pootle daemon <pootle@sugarlabs.org> | 2009-09-03 14:29:21 (GMT) |
---|---|---|
committer | Pootle daemon <pootle@sugarlabs.org> | 2009-09-03 14:29:21 (GMT) |
commit | 84e5f8f26c844dedaa9ab4c99715b733572b9b8c (patch) | |
tree | 26df4622427fdd0a58e4400b2f282281af1b3d91 /taproject.py | |
parent | bef382cbcaa6bb891e86a4a1bc1c5e316a1f9b73 (diff) | |
parent | a528071865a5d720777572ab2c5ae42a64a562ff (diff) |
Merge branch 'master' of gitorious@git.sugarlabs.org:turtleart/mainline
Diffstat (limited to 'taproject.py')
-rw-r--r-- | taproject.py | 85 |
1 files changed, 57 insertions, 28 deletions
diff --git a/taproject.py b/taproject.py index ee65cee..8e0f6cf 100644 --- a/taproject.py +++ b/taproject.py @@ -24,10 +24,21 @@ pygtk.require('2.0') import gtk import pickle try: + _old_Sugar_system = False import json json.dumps + from json import load as jload + from json import dump as jdump except (ImportError, AttributeError): - import simplejson as json + try: + import simplejson as json + from json import load as jload + from json import dump as jdump + except: + # use pickle on old systems + _old_Sugar_system = True + # will try json.read and .write too + from StringIO import StringIO import os.path @@ -56,6 +67,7 @@ def load_file(tw): tw.save_file_name = os.path.basename(fname) def load_files(tw,ta_file, png_file=''): + # ignoring the png_file even if it is present f = open(ta_file, "r") try: data = pickle.load(f) # old-style data format @@ -63,9 +75,13 @@ def load_files(tw,ta_file, png_file=''): # print "reading saved json data" f.seek(0) # rewind necessary because of pickle.load text = f.read() - io = StringIO(text) - listdata = json.load(io) - # listdata = json.decode(text) + if _old_Sugar_system is True: + listdata = json.read(text) + else: + io = StringIO(text) + listdata = jload(io) + print listdata + # listdata = jdecode(text) data = tuplify(listdata) # json converts tuples to lists f.close() new_project(tw) @@ -80,18 +96,22 @@ def get_load_name(tw): # unpack serialized data sent across a share def load_string(tw,text): - io = StringIO(text) - listdata = json.load(io) - # listdata = json.decode(text) + if _old_Sugar_system is True: + listdata = json.read(text) + else: + io = StringIO(text) + listdata = jload(io) data = tuplify(listdata) # json converts tuples to lists new_project(tw) read_data(tw,data) # unpack sserialized data from the clipboard def clone_stack(tw,text): - io = StringIO(text) - listdata = json.load(io) - # listdata = json.decode(text) + if _old_Sugar_system is True: + listdata = json.read(text) + else: + io = StringIO(text) + listdata = jload(io) data = tuplify(listdata) # json converts tuples to lists read_stack(tw,data) @@ -137,7 +157,7 @@ def load_spr(tw,b): label = None proto = tw.protodict[btype] spr = sprNew(tw,b[2]+tw.turtle.canvas.x,b[3]+tw.turtle.canvas.y, \ - proto.image) + proto.image) spr.type = 'block' spr.proto = proto if label is not None: spr.label=label @@ -195,20 +215,27 @@ def get_save_name(tw): def save_data(tw,fname): f = file(fname, "w") data = assemble_data_to_save(tw) - io = StringIO() - json.dump(data,io) - text = io.getvalue() - # text = json.encode(data) - f.write(text) + if _old_Sugar_system is True: + # use pickle here to maintain compatibility with TA 10 + pickle.dump(data,f) + else: + io = StringIO() + jdump(data,io) + text = io.getvalue() + print text + # text = jencode(data) + f.write(text) f.close() # Used to send data across a shared session def save_string(tw): data = assemble_data_to_save(tw) - io = StringIO() - json.dump(data,io) - text = io.getvalue() - # text = json.encode(data) + if _old_Sugar_system is True: + text = json.write(data) + else: + io = StringIO() + jdump(data,io) + text = io.getvalue() return text def assemble_data_to_save(tw): @@ -227,19 +254,21 @@ def assemble_data_to_save(tw): else: connections = None data.append((b.id,name,b.x-tw.turtle.canvas.x, \ - b.y-tw.turtle.canvas.y,connections)) + b.y-tw.turtle.canvas.y,connections)) data.append((-1,'turtle', - tw.turtle.xcor,tw.turtle.ycor,tw.turtle.heading, - tw.turtle.color,tw.turtle.shade,tw.turtle.pensize)) + tw.turtle.xcor,tw.turtle.ycor,tw.turtle.heading, + tw.turtle.color,tw.turtle.shade,tw.turtle.pensize)) return data # serialize a stack to save to the clipboard def serialize_stack(tw): data = assemble_stack_to_clone(tw) - io = StringIO() - json.dump(data,io) - text = io.getvalue() - # text = json.encode(data) + if _old_Sugar_system is True: + text = json.write(data) + else: + io = StringIO() + jdump(data,io) + text = io.getvalue() return text # find the stack under the cursor and serialize it @@ -263,7 +292,7 @@ def assemble_stack_to_clone(tw): else: connections = None data.append((b.id,name,b.x-tw.turtle.canvas.x+20, \ - b.y-tw.turtle.canvas.y+20,connections)) + b.y-tw.turtle.canvas.y+20,connections)) return data def save_pict(tw,fname): |