def activate(self): # Argument for our command, which stores the csv file path. path_argument = Argument(("csv_file",), required=True, type=str) interval_option = Option(("-i", "--interval"), type=int, default=10, help="Sets the time between two checks in seconds") self.commands.register("csv_watcher_list", "Shows all csv watchers", self.csv_watcher_list) self.commands.register("csv_watcher_add", "Adds a permanent watcher", self.csv_watcher_add, params=[path_argument, interval_option]) self.commands.register("csv_watcher_delete", "Removes a permanent watcher", self.csv_watcher_delete, params=[path_argument]) self.setup_db() self.load_watchers()
def decorator(f: Command) -> Command: f.params.append( Argument(("yamlfile", ), type=click.Path(exists=True, dir_okay=False))) f.params.append( Option(("--format", "-f"), type=click.Choice(g.valid_formats), help="Output format", default=g.valid_formats[0])) f.params.append( Option(("--metadata/--no-metadata", ), default=True, help="Include metadata in output")) f.params.append( Option(("--useuris/--metauris", ), default=True, help="Include metadata in output")) f.params.append( Option(("--importmap", "-im"), type=click.File(), help="Import mapping file")) f.params.append( Option(("--log_level", ), type=click.Choice(_LOG_LEVEL_STRINGS), help=f"Logging level (default={DEFAULT_LOG_LEVEL})", default=DEFAULT_LOG_LEVEL)) return f
class Python(): """:wrench: Start a Python shell with PYTHONPATH set""" ctx = CONTEXT pythonpath = Option( ['--pythonpath', '-p'], metavar='PYTHONPATH', default=None, help='Paths to prepend to PYTHONPATH') extra_argv = Argument( ['extra_argv'], nargs=-1, metavar='ARGS', required=False) @classmethod def _setup(cls, pythonpath, **kwargs): vals = Build.opt_defaults() vals.update(kwargs) Build.run(add_path=True, **vals) if pythonpath: for p in reversed(pythonpath.split(os.pathsep)): sys.path.insert(0, p) @classmethod def run(cls, pythonpath, extra_argv=None, **kwargs): cls._setup(pythonpath, **kwargs) if extra_argv: # Don't use subprocess, since we don't want to include the # current path in PYTHONPATH. sys.argv = extra_argv with open(extra_argv[0], 'r') as f: script = f.read() sys.modules['__main__'] = new_module('__main__') ns = dict(__name__='__main__', __file__=extra_argv[0]) exec(script, ns) else: import code code.interact()
def activate(self): self.commands.register("recipe_list", "Lists all recipes", self._recipe_list) self.commands.register("recipe_build", "Builds a given recipe", self._recipe_build, params=[Argument(("recipe", ), required=True)]) self.recipes.register( "gw_package", os.path.abspath( os.path.join(os.path.dirname(__file__), "../recipes/gw_package")), description="Groundwork basic package. Includes places for " "apps, plugins, patterns and recipes.", final_words="Recipe Installation is done.\n\n" "During development use buildout:\n" "Run: python bootstrap.py\n" "Then: bin/buildout\n" "Start the app: bin/app\n\n" "For installation run: 'python setup.py install' \n" "For documentation run: 'make html' inside doc folder " "(after installation!)\n\n" "For more information, please take a look into the README file " "to know how to go on.\n" "For help visit: https://groundwork.readthedocs.io\n\n" "Have fun with your groundwork package.")
def test_command_mandatory_argument(basicApp): """ This test case registers a command with a mandatory argument. Then it calls that command twice: first with the mandatory argument there; then with it missing. It is asserted, that the first call returns 0, the second one 2 as exit code. """ def _test_command(*args, **kwargs): print(args) print(kwargs) plugin = basicApp.plugins.get("CommandPlugin") plugin.commands.unregister("test") # register a command with a mandatory argument plugin.commands.register("test", "my test command", _test_command, params=[Argument(("man_arg", ), required=True)]) # call command with argument -> expect ok result = CliRunner().invoke( basicApp.commands.get("test").click_command, ["arg"]) assert result.exit_code == 0 # call command with no argument -> expect error result = CliRunner().invoke( basicApp.commands.get("test").click_command, []) assert result.exit_code == 2
def activate(self): # Argument for our command, which stores the csv file path. path_argument = Argument(("csv_file", ), required=True, type=str) interval_option = Option( ("-i", "--interval"), type=int, default=10, help="Sets the time between two checks in seconds") self.commands.register("csv_watch", "Monitors csv files", self.csv_watcher_command, params=[path_argument, interval_option]) self.signals.connect(receiver="csv_change_receiver", signal="csv_watcher_change", function=self.csv_change_monitor, description="Gets called for each csv change") csv_files_by_config = self.app.config.get("CSV_FILES", []) csv_interval_by_config = self.app.config.get("CSV_INTERVAL", 5) for csv_file in csv_files_by_config: self.csv_watcher_command(csv_file, csv_interval_by_config)
def test_command_path_argument(basicApp): """ This test case registers a command with a path argument that need to exist. Then it calls that command twice: first with an existing path (the path of this source file); then with a non-existent one. It is asserted, that the first call returns 0, the second one 2 as exit code. """ def _test_command(*args, **kwargs): print(args) print(kwargs) plugin = basicApp.plugins.get("CommandPlugin") plugin.commands.unregister("test") # register a command with a path argument, that must exist plugin.commands.register( "test", "my test command", _test_command, params=[Argument(("man_arg", ), type=click.Path(exists=True))]) # call command with existing path as argument -> expect ok result = CliRunner().invoke( basicApp.commands.get("test").click_command, [__file__]) assert result.exit_code == 0 # call command with non-existing path as argument -> expect error result = CliRunner().invoke( basicApp.commands.get("test").click_command, ['no such path']) assert result.exit_code == 2
def test_command_optional_argument(basicApp): """ This test case registers a command with an optional argument. Then it calls that command twice: first with the optional argument there; then with it missing. It is asserted, that both calls return 0 exit code. """ def _test_command(*args, **kwargs): print(args) print(kwargs) plugin = basicApp.plugins.get("CommandPlugin") plugin.commands.unregister("test") # register a command with an optional argument plugin.commands.register("test", "my test command", _test_command, params=[Argument(("opt_arg", ), required=False)]) # call command with argument -> expect ok result = CliRunner().invoke( basicApp.commands.get("test").click_command, ["arg"]) assert result.exit_code == 0 # call command with no argument -> expect ok result = CliRunner().invoke( basicApp.commands.get("test").click_command, []) assert result.exit_code == 0
class Doc(Task): """:wrench: Build documentation TARGETS: Sphinx build targets [default: 'html-scipyorg'] """ ctx = CONTEXT args = Argument(['args'], nargs=-1, metavar='TARGETS', required=False) list_targets = Option( ['--list-targets', '-t'], default=False, is_flag=True, help='List doc targets', ) parallel = Option(['--parallel', '-j'], default=1, metavar='PARALLEL', help="Number of parallel jobs") @classmethod def task_meta(cls, list_targets, parallel, args, **kwargs): if list_targets: # list MAKE targets, remove default target task_dep = [] targets = '' else: task_dep = ['build'] targets = ' '.join(args) if args else 'html-scipyorg' kwargs.update(cls.ctx.get()) Args = namedtuple('Args', [k for k in kwargs.keys()]) build_args = Args(**kwargs) dirs = Dirs(build_args) make_params = [f'PYTHON="{sys.executable}"'] if parallel: make_params.append(f'SPHINXOPTS="-j{parallel}"') return { 'actions': [ # move to doc/ so local scipy does not get imported (f'cd doc; env PYTHONPATH="{dirs.site}" ' f'make {" ".join(make_params)} {targets}'), ], 'task_dep': task_dep, 'io': { 'capture': False }, }
def activate(self): # Argument for our command, which stores the csv file path. path_argument = Argument(("csv_file", ), required=True, type=str) interval_option = Option( ("-i", "--interval"), type=int, default=10, help="Sets the time between two checks in seconds") self.commands.register("csv_watch", "Monitors csv files", self.csv_watcher_command, params=[path_argument, interval_option])
def activate(self): self.commands.register( "resources", "Prints used resources", self._print_resources, params=[ Argument(("resource", ), required=False), Option( ("--description", "-d"), required=False, help="Will print also a short description of each value", default=False, is_flag=True) ]) self.commands.register("resources_list", "Prints a list of all available resources", self._print_resources_list)
def activate(self): # Argument for our command, which stores the csv file path. path_argument = Argument(("csv_file", ), required=True, type=str) interval_option = Option( ("-i", "--interval"), type=int, default=10, help="Sets the time between two checks in seconds") self.commands.register("csv_watcher_list", "Shows all csv watchers", self.csv_watcher_list) self.commands.register("csv_watcher_add", "Adds a permanent watcher", self.csv_watcher_add, params=[path_argument, interval_option]) self.commands.register("csv_watcher_delete", "Removes a permanent watcher", self.csv_watcher_delete, params=[path_argument]) self.setup_db() self.load_watchers() self.web.db.register(self.Watcher, self.db.session) try: menu_csv = self.web.menus.register(name="CSV", link="#") except Exception: menu_csv = self.web.menus.get("CSV") with self.app.web.flask.app_context(): # Will be http://127.0.0.1:5000/admin/admin_csvwatchers/ menu_csv.register(name="Watchers", link=url_for("admin_csvwatchers.index_view")) self.web.rest.register(self.Watcher, self.db.session) with self.app.web.flask.app_context(): menu_csv.register(name="REST CsvWatchers", link=rest_url_for(self.Watcher))
def activate(self): self.commands.register("server_start", "starts a given server", self.__server_start, params=[Argument(("server",), required=True)]) self.commands.register("server_list", "prints a list of registered server", self.__server_list) template_folder = os.path.join(os.path.abspath(os.path.dirname(__file__)), "templates") static_folder = os.path.join(os.path.abspath(os.path.dirname(__file__)), "static") self.web.contexts.register("web", template_folder=template_folder, static_folder=static_folder, url_prefix="/", description="web context, which was created by GwWeb as initial context") if self.app.config.get("SHOW_WEB_TEST_PAGE", True): self.web.routes.register("/test", methods=["GET"], endpoint=self.__test_view, name="Test", description="Test view of GwWeb") # Register the flask debug server. self.web.servers.register("flask_debug", self.__start_flask_debug_server, "Starts the flask debug server")
class Test(Task): """:wrench: Run tests Examples: $ python do.py test -s {SAMPLE_SUBMODULE} $ python do.py test -t scipy.optimize.tests.test_minimize_constrained $ python do.py test -s stats -- --tb=line """ ctx = CONTEXT verbose = Option(['--verbose', '-v'], default=False, is_flag=True, help="more verbosity") # removed doctests as currently not supported by _lib/_testutils.py # doctests = Option(['--doctests'], default=False) coverage = Option(['--coverage'], default=False, is_flag=True, help=("report coverage of project code. " "HTML output goes under build/coverage")) submodule = Option( ['--submodule', '-s'], default=None, metavar='SUBMODULE', help="Submodule whose tests to run (cluster, constants, ...)") tests = Option(['--tests', '-t'], default=None, multiple=True, metavar='TESTS', help='Specify tests to run') mode = Option(['--mode', '-m'], default='fast', metavar='MODE', show_default=True, help=("'fast', 'full', or something that could be passed to " "`pytest -m` as a marker expression")) parallel = Option(['--parallel', '-j'], default=1, metavar='PARALLEL', help="Number of parallel jobs for testing") pytest_args = Argument(['pytest_args'], nargs=-1, metavar='PYTEST-ARGS', required=False) TASK_META = { 'task_dep': ['build'], } @classmethod def scipy_tests(cls, args, pytest_args): dirs = Dirs(args) dirs.add_sys_path() print(f"SciPy from development installed path at: {dirs.site}") # FIXME: support pos-args with doit extra_argv = pytest_args[:] if pytest_args else [] if extra_argv and extra_argv[0] == '--': extra_argv = extra_argv[1:] if args.coverage: dst_dir = dirs.root / args.build_dir / 'coverage' fn = dst_dir / 'coverage_html.js' if dst_dir.is_dir() and fn.is_file(): shutil.rmtree(dst_dir) extra_argv += ['--cov-report=html:' + str(dst_dir)] shutil.copyfile(dirs.root / '.coveragerc', dirs.site / '.coveragerc') # convert options to test selection if args.submodule: tests = [PROJECT_MODULE + "." + args.submodule] elif args.tests: tests = args.tests else: tests = None runner, version, mod_path = get_test_runner(PROJECT_MODULE) # FIXME: changing CWD is not a good practice with working_dir(dirs.site): print("Running tests for {} version:{}, installed at:{}".format( PROJECT_MODULE, version, mod_path)) # runner verbosity - convert bool to int verbose = int(args.verbose) + 1 result = runner( # scipy._lib._testutils:PytestTester args.mode, verbose=verbose, extra_argv=extra_argv, doctests=False, coverage=args.coverage, tests=tests, parallel=args.parallel) return result @classmethod def run(cls, pytest_args, **kwargs): """run unit-tests""" kwargs.update(cls.ctx.get()) Args = namedtuple('Args', [k for k in kwargs.keys()]) args = Args(**kwargs) return cls.scipy_tests(args, pytest_args)
from item.model.dimensions import list_pairs from item.model.plot import plot_all_item1 model = click.Group("model", help="Manipulate the model database.") def add(fn, *params): """Wrap *fn* and add it to the click.Group.""" model.add_command( click.Command(fn.__name__, callback=fn, help=fn.__doc__, params=list(params))) add(process_raw, Argument(["version"], type=int), Argument(["models"], nargs=-1)) add( list_pairs, Argument(["in_file"], default="quantities.tsv"), Argument(["out_file"], default="pairs.txt"), ) add( make_regions_csv, Argument(["out_file"]), Argument(["models"], default=[]), Option(["--compare"], type=click.Path()), )
), "rep_out_path": Option( ["--rep-out-path"], callback=default_path_cb("reporting_output"), help="Path for reporting output.", ), "rep_template": Option( ["--rep-template"], callback=default_path_cb("message_data", "tools", "post_processing", "MESSAGEix_WorkDB_Template.xlsx"), help="Path incl. filename and extension to reporting template.", ), "run_reporting_only": Option( ["--run-reporting-only"], is_flag=True, callback=store_context, help="Run only reporting.", ), "ssp": Argument(["ssp"], callback=store_context, type=Choice(["SSP1", "SSP2", "SSP3"])), "verbose": Option(["--verbose", "-v"], is_flag=True, help="Print DEBUG-level log messages."), }