def shelve(site, modified_only=False): "Shelve an application's stash, as a worker process." with no_pyc(): # Create shelve time dir if it does not exist try: os.makedirs(SHELVE_TIME_DIR) except (IOError, OSError): pass shelve_time_path = os.path.join(SHELVE_TIME_DIR, site) os.environ['SHELVE_TIME_PATH'] = shelve_time_path site = validate_site(site) Tango.shelve_by_name(site, modified_only=modified_only, logfile=sys.stdout) open(os.environ['SHELVE_TIME_PATH'], 'w').close()
def handle(self, _, site, *args, **kwargs): with no_pyc(): site = validate_site(site) app = Tango.get_app(site) if not app: return Command.handle(self, app, *args, **kwargs)
def handle(self, _, site, host, port, use_debugger, use_reloader): site = validate_site(site) app = Tango.get_app(site) if not app: return app.run(host=host, port=port, debug=use_debugger, use_debugger=use_debugger, use_reloader=use_reloader, **self.server_options)
def run(): sys.path.append('.') # Create a Manager instance to parse arguments & marshal commands. manager = Manager(Tango(__name__), with_default_commands=False) manager.add_command('serve', Server()) manager.add_command('shell', Shell()) manager.add_command('show', Show()) manager.add_command('get', Get()) manager.add_command('put', Put()) manager.add_command('drop', Drop()) manager.add_command('source', Source()) for cmd in commands: manager.command(cmd) manager.run()
def create_app(self): return Tango(__name__)
def create_app(self): return Tango.build_app('sampletypes', import_stash=True)
def create_app(self): return Tango.build_app('simplesite')
from flask import jsonify, request, abort from tango.app import Tango app = Tango(__name__) @app.route('/api/posts/') def posts(): stash = app.connector.get('presto', '/posts/') source = request.args.get('source', None) if source: stash['posts'] = [ entry for entry in stash['posts'] if entry['source'] == source ] return jsonify(stash) @app.route('/api/posts/<int:post_id>/') def post(post_id): stash = app.connector.get('presto', '/posts/') for entry in stash['posts']: if entry['id'] == post_id: return jsonify({'post': entry}) abort(404) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=9000)
def build_app(import_name, import_stash=False, use_snapshot=True, logfile=None): """Create a Tango application object from a Python import name. This function accepts three kinds of import names: 1. a single-module name where the module is itself a stash 2. a package name which has a submodule or sub-package named 'stash'. 3. a dotted module name referring to a module inside a package's stash. In case #1, the module is a self-contained application in one .py file. In case #2, the package is arbitrarily laid out, but the stash module inside it is one or more modules conforming to Tango's stash conventions. In case #3, the module is inside a package matching case #2, but the import name refers to a module which by itself would otherwise match case #1. Case #3 is treated like case #1 with one important exception. Since the module is inside a package, that package is used as the application object's import name for the purposes of loading configuration directives, as stash modules are allowed to and encouraged to use their projects config.py. This is essential to shelving modules in isolation when working with a stash package with more than one stash module. Example, using a single module called simplest.py (case #1): >>> app = build_app('simplest') >>> sorted(app.url_map.iter_rules(), key=lambda rule: rule.rule) ... # doctest:+NORMALIZE_WHITESPACE [<Rule '/' (HEAD, OPTIONS, GET) -> />, <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>] >>> Example, using the simplesite module in this project (case #2): >>> app = build_app('simplesite') >>> sorted(app.url_map.iter_rules(), key=lambda rule: rule.rule) ... # doctest:+NORMALIZE_WHITESPACE [<Rule '/' (HEAD, OPTIONS, GET) -> />, <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>] >>> Example, using submodule in the stash in a package with config (case #3): >>> app = build_app('simplesite.stash.index') >>> sorted(app.url_map.iter_rules(), key=lambda rule: rule.rule) ... # doctest:+NORMALIZE_WHITESPACE [<Rule '/' (HEAD, OPTIONS, GET) -> />, <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>] >>> Example, using submodule in the stash in a package without config (case #3 but identical to case #1): >>> app = build_app('testsite.stash.package.module') >>> sorted(app.url_map.iter_rules(), key=lambda rule: rule.rule) ... # doctest:+NORMALIZE_WHITESPACE [<Rule '/index.json' (HEAD, OPTIONS, GET) -> /index.json>, <Rule '/static/<filename>' (HEAD, OPTIONS, GET) -> static>] >>> For convenience in development, particularly with shell tab completion, a .py module name is also accepted: >>> build_app('simplest.py') # doctest:+ELLIPSIS <tango.app.Tango object at 0x...> >>> Avoids import side effects: >>> build_app('importerror.py') # doctest:+ELLIPSIS <tango.app.Tango object at 0x...> >>> build_app('importerror') # doctest:+ELLIPSIS <tango.app.Tango object at 0x...> >>> """ import_name = fix_import_name_if_pyfile(import_name) # Initialize application. See docstring above for construction logic. app = None package_name, module_name = package_submodule(import_name) if package_name and module_name: # import_name points to submodule, look for package config. root_package_name = namespace_segments(import_name)[0] if module_exists(root_package_name + '.config'): app = Tango(root_package_name) app.config.from_object(root_package_name + '.config') if app is None: app = Tango(import_name) # Check for a site config. if module_exists(import_name + '.config'): app.config.from_object(import_name + '.config') # Create app context, push it onto request stack for use in initialization. # Use `with` or try-finally to ensure context is popped in case of error. with app.request_context(create_environ()): # Load Tango filters. # Only needed for packages; single modules do not have implicit templates. if module_is_package(import_name): tango.filters.init_app(app) # Build application routes, checking for a snapshot first. routes = None if use_snapshot: routes = get_snapshot(import_name) if routes is None: build_options = {'import_stash': import_stash} build_options['logfile'] = logfile if module_exists(import_name + '.stash'): module = __import__(import_name, fromlist=['stash']).stash routes = build_module_routes(module, **build_options) else: routes = build_module_routes(import_name, **build_options) else: print 'Using snapshot with stashed routes.' app.routes = routes # Stitch together context, template, and path. for route in app.routes: app.build_view(route) app.context_processor(lambda: request.view_args) return app
def test_indexerror_app(self): # Must load single-module apps without import. # Test passes if Tango.get_app('indexerror') did not error out. Tango.get_app('indexerror')
from tango.app import Tango app = Tango.build_app('hybrid') app.this_was_added_after_stash = 'Hello, world!'
def get_app(site, module=None): try: return Tango.get_app(module or site) except ModuleNotFound: print "Cannot locate site: '{0}'.".format(site)
def create_app(self): # For application context setup only. return Tango(__name__)