Python os.path.decode() Examples

The following are code examples for showing how to use os.path.decode(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: AshsSDK   Author: thehappydinoa   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, unicode):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 2
Project: aws-cfn-plex   Author: lordmuffin   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, unicode):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 3
Project: grand_central   Author: amiracle   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, unicode):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 4
Project: pycfn-elasticsearch   Author: elelsee   File: __init__.py    Apache License 2.0 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, unicode):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 5
Project: cidr-house-rules   Author: trulia   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, str):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 6
Project: vlcp   Author: hubo1016   File: http.py    Apache License 2.0 6 votes vote down vote up
def getrealpath(self, root, path):
        '''
        Return the real path on disk from the query path, from a root path.
        The input path from URL might be absolute '/abc', or point to parent '../test',
        or even with UNC or drive '\\test\abc', 'c:\test.abc',
        which creates security issues when accessing file contents with the path.
        With getrealpath, these paths cannot point to files beyond the root path.
        
        :param root: root path of disk files, any query is limited in root directory.
        
        :param path: query path from URL.
        '''
        if not isinstance(path, str):
            path = path.decode(self.encoding)
        # In windows, if the path starts with multiple / or \, the os.path library may consider it an UNC path
        # remove them; also replace \ to /
        path = pathrep.subn('/', path)[0]
        # The relative root is considered ROOT, eliminate any relative path like ../abc, which create security issues
        # We can use os.path.relpath(..., '/') but python2.6 os.path.relpath is buggy 
        path = os.path.normpath(os.path.join('/', path))
        # The normalized path can be an UNC path, or event a path with drive letter
        # Send bad request for these types
        if os.path.splitdrive(path)[0]:
            raise HttpInputException('Bad path')
        return os.path.join(root, path[1:]) 
Example 7
Project: Rovers   Author: RIT-Space-Exploration   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, str):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 8
Project: splunk-for-securityHub   Author: splunk   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, unicode):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 9
Project: splunk-for-securityHub   Author: splunk   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, unicode):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 10
Project: aws-alfred-workflow   Author: maxrothman   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, unicode):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 11
Project: AmazonConnectSalesforceLambda   Author: AmazonConnectSalesforceIntegration   File: __init__.py    Apache License 2.0 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, str):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 12
Project: bash-lambda-layer   Author: gkrizek   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, str):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 13
Project: Backend   Author: LockScreen   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, unicode):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 14
Project: AWS-Transit-Gateway-Demo-MultiAccount   Author: MattTunny   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, str):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 15
Project: AWS-Transit-Gateway-Demo-MultiAccount   Author: MattTunny   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, str):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 16
Project: lambda-OCRmyPDF   Author: chronograph-pe   File: __init__.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, str):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 17
Project: laboratoria   Author: almeidaw   File: __init__.py    Apache License 2.0 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, str):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 18
Project: laboratoria   Author: almeidaw   File: __init__.py    Apache License 2.0 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, str):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 19
Project: monKey-server   Author: Amirh24   File: __init__.py    MIT License 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, str):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 20
Project: ec2-remote.serverless   Author: khilnani   File: __init__.py    GNU General Public License v3.0 6 votes vote down vote up
def decode_path(path):
    """
    Ensure `path` is Unicode. Return `nodes.reprunicode` object.

    Decode file/path string in a failsave manner if not already done.
    """
    # see also http://article.gmane.org/gmane.text.docutils.user/2905
    if isinstance(path, unicode):
        return path
    try:
        path = path.decode(sys.getfilesystemencoding(), 'strict')
    except AttributeError: # default value None has no decode method
        return nodes.reprunicode(path)
    except UnicodeDecodeError:
        try:
            path = path.decode('utf-8', 'strict')
        except UnicodeDecodeError:
            path = path.decode('ascii', 'replace')
    return nodes.reprunicode(path) 
Example 21
Project: PTBot   Author: whocares-openscene   File: reloader.py    GNU General Public License v3.0 5 votes vote down vote up
def reload(self, path):
        """
        Loads or reloads a module, given its file path. Thread safe.

        :type path: str
        """
        if not os.path.isfile(path):
            # we check if the file still exists here because some programs modify a file before deleting
            return

        if isinstance(path, bytes):
            path = path.decode()
        self.bot.loop.call_soon_threadsafe(lambda: asyncio.async(self._reload(path), loop=self.bot.loop)) 
Example 22
Project: PTBot   Author: whocares-openscene   File: reloader.py    GNU General Public License v3.0 5 votes vote down vote up
def unload(self, path):
        """
        Unloads a module, given its file path. Thread safe.

        :type path: str
        """
        if isinstance(path, bytes):
            path = path.decode()
        self.bot.loop.call_soon_threadsafe(lambda: asyncio.async(self._unload(path), loop=self.bot.loop)) 
Example 23
Project: CloudBot   Author: paris-ci   File: reloader.py    GNU General Public License v3.0 5 votes vote down vote up
def reload(self, path):
        """
        Loads or reloads a module, given its file path. Thread safe.

        :type path: str
        """
        if not os.path.isfile(path):
            # we check if the file still exists here because some programs modify a file before deleting
            return

        if isinstance(path, bytes):
            path = path.decode()
        self.bot.loop.call_soon_threadsafe(lambda: asyncio.async(self._reload(path), loop=self.bot.loop)) 
Example 24
Project: CloudBot   Author: paris-ci   File: reloader.py    GNU General Public License v3.0 5 votes vote down vote up
def unload(self, path):
        """
        Unloads a module, given its file path. Thread safe.

        :type path: str
        """
        if isinstance(path, bytes):
            path = path.decode()
        self.bot.loop.call_soon_threadsafe(lambda: asyncio.async(self._unload(path), loop=self.bot.loop)) 
Example 25
Project: vlcp   Author: hubo1016   File: http.py    Apache License 2.0 5 votes vote down vote up
def __repr__(self, *args, **kwargs):
        r = self.method.upper() + b' ' + self.path + (b', ' + self.connection.remoteaddr[0].encode(self.encoding)
                                                      if self.connection.remoteaddr
                                                      else b'')
        if not isinstance(r, str):
            return r.decode(self.encoding)
        else:
            return r 
Example 26
Project: vlcp   Author: hubo1016   File: http.py    Apache License 2.0 5 votes vote down vote up
def _tostr(self, arg):
        if isinstance(arg, list):
            return [self._tostr(v) for v in arg]
        elif not isinstance(arg, str):
            return arg.decode(self.encoding)
        else:
            return arg 
Example 27
Project: vlcp   Author: hubo1016   File: http.py    Apache License 2.0 5 votes vote down vote up
def cookietostr(self):
        "Cookie values are bytes in Python3. This function Convert bytes to string with env.encoding(default to utf-8)."
        self.cookies = dict((k, (v.decode(self.encoding) if not isinstance(v, str) else v)) for k,v in self.cookies.items())
        return self.cookies 
Example 28
Project: py-ipfs-http-client   Author: ipfs   File: test_utils.py    MIT License 5 votes vote down vote up
def test_clean_file_unopened_textpath(self):
		"""Tests utils.clean_file() with a text string filepath.

		This test relies on the openability of the file 'fsdfgh'
		located in 'test/functional/fake_dir'.
		"""
		path = os.path.dirname(__file__)
		if isinstance(path, six.binary_type):  #PY2
			path = path.decode(sys.getfilesystemencoding())
		path = os.path.join(path, u"..", u"functional", u"fake_dir", u"fsdfgh")
		f, opened = utils.clean_file(path)
		assert hasattr(f, 'read')
		assert opened
		# Closing file after test assertions.
		f.close() 
Example 29
Project: spanky.py   Author: gc-plp   File: reloader.py    GNU General Public License v3.0 5 votes vote down vote up
def reload(self, path):
        """
        Loads or reloads a module, given its file path. Thread safe.

        :type path: str
        """
        if not os.path.isfile(path):
            # we check if the file still exists here because some programs modify a file before deleting
            return

        if isinstance(path, bytes):
            path = path.decode()
        self._reload(path) 
Example 30
Project: spanky.py   Author: gc-plp   File: reloader.py    GNU General Public License v3.0 5 votes vote down vote up
def unload(self, path):
        """
        Unloads a module, given its file path. Thread safe.

        :type path: str
        """
        if isinstance(path, bytes):
            path = path.decode()
        self._unload(path) 
Example 31
Project: SubChannel   Author: FurCode   File: reloader.py    GNU General Public License v2.0 5 votes vote down vote up
def reload(self, path):
        """
        Loads or reloads a module, given its file path. Thread safe.

        :type path: str
        """
        if not os.path.isfile(path):
            # we check if the file still exists here because some programs modify a file before deleting
            return

        if isinstance(path, bytes):
            path = path.decode()
        self.bot.loop.call_soon_threadsafe(lambda: asyncio.async(self._reload(path), loop=self.bot.loop)) 
Example 32
Project: SubChannel   Author: FurCode   File: reloader.py    GNU General Public License v2.0 5 votes vote down vote up
def unload(self, path):
        """
        Unloads a module, given its file path. Thread safe.

        :type path: str
        """
        if isinstance(path, bytes):
            path = path.decode()
        self.bot.loop.call_soon_threadsafe(lambda: asyncio.async(self._unload(path), loop=self.bot.loop)) 
Example 33
Project: docmeld   Author: riteme   File: docmeld.py    MIT License 5 votes vote down vote up
def get_tag(token):
    NONE = -1
    NON_ASCII = 1
    ASCII = 0
    def get_type(c):
        if ord(c) > 255:
            return NON_ASCII
        return ASCII

    tags = (token.spelling or token.displayname).decode('utf-8').split('\n')
    for i in xrange(len(tags)):
        last = NONE
        ret = []
        for c in tags[i]:
            t = get_type(c)
            if t != last:
                if t == NON_ASCII:
                    ret.append(config.TAG_BEGIN.format(name=config.NON_ASCII_CLASS))
                elif last != NONE:
                    ret.append(config.TAG_END)
            if c in config.REPLACEMENT:
                ret.append(config.REPLACEMENT[c])
            else:
                ret.append(c)
            last = t
        if last == NON_ASCII:
            ret.append(config.TAG_END)
        tags[i] = ''.join(ret)

    return tags 
Example 34
Project: docmeld   Author: riteme   File: docmeld.py    MIT License 5 votes vote down vote up
def parse_markdown(path, dirname):
    DEBUG('Parsing markdown file: %s' % path)
    with open(path, 'r') as reader:
        content = reader.read()
    cache, flag = load_cache(content, path)
    if flag:
        DEBUG('"%s" cached.' % path)
        with open(cache, 'r') as reader:
            return pickle.load(reader)
    result = md.convert(content.decode(config.ENCODING))
    with open(cache, 'w') as writer:
        pickle.dump(result, writer)
    return result

# Resolver