33import sys
44from functools import partial
55
6- from NAF import NAF_TO_DNI , validate_parse_naf
6+ from NAF import is_naf_present , NAF , build_naf_to_dni
77from custom_except import *
88from defines import NAF_DATA_PATH , DocType , from_string
99
@@ -12,6 +12,9 @@ def get_compact_init():
1212 return {DocType .SALARY : False , DocType .PROOFS : False , DocType .CONTRACT : False , DocType .RNT : False , DocType .RLC : False }
1313
1414
15+ # Parser functions that validate the format and type of the data
16+
17+
1518def parse_date (value , formatting = "%Y_%m" ):
1619 """Validate date format"""
1720 try :
@@ -22,21 +25,17 @@ def parse_date(value, formatting="%Y_%m"):
2225
2326
2427def parse_author (author ):
25- with open ("input/users.txt" , newline = "" , encoding = "utf-8" ) as f :
26- for line in f .readlines ():
27- if line .__eq__ (author ):
28- return author
29- raise ArgumentAuthorError ("Author " + author + " is not in the accepted user list (input/users.txt)." )
28+ return author
3029
3130
3231def parse_naf (value ):
3332 try :
34- return validate_parse_naf (value , NAF_TO_DNI . keys () )
33+ return NAF (value )
3534 except ValueError as e :
36- raise ArgumentNafNotPresent ("NAF is not valid" + e .__str__ ())
35+ raise ArgumentNafInvalid ("NAF is not valid" + e .__str__ ())
3736
3837
39- def parse_compact (value ):
38+ def parse_compact_options (value ):
4039 to_compact = get_compact_init ()
4140 try :
4241 if "," in value :
@@ -52,7 +51,16 @@ def parse_compact(value):
5251 exit (1 )
5352
5453
55- def parse_arguments (valid_nafs ):
54+ def parse_input_type (value ):
55+ if value == "sharepoint" :
56+ return value
57+ elif value == "local" :
58+ return value
59+ else :
60+ raise UndefinedInputType ("The type supplied for input type \" " + value + "\" is not defined." )
61+
62+
63+ def parse_arguments ():
5664 """Parse and validate command-line arguments"""
5765 parser = argparse .ArgumentParser (description = "Process NAF and date range." )
5866
@@ -62,23 +70,27 @@ def parse_arguments(valid_nafs):
6270 parser .add_argument ("-e" , "--end" , type = parse_date , required = True , help = "End date (YYYY-MM)" )
6371 parser .add_argument ("-a" , "--author" , type = parse_author , required = True , help = "author's email doing request" )
6472
65- parser .add_argument ("-c" , "--compact" , type = parse_compact , required = False , default = get_compact_init (),
73+ parser .add_argument ("-c" , "--compact" , type = parse_compact_options , required = False , default = get_compact_init (),
6674 help = "Comma separated list of values that indicate which documents need to be merged in one "
6775 "single PDF in the output. Possible values are: " +
6876 "," .join ([dt .value .__str__ () for dt in DocType ]))
6977
78+ parser .add_argument ("-i" , "--input" , type = parse_input_type , required = False , default = "sharepoint" ,
79+ help = "Location of the input data. Possible values are: \" sharepoint\" to download from "
80+ "sharepoint location and \" local\" to use the local file system storage adn read the input"
81+ " folder in the repository root folder." )
7082 args = parser .parse_args ()
7183
7284 return args
7385
7486
75- def process_arguments ():
87+ def process_parse_arguments ():
7688 common = ("Error parsing arguments. Program aborting. The arguments are: "
7789 + str (sys .argv ) + "The program is in a uninitialized state and cannot proceed. This error will be "
7890 "notified to the admin via log file. We can't create log file in user author folder "
7991 "because user author could not be parsed." )
8092 try :
81- args = parse_arguments (NAF_TO_DNI . keys () )
93+ args = parse_arguments ()
8294
8395 except ArgumentNafNotPresent as e :
8496 print ("The NAF provided is valid but is not present in " + NAF_DATA_PATH + ". Internal error is " + e .__str__ ())
@@ -103,3 +115,49 @@ def process_arguments():
103115 return args
104116
105117
118+ # Validations functions that check if the data from the request is valid regarding business rules
119+
120+
121+ def validate_naf (naf , valid_nafs ):
122+ if not is_naf_present (naf , valid_nafs ):
123+ raise ArgumentNafNotPresent
124+
125+
126+ def is_author_present (author , valid_authors ):
127+ return author in valid_authors
128+
129+
130+ def validate_author (author , valid_authors ):
131+ if not is_author_present (author , valid_authors ):
132+ raise ArgumentAuthorError ("Author \" " + str (author ) + " is not valid. " ) # more specific exception
133+
134+
135+ def validate_arguments (args , valid_nafs , valid_authors ):
136+ validate_author (args .author , valid_authors )
137+ validate_naf (args .naf , valid_nafs )
138+
139+
140+ def process_validate_arguments (args , naf_data_path , user_list_data_path ):
141+ common = ("Error validating arguments. Program aborting. The arguments are: "
142+ + str (sys .argv ) + "The program is in a uninitialized state and cannot proceed. This error will be "
143+ "notified to the admin via log file. We can't create log file in user author folder "
144+ "because the process that validates user author could not finish." )
145+
146+ nafs = build_naf_to_dni (naf_data_path ).keys ()
147+
148+ authors = []
149+ with open (user_list_data_path , newline = "" , encoding = "utf-8" ) as f :
150+ for line in f .readlines ():
151+ authors .append (line )
152+
153+ try :
154+ validate_arguments (args , nafs , authors )
155+
156+ except ArgumentNafNotPresent as e :
157+ print ("The NAF provided is valid but is not present in " + naf_data_path + ". Internal error is " + e .__str__ ())
158+ print (common )
159+ exit (1 )
160+ except ArgumentAuthorError as e :
161+ print ("The author is not present in the accepted user list. Internal error is " + e .__str__ ())
162+ print (common )
163+ exit (4 )
0 commit comments