Python six.moves.urllib.parse.unquote() Examples

The following are 30 code examples of six.moves.urllib.parse.unquote(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module six.moves.urllib.parse , or try the search function .
Example #1
Source File: root.py    From allura with Apache License 2.0 6 votes vote down vote up
def create_topic(self, forum_name=None, new_forum=False, **kw):
        forums = model.Forum.query.find(dict(app_config_id=c.app.config._id,
                                             parent_id=None,
                                             deleted=False))
        c.new_topic = self.W.new_topic
        my_forums = []
        forum_name = h.really_unicode(unquote(forum_name)) if forum_name else None
        current_forum = None
        for f in forums:
            if forum_name == f.shortname:
                current_forum = f
            if has_access(f, 'post')():
                my_forums.append(f)
        return dict(forums=my_forums,
                    current_forum=current_forum,
                    subscribed=M.Mailbox.subscribed(artifact=current_forum),
                    subscribed_to_tool=M.Mailbox.subscribed(),
                    ) 
Example #2
Source File: url.py    From pipenv with MIT License 6 votes vote down vote up
def _get_parsed_url(url):
    # type: (S) -> Url
    """This is a stand-in function for `urllib3.util.parse_url`

    The orignal function doesn't handle special characters very well, this simply splits
    out the authentication section, creates the parsed url, then puts the authentication
    section back in, bypassing validation.

    :return: The new, parsed URL object
    :rtype: :class:`~urllib3.util.url.Url`
    """

    try:
        parsed = urllib3_parse(url)
    except ValueError:
        scheme, _, url = url.partition("://")
        auth, _, url = url.rpartition("@")
        url = "{scheme}://{url}".format(scheme=scheme, url=url)
        parsed = urllib3_parse(url)._replace(auth=auth)
    if parsed.auth:
        return parsed._replace(auth=url_unquote(parsed.auth))
    return parsed 
Example #3
Source File: test_tableau.py    From luigi-td with Apache License 2.0 6 votes vote down vote up
def test_default(self):
        target = TestTableauServerResultTarget(test_config.get_tmp_path('result.job'))
        target.datasource = 'test-datasource'
        url = urlparse(target.get_result_url())
        params = parse_qs(url.query)
        eq_(url.scheme, 'tableau')
        eq_(url.hostname, 'tableau.example.com')
        eq_(url.path, '/' + target.datasource)
        eq_(url_unquote(url.username), TestTableauServerResultTarget.username)
        eq_(url_unquote(url.password), TestTableauServerResultTarget.password)
        eq_(params.get('ssl'), ['true'])
        eq_(params.get('ssl_verify'), ['true'])
        eq_(params.get('server_version'), None)
        eq_(params.get('site'), None)
        eq_(params.get('project'), None)
        eq_(params.get('mode'), ['replace']) 
Example #4
Source File: requirements.py    From pipenv with MIT License 6 votes vote down vote up
def line_part(self):
        # type: () -> STRING_TYPE
        link_url = None  # type: Optional[STRING_TYPE]
        seed = None  # type: Optional[STRING_TYPE]
        if self.link is not None:
            link_url = unquote(self.link.url_without_fragment)
        is_vcs = getattr(self.link, "is_vcs", not self.link.is_artifact)
        if self._uri_scheme and self._uri_scheme == "path":
            # We may need any one of these for passing to pip
            seed = self.path or link_url or self.uri
        elif (self._uri_scheme and self._uri_scheme == "file") or (
            (self.link.is_wheel or not is_vcs) and self.link.url
        ):
            seed = link_url or self.uri
        # add egg fragments to remote artifacts (valid urls only)
        if not self._has_hashed_name and self.is_remote_artifact and seed is not None:
            seed += "#egg={0}".format(self.name)
        editable = "-e " if self.editable else ""
        if seed is None:
            raise ValueError("Could not calculate url for {0!r}".format(self))
        return "{0}{1}".format(editable, seed) 
Example #5
Source File: dataurl.py    From pyspider with Apache License 2.0 6 votes vote down vote up
def decode(data_url):
    """
    Decode DataURL data
    """
    metadata, data = data_url.rsplit(',', 1)
    _, metadata = metadata.split('data:', 1)
    parts = metadata.split(';')
    if parts[-1] == 'base64':
        data = b64decode(data)
    else:
        data = unquote(data)

    for part in parts:
        if part.startswith("charset="):
            data = data.decode(part[8:])
    return data 
Example #6
Source File: s3server.py    From ec2-api with Apache License 2.0 6 votes vote down vote up
def get(self, bucket, object_name):
        object_name = parse.unquote(object_name)
        path = self._object_path(bucket, object_name)
        if (not path.startswith(self.application.directory) or
                not os.path.isfile(path)):
            self.set_404()
            return
        info = os.stat(path)
        self.set_header("Content-Type", "application/unknown")
        self.set_header("Last-Modified", datetime.datetime.utcfromtimestamp(
            info.st_mtime))
        object_file = open(path, "rb")
        try:
            self.finish(object_file.read())
        finally:
            object_file.close() 
Example #7
Source File: s3server.py    From ec2-api with Apache License 2.0 6 votes vote down vote up
def put(self, bucket, object_name):
        object_name = parse.unquote(object_name)
        bucket_dir = os.path.abspath(os.path.join(
            self.application.directory, bucket))
        if (not bucket_dir.startswith(self.application.directory) or
                not os.path.isdir(bucket_dir)):
            self.set_404()
            return
        path = self._object_path(bucket, object_name)
        if not path.startswith(bucket_dir) or os.path.isdir(path):
            self.set_status(403)
            return
        directory = os.path.dirname(path)
        fileutils.ensure_tree(directory)
        object_file = open(path, "wb")
        object_file.write(self.request.body)
        object_file.close()
        self.set_header('ETag',
                        '"%s"' % utils.get_hash_str(self.request.body))
        self.finish() 
Example #8
Source File: owncloud.py    From pyocclient with MIT License 6 votes vote down vote up
def _parse_dav_element(self, dav_response):
        """Parses a single DAV element

        :param dav_response: DAV response
        :returns :class:`FileInfo`
        """
        href = parse.unquote(
            self._strip_dav_path(dav_response.find('{DAV:}href').text)
        )

        if six.PY2:
            href = href.decode('utf-8')

        file_type = 'file'
        if href[-1] == '/':
            file_type = 'dir'

        file_attrs = {}
        attrs = dav_response.find('{DAV:}propstat')
        attrs = attrs.find('{DAV:}prop')
        for attr in attrs:
            file_attrs[attr.tag] = attr.text

        return FileInfo(href, file_type, file_attrs) 
Example #9
Source File: base.py    From gnocchi with Apache License 2.0 6 votes vote down vote up
def post_account(self, headers, query_string=None, data=None,
                     response_dict=None):
        if query_string == 'bulk-delete':
            resp = {'Response Status': '200 OK',
                    'Response Body': '',
                    'Number Deleted': 0,
                    'Number Not Found': 0}
            if response_dict is not None:
                response_dict['status'] = 200
            if data:
                for path in data.splitlines():
                    try:
                        __, container, obj = (unquote(path.decode('utf8'))
                                              .split('/', 2))
                        del self.kvs[container][obj]
                        resp['Number Deleted'] += 1
                    except KeyError:
                        resp['Number Not Found'] += 1
            return {}, json.dumps(resp).encode('utf-8')

        if response_dict is not None:
            response_dict['status'] = 204

        return {}, None 
Example #10
Source File: protocol.py    From switchio with Mozilla Public License 2.0 6 votes vote down vote up
def parse_frame(frame):
        parsed = unquote(frame)
        chunk = {}
        last_key = 'Body'
        for line in parsed.strip().splitlines():
            if not line:
                last_key = 'Body'
                continue
            key, sep, value = line.partition(': ')
            if sep and key and key[0] is not '+':  # 'key: value' header
                last_key = key
                chunk[key] = value
            else:
                # no sep - 2 cases: multi-line value or body content
                chunk[last_key] = chunk.setdefault(
                    last_key, '') + line + '\n'
        return chunk 
Example #11
Source File: httpserver.py    From stash with MIT License 6 votes vote down vote up
def translate_path(self, path):
        """Translate a /-separated PATH to the local filename syntax.

        Components that mean special things to the local file system
        (e.g. drive or directory names) are ignored.  (XXX They should
        probably be diagnosed.)

        """
        # abandon query parameters
        path = path.split('?', 1)[0]
        path = path.split('#', 1)[0]
        path = posixpath.normpath(unquote(path))
        words = path.split('/')
        words = filter(None, words)
        path = os.getcwd()
        for word in words:
            drive, word = os.path.splitdrive(word)
            head, word = os.path.split(word)
            if word in (os.curdir, os.pardir): continue
            path = os.path.join(path, word)
        return path 
Example #12
Source File: http.py    From alfred-gmail with MIT License 6 votes vote down vote up
def _header_to_id(self, header):
    """Convert a Content-ID header value to an id.

    Presumes the Content-ID header conforms to the format that _id_to_header()
    returns.

    Args:
      header: string, Content-ID header value.

    Returns:
      The extracted id value.

    Raises:
      BatchError if the header is not in the expected format.
    """
    if header[0] != '<' or header[-1] != '>':
      raise BatchError("Invalid value for Content-ID: %s" % header)
    if '+' not in header:
      raise BatchError("Invalid value for Content-ID: %s" % header)
    base, id_ = header[1:-1].split(' + ', 1)

    return unquote(id_) 
Example #13
Source File: http.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _header_to_id(self, header):
    """Convert a Content-ID header value to an id.

    Presumes the Content-ID header conforms to the format that _id_to_header()
    returns.

    Args:
      header: string, Content-ID header value.

    Returns:
      The extracted id value.

    Raises:
      BatchError if the header is not in the expected format.
    """
    if header[0] != '<' or header[-1] != '>':
      raise BatchError("Invalid value for Content-ID: %s" % header)
    if '+' not in header:
      raise BatchError("Invalid value for Content-ID: %s" % header)
    base, id_ = header[1:-1].rsplit('+', 1)

    return unquote(id_) 
Example #14
Source File: http.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _header_to_id(self, header):
    """Convert a Content-ID header value to an id.

    Presumes the Content-ID header conforms to the format that _id_to_header()
    returns.

    Args:
      header: string, Content-ID header value.

    Returns:
      The extracted id value.

    Raises:
      BatchError if the header is not in the expected format.
    """
    if header[0] != '<' or header[-1] != '>':
      raise BatchError("Invalid value for Content-ID: %s" % header)
    if '+' not in header:
      raise BatchError("Invalid value for Content-ID: %s" % header)
    base, id_ = header[1:-1].rsplit('+', 1)

    return unquote(id_) 
Example #15
Source File: codec.py    From pwnypack with MIT License 6 votes vote down vote up
def deurlquote(d, plus=False):
    """
    Decode a percent encoded string.

    Args:
        d(str): The percent encoded value to decode.
        plus(bool): Parse a plus symbol as a space.

    Returns:
        str: The decoded version of the percent encoded of ``d``.

    Example:
        >>> from pwny import *
        >>> deurlquote('Foo+Bar/Baz')
        'Foo Bar/Baz'
    """
    return unquote_plus(d) if plus else unquote(d) 
Example #16
Source File: repository.py    From allura with Apache License 2.0 6 votes vote down vote up
def index(self, **kw):
        c.tree_widget = self.tree_widget
        c.subscribe_form = self.subscribe_form
        tool_subscribed = M.Mailbox.subscribed()
        tarball_url = None
        if asbool(tg.config.get('scm.repos.tarball.enable', False)):
            cutout = len(b'tree' + self._path.encode('utf8'))
            if request.path.endswith('/') and not self._path.endswith('/'):
                cutout += 1
            tarball_url = h.urlquote('%starball' % unquote(request.path)[:-cutout])
        return dict(
            repo=c.app.repo,
            commit=self._commit,
            tree=self._tree,
            path=self._path,
            parent=self._parent,
            tool_subscribed=tool_subscribed,
            tarball_url=tarball_url) 
Example #17
Source File: project.py    From allura with Apache License 2.0 6 votes vote down vote up
def _lookup(self, name, *remainder):
        name = unquote(name)
        name = six.ensure_text(name)  # we don't support unicode names, but in case a url comes in with one
        if name == '_nav.json':
            return self, ['_nav']

        if c.project.deleted:
            if c.user not in c.project.admins():
                raise exc.HTTPNotFound(name)
        app = c.project.app_instance(name)

        if app:
            c.app = app
            if app.root:
                return app.root, remainder
        subproject = M.Project.query.get(
            shortname=c.project.shortname + '/' + name,
            neighborhood_id=c.project.neighborhood_id)
        if subproject:
            c.project = subproject
            c.app = None
            return ProjectController(), remainder
        raise exc.HTTPNotFound(name) 
Example #18
Source File: loginsight.py    From osprofiler with Apache License 2.0 6 votes vote down vote up
def __init__(
            self, connection_str, project=None, service=None, host=None,
            **kwargs):
        super(LogInsightDriver, self).__init__(connection_str,
                                               project=project,
                                               service=service,
                                               host=host)

        parsed_connection = urlparse.urlparse(connection_str)
        try:
            creds, host = parsed_connection.netloc.split("@")
            username, password = creds.split(":")
        except ValueError:
            raise ValueError("Connection string format is: loginsight://"
                             "<username>:<password>@<loginsight-host>. If the "
                             "username or password contains the character '@' "
                             "or ':', it must be escaped using URL encoding.")

        username = urlparse.unquote(username)
        password = urlparse.unquote(password)
        self._client = LogInsightClient(host, username, password)

        self._client.login() 
Example #19
Source File: url.py    From requirementslib with MIT License 6 votes vote down vote up
def _get_parsed_url(url):
    # type: (S) -> Url
    """This is a stand-in function for `urllib3.util.parse_url`

    The orignal function doesn't handle special characters very well, this simply splits
    out the authentication section, creates the parsed url, then puts the authentication
    section back in, bypassing validation.

    :return: The new, parsed URL object
    :rtype: :class:`~urllib3.util.url.Url`
    """

    try:
        parsed = urllib3_parse(url)
    except ValueError:
        scheme, _, url = url.partition("://")
        auth, _, url = url.rpartition("@")
        url = "{scheme}://{url}".format(scheme=scheme, url=url)
        parsed = urllib3_parse(url)._replace(auth=auth)
    if parsed.auth:
        return parsed._replace(auth=url_unquote(parsed.auth))
    return parsed 
Example #20
Source File: requirements.py    From requirementslib with MIT License 6 votes vote down vote up
def line_part(self):
        # type: () -> STRING_TYPE
        link_url = None  # type: Optional[STRING_TYPE]
        seed = None  # type: Optional[STRING_TYPE]
        if self.link is not None:
            link_url = unquote(self.link.url_without_fragment)
        is_vcs = getattr(self.link, "is_vcs", not self.link.is_artifact)
        if self._uri_scheme and self._uri_scheme == "path":
            # We may need any one of these for passing to pip
            seed = self.path or link_url or self.uri
        elif (self._uri_scheme and self._uri_scheme == "file") or (
            (self.link.is_wheel or not is_vcs) and self.link.url
        ):
            seed = link_url or self.uri
        # add egg fragments to remote artifacts (valid urls only)
        if not self._has_hashed_name and self.is_remote_artifact and seed is not None:
            seed += "#egg={0}".format(self.name)
        editable = "-e " if self.editable else ""
        if seed is None:
            raise ValueError("Could not calculate url for {0!r}".format(self))
        return "{0}{1}".format(editable, seed) 
Example #21
Source File: __init__.py    From awslog with MIT License 6 votes vote down vote up
def prettify(value):
    if isinstance(value, list):
        return [prettify(i) for i in value]

    if isinstance(value, dict):
        return {k: prettify(v) for k, v in sorted(value.items())}

    if isinstance(value, string_types):
        if value.startswith('%7B'):  # URL encoded {
            decoded_value = urlunquote(value)
        else:
            decoded_value = value

        try:
            json_value = json.loads(decoded_value)
        except ValueError:
            return value
        else:
            return prettify(json_value)

    return value 
Example #22
Source File: airtable.py    From airtable-python-wrapper with MIT License 6 votes vote down vote up
def _process_response(self, response):
        try:
            response.raise_for_status()
        except requests.exceptions.HTTPError as exc:
            err_msg = str(exc)

            # Reports Decoded 422 Url for better troubleshooting
            # Disabled in IronPython Bug:
            # https://github.com/IronLanguages/ironpython2/issues/242
            if not IS_IPY and response.status_code == 422:
                err_msg = err_msg.replace(response.url, unquote(response.url))
                err_msg += " (Decoded URL)"

            # Attempt to get Error message from response, Issue #16
            try:
                error_dict = response.json()
            except ValueError:
                pass
            else:
                if "error" in error_dict:
                    err_msg += " [Error: {}]".format(error_dict["error"])
            raise requests.exceptions.HTTPError(err_msg)
        else:
            return response.json() 
Example #23
Source File: alarm_receiver.py    From tacker with Apache License 2.0 6 votes vote down vote up
def handle_url(self, url):
        # alarm_url = 'http://host:port/v1.0/vnfs/vnf-uuid/mon-policy-name/action-name/8ef785' # noqa
        parts = parse.urlparse(url)
        p = parts.path.split('/')
        if len(p) != 7:
            return None

        if any((p[0] != '', p[2] != 'vnfs')):
            return None
        # decode action name: respawn%25log
        p[5] = parse.unquote(p[5])
        qs = parse.parse_qs(parts.query)
        params = dict((k, v[0]) for k, v in qs.items())
        prefix_url = '/%(collec)s/%(vnf_uuid)s/' % {'collec': p[2],
                                                    'vnf_uuid': p[3]}
        return prefix_url, p, params 
Example #24
Source File: aliyun_account.py    From aliyun-datahub-sdk-python with Apache License 2.0 6 votes vote down vote up
def sign_request(self, request):
        """
        Generator signature for request.

        :param request: request object
        :return: none
        """
        url = request.path_url
        url_components = urlparse(unquote(url))
        canonical_str = self._build_canonical_str(url_components, request)
        logger.debug('canonical string: ' + canonical_str)

        sign = to_str(hmac_sha1(self._access_key, canonical_str))

        auth_str = 'DATAHUB %s:%s' % (self._access_id, sign)
        request.headers[Headers.AUTHORIZATION] = auth_str 
Example #25
Source File: http.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _header_to_id(self, header):
    """Convert a Content-ID header value to an id.

    Presumes the Content-ID header conforms to the format that _id_to_header()
    returns.

    Args:
      header: string, Content-ID header value.

    Returns:
      The extracted id value.

    Raises:
      BatchError if the header is not in the expected format.
    """
    if header[0] != '<' or header[-1] != '>':
      raise BatchError("Invalid value for Content-ID: %s" % header)
    if '+' not in header:
      raise BatchError("Invalid value for Content-ID: %s" % header)
    base, id_ = header[1:-1].rsplit('+', 1)

    return unquote(id_) 
Example #26
Source File: http.py    From luci-py with Apache License 2.0 6 votes vote down vote up
def _header_to_id(self, header):
    """Convert a Content-ID header value to an id.

    Presumes the Content-ID header conforms to the format that _id_to_header()
    returns.

    Args:
      header: string, Content-ID header value.

    Returns:
      The extracted id value.

    Raises:
      BatchError if the header is not in the expected format.
    """
    if header[0] != '<' or header[-1] != '>':
      raise BatchError("Invalid value for Content-ID: %s" % header)
    if '+' not in header:
      raise BatchError("Invalid value for Content-ID: %s" % header)
    base, id_ = header[1:-1].rsplit('+', 1)

    return unquote(id_) 
Example #27
Source File: index.py    From allura with Apache License 2.0 5 votes vote down vote up
def from_links(cls, *links):
        '''Convert a sequence of shortlinks to the matching Shortlink objects'''
        if len(links):
            result = {}
            # Parse all the links
            parsed_links = dict((link, cls._parse_link(link))
                                for link in links)
            links_by_artifact = defaultdict(list)
            project_ids = set()
            for link, d in list(parsed_links.items()):
                if d:
                    project_ids.add(d['project_id'])
                    links_by_artifact[unquote(d['artifact'])].append(d)
                else:
                    result[link] = parsed_links.pop(link)
            q = cls.query.find(dict(
                link={'$in': list(links_by_artifact.keys())},
                project_id={'$in': list(project_ids)}
            ), validate=False)
            matches_by_artifact = dict(
                (link, list(matches))
                for link, matches in groupby(q, key=lambda s: unquote(s.link)))
            for link, d in six.iteritems(parsed_links):
                matches = matches_by_artifact.get(unquote(d['artifact']), [])
                matches = (
                    m for m in matches
                    if m.project.shortname == d['project'] and
                    m.project.neighborhood_id == d['nbhd'] and
                    m.app_config is not None and
                    m.project.app_instance(m.app_config.options.mount_point))
                if d['app']:
                    matches = (
                        m for m in matches
                        if m.app_config.options.mount_point == d['app'])
                result[link] = cls._get_correct_match(link, list(matches))
            return result
        else:
            return {} 
Example #28
Source File: wiki_main.py    From allura with Apache License 2.0 5 votes vote down vote up
def __init__(self, title):
        self.title = h.really_unicode(unquote(title)) if title else None
        self.page = WM.Page.query.get(app_config_id=c.app.config._id,
                                      title=self.title,
                                      deleted=False) 
Example #29
Source File: url.py    From requirementslib with MIT License 5 votes vote down vote up
def get_password(self, unquote=False, include_token=True):
        # type: (bool, bool) -> str
        password = self.password if self.password else ""
        if password and unquote and self._password_is_quoted:
            password = url_unquote(password)
        return password 
Example #30
Source File: attachments.py    From allura with Apache License 2.0 5 votes vote down vote up
def _lookup(self, filename=None, *args):
        if filename:
            if not args:
                filename = request.path.rsplit(str('/'), 1)[-1]
            filename = unquote(filename)
            return self.AttachmentControllerClass(filename, self.artifact), args
        else:
            raise exc.HTTPNotFound