本文整理汇总了Python中posixpath.normpath方法的典型用法代码示例。如果您正苦于以下问题:Python posixpath.normpath方法的具体用法?Python posixpath.normpath怎么用?Python posixpath.normpath使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类posixpath
的用法示例。
在下文中一共展示了posixpath.normpath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: translate_path
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def translate_path(self, path):
"""Translate a /-separated PATH to the local filename syntax.
Components that mean special things to the local file system
(e.g. drive or directory names) are ignored. (XXX They should
probably be diagnosed.)
"""
# abandon query parameters
path = path.split('?',1)[0]
path = path.split('#',1)[0]
path = posixpath.normpath(urllib_parse.unquote(path))
words = path.split('/')
words = filter(None, words)
path = os.getcwd()
for word in words:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir): continue
path = os.path.join(path, word)
return path
示例2: _refuri2http
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def _refuri2http(self, node):
# Replace 'refuri' in reference with HTTP address, if possible
# None for no possible address
url = node.get('refuri')
if not node.get('internal'):
return url
# If HTTP page build URL known, make link relative to that.
if not self.markdown_http_base:
return None
this_doc = self.builder.current_docname
if url in (None, ''): # Reference to this doc
url = self.builder.get_target_uri(this_doc)
else: # URL is relative to the current docname.
this_dir = posixpath.dirname(this_doc)
if this_dir:
url = posixpath.normpath('{}/{}'.format(this_dir, url))
url = '{}/{}'.format(self.markdown_http_base, url)
if 'refid' in node:
url += '#' + node['refid']
return url
示例3: safe_join
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def safe_join(directory, *pathnames):
"""Safely join `directory` and one or more untrusted `pathnames`. If this
cannot be done, this function returns ``None``.
:param directory: the base directory.
:param pathnames: the untrusted pathnames relative to that directory.
"""
parts = [directory]
for filename in pathnames:
if filename != "":
filename = posixpath.normpath(filename)
for sep in _os_alt_seps:
if sep in filename:
return None
if os.path.isabs(filename) or filename == ".." or filename.startswith("../"):
return None
parts.append(filename)
return posixpath.join(*parts)
示例4: safe_join
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def safe_join(directory, filename):
"""Safely join `directory` and `filename`.
Example usage::
@app.route('/wiki/<path:filename>')
def wiki_page(filename):
filename = safe_join(app.config['WIKI_FOLDER'], filename)
with open(filename, 'rb') as fd:
content = fd.read() # Read and process the file content...
:param directory: the base directory.
:param filename: the untrusted filename relative to that directory.
:raises: :class:`~werkzeug.exceptions.NotFound` if the resulting path
would fall out of `directory`.
"""
filename = posixpath.normpath(filename)
for sep in _os_alt_seps:
if sep in filename:
raise NotFound()
if os.path.isabs(filename) or \
filename == '..' or \
filename.startswith('../'):
raise NotFound()
return os.path.join(directory, filename)
示例5: _Parse
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def _Parse(self, value):
components = urllib.parse.urlparse(value)
# dont normalise path for http URI's
if components.scheme and not components.scheme == "http":
normalized_path = posixpath.normpath(components.path)
if normalized_path == ".":
normalized_path = ""
components = components._replace(path=normalized_path)
if not components.scheme:
# For file:// URNs, we need to parse them from a filename.
components = components._replace(
netloc="",
path=urllib.request.pathname2url(value),
scheme="file")
self.original_filename = value
return components
示例6: serve
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def serve(request, path, insecure=False, **kwargs):
"""
Serve static files below a given point in the directory structure or
from locations inferred from the staticfiles finders.
To use, put a URL pattern such as::
from django.contrib.staticfiles import views
url(r'^(?P<path>.*)$', views.serve)
in your URLconf.
It uses the django.views.static.serve() view to serve the found files.
"""
if not settings.DEBUG and not insecure:
raise Http404
normalized_path = posixpath.normpath(unquote(path)).lstrip('/')
absolute_path = finders.find(normalized_path)
if not absolute_path:
if path.endswith('/') or path == '':
raise Http404("Directory indexes are not allowed here.")
raise Http404("'%s' could not be found" % path)
document_root, path = os.path.split(absolute_path)
return static.serve(request, path, document_root=document_root, **kwargs)
示例7: translate_path
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def translate_path(self, path):
"""Translate a /-separated PATH to the local filename syntax.
Components that mean special things to the local file system
(e.g. drive or directory names) are ignored. (XXX They should
probably be diagnosed.)
"""
# abandon query parameters
path = path.split('?',1)[0]
path = path.split('#',1)[0]
path = posixpath.normpath(urllib.unquote(path))
words = path.split('/')
words = filter(None, words)
path = os.getcwd()
for word in words:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir): continue
path = os.path.join(path, word)
return path
示例8: safe_join
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def safe_join(directory, *pathnames):
"""Safely join `directory` and one or more untrusted `pathnames`. If this
cannot be done, this function returns ``None``.
:param directory: the base directory.
:param pathnames: the untrusted pathnames relative to that directory.
"""
parts = [directory]
for filename in pathnames:
if filename != '':
filename = posixpath.normpath(filename)
for sep in _os_alt_seps:
if sep in filename:
return None
if os.path.isabs(filename) or \
filename == '..' or \
filename.startswith('../'):
return None
parts.append(filename)
return posixpath.join(*parts)
示例9: translate_path
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def translate_path(self, path):
"""Translate a /-separated PATH to the local filename syntax.
Components that mean special things to the local file system
(e.g. drive or directory names) are ignored. (XXX They should
probably be diagnosed.)
"""
# abandon query parameters
path = path.split('?',1)[0]
path = path.split('#',1)[0]
# Don't forget explicit trailing slash when normalizing. Issue17324
trailing_slash = path.rstrip().endswith('/')
path = posixpath.normpath(urllib.unquote(path))
words = path.split('/')
words = filter(None, words)
path = os.getcwd()
for word in words:
if os.path.dirname(word) or word in (os.curdir, os.pardir):
# Ignore components that are not a simple file/directory name
continue
path = os.path.join(path, word)
if trailing_slash:
path += '/'
return path
示例10: _clean_name
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def _clean_name(self, name):
"""
Cleans the name so that Windows style paths work
"""
if name.startswith("https://") or name.startswith("http://"):
return name
# Normalize Windows style paths
clean_name = posixpath.normpath(name).replace('\\', '/')
# os.path.normpath() can strip trailing slashes so we implement
# a workaround here.
if name.endswith('/') and not clean_name.endswith('/'):
# Add a trailing slash as it was stripped.
return clean_name + '/'
else:
return clean_name
示例11: translate_path
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def translate_path(self, path):
"""
Translate a /-separated PATH to the local filename syntax.
Components that mean special things to the local file system
(e.g. drive or directory names) are ignored. (XXX They should
probably be diagnosed.)
"""
# abandon query parameters
path = urlparse(to_text(path))[2]
path = posixpath.normpath(unquote(path))
words = path.split('/')
words = list(filter(None, words))
path = self.server.cwd
for word in words:
_, word = os.path.splitdrive(word)
_, word = os.path.split(word)
if word in (os.curdir, os.pardir):
continue
path = os.path.join(path, word)
return path
示例12: get_dependents
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def get_dependents(archive, filename):
"""
Normalise dependency file paths to absolute ones
Relative paths are relative to parent object
"""
src = archive.read(filename)
node = fromstring(src)
rels = RelationshipList.from_tree(node)
folder = posixpath.dirname(filename)
parent = posixpath.split(folder)[0]
for r in rels.Relationship:
if r.TargetMode == "External":
continue
elif r.target.startswith("/"):
r.target = r.target[1:]
else:
pth = posixpath.join(parent, r.target)
r.target = posixpath.normpath(pth)
return rels
示例13: serve
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def serve(request, path, insecure=False, **kwargs):
"""
Serve static files below a given point in the directory structure or
from locations inferred from the staticfiles finders.
To use, put a URL pattern such as::
from django.contrib.staticfiles import views
url(r'^(?P<path>.*)$', views.serve)
in your URLconf.
It uses the django.views.static.serve() view to serve the found files.
"""
if not settings.DEBUG and not insecure:
raise Http404
normalized_path = posixpath.normpath(path).lstrip('/')
absolute_path = finders.find(normalized_path)
if not absolute_path:
if path.endswith('/') or path == '':
raise Http404("Directory indexes are not allowed here.")
raise Http404("'%s' could not be found" % path)
document_root, path = os.path.split(absolute_path)
return static.serve(request, path, document_root=document_root, **kwargs)
示例14: translate_path
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def translate_path(self, path):
"""Translate a /-separated PATH to the local filename syntax.
Components that mean special things to the local file system
(e.g. drive or directory names) are ignored. (XXX They should
probably be diagnosed.)
"""
# abandon query parameters
path = path.split('?',1)[0]
path = path.split('#',1)[0]
# Don't forget explicit trailing slash when normalizing. Issue17324
trailing_slash = path.rstrip().endswith('/')
path = posixpath.normpath(urllib.unquote(path))
words = path.split('/')
words = filter(None, words)
path = os.getcwd()
for word in words:
drive, word = os.path.splitdrive(word)
head, word = os.path.split(word)
if word in (os.curdir, os.pardir): continue
path = os.path.join(path, word)
if trailing_slash:
path += '/'
return path
示例15: xnormpath
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import normpath [as 别名]
def xnormpath(path):
""" Cross-platform version of os.path.normpath """
# replace escapes and Windows slashes
normalized = posixpath.normpath(path).replace(b'\\', b'/')
# fold the result
return posixpath.normpath(normalized)