本文整理汇总了Python中twisted.python.filepath.FilePath方法的典型用法代码示例。如果您正苦于以下问题:Python filepath.FilePath方法的具体用法?Python filepath.FilePath怎么用?Python filepath.FilePath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.python.filepath
的用法示例。
在下文中一共展示了filepath.FilePath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setUp
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def setUp(self):
super(FindMachinesIntegrationTests, self).setUp()
from .._discover import findMachines
self.findMachines = findMachines
packageDir = self.FilePath(self.pathDir).child("test_package")
packageDir.makedirs()
self.pythonPath = self.PythonPath([self.pathDir])
self.writeSourceInto(self.SOURCE, packageDir.path, '__init__.py')
subPackageDir = packageDir.child('subpackage')
subPackageDir.makedirs()
subPackageDir.child('__init__.py').touch()
self.makeModule(self.SOURCE, subPackageDir.path, 'module.py')
self.packageDict = self.loadModuleAsDict(
self.pythonPath['test_package'])
self.moduleDict = self.loadModuleAsDict(
self.pythonPath['test_package']['subpackage']['module'])
示例2: _findPath
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def _findPath(path, package):
cwd = FilePath(path)
src_dir = cwd.child("src").child(package.lower())
current_dir = cwd.child(package.lower())
if src_dir.isdir():
return src_dir
elif current_dir.isdir():
return current_dir
else:
raise ValueError(("Can't find under `./src` or `./`. Check the "
"package name is right (note that we expect your "
"package name to be lower cased), or pass it using "
"'--path'."))
示例3: setUp
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def setUp(self):
self.srcdir = FilePath(self.mktemp())
self.srcdir.makedirs()
packagedir = self.srcdir.child('inctestpkg')
packagedir.makedirs()
packagedir.child('__init__.py').setContent(b"""
from incremental import Version
introduced_in = Version('inctestpkg', 'NEXT', 0, 0).short()
next_released_version = "inctestpkg NEXT"
""")
self.getcwd = lambda: self.srcdir.path
self.packagedir = packagedir
class Date(object):
year = 2016
month = 8
self.date = Date()
示例4: setUp
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def setUp(self):
"""
Create a temporary directory with a package structure in it.
"""
self.entry = FilePath(mkdtemp())
self.addCleanup(self.entry.remove)
self.preTestModules = sys.modules.copy()
sys.path.append(self.entry.path)
pkg = self.entry.child("incremental_test_package")
pkg.makedirs()
pkg.child("__init__.py").setContent(
b"from incremental import Version\n"
b"version = Version('incremental_test_package', 1, 0, 0)\n")
self.svnEntries = pkg.child(".svn")
self.svnEntries.makedirs()
示例5: test_render
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def test_render(self):
"""
L{ResourceScriptDirectory.getChild} returns a resource which renders a
response with the HTTP 200 status code and the content of the rpy's
C{request} global.
"""
tmp = FilePath(self.mktemp())
tmp.makedirs()
tmp.child("test.rpy").setContent(b"""
from twisted.web.resource import Resource
class TestResource(Resource):
isLeaf = True
def render_GET(self, request):
return b'ok'
resource = TestResource()""")
resource = ResourceScriptDirectory(tmp._asBytesPath())
request = DummyRequest([b''])
child = resource.getChild(b"test.rpy", request)
d = _render(child, request)
def cbRendered(ignored):
self.assertEqual(b"".join(request.written), b"ok")
d.addCallback(cbRendered)
return d
示例6: test_renderException
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def test_renderException(self):
"""
L{ResourceScriptDirectory.getChild} returns a resource which renders a
response with the HTTP 200 status code and the content of the rpy's
C{request} global.
"""
tmp = FilePath(self.mktemp())
tmp.makedirs()
child = tmp.child("test.epy")
child.setContent(b'raise Exception("nooo")')
resource = PythonScript(child._asBytesPath(), None)
request = DummyRequest([b''])
d = _render(resource, request)
def cbRendered(ignored):
self.assertIn(b"nooo", b"".join(request.written))
d.addCallback(cbRendered)
return d
示例7: _pathOption
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def _pathOption(self):
"""
Helper for the I{--path} tests which creates a directory and creates
an L{Options} object which uses that directory as its static
filesystem root.
@return: A two-tuple of a L{FilePath} referring to the directory and
the value associated with the C{'root'} key in the L{Options}
instance after parsing a I{--path} option.
"""
path = FilePath(self.mktemp())
path.makedirs()
options = Options()
options.parseOptions(['--path', path.path])
root = options['root']
return path, root
示例8: test_pathServer
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def test_pathServer(self):
"""
The I{--path} option to L{makeService} causes it to return a service
which will listen on the server address given by the I{--port} option.
"""
path = FilePath(self.mktemp())
path.makedirs()
port = self.mktemp()
options = Options()
options.parseOptions(['--port', 'unix:' + port, '--path', path.path])
service = makeService(options)
service.startService()
self.addCleanup(service.stopService)
self.assertIsInstance(service.services[0].factory.resource, File)
self.assertEqual(service.services[0].factory.resource.path, path.path)
self.assertTrue(os.path.exists(port))
self.assertTrue(stat.S_ISSOCK(os.stat(port).st_mode))
示例9: test_notFound
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def test_notFound(self):
"""
If a request is made which encounters a L{File} before a final segment
which does not correspond to any file in the path the L{File} was
created with, a not found response is sent.
"""
base = FilePath(self.mktemp())
base.makedirs()
file = static.File(base.path)
request = DummyRequest([b'foobar'])
child = resource.getChildForRequest(file, request)
d = self._render(child, request)
def cbRendered(ignored):
self.assertEqual(request.responseCode, 404)
d.addCallback(cbRendered)
return d
示例10: test_securityViolationNotFound
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def test_securityViolationNotFound(self):
"""
If a request is made which encounters a L{File} before a final segment
which cannot be looked up in the filesystem due to security
considerations, a not found response is sent.
"""
base = FilePath(self.mktemp())
base.makedirs()
file = static.File(base.path)
request = DummyRequest([b'..'])
child = resource.getChildForRequest(file, request)
d = self._render(child, request)
def cbRendered(ignored):
self.assertEqual(request.responseCode, 404)
d.addCallback(cbRendered)
return d
示例11: test_forbiddenResource
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def test_forbiddenResource(self):
"""
If the file in the filesystem which would satisfy a request cannot be
read, L{File.render} sets the HTTP response code to I{FORBIDDEN}.
"""
base = FilePath(self.mktemp())
base.setContent(b'')
# Make sure we can delete the file later.
self.addCleanup(base.chmod, 0o700)
# Get rid of our own read permission.
base.chmod(0)
file = static.File(base.path)
request = DummyRequest([b''])
d = self._render(file, request)
def cbRendered(ignored):
self.assertEqual(request.responseCode, 403)
d.addCallback(cbRendered)
return d
示例12: test_indexNames
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def test_indexNames(self):
"""
If a request is made which encounters a L{File} before a final empty
segment, a file in the L{File} instance's C{indexNames} list which
exists in the path the L{File} was created with is served as the
response to the request.
"""
base = FilePath(self.mktemp())
base.makedirs()
base.child("foo.bar").setContent(b"baz")
file = static.File(base.path)
file.indexNames = [b'foo.bar']
request = DummyRequest([b''])
child = resource.getChildForRequest(file, request)
d = self._render(child, request)
def cbRendered(ignored):
self.assertEqual(b''.join(request.written), b'baz')
self.assertEqual(
request.responseHeaders.getRawHeaders(b'content-length')[0],
b'3')
d.addCallback(cbRendered)
return d
示例13: test_staticFile
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def test_staticFile(self):
"""
If a request is made which encounters a L{File} before a final segment
which names a file in the path the L{File} was created with, that file
is served as the response to the request.
"""
base = FilePath(self.mktemp())
base.makedirs()
base.child("foo.bar").setContent(b"baz")
file = static.File(base.path)
request = DummyRequest([b'foo.bar'])
child = resource.getChildForRequest(file, request)
d = self._render(child, request)
def cbRendered(ignored):
self.assertEqual(b''.join(request.written), b'baz')
self.assertEqual(
request.responseHeaders.getRawHeaders(b'content-length')[0],
b'3')
d.addCallback(cbRendered)
return d
示例14: test_ignoredExtensionsIgnored
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def test_ignoredExtensionsIgnored(self):
"""
A request for the I{base} child of a L{File} succeeds with a resource
for the I{base<extension>} file in the path the L{File} was created
with if such a file exists and the L{File} has been configured to
ignore the I{<extension>} extension.
"""
base = FilePath(self.mktemp())
base.makedirs()
base.child('foo.bar').setContent(b'baz')
base.child('foo.quux').setContent(b'foobar')
file = static.File(base.path, ignoredExts=(b".bar",))
request = DummyRequest([b"foo"])
child = resource.getChildForRequest(file, request)
d = self._render(child, request)
def cbRendered(ignored):
self.assertEqual(b''.join(request.written), b'baz')
d.addCallback(cbRendered)
return d
示例15: test_directoryWithoutTrailingSlashRedirects
# 需要导入模块: from twisted.python import filepath [as 别名]
# 或者: from twisted.python.filepath import FilePath [as 别名]
def test_directoryWithoutTrailingSlashRedirects(self):
"""
A request for a path which is a directory but does not have a trailing
slash will be redirected to a URL which does have a slash by L{File}.
"""
base = FilePath(self.mktemp())
base.makedirs()
base.child('folder').makedirs()
file = static.File(base.path)
request = DummyRequest([b"folder"])
request.uri = b"http://dummy/folder#baz?foo=bar"
child = resource.getChildForRequest(file, request)
self.successResultOf(self._render(child, request))
self.assertEqual(request.responseCode, FOUND)
self.assertEqual(request.responseHeaders.getRawHeaders(b"location"),
[b"http://dummy/folder/#baz?foo=bar"])