Beispiel #1
0
def argument_decorator(f):
    """Decorates a function to create an annotation for adding parameters
    to qualify another.

    .. literalinclude:: /../examples/argdeco.py
       :lines: 5-25
    """
    return parser.use_mixin(DecoratedArgumentParameter, kwargs={"decorator": f})
Beispiel #2
0
def argument_decorator(f):
    """Decorates a function to create an annotation for adding parameters
    to qualify another.

    .. literalinclude:: /../examples/argdeco.py
       :lines: 5-24
    """
    return parser.use_mixin(
        DecoratedArgumentParameter, kwargs={'decorator': f})
Beispiel #3
0
def mapped(values, list_name="list", case_sensitive=None):
    """Creates an annotation for parameters that maps input values to Python
    objects.

    :param sequence values: A sequence of ``pyobj, names, description`` tuples.
        For each item, the user can specify a name from ``names`` and the
        parameter will receive the corresponding ``pyobj`` value.
        ``description`` is used when listing the possible values.
    :param str list_name: The value the user can use to show a list of possible
        values and their description.
    :param bool case_sensitive: Force case-sensitiveness for the input values.
        The default is to guess based on the contents of values.

    .. literalinclude:: /../examples/mapped.py
        :lines: 5-19

    """
    return parser.use_mixin(
        MappedParameter, kwargs={"case_sensitive": case_sensitive, "list_name": list_name, "values": values}
    )
Beispiel #4
0
def mapped(values, list_name='list', case_sensitive=None):
    """Creates an annotation for parameters that maps input values to Python
    objects.

    :param sequence values: A sequence of ``pyobj, names, description`` tuples.
        For each item, the user can specify a name from ``names`` and the
        parameter will receive the corresponding ``pyobj`` value.
        ``description`` is used when listing the possible values.
    :param str list_name: The value the user can use to show a list of possible
        values and their description.
    :param bool case_sensitive: Force case-sensitiveness for the input values.
        The default is to guess based on the contents of values.

    .. literalinclude:: /../examples/mapped.py
        :lines: 4-15

    """
    return parser.use_mixin(MappedParameter, kwargs={
        'case_sensitive': case_sensitive,
        'list_name': list_name,
        'values': values,
    })
Beispiel #5
0
def _main(
        *steps: (use_mixin(StepList), Parameter.REQUIRED),
        each_line: 'l' = False,  # noqa: F821
        raw: 'r' = False,  # noqa: F821
        start: (int, 's') = 0,  # noqa: F821
        end: (int, 'e') = None,  # noqa: F821
        prelude: (multi(), 'p') = 'pass',  # noqa: F821
        pipe_name: Parameter.UNDOCUMENTED = PIPE_NAME,
        no_default_fragments: Parameter.UNDOCUMENTED = False,
        no_exception_handling: Parameter.UNDOCUMENTED = False,
        show_fragments: Parameter.UNDOCUMENTED = False,
        break_: Parameter.UNDOCUMENTED = False):
    """Feed data through a sequence of Python expressions.

    :param steps: At least one Python expression (or suite) to execute
    :param each_line: Process lines as strings rather than all of stdin as a file
    :param start: Don't print before this result (zero-based)
    :param end: Stop after getting this result (zero-based)
    :param prelude: Execute a statement before running any steps. Can be specified more than once.
    :param raw: Don't add helper functionality to stdin
    """
    pipe_name = sys.intern(pipe_name)

    spy.context = context = make_context()

    for stmt in prelude:
        exec(stmt, context, context.view())

    step_src = steps
    steps = []
    for i, code in enumerate(step_src):
        fragment_name = 'Fragment {}'.format(i + 1)
        source = code
        literal = False
        if isinstance(code, _Decorated):
            source = '{} {!r}'.format(code.name, code.value)
            literal = code.LITERAL
            code, funcseq = code.value, code.funcseq
        else:
            funcseq = ()
        debuginfo = (fragment_name, source)
        if literal:
            ca = make_literal(code, context, pipe_name, debuginfo)
        else:
            try:
                co, is_expr = compile_(code, filename=fragment_name)
            except SyntaxError as e:
                pretty_syntax_error(code, e)
                if break_:  # pragma: no cover
                    debugger()
                sys.exit(1)
            ca = make_callable(co, is_expr, context, pipe_name, debuginfo)
        for fn in funcseq:
            ca = fn(ca)
        steps.append(spy.fragment(ca))

    index_offset = 0

    if not no_default_fragments:
        steps.append(fragments.make_limit(start=start, end=end))
        steps.append(fragments.print)

        if each_line:
            steps.insert(0, fragments.foreach)
            index_offset -= 1

    chain = spy.chain(steps, index_offset=index_offset)
    if raw:
        data = [sys.stdin]
    else:
        data = [SpyFile(sys.stdin)]

    if show_fragments:
        print(chain.format())
        return

    with ExitStack() as stack:
        if not no_exception_handling:
            stack.enter_context(catcher.handler(delete_all=True))
        if break_:  # pragma: no cover
            stack.enter_context(DebuggerContext())
        chain.run_to_exhaustion(data)
Beispiel #6
0
def one_of(*values):
    return parser.use_mixin(OneOfParameter, kwargs={'values': values})
Beispiel #7
0
def one_of(*values):
    return parser.use_mixin(OneOfParameter, kwargs={'values': values})
Beispiel #8
0
Datei: cli.py Projekt: edk0/spy
def _main(*steps: use_mixin(StepList),
          each_line: 'l' = False,  # noqa: F821
          raw: 'r' = False,  # noqa: F821
          start: (int, 's') = 0,
          end: (int, 'e') = None,
          prelude: (multi(), 'p') = 'pass',
          pipe_name: Parameter.UNDOCUMENTED = PIPE_NAME,
          no_default_fragments: Parameter.UNDOCUMENTED = False,
          no_exception_handling: Parameter.UNDOCUMENTED = False,
          show_fragments: Parameter.UNDOCUMENTED = False,
          break_: Parameter.UNDOCUMENTED = False):
    """Run Python code.

    :param steps: At least one Python expression (or suite) to execute
    :param each_line: If specified, process lines as strings rather than all of stdin as a file
    :param start: Don't print before this result (zero-based)
    :param end: Stop after getting this result (zero-based)
    :param prelude: Execute this statement before running any steps. Can be specified more than once.
    :param raw: Don't add helper functionality to stdin
    """
    pipe_name = sys.intern(pipe_name)

    spy.context = context = make_context()

    for stmt in prelude:
        exec(stmt, context, context.view())

    step_src = steps
    steps = []
    for i, code in enumerate(step_src):
        fragment_name = 'Fragment {}'.format(i + 1)
        source = code
        if isinstance(code, _Decorated):
            source = '{} {!r}'.format(code.name, code.value)
            code, funcseq = code.value, code.funcseq
        else:
            funcseq = ()
        try:
            co, is_expr = compile_(code, filename=fragment_name)
        except SyntaxError as e:
            pretty_syntax_error(code, e)
            if break_:  # pragma: no cover
                debugger()
            sys.exit(1)
        debuginfo = (fragment_name, source)
        ca = make_callable(co, is_expr, context, pipe_name, debuginfo)
        for fn in funcseq:
            try:
                ca = fn(ca, debuginfo=debuginfo)
            except TypeError:
                ca = fn(ca)
        steps.append(spy.fragment(ca))

    index_offset = 0

    if not no_default_fragments:
        steps.append(fragments.make_limit(start=start, end=end))
        steps.append(fragments.print)

        if each_line:
            steps.insert(0, fragments.foreach)
            index_offset -= 1

        if raw:
            steps.insert(0, fragments.raw_stdin)
        else:
            steps.insert(0, fragments.stdin)
        index_offset -= 1

    chain = spy.chain(steps, index_offset=index_offset)
    data = [None]

    if show_fragments:
        print(chain.format())
        return

    with ExitStack() as stack:
        if not no_exception_handling:
            stack.enter_context(catcher.handler(delete_all=True))
        if break_:  # pragma: no cover
            stack.enter_context(DebuggerContext())
        chain.run_to_exhaustion(data)