Python urllib.pathname2url() Examples

The following are 30 code examples of urllib.pathname2url(). 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 , or try the search function .
Example #1
Source File: test_urllib2.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_trivial(self):
        # A couple trivial tests

        self.assertRaises(ValueError, urllib2.urlopen, 'bogus url')

        # XXX Name hacking to get this to work on Windows.
        fname = os.path.abspath(urllib2.__file__).replace('\\', '/')

        # And more hacking to get it to work on MacOS. This assumes
        # urllib.pathname2url works, unfortunately...
        if os.name == 'riscos':
            import string
            fname = os.expand(fname)
            fname = fname.translate(string.maketrans("/.", "./"))

        if os.name == 'nt':
            file_url = "file:///%s" % fname
        else:
            file_url = "file://%s" % fname

        f = urllib2.urlopen(file_url)

        buf = f.read()
        f.close() 
Example #2
Source File: validation_util.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def validate_json(json_data, schema_file):
    with open(schema_file) as f:
        schema = json.load(f)

    schema_dir = os.path.dirname(schema_file)
    schema_path = 'file://{0}/'.format(pathname2url(schema_dir))

    resolver = RefResolver(schema_path, schema)

    try:
        Draft4Validator.check_schema(schema)
        validate(json_data, schema, resolver=resolver)
    except SchemaError as e:
        raise Exception("Failed to check JSON schema in {}: {}".format(schema_file, e.message))
    except ValidationError as e:
        raise Exception("Unable to validate data against json schema in {}: {}, {}, {}, {}, {}".format(schema_file, e.message, e.context, e.path, e.schema_path, e.cause)) 
Example #3
Source File: test_urllib2.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_trivial(self):
        # A couple trivial tests

        self.assertRaises(ValueError, urllib2.urlopen, 'bogus url')

        # XXX Name hacking to get this to work on Windows.
        fname = os.path.abspath(urllib2.__file__).replace(os.sep, '/')

        # And more hacking to get it to work on MacOS. This assumes
        # urllib.pathname2url works, unfortunately...
        if os.name == 'riscos':
            import string
            fname = os.expand(fname)
            fname = fname.translate(string.maketrans("/.", "./"))

        if os.name == 'nt':
            file_url = "file:///%s" % fname
        else:
            file_url = "file://%s" % fname

        f = urllib2.urlopen(file_url)

        buf = f.read()
        f.close() 
Example #4
Source File: runtestsuite.py    From blenderseed with MIT License 6 votes vote down vote up
def report_detailed_failure(self, scene, reference_filepath, output_filepath, log_filepath, error_message, num_diff, max_diff, num_comps, diff_filepath):
        self.failures += 1

        command = self.__make_update_command(output_filepath, reference_filepath)
        self.all_commands.append(command)

        self.file.write(self.__render(self.detailed_failure_template,
                                      {'project-path': scene,
                                       'ref-image-url': urllib.pathname2url(reference_filepath),
                                       'diff-image-url': urllib.pathname2url(diff_filepath) if diff_filepath is not None else "",
                                       'output-image-url': urllib.pathname2url(output_filepath),
                                       'failure-reason': error_message,
                                       'log-file-url': urllib.pathname2url(log_filepath),
                                       'log-file-path': os.path.basename(log_filepath),
                                       'max-abs-diff': max_diff,
                                       'diff-comps-count': num_diff,
                                       'diff-comps-percents': "{0:.2f}".format(100.0 * num_diff / num_comps),
                                       'update-command': command}))
        self.file.flush() 
Example #5
Source File: remote.py    From guides-cms with GNU Affero General Public License v3.0 6 votes vote down vote up
def contents_url_from_path(path):
    """
    Get github API url for contents of file from full path

    :param path: Path to file (<owner>/<repo>/<dir>/.../<filename>)
    :returns: URL suitable for a content call with github API
    """

    owner, repo, file_path = split_full_file_path(path)

    # Cannot pass unicode data to pathname2url or it can raise KeyError. Must
    # only pass URL-safe bytes. So, something like u'\u2026' will raise a
    # KeyError but if we encode it to bytes, '%E2%80%A6', things work
    # correctly.
    # http://stackoverflow.com/questions/15115588/urllib-quote-throws-keyerror
    owner = owner.encode('utf-8')
    repo = repo.encode('utf-8')
    file_path = file_path.encode('utf-8')

    return urllib.pathname2url('repos/%s/%s/contents/%s' % (owner, repo,
                                                            file_path)) 
Example #6
Source File: imports.py    From cloudify-dsl-parser with Apache License 2.0 6 votes vote down vote up
def _get_resource_location(resource_name,
                           resources_base_path,
                           current_resource_context=None):
    url_parts = resource_name.split(':')
    if url_parts[0] in ['http', 'https', 'file', 'ftp', 'plugin']:
        return resource_name

    if os.path.exists(resource_name):
        return 'file:{0}'.format(
            urllib.pathname2url(os.path.abspath(resource_name)))

    if current_resource_context:
        candidate_url = current_resource_context[
            :current_resource_context.rfind('/') + 1] + resource_name
        if utils.url_exists(candidate_url):
            return candidate_url

    if resources_base_path:
        full_path = os.path.join(resources_base_path, resource_name)
        return 'file:{0}'.format(
            urllib.pathname2url(os.path.abspath(full_path)))

    return None 
Example #7
Source File: test_urllib2.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def test_trivial(self):
        # A couple trivial tests

        self.assertRaises(ValueError, urllib2.urlopen, 'bogus url')

        # XXX Name hacking to get this to work on Windows.
        fname = os.path.abspath(urllib2.__file__).replace('\\', '/')
        if fname[1:2] == ":":
            fname = fname[2:]
        # And more hacking to get it to work on MacOS. This assumes
        # urllib.pathname2url works, unfortunately...
        if os.name == 'mac':
            fname = '/' + fname.replace(':', '/')
        elif os.name == 'riscos':
            import string
            fname = os.expand(fname)
            fname = fname.translate(string.maketrans("/.", "./"))

        file_url = "file://%s" % fname
        f = urllib2.urlopen(file_url)

        buf = f.read()
        f.close() 
Example #8
Source File: test_urllib2.py    From gcblue with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_trivial(self):
        # A couple trivial tests

        self.assertRaises(ValueError, urllib2.urlopen, 'bogus url')

        # XXX Name hacking to get this to work on Windows.
        fname = os.path.abspath(urllib2.__file__).replace('\\', '/')

        # And more hacking to get it to work on MacOS. This assumes
        # urllib.pathname2url works, unfortunately...
        if os.name == 'riscos':
            import string
            fname = os.expand(fname)
            fname = fname.translate(string.maketrans("/.", "./"))

        if os.name == 'nt':
            file_url = "file:///%s" % fname
        else:
            file_url = "file://%s" % fname

        f = urllib2.urlopen(file_url)

        buf = f.read()
        f.close() 
Example #9
Source File: test_urllib2.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test_trivial(self):
        # A couple trivial tests

        self.assertRaises(ValueError, urllib2.urlopen, 'bogus url')

        # XXX Name hacking to get this to work on Windows.
        fname = os.path.abspath(urllib2.__file__).replace('\\', '/')

        # And more hacking to get it to work on MacOS. This assumes
        # urllib.pathname2url works, unfortunately...
        if os.name == 'riscos':
            import string
            fname = os.expand(fname)
            fname = fname.translate(string.maketrans("/.", "./"))

        if os.name == 'nt':
            file_url = "file:///%s" % fname
        else:
            file_url = "file://%s" % fname

        f = urllib2.urlopen(file_url)

        buf = f.read()
        f.close() 
Example #10
Source File: test_urllib2.py    From BinderFilter with MIT License 6 votes vote down vote up
def test_trivial(self):
        # A couple trivial tests

        self.assertRaises(ValueError, urllib2.urlopen, 'bogus url')

        # XXX Name hacking to get this to work on Windows.
        fname = os.path.abspath(urllib2.__file__).replace('\\', '/')

        # And more hacking to get it to work on MacOS. This assumes
        # urllib.pathname2url works, unfortunately...
        if os.name == 'riscos':
            import string
            fname = os.expand(fname)
            fname = fname.translate(string.maketrans("/.", "./"))

        if os.name == 'nt':
            file_url = "file:///%s" % fname
        else:
            file_url = "file://%s" % fname

        f = urllib2.urlopen(file_url)

        buf = f.read()
        f.close() 
Example #11
Source File: resource_loader.py    From python-percy-client with MIT License 6 votes vote down vote up
def build_resources(self):
        resources = []
        if not self.root_dir:
            return resources
        for root, dirs, files in os.walk(self.root_dir, followlinks=True):
            for file_name in files:
                path = os.path.join(root, file_name)
                if os.path.getsize(path) > MAX_FILESIZE_BYTES:
                    continue
                with open(path, 'rb') as f:
                    content = f.read()

                    path_for_url = pathname2url(path.replace(self.root_dir, '', 1))
                    if self.base_url[-1] == '/' and path_for_url[0] == '/':
                        path_for_url = path_for_url.replace('/', '' , 1)


                    resource_url = "{0}{1}".format(self.base_url, path_for_url)
                    resource = percy.Resource(
                        resource_url=resource_url,
                        sha=utils.sha256hash(content),
                        local_path=os.path.abspath(path),
                    )
                    resources.append(resource)
        return resources 
Example #12
Source File: common.py    From Computable with MIT License 6 votes vote down vote up
def file_path_to_url(path):
    """
    converts an absolute native path to a FILE URL.

    Parameters
    ----------
    path : a path in native format

    Returns
    -------
    a valid FILE URL
    """
    return urljoin('file:', pathname2url(path))


# ZipFile is not a context manager for <= 2.6
# must be tuple index here since 2.6 doesn't use namedtuple for version_info 
Example #13
Source File: test_urllib.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_basic(self):
        # Make sure simple tests pass
        expected_path = os.path.join("parts", "of", "a", "path")
        expected_url = "parts/of/a/path"
        result = urllib.pathname2url(expected_path)
        self.assertEqual(expected_url, result,
                         "pathname2url() failed; %s != %s" %
                         (result, expected_url))
        result = urllib.url2pathname(expected_url)
        self.assertEqual(expected_path, result,
                         "url2pathame() failed; %s != %s" %
                         (result, expected_path)) 
Example #14
Source File: dataset-get.py    From TextDetector with GNU General Public License v3.0 5 votes vote down vote up
def local_path_as_url( filename ):
    """
    Takes a local, OS-specific path or
    filename and transforms it into an
    url starting with file:// (it
    simplifies a lot of things).

    :param filename: a relative or absolute pathname
    :returns: the urlified absolute path
    """
    return "file://"+urllib.pathname2url(os.path.abspath(filename))

######################################## 
Example #15
Source File: test_urllib.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_quoting(self):
        # Test automatic quoting and unquoting works for pathnam2url() and
        # url2pathname() respectively
        given = os.path.join("needs", "quot=ing", "here")
        expect = "needs/%s/here" % urllib.quote("quot=ing")
        result = urllib.pathname2url(given)
        self.assertEqual(expect, result,
                         "pathname2url() failed; %s != %s" %
                         (expect, result))
        expect = given
        result = urllib.url2pathname(result)
        self.assertEqual(expect, result,
                         "url2pathname() failed; %s != %s" %
                         (expect, result))
        given = os.path.join("make sure", "using_quote")
        expect = "%s/using_quote" % urllib.quote("make sure")
        result = urllib.pathname2url(given)
        self.assertEqual(expect, result,
                         "pathname2url() failed; %s != %s" %
                         (expect, result))
        given = "make+sure/using_unquote"
        expect = os.path.join("make+sure", "using_unquote")
        result = urllib.url2pathname(given)
        self.assertEqual(expect, result,
                         "url2pathname() failed; %s != %s" %
                         (expect, result)) 
Example #16
Source File: WebIDE.py    From pythonista-scripts with MIT License 5 votes vote down vote up
def make_file_tree(dir_path=ROOT):
    file_dict = {}
    def recur(path, list):
        for l in os.listdir(path):
            f = os.path.join(path, l)
            if l[0] == '.':
              continue
            elif os.path.isdir(f):
                list[l] = {}
                recur(f, list[l])
            elif l.split('.')[-1] in ['py', 'txt', 'pyui', 'json']:
                list[l] = urllib.pathname2url(f[len(dir_path)+1:])
    recur(dir_path.rstrip('/'), file_dict)
    return file_dict 
Example #17
Source File: confluence.py    From confluence-publisher with MIT License 5 votes vote down vote up
def _read_file(filepath):
        filename = os.path.basename(filepath)
        url_ = pathname2url(filename)
        media_type, encoding = mimetypes.guess_type(url_)
        with open(filepath, 'rb') as file_:
            new_attachment_file = AttachmentFile(filename, file_.read(), media_type)
        return new_attachment_file 
Example #18
Source File: package.py    From elijah-provisioning with Apache License 2.0 5 votes vote down vote up
def is_zip_contained(filepath):
        if os.path.exists(filepath):
            # refular file
            abspath = os.path.abspath(filepath)
            urlpath = urlparse.urljoin("file:", pathname2url(abspath))
        else:
            urlpath = filepath
        try:
            overlay = VMOverlayPackage(urlpath)
            return True, urlpath
        except Exception as e:
            return False, None 
Example #19
Source File: playsound.py    From playsound with MIT License 5 votes vote down vote up
def _playsoundNix(sound, block=True):
    """Play a sound using GStreamer.

    Inspired by this:
    https://gstreamer.freedesktop.org/documentation/tutorials/playback/playbin-usage.html
    """
    if not block:
        raise NotImplementedError(
            "block=False cannot be used on this platform yet")

    # pathname2url escapes non-URL-safe characters
    import os
    try:
        from urllib.request import pathname2url
    except ImportError:
        # python 2
        from urllib import pathname2url

    import gi
    gi.require_version('Gst', '1.0')
    from gi.repository import Gst

    Gst.init(None)

    playbin = Gst.ElementFactory.make('playbin', 'playbin')
    if sound.startswith(('http://', 'https://')):
        playbin.props.uri = sound
    else:
        playbin.props.uri = 'file://' + pathname2url(os.path.abspath(sound))

    set_result = playbin.set_state(Gst.State.PLAYING)
    if set_result != Gst.StateChangeReturn.ASYNC:
        raise PlaysoundException(
            "playbin.set_state returned " + repr(set_result))

    # FIXME: use some other bus method than poll() with block=False
    # https://lazka.github.io/pgi-docs/#Gst-1.0/classes/Bus.html
    bus = playbin.get_bus()
    bus.poll(Gst.MessageType.EOS, Gst.CLOCK_TIME_NONE)
    playbin.set_state(Gst.State.NULL) 
Example #20
Source File: test_urllib.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def constructLocalFileUrl(self, filePath):
        return "file://%s" % urllib.pathname2url(os.path.abspath(filePath)) 
Example #21
Source File: __init__.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def show(self, wait=1.2, scale=10, module_color=(0, 0, 0, 255),
            background=(255, 255, 255, 255), quiet_zone=4):
        """Displays this QR code.

        This method is mainly intended for debugging purposes.

        This method saves the output of the :py:meth:`png` method (with a default
        scaling factor of 10) to a temporary file and opens it with the
        standard PNG viewer application or within the standard webbrowser. The
        temporary file is deleted afterwards.

        If this method does not show any result, try to increase the `wait`
        parameter. This parameter specifies the time in seconds to wait till
        the temporary file is deleted. Note, that this method does not return
        until the provided amount of seconds (default: 1.2) has passed.

        The other parameters are simply passed on to the `png` method.
        """
        import os
        import time
        import tempfile
        import webbrowser
 
        try:  # Python 2
            from urlparse import urljoin
            from urllib import pathname2url
        except ImportError:  # Python 3
            from urllib.parse import urljoin
            from urllib.request import pathname2url

        f = tempfile.NamedTemporaryFile('wb', suffix='.png', delete=False)
        self.png(f, scale=scale, module_color=module_color,
                 background=background, quiet_zone=quiet_zone)
        f.close()
        webbrowser.open_new_tab(urljoin('file:', pathname2url(f.name)))
        time.sleep(wait)
        os.unlink(f.name) 
Example #22
Source File: video_gi.py    From Tickeys-linux with MIT License 5 votes vote down vote up
def _get_uri(self):
        uri = self.filename
        if not uri:
            return
        if not '://' in uri:
            uri = 'file:' + pathname2url(realpath(uri))
        return uri 
Example #23
Source File: mast.py    From eleanor with MIT License 5 votes vote down vote up
def mastQuery(request):
    """Sends a request to the MAST server.

    Parameters
    ----------
    request : str
        JSON string for request.

    Returns
    ----------
    head :
        Retrieved data headers from MAST.
    content :
        Retrieved data contents from MAST.
    """
    server = 'mast.stsci.edu'

    # Grab Python Version
    version = '.'.join(map(str, sys.version_info[:3]))
        # Create Http Header Variables
    headers = {'Content-type': 'application/x-www-form-urlencoded',
               'Accept': 'text/plain',
               'User-agent': 'python-requests/'+version}
    # Encoding the request as a json string
    requestString = urlencode(json.dumps(request))
    # Opening the https cnnection
    conn = httplib.HTTPSConnection(server)
    # Making the query
    conn.request('POST', '/api/v0/invoke', 'request='+requestString, headers)

    # Getting the response
    resp = conn.getresponse()
    head = resp.getheaders()
    content = resp.read().decode('utf-8')

    # Close the https connection
    conn.close()

    return head, content 
Example #24
Source File: test_urllib2.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def sanepathname2url(path):
    import urllib
    urlpath = urllib.pathname2url(path)
    if os.name == "nt" and urlpath.startswith("///"):
        urlpath = urlpath[2:]
    # XXX don't ask me about the mac...
    return urlpath 
Example #25
Source File: test_urllib.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_quoting(self):
        # Test automatic quoting and unquoting works for pathnam2url() and
        # url2pathname() respectively
        given = os.path.join("needs", "quot=ing", "here")
        expect = "needs/%s/here" % urllib.quote("quot=ing")
        result = urllib.pathname2url(given)
        self.assertEqual(expect, result,
                         "pathname2url() failed; %s != %s" %
                         (expect, result))
        expect = given
        result = urllib.url2pathname(result)
        self.assertEqual(expect, result,
                         "url2pathname() failed; %s != %s" %
                         (expect, result))
        given = os.path.join("make sure", "using_quote")
        expect = "%s/using_quote" % urllib.quote("make sure")
        result = urllib.pathname2url(given)
        self.assertEqual(expect, result,
                         "pathname2url() failed; %s != %s" %
                         (expect, result))
        given = "make+sure/using_unquote"
        expect = os.path.join("make+sure", "using_unquote")
        result = urllib.url2pathname(given)
        self.assertEqual(expect, result,
                         "url2pathname() failed; %s != %s" %
                         (expect, result)) 
Example #26
Source File: test_urllib.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_basic(self):
        # Make sure simple tests pass
        expected_path = os.path.join("parts", "of", "a", "path")
        expected_url = "parts/of/a/path"
        result = urllib.pathname2url(expected_path)
        self.assertEqual(expected_url, result,
                         "pathname2url() failed; %s != %s" %
                         (result, expected_url))
        result = urllib.url2pathname(expected_url)
        self.assertEqual(expected_path, result,
                         "url2pathame() failed; %s != %s" %
                         (result, expected_path)) 
Example #27
Source File: test_urllib.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def constructLocalFileUrl(self, filePath):
        return "file://%s" % urllib.pathname2url(os.path.abspath(filePath)) 
Example #28
Source File: fileparam.py    From DAMM with GNU General Public License v2.0 5 votes vote down vote up
def set_location(_option, _opt_str, value, parser):
    """Sets the location variable in the parser to the filename in question"""
    if not os.path.exists(os.path.abspath(value)):
        debug.error("The requested file doesn't exist")
    if parser.values.location == None:
        slashes = "//"
        # Windows pathname2url decides to convert C:\blah to ///C:/blah
        # So to keep the URLs correct, we only add file: rather than file://
        if sys.platform.startswith('win'):
            slashes = ""
        parser.values.location = "file:" + slashes + urllib.pathname2url(os.path.abspath(value)) 
Example #29
Source File: video_pygst.py    From Tickeys-linux with MIT License 5 votes vote down vote up
def _get_uri(self):
        uri = self.filename
        if not uri:
            return
        if not '://' in uri:
            uri = 'file:' + pathname2url(path.realpath(uri))
        return uri 
Example #30
Source File: video_gstplayer.py    From Tickeys-linux with MIT License 5 votes vote down vote up
def _get_uri(self):
        uri = self.filename
        if not uri:
            return
        if not '://' in uri:
            uri = 'file:' + pathname2url(realpath(uri))
        return uri