@@ -44,9 +44,14 @@ def configure():
4444 config_path = home / ".dataherb/config.json"
4545
4646 if config_path .exists ():
47- is_overwite = click .confirm (f"Config file ({ config_path } ) already exists. Overwrite?" , default = False )
47+ is_overwite = click .confirm (
48+ click .style (
49+ f"Config file ({ config_path } ) already exists. Overwrite?" , fg = "red"
50+ ),
51+ default = False ,
52+ )
4853 if is_overwite :
49- click .echo ("Overwriting config file..." )
54+ click .echo (click . style ( "Overwriting config file..." , fg = "red" ) )
5055 else :
5156 click .echo ("Skipping..." )
5257 sys .exit (0 )
@@ -62,35 +67,36 @@ def configure():
6267 "workdir" ,
6368 message = "Where should I put all the datasets and flora database? An empty folder is recommended." ,
6469 # path_type=inquirer.Path.DIRECTORY,
65- normalize_to_absolute_path = True
70+ normalize_to_absolute_path = True ,
6671 ),
6772 inquirer .Text (
6873 "default_flora" ,
6974 message = "How would you name the default flora? Please keep the default value if this is not clear to you." ,
70- default = "flora"
71- )
75+ default = "flora" ,
76+ ),
7277 ]
7378
7479 answers = inquirer .prompt (questions )
7580
7681 config = {
7782 "workdir" : answers .get ("workdir" ),
78- "default" : {
79- "flora" : answers .get ("default_flora" )
80- }
83+ "default" : {"flora" : answers .get ("default_flora" )},
8184 }
8285
8386 logger .debug (f"config: { config } " )
8487
8588 with open (config_path , "w" ) as f :
8689 json .dump (config , f , indent = 4 )
8790
88- click .echo (f"The dataherb config has been saved to { config_path } !" )
89-
91+ click .echo (
92+ click .style (f"The dataherb config has been saved to { config_path } !" , fg = "green" )
93+ )
9094
9195
92- CONFIG = load_dataherb_config ()
96+ CONFIG = load_dataherb_config (no_config_error = False )
9397logger .debug (CONFIG )
98+ if not CONFIG :
99+ click .echo ("No config file found. Please run 'dataherb configure' to create one." )
94100WD = CONFIG .get ("workdir" , "." )
95101which_flora = CONFIG .get ("default" , {}).get ("flora" )
96102if which_flora :
@@ -100,8 +106,6 @@ def configure():
100106 raise Exception (f"flora config { which_flora } does not exist" )
101107
102108
103-
104-
105109@dataherb .command ()
106110@click .argument ("keywords" , required = False )
107111@click .option ("--id" , "-i" , required = False )
@@ -138,21 +142,24 @@ def search(flora, id=None, keywords=None):
138142@click .option ("--flora" , "-f" , default = which_flora )
139143@click .option ("--workdir" , "-w" , default = WD , required = True )
140144@click .option ("--dev_addr" , "-a" , metavar = "<IP:PORT>" )
141- def serve (flora , workdir , dev_addr ):
145+ @click .option ("--recreate" , "-r" , default = False , required = False )
146+ def serve (flora , workdir , dev_addr , recreate ):
142147 fl = Flora (flora = flora )
143148 mk = SaveMkDocs (flora = fl , workdir = workdir )
144- mk .save_all ()
149+ mk .save_all (recreate = recreate )
145150
146- mkdocs_config = str (Path (WD ) / "serve" / "mkdocs.yml" )
151+ mkdocs_config = str (Path (workdir ) / "serve" / "mkdocs.yml" )
147152
148153 click .echo ("Open http://localhost:8000" )
154+ click .launch ("http://localhost:8000" )
149155 _serve (config_file = mkdocs_config , dev_addr = dev_addr )
150156
151157
152158@dataherb .command ()
153159@click .argument ("id" , required = True )
154160@click .option ("--flora" , "-f" , default = which_flora )
155- def download (id , flora ):
161+ @click .option ("--workdir" , "-w" , default = WD , required = True )
162+ def download (id , flora , workdir ):
156163 """
157164 download dataset using id
158165 """
@@ -167,7 +174,7 @@ def download(id, flora):
167174 click .echo (f'Downloading DataHerb ID: { result_metadata .get ("id" )} ' )
168175 result_uri = result_metadata .get ("uri" )
169176 result_id = result_metadata .get ("id" )
170- dest_folder = str (Path (WD ) / result_id )
177+ dest_folder = str (Path (workdir ) / result_id )
171178 if os .path .exists (dest_folder ):
172179 click .echo (f"Can not download dataset to { dest_folder } : folder exists.\n " )
173180
@@ -225,18 +232,53 @@ def create(flora):
225232
226233 md .metadata .update (pkg_descriptor )
227234
228- md .create ()
229-
230- click .echo (
231- "The dataherb.json file has been created inside \n "
232- f"{ __CWD__ } \n "
233- "Please review the dataherb.json file and update other necessary fields."
234- )
235+ if (Path (__CWD__ ) / "dataherb.json" ).exists ():
236+ is_overwrite = click .confirm ("Replace the current dataherb.json file?" , default = False )
237+ if is_overwrite :
238+ md .create (overwrite = is_overwrite )
235239
236- hb = Herb (md .metadata )
240+ click .echo (
241+ f"The dataherb.json file in folder { __CWD__ } has been replaced. \n "
242+ "Please review the dataherb.json file and update other necessary fields."
243+ )
244+ else :
245+ click .echo ("We did nothing." )
246+ sys .exit ()
247+ else :
248+ md .create ()
249+ click .echo (
250+ "The dataherb.json file has been created inside \n "
251+ f"{ __CWD__ } \n "
252+ "Please review the dataherb.json file and update other necessary fields."
253+ )
254+
255+ hb = Herb (md .metadata , with_resources = False )
237256 fl .add (hb )
238257
239- click .echo (f"Added { hb .metadata ['id' ]} into the flora." )
258+ click .echo (f"Added { hb .id } into the flora." )
259+
260+
261+ @dataherb .command ()
262+ @click .option ("--flora" , "-f" , default = which_flora )
263+ @click .argument ("herb_id" , required = True )
264+ def remove (flora , herb_id ):
265+ """
266+ remove herb from flora
267+ """
268+
269+ fl = Flora (flora = flora )
270+ herb = fl .herb (herb_id )
271+ if not herb :
272+ click .echo (click .style (f"Could not find herb with id { herb_id } " , fg = "red" ))
273+ click .echo ("We did nothing." )
274+ sys .exit ()
275+
276+ to_remove = click .confirm (f"Remove { herb_id } from the flora?" , default = False )
277+ if to_remove :
278+ fl .remove (herb_id )
279+ click .echo (f"Removed { herb_id } into the flora." )
280+ else :
281+ click .echo ("We did nothing." )
240282
241283
242284@dataherb .command ()
@@ -329,10 +371,6 @@ def echo_summary(key, value_dict, bg=None, fg=None):
329371 )
330372
331373
332-
333-
334-
335-
336374if __name__ == "__main__" :
337375 fl = Flora ()
338376 pass
0 commit comments