def prepare_request(self,url,token=None,secret='',additional_params={},method='GET'): encode=lambda t:urlquote(str(t),'') urlquh=lambda t:urlquote(str(t),'~') params={'oauth_consumer_key':self.consumer_key, 'oauth_signature_method':'HMAC-SHA1', 'oauth_timestamp':str(int(time())), 'oauth_nonce':''.join(list(choice(seqstr) for i in xrange(64))), 'oauth_version':'1.0'} if token: params['oauth_token']=token elif self.callback_url: params['oauth_callback']=self.callback_url params.update(additional_params) for k,v in params.iteritems(): if isinstance(v,unicode): params[k]=v.encode('utf8') params_str='&'.join([encode(k)+'='+urlquh(v) for k,v in sorted(params.iteritems())]) params['oauth_signature']=hmac('&'.join([self.consumer_secret,secret]), '&'.join([method,encode(url),urlquh(params_str)]), sha1).digest().encode('base64').strip() return '&'.join([urlquote_plus(str(k),'~')+'='+urlquote_plus(str(v),'~') for k,v in sorted(params.iteritems())])
def __new__( self, server=None, user=None, passwd=None, db=None, port=1433, drv=ODBC_DRIVER, **engine_args ): """ It establishes a new database connection and returns engine Args: server (str): IP or Hostname to database user (str): Username for database passwd (str): Password for database db (str): Name of database port (int): Connection port drv (str): Name of underlying database driver for connection **engine_args (mixed): Kwargs to pass to ``create_engine`` Return: sqlalchemy.engine.Engine """ if drv != self.SQLITE: params = { 'usr': urlquote(user), 'pass': urlquote(passwd), 'srv': server, 'drv': drv, 'db': db, 'prt': port, } dsn = 'mssql+pyodbc://{usr}:{pass}@{srv}:{prt}/{db}?driver={drv}' return create_engine(dsn.format(**params), **engine_args) else: return create_engine('%s://' % self.SQLITE, **engine_args)
def do_pastebin_json(self, s): """Upload to pastebin via json interface.""" url = urljoin(self.config.pastebin_url, '/json/new') payload = { 'code': s, 'lexer': 'pycon', 'expiry': self.config.pastebin_expiry } self.interact.notify(_('Posting data to pastebin...')) try: response = requests.post(url, data=payload, verify=True) response.raise_for_status() except requests.exceptions.RequestException as exc: self.interact.notify(_('Upload failed: %s') % (str(exc), )) return self.prev_pastebin_content = s data = response.json() paste_url_template = Template(self.config.pastebin_show_url) paste_id = urlquote(data['paste_id']) paste_url = paste_url_template.safe_substitute(paste_id=paste_id) removal_url_template = Template(self.config.pastebin_removal_url) removal_id = urlquote(data['removal_id']) removal_url = removal_url_template.safe_substitute(removal_id=removal_id) self.prev_pastebin_url = paste_url self.interact.notify(_('Pastebin URL: %s - Removal URL: %s') % (paste_url, removal_url)) return paste_url
def compute_url_for_host(self, host, *args, **kwargs): out = self.scheme + '://' + host + '/' + '/'.join( arg.encode('utf-8') for arg in args ) if kwargs: out += '?' _set = 0 _l = '' for key, value in kwargs.items(): key = urlquote(key).replace(' ', '+') if value is None: value = '' if isinstance(value, list): for val in value: if _set: _l = '&' out += '%s%s=%s' % ( _l, key, urlquote(val.encode('utf-8')).replace(' ', '+') ) _set = 1 else: if _set: _l = '&' out += '%s%s=%s' % ( _l, key, urlquote(value.encode('utf-8')).replace(' ', '+') ) _set = 1 return out
def compute_site_uri(self, *args, **kwargs): request_charset = self.request_charset out = self.site_uri + '/' + '/'.join( arg.encode(request_charset) for arg in args ) if kwargs: out += '?' _set = 0 _l = '' for key, value in kwargs.items(): key = urlquote(key).replace(' ', '+') if value is None: value = '' if isinstance(value, list): for val in value: if _set: _l = '&' out += '%s%s=%s' % ( _l, key, urlquote(val.encode(request_charset)).replace(' ', '+') ) _set = 1 else: if _set: _l = '&' out += '%s%s=%s' % ( _l, key, quote(value.encode(request_charset)).replace(' ', '+') ) _set = 1 return out
def url_to_cache_name(label, url): tmp = ["gitlab_projects"] if label is not None: tmp.append(urlquote(label, safe="")) if url is not None: _, tail = url.split("://", 1) tmp.append(urlquote(tail, safe="")) return "_".join(tmp)
def _gen_link(self, link): if link.startswith("http://"): link = "http://{0}".format(urlquote(link[7:])) elif link.startswith("https://"): link = "http://{0}".format(urlquote(link[8:])) else: link = "http://{0}".format(urlquote(link)) return link
def build_uri(username, password, hostname, port, database_name): uri_template = 'mysql+mysqldb://{username}:{password}@{hostname}' \ ':{port}/{database_name}?charset=utf8mb4' return uri_template.format(username=urlquote(username), password=urlquote(password), hostname=urlquote(hostname), port=port, database_name=urlquote(database_name))
def _gen_searchlink(self, sid, params): link = self._sites[sid]['url'] if link.startswith("http://"): link = "http://{0}".format(urlquote(link[7:].format(*params.split("&")), '/')) elif link.startswith("https://"): link = "http://{0}".format(urlquote(link[8:].format(*params.split("&")), '/')) else: link = "http://{0}".format(urlquote(link.format(*params.split("&")), '/')) return link
def get_keypath(self, key, userid=None, appid=None): if userid is None: userid = self.userid if appid is None: appid = self.audience keypath = urljoin(self.root_url,"/app/%s/users/%s/keys/%s") keypath %= (urlquote(appid, safe=""), urlquote(userid, safe=""), urlquote(key, safe="")) return keypath
def test_mssql_dsn(self, mk): Db( self.params['srv'], self.params['usr'], self.params['pass'], self.params['db'], self.params['prt'], self.params['drv'], ) self.params['usr'] = urlquote(self.params['usr']) self.params['pass'] = urlquote(self.params['pass']) mk.assert_called_once_with(self.dsn.format(**self.params))
def get_authorization_url(self): """Get Authorization URL.""" token = self._get_auth_token() if self.use_sandbox: return ("https://sandbox.evernote.com/OAuth.action?" "oauth_token=%s&oauth_callback=%s" % (token, urlquote(self.callback_url))) else : return ("https://www.evernote.com/OAuth.action?" "oauth_token=%s&oauth_callback=%s" % (token, urlquote(self.callback_url)))
def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False): """ Converts any URLs in text into clickable links. Works on http://, https://, www. links and links ending in .org, .net or .com. Links can have trailing punctuation (periods, commas, close-parens) and leading punctuation (opening parens) and it'll still do the right thing. If trim_url_limit is not None, the URLs in link text longer than this limit will truncated to trim_url_limit-3 characters and appended with an elipsis. If nofollow is True, the URLs in link text will get a rel="nofollow" attribute. If autoescape is True, the link text and URLs will get autoescaped. """ trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x words = word_split_re.split(unicode(text)) nofollow_attr = nofollow and ' rel="nofollow"' or '' for i, word in enumerate(words): match = None if '.' in word or '@' in word or ':' in word: match = punctuation_re.match(word) if match: lead, middle, trail = match.groups() # Make URL we want to point to. url = None if middle.startswith('http://') or middle.startswith('https://'): url = urlquote(middle, safe='/&=:;#?+*') elif middle.startswith('www.') or ('@' not in middle and \ middle and middle[0] in string.ascii_letters + string.digits and \ (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))): url = urlquote('http://%s' % middle, safe='/&=:;#?+*') elif '@' in middle and not ':' in middle and simple_email_re.match(middle): url = 'mailto:%s' % middle nofollow_attr = '' # Make link. if url: trimmed = trim_url(middle) if autoescape: lead, trail = escape(lead), escape(trail) url, trimmed = escape(url), escape(trimmed) middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr, trimmed) words[i] = '%s%s%s' % (lead, middle, trail) else: if autoescape: words[i] = escape(word) elif autoescape: words[i] = escape(word) return u''.join(words)
def run(self, edit): for s in self.view.sel(): if s.empty(): s = self.view.word(s) selected = self.view.substr(s) txt = urlquote(selected.encode('utf8'), '') self.view.replace(edit, s, txt)
def ping_google(url, sitemap_id): """Ping sitemap to Google""" resurl = url + "/" + sitemap_id if DevelopmentMode or config.testing: #prevent pinging in debug or testing mode print "Pinged %s sitemap to Google" % resurl return 0 request = 'http://www.google.com/webmasters/tools/ping?sitemap=' +\ urlquote(resurl) try: # BBB: Change urlopen -> socket.urlopen when # compatibility with python2.4 is not important g = urlopen(request) except urllib2.URLError, e: if hasattr(e, 'reason'): logger.error('We failed to reach a server. ' 'Request: %s. ' 'Reason: %s' % (request, e.reason)) elif hasattr(e, 'code'): logger.error('The server couldn\'t fulfill the request. ' 'Request: %s ' 'Error code: %s. ' % (request, e.code))
def get_node_url(self, nodename=""): """Return the url for nodes""" url = urlparse.urljoin( self.base_server_url(), 'computer/%s' % urlquote(nodename)) return url
def ping_google(url): """Ping sitemap to Google""" sitemap_url = urlquote(url + "/google-sitemaps") g = urlopen('http://www.google.com/webmasters/sitemaps/ping?sitemap='+sitemap_url) result = g.read() g.close() return 0
def create_resource(self, resource, resource_data, resource_type, **kwargs): """Creates new resource and creates it's data by provided\ `feed_id` and `resource_id`.""" if not isinstance(resource, Resource): raise KeyError("'resource' should be an instance of Resource") if not isinstance(resource_data, ResourceData) or not resource_data.value: raise KeyError( "'resource_data' should be ResourceData with 'value' attribute") if not isinstance(resource_type, ResourceType): raise KeyError("'resource_type' should be an instance of ResourceType") if not kwargs or 'feed_id' not in kwargs: raise KeyError('Variable "feed_id" id mandatory field!') resource_id = urlquote(resource.id, safe='') r = self._post_inv_status('entity/f;{}/resource'.format(kwargs['feed_id']), data={"name": resource.name, "id": resource.id, "resourceTypePath": "rt;{}" .format(resource_type.path.resource_type_id)}) if r: r = self._post_inv_status('entity/f;{}/r;{}/data' .format(kwargs['feed_id'], resource_id), data={'role': 'configuration', "value": resource_data.value}) else: # if resource or it's data was not created correctly, delete resource self._delete_inv_status('entity/f;{}/r;{}'.format(kwargs['feed_id'], resource_id)) return r
def do_pastebin_xmlrpc(self, s): """Upload to pastebin via XML-RPC.""" try: pasteservice = ServerProxy(self.config.pastebin_url) except IOError as e: self.interact.notify(_("Pastebin error for URL '%s': %s") % (self.config.pastebin_url, str(e))) return self.interact.notify(_('Posting data to pastebin...')) try: paste_id = pasteservice.pastes.newPaste('pycon', s, '', '', '', self.config.pastebin_private) except (SocketError, XMLRPCError) as e: self.interact.notify(_('Upload failed: %s') % (str(e), )) return self.prev_pastebin_content = s paste_url_template = Template(self.config.pastebin_show_url) paste_id = urlquote(paste_id) paste_url = paste_url_template.safe_substitute(paste_id=paste_id) self.prev_pastebin_url = paste_url self.interact.notify(_('Pastebin URL: %s') % (paste_url, ), 10) return paste_url
def get_authorization_url(self): '''Get Authorization URL.''' token = self._get_auth_token() return ('http://www.dropbox.com/0/oauth/authorize?' 'oauth_token=%s&oauth_callback=%s' % (token, urlquote(self.callback_url)))
def cmd_google(word, word_eol, userdata=False): """ /GOOGLE Command Handler: userdata is debug.""" debug = False for debugarg in ('-d', '--debug'): if debugarg in word: word.remove(debugarg) debug = True try: if len(word) < 2: print(help_str) return None userquery = '+'.join([urlquote(s) for s in word[1:]]) site = 'https://www.google.com/search?q=%s' % (userquery) if debug: print('Opening site: {}'.format(site)) nonzero = open_site(site, debug=debug) if nonzero: print('Unable to google.') else: if debug: print('\nGoogled {}'.format(site)) return None except Exception as ex: print('Error during that google:\n{}'.format(ex)) return None
def name_to_href(self, name, base=None): '''Convert a name to a href relative to base, which must be a name or None in which case self.root is used as the base''' fullpath = self.name_to_abspath(name) basepath = self.root if base is None else os.path.dirname(self.name_to_abspath(base)) path = relpath(fullpath, basepath).replace(os.sep, '/') return urlquote(path)
def createMutlipartResponse(requestPath, meta, requestedProperties,postfix): rootEl = ET.Element("{DAV:}multistatus") for (path, data) in meta.items(): responseEl = ET.SubElement(rootEl, '{DAV:}response') hrefEl = ET.SubElement(responseEl, '{DAV:}href') href = path.replace(requestPath, postfix,1) href = re.sub(r'/{2,}', '/', href) hrefEl.text = urlquote(href, safe='/') propstatEl = ET.SubElement(responseEl, '{DAV:}propstat') propEl = ET.SubElement(propstatEl, '{DAV:}prop') missingProperties = copy(requestedProperties) for (key, value) in data.items(): if len(requestedProperties) > 0 and not requestedProperties.has_key(key): continue if key[0:1] != '{': # The following brakes some webdav clients # There it is allowed only for the litmus tests #if not ( request.env.has_key('HTTP_User-Agent') and \ #request.env['HTTP_User-Agent'][0].find('litmus') > -1): # if no namespace is given then assign 'none:' as default namespace key = "{none:}%s" % key addXmlProp(propEl, key, value) if len(missingProperties) and missingProperties.has_key(key): del missingProperties[key] statEl = ET.SubElement(propstatEl, '{DAV:}status') statEl.text = "HTTP/1.1 200 OK" if len(missingProperties): missingPropstatEl = ET.SubElement(responseEl, '{DAV:}propstat') missingPropEl = ET.SubElement(missingPropstatEl, '{DAV:}prop') for property in missingProperties: ET.SubElement(missingPropEl, property) missingStatEl = ET.SubElement(missingPropstatEl, '{DAV:}status') missingStatEl.text = "HTTP/1.1 404 Not Found" return rootEl
def setUp(self): """Setting up test.""" self.logd("setUp") self.server_url = self.conf_get('main', 'url') # XXX here you can setup the credential access like this credential_host = self.conf_get('credential', 'host') credential_port = self.conf_getInt('credential', 'port') self.login, self.password = xmlrpc_get_credential(credential_host, credential_port, 'members') #self.login = '******' #self.password = '******' # Set the zope cookie. This is a little evil but avoids having to # call the login page. morsel = Morsel() morsel.key = '__ac' morsel.value = morsel.coded_value = urlquote( encodestring('%s:%s' % (self.login, self.password))) self._browser.cookies = { 'rhaptos': { '/': { '__ac': morsel } } }
def engine_uri(database=None): """ By default doesn't include the specific database. """ config_prefix = 'RDS' if is_prod() else 'MYSQL' username = config.get('{0}_USER'.format(config_prefix), None) assert username, "Must have database username to connect!" password = config.get('{0}_PASSWORD'.format(config_prefix), None) assert password, "Must have database password to connect!" host = config.get('{0}_HOSTNAME'.format(config_prefix), None) assert host, "Must have database to connect!" port = config.get('{0}_PORT'.format(config_prefix), None) assert port, "Must have database port to connect!" uri_template = 'mysql://{username}:{password}@{host}:{port}/{database}?charset=utf8mb4' return uri_template.format( username=username, # http://stackoverflow.com/questions/15728290/sqlalchemy-valueerror-for-slash-in-password-for-create-engine (also applicable to '+' sign) password=urlquote(password), host=host, port=port, database=database if database else '')
def toggle_temporarily_offline(self, message="requested from jenkinsapi"): """ Switches state of connected node (online/offline) and set 'temporarilyOffline' property (True/False) Calling the same method again will bring node status back. :param message: optional string can be used to explain why you are taking this node offline """ initial_state = self.is_temporarily_offline() url = self.baseurl + \ "/toggleOffline?offlineMessage=" + urlquote(message) try: html_result = self.jenkins.requester.get_and_confirm_status(url) except PostRequired: html_result = self.jenkins.requester.post_and_confirm_status( url, data={}) self.poll() log.debug(html_result) state = self.is_temporarily_offline() if initial_state == state: raise AssertionError( "The node state has not changed: temporarilyOffline = %s" % state)
def setUp(self): """Setting up test, in current case just setting the server url.""" self.logd("setUp") self.label = "Authenticated Tests (QAauth)" self.server_url = self.conf_get('main', 'url') hostname = urlparse(self.server_url)[1].split(':')[0] credential_host = self.conf_get('credential', 'host') credential_port = self.conf_getInt('credential', 'port') self.login, self.password = xmlrpc_get_credential(credential_host, credential_port, 'members') # Set the zope cookie. This is a little evil but avoids having to # call the login page. morsel = Morsel() morsel.key = '__ac' morsel.value = morsel.coded_value = urlquote( encodestring('%s:%s' % (self.login, self.password))) self._browser.cookies = { hostname: { '/': { '__ac': morsel } } }
def get_authorization_url(self): """Get Authorization URL.""" token = self._get_auth_token() return ("http://www.dropbox.com/0/oauth/authorize?" "oauth_token=%s&oauth_callback=%s" % (token, urlquote(self.callback_url)))
def getChild(self, path, request): return CFProxyResource( self.session, self.host, self.port, self.path + '/' + urlquote(path, safe='') )
def tfw(phenny, input, fahrenheit=False, celsius=False): """.tfw <city/zip> - Show the f*****g weather at the specified location.""" zipcode = input.group(2) if not zipcode or zipcode == 'tgg': # default to Grand Rapids, Michigan zipcode = "49504" if fahrenheit: celsius_param = "" else: celsius_param = "&CELSIUS=yes" try: req = urlopen("http://thefuckingweather.com/?zipcode=%s%s" % (urlquote(zipcode), celsius_param)) except HTTPError: phenny.say("THE INTERNET IS F*****G BROKEN. Please try again later.") return doc = lxml.html.parse(req) location = doc.getroot().find_class('small')[0].text_content() try: weather = doc.getroot().get_element_by_id('content') except KeyError: phenny.say("Unknown location") return main = weather.find_class('large') # temperature is everything up to first <br /> tempt = "" for c in main[0].text: if c.isdigit(): tempt += c temp = int(tempt) deg = unichr(176).encode('latin-1') # add units and convert if necessary if fahrenheit: temp = "%d%cF?!" % (temp, deg) elif celsius: temp = "%d%cC?!" % (temp, deg) else: tempev = (temp + 273.15) * 8.617343e-5 * 1000 temp = "%f meV?!" % tempev # parse comment (broken by <br />, so we have do it this way) comments = main[0].xpath('text()') if len(comments) > 2: comment = "%s %s" % (comments[1], comments[2]) else : comment = comments[1] # remark is in its own div, so we have it easy remark = weather.get_element_by_id('remark').text_content() response = "%s %s IN %s! '%s'" % (temp, comment, location, remark) phenny.say(response)
def define(term): """Search for term/phrase and return list of UrbanDefinition objects. Keyword arguments: term -- term or phrase to search for (str) """ json = _get_urban_json(UD_DEFINE_URL + urlquote(term)) return _parse_urban_json(json)
def defineID(defid): """Search for UD's definition ID and return list of UrbanDefinition objects. Keyword arguments: defid -- definition ID to search for (int or str) """ json = _get_urban_json(UD_DEFID_URL + urlquote(str(defid))) return _parse_urban_json(json)
def name_to_href(self, name, base=None): '''Convert a name to a href relative to base, which must be a name or None in which case self.root is used as the base''' fullpath = self.name_to_abspath(name) basepath = self.root if base is None else os.path.dirname( self.name_to_abspath(base)) path = relpath(fullpath, basepath).replace(os.sep, '/') return urlquote(path)
def readCookieFile(self): cookiefile = os.path.join(self.satoshiHome, '.cookie') if os.path.exists(cookiefile): # This only works if bitcoind has started with open(cookiefile, 'r') as f: userpass = f.readline().split(":", 1) self.bitconf['rpcuser'] = userpass[0] self.bitconf['rpcpassword'] = urlquote(userpass[1])
def getChild(self, path, request): fragments = request.uri.split("/") fragments.pop(0) proxyArgs = (self.host, self.port, self.socket, self.path + '/' + urlquote(path, safe=""), self.reactor) #if not request.postpath: resource = DockerProxy(*proxyArgs, config=self.config) return resource
def getpathurl(self, path, allow_none=False): """Convert a client-side path into a server-side URL.""" path = relpath(normpath(path)) if path.endswith("/"): path = path[:-1] if isinstance(path, unicode): path = path.encode("utf8") return self.url + urlquote(path)
def getChild(self, path, request): """ This is necessary because the parent class would call proxy.ReverseProxyResource instead of CacheProxyResource """ return CacheProxyResource(self.host, self.port, self.path + '/' + urlquote(path, safe=""), self.reactor)
def get_db_engine(db_name='write_to'): """ Emits a SQL alchemy engine from the settings file """ db = settings.CUSTOM_DATABASES[db_name] engine_string = '%s://%s:%s@%s:%s/%s' % ( db['ENGINE'].split('.')[-1].lower(), db['USER'], urlquote(db['PASSWORD']), db['HOST'], db['PORT'], db['NAME']) return sqlalchemy.create_engine(engine_string, )
def project_resources(self, project, **kwargs): """Wraps `Rundeck API GET /project/[NAME]/resources <http://rundeck.org/docs/api/index.html#updating-and-listing-resources-for-a-project>`_ :Parameters: project : str name of the project :Keywords: fmt : str the format of the response one of :class:`~rundeck.defaults.ExecutionOutputFormat` ``values`` (default: 'text') hostname : str hostname inclusion filter tags : str tags inclusion filter os-name : str os-name inclusion filter os-family : str os-family inclusion filter os-arch : str os-arch inclusion filter os-version : str os-version inclusion filter name : str name inclusion filter exlude-hostname : str hostname exclusion filter exlude-tags : str tags exclusion filter exlude-os-name : str os-name exclusion filter exlude-os-family : str os-family exclusion filter exlude-os-arch : str os-arch exclusion filter exlude-os-version : str os-version exclusion filter exlude-name : str name exclusion filter :return: A :class:`~.rundeck.connection.RundeckResponse` :rtype: :class:`~.rundeck.connection.RundeckResponse` """ self.requires_version(2) params = cull_kwargs(('fmt', 'scriptInterpreter', 'interpreterArgsQuoted', 'hostname', \ 'tags', 'os-name', 'os-family', 'os-arch', 'os-version', 'name', 'exlude-hostname', \ 'exlude-tags', 'exlude-os-name', 'exlude-os-family', 'exlude-os-arch', \ 'exlude-os-version', 'exlude-name'), kwargs) if 'fmt' in params: params['format'] = params.pop('fmt') return self._exec(GET, 'project/{0}/resources'.format(urlquote(project)), params=params, **kwargs)
def getChild(self, path, request): """ Create and return a proxy resource with the same proxy configuration as this one, except that its path also contains the segment given by C{path} at the end. """ return MyReverseProxy(self.host, self.port, self.path + '/' + urlquote(path, safe=""), self.engine, self.reactor)
def keypath(self, key, userid=None, appid=None): """Get the server path at which to access the given key.""" if userid is None: userid = self.userid if appid is None: appid = self.store.appid path = "/app/%s/users/%s/keys/%s" path = path % tuple(urlquote(v, safe="") for v in (appid, userid, key)) return path
def get_participant(state, restrict=True, redirect_stub=True, allow_member=False): """Given a Request, raise Response or return Participant. If restrict is True then we'll restrict access to owners and admins. """ request = state['request'] user = state['user'] slug = request.line.uri.path['username'] _ = state['_'] if restrict: if user.ANON: if request.method == 'GET': url = '/sign-in?back_to='+urlquote(request.line.uri) raise Response(302, headers={'Location': url}) raise Response(403, _("You need to log in to access this page.")) if slug.startswith('~'): thing = 'id' value = slug[1:] participant = user if user and str(user.id) == value else None else: thing = 'lower(username)' value = slug.lower() participant = user if user and user.username.lower() == value else None if participant is None: from liberapay.models.participant import Participant # avoid circular import participant = Participant._from_thing(thing, value) if value else None if participant is None: raise Response(404) if request.method in ('GET', 'HEAD'): if slug != participant.username: canon = '/' + participant.username + request.line.uri[len(slug)+1:] raise Response(302, headers={'Location': canon}) status = participant.status if status == 'closed': if user.is_admin: return participant raise Response(410) elif status == 'stub': if redirect_stub: to = participant.resolve_stub() assert to raise Response(302, headers={'Location': to}) if restrict: if participant != user: if allow_member and participant.kind == 'group' and user.member_of(participant): pass elif not user.is_admin: raise Response(403, _("You are not authorized to access this page.")) return participant
def get_url(self, path): netloc = self.host if self.port is not None: netloc += ":%d" % self.port if self.username is not None: netloc = urlquote(self.username, '@/:') + "@" + netloc return urlparse.urlunsplit(('ssh', netloc, path, '', ''))
def _fetch(self, service, **kwargs): "Fetch data from the Random.org HTTP Interface" url = 'https://www.random.org/%s/?' % urlquote(service) options = dict(format='plain') options.update(kwargs) headers = {'User-Agent': 'RandomSources.randomDotOrg/%s' % self.__version__} req = Request(url + urlencode(options), headers=headers) return urlopen(req).read().splitlines()
def test_from_parsedurl_on_url_with_quoted_credentials(self): original_username = '******' quoted_username = urlquote(original_username) original_password = '******' quoted_password = urlquote(original_password) url = 'https://{username}:{password}@github.com/jelmer/dulwich'.format( username=quoted_username, password=quoted_password) c = HttpGitClient.from_parsedurl(urlparse.urlparse(url)) self.assertEqual(original_username, c._username) self.assertEqual(original_password, c._password) basic_auth = c.pool_manager.headers['authorization'] auth_string = '%s:%s' % (original_username, original_password) b64_credentials = self.b64encode(auth_string) expected_basic_auth = 'Basic %s' % str(b64_credentials) self.assertEqual(basic_auth, expected_basic_auth)
def _urlquote(string, safe=''): """ Quotes a unicode string for use in a URL :param string: A unicode string :param safe: A unicode string of character to not encode :return: None (if string is None) or an ASCII byte string of the quoted string """ if string is None or string == '': return None # Anything already hex quoted is pulled out of the URL and unquoted if # possible escapes = [] if re.search('%[0-9a-fA-F]{2}', string): # Try to unquote any percent values, restoring them if they are not # valid UTF-8. Also, requote any safe chars since encoded versions of # those are functionally different than the unquoted ones. def _try_unescape(match): byte_string = unquote_to_bytes(match.group(0)) unicode_string = byte_string.decode('utf-8', 'iriutf8') for safe_char in list(safe): unicode_string = unicode_string.replace( safe_char, '%%%02x' % ord(safe_char)) return unicode_string string = re.sub('(?:%[0-9a-fA-F]{2})+', _try_unescape, string) # Once we have the minimal set of hex quoted values, removed them from # the string so that they are not double quoted def _extract_escape(match): escapes.append(match.group(0).encode('ascii')) return '\x00' string = re.sub('%[0-9a-fA-F]{2}', _extract_escape, string) output = urlquote(string.encode('utf-8'), safe=safe.encode('utf-8')) if not isinstance(output, byte_cls): output = output.encode('ascii') # Restore the existing quoted values that we extracted if len(escapes) > 0: def _return_escape(_): return escapes.pop(0) output = re.sub(b'%00', _return_escape, output) return output
def url(self): scheme = self.environ.get('wsgi.url_scheme', 'http') host = self.environ.get('HTTP_X_FORWARDED_HOST', self.environ.get('HTTP_HOST', None)) if not host: host = self.environ.get('SERVER_NAME') port = self.environ.get('SERVER_PORT', '80') if scheme + port not in ('https443', 'http80'): host += ':' + port parts = (scheme, host, urlquote(self.fullpath), self.query_string, '') return urlunsplit(parts)
def url(self): """ The full URL as requested by the client """ scheme = self.environ.get('wsgi.url_scheme', 'http') host = self.environ.get('HTTP_HOST', None) if not host: host = self.environ.get('SERVER_NAME') port = self.environ.get('SERVER_PORT', '80') if scheme + port not in ('https443', 'http80'): host += ':' + port parts = (scheme, host, urlquote(self.fullpath), self.query_string, '') return urlunsplit(parts)
def test_from_parsedurl_on_url_with_quoted_credentials(self): original_username = '******' quoted_username = urlquote(original_username) original_password = '******' quoted_password = urlquote(original_password) url = 'https://{username}:{password}@github.com/jelmer/dulwich'.format( username=quoted_username, password=quoted_password) c = HttpGitClient.from_parsedurl(urlparse.urlparse(url)) self.assertEqual(original_username, c._username) self.assertEqual(original_password, c._password) [pw_handler] = [ h for h in c.opener.handlers if getattr(h, 'passwd', None) is not None ] self.assertEqual((original_username, original_password), pw_handler.passwd.find_user_password( None, 'https://github.com/jelmer/dulwich'))
def dynuser_copyurl (dyn) : db = dyn._db dyn = dyn._klass.getnode (dyn._nodeid) fields = user_dynamic.dynuser_copyfields url = 'user_dynamic?:template=item&' + '&'.join \ ('%s=%s' % (n, urlquote (str (dyn [n] or ''))) for n in fields) if _dynuser_half_frozen (db, dyn.user, dyn.valid_from, dyn.valid_to) : fr = freeze.frozen (db, dyn.user, dyn.valid_from, order = '-') [0] fr = db.daily_record_freeze.get (fr, 'date') + common.day url += '&valid_from=%s' % fr.pretty (common.ymd) return url
def unsafe(self, id): """ unsafe raw... not really documented, but identical to raw, except it will be exactly what you uploaded. """ url = '/api/trans/unsafe' + urlquote(id) self.log.info(url) resp, body = self._httpreq(url, headers={'x-requested-with' : 'Freebase-Python'}) self.log.info('unsafe is %d bytes' % len(body)) return body
def blurb(self, id, break_paragraphs=False, maxlength=200): """translate only the text in blob from id. For a more complete description, see http://www.freebase.com/view/en/api_trans_blurb""" url = '/api/trans/blurb' + urlquote(id) self.log.info(url) resp, body = self._httpreq(url, form=dict(break_paragraphs=break_paragraphs, maxlength=maxlength)) self.log.info('blurb is %d bytes' % len(body)) return body
def search_users(self, query, count=3): query = urlquote( cleanblanks(query).strip('@').lower().replace(' ', '+'), '') users = self._send_query(self.conn.users.search, { 'q': query, 'count': count, 'include_entities': 'false' }, return_result=True) if isinstance(users, str): return [] return [u['screen_name'] for u in users]
def _encode_params(kw): ''' Encode parameters using utf-8 and join them together ''' args = [] for k, v in kw.items(): try: qv = v.encode('utf-8') if isinstance(v, unicode) else str(v) except: qv = v args.append('%s=%s' % (k, urlquote(qv))) return '&'.join(args)
def urbandictionary(event, bot): """ urbandictionary [TERM]. Searches Urban Dictionary for TERM if supplied. Otherwise a random definition will be displayed.""" if not event.argument: json_obj = jsonload(urlopen(RANDOM_URL)) else: json_obj = jsonload(urlopen(API_URL + urlquote(event.argument))) if not json_obj['list']: return bot.say("No definition found for '%s'." % bold(event.argument)) return bot.say(format_definition(json_obj['list'][0]))
def project_resources(self, project, **kwargs): ''' Wraprs Rundeck API GET /project/[NAME]/resources <http://rundeck.org/docs/api/index.html#updating-and-listing-resources-for-a-project> Returns class ``rundeck.conection.RundeckResponse`` :param project: (str) name of the project :keyword args: fmt (str): [default: 'text'] the format of the response of ``rundeck.defaults.ExecutionOutputFormat`` values hostname (str): hostname inclusion filter tags (str): tags inclusion filter os-name (str): os-name inclusion filter os-family (str): os-family inclusion filter os-arch (str): os-arch inclusion filter os-version (str): os-version inclusion filter name (str): name inclusion filter exclude-hostname (str): hostname exclusion filter exclude-tags (str): tags exclusion filter exclude-os-name (str): os-name exclusion filter exclude-os-family (str): os-family exclusion filter exclude-os-arch (str): os-arch exclusion filter exclude-os-version (str): os-version exclusion filter exclude-name (str): name exclusion filter ''' self.requires_version(2) params = cull_kwargs(('fmt', 'scriptInterpreter', 'interpreterArgsQuoted', 'hostname', 'tags', 'os-name', 'os-family', 'os-arch', 'os-version', 'name', 'exclude-hostname', 'exclude-tags', 'exclude-os-name', 'exclude-os-family', 'exclude-os-arch', 'exclude-os-version', 'exclude-name'), kwargs) if 'fmt' in params: params['format'] = params.pop('fmt') return self._exec(GET, 'project/{0}/resources'.format(urlquote(project)), params=params, **kwargs)
def raw(self, id): """translate blob from id. For a more complete description, see http://www.freebase.com/view/en/api_trans_raw""" url = '/api/trans/raw' + urlquote(id) self.log.info(url) resp, body = self._httpreq(url) self.log.info('raw is %d bytes' % len(body)) return body
def search(self, title): """Searches for a film by its title. Returns SearchResults (a list) containing all matches (Movie instances) """ title = urlquote(title.encode("utf-8")) url = config['urls']['movie.search'] % (title) etree = XmlHandler(url).getEt() search_results = SearchResults() for cur_result in etree.find("movies").findall("movie"): cur_movie = self._parseSearchResults(cur_result) search_results.append(cur_movie) return search_results
def _encode_params(kw): """ Encode parameters. """ args = [] for k, v in sorted(kw.items()): try: # Python 2 qv = v.encode('utf-8') if isinstance(v, unicode) else str(v) except Exception: qv = v args.append('%s=%s' % (k, urlquote(qv))) return '&'.join(args)
def _get(self, name): assert self._lock is not None index = self._index(name) num = self._lookup[index] if num == self._EMPTY: raise KeyError(name) elif num == self._COLLISION: # lookupsize should be made large enough that collisions are unlikely collisionsfilename = os.path.join(self.directory, self.COLLISIONS_DIR, repr(index)) with open(collisionsfilename, "r") as collisionsfile: num = json.load(collisionsfile)[urlquote(name, safe="")] dir, prefix = os.path.split(self._num2path(num)) path = os.path.join( dir, prefix + self.config.delimiter + urlquote(name, safe="")) if not os.path.exists(path): raise KeyError(name) else: return path