本文整理汇总了Python中uritemplate.expand函数的典型用法代码示例。如果您正苦于以下问题:Python expand函数的具体用法?Python expand怎么用?Python expand使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了expand函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_HALNavigator__getitem_gauntlet
def test_HALNavigator__getitem_gauntlet():
with httprettify():
index_uri = 'http://www.example.com/'
index_regex = re.compile(index_uri + '.*')
template_href = 'http://www.example.com/{?max,page}'
index_links = {'first': {
'href': template_href,
'templated': True
}}
register_hal(index_regex, index_links)
N = HN.HALNavigator(index_uri)
expanded_nav = N['first', 'page':0, 'max':1]
assert expanded_nav.uri == uritemplate.expand(template_href,
{'max': 1, 'page': '0'})
assert N['first'].expand(page=0, max=1) == expanded_nav
assert N['first']['page': 0].uri == uritemplate \
.expand(template_href, {'page': '0'})
assert N['first', :].uri == uritemplate.expand(
template_href, variables={})
first_page_expanded = uritemplate.expand(template_href, {'page': '0'})
first_null_expanded = uritemplate.expand(template_href, {})
first_both_expanded = uritemplate.expand(
template_href, {'page': '0', 'max': 4})
# (somewhat) exhaustive combinations
N_first = N['first']
with pytest.raises(TypeError):
assert N['page': 0]
assert N_first['page':0].uri == first_page_expanded
assert N[...].uri == N.uri
with pytest.raises(TypeError):
assert N['page': 0, ...]
assert N_first['page':0, ...].uri == first_page_expanded
assert N_first['page':0, ...].templated
with pytest.raises(TypeError):
assert N[:]
assert N_first[:].uri == first_null_expanded
with pytest.raises(TypeError):
assert N['page':0, :]
assert N_first['page':0, :].uri == first_page_expanded
assert not N_first['page':0, :].templated
with pytest.raises(SyntaxError):
assert N[:, ...]
with pytest.raises(SyntaxError):
assert N['page':0, :, ...]
assert N['first'].template_uri == template_href
assert N['first', 'page': 0].uri == first_page_expanded
assert N['first', ...].template_uri == template_href
assert N['first', 'page':0, ...].template_uri == template_href
assert N['first', 'page':0, ...].templated
assert N['first', 'page':0, ...]['max': 4].uri == first_both_expanded
assert N['first', :].uri == first_null_expanded
assert not N['first', :].templated
assert N['first', 'page':0, :].uri == first_page_expanded
assert not N['first', 'page':0, :].templated
with pytest.raises(SyntaxError):
assert N['first', :, ...]
with pytest.raises(SyntaxError):
assert N['first', 'page': 0, :, ...]
示例2: get_touched_branch_files
def get_touched_branch_files(payload, github_auth):
''' Return a set of files modified between master and payload head.
'''
branch_sha = payload['head_commit']['id']
compare1_url = payload['repository']['compare_url']
compare1_url = expand(compare1_url, dict(base='master', head=branch_sha))
current_app.logger.debug('Compare URL 1 {}'.format(compare1_url))
compare1 = get(compare1_url, auth=github_auth).json()
merge_base_sha = compare1['merge_base_commit']['sha']
# That's no branch.
if merge_base_sha == branch_sha:
return set()
compare2_url = payload['repository']['compare_url']
compare2_url = expand(compare2_url, dict(base=merge_base_sha, head=branch_sha))
current_app.logger.debug('Compare URL 2 {}'.format(compare2_url))
compare2 = get(compare2_url, auth=github_auth).json()
touched = set([file['filename'] for file in compare2['files']])
current_app.logger.debug('Touched files {}'.format(', '.join(touched)))
return touched
示例3: __init__
def __init__(self, bb, mode='fetch', **kwargs):
self.bb = bb
if mode:
self.url = [uritemplate.expand(x, **kwargs) for x in self.uri]
self.data = {}
if mode == 'fetch':
for arg in kwargs:
setattr(self, arg, kwargs[arg])
self.data = {}
for url in self.url:
self.data.update(self.get(url))
elif mode == 'list':
self.instances = []
for instance in self.get(self.url[0]):
kw = kwargs.copy()
kw.update(instance)
instance = type(self)(self.bb, mode=None, **kw)
instance.url = [uritemplate.expand(x, **kw) for x in instance.uri]
self.instances.append(instance)
else:
self.data = kwargs
for datum in self.data:
if datum == 'data':
setattr(self, '_' + datum, self.data[datum])
else:
setattr(self, datum, self.data[datum])
示例4: get_route_from_fk
def get_route_from_fk(resource_type, pk=None):
"""Gets a fully qualified URL for a given resource_type, pk"""
routes = requests.get(zc_settings.GATEWAY_ROOT_PATH).json()
for route in routes.iterkeys():
if 'resource_type' in routes[route] and routes[route]['resource_type'] == resource_type:
if isinstance(pk, (list, set)):
expanded = '{}?filter[id__in]={}'.format(expand(route, {}), ','.join([str(x) for x in pk]))
else:
expanded = expand(route, {'id': pk})
return '{0}{1}'.format(routes[route]['domain'], expanded)
raise RouteNotFoundException('No route for resource_type: "{0}"'.format(resource_type))
示例5: build
def build(serviceName, version,
http=None,
discoveryServiceUrl=DISCOVERY_URI,
developerKey=None,
model=JsonModel(),
requestBuilder=HttpRequest):
params = {
'api': serviceName,
'apiVersion': version
}
if http is None:
http = httplib2.Http()
requested_url = uritemplate.expand(discoveryServiceUrl, params)
logging.info('URL being requested: %s' % requested_url)
resp, content = http.request(requested_url)
service = simplejson.loads(content)
fn = os.path.join(os.path.dirname(__file__), "contrib",
serviceName, "future.json")
try:
f = file(fn, "r")
d = simplejson.load(f)
f.close()
future = d['resources']
auth_discovery = d['auth']
except IOError:
future = {}
auth_discovery = {}
base = urlparse.urljoin(discoveryServiceUrl, service['restBasePath'])
resources = service['resources']
class Service(object):
"""Top level interface for a service"""
def __init__(self, http=http):
self._http = http
self._baseUrl = base
self._model = model
self._developerKey = developerKey
self._requestBuilder = requestBuilder
def auth_discovery(self):
return auth_discovery
def createMethod(theclass, methodName, methodDesc, futureDesc):
def method(self):
return createResource(self._http, self._baseUrl, self._model,
self._requestBuilder, methodName,
self._developerKey, methodDesc, futureDesc)
setattr(method, '__doc__', 'A description of how to use this function')
setattr(method, '__is_resource__', True)
setattr(theclass, methodName, method)
for methodName, methodDesc in resources.iteritems():
createMethod(Service, methodName, methodDesc, future.get(methodName, {}))
return Service()
示例6: __get_discovery_doc
def __get_discovery_doc(self, service_name, version, http=None,
discovery_service_url=DISCOVERY_URI):
# Returned a cached copy if we have it.
cached = memcache.get("discovery_doc")
if cached:
return cached
logging.info("Cache miss in discovery document.")
params = {'api': service_name, 'apiVersion': version}
requested_url = uritemplate.expand(discovery_service_url, params)
# REMOTE_ADDR is defined by the CGI spec [RFC3875] as the environment
# variable that contains the network address of the client sending the
# request. If it exists then add that to the request for the discovery
# document to avoid exceeding the quota on discovery requests.
if 'REMOTE_ADDR' in os.environ:
requested_url = _add_query_parameter(requested_url, 'userIp',
os.environ['REMOTE_ADDR'])
http = http or httplib2.Http()
resp, content = http.request(requested_url)
if resp.status >= 400:
raise HttpError(resp, content, uri=requested_url)
# Store it in the memcache.
memcache.set("discovery_doc", content, time=60 * 60 * 24)
return content
示例7: widget
def widget():
"""
Finds issues based on the given label. Render them in the widget
"""
# Get optional parameters
labels = request.args.get("labels", None)
org_name = request.args.get("organization_name")
org_type = request.args.get("org_type")
number = request.args.get("number")
tracking_status = request.args.get("tracking")
# Build the url
if org_name and labels:
issues_path_template = "organizations{/org_name}/issues/labels{/labels}{?query*}"
elif org_name:
issues_path_template = "organizations{/org_name}/issues{?query*}"
elif labels:
issues_path_template = "issues/labels{/labels}{?query*}"
else:
issues_path_template = "issues{?query*}"
issues_url_template = urljoin(CFAPI_BASE, issues_path_template)
issues_url_kwargs = ("organization_type", org_type), ("per_page", number)
url_args = dict(org_name=org_name, labels=labels, query={k: v for (k, v) in issues_url_kwargs if v})
issues_url = expand(issues_url_template, url_args)
# Get the actual issues from the API
try:
issues_response = get(issues_url, timeout=5)
except Timeout:
return render_template("widget.html", error=True)
except ConnectionError, e:
return render_template("widget.html", error=True)
示例8: main
def main():
'''Main logic'''
import argparse
parser = argparse.ArgumentParser('Create GitHub relase')
parser.add_argument('--token', '-t', required=True,
help='GitHub authentication token')
parser.add_argument('--user', '-u', required=True,
help='GitHub user account')
parser.add_argument('--repo', '-r', required=True,
help='GitHub repo name')
parser.add_argument('--version', '-v', required=True,
help='Version to create')
parser.add_argument('--file', '-f',
help='File to upload to release')
parser.add_argument('--content-type', '-c',
help='Content type of file')
args = parser.parse_args()
api_url = 'https://api.github.com/repos/{user}/{repo}/releases'
api_url = api_url.format(user=args.user, repo=args.repo)
# Create release
release_json = create_release(api_url, args.token, args.version)
# Upload file
if args.file:
upload_url = expand(release_json['upload_url'],
{'name': os.path.basename(args.file)})
upload_file(upload_url, args.token, args.file, args.content_type)
示例9: RelativeName
def RelativeName(self, url_escape=False):
"""Relative resource name.
A URI path ([path-noscheme](http://tools.ietf.org/html/rfc3986#appendix-A))
without the leading "/". It identifies a resource within the API service.
For example:
"shelves/shelf1/books/book2"
Args:
url_escape: bool, if true would url escape each parameter.
Returns:
Unescaped part of SelfLink which is essentially base_url + relative_name.
For example if SelfLink is
https://pubsub.googleapis.com/v1/projects/myprj/topics/mytopic
then relative name is
projects/myprj/topics/mytopic.
"""
escape_func = urllib.parse.quote if url_escape else lambda x, safe: x
effective_params = dict(
[(k, escape_func(getattr(self, k), safe=''))
for k in self._params])
return urllib.parse.unquote(
uritemplate.expand(self._path, effective_params))
示例10: retrieve_discovery_doc
def retrieve_discovery_doc(serviceName, version,
discoveryServiceUrl=DISCOVERY_URI):
params = {'api': serviceName, 'apiVersion': version}
requested_url = uritemplate.expand(discoveryServiceUrl, params)
# REMOTE_ADDR is defined by the CGI spec [RFC3875] as the environment
# variable that contains the network address of the client sending the
# request. If it exists then add that to the request for the discovery
# document to avoid exceeding the quota on discovery requests.
if 'REMOTE_ADDR' in os.environ:
requested_url = _add_query_parameter(requested_url, 'userIp',
os.environ['REMOTE_ADDR'])
http = httplib2.Http()
resp, content = http.request(requested_url)
if resp.status >= 400:
raise HttpError(resp, content, uri=requested_url)
try:
service = json.loads(content)
except ValueError:
raise InvalidJsonError(
'Bad JSON: %s from %s.' % (content, requested_url))
# We return content instead of the JSON deserialized service because
# build_from_document() consumes a string rather than a dictionary.
return content
示例11: expand_uri
def expand_uri(self, name, **kwargs):
link = self.schema.get_link(name)
href = link.get('href', '')
context = dict(self.data, **kwargs)
return uritemplate.expand(href, context)
示例12: _delete
def _delete(self, **kwargs):
# TODO: Refactor to resolve circular dependency.
from pluct import resource
self.href = expand(self.href, self.resource.data)
response = requests.delete(url=self.href, headers=self.get_headers())
return resource.from_response(response)
示例13: _refresh
def _refresh(self, http_request):
"""Refreshes the access_token.
Skip all the storage hoops and just refresh using the API.
Args:
http_request: callable, a callable that matches the method signature of
httplib2.Http.request, used to make the refresh request.
Raises:
AccessTokenRefreshError: When the refresh fails.
"""
uri = uritemplate.expand(META, {'scope': self.scope})
response, content = http_request(uri)
if response.status == 200:
try:
d = simplejson.loads(content)
except Exception as e:
raise AccessTokenRefreshError(str(e))
self.access_token = d['accessToken']
else:
if response.status == 404:
content = content + (' This can occur if a VM was created'
' with no service account or scopes.')
raise AccessTokenRefreshError(content)
示例14: get_odes_extracts
def get_odes_extracts(db, api_key):
'''
'''
odeses, extracts = list(), list()
vars = dict(api_key=api_key)
extracts_url = uritemplate.expand(odes_extracts_url, vars)
resp = requests.get(extracts_url)
if resp.status_code in range(200, 299):
odeses.extend([data.ODES(str(oj['id']), status=oj['status'], bbox=oj['bbox'],
links=oj.get('download_links', {}),
processed_at=(parse_datetime(oj['processed_at']) if oj['processed_at'] else None),
created_at=(parse_datetime(oj['created_at']) if oj['created_at'] else None))
for oj in resp.json()])
for odes in sorted(odeses, key=attrgetter('created_at'), reverse=True):
extract = data.get_extract(db, odes=odes)
if extract is None:
extract = data.Extract(None, None, None, odes, None, None, None)
extracts.append(extract)
return extracts
示例15: get_odes_extract
def get_odes_extract(db, id, api_key):
'''
'''
extract, odes = data.get_extract(db, extract_id=id), None
if extract is None:
# Nothing by that name in the database, so ask the ODES API.
vars = dict(id=id, api_key=api_key)
extract_url = uritemplate.expand(odes_extracts_url, vars)
resp = requests.get(extract_url)
if resp.status_code in range(200, 299):
oj = resp.json()
odes = data.ODES(str(oj['id']), status=oj['status'], bbox=oj['bbox'],
links=oj.get('download_links', {}),
processed_at=(parse_datetime(oj['processed_at']) if oj['processed_at'] else None),
created_at=(parse_datetime(oj['created_at']) if oj['created_at'] else None))
if odes is None:
# Nothing at all for this ID anywhere.
return None
if odes is None:
# A DB extract was found, but nothing in ODES - very weird!
return get_odes_extract(db, extract.odes.id, api_key)
# We have a known ODES, so look for it in the database.
extract = data.get_extract(db, odes=odes)
if extract is None:
# Known ODES, but nothing in the DB so make one up.
return data.Extract(None, None, None, odes, None, None, None)
return extract