From a8b5f4c91b12af9c3a2d84b5da99a71f46c6284d Mon Sep 17 00:00:00 2001 From: Vincent Vinet Date: Fri, 27 Mar 2009 16:08:14 +0000 Subject: Add shells for gobject and gtk parsers in UAM, complete tests --- (limited to 'src') diff --git a/src/sugar/tutorius/tests/uamtests.py b/src/sugar/tutorius/tests/uamtests.py index 5bbebd4..b2a5901 100644 --- a/src/sugar/tutorius/tests/uamtests.py +++ b/src/sugar/tutorius/tests/uamtests.py @@ -18,7 +18,7 @@ import unittest -from sugar.tutorius.uam import parse_uri +from sugar.tutorius.uam import parse_uri, SchemeError PARSE_SUITE={ #URI SCHEME HOST PARAMS PATH QUERY FRAGMENT @@ -28,7 +28,9 @@ PARSE_SUITE={ } class ParseUriTests(unittest.TestCase): + """Tests the UAM parsers""" def test_parse_uri(self): + """Test parsing results""" for uri, test in PARSE_SUITE.items(): res = parse_uri(uri) @@ -39,7 +41,21 @@ class ParseUriTests(unittest.TestCase): assert res.query == test[4], "%s : Expected query %s, got %s" % (uri, test[4], res.query) assert res.fragment == test[5], "%s : Expected fragment %s, got %s" % (uri, test[5], res.fragment) - + def test_errors(self): + """Test exceptions""" + try: + parse_uri("http://something.org/path") + assert False, "Parsing http should fail" + except SchemeError: + pass + + try: + parse_uri("tap.notarealsubscheme://something.org/path") + assert False, "Invalid Subscheme should fail" + except SchemeError: + pass + + if __name__ == "__main__": unittest.main() diff --git a/src/sugar/tutorius/uam/Makefile.am b/src/sugar/tutorius/uam/Makefile.am index 83d2119..219291e 100644 --- a/src/sugar/tutorius/uam/Makefile.am +++ b/src/sugar/tutorius/uam/Makefile.am @@ -1,3 +1,5 @@ sugardir = $(pythondir)/sugar/tutorius/uam sugar_PYTHON = \ + gobjectparser.py \ + gtkparser.py \ __init__.py diff --git a/src/sugar/tutorius/uam/__init__.py b/src/sugar/tutorius/uam/__init__.py index b55ae28..7cf5671 100644 --- a/src/sugar/tutorius/uam/__init__.py +++ b/src/sugar/tutorius/uam/__init__.py @@ -15,20 +15,23 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA """ -Universal addressing module +Universal Addressing Mechanism module Allows addressing Events, signals, widgets, etc for supported platforms """ from urllib2 import urlparse -SCHEMA="tap" #Tutorius Adressing Protocol +import gtkparser +import gobjectparser -SUBSCHEMAS = [ - "gtk", - "pygame", - "gobject", -] + +SCHEME="tap" #Tutorius Adressing Protocol + +__parsers = { + gtkparser.SCHEME:gtkparser.parse_gtk, + gobjectparser.SCHEME:gobjectparser.parse_gobject, +} def __add_to_urlparse(name): #Add to uses_netloc @@ -52,12 +55,34 @@ def __add_to_urlparse(name): urlparse.uses_fragment.append(name) -#Add schemas to urlparse -__add_to_urlparse(SCHEMA) +#Add schemes to urlparse +__add_to_urlparse(SCHEME) + +for subscheme in [".".join([SCHEME,s]) for s in __parsers]: + __add_to_urlparse(subscheme) + + +class SchemeError(Exception): + def __init__(self, message): + Exception.__init__(self, message) + self.message = message + -for subschema in [".".join([SCHEMA,s]) for s in SUBSCHEMAS]: - __add_to_urlparse(subschema) +def parse_uri(uri): + res = urlparse.urlparse(uri) + scheme = res.scheme.split(".")[0] + subscheme = ".".join(res.scheme.split(".")[1:]) + if not scheme == SCHEME: + raise SchemeError("Scheme %s not supported" % scheme) + + if subscheme != "" and not subscheme in __parsers: + raise SchemeError("SubScheme %s not supported" % subscheme) + + if subscheme: + return __parsers[subscheme](res) + + return res + + -#For now, just export the urlparse urlparse method -parse_uri = urlparse.urlparse diff --git a/src/sugar/tutorius/uam/gobjectparser.py b/src/sugar/tutorius/uam/gobjectparser.py new file mode 100644 index 0000000..c1fba3d --- /dev/null +++ b/src/sugar/tutorius/uam/gobjectparser.py @@ -0,0 +1,27 @@ +# Copyright (C) 2009, Tutorius.org +# Copyright (C) 2009, Vincent Vinet +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +""" +UAM Parser for gobject subscheme + +To be completed +""" + +SCHEME="gobject" + +def parse_gobject(parsed_uri): + """Do nothing for now""" + return parsed_uri diff --git a/src/sugar/tutorius/uam/gtkparser.py b/src/sugar/tutorius/uam/gtkparser.py new file mode 100644 index 0000000..ede2f03 --- /dev/null +++ b/src/sugar/tutorius/uam/gtkparser.py @@ -0,0 +1,44 @@ +# Copyright (C) 2009, Tutorius.org +# Copyright (C) 2009, Vincent Vinet +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program 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 General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +""" +UAM Parser for gtk subscheme + +Allows addressing Gtk Events, signals, widgets + +The gtk subscheme for tutorius is + +:///[?#] + +where: + + is the uam.SCHEME + "." + SCHEME + + is the activity's dns identifier, such as battleship.tutorius.org + + is the Hierarchical path to the widget, where 0 is the activity, such as /0/0/1/0/1/0 + + can be used to specify additionnal parameters required for an event handler or action, such as event=clicked + + must be used with params to specify which action or eventfilter to use, such as "DialogMessage" + +""" + +SCHEME="gtk" + +def parse_gtk(parsed_uri): + """Do nothing for now""" + return parsed_uri -- cgit v0.9.1