Added TextParser.

This commit is contained in:
Enol Fernandez 2015-04-14 16:33:08 +02:00
parent fa2b1c809f
commit 8a336a5407
1 changed files with 30 additions and 14 deletions

View File

@ -91,10 +91,6 @@ class BaseParser(object):
return True return True
class TextParser(BaseParser):
pass
def _lexize(s, separator, ignore_whitespace=False): def _lexize(s, separator, ignore_whitespace=False):
lex = shlex.shlex(instream=s, posix=True) lex = shlex.shlex(instream=s, posix=True)
lex.commenters = "" lex.commenters = ""
@ -106,16 +102,20 @@ def _lexize(s, separator, ignore_whitespace=False):
return list(lex) return list(lex)
class HeaderParser(BaseParser): def _lexise_header(s):
def parse_categories(self): return _lexize(s, separator=",", ignore_whitespace=True)
class TextParser(BaseParser):
def parse_categories(self, headers):
kind = None kind = None
mixins = collections.Counter() mixins = collections.Counter()
schemes = collections.defaultdict(list) schemes = collections.defaultdict(list)
try: try:
categories = self.headers["Category"] categories = headers["Category"]
except KeyError: except KeyError:
raise exception.OCCIInvalidSchema("No categories") raise exception.OCCIInvalidSchema("No categories")
for ctg in _lexize(categories, separator=",", ignore_whitespace=True): for ctg in _lexise_header(categories):
ll = _lexize(ctg, ";") ll = _lexize(ctg, ";")
d = {"term": ll[0]} # assumes 1st element => term's value d = {"term": ll[0]} # assumes 1st element => term's value
d.update(dict([i.split('=') for i in ll[1:]])) d.update(dict([i.split('=') for i in ll[1:]]))
@ -134,21 +134,37 @@ class HeaderParser(BaseParser):
"schemes": schemes, "schemes": schemes,
} }
def parse_attributes(self): def parse_attributes(self, headers):
attrs = {} attrs = {}
try: try:
header_attrs = self.headers["X-OCCI-Attribute"] header_attrs = headers["X-OCCI-Attribute"]
for attr in _lexize(header_attrs, separator=",", for attr in _lexise_header(header_attrs):
ignore_whitespace=True):
n, v = attr.split('=', 1) n, v = attr.split('=', 1)
attrs[n.strip()] = v attrs[n.strip()] = v
except KeyError: except KeyError:
pass pass
return attrs return attrs
def _convert_to_headers(self):
if not self.body:
raise exception.OCCIInvalidSchema("No schema found")
hdrs = collections.defaultdict(list)
for l in self.body.splitlines():
hdr, content = l.split(":", 1)
hdrs[hdr].append(content)
return {hdr: ','.join(hdrs[hdr]) for hdr in hdrs}
def parse(self): def parse(self):
obj = self.parse_categories() body_headers = self._convert_to_headers()
obj['attributes'] = self.parse_attributes() obj = self.parse_categories(body_headers)
obj['attributes'] = self.parse_attributes(body_headers)
return obj
class HeaderParser(TextParser):
def parse(self):
obj = self.parse_categories(self.headers)
obj['attributes'] = self.parse_attributes(self.headers)
return obj return obj