Python urllib.request.url2pathname() Examples

The following are 17 code examples of urllib.request.url2pathname(). 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 urllib.request , or try the search function .
Example #1
Source File: bazelci.py    From continuous-integration with Apache License 2.0 6 votes vote down vote up
def test_logs_for_status(bep_file, status):
    targets = []
    with open(bep_file, encoding="utf-8") as f:
        raw_data = f.read()
    decoder = json.JSONDecoder()

    pos = 0
    while pos < len(raw_data):
        try:
            bep_obj, size = decoder.raw_decode(raw_data[pos:])
        except ValueError as e:
            eprint("JSON decoding error: " + str(e))
            return targets
        if "testSummary" in bep_obj:
            test_target = bep_obj["id"]["testSummary"]["label"]
            test_status = bep_obj["testSummary"]["overallStatus"]
            if test_status in status:
                outputs = bep_obj["testSummary"]["failed"]
                test_logs = []
                for output in outputs:
                    test_logs.append(url2pathname(urlparse(output["uri"]).path))
                targets.append((test_target, test_logs))
        pos += size + 1
    return targets 
Example #2
Source File: preferencesDialog.py    From pychess with GNU General Public License v3.0 6 votes vote down vote up
def playAction(cls, action):
        if not cls.useSounds:
            return

        if isinstance(action, str):
            key_no = cls.actionToKeyNo[action]
        else:
            key_no = action
        typ = cls.soundcombo[key_no]
        if typ == SOUND_BEEP:
            sys.stdout.write("\a")
            sys.stdout.flush()
        elif typ == SOUND_URI:
            uri = cls.sounduri[key_no]
            if not os.path.isfile(url2pathname(uri[5:])):
                conf.set("soundcombo%d" % key_no, SOUND_MUTE)
                return
            cls.getPlayer().play(uri) 
Example #3
Source File: content_helpers.py    From djangocms-spa with MIT License 6 votes vote down vote up
def get_partial_names_for_template(template=None, get_all=True, requested_partials=None):
    template = template or settings.DJANGOCMS_SPA_DEFAULT_TEMPLATE

    if requested_partials:
        # Transform the requested partials into a list
        requested_partials = url2pathname(requested_partials).split(',')
    else:
        requested_partials = []

    try:
        partials = settings.DJANGOCMS_SPA_TEMPLATES[template]['partials']
    except KeyError:
        try:
            default_template_path = settings.DJANGOCMS_SPA_DEFAULT_TEMPLATE
            partials = settings.DJANGOCMS_SPA_TEMPLATES[default_template_path]['partials']
        except KeyError:
            partials = []

    if get_all:
        return partials
    else:
        return [partial for partial in partials if partial in requested_partials] 
Example #4
Source File: Main.py    From pychess with GNU General Public License v3.0 5 votes vote down vote up
def update_recent(self, gamemodel, uri):
        if isinstance(uri, str):
            path = url2pathname(uri)
            recent_manager.add_item("file:" + pathname2url(path)) 
Example #5
Source File: urlpath.py    From rstWeb with MIT License 5 votes vote down vote up
def nativejoin(base, path):
    """
    Joins two paths - returning a native file path.

    Given a base path and a relative location, (in posix format)
    return a file path in a (relatively) OS native way.
    """
    return url2pathname(pathjoin(base, path)) 
Example #6
Source File: util.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def url2path(path):
    """Path to URL."""

    return url2pathname(path) 
Example #7
Source File: url.py    From LSP with MIT License 5 votes vote down vote up
def uri_to_filename(uri: str) -> str:
    if os.name == 'nt':
        # url2pathname does not understand %3A (VS Code's encoding forced on all servers :/)
        return url2pathname(urlparse(uri).path).strip('\\')
    else:
        return url2pathname(urlparse(uri).path) 
Example #8
Source File: recipe-578957.py    From code with MIT License 5 votes vote down vote up
def s3_open(self, req):
        # The implementation was inspired mainly by the code behind
        # urllib.request.FileHandler.file_open().

        bucket_name = req.host
        key_name = url2pathname(req.selector)[1:]

        if not bucket_name or not key_name:
            raise URLError('url must be in the format s3://<bucket>/<key>')

        try:
            conn = self._conn
        except AttributeError:
            conn = self._conn = boto.s3.connection.S3Connection()

        bucket = conn.get_bucket(bucket_name, validate=False)
        key = bucket.get_key(key_name)

        origurl = 's3://{}/{}'.format(bucket_name, key_name)

        if key is None:
            raise URLError('no such resource: {}'.format(origurl))

        headers = [
            ('Content-type', key.content_type),
            ('Content-encoding', key.content_encoding),
            ('Content-language', key.content_language),
            ('Content-length', key.size),
            ('Etag', key.etag),
            ('Last-modified', key.last_modified),
        ]

        headers = email.message_from_string(
            '\n'.join('{}: {}'.format(key, value) for key, value in headers
                      if value is not None))

        return addinfourl(_FileLikeKey(key), headers, origurl) 
Example #9
Source File: handlers.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def file_path(self, url):
        """
        Return the relative path to the media file on disk for the given URL.
        """
        relative_url = url[len(self.base_url[2]):]
        return url2pathname(relative_url) 
Example #10
Source File: testcases.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def file_path(self, url):
        """Return the relative path to the file on disk for the given URL."""
        relative_url = url[len(self.base_url[2]):]
        return url2pathname(relative_url) 
Example #11
Source File: testcases.py    From bioforum with MIT License 5 votes vote down vote up
def file_path(self, url):
        """Return the relative path to the file on disk for the given URL."""
        relative_url = url[len(self.base_url[2]):]
        return url2pathname(relative_url) 
Example #12
Source File: Main.py    From pychess with GNU General Public License v3.0 5 votes vote down vote up
def on_recent_game_activated(self, uri):
        if isinstance(uri, str):
            path = url2pathname(uri)
            recent_manager.add_item("file:" + pathname2url(path))

    # Drag 'n' Drop 
Example #13
Source File: gstreamer.py    From pychess with GNU General Public License v3.0 5 votes vote down vote up
def play(self, uri):
            if self.player is not None:
                try:
                    self.stdin.write(url2pathname(uri[5:]) + "\n")
                except BrokenPipeError:
                    pass 
Example #14
Source File: gstreamer.py    From pychess with GNU General Public License v3.0 5 votes vote down vote up
def play(self, uri):
            try:
                winsound.PlaySound(None, 0)
                winsound.PlaySound(
                    url2pathname(uri[5:]), winsound.SND_FILENAME | winsound.SND_ASYNC)
            except RuntimeError:
                log.error("ERROR: RuntimeError while playing %s." %
                          url2pathname(uri[5:])) 
Example #15
Source File: handlers.py    From bioforum with MIT License 5 votes vote down vote up
def file_path(self, url):
        """
        Return the relative path to the media file on disk for the given URL.
        """
        relative_url = url[len(self.base_url[2]):]
        return url2pathname(relative_url) 
Example #16
Source File: main.py    From kitty with GNU General Public License v3.0 4 votes vote down vote up
def process_single_item(
    item: Union[bytes, str],
    args: IcatCLIOptions,
    parsed_opts: ParsedOpts,
    url_pat: Optional[Pattern] = None,
    maybe_dir: bool = True
) -> None:
    is_tempfile = False
    file_removed = False
    try:
        if isinstance(item, bytes):
            tf = NamedTemporaryFile(prefix='stdin-image-data-', delete=False)
            tf.write(item), tf.close()
            item = tf.name
            is_tempfile = True
        if url_pat is not None and url_pat.match(item) is not None:
            from urllib.request import urlretrieve
            with NamedTemporaryFile(prefix='url-image-data-', delete=False) as tf:
                try:
                    with socket_timeout(30):
                        urlretrieve(item, filename=tf.name)
                except Exception as e:
                    raise SystemExit('Failed to download image at URL: {} with error: {}'.format(item, e))
                item = tf.name
            is_tempfile = True
            file_removed = process(item, args, parsed_opts, is_tempfile)
        elif item.lower().startswith('file://'):
            from urllib.parse import urlparse
            from urllib.request import url2pathname
            pitem = urlparse(item)
            if os.sep == '\\':
                item = pitem.netloc + pitem.path
            else:
                item = pitem.path
            item = url2pathname(item)
            file_removed = process(item, args, parsed_opts, is_tempfile)
        else:
            if maybe_dir and os.path.isdir(item):
                for (x, mt) in scan(item):
                    process_single_item(x, args, parsed_opts, url_pat=None, maybe_dir=False)
            else:
                file_removed = process(item, args, parsed_opts, is_tempfile)
    finally:
        if is_tempfile and not file_removed:
            os.remove(item) 
Example #17
Source File: utils.py    From kelner with MIT License 4 votes vote down vote up
def s3_open(self, req):
        # The implementation was inspired mainly by the code behind
        # urllib.request.FileHandler.file_open().
        #
        # recipe copied from:
        # http://code.activestate.com/recipes/578957-urllib-handler-for-amazon-s3-buckets/
        # converted to boto3

        if version_info[0] < 3:
            bucket_name = req.get_host()
            key_name = url2pathname(req.get_selector())[1:]
        else:
            bucket_name = req.host
            key_name = url2pathname(req.selector)[1:]

        if not bucket_name or not key_name:
            raise URLError('url must be in the format s3://<bucket>/<key>')

        s3 = boto3.resource('s3')

        key = s3.Object(bucket_name, key_name)

        client = boto3.client('s3')
        obj = client.get_object(Bucket=bucket_name, Key=key_name)
        filelike = _FileLikeKey(obj['Body'])

        origurl = 's3://{}/{}'.format(bucket_name, key_name)

        if key is None:
            raise URLError('no such resource: {}'.format(origurl))

        headers = [
            ('Content-type', key.content_type),
            ('Content-encoding', key.content_encoding),
            ('Content-language', key.content_language),
            ('Content-length', key.content_length),
            ('Etag', key.e_tag),
            ('Last-modified', key.last_modified),
        ]

        headers = email.message_from_string(
            '\n'.join('{}: {}'.format(key, value) for key, value in headers
                      if value is not None))
        return addinfourl(filelike, headers, origurl)