In [20]:
%load_ext autoreload
%autoreload 2
%reload_ext autoreload
In [21]:
import ArgConfigParse
import logging
import logging.config
import constants
import sys
from os import chdir
import shutil
import re
In [22]:
def main():
#### CONSTANTS ####
# pull the absolute path from the constants file that resides in the root of the project
absPath = constants.absPath
# change the working directory - simplifies all the other path work later
chdir(absPath)
version = constants.version
app_name = constants.app_name
app_long_name = constants.app_long_name
# set the base log level
loglevel = 50
## CONFIGURATION FILES ##
# logging configuration file
logging_cfg = constants.logging_cfg
# default base configuration file
base_cfg = ArgConfigParse.fullPath(constants.default_cfg_name)
user_cfg = ArgConfigParse.fullPath(constants.user_cfg)
# Setup logging
logging.getLogger(__name__)
logging.config.fileConfig(logging_cfg)
logging.root.setLevel(loglevel)
# record updates to config file
update_config = False
args = ArgConfigParse.CmdArgs()
args.add_argument('-c', '--config', dest='config', ignore_none=True, default=None,
metavar='cfg.ini',
help='Use specified configuration file')
args.add_argument('-v', action='count', default=0, dest='verbose', ignore_none=True,
help='increase verbosity of logging by adding additional \'-v\'')
args.add_argument('-V', '--version', ignore_none=True, dest='version', action='store_true',
help='display version number and exit')
args.add_argument('-i', '--interactive', ignore_none=True, dest='interactive', action='store_true',
help='interactively add rsync jobs to config file')
# handle basic command line arguments
args.parse_args()
print(args.options)
if args.options.version:
print(f'{app_name} version {version}')
sys.exit(0)
logging.root.setLevel(loglevel-args.options.verbose*10)
logging.root.setLevel(10)
logging.debug(f'log level set to {logging.root.getEffectiveLevel()}')
# use the config from the command line
if args.options.config:
user_cfg = ArgConfigParse.fullPath(args.options.config)
elif not user_cfg.exists():
# create the config file directory
user_cfg.parent.mkdir(parents=True, exist_ok=True)
update_config = True
config_file_list = [base_cfg, user_cfg]
config_file = ArgConfigParse.ConfigFile(config_file_list)
config_file.parse_config()
# prompt user to create configuration file
config = ArgConfigParse.merge_dict(config_file.config_dict, args.nested_opts_dict)
if update_config:
logging.info(f'updating configuration file: {user_cfg}')
ArgConfigParse.write(config, user_cfg)
return config, config_file, args
In [23]:
if __name__ == "__main__":
c, cf, a = main()
In [ ]:
c
In [ ]:
a.nested_opts_dict
In [ ]:
o.parent
In [ ]:
c = ArgConfigParse.ConfigFile(['./automate_rsync.ini'])
In [ ]:
c.parse_config()
In [ ]:
re.split('\s{0,},\s{0,}', c.config_dict['%BaseConfig']['rsyncoptions'])
In [ ]:
In [ ]:
s = '-a, -z ,-v,-h'
re.split('\s{0,},\s{0,}', s)
In [ ]:
p = ArgConfigParse.fullPath('./foo/bar/text.txt')
In [ ]:
p.parent.mkdir(parents=True, exist_ok=True)
In [ ]:
p.parents.mkdir(parents=True, exist_ok=True)
In [ ]:
o.nested_opts_dict
In [ ]:
from sys import argv
In [ ]:
argv
In [ ]:
argv.pop()
In [ ]:
sys.argv.append("-vvvv")
In [ ]:
argv.append('-c')
In [ ]:
argv.append('foo.ini')
In [ ]: