-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgjain_python_script_template.py
122 lines (101 loc) · 4.83 KB
/
gjain_python_script_template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/local/bin/python
# -*- coding: utf-8 -*-
"""
***********************************************
- PROGRAM: gjain_python_script_template.py
- CONTACT: Gaurav Jain ([email protected])
- LICENSE: Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***********************************************
__author__ = "Gaurav Jain"
__copyright__ = "Copyright 2019"
__credits__ = ["Gaurav Jain"]
__license__ = "GPL3+"
__maintainer__ = "Gaurav Jain"
__email__ = "gaurav.jain near tum.de"
__status__ = "Development"
***********************************************
"""
print (__doc__)
def main():
# Get input options
args = check_options()
my_argument1 = args.my_argument1
my_argument2 = args.my_argument2
my_argument3 = args.my_argument3
some_flag = args.smflag
my_argument4 = args.my_argument4
# Your main function code
################ USER DEFINED FUNCTIONS ###################
def check_options():
''' Checks the options to the program '''
# Create parser object
parser = argparse.ArgumentParser(add_help=True,
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=textwrap.dedent('''\
----------------- SAMPLE USAGE ------------------
- python gjain_python_script_template.py -a1="my_first_argument"
- python gjain_python_script_template.py -a1="my_first_argument" -a2="my_second_argument" -a3=49
- python gjain_python_script_template.py -a1="my_first_argument" -a2="my_second_argument" -a3=49 -fg
- python gjain_python_script_template.py -a1="my_first_argument" -a2="my_second_argument" -a3=49 -fg -a4='r'
-------------------------------------------------
CONTACT:
Gaurav Jain
-------------------------------------------------
'''))
# Add arguments
parser.add_argument("-a1", metavar="--myarg1" , help="*My argument one" , dest="my_argument1" , type=str, required=True)
parser.add_argument("-a2", metavar="--myarg2" , help=" My argument two" , dest="my_argument2" , type=str)
parser.add_argument("-a3", metavar="--myarg3" , help=" My argument three", dest="my_argument3" , type=int)
parser.add_argument("-fg", "--smflag" , help=" This is some flag. if set, perform some additional tasks", action='store_true', default=False)
parser.add_argument('-a4', "--selval" , help=" The argument that can have only certain options\n - Please enter:\n\t -sc='n' for NONE\n\t -sc='r' for ROW\n\t -sc='c' for COLUMN", dest="my_argument4", default='n', action='store', choices=['n', 'r', 'c'])
# Print the help message only if no arguments are supplied
if len(sys.argv)==1:
parser.print_help()
sys.exit(1)
# Save the STDOUT output in a log file
if parser.parse_args().my_argument1:
logdir = "{0}/logs".format(get_file_info(parser.parse_args().my_argument1)[0])
create_dir(logdir)
logfile = "{0}/{1}.log".format(logdir, get_file_info(parser.parse_args().my_argument1)[1])
else:
logdir = "{0}/logs".format(os.getcwd())
create_dir(logdir)
logfile = "{0}/{1}.log".format(logdir,get_file_info(sys.argv[0])[1])
logf = open(logfile, 'w')
sys.stdout = Log(logf, sys.stdout)
# Parse command line with parse_args and store it in an object
args = parser.parse_args()
print_initial_arguments(parser)
return args
# main function
if __name__=="__main__":
# Built in modules
import argparse
import os.path
import sys
# 3rd party modules
import textwrap
import re
import subprocess
import math
from collections import *
# For looping files in a dir
import glob
from os import listdir
from os.path import isfile, join
# user defined modules
#from gjainLIB import * # import all the functions from the Gaurav`s python library
################ USER CONFIGURATION ###################
#######################################################
main()