def test_node_depth_of_with_file(isfile): ("Node#depth_of(path) should return the approriate number") isfile.return_value = True Node("/foo/bar/").depth_of("/foo/bar/another/dir/file.py").should.equal(2) Node("/foo/bar").depth_of("/foo/bar/another/dir/file.py").should.equal(2) Node("/foo/bar//").depth_of("/foo/bar/another/dir/file.py").should.equal(2)
def test_cd_enters_a_path_and_returns_a_node_representing_it_abs(exists): ("Node#cd should return a node representing the given path. " "Testing with absolute path") nd = Node("/foo/bar/") other = nd.cd("/etc/") other.path.should.equal('/etc')
def test_open(io): ("Node#open should return an open file") io.open.return_value = 'an open file' nd = Node('/foo/bar') nd.open('wee.py').should.equal('an open file') io.open.assert_called_once_with('/foo/bar/wee.py')
def test_open(io): ("Node#open should return an open file") io.open.return_value = 'an open file' nd = Node('/foo/bar') nd.open('wee.py').should.equal('an open file') io.open.assert_once_called_with('/foo/bar/wee.py')
def test_node_list(os): ("Node#list should return a list containing one node " "per file found inside of the current node") os.listdir.return_value = ['/foo/bar/items/whatever.py'] nd = Node('/foo/bar/items/') nd.list().should.have.length_of(1) os.listdir.assert_once_called_with('/foo/bar/items')
def test_node_could_be_updated_by_false(exists): ("Node#could_be_updated_by returns False if given " "node has an older modification time") nd = Node(__file__) nd.metadata.mtime = 1000 other = Mock() other.metadata.mtime = 0 nd.could_be_updated_by(other).should.be.false
def test_contains_checks_if_path_exists(exists): ("Node#cd should return a node representing the given path.") exists.return_value = True nd = Node("/foo/bar/") nd.contains("file.py").should.be.truthy exists.assert_has_calls([ call('/foo/bar/file.py'), ]) exists.call_count.should.equal(1)
def test_node_list(os, isdir, dirname): ("Node#list should return a list containing one node " "per file found inside of the current node") dirname.return_value = '/foo/bar/items' isdir.return_value = False os.listdir.return_value = ['/foo/bar/items/whatever.py'] nd = Node('/foo/bar/items/') nd.list().should.have.length_of(1) os.listdir.assert_called_once_with('/foo/bar/items')
def test_trip_at_when_not_lazy_relative(os): ("Node#trip_at(path, lazy=False) returns a list when lazy=False " "(testing with relative path)") os.walk.return_value = [ ("/foo/bar/somewhere", [], ["file1.py", "file2.py"]), ] nd = Node("/foo/bar/") nd.trip_at('somewhere', lazy=False).should.equal([ "/foo/bar/somewhere/file1.py", "/foo/bar/somewhere/file2.py", ])
def test_trip_at_when_not_lazy_absolute(os): ("Node#trip_at(path, lazy=False) returns a list when lazy=False " "(testing with absolute path)") os.walk.return_value = [ ("/dummy/", [], ["file1.py", "file2.py"]), ] nd = Node("/foo/bar/") nd.trip_at('/dummy/', lazy=False).should.equal([ "/dummy/file1.py", "/dummy/file2.py", ])
def test_trip_at_when_lazy_relative(os): ("Node#trip_at(path, lazy=True) returns a generator when lazy=True " "(testing with relative path)") os.walk.return_value = [ ("/dummy/", [], ["file1.py", "file2.py"]), ] nd = Node("/foo/bar/") nd.trip_at('/dummy/', lazy=True).should.be.a('types.GeneratorType') list(nd.trip_at('/dummy/', lazy=True)).should.equal([ "/dummy/file1.py", "/dummy/file2.py", ])
def test_find_find_with_regexs_and_get_the_first_one_none(exists): ('Node#find returns the None if nothing is found') nd = Node('/foo/bar') nd.walk = Mock() nd.walk.return_value = [ "/foo/wisdom/aaa.py", "/foo/wisdom/bbb.txt", "/foo/wisdom/ccc.php", "/foo/wisdom/ddd.py", ] ret = nd.find('^$') ret.should.be.none nd.walk.assert_once_called_with(lazy=True)
def test_find_find_with_regexs_and_get_the_first_one_none(exists): ('Node#find returns the None if nothing is found') nd = Node('/foo/bar') nd.walk = Mock() nd.walk.return_value = [ "/foo/wisdom/aaa.py", "/foo/wisdom/bbb.txt", "/foo/wisdom/ccc.php", "/foo/wisdom/ddd.py", ] ret = nd.find('^$') ret.should.be.none nd.walk.assert_called_once_with(lazy=True)
def test_find_find_with_regexs_and_get_the_first_one(exists): ('Node#find returns the first result from find_with_regex when found') nd = Node('/foo/bar') nd.walk = Mock() nd.walk.return_value = [ "/foo/wisdom/aaa.py", "/foo/wisdom/bbb.txt", "/foo/wisdom/ccc.php", "/foo/wisdom/ddd.py", ] ret = nd.find('[.]\w{3}$') ret.should.be.a(Node) ret.should.equal(Node("/foo/wisdom/bbb.txt")) nd.walk.assert_called_once_with(lazy=True)
def test_find_find_with_regexs_and_get_the_first_one(exists): ('Node#find returns the first result from find_with_regex when found') nd = Node('/foo/bar') nd.walk = Mock() nd.walk.return_value = [ "/foo/wisdom/aaa.py", "/foo/wisdom/bbb.txt", "/foo/wisdom/ccc.php", "/foo/wisdom/ddd.py", ] ret = nd.find('[.]\w{3}$') ret.should.be.a(Node) ret.should.equal(Node("/foo/wisdom/bbb.txt")) nd.walk.assert_once_called_with(lazy=True)
def __init__(self, secret_key=settings.SECRET_KEY, host='127.0.0.1', port=None, ssl=None): app_node = Node(__file__).dir super(Application, self).__init__( __name__, static_folder=settings.STATIC_FOLDER_PATH, template_folder=settings.HTML_TEMPLATE_PATH, static_url_path=settings.STATIC_URL_PREFIX, ) self.scheme = ssl is None and 'http' or 'https' self.host = host self.port = int(port or get_free_tcp_port()) self.ssl = ssl self.config.from_object('oldspeak.settings') self.app_node = app_node self.secret_key = os.environ.get('SECRET_KEY') self.oauth = OAuth2Provider(self) self.secret_key = secret_key components = get_server_components() if not components: raise RuntimeError('no components found') for bp in components: logger.info('registering component {}'.format(bp)) self.register_blueprint(bp)
def test_find_with_regex_filters_results_from_walk_using_regex(exists): ('Node#find_with_regex returns a lazy list of nodes') nd = Node('/foo/bar') nd.walk = Mock() nd.walk.return_value = [ "/foo/wisdom/aaa.py", "/foo/wisdom/bbb.txt", "/foo/wisdom/ccc.php", "/foo/wisdom/ddd.py", ] ret = nd.find_with_regex('[.]\w{3}$', lazy='passed-to-walk') ret.should.be.a('types.GeneratorType') list(ret).should.equal([ Node("/foo/wisdom/bbb.txt"), Node("/foo/wisdom/ccc.php"), ]) nd.walk.assert_called_once_with(lazy='passed-to-walk')
def test_node_path_to_related(): ("Node#path_to_related(path) should return the approriate path") source_path = L('sandbox_simple/img/logo.png') requesting_path = L('sandbox_simple/index.md') result = Node(source_path).path_to_related(requesting_path) result.should.equal("./img/logo.png")
def test_find_with_regex_filters_results_from_walk_using_regex_nonlazy(exists): ('Node#find_with_regex returns an evaluated list of nodes') nd = Node('/foo/bar') nd.walk = Mock() nd.walk.return_value = [ "/foo/wisdom/aaa.py", "/foo/wisdom/bbb.txt", "/foo/wisdom/ccc.php", "/foo/wisdom/ddd.py", ] ret = nd.find_with_regex('[.]\w{3}$', lazy=False) ret.should.be.a(list) ret.should.equal([ Node("/foo/wisdom/bbb.txt"), Node("/foo/wisdom/ccc.php"), ]) nd.walk.assert_once_called_with(lazy=False)
def test_glob_filters_results_from_walk_using_fnmatch_nonlazy(exists): ('Node#glob returns an evaluated list of nodes') nd = Node('/foo/bar') nd.walk = Mock() nd.walk.return_value = [ "/foo/wisdom/aaa.py", "/foo/wisdom/bbb.txt", "/foo/wisdom/ccc.php", "/foo/wisdom/ddd.py", ] ret = nd.glob('*.py', lazy=False) ret.should.be.a(list) ret.should.equal([ Node("/foo/wisdom/aaa.py"), Node("/foo/wisdom/ddd.py"), ]) nd.walk.assert_once_called_with(lazy=False)
def test_glob_filters_results_from_walk_using_fnmatch(exists): ('Node#glob returns a lazy list of nodes') nd = Node('/foo/bar') nd.walk = Mock() nd.walk.return_value = [ "/foo/wisdom/aaa.py", "/foo/wisdom/bbb.txt", "/foo/wisdom/ccc.php", "/foo/wisdom/ddd.py", ] ret = nd.glob('*.py', lazy='passed-to-walk') ret.should.be.a('types.GeneratorType') list(ret).should.equal([ Node("/foo/wisdom/aaa.py"), Node("/foo/wisdom/ddd.py"), ]) nd.walk.assert_once_called_with(lazy='passed-to-walk')
def test_node_path_to_related_in_subtree(): ("Node#path_to_related(path) should return the " "approriate number when in a subtree") source_path = L('sandbox_simple/img/logo.png') requesting_path = L('sandbox_simple/docs/strings.md') result = Node(source_path).path_to_related(requesting_path) result.should.equal("../img/logo.png")
def test_node_path_to_related_in_subtree_deep_in_it(): ("Node#path_to_related(path) should return the " "approriate number when really deep in a subtree") source_path = L('sandbox_simple/img/logo.png') requesting_path = L('sandbox_simple/docs/even/deeper/item.md') result = Node(source_path).path_to_related(requesting_path) result.should.equal("../../../img/logo.png")
def test_node_non_existing_path_to_related(): ("Node#path_to_related(path) should return the approriate path" "even when it does not exist") source_path = L('sandbox_simple/img/404.png') requesting_path = L('sandbox_simple/index.markdown') result = Node(source_path).path_to_related(requesting_path) result.should.equal("./img/404.png")
def test_node_non_existing_path_to_related_in_subtree(): ("Node#path_to_related(path) should return the " "approriate number when in a subtree" "even when it does not exist") source_path = L('sandbox_simple/img/404.png') requesting_path = L('sandbox_simple/docs/strings.markdown') result = Node(source_path).path_to_related(requesting_path) result.should.equal("../img/404.png")
def test_node_depth_of(): ("Node#depth_of(path) should return the approriate number") path = L() (Node(path).depth_of(L('sandbox_simple/img/logo.png')).should.equal(2)) (Node(path).depth_of(L('sandbox_simple/img/')).should.equal(2)) (Node(path).depth_of(L('sandbox_simple/img')).should.equal(2)) path = L().rstrip('/') (Node(path).depth_of(L('sandbox_simple/img/logo.png')).should.equal(2)) (Node(path).depth_of(L('sandbox_simple/img/')).should.equal(2)) (Node(path).depth_of(L('sandbox_simple/img')).should.equal(2)) path = L() + "///" (Node(path).depth_of(L('sandbox_simple/img/logo.png')).should.equal(2)) (Node(path).depth_of(L('sandbox_simple/img/')).should.equal(2)) (Node(path).depth_of(L('sandbox_simple/img')).should.equal(2))
def test_walk_trips_at_node_path(): ("Node#walk() trips at node.path") nd = Node("/foo/bar/") nd.trip_at = Mock() nd.walk() nd.walk(lazy=True) nd.trip_at.assert_has_calls([ call('/foo/bar', lazy=False), call('/foo/bar', lazy=True), ])
class PipelineScanner(object): def __init__(self, lookup_path): self.node = Node(lookup_path) self.found = self.find_python_files() def find_python_files(self): found = [] for node in self.node.find_with_regex("pipelines.py$"): module_name = "{0}.{1}".format( node.dir.basename, node.basename.replace('.py', ''), ) try: found.append(imp.load_source(module_name, node.path)) except (ImportError, SystemError): msg = "Failed to import \033[1;33m%s\033[0m" logger.exception(msg, str(node.path)) return found def get_pipelines(self): return Registry.pipelines_by_name()
def test_find_with_regex_filters_results_from_walk_using_regex_nonlazy(exists): ('Node#find_with_regex returns an evaluated list of nodes') nd = Node('/foo/bar') nd.walk = Mock() nd.walk.return_value = [ "/foo/wisdom/aaa.py", "/foo/wisdom/bbb.txt", "/foo/wisdom/ccc.php", "/foo/wisdom/ddd.py", ] ret = nd.find_with_regex('[.]\w{3}$', lazy=False) ret.should.be.a(list) ret.should.equal([ Node("/foo/wisdom/bbb.txt"), Node("/foo/wisdom/ccc.php"), ]) nd.walk.assert_called_once_with(lazy=False)
def test_glob_filters_results_from_walk_using_fnmatch(exists): ('Node#glob returns a lazy list of nodes') nd = Node('/foo/bar') nd.walk = Mock() nd.walk.return_value = [ "/foo/wisdom/aaa.py", "/foo/wisdom/bbb.txt", "/foo/wisdom/ccc.php", "/foo/wisdom/ddd.py", ] ret = nd.glob('*.py', lazy='passed-to-walk') ret.should.be.a('types.GeneratorType') list(ret).should.equal([ Node("/foo/wisdom/aaa.py"), Node("/foo/wisdom/ddd.py"), ]) nd.walk.assert_called_once_with(lazy='passed-to-walk')
def test_glob_filters_results_from_walk_using_fnmatch_nonlazy(exists): ('Node#glob returns an evaluated list of nodes') nd = Node('/foo/bar') nd.walk = Mock() nd.walk.return_value = [ "/foo/wisdom/aaa.py", "/foo/wisdom/bbb.txt", "/foo/wisdom/ccc.php", "/foo/wisdom/ddd.py", ] ret = nd.glob('*.py', lazy=False) ret.should.be.a(list) ret.should.equal([ Node("/foo/wisdom/aaa.py"), Node("/foo/wisdom/ddd.py"), ]) nd.walk.assert_called_once_with(lazy=False)
def prepare_app(context): context.node = Node(__file__) context.app = Application(context.node) @context.app.route('/html') def html(): return context.app.template_response('index.html') @context.app.route('/text') def text(): return context.app.text_response('text') @context.app.route('/json') def json(): return context.app.json_response({ 'name': 'p4rr0t007' }) @context.app.route('/error') def error(): raise IOError('emulating a 500') context.http = context.app.test_client()
def get_js_nodes(): this_node = Node(__file__).dir js_node = this_node.cd('static/js') return js_node.glob('*.js')
# -*- coding: utf-8 -*- import os import shutil from plant import Node from gitgraph import GitGraph from sure import scenario node = Node(__file__).dir sandbox = node.cd('sandbox') def with_hexastore(name, **kw): if not os.path.exists(sandbox.path): os.makedirs(sandbox.path) def create_store(context): context.store_path = sandbox.join(name) context.store = GitGraph(os.path.relpath(context.store_path), **kw) def destroy_store(context): if os.path.isdir(context.store_path): shutil.rmtree(context.store_path) return scenario(create_store, destroy_store)
def test_node_path_to_related(exists): ("Node#path_to_related takes a path and returns the relative way there") nd = Node("/foo/bar/something.py") result = nd.path_to_related("/foo/docs/assets/style.css") result.should.equal('../../bar/something.py')
def test_node_dir_when_is_dir(): ("Node#dir should return itself when the current " "node is pointing to a file") nd = Node('/foo/bar/items/') nd.dir.path.should.equal('/foo/bar/items')
def test_cd_enters_a_path_and_returns_a_node_representing_it(exists): ("Node#cd should return a node representing the given path") nd = Node("/foo/bar/") other = nd.cd("awesome/") other.path.should.equal('/foo/bar/awesome')
def get_upload_node(): return Node(settings.UPLOAD_PATH)
#!/usr/bin/env python # -*- coding: utf-8 -*- # # flake8: noqa import os import logging from plant import Node from carpentry import routes from carpentry.server import CarpentryHttpServer log_path = os.getenv('CARPENTRY_LOG_PATH', '/var/log/carpentry.log') logging.basicConfig( filename=log_path, level=logging.INFO, ) root_node = Node(__file__).dir application = CarpentryHttpServer( log_level=logging.INFO, template_folder=root_node.join('templates'), static_folder=root_node.join('static'), static_url_path='/static', use_sqlalchemy=False )
# Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. # import sys import sphinx_rtd_theme from plant import Node sys.path.insert(0, Node(__file__).dir.join('../..')) from plural.version import version # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. # # needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [ 'sphinx.ext.autodoc', 'sphinx.ext.autosummary',
def __init__(self, lookup_path): self.node = Node(lookup_path) self.found = self.find_python_files()
# -*- coding: utf-8 -*- from datetime import datetime from plant import Node from flask import request from p4rr0t007.web import Application here = Node(__file__).dir server = Application(here, static_path='/dist', template_folder=here.path, static_folder=here.join('dist')) @server.route('/') def index(): return server.template_response("index.html", { 'user_token': request.cookies.get('bellyfeel_token') or '' }) @server.route('/api/login') def projects(): return server.json_response([ { "name": "Foo", "description": "The foo of the bar", "tags": ["test", "local"], "url": "http://foo.co", "last_build": datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S"), }, { "name": "Bar", "description": "The bar whence the foo belongs", "tags": ["example", "local"],
def test_node_relative(exists): ("Node#relative() returns a path relative to the base") nd = Node("/foo/bar/") nd.relative('/foo/bar/yes.py').should.equal('yes.py')