本文整理汇总了Python中posixpath.basename方法的典型用法代码示例。如果您正苦于以下问题:Python posixpath.basename方法的具体用法?Python posixpath.basename怎么用?Python posixpath.basename使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类posixpath
的用法示例。
在下文中一共展示了posixpath.basename方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: filename_from_url
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def filename_from_url(url: QUrl) -> typing.Optional[str]:
"""Get a suitable filename from a URL.
Args:
url: The URL to parse, as a QUrl.
Return:
The suggested filename as a string, or None.
"""
if not url.isValid():
return None
pathname = posixpath.basename(url.path())
if pathname:
return pathname
elif url.host():
return url.host() + '.html'
else:
return None
示例2: get_package_loader
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def get_package_loader(self, package, package_path):
from pkg_resources import DefaultProvider, ResourceManager, \
get_provider
loadtime = datetime.utcnow()
provider = get_provider(package)
manager = ResourceManager()
filesystem_bound = isinstance(provider, DefaultProvider)
def loader(path):
if path is None:
return None, None
path = posixpath.join(package_path, path)
if not provider.has_resource(path):
return None, None
basename = posixpath.basename(path)
if filesystem_bound:
return basename, self._opener(
provider.get_resource_filename(manager, path))
s = provider.get_resource_string(manager, path)
return basename, lambda: (
BytesIO(s),
loadtime,
len(s)
)
return loader
示例3: test_realpath_resolve_before_normalizing
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def test_realpath_resolve_before_normalizing(self):
# Bug #990669: Symbolic links should be resolved before we
# normalize the path. E.g.: if we have directories 'a', 'k' and 'y'
# in the following hierarchy:
# a/k/y
#
# and a symbolic link 'link-y' pointing to 'y' in directory 'a',
# then realpath("link-y/..") should return 'k', not 'a'.
try:
os.mkdir(ABSTFN)
os.mkdir(ABSTFN + "/k")
os.mkdir(ABSTFN + "/k/y")
os.symlink(ABSTFN + "/k/y", ABSTFN + "/link-y")
# Absolute path.
self.assertEqual(realpath(ABSTFN + "/link-y/.."), ABSTFN + "/k")
# Relative path.
with support.change_cwd(dirname(ABSTFN)):
self.assertEqual(realpath(basename(ABSTFN) + "/link-y/.."),
ABSTFN + "/k")
finally:
test_support.unlink(ABSTFN + "/link-y")
safe_rmdir(ABSTFN + "/k/y")
safe_rmdir(ABSTFN + "/k")
safe_rmdir(ABSTFN)
示例4: test_realpath_resolve_first
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def test_realpath_resolve_first(self):
# Bug #1213894: The first component of the path, if not absolute,
# must be resolved too.
try:
os.mkdir(ABSTFN)
os.mkdir(ABSTFN + "/k")
os.symlink(ABSTFN, ABSTFN + "link")
with support.change_cwd(dirname(ABSTFN)):
base = basename(ABSTFN)
self.assertEqual(realpath(base + "link"), ABSTFN)
self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
finally:
test_support.unlink(ABSTFN + "link")
safe_rmdir(ABSTFN + "/k")
safe_rmdir(ABSTFN)
示例5: test_realpath_resolve_first
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def test_realpath_resolve_first(self):
# Bug #1213894: The first component of the path, if not absolute,
# must be resolved too.
try:
old_path = abspath('.')
os.mkdir(ABSTFN)
os.mkdir(ABSTFN + "/k")
os.symlink(ABSTFN, ABSTFN + "link")
os.chdir(dirname(ABSTFN))
base = basename(ABSTFN)
self.assertEqual(realpath(base + "link"), ABSTFN)
self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
finally:
os.chdir(old_path)
test_support.unlink(ABSTFN + "link")
safe_rmdir(ABSTFN + "/k")
safe_rmdir(ABSTFN)
示例6: _find_url_name
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def _find_url_name(self, index_url, url_name, req):
"""Finds the true URL name of a package, when the given name isn't quite correct.
This is usually used to implement case-insensitivity."""
if not index_url.url.endswith('/'):
# Vaguely part of the PyPI API... weird but true.
## FIXME: bad to modify this?
index_url.url += '/'
page = self._get_page(index_url, req)
if page is None:
logger.fatal('Cannot fetch index base URL %s' % index_url)
return
norm_name = normalize_name(req.url_name)
for link in page.links:
base = posixpath.basename(link.path.rstrip('/'))
if norm_name == normalize_name(base):
logger.notify('Real name of requirement %s is %s' % (url_name, base))
return base
return None
示例7: make_available
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def make_available(self, meta, built_dist_path, just_built, config=None):
if self._cli is None:
self._cli = binstar_client.utils.get_binstar(Namespace(token=self.token, site=None))
already_with_owner = inspect_binstar.distribution_exists(self._cli, self.owner, meta)
already_on_channel = inspect_binstar.distribution_exists_on_channel(self._cli,
self.owner,
meta,
channel=self.channel)
if already_on_channel and not just_built:
log.info('Nothing to be done for {} - it is already on {}/{}.'.format(meta.name(), self.owner, self.channel))
elif already_on_channel and just_built:
# We've just built, and the owner already has a distribution on this channel.
log.warn("Assuming the distribution we've just built and the one on {}/{} are the same.".format(self.owner, self.channel))
elif already_with_owner:
if just_built:
log.warn("Assuming the distribution we've just built and the one owned by {} are the same.".format(self.owner))
# Link a distribution.
log.info('Adding existing {} to the {}/{} channel.'.format(meta.dist(), self.owner, self.channel))
inspect_binstar.add_distribution_to_channel(self._cli, self.owner, meta, channel=self.channel)
elif just_built:
# Upload the distribution
log.info('Uploading {} to the {} channel.'.format(meta.name(), self.channel))
build.upload(self._cli, meta, self.owner, channels=[self.channel],
config=config)
elif not just_built:
# The distribution already existed, but not under the target owner.
if 'http://' in built_dist_path or 'https://' in built_dist_path:
source_owner = urlpath.basename(urlpath.dirname(built_dist_path.rstrip('/')))
inspect_binstar.copy_distribution_to_owner(self._cli, source_owner, self.owner, meta,
channel=self.channel)
示例8: get_file_loader
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def get_file_loader(self, filename):
return lambda x: (os.path.basename(filename), self._opener(filename))
示例9: get_package_loader
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def get_package_loader(self, package, package_path):
from pkg_resources import DefaultProvider, ResourceManager, get_provider
loadtime = datetime.utcnow()
provider = get_provider(package)
manager = ResourceManager()
filesystem_bound = isinstance(provider, DefaultProvider)
def loader(path):
if path is None:
return None, None
path = posixpath.join(package_path, path)
if not provider.has_resource(path):
return None, None
basename = posixpath.basename(path)
if filesystem_bound:
return (
basename,
self._opener(provider.get_resource_filename(manager, path)),
)
s = provider.get_resource_string(manager, path)
return basename, lambda: (BytesIO(s), loadtime, len(s))
return loader
示例10: get_directory_loader
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def get_directory_loader(self, directory):
def loader(path):
if path is not None:
path = os.path.join(directory, path)
else:
path = directory
if os.path.isfile(path):
return os.path.basename(path), self._opener(path)
return None, None
return loader
示例11: score_url
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def score_url(self, url):
"""
Give an url a score which can be used to choose preferred URLs
for a given project release.
"""
t = urlparse(url)
basename = posixpath.basename(t.path)
compatible = True
is_wheel = basename.endswith('.whl')
if is_wheel:
compatible = is_compatible(Wheel(basename), self.wheel_tags)
return (t.scheme != 'https', 'pypi.python.org' in t.netloc,
is_wheel, compatible, basename)
示例12: filename
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def filename(self):
_, netloc, path, _, _ = urllib_parse.urlsplit(self.url)
name = posixpath.basename(path.rstrip('/')) or netloc
name = urllib_parse.unquote(name)
assert name, ('URL %r produced no filename' % self.url)
return name
示例13: show_url
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def show_url(self):
return posixpath.basename(self.url.split('#', 1)[0].split('?', 1)[0])
示例14: parse
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def parse(message):
try:
parsed = MESSAGE.parseString(message, parseAll=True)
except ParseException as exc:
raise UnparseableEvent("{!r} {}".format(message, exc)) from None
data = {}
data["timestamp"] = parsed.timestamp
data["tls_protocol"] = _value_or_none(parsed.tls_protocol)
data["tls_cipher"] = _value_or_none(parsed.tls_cipher)
data["country_code"] = _value_or_none(parsed.country_code)
data["url"] = parsed.url
data["file"] = {}
data["file"]["filename"] = posixpath.basename(parsed.url)
data["file"]["project"] = _value_or_none(parsed.project_name)
data["file"]["version"] = _value_or_none(parsed.version)
data["file"]["type"] = _value_or_none(parsed.package_type)
download = _cattr.structure(data, Download)
try:
ua = user_agents.parse(parsed.user_agent)
if ua is None:
return # Ignored user agents mean we'll skip trying to log this event
except user_agents.UnknownUserAgentError:
logging.info("Unknown User agent: %r", parsed.user_agent)
else:
download = attr.evolve(download, details=ua)
return download
示例15: splitext
# 需要导入模块: import posixpath [as 别名]
# 或者: from posixpath import basename [as 别名]
def splitext(self):
return splitext(posixpath.basename(self.path.rstrip('/')))