From 088a1576c5a29248e409c24b7ca1ff13cc28e16d Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Wed, 03 Mar 2010 22:48:06 +0000 Subject: tightened up get path code --- diff --git a/TurtleArtActivity.py b/TurtleArtActivity.py index cd862ce..b3b825a 100644 --- a/TurtleArtActivity.py +++ b/TurtleArtActivity.py @@ -62,7 +62,7 @@ import sys from taconstants import * from taexporthtml import save_html from taexportlogo import save_logo -from tautils import data_to_file, data_to_string, data_from_string +from tautils import data_to_file, data_to_string, data_from_string, get_path from tawindow import TurtleArtWindow SERVICE = 'org.laptop.TurtleArtActivity' @@ -72,9 +72,9 @@ PATH = '/org/laptop/TurtleArtActivity' class TurtleArtActivity(activity.Activity): def __init__(self, handle): - super(TurtleArtActivity,self).__init__(handle) + super(TurtleArtActivity, self).__init__(handle) - datapath = self._get_datapath() + datapath = get_path(activity, 'data') self._setup_visibility_handler() @@ -107,7 +107,7 @@ class TurtleArtActivity(activity.Activity): return # save the html code to the instance directory - datapath = os.path.join(activity.get_activity_root(), "instance") + datapath = get_path(activity, 'instance') html_file = os.path.join(datapath, "portfolio.html") f = file(html_file, "w") @@ -167,7 +167,7 @@ class TurtleArtActivity(activity.Activity): dsobject.metadata['icon-color'] = profile.get_color().to_string() # save the html code to the instance directory - datapath = os.path.join(activity.get_activity_root(), "instance") + datapath = get_path(activity, 'instance') # Write the file to the data directory of this activity's root. file_path = os.path.join(datapath, filename) @@ -247,19 +247,9 @@ class TurtleArtActivity(activity.Activity): """ Save snapshot """ def _do_keep_cb(self, button): # Create a datastore object - # save the current state of the project to the instance directory + datapath = get_path(activity, 'instance') - # work-around Rainbow which doesn't seem to like tempfile.mkstemp - try: - tmppath = os.path.join(activity.get_activity_root(), "instance") - except: - # Early versions of Sugar (e.g., 656) didn't support - # get_activity_root() - tmppath = os.path.join( \ - os.environ['HOME'], \ - ".sugar/default/org.laptop.TurtleArtActivity/instance") - - tafile = os.path.join(tmppath,"tmpfile.ta") + tafile = os.path.join(datapath,"tmpfile.ta") print tafile try: data_to_file(self.tw.assemble_data_to_save(), tafile) @@ -450,16 +440,8 @@ class TurtleArtActivity(activity.Activity): async_cb(logo_code_path) def _dump_logo_code(self): - # work-around Rainbow which doesn't seem to like tempfile.mkstemp - try: - tmppath = os.path.join(activity.get_activity_root(), "instance") - except: - # Early versions of Sugar (e.g., 656) didn't support - # get_activity_root() - tmppath = os.path.join( \ - os.environ['HOME'], \ - ".sugar/default/org.laptop.TurtleArtActivity/instance") - tafile = os.path.join(tmppath,"tmpfile.ta") + datapath = get_path(activity, 'instance') + tafile = os.path.join(datapath,"tmpfile.ta") try: code = save_logo(self, self.tw) f = file(tafile, "w") @@ -1050,19 +1032,6 @@ class TurtleArtActivity(activity.Activity): self.connect('shared', self._shared_cb) self.connect('joined', self._joined_cb) - """ - get datapath - """ - def _get_datapath(self): - try: - datapath = os.path.join(activity.get_activity_root(), "data") - except: - # Early versions of Sugar (e.g., 656) didn't support - # get_activity_root() - datapath = os.path.join( \ - os.environ['HOME'], \ - ".sugar/default/org.laptop.TurtleArtActivity/data") - return datapath """ Notify when the visibility state changes diff --git a/tacanvas.py b/tacanvas.py index 054f309..5516327 100644 --- a/tacanvas.py +++ b/tacanvas.py @@ -23,6 +23,7 @@ import gtk from math import sin, cos, pi from sprites import Sprite from tasprite_factory import SVG +from tautils import image_to_base64 import pango from taconstants import CANVAS_LAYER, DEFAULT_TURTLE @@ -306,7 +307,13 @@ class TurtleGraphics: self.canvas.images[0].draw_pixbuf(self.gc, pixbuf, a, b, x, y) self.invalt(x, y, w, h) if self.tw.saving_svg: - self.tw.svg_string += self.svg.image(x-self.width/2, y, w, h, path) + if self.tw.running_sugar: + # In Sugar, we need to embed the images inside the SVG + self.tw.svg_string += self.svg.image(x-self.width/2, y, w, h, + path, image_to_base64(pixbuf, self.tw.activity)) + else: + self.tw.svg_string += self.svg.image(x-self.width/2, y, w, h, + path) def draw_text(self, label, x, y, size, w): w *= self.tw.coord_scale diff --git a/tasprite_factory.py b/tasprite_factory.py index 45dcb99..3f621fd 100755 --- a/tasprite_factory.py +++ b/tasprite_factory.py @@ -671,17 +671,23 @@ class SVG: "\" style=\"font-size:", size, "px;fill:", self._stroke, "\">", string, "\n \n") - def image(self, x, y, w, h, path): + def image(self, x, y, w, h, path, image_data=None): self._x = x self._y = y self._check_min_max() self._x = x+w self._y = y+h self._check_min_max() - return " %s%.1f%s%.1f%s%.1f%s%.1f%s%s%s" % ( - "\n") + if image_data == None: + return " %s%.1f%s%.1f%s%.1f%s%.1f%s%s%s" % ( + "\n") + else: + return " %s%.1f%s%.1f%s%.1f%s%.1f%s%s%s" % ( + "\n") def _circle(self, r, cx, cy): diff --git a/tautils.py b/tautils.py index 48ac5ce..81eeca6 100644 --- a/tautils.py +++ b/tautils.py @@ -21,6 +21,7 @@ import gtk import pickle +import subprocess try: _old_Sugar_system = False import json @@ -164,6 +165,25 @@ def get_pixbuf_from_journal(dsobject, w, h): pixbuf = None return pixbuf +def get_path(activity, subpath ): + try: + return(os.path.join(activity.get_activity_root(), subpath)) + except: + # Early versions of Sugar didn't support get_activity_root() + return(os.path.join(os.environ['HOME'], ".sugar/default", + "org.laptop.TurtleArtActivity", subpath)) + +def image_to_base64(pixbuf, activity): + filename = os.path.join(get_path(activity, 'instance'), 'imagetmp.png') + pixbuf.save(filename, "png") + base64 = os.path.join(get_path(activity, 'instance'), 'base64tmp') + cmd = "base64 <" + filename + " >" + base64 + subprocess.check_call(cmd, shell=True) + f = open( base64, 'r') + data = f.read() + f.close() + return data + def movie_media_type(name): return name.endswith(('.ogv','.vob','.mp4','.wmv','.mov', '.mpeg')) -- cgit v0.9.1