Exemple #1
0
from typing import Tuple, Generator, Any

from amino import List, Map, Lists, _, Regex, Either, Right, Left, Id, Path
from amino.state import EitherState, State
from amino.regex import Match
from amino.do import do
from amino.util.numeric import parse_int

Files = Map[Path, List[str]]
Entry = Map[str, Any]
excludes = List(Regex('In module imported'), Regex('__coconut__'))
error_rex = Regex('(?P<path>[^:]*):(?P<lnum>\d+)(:(?P<col>\d+))?: (error|note): (?P<error>.*)')
lnum_rex = Regex('# line (?P<lnum>\d+)')


@do(Either[str, Tuple[str, int, Either[str, int], str]])
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))


def update_for(path: Path, files: Files) -> Files:
    return files if path in files else files + (path, Lists.lines(path.read_text()))


@do(Either[str, Entry])
def substitute(files: Files, path: Path, lnum: int, col: Either[str, int], error: str, coco_path: Path) -> Generator:
Exemple #2
0

class HsDirRplugin(Rplugin):
    pass


ctors: Map[str, Type[Rplugin]] = Map(
    dir=DirRplugin,
    site=SiteRplugin,
    venv=DistRplugin,
    hackage=HackageRplugin,
    stackage=StackageRplugin,
    hs_dir=HsDirRplugin,
)
prefixes = ctors.k.mk_string('|')
spec_rex = Regex(f'(?P<prefix>{prefixes}):(?P<spec>.*)')


@do(Either[str, Tuple[Type[Rplugin], str]])
def parse_spec(raw_spec: str) -> Do:
    match = yield spec_rex.match(raw_spec)
    prefix, spec = yield match.all_groups('prefix', 'spec')
    tpe = yield ctors.lift(prefix).to_either(
        'invalid rplugin spec prefix `{prefix}`')
    return tpe, spec


def cons_rplugin(conf: ConfigRplugin) -> Rplugin:
    tpe, spec = parse_spec(conf.spec).get_or_strict((DistRplugin, conf.spec))
    return tpe.cons(
        conf.name.get_or_strict(spec),
Exemple #3
0
from amino import Dat, do, Either, Do, Right, List, Nil, Regex, _, Boolean
from amino.util.numeric import parse_int

from chiasma.io.compute import TmuxIO
from chiasma.command import tmux_data_cmd, TmuxCmdData
from chiasma.commands.session import session_id

window_id_re = Regex('^@(?P<id>\d+)$')


@do(Either[str, int])
def parse_window_id(window_id: str) -> Do:
    match = yield window_id_re.match(window_id)
    id_s = yield match.group('id')
    yield parse_int(id_s)


def window_id(id: int) -> str:
    return f'@{id}'


class WindowData(Dat['WindowData']):
    @staticmethod
    @do(Either[str, 'WindowData'])
    def from_tmux(window_id: str, window_width: str, window_height: str) -> Do:
        id = yield parse_window_id(window_id)
        w = yield parse_int(window_width)
        h = yield parse_int(window_height)
        yield Right(WindowData(id, w, h))

    def __init__(self, id: int, width: int, height: int) -> None:
Exemple #4
0
from amino import List, Either, Regex, do, Do, Dat, Right, Try, Left, Boolean, L, Lists, _, Path, IO
from amino.util.numeric import parse_int
from amino.logging import module_log

from chiasma.io.compute import TmuxIO
from chiasma.command import tmux_data_cmd, TmuxCmdData, simple_tmux_cmd_attr
from chiasma.data.window import Window
from chiasma.data.pane import Pane
from chiasma.commands.window import window_id, parse_window_id
from chiasma.commands.session import parse_session_id
from chiasma.ui.view import UiPane

log = module_log()
pane_id_re = Regex('^%(?P<id>\d+)$')


@do(Either[str, int])
def parse_pane_id(pane_id: str) -> Do:
    match = yield pane_id_re.match(pane_id)
    id_s = yield match.group('id')
    yield parse_int(id_s)


@do(Either[str, bool])
def parse_bool(data: str) -> Do:
    as_int = yield parse_int(data)
    yield (Right(as_int == 1) if as_int in [0, 1] else
           Left(f'invalid number for boolean: {as_int}'))


def pane_id(id: int) -> str:
Exemple #5
0
from amino import Dat, do, Either, Do, Right, List, Nil, Regex, _, Boolean
from amino.util.numeric import parse_int

from chiasma.io.compute import TmuxIO
from chiasma.command import tmux_data_cmd, TmuxCmdData

session_id_re = Regex('^\$(?P<id>\d+)$')


@do(Either[str, int])
def parse_session_id(session_id: str) -> Do:
    match = yield session_id_re.match(session_id)
    id_s = yield match.group('id')
    yield parse_int(id_s)


def session_id(id: int) -> str:
    return f'${id}'


class SessionData(Dat['SessionData']):
    @staticmethod
    @do(Either[str, 'SessionData'])
    def from_tmux(session_id: str) -> Do:
        id = yield parse_session_id(session_id)
        yield Right(SessionData(id))

    def __init__(self, id: int) -> None:
        self.id = id

Exemple #6
0
from typing import Generator
import re
import requests

from lxml import etree

from amino import Maybe, List, Nil, Lists, Boolean, Regex, Try, Just
from amino.do import tdo

sanitize_re = re.compile('[._ ]')
url_re = Regex('/(?P<hash>[^/.]+).torrent')
size_re = Regex('(?P<num>[\d.]+) (?P<prefix>[MG])B')


def magnet(hash: str) -> str:
    return f'magnet:?xt=urn:btih:{hash}'


@tdo(Maybe[int])
def parse_size(size_str: str) -> Generator:
    match = yield size_re.match(size_str).to_maybe
    num = yield match.group('num').to_maybe
    prefix = yield match.group('prefix').to_maybe
    mult = 1e6 if prefix == 'M' else 1e9
    num_i = yield Try(float, num).to_maybe
    yield Just(num_i * mult)


def sanitize_query(query: str) -> str:
    return sanitize_re.sub('-', query)