本文整理汇总了Python中urllib.parse.urlquote函数的典型用法代码示例。如果您正苦于以下问题:Python urlquote函数的具体用法?Python urlquote怎么用?Python urlquote使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了urlquote函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_repos
def get_repos(self, account, page_url=None, sess=None, refresh=True):
if not page_url:
page_url = self.api_repos_path.format(
user_id=urlquote(account.user_id),
user_name=urlquote(account.user_name or ''),
)
if not getattr(self.api_repos_path, 'use_session', True):
sess = None
r = self.api_get(account.domain, page_url, sess=sess)
repos, count, pages_urls = self.api_paginator(r, self.api_parser(r))
repos = [self.extract_repo_info(repo, account.domain) for repo in repos]
if '{user_name}' in self.api_repos_path and repos and repos[0].owner_id != account.user_id:
# https://hackerone.com/reports/452920
if not refresh:
raise TokenExpiredError()
from liberapay.models.account_elsewhere import UnableToRefreshAccount
try:
account = account.refresh_user_info()
except UnableToRefreshAccount:
raise TokenExpiredError()
# Note: we can't pass the page_url below, because it contains the old user_name
return self.get_repos(account, page_url=None, sess=sess, refresh=False)
if count == -1 and hasattr(self, 'x_repos_count'):
count = self.x_repos_count(None, account.extra_info, -1)
return repos, count, pages_urls
示例2: _encode_netloc
def _encode_netloc(components):
host = ''
if components.hostname:
host = encode_idna(components.hostname).decode('ascii')
if ':' in host:
host = '[%s]' % host
netloc = host
if components.port:
if not 0 <= int(components.port) <= 65535:
raise ValueError('Invalid port')
netloc = '%s:%s' % (netloc, components.port)
if components.username or components.password:
if components.username:
username = urlquote(
components.username, safe='/:%'
)
else:
username = ''
if components.password:
password = urlquote(
components.password, safe='/:%'
)
auth = '%s:%s' % (username, password)
else:
auth = username
netloc = '%[email protected]%s' % (auth, netloc)
return netloc
示例3: import_data
def import_data(self, db, table, format, bytes_or_stream, size, unique_id=None):
"""Import data into Treasure Data Service
This method expects data from a file-like object formatted with "msgpack.gz".
Params:
db (str): name of a database
table (str): name of a table
format (str): format of data type (e.g. "msgpack.gz")
bytes_or_stream (str or file-like): a byte string or a file-like object contains the data
size (int): the length of the data
unique_id (str): a unique identifier of the data
Returns: float represents the elapsed time to import data
"""
if unique_id is not None:
path = "/v3/table/import_with_id/%s/%s/%s/%s" % (urlquote(str(db)), urlquote(str(table)), urlquote(str(unique_id)), urlquote(str(format)))
else:
path = "/v3/table/import/%s/%s/%s" % (urlquote(str(db)), urlquote(str(table)), urlquote(str(format)))
kwargs = {}
with self.put(path, bytes_or_stream, size, **kwargs) as res:
code, body = res.status, res.read()
if code / 100 != 2:
self.raise_error("Import failed", res, body)
js = self.checked_json(body, ["elapsed_time"])
time = float(js["elapsed_time"])
return time
示例4: attach
def attach(self, name, data, callback, type='text/plain'):
def _really_callback(response):
if response.code != 201:
callback(_error_response(response))
return
data = json.loads(response.body.decode('utf-8'))
assert data['id'] == self.id
self.rev = data['rev']
self.attachments[name] = {
'content_type': type,
'length': len(data),
'stub': True,
}
callback(self)
headers = {'Content-Type': type, 'Expect': ''}
self.db._fetch(
'%s/%s?rev=%s' % (
urlquote(self.id, safe=''),
urlquote(name, safe=''),
self.rev),
_really_callback,
method='PUT',
body=data,
headers=headers,
)
示例5: _create_table
def _create_table(self, db, table, type, params=None):
params = {} if params is None else params
with self.post("/v3/table/create/%s/%s/%s" % (urlquote(str(db)), urlquote(str(table)), urlquote(str(type))), params) as res:
code, body = res.status, res.read()
if code != 200:
self.raise_error("Create %s table failed" % (type), res, body)
return True
示例6: sentiment_targeted
def sentiment_targeted(self, flavor, data, target, options={}):
"""
Calculates the targeted sentiment for text, a URL or HTML.
For an overview, please refer to: http://www.alchemyapi.com/products/features/sentiment-analysis/
For the docs, please refer to: http://www.alchemyapi.com/api/sentiment-analysis/
INPUT:
flavor -> which version of the call, i.e. text, url or html.
data -> the data to analyze, either the text, the url or html code.
target -> the word or phrase to run sentiment analysis on.
options -> various parameters that can be used to adjust how the API works, see below for more info on the available options.
Available Options:
showSourceText -> 0: disabled, 1: enabled
OUTPUT:
The response, already converted from JSON to a Python object.
"""
#Make sure the target is valid
if target is None or target == '':
return { 'status':'ERROR', 'statusInfo':'targeted sentiment requires a non-null target' }
#Make sure this request supports this flavor
if flavor not in AlchemyAPI.ENDPOINTS['sentiment_targeted']:
return { 'status':'ERROR', 'statusInfo':'targeted sentiment analysis for ' + flavor + ' not available' }
#add the URL encoded data and target to the options and analyze
options[flavor] = urlquote(data)
options['target'] = urlquote(target)
return self.__analyze(AlchemyAPI.ENDPOINTS['sentiment_targeted'][flavor], options)
示例7: query
def query(self, q, type="hive", db=None, result_url=None, priority=None, retry_limit=None, **kwargs):
"""
TODO: add docstring
=> jobId:str
"""
params = {"query": q}
params.update(kwargs)
if result_url is not None:
params["result"] = result_url
if priority is not None:
if not isinstance(priority, int):
priority_name = str(priority).upper()
if priority_name in self.JOB_PRIORITY:
priority = self.JOB_PRIORITY[priority_name]
else:
raise(ValueError("unknown job priority: %s" % (priority_name,)))
params["priority"] = priority
if retry_limit is not None:
params["retry_limit"] = retry_limit
with self.post("/v3/job/issue/%s/%s" % (urlquote(str(type)), urlquote(str(db))), params) as res:
code, body = res.status, res.read()
if code != 200:
self.raise_error("Query failed", res, body)
js = self.checked_json(body, ["job_id"])
return str(js["job_id"])
示例8: get_current_url
def get_current_url(
environ, root_only=False, strip_querystring=False,
host_only=False, trusted_hosts=None,
):
"""A handy helper function that recreates the full URL as IRI for the
current request or parts of it. Here an example:
>>> from verktyg.test import create_environ
>>> env = create_environ("/?param=foo", "http://localhost/script")
>>> get_current_url(env)
'http://localhost/script/?param=foo'
>>> get_current_url(env, root_only=True)
'http://localhost/script/'
>>> get_current_url(env, host_only=True)
'http://localhost/'
>>> get_current_url(env, strip_querystring=True)
'http://localhost/script/'
This optionally it verifies that the host is in a list of trusted hosts.
If the host is not in there it will raise a
:exc:`~verktyg.exceptions.SecurityError`.
Note that the string returned might contain unicode characters as the
representation is an IRI not an URI. If you need an ASCII only
representation you can use the :func:`~verktyg.urls.iri_to_uri`
function:
>>> from verktyg.urls import iri_to_uri
>>> iri_to_uri(get_current_url(env))
'http://localhost/script/?param=foo'
:param environ:
The WSGI environment to get the current URL from.
:param root_only:
Set `True` if you only want the root URL.
:param strip_querystring:
Set to `True` if you don't want the querystring.
:param host_only:
Set to `True` if the host URL should be returned.
:param trusted_hosts:
A list of trusted hosts, see :func:`host_is_trusted` for more
information.
"""
tmp = [environ['wsgi.url_scheme'], '://', get_host(environ, trusted_hosts)]
cat = tmp.append
if host_only:
return uri_to_iri(''.join(tmp) + '/')
cat(urlquote(wsgi_get_bytes(environ.get('SCRIPT_NAME', ''))).rstrip('/'))
cat('/')
if not root_only:
cat(urlquote(
wsgi_get_bytes(environ.get('PATH_INFO', '')).lstrip(b'/')
))
if not strip_querystring:
qs = get_query_string(environ)
if qs:
cat('?' + qs)
return uri_to_iri(''.join(tmp))
示例9: update_expire
def update_expire(self, db, table, expire_days):
"""
TODO: add docstring
"""
with self.post("/v3/table/update/%s/%s" % (urlquote(str(db)), urlquote(str(table))), {"expire_days": expire_days}) as res:
code, body = res.status, res.read()
if code != 200:
self.raise_error("Update table expiration failed", res, body)
return True
示例10: __init__
def __init__(self, plugin_name, init_schema, db_file_name=None, upgrade_mod=None):
"""
Runs the initialisation process that includes creating the connection to the database and the tables if they do
not exist.
``plugin_name``
The name to setup paths and settings section names
``init_schema``
The init_schema function for this database
``upgrade_schema``
The upgrade_schema function for this database
``db_file_name``
The file name to use for this database. Defaults to None resulting in the plugin_name being used.
"""
settings = Settings()
settings.beginGroup(plugin_name)
self.db_url = ''
self.is_dirty = False
self.session = None
db_type = settings.value('db type')
if db_type == 'sqlite':
if db_file_name:
self.db_url = 'sqlite:///%s/%s' % (AppLocation.get_section_data_path(plugin_name), db_file_name)
else:
self.db_url = 'sqlite:///%s/%s.sqlite' % (AppLocation.get_section_data_path(plugin_name), plugin_name)
else:
self.db_url = '%s://%s:%[email protected]%s/%s' % (db_type,
urlquote(settings.value('db username')),
urlquote(settings.value('db password')),
urlquote(settings.value('db hostname')),
urlquote(settings.value('db database')))
if db_type == 'mysql':
db_encoding = settings.value('db encoding')
self.db_url += '?charset=%s' % urlquote(db_encoding)
settings.endGroup()
if upgrade_mod:
db_ver, up_ver = upgrade_db(self.db_url, upgrade_mod)
if db_ver > up_ver:
critical_error_message_box(
translate('OpenLP.Manager', 'Database Error'),
translate('OpenLP.Manager', 'The database being loaded was created in a more recent version of '
'OpenLP. The database is version %d, while OpenLP expects version %d. The database will not '
'be loaded.\n\nDatabase: %s') % (db_ver, up_ver, self.db_url)
)
return
try:
self.session = init_schema(self.db_url)
except (SQLAlchemyError, DBAPIError):
log.exception('Error loading database: %s', self.db_url)
critical_error_message_box(
translate('OpenLP.Manager', 'Database Error'),
translate('OpenLP.Manager', 'OpenLP cannot load your database.\n\nDatabase: %s') % self.db_url
)
示例11: bulk_import_upload_part
def bulk_import_upload_part(self, name, part_name, stream, size):
"""
TODO: add docstring
=> None
"""
self.validate_part_name(part_name)
with self.put("/v3/bulk_import/upload_part/%s/%s" % (urlquote(str(name)), urlquote(str(part_name))), stream, size) as res:
code, body = res.status, res.read()
if code / 100 != 2:
self.raise_error("Upload a part failed", res, body)
示例12: update_schema
def update_schema(self, db, table, schema_json):
"""
TODO: add docstring
=> True
"""
with self.post("/v3/table/update-schema/%s/%s" % (urlquote(str(db)), urlquote(str(table))), {"schema": schema_json}) as res:
code, body = res.status, res.read()
if code != 200:
self.raise_error("Create schema table failed", res, body)
return True
示例13: swap_table
def swap_table(self, db, table1, table2):
"""
TODO: add docstring
=> True
"""
with self.post("/v3/table/swap/%s/%s/%s" % (urlquote(str(db)), urlquote(str(table1)), urlquote(str(table2)))) as res:
code, body = res.status, res.read()
if code != 200:
self.raise_error("Swap tables failed", res, body)
return True
示例14: addFile
def addFile(self, newFile, path):
"""
This adds the given file to the owncloud server. newFile is a string path to a local file and that file name
will be used as its name.
"""
self.log.debug("Adding New File: %s/%s" % (path, os.path.basename(newFile)))
data = open(newFile, "rb").read()
if path not in self.DIRS:
self.mkdir(path)
self.http(
str("{}/{}/{}".format(self.url, urlquote(path), urlquote(os.path.basename(newFile)))), "PUT", data=data)
示例15: bulk_import_upload_part
def bulk_import_upload_part(self, name, part_name, stream, size):
"""
TODO: add docstring
=> None
"""
if 0 < part_name.find("/"):
raise ValueError("part name must not contain '/': %s" % (repr(part_name,)))
with self.put("/v3/bulk_import/upload_part/%s/%s" % (urlquote(str(name)), urlquote(str(part_name))), stream, size) as res:
code, body = res.status, res.read()
if code / 100 != 2:
self.raise_error("Upload a part failed", res, body)