lundi 21 janvier 2019

How do i run my script without having to pass args -c and -h to my function

So i have my main function where if i pass the arguments -c , -p it will run the customer function , however if these values are not run , it will run the default function. This script ('report.py'), i am calling in my main script , when i run this , i seem to be getting the following :

usage: main.py [-h] [-c] [-p]
main.py: error: unrecognized arguments: -d 2019-01-13

I am running this via my main script , which then goes to the main() function in report.py , since i do not pass any -c or -p arguments , this should run the functon default report , but it does not.

main.py is as follows :

import argparse
import os
from test_compare_filesets import testing
import generate_json as create_filesize_report
from generate_report import main as create_report
import functions as funcs


def get_json_location():
    json_path = os.getcwd() + os.path.join('/Testdata')
    return json_path


def main():

 parser = argparse.ArgumentParser()
 parser.add_argument("-d", "--execute-date", action="store", required=False)
 parser.add_argument("-t", "--execute-test", action="store_true", required=False)
 args = parser.parse_args()

 if args.execute_test :
  testing()

 if args.execute_date:

  yml_directory = os.listdir(os.getcwd() + os.path.join('/yaml'))
  yml_directory.remove('export_config.yaml')
  date = args.execute_date

  with open('dates/' + date + '.json', 'w') as start:
    start.close()

    for yml in yml_directory :
      print("Running export for " + os.path.join('yaml/' + yml))
      abs_yml_path =  os.path.join('yaml/' + yml)
      json_path = get_json_location()
      yml_file = funcs.read_config(abs_yml_path)
      create_filesize_report.generate_data_report(json_path , yml_file , date)
    create_report()

 else :
      print ("Please select a mode to run")


if __name__ == '__main__':
    main()

report.py

def get_report_config():

    parser = argparse.ArgumentParser()
    parser.add_argument("-c", "--current-date", action="store_true", required=False)
    parser.add_argument("-p", "--previous-date", action="store_true", required=False)
    args = parser.parse_args()
    return [args.current_date , args.previous_date]

cd_report = get_report_config() [0]
pd_report = get_report_config() [1]


def custom_report(file_names, previous_data , current_data):


    '''Get current_day file name'''
    current_day = os.path.splitext(cd_report)[0]

    '''Get previous_day file name'''
    previous_day = os.path.splitext(pd_report)[0]

    reporting = open('reports/' + os.path.basename(current_day) + "-report.txt", "w")
    reporting.write("This is the comparison report between the directories" " " + current_day +
                    " " "and" " " + previous_day + "\n\n")
    for item in file_names:
        reporting.write(item + compare(previous_data.get(item), current_data.get(item)) + "\n")
    reporting.close()


def main():

 if cd_report and pd_report:
    dict_9 = read_file_into_dict('dates/' + cd_report)
    dict_10 = read_file_into_dict('dates/' + pd_report)
    custom_report(file_names=return_yml_file_names(),
                    previous_data=dict_10,
                    current_data=dict_9
                    )
    print("Custom report has been generated for " + cd_report + ' ' + pd_report)


 else:


     dict_1 = read_file_into_dict('dates/' + current_day())
     dict_2 = read_file_into_dict('dates/' + previous_day())
     default_report(file_names=return_yml_file_names(),
                        previous_data=dict_2,
                        current_data=dict_1
                        )
     print("Report has been generated for " + current_day() + ' ' +  previous_day())



if __name__ == '__main__':

    main()

Aucun commentaire:

Enregistrer un commentaire