From aa2d1d42f01f87b5a0a7e533bb6a93d3720b9d1a Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Mon, 21 Feb 2011 23:00:50 +0000 Subject: Merge git.sugarlabs.org:~walter/turtleart/collaboration-refactoring Conflicts: NEWS TurtleArt/talogo.py --- (limited to 'util') diff --git a/util/RtfParser.py b/util/RtfParser.py new file mode 100644 index 0000000..9a141a4 --- /dev/null +++ b/util/RtfParser.py @@ -0,0 +1,150 @@ +#Copyright (c) 2010, Loic Fejoz +#Copyright (c) 2010, Walter Bender + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. + + +import sys + + +class RtfException(Exception): + pass + +plaintext = 1 +control = 2 +argument = 3 +backslash = 4 +escapedChar = 5 + + +class RtfParser(object): + + def __init__(self, unicode=False): + self.state = plaintext + self.arg = '' + self.token = '' + self.unicode = unicode + self.par = False + self.output = '' + + def getChar(self, code): + """ called when an escaped char is found """ + return chr(code) + + def getNonBreakingSpace(self): + return ' ' + + def pushState(self): + pass + + def popState(self): + pass + + def putChar(self): + pass + + def doControl(self, token, arg): + pass + + def feed(self, txt): + for c in txt: + self.feedChar(c) + + def feedChar(self, char): + if self.state == plaintext: # this is just normal user content + if char == '\\': + self.state = backslash + elif char == '{': + self.pushState() + elif char == '}': + self.popState() + else: + self.putChar(char) + elif self.state == backslash: # a command or escape + if char == '\\' or char == '{' or char == '}': + self.putChar(char) + self.state = plaintext + else: + if char.isalpha() or char in ('*', '-', '|'): + self.state = control + self.token = char + elif char == "'": + self.state = escapedChar + self.escapedChar = '' + elif char in ['\\', '{', '}']: + self.putChar(char) + self.state = plaintext + elif char == "~": # non breaking space + self.putChar(self.getNonBreakingSpace()) + self.state = plaintext + else: + raise RtfException(('unexpected %s after \\' % char)) + elif self.state == escapedChar: + self.escapedChar = self.escapedChar + char + if len(self.escapedChar) == 2: + char = self.getChar(int(self.escapedChar, 16)) + self.putChar(char) + self.state = plaintext + elif self.state == control: # collecting the command token + if char.isalpha(): + self.token = self.token + char + elif char.isdigit() or char == '-': + self.state = argument + self.arg = char + else: + self.doControl(self.token, self.arg) + self.state = plaintext + if char == '\\': + self.state = backslash + elif char == '{': + self.pushState() + elif char == '}': + self.popState() + else: + if not char.isspace(): + self.putChar(char) + elif self.state == argument: # collecting the optional argument + if char.isdigit(): + self.arg = self.arg + char + else: + self.state = plaintext + self.doControl(self.token, self.arg) + if char == '\\': + self.state = backslash + elif char == '{': + self.pushState() + elif char == '}': + self.popState() + else: + if not char.isspace(): + self.putChar(char) + + +class RtfTextOnly(RtfParser): + + def __init__(self): + RtfParser.__init__(self) + self.level = 0 + + def pushState(self): + self.level = self.level + 1 + + def popState(self): + self.level = self.level - 1 + + def putChar(self, ch): + if self.par: + self.output += ch + + def doControl(self, token, arg): + if token[0:3] == 'par': + self.par = True + pass diff --git a/util/configfile.py b/util/configfile.py index 5df8f59..adabb34 100644 --- a/util/configfile.py +++ b/util/configfile.py @@ -1,10 +1,24 @@ #!/usr/bin/env python # # Copyright (c) 2011 Collabora Ltd. +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. # +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. import gobject + class ConfigFile(gobject.GObject): """Load/save a simple (key = value) config file""" @@ -15,7 +29,7 @@ class ConfigFile(gobject.GObject): ()), } - def __init__(self, config_file_path, valid_keys = {}): + def __init__(self, config_file_path, valid_keys={}): gobject.GObject.__init__(self) self._config_file_path = config_file_path @@ -29,24 +43,24 @@ class ConfigFile(gobject.GObject): def is_loaded(self): return self._is_loaded - def get(self, key, empty_if_not_loaded = False): - if not self._valid_keys.has_key(key): + def get(self, key, empty_if_not_loaded=False): + if not key in self._valid_keys: raise RuntimeError("Unknown config value %s" % key) - if self._config_hash.has_key(key): + if key in self._config_hash: value = self._config_hash[key] else: if self._valid_keys[key]["type"] == "text": value = "" - elif self._valid_keys[key]["type"] == "boolean": + elif self._valid_keys[key]["type"] == "boolean": value = False - elif self._valid_keys[key]["type"] == "integer": - value = 0 + elif self._valid_keys[key]["type"] == "integer": + value = 0 return value def set(self, key, value): - if not self._valid_keys.has_key(key): + if not key in self._valid_keys: raise RuntimeError("Unknown config value %s" % key) self._config_hash[key] = value @@ -58,10 +72,10 @@ class ConfigFile(gobject.GObject): config_file.close() for line in lines: line = line.strip() - k,v = line.split('=') + k, v = line.split('=') k = k.strip(' ') v = v.strip(' ') - if not self._valid_keys.has_key(k): + if not k in self._valid_keys: raise RuntimeError("Unknown config value %s" % k) value_type = self._valid_keys[k]["type"] if value_type == "text": @@ -73,11 +87,11 @@ class ConfigFile(gobject.GObject): self._config_hash[k] = value self._is_loaded = True self.emit('configuration-loaded') - except Exception,e: + except Exception, e: print e return self._is_loaded - + def save(self): config_file = open(self._config_file_path, 'w') for k in self._config_hash.keys(): @@ -94,14 +108,15 @@ class ConfigFile(gobject.GObject): l = "%s = %s\n" % (k, v) print l + def test_save_load(test_config_file): keys = {} - keys["nick"] = { "type" : "text" } - keys["account_id"] = { "type" : "text" } - keys["server"] = { "type" : "text" } - keys["port"] = { "type" : "text" } - keys["password"] = { "type" : "text" } - keys["register"] = { "type" : "text" } + keys["nick"] = {"type": "text"} + keys["account_id"] = {"type": "text"} + keys["server"] = {"type": "text"} + keys["port"] = {"type": "text"} + keys["password"] = {"type": "text"} + keys["register"] = {"type": "text"} c = ConfigFile(test_config_file) c.set_valid_keys(keys) @@ -113,28 +128,31 @@ def test_save_load(test_config_file): c.set("register", True) c.save() - + c = ConfigFile(test_config_file) c.set_valid_keys(keys) c.load() c.dump_keys() + def _configuration_saved_cb(config_file_obj): print "_configuration_saved_cb called" config_file_obj.dump_keys() + def _configuration_loaded_cb(config_file_obj): print "_configuration_loaded_cb called" config_file_obj.dump_keys() + def test_signals(test_config_file): keys = {} - keys["nick"] = { "type" : "text" } - keys["account_id"] = { "type" : "text" } - keys["server"] = { "type" : "text" } - keys["port"] = { "type" : "text" } - keys["password"] = { "type" : "text" } - keys["register"] = { "type" : "text" } + keys["nick"] = {"type": "text"} + keys["account_id"] = {"type": "text"} + keys["server"] = {"type": "text"} + keys["port"] = {"type": "text"} + keys["password"] = {"type": "text"} + keys["register"] = {"type": "text"} c = ConfigFile(test_config_file) c.connect('configuration-saved', _configuration_saved_cb) @@ -147,12 +165,13 @@ def test_signals(test_config_file): c.set("register", True) c.save() - + c = ConfigFile(test_config_file) c.connect('configuration-loaded', _configuration_loaded_cb) c.set_valid_keys(keys) c.load() + if __name__ == "__main__": test_save_load("/tmp/configfile.0001") test_signals("/tmp/configfile.0002") diff --git a/util/configwizard.py b/util/configwizard.py index 7716362..6c66bd1 100644 --- a/util/configwizard.py +++ b/util/configwizard.py @@ -1,25 +1,41 @@ #!/usr/bin/python +# Copyright (c) 2011 Collabora Ltd. +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. from configfile import ConfigFile -import gtk +import gtk + class ConfigWizard(): """Simple configuration wizard window.""" - + def __init__(self, config_file_path): self._config_items = [] self._config_entries = {} self._config_file_path = config_file_path - self._config_file_obj = None + self._config_file_obj = None """ - [ { item_label, item_type, item_name, item_with_value } , ... ] + [ {item_label, item_type, item_name, item_with_value} , ... ] """ def set_config_items(self, items): self._config_items = items keys = {} for i in self._config_items: - keys[i["item_name"]] = { "type" : i["item_type"] } + keys[i["item_name"]] = {"type": i["item_type"]} self._valid_keys = keys def set_config_file_obj(self, obj): @@ -28,7 +44,7 @@ class ConfigWizard(): def get_config_file_obj(self, obj): return self._config_file_obj - def show(self, read_from_disc = False): + def show(self, read_from_disc=False): if read_from_disc: self._config_file_obj = ConfigFile(self._config_file_path) @@ -47,8 +63,8 @@ class ConfigWizard(): row = 1 for i in self._config_items: hbox = self._create_param(i) - table.attach(hbox, 0, 1, row, row+1, xpadding=5, ypadding=2) - row = row +1 + table.attach(hbox, 0, 1, row, row + 1, xpadding=5, ypadding=2) + row = row + 1 hbox = gtk.HBox() save_button = gtk.Button('Save') @@ -59,7 +75,7 @@ class ConfigWizard(): cancel_button.set_size_request(50, 15) cancel_button.connect('pressed', self._close_config_cb) hbox.add(cancel_button) - table.attach(hbox, 0, 1, row, row+1, xpadding=5, ypadding=2) + table.attach(hbox, 0, 1, row, row + 1, xpadding=5, ypadding=2) self._config_popup.show_all() @@ -89,11 +105,12 @@ class ConfigWizard(): self._config_file_obj.save() """ - { item_label, item_type, item_name, item_with_value } + {item_label, item_type, item_name, item_with_value} """ def _create_param(self, opts): param_name = opts["item_name"] - with_value = opts["item_with_value"] if opts.has_key("item_with_value") else True + with_value = opts["item_with_value"] if "item_with_value" in opts \ + else True hbox = gtk.HBox() if opts["item_type"] == "text": entry = gtk.Entry() @@ -117,14 +134,15 @@ class ConfigWizard(): def _close_config_cb(self, widget, event=None): self._config_popup.hide() + def test_wizard_from_config_file_obj(test_config_file): keys = {} - keys["nick"] = { "type" : "text" } - keys["account_id"] = { "type" : "text" } - keys["server"] = { "type" : "text" } - keys["port"] = { "type" : "text" } - keys["password"] = { "type" : "text" } - keys["register"] = { "type" : "text" } + keys["nick"] = {"type": "text"} + keys["account_id"] = {"type": "text"} + keys["server"] = {"type": "text"} + keys["port"] = {"type": "text"} + keys["password"] = {"type": "text"} + keys["register"] = {"type": "text"} c = ConfigFile(test_config_file) c.set_valid_keys(keys) @@ -136,32 +154,36 @@ def test_wizard_from_config_file_obj(test_config_file): c.set("register", True) c.save() - + c = ConfigFile(test_config_file) c.set_valid_keys(keys) c.load() config_w = ConfigWizard(test_config_file) config_items = [ - {"item_label" : "Nickname", "item_type" : "text", "item_name" : "nick" }, - { "item_label" : "Account ID", "item_type" : "text", "item_name" : "account_id" }, - { "item_label" : "Server", "item_type" : "text", "item_name" : "server" }, - { "item_label" : "Port", "item_type" : "text", "item_name" : "port" }, - { "item_label" : "Password", "item_type" : "text", "item_name" : "password" }, - { "item_label" : "Register", "item_type" : "text", "item_name" : "register" } + {"item_label": "Nickname", "item_type": "text", "item_name": "nick"}, + {"item_label": "Account ID", "item_type": "text", + "item_name": "account_id"}, + {"item_label": "Server", "item_type": "text", "item_name": "server"}, + {"item_label": "Port", "item_type": "text", "item_name": "port"}, + {"item_label": "Password", "item_type": "text", + "item_name": "password"}, + {"item_label": "Register", "item_type": "text", + "item_name": "register"} ] config_w.set_config_items(config_items) config_w.set_config_file_obj(c) config_w.show() + def test_wizard_from_config_file_path(test_config_file): keys = {} - keys["nick"] = { "type" : "text" } - keys["account_id"] = { "type" : "text" } - keys["server"] = { "type" : "text" } - keys["port"] = { "type" : "text" } - keys["password"] = { "type" : "text" } - keys["register"] = { "type" : "text" } + keys["nick"] = {"type": "text"} + keys["account_id"] = {"type": "text"} + keys["server"] = {"type": "text"} + keys["port"] = {"type": "text"} + keys["password"] = {"type": "text"} + keys["register"] = {"type": "text"} c = ConfigFile(test_config_file) c.set_valid_keys(keys) @@ -173,19 +195,23 @@ def test_wizard_from_config_file_path(test_config_file): c.set("register", True) c.save() - + config_w = ConfigWizard(test_config_file) config_items = [ - {"item_label" : "Nickname", "item_type" : "text", "item_name" : "nick" }, - { "item_label" : "Account ID", "item_type" : "text", "item_name" : "account_id" }, - { "item_label" : "Server", "item_type" : "text", "item_name" : "server" }, - { "item_label" : "Port", "item_type" : "text", "item_name" : "port" }, - { "item_label" : "Password", "item_type" : "text", "item_name" : "password" }, - { "item_label" : "Register", "item_type" : "text", "item_name" : "register" } + {"item_label": "Nickname", "item_type": "text", "item_name": "nick"}, + {"item_label": "Account ID", "item_type": "text", + "item_name": "account_id"}, + {"item_label": "Server", "item_type": "text", "item_name": "server"}, + {"item_label": "Port", "item_type": "text", "item_name": "port"}, + {"item_label": "Password", "item_type": "text", + "item_name": "password"}, + {"item_label": "Register", "item_type": "text", + "item_name": "register"} ] config_w.set_config_items(config_items) config_w.show(True) + if __name__ == "__main__": #test_wizard_from_config_file_obj("/tmp/configwizard.test.0001") test_wizard_from_config_file_path("/tmp/configwizard.test.0002") diff --git a/util/menubuilder.py b/util/menubuilder.py index 302d510..4ee4550 100644 --- a/util/menubuilder.py +++ b/util/menubuilder.py @@ -1,7 +1,23 @@ #!/usr/bin/python +# Copyright (c) 2011 Collabora Ltd. +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. import gtk + class MenuBuilder(): @classmethod def make_sub_menu(cls, menu, name): -- cgit v0.9.1