forked from Nanonid/rison
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
421 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
dist | ||
*.pyc | ||
|
||
.settings | ||
.DS_Store | ||
|
||
lib-cov | ||
*.seed | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2010 Oliver Steele, nix, Stijn Debrouwere | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
python-rison is, not surprisingly, a Rison parser for Python. | ||
|
||
Rison is a data serialization format optimized for compactness in URIs. Rison is a | ||
slight variation of JSON that looks vastly superior after URI encoding. Rison still | ||
expresses exactly the same set of data structures as JSON, so data can be translated | ||
back and forth without loss or guesswork. | ||
|
||
See http://mjtemplate.org/examples/rison.html to learn more. | ||
|
||
Rison URLs look like this: | ||
|
||
http://example.org/example-resource?a=(key:val,key2:val2)&b=!(a,list,of,values) | ||
|
||
O-Rison and A-Rison shortcuts make these URLs cleaner still. If you know beforehand which | ||
data type an argument will receive, you can use O-Rison for objects and A-Rison for arrays, | ||
and leave off the () and !() syntax respectively: | ||
|
||
http://example.org/example-resource?a=key:val,key2:val2&b=a,list,of,values | ||
|
||
Current version may not work with unicode URLs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Metadata-Version: 1.0 | ||
Name: rison | ||
Version: 1.1 | ||
Summary: A Rison parser | ||
Home-page: UNKNOWN | ||
Author: Stijn Debrouwere | ||
Author-email: [email protected] | ||
License: MIT | ||
Download-URL: http://www.github.com/stdbrouw/rison/tarball/master | ||
Description: python-rison is, not surprisingly, a Rison parser for Python. | ||
|
||
Rison is a data serialization format optimized for compactness in URIs. Rison is a | ||
slight variation of JSON that looks vastly superior after URI encoding. Rison still | ||
expresses exactly the same set of data structures as JSON, so data can be translated | ||
back and forth without loss or guesswork. | ||
|
||
See http://mjtemplate.org/examples/rison.html to learn more. | ||
|
||
Rison URLs look like this: | ||
|
||
http://example.org/example-resource?a=(key:val,key2:val2)&b=!(a,list,of,values) | ||
|
||
O-Rison and A-Rison shortcuts make these URLs cleaner still. If you know beforehand which | ||
data type an argument will receive, you can use O-Rison for objects and A-Rison for arrays, | ||
and leave off the () and !() syntax respectively: | ||
|
||
http://example.org/example-resource?a=key:val,key2:val2&b=a,list,of,values | ||
|
||
Current version may not work with unicode URLs. | ||
Keywords: json serialization uri url | ||
Platform: UNKNOWN | ||
Classifier: Development Status :: 5 - Production/Stable | ||
Classifier: Environment :: Web Environment | ||
Classifier: Intended Audience :: Developers | ||
Classifier: License :: OSI Approved :: MIT License | ||
Classifier: Operating System :: OS Independent | ||
Classifier: Programming Language :: Python | ||
Classifier: Topic :: Software Development :: Libraries :: Python Modules | ||
Classifier: Topic :: Utilities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
README | ||
setup.py | ||
rison/__init__.py | ||
rison/parser.py | ||
rison/tests.py | ||
rison.egg-info/PKG-INFO | ||
rison.egg-info/SOURCES.txt | ||
rison.egg-info/dependency_links.txt | ||
rison.egg-info/top_level.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rison |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.pyc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# encoding: utf-8 | ||
|
||
VERSION = (1, 1, 0) | ||
|
||
from parser import ParserException, Parser, loads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
# encoding: utf-8 | ||
|
||
import os, sys, re | ||
|
||
class ParserException(Exception): | ||
pass | ||
|
||
class Parser(object): | ||
WHITESPACE = '' | ||
#WHITESPACE = " \t\n\r\f" | ||
|
||
# we divide the uri-safe glyphs into three sets | ||
# <rison> and <reserved> classes are illegal in ids. | ||
# <rison> - used by rison (possibly later) | ||
# <reserved> - not common in strings, reserved | ||
#not_idchar = "'!=:(),*@$;&"; | ||
|
||
idchar_punctuation = '_-./~' | ||
not_idchar = ''.join([c for c in (chr(i) for i in range(127)) | ||
if not (c.isalnum() | ||
or c in idchar_punctuation)]) | ||
|
||
# additionally, we need to distinguish ids and numbers by first char | ||
not_idstart = "-0123456789"; | ||
|
||
# regexp string matching a valid id | ||
idrx = ('[^' + not_idstart + not_idchar + | ||
'][^' + not_idchar + ']*') | ||
|
||
# regexp to check for valid rison ids | ||
id_ok_re = re.compile('^' + idrx + '$', re.M) | ||
|
||
# regexp to find the end of an id when parsing | ||
next_id_re = re.compile(idrx, re.M) | ||
|
||
""" | ||
This parser supports RISON, RISON-A and RISON-O. | ||
""" | ||
def parse(self, string, format=str): | ||
if format in [list, 'A']: | ||
self.string = "!({0})".format(string) | ||
elif format in [dict, 'O']: | ||
self.string = "({0})".format(string) | ||
elif format is str: | ||
self.string = string | ||
else: | ||
raise ValueError("""Parse format should be one of str, list, dict, | ||
'A' (alias for list), '0' (alias for dict).""") | ||
|
||
self.index = 0 | ||
|
||
value = self.readValue() | ||
if self.next(): | ||
raise ParserException("unable to parse rison string %r" % (string,)) | ||
return value | ||
|
||
def readValue(self): | ||
c = self.next() | ||
|
||
if c == '!': | ||
return self.parse_bang() | ||
if c == '(': | ||
return self.parse_open_paren() | ||
if c == "'": | ||
return self.parse_single_quote() | ||
if c in '-0123456789': | ||
return self.parse_number() | ||
|
||
# fell through table, parse as an id | ||
s = self.string | ||
i = self.index-1 | ||
|
||
m = self.next_id_re.match(s, i) | ||
if m: | ||
id = m.group(0) | ||
self.index = i + len(id) | ||
return id # a string | ||
|
||
if c: | ||
raise ParserException("invalid character: '" + c + "'") | ||
raise ParserException("empty expression") | ||
|
||
def parse_array(self): | ||
ar = [] | ||
while 1: | ||
c = self.next() | ||
if c == ')': | ||
return ar | ||
|
||
if c is None: | ||
raise ParserException("unmatched '!('") | ||
|
||
if len(ar): | ||
if c != ',': | ||
raise ParserException("missing ','") | ||
elif c == ',': | ||
raise ParserException("extra ','") | ||
else: | ||
self.index -= 1 | ||
n = self.readValue() | ||
ar.append(n) | ||
|
||
return ar | ||
|
||
def parse_bang (self): | ||
s = self.string | ||
c = s[self.index] | ||
self.index += 1 | ||
if c is None: | ||
raise ParserException('"!" at end of input') | ||
if c not in self.bangs: | ||
raise ParserException('unknown literal: "!' + c + '"') | ||
x = self.bangs[c] | ||
if callable(x): | ||
return x(self) | ||
|
||
return x | ||
|
||
|
||
def parse_open_paren (self): | ||
count = 0 | ||
o = {} | ||
|
||
while 1: | ||
c = self.next() | ||
if c == ')': | ||
return o | ||
if count: | ||
if c != ',': | ||
raise ParserException("missing ','") | ||
elif c == ',': | ||
raise ParserException("extra ','") | ||
else: | ||
self.index -= 1 | ||
k = self.readValue() | ||
|
||
if self.next() != ':': | ||
raise ParserException("missing ':'") | ||
v = self.readValue() | ||
|
||
o[k] = v | ||
count += 1 | ||
|
||
|
||
def parse_single_quote(self): | ||
s = self.string | ||
i = self.index | ||
start = i | ||
segments = [] | ||
|
||
while 1: | ||
if i >= len(s): | ||
raise ParserException('unmatched "\'"') | ||
|
||
c = s[i] | ||
i += 1 | ||
if c == "'": | ||
break | ||
|
||
if c == '!': | ||
if start < i-1: | ||
segments.append(s[start:i-1]) | ||
c = s[i] | ||
i += 1 | ||
if c in "!'": | ||
segments.append(c) | ||
else: | ||
raise ParserException('invalid string escape: "!'+c+'"') | ||
|
||
start = i | ||
|
||
|
||
if start < i-1: | ||
segments.append(s[start:i-1]) | ||
self.index = i | ||
return ''.join(segments) | ||
|
||
|
||
# Also any number start (digit or '-') | ||
def parse_number(self): | ||
s = self.string | ||
i = self.index | ||
start = i-1 | ||
state = 'int' | ||
permittedSigns = '-' | ||
transitions = { | ||
'int+.': 'frac', | ||
'int+e': 'exp', | ||
'frac+e': 'exp' | ||
} | ||
|
||
while 1: | ||
if i >= len(s): | ||
i += 1 | ||
break | ||
|
||
c = s[i] | ||
i += 1 | ||
|
||
if '0' <= c and c <= '9': | ||
continue | ||
|
||
if permittedSigns.find(c) >= 0: | ||
permittedSigns = '' | ||
continue | ||
|
||
state = transitions.get(state + '+' + c.lower(), None) | ||
if state is None: | ||
break | ||
if state == 'exp': | ||
permittedSigns = '-' | ||
|
||
self.index = i - 1 | ||
s = s[start:self.index] | ||
if s == '-': | ||
raise ParserException("invalid number") | ||
if re.search('[.e]', s): | ||
return float(s) | ||
return int(s) | ||
|
||
# return the next non-whitespace character, or undefined | ||
def next(self): | ||
l = len(self.string) | ||
s = self.string | ||
i = self.index | ||
|
||
while 1: | ||
if i == len(s): | ||
return None | ||
c = s[i] | ||
i += 1 | ||
if c not in self.WHITESPACE: | ||
break | ||
|
||
self.index = i | ||
return c | ||
|
||
|
||
bangs = { | ||
't': True, | ||
'f': False, | ||
'n': None, | ||
'(': parse_array | ||
} | ||
|
||
def loads(s, format=str): | ||
return Parser().parse(s, format=format) |
Oops, something went wrong.