From: Thomas Jarosch Date: Fri, 29 Jun 2012 07:33:28 +0000 (+0200) Subject: Don't hardcode filenames in multiple places, centralize them. X-Git-Url: http://developer.intra2net.com/git/?p=imap-fix-internaldate;a=commitdiff_plain;h=6177b21db21406891b6b8a494860d6149772eb83 Don't hardcode filenames in multiple places, centralize them. New name for the config file: fix_imap_internaldate.cfg --- diff --git a/src/fix_imap_internaldate.py b/src/fix_imap_internaldate.py index 1f1cf74..7e05ac6 100644 --- a/src/fix_imap_internaldate.py +++ b/src/fix_imap_internaldate.py @@ -29,6 +29,10 @@ from mail_date_parser import MailDateParser from mail_iterator import MailIterator from caching_data import CachingData +CONFIG_FILENAME = "fix_imap_internaldate.cfg" +LOG_FILENAME = "fix_imap_internaldate.log" +CSV_FILENAME = "userdata.csv" + def main(): """Interprets command arguments and initializes configuration and logger. Then begins mail synchronization.""" @@ -61,30 +65,30 @@ def main(): def load_configuration(): """Loads the script configuration from a file or creates such.""" config = configparser.RawConfigParser() - success = config.read('confscript.cfg') + success = config.read(CONFIG_FILENAME) if(len(success)==0): config.add_section('basic_settings') config.set('basic_settings', 'file_log_level', logging.INFO) config.set('basic_settings', 'console_log_level', logging.INFO) config.set('basic_settings', 'imap_server', 'imap.company.com') config.set('basic_settings', 'tolerance', 30) - with open('confscript.cfg', 'w') as configfile: + with open(CONFIG_FILENAME, 'w') as configfile: config.write(configfile) configfile.write("# 0 NOTSET, 10 DEBUG, 20 INFO, 30 WARNING, 40 ERROR, 50 CRITICAL") return config def prepare_logger(config): """Sets up the logging functionality""" - + # reset the log - with open('fix_imap_internaldate.log', 'w'): + with open(LOG_FILENAME, 'w'): pass - + # add basic configuration - logging.basicConfig(filename='fix_imap_internaldate.log', + logging.basicConfig(filename=LOG_FILENAME, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=config.getint('basic_settings', 'file_log_level')) - + # add a handler for a console output console = logging.StreamHandler() console.setLevel(config.getint('basic_settings', 'console_log_level')) @@ -103,7 +107,7 @@ def synchronize_csv(config, test_mode): tolerance = config.getint('basic_settings', 'tolerance') * 60 # iterate through the users in the csv data - user_reader = csv.DictReader(open("userdata.csv", "r"), delimiter=',') + user_reader = csv.DictReader(open(CSV_FILENAME, "r"), delimiter=',') for user in user_reader: try: session = MailIterator(server, user['username'], user['password']) @@ -152,7 +156,7 @@ def synchronize_csv(config, test_mode): # if all messages were successfully fixed confirm caching if(not test_mode): box.confirm_change() - + # final report on date conflicts caching_data.report_conflicts() return