11import json
2- import os , sys
2+ import os
3+ import sys
34from pathlib import Path
45
56import click
6- from rich .console import Console
77import git
8- from datapackage import Package
98import inquirer
9+ from datapackage import Package
1010from loguru import logger
1111from mkdocs .commands .serve import serve as _serve
12+ from rich .console import Console
1213
1314from dataherb .cmd .create import describe_dataset
15+ from dataherb .cmd .search import HerbTable
1416from dataherb .cmd .sync_git import upload_dataset_to_git
1517from dataherb .cmd .sync_s3 import upload_dataset_to_s3
1618from dataherb .core .base import Herb
@@ -39,8 +41,15 @@ def dataherb(ctx):
3941
4042
4143@dataherb .command ()
42- @click .option ('--show/--no-show' , '-s/ ' , default = False , help = "Show the current configuration" )
43- @click .option ('--locate/--no-locate' , '-l/ ' , default = False , help = "Locate the folder that contains the configuration" )
44+ @click .option (
45+ "--show/--no-show" , "-s/ " , default = False , help = "Show the current configuration"
46+ )
47+ @click .option (
48+ "--locate/--no-locate" ,
49+ "-l/ " ,
50+ default = False ,
51+ help = "Locate the folder that contains the configuration" ,
52+ )
4453def configure (show , locate ):
4554 """
4655 Configure dataherb; inspect, or lcoate the current configurations.
@@ -95,36 +104,42 @@ def configure(show, locate):
95104 with open (config_path , "w" ) as f :
96105 json .dump (config , f , indent = 4 )
97106
98- click .secho (
99- f"The dataherb config has been saved to { config_path } !" , fg = "green"
100- )
107+ click .secho (f"The dataherb config has been saved to { config_path } !" , fg = "green" )
101108 else :
102109 if not config_path .exists ():
103110 click .secho (f"Config file ({ config_path } ) doesn't exist." , fg = "red" )
104111 else :
105112 c = Config ()
106113 click .secho (f"The current config for dataherb is:" )
107- click .secho (json .dumps (c .config , indent = 2 , sort_keys = True , ensure_ascii = False ))
114+ click .secho (
115+ json .dumps (c .config , indent = 2 , sort_keys = True , ensure_ascii = False )
116+ )
108117 click .secho (f"The above config is extracted from { config_path } " )
109118
110119 if locate :
111120 click .launch (str (config_path .parent ))
112121
113122
114-
115123@dataherb .command ()
116- @click .argument ("keywords" , required = False )
124+ @click .option (
125+ "--flora" ,
126+ "-f" ,
127+ default = None ,
128+ help = "Path to the flora file; Defaults to the default flora in the configuration." ,
129+ )
117130@click .option ("--id" , "-i" , required = False , help = "The id of the dataset to describe." )
118- @click .option ("--flora" , "-f" , default = None , help = "Path to the flora file; Defaults to the default flora in the configuration." )
119- def search (flora , id = None , keywords = None ):
131+ @click .argument ("keywords" , required = False )
132+ @click .option (
133+ "--full/--summary" , default = False , help = "Whether to show the full json result"
134+ )
135+ def search (flora , id , keywords , full ):
120136 """
121137 search datasets on DataHerb by keywords or id
122138 """
123139 if flora is None :
124140 c = Config ()
125141 flora = c .flora_path
126142
127- SHOW_KEYS = ["name" , "description" , "contributors" ]
128143 fl = Flora (flora = flora )
129144 if not id :
130145 click .echo ("Searching Herbs in DataHerb Flora ..." )
@@ -134,25 +149,63 @@ def search(flora, id=None, keywords=None):
134149 click .echo (f"Could not find dataset related to { keywords } " )
135150 else :
136151 for result in results :
152+ result_herb = result .get ("herb" )
137153 result_metadata = result .get ("herb" ).metadata
138- click .echo (f'DataHerb ID: { result_metadata .get ("id" )} ' )
139- click .echo (result_metadata )
154+ if not full :
155+ ht = HerbTable (result_herb )
156+ console .rule (title = f"{ result_herb .id } " , characters = "||" )
157+ console .print (ht .table ())
158+ console .print (ht .resource_tree ())
159+ else :
160+ console .rule (title = f"{ result_herb .id } " , characters = "||" )
161+ click .secho (f"DataHerb ID: { result_herb .id } " )
162+ click .echo (json .dumps (result_metadata , indent = 2 , sort_keys = True ))
140163 else :
141164 click .echo (f"Fetching Herbs { id } in DataHerb Flora ..." )
142165 result = fl .herb (id )
143166 if not result :
144167 click .echo (f"Could not find dataset with id { id } " )
145168 else :
146169 result_metadata = result .metadata
147- click .echo (f'DataHerb ID: { result_metadata .get ("id" )} ' )
148- click .echo (result_metadata )
170+
171+ if not full :
172+ ht = HerbTable (result )
173+ console .rule (title = f"{ result .id } " , characters = "||" )
174+ console .print (ht .table ())
175+ console .print (ht .resource_tree ())
176+ else :
177+ console .rule (title = f"{ result .id } " , characters = "||" )
178+ click .secho (f"DataHerb ID: { result .id } " )
179+ click .echo (json .dumps (result_metadata , indent = 2 , sort_keys = True ))
149180
150181
151182@dataherb .command ()
152- @click .option ("--flora" , "-f" , default = None , help = "Specify the path to the flora; defaults to default flora in configuration." )
153- @click .option ("--workdir" , "-w" , default = None , help = "Specify the path to the work directory; defaults to the workdir in configuration." )
154- @click .option ("--dev_addr" , "-a" , default = "localhost:52125" , metavar = "<IP:PORT>" , help = "Specify the address of the dev server; defaults to localhost:52125" )
155- @click .option ("--recreate" , "-r" , default = False , required = False , help = "Whether to recreate the website. Recreation will delete all the current generated pages and rebuild the whole website." )
183+ @click .option (
184+ "--flora" ,
185+ "-f" ,
186+ default = None ,
187+ help = "Specify the path to the flora; defaults to default flora in configuration." ,
188+ )
189+ @click .option (
190+ "--workdir" ,
191+ "-w" ,
192+ default = None ,
193+ help = "Specify the path to the work directory; defaults to the workdir in configuration." ,
194+ )
195+ @click .option (
196+ "--dev_addr" ,
197+ "-a" ,
198+ default = "localhost:52125" ,
199+ metavar = "<IP:PORT>" ,
200+ help = "Specify the address of the dev server; defaults to localhost:52125" ,
201+ )
202+ @click .option (
203+ "--recreate" ,
204+ "-r" ,
205+ default = False ,
206+ required = False ,
207+ help = "Whether to recreate the website. Recreation will delete all the current generated pages and rebuild the whole website." ,
208+ )
156209def serve (flora , workdir , dev_addr , recreate ):
157210 """
158211 create a dataherb server and view the flora in your browser
@@ -179,8 +232,18 @@ def serve(flora, workdir, dev_addr, recreate):
179232
180233@dataherb .command ()
181234@click .argument ("id" , required = True )
182- @click .option ("--flora" , "-f" , default = None , help = "Specify the path to the flora; defaults to default flora in configuration." )
183- @click .option ("--workdir" , "-w" , default = None , help = "Specify the path to the work directory; defaults to the workdir in configuration." )
235+ @click .option (
236+ "--flora" ,
237+ "-f" ,
238+ default = None ,
239+ help = "Specify the path to the flora; defaults to default flora in configuration." ,
240+ )
241+ @click .option (
242+ "--workdir" ,
243+ "-w" ,
244+ default = None ,
245+ help = "Specify the path to the work directory; defaults to the workdir in configuration." ,
246+ )
184247def download (id , flora , workdir ):
185248 """
186249 Download dataset using id
@@ -229,7 +292,12 @@ def download(id, flora, workdir):
229292 "A dataherb.json file will be created right here.\n "
230293 "Are you sure this is the correct path?"
231294)
232- @click .option ("--flora" , "-f" , default = None , help = "Specify the path to the flora; defaults to default flora in configuration." )
295+ @click .option (
296+ "--flora" ,
297+ "-f" ,
298+ default = None ,
299+ help = "Specify the path to the flora; defaults to default flora in configuration." ,
300+ )
233301def create (flora ):
234302 """
235303 creates metadata for current dataset
@@ -266,7 +334,9 @@ def create(flora):
266334 md .metadata .update (pkg_descriptor )
267335
268336 if (Path (__CWD__ ) / "dataherb.json" ).exists ():
269- is_overwrite = click .confirm ("Replace the current dataherb.json file?" , default = False )
337+ is_overwrite = click .confirm (
338+ "Replace the current dataherb.json file?" , default = False
339+ )
270340 if is_overwrite :
271341 md .create (overwrite = is_overwrite )
272342
@@ -292,7 +362,12 @@ def create(flora):
292362
293363
294364@dataherb .command ()
295- @click .option ("--flora" , "-f" , default = None , help = "Specify the path to the flora; defaults to default flora in configuration." )
365+ @click .option (
366+ "--flora" ,
367+ "-f" ,
368+ default = None ,
369+ help = "Specify the path to the flora; defaults to default flora in configuration." ,
370+ )
296371@click .argument ("herb_id" , required = True )
297372def remove (flora , herb_id ):
298373 """
0 commit comments