本文整理汇总了Python中os.path.decode方法的典型用法代码示例。如果您正苦于以下问题:Python path.decode方法的具体用法?Python path.decode怎么用?Python path.decode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os.path
的用法示例。
在下文中一共展示了path.decode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_home
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
def get_home():
"""Find user's home directory if possible.
Otherwise, returns None.
:see:
http://mail.python.org/pipermail/python-list/2005-February/325395.html
"""
try:
if six.PY2 and sys.platform == 'win32':
path = os.path.expanduser(b"~").decode(sys.getfilesystemencoding())
else:
path = os.path.expanduser("~")
except ImportError:
# This happens on Google App Engine (pwd module is not present).
pass
else:
if os.path.isdir(path):
return path
for evar in ('HOME', 'USERPROFILE', 'TMP'):
path = os.environ.get(evar)
if path is not None and os.path.isdir(path):
return path
return None
示例2: decode_path
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
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)
示例3: decode_path
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
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)
示例4: getrealpath
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
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:])
示例5: _decode_filesystem_path
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
def _decode_filesystem_path(path):
if isinstance(path, bytes):
return path.decode(sys.getfilesystemencoding())
else:
return path
示例6: wait_for_thread_stopped
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
def wait_for_thread_stopped(self, reason='breakpoint', line=None, file=None, name=None):
'''
:param file:
utf-8 bytes encoded file or unicode
'''
stopped_event = self.wait_for_json_message(StoppedEvent)
assert stopped_event.body.reason == reason
json_hit = self.get_stack_as_json_hit(stopped_event.body.threadId)
if file is not None:
path = json_hit.stack_trace_response.body.stackFrames[0]['source']['path']
if IS_PY2:
if isinstance(file, bytes):
file = file.decode('utf-8')
if isinstance(path, bytes):
path = path.decode('utf-8')
if not path.endswith(file):
raise AssertionError('Expected path: %s to end with: %s' % (path, file))
if name is not None:
assert json_hit.stack_trace_response.body.stackFrames[0]['name'] == name
if line is not None:
found_line = json_hit.stack_trace_response.body.stackFrames[0]['line']
if not isinstance(line, (tuple, list)):
line = [line]
assert found_line in line, 'Expect to break at line: %s. Found: %s' % (line, found_line)
return json_hit
示例7: __repr__
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
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
示例8: _tostr
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
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
示例9: cookietostr
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
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
示例10: reload
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
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))
示例11: unload
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
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))
示例12: get_server_url
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
def get_server_url():
for path in load_config_paths(CONFIG_RESOURCE):
path = os.path.join(path.decode(), SERVER_CONFIG_FILENAME)
if os.path.isfile(path):
with open(path) as f:
return f.read().strip()
parser.exit('Geofront server URL is not configured yet.\n'
'Try `{0} start` command.'.format(parser.prog))
示例13: start
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
def start(args):
"""Set up the Geofront server URL."""
for path in load_config_paths(CONFIG_RESOURCE):
path = os.path.join(path.decode(), SERVER_CONFIG_FILENAME)
if os.path.isfile(path):
message = 'Geofront server URL is already configured: ' + path
if args.force:
print(message + '; overwriting...', file=sys.stderr)
else:
parser.exit(message)
while True:
server_url = input('Geofront server URL: ')
if not server_url.startswith(('https://', 'http://')):
print(server_url, 'is not a valid url.')
continue
elif not server_url.startswith('https://'):
cont = input('It is not a secure URL. '
'https:// is preferred over http://. '
'Continue (y/N)? ')
if cont.strip().lower() != 'y':
continue
break
server_config_filename = os.path.join(
save_config_path(CONFIG_RESOURCE).decode(),
SERVER_CONFIG_FILENAME
)
with open(server_config_filename, 'w') as f:
print(server_url, file=f)
authenticate.call(args)
示例14: fix_mac_codesign
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
def fix_mac_codesign():
"""If the running Python interpreter isn't property signed on macOS
it's unable to get/set password using keyring from Keychain.
In such case, we need to sign the interpreter first.
https://github.com/jaraco/keyring/issues/219
"""
global fix_mac_codesign
logger = logging.getLogger(__name__ + '.fix_mac_codesign')
p = subprocess.Popen(['codesign', '-dvvvvv', sys.executable],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
def prepend_lines(c, text):
if not isinstance(text, str):
text = text.decode()
return ''.join(c + l for l in text.splitlines(True))
logger.debug('codesign -dvvvvv %s:\n%s\n%s',
sys.executable,
prepend_lines('| ', stdout),
prepend_lines('> ', stderr))
if b'\nSignature=' in stderr:
logger.debug('%s: already signed', sys.executable)
return
logger.info('%s: not signed yet; try signing...', sys.executable)
p = subprocess.Popen(['codesign', '-f', '-s', '-', sys.executable],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
os.waitpid(p.pid, 0)
logger.debug('%s: signed\n%s\n%s',
sys.executable,
prepend_lines('| ', stdout),
prepend_lines('> ', stderr))
logger.debug('respawn the equivalent process...')
raise SystemExit(subprocess.call(sys.argv))
示例15: pathparse
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import decode [as 别名]
def pathparse(path):
"""Handle variations in a path argument.
Cloud: - Root of the defined cloud
Cloud:some/path - Supported with our without path leading '/'s
X: - Windows drive letter
X:\\some\\path - Windows drive letter with absolute or relative path
some/path - Relative path from cwd (and on current drive on Windows)
//server/path - UNC paths are supported
On Windows a one-character cloud name is not supported - it will be interprested as a drive letter.
"""
if is_Linux and is_Py27:
path = path.decode("utf-8")
_cloud = False
if ':' in path:
if len(path) == 1: # Handle corner case of ':' only passed in
logging.error("ERROR Path argument <{}> not a legal path".format(path)); exit()
if path[1] == ':' and is_Windows: # Windows drive letter case
path_base = path
if not path_base.endswith('\\'): # For consistency ensure the path ends with '/'
path_base += '/'
else: # Cloud case with optional path part
path_FORMAT = re.compile(r'([\w-]+):(.*)')
out = path_FORMAT.match(path)
if out:
_cloud = True
cloud_name = out.group(1) + ':'
if cloud_name not in clouds:
logging.error("ERROR Path argument <{}> not in list of configured Clouds: {}"
.format(cloud_name, clouds)); exit()
path_part = out.group(2)
if path_part:
# if not path_part.startswith('/'): # For consistency ensure the cloud path part starts and ends with /'s
# path_part = '/' + path_part
if not (path_part.endswith('/') or path_part.endswith('\\')): # 2nd check is for Windows paths
path_part += '/'
path_base = cloud_name + path_part
else: # Local path (without Windows drive letter)
path_base = path
if not (path_base.endswith('/') or path_base.endswith('\\')):
path_base += '/'
if not _cloud:
if not os.path.exists(path_base):
logging.error("ERROR Local path parameter <{}> cannot be accessed. Path error? Aborting"
.format(path_base)); exit()
return path_base