Wrestling with Python
In my attempt to write idiomatically correct python code. I laid out the following structure.
Define classes using args and kwargs
class SyncFileMetadata:
file_type = ""
def __init__(self, *args, **kwargs):
if 'file_type' in kwargs:
self.file_type = kwargs.get('file_type')
from datetime import datetime, timezone
import pytz
import os
from sync_file_metadata import SyncFileMetadata
class SyncArguments:
verbose = False
debug = False
filename = ""
output = os.curdir
is_exact = False
def __init__(self, *args, **kwargs):
if 'verbose' in kwargs:
self.verbose = kwargs.get('verbose')
if 'debug' in kwargs:
self.debug = kwargs.get('debug')
if 'output' in kwargs:
self.output = kwargs.get('output')
if 'filename' in kwargs:
self.filename = kwargs.get('filename')
if len(self.filename) > 0:
if os.path.isabs(self.filename):
temp = os.path.basename(self.filename)
file_metadata = SyncFileMetadata.parse(temp)
else:
self.filename = self.filename.upper()
file_metadata = SyncFileMetadata.parse(self.filename)
Then construct the main entry program as such using the common argparse library
import argparse
import glob
import threading
from time import sleep
import sys
import datetime
import os
def parse_arguments():
""" Creates an ArgumentParser, adds arguments and options, and then returns
the populated name space
Returns:
Populated namespace
"""
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", dest="input_file", help="input file or pattern", default="")
...
parser.set_defaults(verbose=False)
parser.set_defaults(debug=False)
parser.set_defaults(sequential=False)
return parser.parse_args()
def sync_arguments():
args = parse_arguments()
...
return sync_files
def sync_action(command_args):
try:
...
except Exception as ex:
print("Failed to create file {0} because {1}".format(..., ex))
def main():
commands = []
for current_sync_command in sync_arguments():
if bool(parse_arguments().sequential):
sync_action(current_sync_command)
else:
commands.append(threading.Thread(target=sync_action, args=(current_sync_command,)))
if len(commands) > 0:
[current.start() for current in commands]
[current.join() for current in commands]
if __name__ == "__main__":
print('Starting {0}'.format(str(sys.argv)))
main()
Gives the general idea