本文整理汇总了Python中os.path.open方法的典型用法代码示例。如果您正苦于以下问题:Python path.open方法的具体用法?Python path.open怎么用?Python path.open使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os.path
的用法示例。
在下文中一共展示了path.open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_package_sync_with_canonical_simple_page
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def test_package_sync_with_canonical_simple_page(mirror: Mirror) -> None:
mirror.packages_to_sync = {"Foo": 1}
package = Package("Foo", 1, mirror)
await package.sync()
# Cross-check that simple directory hashing is disabled.
assert not os.path.exists("web/simple/f/foo/index.html")
assert (
open("web/simple/foo/index.html").read()
== """\
<!DOCTYPE html>
<html>
<head>
<title>Links for Foo</title>
</head>
<body>
<h1>Links for Foo</h1>
{}
</body>
</html>
<!--SERIAL 654321-->\
""".format(
EXPECTED_REL_HREFS
)
)
示例2: test_package_sync_simple_page_with_files
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def test_package_sync_simple_page_with_files(mirror: Mirror) -> None:
mirror.packages_to_sync = {"foo": 1}
package = Package("foo", 1, mirror)
await package.sync()
assert not mirror.errors
assert (
open("web/simple/foo/index.html").read()
== """\
<!DOCTYPE html>
<html>
<head>
<title>Links for foo</title>
</head>
<body>
<h1>Links for foo</h1>
{}
</body>
</html>
<!--SERIAL 654321-->\
""".format(
EXPECTED_REL_HREFS
)
)
示例3: test_package_sync_does_not_touch_existing_local_file
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def test_package_sync_does_not_touch_existing_local_file(mirror: Mirror) -> None:
pkg_file_path_str = "web/packages/any/f/foo/foo.zip"
pkg_file_path = Path(pkg_file_path_str)
touch_files((pkg_file_path,))
with pkg_file_path.open("w") as f:
f.write("")
old_stat = pkg_file_path.stat()
mirror.packages_to_sync = {"foo": 1}
package = Package("foo", 1, mirror)
await package.sync()
assert not mirror.errors
# Use Pathlib + create a new object to ensure no caching
# Only compare the relevant stat fields
assert old_stat.st_mtime == Path(pkg_file_path_str).stat().st_mtime
assert old_stat.st_ctime == Path(pkg_file_path_str).stat().st_ctime
示例4: test_mirror_filter_packages_match
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def test_mirror_filter_packages_match(tmpdir: Path) -> None:
"""
Packages that exist in the blacklist should be removed from the list of
packages to sync.
"""
test_configuration = """\
[blacklist]
plugins = blacklist_project
packages =
example1
"""
Singleton._instances = {}
with open("test.conf", "w") as testconfig_handle:
testconfig_handle.write(test_configuration)
BandersnatchConfig("test.conf")
for plugin in filter_project_plugins():
plugin.initialize_plugin()
m = Mirror(tmpdir, mock.Mock())
m.packages_to_sync = {"example1": "", "example2": ""}
m._filter_packages()
assert "example1" not in m.packages_to_sync.keys()
示例5: test_mirror_filter_packages_nomatch_package_with_spec
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def test_mirror_filter_packages_nomatch_package_with_spec(tmpdir: Path) -> None:
"""
Package lines with a PEP440 spec on them should not be filtered from the
list of packages.
"""
test_configuration = """\
[blacklist]
packages =
example3>2.0.0
"""
Singleton._instances = {}
with open("test.conf", "w") as testconfig_handle:
testconfig_handle.write(test_configuration)
BandersnatchConfig("test.conf")
for plugin in filter_project_plugins():
plugin.initialize_plugin()
m = Mirror(tmpdir, mock.Mock())
m.packages_to_sync = {"example1": "", "example3": ""}
m._filter_packages()
assert "example3" in m.packages_to_sync.keys()
示例6: mirror_sync_package_error_early_exit
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def mirror_sync_package_error_early_exit(mirror: Mirror) -> None:
mirror.master.all_packages = asynctest.CoroutineMock( # type: ignore
return_value={"foo": 1}
)
with Path("web/simple/index.html").open("wb") as index:
index.write(b"old index")
mirror.errors = True
mirror.stop_on_error = True
with pytest.raises(SystemExit):
await mirror.synchronize()
assert """\
.lock
generation
todo
web{0}packages{0}any{0}f{0}foo{0}foo.zip
web{0}simple{0}foo{0}index.html
web{0}simple{0}index.html""".format(
sep
) == utils.find(
mirror.homedir, dirs=False
)
assert open("web{0}simple{0}index.html".format(sep)).read() == "old index"
assert open("todo").read() == "1\n"
示例7: test_find_package_indexes_in_dir_threaded
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def test_find_package_indexes_in_dir_threaded(mirror: Mirror) -> None:
directories = (
"web/simple/peerme",
"web/simple/click",
"web/simple/zebra",
"web/simple/implicit",
"web/simple/pyaib",
"web/simple/setuptools",
)
with TemporaryDirectory() as td:
# Create local mirror first so we '_bootstrap'
mirror_base = Path(td)
local_mirror = Mirror(mirror_base, mirror.master, stop_on_error=True)
# Create fake file system objects
for directory in directories:
(mirror_base / directory).mkdir(parents=True, exist_ok=True)
with (mirror_base / "web/simple/index.html").open("w") as index:
index.write("<html></html>")
packages = local_mirror.find_package_indexes_in_dir(mirror_base / "web/simple")
assert "index.html" not in packages # This should never be in the list
assert len(packages) == 6 # We expect 6 packages with 6 dirs created
assert packages[0] == "click" # Check sorted - click should be first
示例8: get_requirements
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def get_requirements(self, optional=False):
if not any(r.is_optional() == optional for r in self.package.all_requires):
return
with self.requirements_path.open(encoding='utf-8') as f:
document = f.read()
template = Environment().from_string(document)
document = template.render(
package=self.package,
optional=optional,
format_vcs=self._format_vcs,
)
# rm junk
document = document.replace(' ', '')
# sort lines
lines = sorted(line for line in document.split('\n') if line)
document = '\n'.join(lines) + '\n'
return document
示例9: get_setup
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def get_setup(self):
# render template
with self.setup_path.open(encoding='utf-8') as f:
document = f.read()
template = Environment().from_string(document)
document = template.render(
package=self.package,
format_vcs=self._format_vcs,
)
# format by yapf
style = CreateGoogleStyle()
document, _changed = FormatCode(document, style_config=style)
# remove empty strings
while '\n\n' in document:
document = document.replace('\n\n', '\n')
# format by autopep8
document = fix_code(document)
return document
示例10: sync
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def sync(self):
document = self.get_requirements(optional=False)
if document:
path = self.path / self.requirements_name
with path.open('w', encoding='utf-8') as f:
f.write(document)
document = self.get_requirements(optional=True)
if document:
path = self.path / self.constraints_name
with path.open('w', encoding='utf-8') as f:
f.write(document)
document = self.get_setup()
path = self.path / self.setup_name
with path.open('w', encoding='utf-8') as f:
f.write(document)
示例11: test_recarray_fromfile
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def test_recarray_fromfile(self):
data_dir = path.join(path.dirname(__file__), 'data')
filename = path.join(data_dir, 'recarray_from_file.fits')
fd = open(filename, 'rb')
fd.seek(2880 * 2)
r1 = np.rec.fromfile(fd, formats='f8,i4,a5', shape=3, byteorder='big')
fd.seek(2880 * 2)
r2 = np.rec.array(fd, formats='f8,i4,a5', shape=3, byteorder='big')
fd.close()
assert_equal(r1, r2)
示例12: test_tofile_fromfile
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def test_tofile_fromfile(self):
with temppath(suffix='.bin') as path:
path = Path(path)
np.random.seed(123)
a = np.random.rand(10).astype('f8,i4,a5')
a[5] = (0.5,10,'abcde')
with path.open("wb") as fd:
a.tofile(fd)
x = np.core.records.fromfile(path,
formats='f8,i4,a5',
shape=10)
assert_array_equal(x, a)
示例13: sandwich
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def sandwich(resources):
# Has XMP, docinfo, <?adobe-xap-filters esc="CRLF"?>, shorthand attribute XMP
return Pdf.open(resources / 'sandwich.pdf')
示例14: test_overwrite_input
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def test_overwrite_input(resources, outdir):
copy(resources / 'sandwich.pdf', outdir / 'sandwich.pdf')
p = Pdf.open(outdir / 'sandwich.pdf')
with pytest.raises(ValueError, match=r'overwrite input file'):
p.save(outdir / 'sandwich.pdf')
示例15: test_fail_only_overwrite_input_check
# 需要导入模块: from os import path [as 别名]
# 或者: from os.path import open [as 别名]
def test_fail_only_overwrite_input_check(monkeypatch, resources, outdir):
copy(resources / 'sandwich.pdf', outdir / 'sandwich.pdf')
p = Pdf.open(outdir / 'sandwich.pdf')
def mockraise(*args):
raise OSError("samefile mocked")
monkeypatch.setattr(os.path, 'samefile', mockraise)
with pytest.raises(OSError, match=r'samefile mocked'):
p.save(outdir / 'wouldwork.pdf')