Python urllib.request.pathname2url() Examples

The following are 30 code examples of urllib.request.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.request , or try the search function .
Example #1
Source File: test_workflows.py    From python-mistralclient with Apache License 2.0 6 votes vote down vote up
def test_update_with_file_uri(self):
        self.requests_mock.put(self.TEST_URL + URL_TEMPLATE_SCOPE,
                               json={'workflows': [WORKFLOW]})

        # The contents of wf_v2.yaml must be identical to WF_DEF
        path = pkg.resource_filename(
            'mistralclient',
            'tests/unit/resources/wf_v2.yaml'
        )
        path = os.path.abspath(path)

        # Convert the file path to file URI
        uri = parse.urljoin('file:', request.pathname2url(path))

        wfs = self.workflows.update(uri)

        self.assertIsNotNone(wfs)
        self.assertEqual(WF_DEF, wfs[0].definition)

        last_request = self.requests_mock.last_request

        self.assertEqual(WF_DEF, last_request.text)
        self.assertEqual('text/plain', last_request.headers['content-type']) 
Example #2
Source File: test_integration.py    From deepmatcher with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def setUp(self):
        self.vectors_cache_dir = '.cache'
        if os.path.exists(self.vectors_cache_dir):
            shutil.rmtree(self.vectors_cache_dir)

        self.data_cache_path = os.path.join(test_dir_path, 'test_datasets',
            'train_cache.pth')
        if os.path.exists(self.data_cache_path):
            os.remove(self.data_cache_path)

        vec_dir = os.path.abspath(os.path.join(test_dir_path, 'test_datasets'))
        filename = 'fasttext_sample.vec.zip'
        url_base = urljoin('file:', pathname2url(vec_dir)) + os.path.sep
        ft = FastText(filename, url_base=url_base, cache=self.vectors_cache_dir)

        self.train, self.valid, self.test = process(
            path=os.path.join(test_dir_path, 'test_datasets'),
            cache='train_cache.pth',
            train='test_train.csv',
            validation='test_valid.csv',
            test='test_test.csv',
            embeddings=ft,
            embeddings_cache_path='',
            ignore_columns=('left_id', 'right_id')) 
Example #3
Source File: test_field.py    From deepmatcher with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_extend_vocab_1(self):
        vectors_cache_dir = '.cache'
        if os.path.exists(vectors_cache_dir):
            shutil.rmtree(vectors_cache_dir)

        mf = MatchingField()
        lf = MatchingField(id=True, sequential=False)
        fields = [('id', lf), ('left_a', mf), ('right_a', mf), ('label', lf)]
        col_naming = {'id': 'id', 'label': 'label', 'left': 'left_', 'right': 'right_'}

        pathdir = os.path.abspath(os.path.join(test_dir_path, 'test_datasets'))
        filename = 'fasttext_sample.vec'
        file = os.path.join(pathdir, filename)
        url_base = urljoin('file:', pathname2url(file))
        vecs = Vectors(name=filename, cache=vectors_cache_dir, url=url_base)

        data_path = os.path.join(test_dir_path, 'test_datasets', 'sample_table_small.csv')
        md = MatchingDataset(fields, col_naming, path=data_path)

        mf.build_vocab()
        mf.vocab.vectors = torch.Tensor(len(mf.vocab.itos), 300)
        mf.extend_vocab(md, vectors=vecs)
        self.assertEqual(len(mf.vocab.itos), 6)
        self.assertEqual(mf.vocab.vectors.size(), torch.Size([6, 300])) 
Example #4
Source File: test_field.py    From deepmatcher with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_extend_vectors_1(self):
        vectors_cache_dir = '.cache'
        if os.path.exists(vectors_cache_dir):
            shutil.rmtree(vectors_cache_dir)

        pathdir = os.path.abspath(os.path.join(test_dir_path, 'test_datasets'))
        filename = 'fasttext_sample.vec'
        file = os.path.join(pathdir, filename)
        url_base = urljoin('file:', pathname2url(file))
        vecs = Vectors(name=filename, cache=vectors_cache_dir, url=url_base)
        self.assertIsInstance(vecs, Vectors)

        vec_data = MatchingField._get_vector_data(vecs, vectors_cache_dir)
        v = MatchingVocab(Counter())
        v.vectors = torch.Tensor(1, vec_data[0].dim)
        v.unk_init = torch.Tensor.zero_
        tokens = {'hello', 'world'}
        v.extend_vectors(tokens, vec_data)
        self.assertEqual(len(v.itos), 4)
        self.assertEqual(v.vectors.size(), torch.Size([4, 300]))
        self.assertEqual(list(v.vectors[2][0:10]), [0.0] * 10)
        self.assertEqual(list(v.vectors[3][0:10]), [0.0] * 10)

        if os.path.exists(vectors_cache_dir):
            shutil.rmtree(vectors_cache_dir) 
Example #5
Source File: pbm.py    From oslo.vmware with Apache License 2.0 6 votes vote down vote up
def get_pbm_wsdl_location(vc_version):
    """Return PBM WSDL file location corresponding to VC version.

    :param vc_version: a dot-separated version string. For example, "1.2".
    :return: the pbm wsdl file location.
    """
    if not vc_version:
        return
    ver = vc_version.split('.')
    major_minor = ver[0]
    if len(ver) >= 2:
        major_minor = '%s.%s' % (major_minor, ver[1])
    curr_dir = os.path.abspath(os.path.dirname(__file__))
    pbm_service_wsdl = os.path.join(curr_dir, 'wsdl', major_minor,
                                    'pbmService.wsdl')
    if not os.path.exists(pbm_service_wsdl):
        LOG.warning("PBM WSDL file %s not found.", pbm_service_wsdl)
        return
    pbm_wsdl = urlparse.urljoin('file:', urllib.pathname2url(pbm_service_wsdl))
    LOG.debug("Using PBM WSDL location: %s.", pbm_wsdl)
    return pbm_wsdl 
Example #6
Source File: test_pbm.py    From oslo.vmware with Apache License 2.0 6 votes vote down vote up
def test_get_pbm_wsdl_location(self):
        wsdl = pbm.get_pbm_wsdl_location(None)
        self.assertIsNone(wsdl)

        def expected_wsdl(version):
            driver_abs_dir = os.path.abspath(os.path.dirname(pbm.__file__))
            path = os.path.join(driver_abs_dir, 'wsdl', version,
                                'pbmService.wsdl')
            return urlparse.urljoin('file:', urllib.pathname2url(path))

        with mock.patch('os.path.exists') as path_exists:
            path_exists.return_value = True
            wsdl = pbm.get_pbm_wsdl_location('5')
            self.assertEqual(expected_wsdl('5'), wsdl)
            wsdl = pbm.get_pbm_wsdl_location('5.5')
            self.assertEqual(expected_wsdl('5.5'), wsdl)
            wsdl = pbm.get_pbm_wsdl_location('5.5.1')
            self.assertEqual(expected_wsdl('5.5'), wsdl)
            path_exists.return_value = False
            wsdl = pbm.get_pbm_wsdl_location('5.5')
            self.assertIsNone(wsdl) 
Example #7
Source File: test_actions.py    From python-mistralclient with Apache License 2.0 6 votes vote down vote up
def test_update_with_file_uri(self):
        self.requests_mock.put(self.TEST_URL + URL_TEMPLATE,
                               json={'actions': [ACTION]})

        # The contents of action_v2.yaml must be identical to ACTION_DEF
        path = pkg.resource_filename(
            'mistralclient',
            'tests/unit/resources/action_v2.yaml'
        )
        path = os.path.abspath(path)

        # Convert the file path to file URI
        uri = parse.urljoin('file:', request.pathname2url(path))

        actions = self.actions.update(uri)

        self.assertIsNotNone(actions)
        self.assertEqual(ACTION_DEF, actions[0].definition)

        last_request = self.requests_mock.last_request
        self.assertEqual('scope=private', last_request.query)
        self.assertEqual('text/plain', last_request.headers['content-type'])
        self.assertEqual(ACTION_DEF, last_request.text) 
Example #8
Source File: test_workbooks.py    From python-mistralclient with Apache License 2.0 6 votes vote down vote up
def test_create_with_file_uri(self):
        self.requests_mock.post(self.TEST_URL + URL_TEMPLATE,
                                json=WORKBOOK,
                                status_code=201)

        # The contents of wb_v2.yaml must be identical to WB_DEF
        path = pkg.resource_filename(
            'mistralclient',
            'tests/unit/resources/wb_v2.yaml'
        )
        path = os.path.abspath(path)

        # Convert the file path to file URI
        uri = parse.urljoin('file:', request.pathname2url(path))

        wb = self.workbooks.create(uri)

        self.assertIsNotNone(wb)
        self.assertEqual(WB_DEF, wb.definition)

        last_request = self.requests_mock.last_request

        self.assertEqual(WB_DEF, last_request.text)
        self.assertEqual('text/plain', last_request.headers['content-type']) 
Example #9
Source File: utils.py    From python-mistralclient with Apache License 2.0 6 votes vote down vote up
def get_contents_if_file(contents_or_file_name):
    """Get the contents of a file.

    If the value passed in is a file name or file URI, return the
    contents. If not, or there is an error reading the file contents,
    return the value passed in as the contents.

    For example, a workflow definition will be returned if either the
    workflow definition file name, or file URI are passed in, or the
    actual workflow definition itself is passed in.
    """
    try:
        if parse.urlparse(contents_or_file_name).scheme:
            definition_url = contents_or_file_name
        else:
            path = os.path.abspath(contents_or_file_name)
            definition_url = parse.urljoin(
                'file:',
                request.pathname2url(path)
            )
        return request.urlopen(definition_url).read().decode('utf8')
    except Exception:
        return contents_or_file_name 
Example #10
Source File: DenominoViso.py    From addons-source with GNU General Public License v2.0 6 votes vote down vote up
def relpathA2B(self,A,B):
        """Return a relative pathname to get from file A to B"""
        retval = ''
        A = os.path.abspath(A)
        B = os.path.abspath(B)
        cp = os.path.split(os.path.commonprefix([A,B]))[0]
        if cp == '':
            #raise NameError('Unable to construct a relative path from ' + \
            #    A + ' to ' + B)
            # As Heinz indicates this can happen on M$ with driveletters, so
            # return absolute path to B, hopefully that works.
            return 'file:' + pathname2url(B)
        a = os.path.dirname(A)
        while a != cp:
            retval += "../"
            a = os.path.dirname(a)
        rv = os.path.basename(B)
        b = os.path.dirname(B)
        while b != cp:
            rv = os.path.basename(b) + '/' + rv
            b = os.path.dirname(b)
        return retval + rv 
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: __init__.py    From PyLoxone with Apache License 2.0 6 votes vote down vote up
def encrypt(self, command):
        from Crypto.Util import Padding
        if not self._encryption_ready:
            return command
        if self._salt != "" and self.new_salt_needed():
            prev_salt = self._salt
            self._salt = self.genarate_salt()
            s = "nextSalt/{}/{}/{}\0".format(prev_salt, self._salt, command)
        else:
            if self._salt == "":
                self._salt = self.genarate_salt()
            s = "salt/{}/{}\0".format(self._salt, command)
        s = Padding.pad(bytes(s, "utf-8"), 16)
        aes_cipher = self.get_new_aes_chiper()
        encrypted = aes_cipher.encrypt(s)
        encoded = b64encode(encrypted)
        encoded_url = req.pathname2url(encoded.decode("utf-8"))
        return CMD_ENCRYPT_CMD + encoded_url 
Example #14
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 
Example #15
Source File: decorator.py    From ptest with Apache License 2.0 5 votes vote down vote up
def __get_location(func):
    file_path = os.path.abspath(inspect.getfile(func))
    _, line_no = inspect.getsourcelines(func)
    return urljoin("file:", "%s:%s" % (unquote(pathname2url(file_path)), line_no)) 
Example #16
Source File: audio_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 
Example #17
Source File: document.py    From pympress with GNU General Public License v2.0 5 votes vote down vote up
def path_to_uri(path):
        """ Transform a path to a file URI, and maintains others URIs.
        """
        # Do not trust urlsplit, manually check we have an URI
        pos = path.index(':') if ':' in path else -1
        if path[pos:pos + 3] == '://' or (pos > 1 and set(path[:pos]) <= scheme_chars):
            return path
        else:
            return urljoin('file:', pathname2url(path)) 
Example #18
Source File: utils.py    From openstacksdk with Apache License 2.0 5 votes vote down vote up
def normalise_file_path_to_url(path):
    if parse.urlparse(path).scheme:
        return path
    path = os.path.abspath(path)
    return parse.urljoin('file:', request.pathname2url(path)) 
Example #19
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 #20
Source File: audio_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 
Example #21
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 
Example #22
Source File: playsound.py    From goreviewpartner with GNU General Public License v3.0 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 #23
Source File: common.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 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)) 
Example #24
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 #25
Source File: url.py    From LSP with MIT License 5 votes vote down vote up
def filename_to_uri(path: str) -> str:
    return urljoin('file:', pathname2url(path)) 
Example #26
Source File: common.py    From elasticintel with GNU General Public License v3.0 5 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)) 
Example #27
Source File: R.py    From HoshinoBot with GNU General Public License v3.0 5 votes vote down vote up
def url(self):
        """资源文件的url,供酷Q(或其他远程服务)使用"""
        return urljoin(hoshino.config.RES_URL, pathname2url(self.__path)) 
Example #28
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 #29
Source File: translator.py    From mopidy-tunein with Apache License 2.0 5 votes vote down vote up
def mopidy_to_tunein_query(mopidy_query):
    tunein_query = []
    for (field, values) in mopidy_query.items():
        for value in values:
            if field == "any":
                tunein_query.append(value)
    query = " ".join(tunein_query)
    return request.pathname2url(query) 
Example #30
Source File: util.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def path2url(url):
    """URL to path."""

    path = pathname2url(url)
    # If on windows, replace the notation to use a default protocol `///` with nothing.
    if is_win() and RE_WIN_DEFAULT_PROTOCOL.match(path):
        path = path.replace('///', '', 1)
    return path