def main(prog_name: parameters.pass_name, command: LAST_OPTION, *args, profile: value_converter(lambda _: _, name='FILENAME') = None, memprofile: value_converter(lambda _: _, name='FILENAME') = None, verbose=False, dry_run=False): """IMPROVER NWP post-processing toolbox Results from commands can be passed into file-like arguments of other commands by surrounding them by square brackets:: improver command [ command ... ] ... Spaces around brackets are mandatory. Args: prog_name: The program name from argv[0]. command (str): Command to execute args (tuple): Command arguments profile (str): If given, will write profiling to the file given. To write to stdout, use a hyphen (-) memprofile (str): Creates 2 files, a tracemalloc snapsot at the point of highest memory consumption of your program (*_SNAPSHOT) and a track of the maximum memory used by your program over time (*_MAX_TRACKER). verbose (bool): Print executed commands dry_run (bool): Print commands to be executed See improver help [--usage] [command] for more information on available command(s). """ args = unbracket(args) exec_cmd = execute_command if profile is not None: from improver.profile import profile_hook_enable profile_hook_enable(dump_filename=None if profile == '-' else profile) if memprofile is not None: from improver.memprofile import memory_profile_decorator exec_cmd = memory_profile_decorator(exec_cmd, memprofile) result = exec_cmd(SUBCOMMANDS_DISPATCHER, prog_name, command, *args, verbose=verbose, dry_run=dry_run) return result
def test_method_conv(self): class Spam(object): def method(self, arg): return self s = Spam() conv = parser.value_converter(s.method, name="TCONV") sig = support.s("*, par: conv", locals={"conv": conv}) self._do_test(sig, parser.OptionParameter, "--par=TCONV", {"conv": conv}) csig = parser.CliSignature.from_signature(sig) ba = read_arguments(csig, ["--par=arg"]) arg = ba.kwargs["par"] self.assertTrue(arg is s)
def test_method_conv(self): class Spam(object): def method(self, arg): return self s = Spam() conv = parser.value_converter(s.method, name='TCONV') sig = support.s('*, par: conv', locals={'conv': conv}) self._do_test(sig, parser.OptionParameter, '--par=TCONV', {'conv': conv}) csig = parser.CliSignature.from_signature(sig) ba = self.read_arguments(csig, ['--par=arg']) arg = ba.kwargs['par'] self.assertTrue(arg is s)
def test_method_conv(self): class Spam(object): def method(self, arg): return self s = Spam() conv = parser.value_converter(s.method, name='TCONV') sig = support.s('*, par: conv', locals={'conv': conv}) self._do_test(sig, parser.OptionParameter, '--par=TCONV', { 'conv': conv }) csig = parser.CliSignature.from_signature(sig) ba = read_arguments(csig, ['--par=arg']) arg = ba.kwargs['par'] self.assertTrue(arg is s)
def main(prog_name: parameters.pass_name, command: LAST_OPTION, *args, profile: value_converter(lambda _: _, name='FILENAME') = None, verbose=False, dry_run=False): """IMPROVER NWP post-processing toolbox Results from commands can be passed into file-like arguments of other commands by surrounding them by square brackets:: improver command [ command ... ] ... Spaces around brackets are mandatory. Args: prog_name: The program name from argv[0]. command (str): Command to execute args (tuple): Command arguments profile (str): If given, will write profiling to the file given. To write to stdout, use a hyphen (-) verbose (bool): Print executed commands dry_run (bool): Print commands to be executed See improver help [--usage] [command] for more information on available command(s). """ args = unbracket(args) if profile is not None: from improver.profile import profile_hook_enable profile_hook_enable(dump_filename=None if profile == '-' else profile) result = execute_command(SUBCOMMANDS_DISPATCHER, prog_name, command, *args, verbose=verbose, dry_run=dry_run) return result
def file(stdio='-', keep_stdio_open=False, **kwargs): """Takes a file argument and provides a Python object that opens a file :: def main(in_: file(), out: file(mode='w')): with in_ as infile, out as outfile: outfile.write(infile.read()) :param stdio: If this value is passed as argument, it will be interpreted as *stdin* or *stdout* depending on the ``mode`` parameter supplied. :param keep_stdio_open: If true, does not close the file if it is *stdin* or *stdout*. Other arguments will be relayed to `io.open`. """ return parser.value_converter( partial(_FileOpener, kwargs=kwargs, stdio=stdio, keep_stdio_open=keep_stdio_open), name='FILE')
def file(arg=util.UNSET, stdio='-', keep_stdio_open=False, **kwargs): """Takes a file argument and provides a Python object that opens a file :: def main(in_: file(), out: file(mode='w')): with in_ as infile, out as outfile: outfile.write(infile.read()) :param stdio: If this value is passed as argument, it will be interpreted as *stdin* or *stdout* depending on the ``mode`` parameter supplied. :param keep_stdio_open: If true, does not close the file if it is *stdin* or *stdout*. Other arguments will be relayed to `io.open`. This converter also opens the file or stream designated by the default value:: def main(inf: file()='-'): with inf as f: print(f) .. code-block:: console $ python3 ./main.py <_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'> """ if arg is not util.UNSET: return _none_guard(_FileOpener, arg, kwargs, stdio, keep_stdio_open) return parser.value_converter( partial(_none_guard, _FileOpener, kwargs=kwargs, stdio=stdio, keep_stdio_open=keep_stdio_open), name='FILE', convert_default=True)
def _convert_coerce(func): if func not in parser._implicit_converters: func = parser.value_converter(func) return func
def file(**kwargs): return parser.value_converter( partial(_FileOpener, kwargs=kwargs), name='FILE')
def file(**kwargs): return parser.value_converter(partial(_FileOpener, kwargs=kwargs), name='FILE')
Naval battle example from docopt """ from collections import OrderedDict from clize import run, parser def ship_new(name): """Create a new ship name: The name to attribute to the ship """ return "Created ship {0}".format(name) knots = parser.value_converter(float, name='KN') def ship_move(ship, x: float, y: float, *, speed: knots = 10): """Move a ship ship: The ship which to move x: X coordinate y: Y coordinate speed: Speed in knots """ return "Moving ship {0} to {1},{2} with speed {3}".format( ship, x, y, speed)
""" from collections import OrderedDict from clize import run, parser def ship_new(name): """Create a new ship name: The name to attribute to the ship """ return "Created ship {0}".format(name) knots = parser.value_converter(float, name='KN') def ship_move(ship, x:float, y:float, *, speed:knots=10): """Move a ship ship: The ship which to move x: X coordinate y: Y coordinate speed: Speed in knots """ return "Moving ship {0} to {1},{2} with speed {3}".format(ship, x, y, speed)