Skip to content

Commit 517a220

Browse files
committed
using folder for each herb
1 parent 87b65c1 commit 517a220

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

dataherb/command.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,24 @@ def configure(show, locate):
9696

9797
config = {
9898
"workdir": answers.get("workdir"),
99-
"default": {"flora": answers.get("default_flora")},
99+
"default": {
100+
"flora": answers.get("default_flora"),
101+
"aggregrated": False # if false, we will use folders for each herb.
102+
},
100103
}
101104

105+
flora_path_workdir = answers.get("workdir", "")
106+
if flora_path_workdir.startswith("~"):
107+
home = Path.home()
108+
flora_path_workdir = str(home / flora_path_workdir[2:])
109+
110+
flora_path = Path(flora_path_workdir) / "flora" / f"{answers.get('default_flora')}"
111+
if not flora_path.exists():
112+
click.secho(f"{flora_path} doesn't exist. Creating {flora_path}...", fg="red")
113+
flora_path.mkdir(parents=True)
114+
else:
115+
click.secho(f"{flora_path} exists, using the folder directly.", fg="green")
116+
102117
logger.debug(f"config: {config}")
103118

104119
with open(config_path, "w") as f:
@@ -387,7 +402,7 @@ def remove(flora, herb_id):
387402
to_remove = click.confirm(f"Remove {herb_id} from the flora?", default=False)
388403
if to_remove:
389404
fl.remove(herb_id)
390-
click.echo(f"Removed {herb_id} into the flora.")
405+
click.echo(f"Removed {herb_id} from the flora.")
391406
else:
392407
click.echo("We did nothing.")
393408

dataherb/flora.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ def add(self, herb):
9999
raise Exception(f"herb id = {herb.id} already exists")
100100

101101
self.flora.append(herb)
102-
self.save()
102+
if self.is_aggregated:
103+
self.save()
104+
else:
105+
self.save(herb=herb)
103106

104107
def remove(self, herb_id):
105108
"""
@@ -116,12 +119,15 @@ def remove(self, herb_id):
116119
else:
117120
self.remove_herb_from_flora(herb_id)
118121

119-
def save(self, path=None, id=None):
122+
def save(self, path=None, id=None, herb=None):
120123
"""save flora metadata to json file"""
121124

122125
if path is None:
123126
path = self.flora_config
124127

128+
if isinstance(path, str):
129+
path = Path(path)
130+
125131
logger.debug(
126132
f"type of a herb in flora: {type(self.flora[0])}\n{self.flora[0].metadata}"
127133
)
@@ -137,8 +143,15 @@ def save(self, path=None, id=None):
137143
serialized_flora, fp, sort_keys=True, indent=4, separators=(",", ": ")
138144
)
139145
else:
140-
if not id:
146+
if (not id) and (not herb):
141147
raise Exception("dataherb id must be provided")
148+
elif herb:
149+
logger.debug(f"Saving herb using herb object")
150+
self.save_herb_meta(herb.id, path / f"{herb.id}")
151+
elif id:
152+
logger.debug(f"Saving herb using herb id")
153+
self.save_herb_meta(id, path / f"{id}")
154+
142155

143156
def save_herb_meta(self, id, path=None):
144157
"""Save a herb metadata to json file"""

dataherb/utils/configs.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
class Config:
1111
"""Config system for Dataherb"""
1212

13-
def __init__(self, config_path=None, no_config_error=False):
13+
def __init__(self, is_aggregated=None, config_path=None, no_config_error=False):
14+
15+
if is_aggregated is None:
16+
is_aggregated = False
17+
18+
self.is_aggregated = is_aggregated
1419

1520
self.config_path = config_path
1621
self.no_config_error = no_config_error
@@ -33,7 +38,10 @@ def _flora_path(self, flora, workdir=None):
3338
if workdir is None:
3439
workdir = self.config["WD"]
3540

36-
which_flora_path = Path(workdir) / "flora" / Path(flora + ".json")
41+
if self.is_aggregated:
42+
which_flora_path = Path(workdir) / "flora" / Path(flora + ".json")
43+
else:
44+
which_flora_path = Path(workdir) / "flora" / Path(flora)
3745
logger.debug(f"Using flora path: {which_flora_path}")
3846
if not which_flora_path.exists():
3947
raise Exception(f"flora config {which_flora_path} does not exist")

0 commit comments

Comments
 (0)