1+ #TODO find appropiate names
2+ from . import __version__
3+ import click
4+ import os
5+ import logging
6+ CONTEXT_SETTINGS = dict (help_option_names = ['-h' , '--help' ])
7+ VALID_EXTENSIONS = ['.txt' , '.json' ]
8+
9+
10+ @click .group (context_settings = CONTEXT_SETTINGS )
11+ @click .version_option (__version__ )
12+ def cli ():
13+ """
14+ ███████ ███████ ██ ██ ██████ \n
15+ ██ ██ ██ ██ ██ \n
16+ ███████ ███████ █████ ██ ███ \n
17+ ██ ██ ██ ██ ██ ██ \n
18+ ███████ ███████ ██ ██ ██████ \n
19+
20+ Scientific Software Knowledge Graphs (SSKG)\n
21+ Find and assess Research Software within Research papers.\n
22+
23+ Usage:\n
24+ 1. (assess) Assess doi for unidirectionality or bidirectionality\n
25+ 2. (download) Download PDF (paper) from a doi or list\n
26+ 3. (process) Process downloaded pdf to find urls and abstract\n
27+
28+ """
29+ pass
30+
31+ # #TODO
32+ # @cli.command()
33+ # def configure():
34+ # """This creates a ~/.soca/configure.ini file"""
35+ # #TODO defaults check
36+ # url = click.prompt("URL to database",default = "http://localhost:8086")
37+ # bucket = click.prompt("Bucket", default = "my-bucket")
38+ # org = click.prompt("Organisation",default = "org_name")
39+ # token = click.prompt("Token", default = "")
40+ # if len(token) == 0:
41+ # click.echo("No token given, please enter token or press enter")
42+ # token = click.prompt("Token", default = "")
43+ # try:
44+ # from soca.commands import create_config
45+ #
46+ # create_config.create_config(url,bucket,token,org)
47+ # click.secho(f"Success", fg="green")
48+ # except Exception as e:
49+ # click.secho(f"Error: "+str(e),fg="red")
50+ # exit(1)
51+
52+ @cli .command ()
53+ @click .option ('--input' ,'-i' , required = True , help = "DOI or path to .txt list of DOIs" , metavar = '<name>' )
54+ @click .option ('--output' ,'-o' , default = "output" , show_default = True , help = "Output csv file" , metavar = '<path>' )
55+ @click .option ('--unidir' , '-U' , is_flag = True , default = False , help = "Unidirectionality" )
56+ @click .option ('--bidir' , '-B' , is_flag = True , default = False , help = "Bidirectionality" )
57+ def assess (input , output ,unidir ,bidir ):
58+ from .object_creator .pipeline import dois_txt_to_unidir_json , dois_txt_to_bidir_json , single_doi_pipeline_unidir , \
59+ single_doi_pipeline_bidir , papers_json_to_unidir_json , papers_json_to_bidir_json
60+ if unidir :
61+ if input .endswith (".txt" ) and os .path .exists (input ):
62+ dois_txt_to_unidir_json (dois_txt = input ,output_dir = output )
63+ if input .endswith (".json" ) and os .path .exists (input ):
64+ papers_json_to_unidir_json (papers_json = input , output_dir = output )
65+ return
66+ else :
67+ single_doi_pipeline_unidir (doi = input ,output_dir = output )
68+ return
69+
70+ elif bidir :
71+ if input .endswith (".txt" ) and os .path .exists (input ):
72+ dois_txt_to_bidir_json (dois_txt = input ,output_dir = output )
73+ if input .endswith (".json" ) and os .path .exists (input ):
74+ papers_json_to_bidir_json (papers_json = input , output_dir = output )
75+ else :
76+ single_doi_pipeline_bidir (doi = input , output_dir = output )
77+ return
78+ else :
79+ print ("Please select a directionality to measure" )
80+ print ("-U is to assess Uni-directionality" )
81+ print ("-B is to assess Bi-directionality" )
82+ pass
83+
84+
85+
86+ @cli .command ()
87+ @click .option ('--input' ,'-i' , required = True , help = "DOI or path to .txt list of DOIs" , metavar = '<name>' )
88+ @click .option ('--output' ,'-o' , default = "./" , show_default = True , help = "Output Directory " , metavar = '<path>' )
89+ def download (input , output ):
90+ from .object_creator .create_downloadedObj import doi_to_downloadedJson , dois_txt_to_downloadedJson
91+ if input .endswith (".txt" ) and os .path .exists (input ):
92+ dois_txt_to_downloadedJson (dois_txt = input , output_dir = output )
93+ else :
94+ try :
95+ doi_to_downloadedJson (doi = input , output_dir = output )
96+ except Exception as e :
97+ print (e )
98+ return
99+ @cli .command ()
100+ @click .option ('--input' ,'-i' , required = True , help = "DOI or path to .txt list of DOIs" , metavar = '<name>' )
101+ @click .option ('--output' ,'-o' , default = "./" , show_default = True , help = "Output Directory " , metavar = '<path>' )
102+ def process (input ,output ):
103+ from .object_creator .downloaded_to_paperObj import dwnlddJson_to_paperJson , dwnldd_obj_to_paper_json
104+ from .object_creator .create_downloadedObj import pdf_to_downloaded_obj
105+
106+ if os .path .isdir (input ):
107+ _aux_pdfs_to_pp_json (input = input , output = output )
108+ return
109+ if input .endswith (".json" ) and os .path .exists (input ):
110+ dwnlddJson_to_paperJson (input ,output )
111+ if input .endswith (".pdf" ) and os .path .exists (input ):
112+ #TODO
113+ dwnldd = pdf_to_downloaded_obj (pdf = input , output_dir = output )
114+ dwnldd_obj_to_paper_json (download_obj = dwnldd ,output_dir = output )
115+ return
116+ else :
117+ print ("Error" )
118+ return
119+
120+ def _aux_pdfs_to_pp_json (input , output ):
121+ from .object_creator .create_downloadedObj import pdf_to_downloaded_obj
122+ from .object_creator .downloaded_to_paperObj import dwnldd_obj_to_paper_dic
123+ import json
124+ try :
125+ result = {}
126+ for pdfFile in os .listdir (input ):
127+ print (pdfFile )
128+ try :
129+ if os .path .isfile (pdfFile ) and pdfFile .endswith (".pdf" ):
130+ dwnldd = pdf_to_downloaded_obj (pdf = pdfFile , output_dir = output )
131+ pp_dic = dwnldd_obj_to_paper_dic (downloaded_obj = dwnldd )
132+ try :
133+ result .update (pp_dic )
134+ except Exception as update_error :
135+ logging .error (f"Error updating result with pp_dic: { str (update_error )} " )
136+ continue
137+ print (pp_dic )
138+ print (pdfFile )
139+ except Exception as file_error :
140+ logging .error (f"Error processing file: { str (file_error )} " )
141+ continue
142+ output_path = output + "/" + "processed_metadata.json"
143+ with open (output_path , 'w+' ) as out_file :
144+ json .dump (result , out_file , sort_keys = True , indent = 4 ,
145+ ensure_ascii = False )
146+ return output_path
147+ except Exception as e :
148+ logging .error (f"an error occurred: { str (e )} " )
149+ print (str (e ))
0 commit comments