def _trailing(template, *targets):
"""Substring of *template* following all *targets*.
**Example**::
template = "this is a test of the bunnies."
_trailing(template, "is", "est", "the") == " bunnies"
Each target is matched successively in the string, and the string
remaining after the last target is returned. If one of the targets
fails to match, a ValueError is raised.
:param template: Template to extract a trailing string from.
:type template: ``string``
:param targets: Strings to successively match in *template*.
:type targets: list of ``string``s
:return: Trailing string after all targets are matched.
:rtype: ``string``
:raises ValueError: Raised when one of the targets does not match.
"""
s = template
for t in targets:
n = s.find(t)
if n == -1:
raise ValueError("Target " + t + " not found in template.")
s = s[n + len(t):]
return s
# Filter the given state content record according to the given arg list.
python类record()的实例源码
def _filter_content(content, *args):
if len(args) > 0:
return record((k, content[k]) for k in args)
return record((k, v) for k, v in content.iteritems()
if k not in ['eai:acl', 'eai:attributes', 'type'])
# Construct a resource path from the given base path + resource name
def _path(base, name):
if not base.endswith('/'): base = base + '/'
return base + name
# Load an atom record from the body of the given response
def _load_sid(response):
return _load_atom(response).response.sid
# Parse the given atom entry record into a generic entity state record
def _parse_atom_entry(entry):
title = entry.get('title', None)
elink = entry.get('link', [])
elink = elink if isinstance(elink, list) else [elink]
links = record((link.rel, link.href) for link in elink)
# Retrieve entity content values
content = entry.get('content', {})
# Host entry metadata
metadata = _parse_atom_metadata(content)
# Filter some of the noise out of the content record
content = record((k, v) for k, v in content.iteritems()
if k not in ['eai:acl', 'eai:attributes'])
if 'type' in content:
if isinstance(content['type'], list):
content['type'] = [t for t in content['type'] if t != 'text/xml']
# Unset type if it was only 'text/xml'
if len(content['type']) == 0:
content.pop('type', None)
# Flatten 1 element list
if len(content['type']) == 1:
content['type'] = content['type'][0]
else:
content.pop('type', None)
return record({
'title': title,
'links': links,
'access': metadata.access,
'fields': metadata.fields,
'content': content,
'updated': entry.get("updated")
})
# Parse the metadata fields out of the given atom entry content record
def __getitem__(self, key):
# getattr attempts to find a field on the object in the normal way,
# then calls __getattr__ if it cannot.
return getattr(self, key)
# Load the Atom entry record from the given response - this is a method
# because the "entry" record varies slightly by entity and this allows
# for a subclass to override and handle any special cases.
def _load_atom_entry(self, response):
elem = _load_atom(response, XNAME_ENTRY)
if isinstance(elem, list):
raise AmbiguousReferenceException("Fetch from server returned multiple entries for name %s." % self.name)
else:
return elem.entry
# Load the entity state record from the given response
def state(self):
"""Returns the entity's state record.
:return: A ``dict`` containing fields and metadata for the entity.
"""
if self._state is None: self.refresh()
return self._state
def __init__(self, service, sid, **kwargs):
path = PATH_JOBS + sid
Entity.__init__(self, service, path, skip_refresh=True, **kwargs)
self.sid = sid
# The Job entry record is returned at the root of the response
def _trailing(template, *targets):
"""Substring of *template* following all *targets*.
**Example**::
template = "this is a test of the bunnies."
_trailing(template, "is", "est", "the") == " bunnies"
Each target is matched successively in the string, and the string
remaining after the last target is returned. If one of the targets
fails to match, a ValueError is raised.
:param template: Template to extract a trailing string from.
:type template: ``string``
:param targets: Strings to successively match in *template*.
:type targets: list of ``string``s
:return: Trailing string after all targets are matched.
:rtype: ``string``
:raises ValueError: Raised when one of the targets does not match.
"""
s = template
for t in targets:
n = s.find(t)
if n == -1:
raise ValueError("Target " + t + " not found in template.")
s = s[n + len(t):]
return s
# Filter the given state content record according to the given arg list.
def _filter_content(content, *args):
if len(args) > 0:
return record((k, content[k]) for k in args)
return record((k, v) for k, v in content.iteritems()
if k not in ['eai:acl', 'eai:attributes', 'type'])
# Construct a resource path from the given base path + resource name
def _path(base, name):
if not base.endswith('/'): base = base + '/'
return base + name
# Load an atom record from the body of the given response
def _load_sid(response):
return _load_atom(response).response.sid
# Parse the given atom entry record into a generic entity state record
def _parse_atom_entry(entry):
title = entry.get('title', None)
elink = entry.get('link', [])
elink = elink if isinstance(elink, list) else [elink]
links = record((link.rel, link.href) for link in elink)
# Retrieve entity content values
content = entry.get('content', {})
# Host entry metadata
metadata = _parse_atom_metadata(content)
# Filter some of the noise out of the content record
content = record((k, v) for k, v in content.iteritems()
if k not in ['eai:acl', 'eai:attributes'])
if 'type' in content:
if isinstance(content['type'], list):
content['type'] = [t for t in content['type'] if t != 'text/xml']
# Unset type if it was only 'text/xml'
if len(content['type']) == 0:
content.pop('type', None)
# Flatten 1 element list
if len(content['type']) == 1:
content['type'] = content['type'][0]
else:
content.pop('type', None)
return record({
'title': title,
'links': links,
'access': metadata.access,
'fields': metadata.fields,
'content': content,
'updated': entry.get("updated")
})
# Parse the metadata fields out of the given atom entry content record
def __getitem__(self, key):
# getattr attempts to find a field on the object in the normal way,
# then calls __getattr__ if it cannot.
return getattr(self, key)
# Load the Atom entry record from the given response - this is a method
# because the "entry" record varies slightly by entity and this allows
# for a subclass to override and handle any special cases.
def _load_atom_entry(self, response):
elem = _load_atom(response, XNAME_ENTRY)
if isinstance(elem, list):
raise AmbiguousReferenceException("Fetch from server returned multiple entries for name %s." % self.name)
else:
return elem.entry
# Load the entity state record from the given response
def state(self):
"""Returns the entity's state record.
:return: A ``dict`` containing fields and metadata for the entity.
"""
if self._state is None: self.refresh()
return self._state
def __init__(self, service, sid, **kwargs):
path = PATH_JOBS + sid
Entity.__init__(self, service, path, skip_refresh=True, **kwargs)
self.sid = sid
# The Job entry record is returned at the root of the response
def _trailing(template, *targets):
"""Substring of *template* following all *targets*.
**Example**::
template = "this is a test of the bunnies."
_trailing(template, "is", "est", "the") == " bunnies"
Each target is matched successively in the string, and the string
remaining after the last target is returned. If one of the targets
fails to match, a ValueError is raised.
:param template: Template to extract a trailing string from.
:type template: ``string``
:param targets: Strings to successively match in *template*.
:type targets: list of ``string``s
:return: Trailing string after all targets are matched.
:rtype: ``string``
:raises ValueError: Raised when one of the targets does not match.
"""
s = template
for t in targets:
n = s.find(t)
if n == -1:
raise ValueError("Target " + t + " not found in template.")
s = s[n + len(t):]
return s
# Filter the given state content record according to the given arg list.
def _filter_content(content, *args):
if len(args) > 0:
return record((k, content[k]) for k in args)
return record((k, v) for k, v in content.iteritems()
if k not in ['eai:acl', 'eai:attributes', 'type'])
# Construct a resource path from the given base path + resource name
def _path(base, name):
if not base.endswith('/'): base = base + '/'
return base + name
# Load an atom record from the body of the given response
def _load_sid(response):
return _load_atom(response).response.sid
# Parse the given atom entry record into a generic entity state record
def _parse_atom_entry(entry):
title = entry.get('title', None)
elink = entry.get('link', [])
elink = elink if isinstance(elink, list) else [elink]
links = record((link.rel, link.href) for link in elink)
# Retrieve entity content values
content = entry.get('content', {})
# Host entry metadata
metadata = _parse_atom_metadata(content)
# Filter some of the noise out of the content record
content = record((k, v) for k, v in content.iteritems()
if k not in ['eai:acl', 'eai:attributes'])
if 'type' in content:
if isinstance(content['type'], list):
content['type'] = [t for t in content['type'] if t != 'text/xml']
# Unset type if it was only 'text/xml'
if len(content['type']) == 0:
content.pop('type', None)
# Flatten 1 element list
if len(content['type']) == 1:
content['type'] = content['type'][0]
else:
content.pop('type', None)
return record({
'title': title,
'links': links,
'access': metadata.access,
'fields': metadata.fields,
'content': content,
'updated': entry.get("updated")
})
# Parse the metadata fields out of the given atom entry content record
def __getitem__(self, key):
# getattr attempts to find a field on the object in the normal way,
# then calls __getattr__ if it cannot.
return getattr(self, key)
# Load the Atom entry record from the given response - this is a method
# because the "entry" record varies slightly by entity and this allows
# for a subclass to override and handle any special cases.
def _load_atom_entry(self, response):
elem = _load_atom(response, XNAME_ENTRY)
if isinstance(elem, list):
raise AmbiguousReferenceException("Fetch from server returned multiple entries for name %s." % self.name)
else:
return elem.entry
# Load the entity state record from the given response
def state(self):
"""Returns the entity's state record.
:return: A ``dict`` containing fields and metadata for the entity.
"""
if self._state is None: self.refresh()
return self._state
def __init__(self, service, sid, **kwargs):
path = PATH_JOBS + sid
Entity.__init__(self, service, path, skip_refresh=True, **kwargs)
self.sid = sid
# The Job entry record is returned at the root of the response
def _trailing(template, *targets):
"""Substring of *template* following all *targets*.
**Example**::
template = "this is a test of the bunnies."
_trailing(template, "is", "est", "the") == " bunnies"
Each target is matched successively in the string, and the string
remaining after the last target is returned. If one of the targets
fails to match, a ValueError is raised.
:param template: Template to extract a trailing string from.
:type template: ``string``
:param targets: Strings to successively match in *template*.
:type targets: list of ``string``s
:return: Trailing string after all targets are matched.
:rtype: ``string``
:raises ValueError: Raised when one of the targets does not match.
"""
s = template
for t in targets:
n = s.find(t)
if n == -1:
raise ValueError("Target " + t + " not found in template.")
s = s[n + len(t):]
return s
# Filter the given state content record according to the given arg list.
def _filter_content(content, *args):
if len(args) > 0:
return record((k, content[k]) for k in args)
return record((k, v) for k, v in content.iteritems()
if k not in ['eai:acl', 'eai:attributes', 'type'])
# Construct a resource path from the given base path + resource name
def _path(base, name):
if not base.endswith('/'): base = base + '/'
return base + name
# Load an atom record from the body of the given response