Skip to content

Commit

Permalink
adding more to the CLI. Ready to test live
Browse files Browse the repository at this point in the history
  • Loading branch information
pweids committed Jun 19, 2019
1 parent 7e25a66 commit cd51aaa
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 12 deletions.
8 changes: 6 additions & 2 deletions cairo/cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class CairoException(BaseException):
""" Cairo specific exceptions """
pass


# Query

def ft_at_time(node: FileObject, dt: datetime) -> None:
Expand Down Expand Up @@ -107,12 +108,13 @@ def search_all(root: FileObject, query: str) \

def search_file(root: FileObject, fp: Path, query: str) \
-> Set[Tuple[Path, Optional[Version]]]:
if isinstance(fp, str): fp = Path(fp)
all_vs = search_all(root, query)
return set(filter(lambda v: v[0] == fp, all_vs))


def get_versions(root: FileObject) -> List[Version]:
""" Returns all of the versions in the FileObject """
""" Returns all of the versions recursively in the FileObject """
def go(ft, vs):
vs = vs.union(set([m.version for m in ft.mods]))
for c in _rc(ft):
Expand Down Expand Up @@ -365,6 +367,7 @@ def _rd(ft: FileObject, dt: datetime = None):
"""
return resolve(ft, dt).data


def _rfp(ft: FileObject, dt: datetime = None) -> Path:
return resolve(ft, dt).path

Expand Down Expand Up @@ -406,7 +409,7 @@ def _mk_ver() -> Version:
return Version(uuid1(), datetime.now())


def _fp_in_tree(root: FileObject, fp: Path) -> bool:
def _is_fp_in_tree(root: FileObject, fp: Path) -> bool:
return find_file_path(root, fp) is not None


Expand Down Expand Up @@ -457,6 +460,7 @@ def _write_data(fp: Path, data):
else:
fp.write_bytes(data)


def _rm_f_or_d(fp):
if fp.is_dir():
rmtree(fp)
Expand Down
42 changes: 33 additions & 9 deletions cairo/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import click
from . import mock_cairo as cairo
from dateutil.parser import parse
from datetime import datetime
from time import sleep


Expand All @@ -12,7 +13,8 @@
@click.pass_context
def cli(ctx, path):
""" step through the gate """
pass
ctx.ensure_object(dict)
ctx.obj['PATH'] = path


@cli.command()
Expand All @@ -38,6 +40,8 @@ def status(ctx):
""" peek through the gates, seeing what has changed
"""
root = _ensure_init(ctx)
cf = cairo.changed_files(root)
if cf: _pretty_print_changes(cf)


@cli.command()
Expand All @@ -61,22 +65,42 @@ def commit(ctx):
@click.argument('date')
@click.pass_context
def gate(ctx, date):
""" visit your files at another time """
""" visit your files at another time. type 'now' to return to the present """
root = _ensure_init(ctx)
try:
date = parse(date)
click.secho(f'transporting you to {date.strftime("%I:%M:%S %p on %A, %B %d, %Y")}', fg='bright_magenta')
cairo.ft_at_time(root)
except ValueError:
click.secho(f'i do not understand the time "{date}"', fg='bright_red')
if date == "now":
click.secho(f'transporting you to the present', fg='bright_magenta')
cairo.ft_at_time(root, datetime.now())
else:
try:
date = parse(date)
click.secho(f'transporting you to {date.strftime("%I:%M:%S %p on %A, %B %d, %Y")}', fg='bright_magenta')
cairo.ft_at_time(root, date)
except ValueError:
click.secho(f'i do not understand the time "{date}"', fg='bright_red')


@cli.command()
@click.pass_context
def hist(ctx):
""" display the full timeline """
root = _ensure_init(ctx)
pass
vs = cairo.get_versions(root)
for v in vs:
print(f"version {v.verid}\t{v.time.strftime('%I:%M:%S %p on %m-%d-%Y')}")


@cli.command()
@click.argument('query')
@click.option('-f', '--file', default=None, type=str)
@click.pass_context
def find(ctx, query, file):
""" find all versions where your query is present """
root = _ensure_init(ctx)
if file:
cairo.search_file(root, file)
else:
cairo.search_all(root)



# helpers
Expand Down
22 changes: 21 additions & 1 deletion cairo/mock_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
from . import cairo
from random import randint
from datetime import datetime

def init(fp):
fp = fp or Path()
Expand All @@ -16,4 +17,23 @@ def commit(*args, **kwargs):
def changed_files(*args, **kwargs):
x = randint(0,10)
if x < 3:
return [('.', 'mod'), ('./cairo', 'new'), ('./tmp/cairo', 'rmv')]
return [('.', 'mod'), ('./cairo', 'new'), ('./tmp/cairo', 'rmv')]

def search_file(*args, **kwargs):
print("calling search_file")
pass


def search_all(*args, **kwargs):
print("calling search_all")
pass


def ft_at_time(_, tiem):
print(f"traveling to {tiem}")


def get_versions(*args, **kwargs):
V = cairo.Version
vs = [V(i, datetime(2019, 5, i+1, 12, 0)) for i in range(20)]
return vs

0 comments on commit cd51aaa

Please sign in to comment.