def venv_package_status_site(venv: Venv, site: Path, req_spec: str) -> Do: ws = yield IO.delay(pkg_resources.WorkingSet, [site]) req = yield IO.delay(pkg_resources.Requirement, req_spec) return Maybe.check(ws.by_key.get(req.key)).cata_strict( lambda a: VenvPackageExistent(venv, a), VenvPackageAbsent(venv), )
def _lookup(self, data): def check(resp): return (IO.failed('no release found') if resp == -1 else IO.pure( (resp, self.rest))) return IO.delay(self.client.get, '{}_id'.format(self._type), body=data) // check
def find_interpreter(spec: str) -> Do: path = Path(spec) exists = yield IO.delay(path.exists) venv = env.get('VIRTUAL_ENV').get_or_strict('[no venv]') yield ( IO.pure(path) if exists else virtualenv_interpreter(venv, spec) )
def check_venv(base_dir: Path, plugin: Rplugin) -> Do: dir = base_dir / plugin.name pip = dir / 'bin' / 'pip' exists = yield IO.delay(pip.exists) return ( venv_present(dir, plugin) if exists else VenvAbsent(plugin) )
def list_shows(self, cmd): regex = cmd.args.head | '' def output(matches): if matches: for id, description in matches: i = ColorString('#{}'.format(id), term.green) d = ColorString(description, term.yellow) yield '{}: {}'.format(i, d) else: yield 'No matching show found.' return (IO.delay(self.client.get, 'show', body=dict(regex=regex)) / output / List.wrap)
def help(self, cmd): if self.doc: maxlen = len(max(self.doc.keys(), key=len)) pad = lambda s: s.ljust(maxlen) def output(): yield 'Available commands:' data = List.wrap(self.doc.items()).sort_by(_[0]) for name, (param_desc, desc) in data: yield '' yield '{} {}'.format(pad(name), param_desc) yield ' {}'.format(desc) return IO.delay(output) / '\n'.join else: return IO.failed('no doc for client')
def venv_site(venv: Venv) -> Do: lib_dir = venv.meta.dir / 'lib' libs = yield IO.delay(lib_dir.glob, 'python3.?') lib = Lists.wrap(libs).head.to_either(f'no python dirs in {lib_dir}') return lib.map(lambda a: a / 'site-packages')
def create_dir(dir: Path) -> IO[None]: return IO.delay(dir.mkdir, parents=True, exist_ok=True)
def child_pids(pid: int) -> Do: proc = yield IO.delay(Process, pid) children = yield IO.delay(proc.children, recursive=True) yield IO.delay(Lists.wrap(children).map, lambda a: a.pid)
def codegen(task: CodegenTask) -> Do: template_path = yield IO.delay(task.template.absolute) template_code = yield IO.delay(template_path.read_text) replaced = task.subs.fold_left(template_code)( lambda z, a: re.sub(a[0], a[1], z)) return task.imports.cat(replaced).join_lines
def create_episode(self, cmd): series, season, episode = cmd.args data = dict(episode=episode) path = 'series/{}/seasons/{}/episodes'.format(series, season) return IO.delay(self.client.post, path, body=data)
def virtualenv_interpreter(venv: str, executable: str) -> Do: path_s = yield IO.from_either(env.get('PATH')) path = Lists.split(path_s, ':') clean_path = path.filter_not(lambda a: a.startswith(venv)) candidate = yield IO.delay(shutil.which, executable, path=clean_path.mk_string(':')) return Path(candidate)
def pane_dir(pane: P) -> Do: ui_pane = yield TmuxIO.from_either(UiPane.e_for(pane)) yield (ui_pane.cwd(pane) / TmuxIO.pure).get_or(lambda: TmuxIO.from_io(IO.delay(Path.cwd)))
def clear_cache() -> Do: yield IO.delay(shutil.rmtree, str(venvs_path), ignore_errors=True) yield create_dir(venvs_path)
def delay(f: Callable[..., A], *a: Any, **kw: Any) -> 'IOState[S, A]': return IOState.lift(IO.delay(f, *a, **kw))
def io(i, body): return IO.delay(meth(self.client), '{}/{}{}'.format(self._type, i, sub), body=body)
def which(name: str) -> Do: exe = yield IO.delay(shutil.which, name) return Path(exe)
def to_io(fa: 'TmuxIO[A]', tmux: Tmux) -> Do: result = yield IO.delay(fa.run, tmux) yield IO.from_either(result.to_either)
def mkdir(path: Path) -> IO[None]: return IO.delay(path.mkdir, parents=True, exist_ok=True)
def subfps(self, cmd): series, season, episode, subfps = cmd.args data = dict(subfps=subfps) path = 'series/{}/seasons/{}/episodes/{}'.format( series, season, episode) return IO.delay(self.client.put, path, body=data)
def remove_dir(dir: Path) -> Do: exists = yield IO.delay(dir.exists) yield IO.delay(shutil.rmtree, dir) if exists else IO.pure(None)
def codegen_write(task: CodegenTask, outpath: Path) -> Do: code = yield codegen(task) yield IO.delay(outpath.write_text, code) return code
def cleanup(a: Any) -> Do: yield IO.delay(tmux_proc.kill).recover(const(None)) yield IO.delay(tmux_proc.wait).recover(const(None)) yield IO.delay(kill_server().result, tmux).recover(const(None))