diff options
author | Walter Bender <walter.bender@gmail.com> | 2011-11-30 02:39:14 (GMT) |
---|---|---|
committer | Walter Bender <walter.bender@gmail.com> | 2011-11-30 02:39:14 (GMT) |
commit | ce985f26c7cc732026f175ca95ac46be2d4059f9 (patch) | |
tree | c4ca9007824b38fe5e023bc87113b86499e6a3d0 /utils.py |
new project
Diffstat (limited to 'utils.py')
-rw-r--r-- | utils.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..d2256b6 --- /dev/null +++ b/utils.py @@ -0,0 +1,63 @@ +#Copyright (c) 2011 Walter Bender + +# 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 3 of the License, or +# (at your option) any later version. +# +# You should have received a copy of the GNU General Public License +# along with this library; if not, write to the Free Software +# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA + + +import gtk +from StringIO import StringIO +try: + OLD_SUGAR_SYSTEM = False + import json + json.dumps + from json import load as jload + from json import dump as jdump +except (ImportError, AttributeError): + try: + import simplejson as json + from simplejson import load as jload + from simplejson import dump as jdump + except: + OLD_SUGAR_SYSTEM = True + + +def json_load(text): + """ Load JSON data using what ever resources are available. """ + if OLD_SUGAR_SYSTEM is True: + listdata = json.read(text) + else: + # strip out leading and trailing whitespace, nulls, and newlines + io = StringIO(text) + try: + listdata = jload(io) + except ValueError: + # assume that text is ascii list + listdata = text.split() + for i, value in enumerate(listdata): + listdata[i] = int(value) + return listdata + + +def json_dump(data): + """ Save data using available JSON tools. """ + if OLD_SUGAR_SYSTEM is True: + return json.write(data) + else: + _io = StringIO() + jdump(data, _io) + return _io.getvalue() + + +def svg_str_to_pixbuf(svg_string): + ''' Load pixbuf from SVG string ''' + pl = gtk.gdk.PixbufLoader('svg') + pl.write(svg_string) + pl.close() + pixbuf = pl.get_pixbuf() + return pixbuf |