Skip to content

Commit

Permalink
using relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
xtianpoli committed Feb 8, 2024
1 parent bc22bef commit 611d7ae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
36 changes: 31 additions & 5 deletions ipyprogressivis/nbwidgets/stage_widgets/csv_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@

from typing import List, Optional, Any, Dict, Callable, cast

HOME = os.getenv("HOME")
assert HOME is not None


def _expand_url(url: str) -> str:
if url[0] == "~":
return f"{HOME}/{url[1:]}"
return url


def expand_urls(urls: list[str]) -> list[str]:
return [_expand_url(url) for url in urls if url]


def _relative_url(url: str) -> str:
assert HOME is not None
if url.startswith(HOME):
return url.replace(HOME, "~", 1)
return url


def relative_urls(urls: list[str]) -> list[str]:
return [_relative_url(url) for url in urls if url]


def clean_nodefault(d: Dict[str, Any]) -> Dict[str, Any]:
return {k: v for (k, v) in d.items() if type(v).__name__ != "_NoDefault"}
Expand Down Expand Up @@ -106,7 +130,7 @@ def init(self, urls: List[str] = [], to_sniff: str = "", lines: int = 100) -> No
self.c_.reuse_ck.observe(self._reuse_cb, names="value")
else:
self.c_.reuse_ck = None
home = os.getenv("HOME")
home = HOME
bmk_disabled = True
bookmarks = [
f"no '{home}/.progressivis/' directory found",
Expand Down Expand Up @@ -250,6 +274,7 @@ def _sniffer_cb(self, btn: ipw.Button) -> None:
to_sniff = self.c_.to_sniff.value.strip()
if not to_sniff:
to_sniff = urls[0]
to_sniff = _expand_url(to_sniff)
n_lines = self.c_.n_lines.value
self._sniffer = CSVSniffer(path=to_sniff, lines=n_lines)
self.c_.sniffer = self._sniffer.box
Expand Down Expand Up @@ -293,7 +318,7 @@ def _sniffer_cb(self, btn: ipw.Button) -> None:
btn.description = "Hide sniffer"

def _start_loader_cb(self, btn: ipw.Button) -> None:
urls = self._urls
urls = relative_urls(self._urls)
assert self._sniffer is not None
pv_params = self._sniffer.progressivis
filter_ = pv_params.get("filter_values", {})
Expand All @@ -314,7 +339,7 @@ def _start_loader_cb(self, btn: ipw.Button) -> None:

@property
def dot_progressivis(self) -> str:
home = os.getenv("HOME")
home = HOME
pv_dir = f"{home}/.progressivis/"
if os.path.isdir(pv_dir):
return pv_dir
Expand Down Expand Up @@ -347,7 +372,7 @@ def _save_settings_cb(self, btn: ipw.Button) -> None:
schema = get_schema(self._sniffer)
filter_ = pv_params.get("filter_values", {})
res = dict(
urls=self._urls,
urls=relative_urls(self._urls),
throttle=self.c_.throttle.value,
sniffed_params=clean_nodefault(self._sniffer.params),
schema=schema,
Expand Down Expand Up @@ -380,14 +405,15 @@ def init_modules(
) -> SimpleCSVLoader:
if urls is None:
assert self._sniffer is not None
urls = self._urls
urls = expand_urls(self._urls)
params = self._sniffer.params.copy()
pv_params = self._sniffer.progressivis
if "filter_values" in pv_params:
filter_fnc = make_filter(pv_params["filter_values"])
params["filter_"] = filter_fnc
params["throttle"] = self.c_.throttle.value
else:
urls = expand_urls(urls)
if filter_:
filter_ = dict(filter_=make_filter(filter_))
else:
Expand Down
5 changes: 4 additions & 1 deletion ipyprogressivis/nbwidgets/stage_widgets/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def b642json(b64str: str) -> AnyType:


def bpack(bak: List[AnyType]) -> str:
# return json.dumps(bak)
return ";".join([json2b64(elt) for elt in bak])


Expand All @@ -122,6 +121,10 @@ def bunpack(bstr: str) -> List[AnyType]:
return bstr.split(";")


def backup_to_json() -> AnyType:
return [b642json(step) for step in PARAMS["header"].backup.value.split(";")]


class Recorder:
def __init__(self, value: str = "") -> None:
self.tape = value
Expand Down

0 comments on commit 611d7ae

Please sign in to comment.