From 383de1d3c95423b53eff8d972c2dd7706a37d9dd Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Thu, 17 Jan 2013 17:23:50 +0000 Subject: using constants for suffix and mimetype --- diff --git a/TurtleArt/taconstants.py b/TurtleArt/taconstants.py index 6985ce0..97a26b7 100644 --- a/TurtleArt/taconstants.py +++ b/TurtleArt/taconstants.py @@ -21,10 +21,12 @@ from gettext import gettext as _ -# -# Sprite layers -# +# Packaging constants +SUFFIX = ['.ta', '.tb'] +MAGICNUMBER = 'TB' +MIMETYPE = ['application/x-turtle-art', 'application/vnd.turtleblocks'] +# Sprite layers OVERLAY_LAYER = 400 TURTLE_LAYER = 500 BLOCK_LAYER = 600 @@ -45,9 +47,7 @@ BOX_COLORS = {'red': ["#FF0000", "#A00000"], 'white': ["#FFFFFF", "#A0A0A0"], 'black': ["#000000", "#000000"]} -# # Misc. parameters -# PALETTE_HEIGHT = 120 PALETTE_WIDTH = 175 SELECTOR_WIDTH = 55 @@ -84,9 +84,7 @@ CONSTANTS = {'leftpos': None, 'toppos': None, 'rightpos': None, 'purple': 90, 'titlex': None, 'titley': None, 'leftx': None, 'topy': None, 'rightx': None, 'bottomy': None} -# # Blocks that are expandable -# EXPANDABLE_STYLE = ['boolean-style', 'compare-porch-style', 'compare-style', 'number-style-porch', 'number-style', 'basic-style-2arg', 'number-style-block', 'box-style-media'] @@ -100,9 +98,7 @@ EXPANDABLE_ARGS = ['list', 'myfunc1arg', 'myfunc2arg', 'myfunc3arg', 'userdefined', 'userdefined2args', 'userdefined3args', 'loadblock', 'loadblock2arg', 'loadblock3arg'] -# # Deprecated block styles that need dock adjustments -# OLD_DOCK = ['and', 'or', 'plus', 'minus', 'division', 'product', 'remainder'] CONTENT_ARGS = ['show', 'showaligned', 'push', 'storein', 'storeinbox1', @@ -110,9 +106,7 @@ CONTENT_ARGS = ['show', 'showaligned', 'push', 'storein', 'storeinbox1', PREFIX_DICTIONARY = {} -# # These blocks get a special skin -# BLOCKS_WITH_SKIN = [] PYTHON_SKIN = [] @@ -125,9 +119,7 @@ NO_IMPORT = [] EXPAND_SKIN = {} -# # Status blocks -# OVERLAY_SHAPES = ['Cartesian', 'Cartesian_labeled', 'polar', 'metric'] STATUS_SHAPES = ['status', 'info', 'nostack', 'dupstack', 'noinput', @@ -135,15 +127,11 @@ STATUS_SHAPES = ['status', 'info', 'nostack', 'dupstack', 'noinput', 'negroot', 'syntaxerror', 'nofile', 'nojournal', 'zerodivide', 'notanumber', 'incompatible', 'help', 'print'] -# # Emulate Sugar toolbar when running from outside of Sugar -# TOOLBAR_SHAPES = ['hideshowoff', 'eraseron', 'run-fastoff', 'run-slowoff', 'stopiton'] -# # Legacy names -# OLD_NAMES = {'product': 'product2', 'storeinbox': 'storein', 'minus': 'minus2', 'division': 'division2', 'plus': 'plus2', 'and': 'and2', 'or': 'or2', 'less': 'less2', 'greater': 'greater2', @@ -157,15 +145,12 @@ OLD_NAMES = {'product': 'product2', 'storeinbox': 'storein', 'minus': 'minus2', 'sandwichtop2': 'sandwichtop', 'image': 'show', 'container': 'indentity2', 'insertimage': 'show'} -# # Define the relative size and postion of media objects # (w, h, x, y, dx, dy) # TITLEXY = (0.9375, 0.875) -# # Relative placement of portfolio objects (used by deprecated blocks) -# TEMPLATES = {'t1x1': (0.5, 0.5, 0.0625, 0.125, 1.05, 0), 't2z1': (0.5, 0.5, 0.0625, 0.125, 1.05, 1.05), 't1x2': (0.45, 0.45, 0.0625, 0.125, 1.05, 1.05), @@ -190,9 +175,7 @@ VOICES = {'af': 'afrikaans', 'cy': 'welsh-test', 'el': 'greek', 'cs': 'czech', 'it': 'italian', 'pl': 'polish', 'ru': 'russian_test', 'sv': 'swedish', 'tr': 'turkish'} -# # Macros (groups of blocks) -# MACROS = { 'ifthenelse': # Because it is too big to fit on the palette [[0, 'ifelse', 0, 0, [None, None, None, None, None]]], diff --git a/TurtleArt/tautils.py b/TurtleArt/tautils.py index 7d3a351..5f1635b 100644 --- a/TurtleArt/tautils.py +++ b/TurtleArt/tautils.py @@ -1,5 +1,5 @@ #copyright (c) 2007-8, Playful Invention Company. -#Copyright (c) 2008-12, Walter Bender +#Copyright (c) 2008-13, Walter Bender #Permission is hereby granted, free of charge, to any person obtaining a copy #of this software and associated documentation files (the "Software"), to deal @@ -44,7 +44,7 @@ except (ImportError, AttributeError): from StringIO import StringIO from taconstants import (HIT_HIDE, HIT_SHOW, XO1, XO15, XO175, XO30, XO4, - UNKNOWN) + UNKNOWN, MAGICNUMBER) import logging _logger = logging.getLogger('turtleart-activity') @@ -140,8 +140,12 @@ def json_load(text): if OLD_SUGAR_SYSTEM is True: listdata = json.read(text) else: - # Strip out leading and trailing whitespace, nulls, and newlines - clean_text = text.lstrip() + # Remove MAGIC NUMBER, if present, and leading whitespace + if text[0:2] == MAGICNUMBER: + clean_text = text[2:].lstrip() + else: + clean_text = text.lstrip() + # Strip out trailing whitespace, nulls, and newlines clean_text = clean_text.replace('\12', '') clean_text = clean_text.replace('\00', '') clean_text = clean_text.rstrip() diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index a7432d7..ba072f6 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- #Copyright (c) 2007, Playful Invention Company -#Copyright (c) 2008-12, Walter Bender +#Copyright (c) 2008-13, Walter Bender #Copyright (c) 2009-11 Raúl Gutiérrez Segalés #Copyright (c) 2011 Collabora Ltd. @@ -54,7 +54,7 @@ from taconstants import (HORIZONTAL_PALETTE, VERTICAL_PALETTE, BLOCK_SCALE, OLD_NAMES, DEFAULT_TURTLE, TURTLE_LAYER, EXPANDABLE, NO_IMPORT, TEMPLATES, PYTHON_SKIN, PALETTE_HEIGHT, STATUS_LAYER, OLD_DOCK, EXPANDABLE_ARGS, XO1, XO15, XO175, XO30, XO4, TITLEXY, CONTENT_ARGS, - CONSTANTS, EXPAND_SKIN, PROTO_LAYER, EXPANDABLE_FLOW) + CONSTANTS, EXPAND_SKIN, PROTO_LAYER, EXPANDABLE_FLOW, SUFFIX) from tapalette import (palette_names, palette_blocks, expandable_blocks, block_names, content_blocks, default_values, special_names, block_styles, help_strings, hidden_proto_blocks, string_or_number_args, @@ -3401,8 +3401,8 @@ may not terminate.', False) self.load_save_folder) if _file_name is None: return - if not _file_name[-3:] in ['.ta', '.tb']: - _file_name = _file_name + '.tb' + if not _file_name[-3:] in SUFFIX: + _file_name = _file_name + SUFFIX[1] self.load_files(_file_name, create_new_project) if create_new_project: self.save_file_name = os.path.basename(_file_name) @@ -3698,19 +3698,19 @@ may not terminate.', False) else: self.process_data(data_from_file(ta_file)) - def save_file(self, _file_name=None): + def save_file(self, file_name=None): ''' Start a project to a file ''' if self.save_folder is not None: self.load_save_folder = self.save_folder - if _file_name is None: - _file_name, self.load_save_folder = get_save_name('.t[a-b]', - self.load_save_folder, self.save_file_name) - if _file_name is None: + if file_name is None: + file_name, self.load_save_folder = get_save_name( + '.t[a-b]', self.load_save_folder, self.save_file_name) + if file_name is None: return - if not _file_name[-3:] in ['.ta', '.tb']: - _file_name = _file_name + '.tb' - data_to_file(self.assemble_data_to_save(), _file_name) - self.save_file_name = os.path.basename(_file_name) + if not file_name[-3:] in SUFFIX: + file_name = file_name + SUFFIX[1] + data_to_file(self.assemble_data_to_save(), file_name) + self.save_file_name = os.path.basename(file_name) if not self.running_sugar: self.save_folder = self.load_save_folder @@ -3852,14 +3852,17 @@ may not terminate.', False) dy *= h return(w, h, x, y, dx, dy) - def save_for_upload(self, _file_name): + def save_for_upload(self, file_name): ''' Grab the current canvas and save it for upload ''' - if _file_name[-3:] in ['.ta', '.tb']: - _file_name = _file_name[0: -3] - data_to_file(self.assemble_data_to_save(), _file_name + '.ta') - save_picture(self.canvas, _file_name + '.png') - ta_file = _file_name + '.ta' - image_file = _file_name + '.png' + if not file_name[-3:] in SUFFIX: + ta_name = file_name + SUFFIX[1] + image_file = file_name + '.png' + else: + ta_file = file_name + image_file = file_name[0:-3] + '.png' + + data_to_file(self.assemble_data_to_save(), ta_file) + save_picture(self.canvas, image_file) return ta_file, image_file def save_as_image(self, name="", svg=False): diff --git a/TurtleArtActivity.py b/TurtleArtActivity.py index 69e17a8..d379822 100644 --- a/TurtleArtActivity.py +++ b/TurtleArtActivity.py @@ -57,7 +57,7 @@ from gettext import gettext as _ from TurtleArt.tapalette import palette_names, help_strings, help_palettes, \ help_windows from TurtleArt.taconstants import ICON_SIZE, BLOCK_SCALE, XO1, XO15, XO175, \ - XO30 + XO30, XO4, MIMETYPE from TurtleArt.taexportlogo import save_logo from TurtleArt.tautils import data_to_file, data_to_string, data_from_string, \ get_path, chooser, get_hardware @@ -180,7 +180,7 @@ class TurtleArtActivity(activity.Activity): dsobject.metadata['title'] = self.metadata['title'] + ' ' + \ _('snapshot') dsobject.metadata['icon-color'] = profile.get_color().to_string() - dsobject.metadata['mime_type'] = 'application/x-turtle-art' + dsobject.metadata['mime_type'] = MIMETYPE[0] dsobject.metadata['activity'] = 'org.laptop.TurtleArtActivity' dsobject.set_file_path(tmpfile) datastore.write(dsobject) @@ -558,7 +558,7 @@ class TurtleArtActivity(activity.Activity): self.do_cartesian_cb, view_toolbar) self._add_button('view-polar', _('Polar coordinates'), self.do_polar_cb, view_toolbar) - if get_hardware() in [XO1, XO15, XO175]: + if get_hardware() in [XO1, XO15, XO175, XO4]: self._add_button('view-metric', _('Metric coordinates'), self.do_metric_cb, view_toolbar) self._add_separator(view_toolbar, visible=False) @@ -711,7 +711,7 @@ class TurtleArtActivity(activity.Activity): add_paragraph(help_box, _('Cartesian coordinates'), icon='view-Cartesian') add_paragraph(help_box, _('Polar coordinates'), icon='view-polar') - if get_hardware() in [XO1, XO15, XO175]: + if get_hardware() in [XO1, XO15, XO175, XO4]: add_paragraph(help_box, _('Metric coordinates'), icon='view-metric') add_paragraph(help_box, _('Rescale coordinates up'), @@ -736,7 +736,7 @@ class TurtleArtActivity(activity.Activity): self.do_palette_buttons_cb, i, help_strings[palette_name], palette_group)) - if self.tw.hw in [XO1, XO15, XO175]: + if self.tw.hw in [XO1, XO15, XO175, XO4]: self._add_separator(self._palette_toolbar, expand=True, visible=False) self._make_palette_buttons(self._palette_toolbar) @@ -963,7 +963,7 @@ class TurtleArtActivity(activity.Activity): def write_file(self, file_path): ''' Write the project to the Journal. ''' data_to_file(self.tw.assemble_data_to_save(), file_path) - self.metadata['mime_type'] = 'application/x-turtle-art' + self.metadata['mime_type'] = MIMETYPE[0] self.metadata['turtle blocks'] = ''.join(self.tw.used_block_list) self.metadata['public'] = data_to_string(['activity count', 'turtle blocks']) diff --git a/turtleblocks.py b/turtleblocks.py index 83fd8a7..df8c436 100755 --- a/turtleblocks.py +++ b/turtleblocks.py @@ -48,7 +48,7 @@ sys.argv[1:] = [] # Execution of import gst cannot see '--help' or '-h' import gettext from TurtleArt.taconstants import OVERLAY_LAYER, DEFAULT_TURTLE_COLORS, \ - TAB_LAYER + TAB_LAYER, SUFFIX from TurtleArt.tautils import data_to_string, data_from_string, get_save_name from TurtleArt.tawindow import TurtleArtWindow from TurtleArt.taexportlogo import save_logo @@ -79,13 +79,13 @@ class TurtleMain(): gettext.textdomain(bundle_id) global _ _ = gettext.gettext - self._HELP_MSG = 'turtleart.py: ' + _('usage is') + ''' - \tturtleart.py - \tturtleart.py project.ta - \tturtleart.py --output_png project.ta - \tturtleart.py -o project - \tturtleart.py --run project.ta - \tturtleart.py -r project''' + self._HELP_MSG = 'turtleblocks.py: ' + _('usage is') + ''' + \tturtleblocks.py + \tturtleblocks.py project.tb + \tturtleblocks.py --output_png project.tb + \tturtleblocks.py -o project + \tturtleblocks.py --run project.tb + \tturtleblocks.py -r project''' self._init_vars() self._parse_command_line() self._ensure_sugar_paths() @@ -243,8 +243,8 @@ class TurtleMain(): sys.exit() if self._ta_file is not None: - if not self._ta_file.endswith(('.ta')): - self._ta_file += '.ta' + if not self._ta_file.endswith(SUFFIX): + self._ta_file += '.tb' if not os.path.exists(self._ta_file): self._ta_file = os.path.join(self._abspath, self._ta_file) if not os.path.exists(self._ta_file): -- cgit v0.9.1