def parse_locations(self, body, headers): """Parse OCCI Entity collection :param string body[]: text to parse :param string headers[]: headers to parse :return: array of renderer-specific strings :rtype: string[] """ locations = [] for uri in body: if not check_url(uri, scheme=True, host=True): raise occi.ParseError('Invalid URI in OCCI Entity collection', uri) locations.append(uri) return locations
def parse_actions(self, body): """Parse OCCI Actions. Example:: http://schemas.ogf.org/occi/infrastructure/compute/action#start http://schemas.ogf.org/occi/infrastructure/compute/action#stop http://schemas.ogf.org/occi/infrastructure/compute/action#restart http://schemas.ogf.org/occi/infrastructure/compute/action#suspend :param string body: text to parse :return: array of string :rtype: string[] """ actions = TextRenderer.reSP.split(body) for action in actions: # let's require scheme and hostname in scheme URI if not check_url(action, scheme=True, host=True): raise occi.ParseError('URI expected as an action', action) return actions
def parse_locations(self, body, headers): """Parse OCCI Entity collection :param string body[]: text to parse :param string headers[]: headers to parse (unused in text/plain) :return: Array of links :rtype: string[] """ locations = [] for line in body: matched = TextRenderer.reLocation.match(line) if not matched: raise occi.ParseError("OCCI Location expected in OCCI Entity collection", line) uri = matched.group(2) if not check_url(uri, scheme=True, host=True): raise occi.ParseError("Invalid URI in OCCI Entity collection", line) locations.append(uri) return locations
def parse_locations(self, body, headers): """Parse OCCI Entity collection :param string body[]: text to parse :param string headers[]: headers to parse (unused in text/plain) :return: Array of links :rtype: string[] """ locations = [] for line in body: if not line.strip(): continue matched = TextRenderer.reLocation.match(line) if not matched: raise occi.ParseError( 'OCCI Location expected in OCCI Entity collection', line) uri = matched.group(2) if not check_url(uri, scheme=True, host=True): raise occi.ParseError('Invalid URI in OCCI Entity collection', line) locations.append(uri) return locations
def parse_link_body(self, body): """Parse OCCI Link body Example:: </storage/0>;rel="http://schemas.ogf.org/occi/infrastructure#storage";self="/link/storagelink/compute_103_disk_0";category="http://schemas.ogf.org/occi/infrastructure#storagelink http://opennebula.org/occi/infrastructure#storagelink";occi.core.id="compute_103_disk_0";occi.core.title="ttylinux";occi.core.target="/storage/0";occi.core.source="/compute/103";occi.storagelink.deviceid="/dev/hda";occi.storagelink.state="active" :param string body: text to parse :return: OCCI Link :rtype: occi.Link """ link = occi.Link() chunks = TextRenderer.reChunks.split(body) if not chunks[0]: raise occi.ParseError('Invalid format of OCCI Link, URI and "rel" expected', body) matched = TextRenderer.reQuotedLink.match(chunks[0]) if not matched: raise occi.ParseError('URI is not properly quoted in OCCI Link', body) link['uri'] = matched.group(1) if not check_url(link['uri']): raise occi.ParseError('URL is not valid in OCCI Link', link['uri']) # skip the first chunk (URI) for chunk in chunks[1:]: keyvalue = TextRenderer.reKeyValue.split(chunk, 1) key = keyvalue[0] value = keyvalue[1] keymatch = TextRenderer.reKeyCheck.match(key) if keymatch is None: raise occi.ParseError('Invalid characters in link property', chunk) valuematch = TextRenderer.reQuoted.match(value) # mandatory quoting if key in ['rel', 'self', 'category']: if valuematch is None: raise occi.ParseError('Link value not properly quoted or unexpected EOF', chunk) # quoting of the other attributes optional if valuematch is not None: value = valuematch.group(1) # sanity check: there should not be any quotes now if value[0] == '"' or (len(value) >= 2 and value[-1] == '"'): raise occi.ParseError('Unexpected quotes in OCCI Link values', chunk) if key == 'scheme': if not check_url(value): raise occi.ParseError('URL is not valid in OCCI Category scheme', chunk) link[key] = value elif key in ['rel', 'category']: link[key] = TextRenderer.reSP.split(value) elif key in ['self']: link[key] = value else: if 'attributes' not in link: link['attributes'] = collections.OrderedDict() link['attributes'][key] = value if not link.validate(): raise occi.ParseError('Missing fields in OCCI Link', body) return link
def parse_category_body(self, body): """Parse OCCI Category body Example:: entity;scheme="http://schemas.ogf.org/occi/core#";class="kind";title="entity";location="/entity/";attributes="occi.core.id{immutable required} occi.core.title" :param string body: text to parse :return: OCCI Category :rtype: occi.Category """ category = occi.Category() chunks = TextRenderer.reChunks.split(body) if not chunks[0]: raise occi.ParseError('Invalid format of category, term expected', body) category['term'] = chunks[0] # skip the first chunk (category term) for chunk in chunks[1:]: keyvalue = TextRenderer.reKeyValue.split(chunk, 1) if len(keyvalue) != 2: raise occi.ParseError('Key/value pair expected in category', chunk) key = keyvalue[0] value = keyvalue[1] keymatch = TextRenderer.reKeyCheck.match(key) if keymatch is None: raise occi.ParseError('Invalid characters in category property', chunk) # every value quoted, only class has quoting optional valuematch = TextRenderer.reQuoted.match(value) if valuematch is None and key != 'class': raise occi.ParseError('Category value not properly quoted or unexpected EOF', chunk) if valuematch: value = valuematch.group(1) # sanity check: there should not be any quotes now if value[0] == '"' or (len(value) >= 2 and value[-1] == '"'): raise occi.ParseError('Unexpected quotes in category', chunk) if key == 'location': if not check_url(value): raise occi.ParseError('URL is not valid in OCCI Category location', chunk) category[key] = value elif key == 'scheme': if not check_url(value): raise occi.ParseError('URL is not valid in OCCI Category scheme', chunk) category[key] = value elif key == 'attributes': category[key] = self.parse_attribute_defs(value) elif key == 'actions': category[key] = self.parse_actions(value) elif key in ['class', 'title', 'rel']: category[key] = value else: raise occi.ParseError('Unknown key "%s" in category' % key, chunk) if not category.validate(): raise occi.ParseError('Missing fields in OCCI Category', body) return category
def parse_link_body(self, body): """Parse OCCI Link body Example:: </storage/0>;rel="http://schemas.ogf.org/occi/infrastructure#storage";self="/link/storagelink/compute_103_disk_0";category="http://schemas.ogf.org/occi/infrastructure#storagelink http://opennebula.org/occi/infrastructure#storagelink";occi.core.id="compute_103_disk_0";occi.core.title="ttylinux";occi.core.target="/storage/0";occi.core.source="/compute/103";occi.storagelink.deviceid="/dev/hda";occi.storagelink.state="active" :param string body: text to parse :return: OCCI Link :rtype: occi.Link """ link = occi.Link() chunks = TextRenderer.reChunks.split(body) if not chunks[0]: raise occi.ParseError( 'Invalid format of OCCI Link, URI and "rel" expected', body) matched = TextRenderer.reQuotedLink.match(chunks[0]) if not matched: raise occi.ParseError('URI is not properly quoted in OCCI Link', body) link['uri'] = matched.group(1) if not check_url(link['uri']): raise occi.ParseError('URL is not valid in OCCI Link', link['uri']) # skip the first chunk (URI) for chunk in chunks[1:]: keyvalue = TextRenderer.reKeyValue.split(chunk, 1) key = keyvalue[0] value = keyvalue[1] keymatch = TextRenderer.reKeyCheck.match(key) if keymatch is None: raise occi.ParseError('Invalid characters in link property', chunk) valuematch = TextRenderer.reQuoted.match(value) # mandatory quoting if key in ['rel', 'self', 'category']: if valuematch is None: raise occi.ParseError( 'Link value not properly quoted or unexpected EOF', chunk) # quoting of the other attributes optional if valuematch is not None: value = valuematch.group(1) # sanity check: there should not be any quotes now if value[0] == '"' or (len(value) >= 2 and value[-1] == '"'): raise occi.ParseError('Unexpected quotes in OCCI Link values', chunk) if key == 'scheme': if not check_url(value): raise occi.ParseError( 'URL is not valid in OCCI Category scheme', chunk) link[key] = value elif key in ['rel', 'category']: link[key] = TextRenderer.reSP.split(value) elif key in ['self']: link[key] = value else: if 'attributes' not in link: link['attributes'] = collections.OrderedDict() link['attributes'][key] = value if not link.validate(): raise occi.ParseError('Missing fields in OCCI Link', body) return link
def parse_category_body(self, body): """Parse OCCI Category body Example:: entity;scheme="http://schemas.ogf.org/occi/core#";class="kind";title="entity";location="/entity/";attributes="occi.core.id{immutable required} occi.core.title" :param string body: text to parse :return: OCCI Category :rtype: occi.Category """ category = occi.Category() chunks = TextRenderer.reChunks.split(body) if not chunks[0]: raise occi.ParseError('Invalid format of category, term expected', body) category['term'] = chunks[0] # skip the first chunk (category term) for chunk in chunks[1:]: keyvalue = TextRenderer.reKeyValue.split(chunk, 1) if len(keyvalue) != 2: raise occi.ParseError('Key/value pair expected in category', chunk) key = keyvalue[0] value = keyvalue[1] keymatch = TextRenderer.reKeyCheck.match(key) if keymatch is None: raise occi.ParseError( 'Invalid characters in category property', chunk) # every value quoted, only class has quoting optional valuematch = TextRenderer.reQuoted.match(value) if valuematch is None and key != 'class': raise occi.ParseError( 'Category value not properly quoted or unexpected EOF', chunk) if valuematch: value = valuematch.group(1) # sanity check: there should not be any quotes now if value[0] == '"' or (len(value) >= 2 and value[-1] == '"'): raise occi.ParseError('Unexpected quotes in category', chunk) if key == 'location': if not check_url(value): raise occi.ParseError( 'URL is not valid in OCCI Category location', chunk) category[key] = value elif key == 'scheme': if not check_url(value): raise occi.ParseError( 'URL is not valid in OCCI Category scheme', chunk) category[key] = value elif key == 'attributes': category[key] = self.parse_attribute_defs(value) elif key == 'actions': category[key] = self.parse_actions(value) elif key in ['class', 'title', 'rel']: category[key] = value else: raise occi.ParseError('Unknown key "%s" in category' % key, chunk) if not category.validate(): raise occi.ParseError('Missing fields in OCCI Category', body) return category