Exemplo n.º 1
0
 def eff_flat_io_empty(self):
     t = IO.now(List(Right(Empty())))
     target = List(Right(Empty()))
     res = (t.effs(
         4, List, Either, Maybe,
         Either).flat_map(lambda x: IO.now(List(Right(Just(Right(1)))))))
     res.value.run().should.equal(target)
Exemplo n.º 2
0
 def eff_flat(self):
     a, b = self._r
     t = List(Just(Right(Just(a))))
     target = List(Just(Right(Just(a + b))))
     res = (t.effs(
         3, Maybe, Either,
         Maybe).flat_map(lambda x: List(Just(Right(Just(x + b))))))
     res.value.should.equal(target)
Exemplo n.º 3
0
 def decode(self, tpe: Type[Maybe],
            data: Json) -> Either[JsonError, Maybe[A]]:
     inner = Lists.wrap(tpe.__args__).head
     return (Right(Nothing) if data.absent else maybe_from_object(
         data, inner) if data.object else decode.match(data) / Just
             if data.array else Right(Nothing) if data.null else inner.cata(
                 lambda a: decode_json_type_json(data, a) / Just, lambda:
                 data.scalar.e(f'invalid type for `Maybe`: {data}',
                               Maybe.check(data.data))))
Exemplo n.º 4
0
    def eff(self) -> None:
        def f(a: int) -> EvalState[int, Either[str, int]]:
            return EvalState.pure(Right(2))

        s0 = EvalState.pure(Right(1))
        s0.eff(Either).flat_map(f).value.run(1)._value().should.equal(
            (1, Right(2)))
        (s0 // EvalState.modify(I).replace
         ).eff(Either).flat_map(f).value.run(1)._value().should.equal(
             (1, Right(2)))
Exemplo n.º 5
0
 def read_conf(self) -> Expectation:
     rplugins = conf_data.map(Rplugin.from_config)
     successes = List(
         Right(
             DirRplugin.cons('null', '/dev/null', True,
                             List('/some/path'))),
         Right(SiteRplugin.cons('plug', 'plug')),
     )
     return ((k(rplugins[:2]) == successes)
             & k(rplugins.lift(2)).must(be_just(be_left))
             & k(rplugins.lift(3)).must(be_just(be_left)))
Exemplo n.º 6
0
 def gather(self, po: ProgGather,
            output: Gather[A]) -> Prog[List[Either[IOException, A]]]:
     return Prog.pure(
         List(
             Right(
                 GatherSubprocessResult(
                     SubprocessResult(0, Nil, Nil, self.venv.name)))))
Exemplo n.º 7
0
    def staticmethod(self) -> None:
        @do(Either[str, int])
        def run() -> Do:
            decoded = yield _code_json(_SM.cons)
            return decoded()

        run().should.equal(Right(65))
Exemplo n.º 8
0
    def function(self) -> None:
        @do(Either[str, int])
        def run() -> Do:
            decoded = yield _code_json(Fun(encode_me))
            return decoded.f()

        run().should.equal(Right(5))
Exemplo n.º 9
0
def extract(match: Match) -> Generator:
    path = yield match.group('path')
    lnum = yield match.group('lnum')
    lnum_i = yield parse_int(lnum)
    col = match.group('col') // parse_int
    error = yield match.group('error')
    yield Right((Path(path), lnum_i, col, error))
Exemplo n.º 10
0
def read_result(result: TmuxCmdResult) -> Either[List[str], List[str]]:
    return (
        Right(result.output.output) if isinstance(result, TmuxCmdSuccess) else
        Left(result.output.output.cons(f'tmux command {result.cmd} failed:'))
        if isinstance(result, TmuxCmdError) else Left(
            result.output.output.cons(
                f'fatal tmux command execution in {result.cmds}:')))
Exemplo n.º 11
0
def parse_magnet(magnet) -> Either[str, Magnet]:
    pr = urlparse(magnet)
    if pr.scheme == 'magnet':
        q = Map(parse_qs(pr.query)).valmap(List.wrap)
        name = q.get('dn') // _.head
        return Right(Magnet(name, q))
    else:
        return Left('not a magnet')
Exemplo n.º 12
0
def substitute(files: Files, path: Path, lnum: int, col: Either[str, int], error: str, coco_path: Path) -> Generator:
    lines = yield files.lift(path).to_either('corrupt state')
    line = yield lines.lift(lnum - 1).to_either(f'invalid line number {lnum} for {path}')
    lnum_match = yield lnum_rex.search(line)
    coco_lnum = yield lnum_match.group('lnum')
    coco_lnum_i = yield parse_int(coco_lnum)
    col_map = col / (lambda a: Map(col=a)) | Map()
    yield Right(Map(lnum=coco_lnum_i, text=error, valid=1, maker_name='mypy') ** col_map)
Exemplo n.º 13
0
 def optional(self) -> None:
     a = 'a'
     b = 'b'
     Maybe.optional(a).to_maybe.should.just_contain(a)
     Empty().to_maybe.should.be.a(Empty)
     Maybe.optional(a).to_either(b).should.equal(Right(a))
     Empty().to_either(b).should.equal(Left(b))
     Empty().to_either(lambda: b).should.equal(Left(b))
Exemplo n.º 14
0
def single_venv_config(name: str, spec: str,
                       **extra_vars: Any) -> Tuple[Rplugin, Venv, TestConfig]:
    rplugin = simple_rplugin(name, spec)
    dir = temp_dir('rplugin', 'venv')
    vars = Map(chromatin_venv_dir=str(dir))**Map(extra_vars)
    conf = lens.basic.state_ctor.set(LogBufferEnv.cons)(chromatin_config)
    venv = Venv(
        rplugin.name,
        VenvMeta(name, dir / name, Right(Path('/dev/null')),
                 Right(Path('/dev/null'))))
    return rplugin, venv, TestConfig.cons(
        conf,
        vars=vars,
        io_interpreter=single_venv_io_interpreter(venv),
        logger=buffering_logger,
        function_handler=test_function_handler(exists=1),
        command_handler=test_command_handler(),
    )
Exemplo n.º 15
0
def type_arg(tpe: type, index: int) -> Do:
    def error() -> str:
        return f'{tpe} has no type args'

    raw = yield Maybe.getattr(tpe, '__args__').to_either_f(error)
    types = yield Right(Lists.wrap(raw)) if isinstance(
        raw, Iterable) else Left(error())
    yield types.lift(index).to_either_f(
        lambda: f'{tpe} has less than {index + 1} args')
Exemplo n.º 16
0
 def run(data: Json) -> Do:
     mod_field = yield data.field('mod')
     mod_path = yield mod_field.as_scalar
     names_field = yield data.field('names')
     names_json = yield names_field.as_array
     names = Lists.wrap(names_json.native)
     mod = yield (Either.import_module(mod_path.native) if isinstance(
         mod_path.native, str) else Left(
             JsonError(data, 'module is not a string')))
     yield names.fold_m(Right(mod))(Either.getattr)
Exemplo n.º 17
0
    def traverse(self) -> None:
        def f(a: int) -> Either[str, int]:
            return Right(a * 2)

        Map({
            1: 2,
            3: 4
        }).traverse(f, Either).should.equal(Right(Map({
            1: 4,
            3: 8
        })))
Exemplo n.º 18
0
 def eff_flat_task(self):
     a, b = self._r
     t = Task.now(List(Right(Just(Right(a)))))
     target = List(Right(Just(Right(a + b))))
     res = (t.effs(4, List, Either, Maybe, Either).flat_map(
         lambda x: Task.now(List(Right(Just(Right(x + b)))))))
     res.value.run().should.equal(target)
Exemplo n.º 19
0
 def eff_map(self):
     a, b = self._r
     c = -1
     t = Right(List(Just(Right(a)), Just(Left(c)), Empty()))
     target = Right(List(Just(Right(a + b)), Just(Left(c)), Empty()))
     res = t.effs(3).map(_ + b)
     res.value.should.equal(target)
Exemplo n.º 20
0
 def from_tmux(
     pane_id: str,
     pane_width: str,
     pane_height: str,
     pane_top: str,
     pane_pid: str,
     window_id: str,
     session_id: str,
 ) -> Do:
     id = yield parse_pane_id(pane_id)
     width = yield parse_int(pane_width)
     height = yield parse_int(pane_height)
     top = yield parse_int(pane_top)
     pid = yield parse_int(pane_pid)
     wid = yield parse_window_id(window_id)
     sid = yield parse_session_id(session_id)
     yield Right(PaneData(id, width, height, top, pid, wid, sid))
Exemplo n.º 21
0
 def as_array(self) -> Either[JsonError, 'JsonArray']:
     return Right(self) if self.array else Left(self.error('not an array'))
Exemplo n.º 22
0
 def encode(self, a: Maybe[A]) -> Either[JsonError, Json]:
     return Right(JsonScalar(a | None))
Exemplo n.º 23
0
 def encode(self, a: Union[Number, str, None]) -> Either[JsonError, Json]:
     return Right(JsonScalar(a))
Exemplo n.º 24
0
def either_from_object(data: JsonObject, ltype: Type[A], rtype: Type[B]) -> Do:
    value = yield data.field('value')
    decoded = yield decode.match(value)
    tpe = yield data.tpe
    yield Right(tpe(decoded))
Exemplo n.º 25
0
def decode_type(data: Json) -> Do:
    mod = yield data.field('mod')
    names = yield data.field('names')
    yield (Right(type(None)) if mod.native == 'builtins'
           and names.native == ['NoneType'] else decode_instance(data, 'type'))
Exemplo n.º 26
0
 def fix(s: Map, r: Map) -> Id[Tuple[Map, Either[str, Map]]]:
     return Id((s, Right(r)))
Exemplo n.º 27
0
 def eff_flat_empty(self):
     t = List(Right(Empty()))
     target = List(Right(Empty()))
     res = (t.effs(3, Either, Maybe,
                   Maybe).flat_map(lambda x: List(Right(Just(Just(1))))))
     res.value.should.equal(target)
Exemplo n.º 28
0
 def as_object(self) -> Either[JsonError, 'JsonObject']:
     return Right(self) if self.object else Left(
         self.error('not an object'))
Exemplo n.º 29
0
 def eff_flat_io_left(self):
     a, b = self._r
     t = IO.now(Left(Just(a)))
     target = Left(Just(a))
     res = t.effs(1, Either, Maybe) // (lambda x: IO.now(Right(Just(b))))
     res.value.run().should.equal(target)
Exemplo n.º 30
0
 def field(self, key: str) -> Either[JsonError, Json]:
     return Right(
         self.data.lift(key) | JsonAbsent(self.error(f'no field `{key}`')))