Made working dir customizable by options
authorJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sat, 26 May 2012 08:44:59 +0000 (08:44 +0000)
committerJean-Michel Nirgal Vourgère <jmv@nirgal.com>
Sat, 26 May 2012 08:44:59 +0000 (08:44 +0000)
Removed a few globals

members.py

index 1e5b09bc7407cc02cd2adadab03ed84ff11aeeb4..404e3049958a7566d4cc59b702dea72fc44408f6 100755 (executable)
@@ -30,15 +30,14 @@ FLOOD,Nick1,Grade1,100,102,Nick3,Grade3,100,99,1,1.00,Nick1 a pris 1 cm² à Nic
 Note that in last 2 lines exemple, NewField1 is 102 that is different from 100+1 because of multiple floods.
 """
 
-
-
+# globals:
+#BASE_DIRECTORY
 #SERVER
 #LOGIN
 #PASSWORD
 #BASE_URL
-HOME = os.environ['HOME']
 
-def read_config():
+def read_config(config_dir):
     '''
     That function will read config.py in .fourmizzz directory
     and set up globals SERVER, LOGIN, PASSWORD, and BASE_URL.
@@ -51,13 +50,13 @@ LOGIN = 'MonIdentifiant'
 PASSWORD = 'MonMotDePasse'
     '''
 
-    sys.path.append(HOME+'/.fourmizzz')
+    sys.path.append(config_dir)
     try:
         import config
     except ImportError:
-        CONFIG = HOME+'/.fourmizzz/config.py'
-        logging.fatal("No configuration file. Creating %s", CONFIG)
-        f = open(CONFIG, mode='w+', encoding='utf-8')
+        config_filename = os.path.join(config_dir, 'config.py')
+        logging.fatal("No configuration file. Creating %s", config_filename)
+        f = open(config_filename, mode='w+', encoding='utf-8')
         f.write(CONFIG_TEMPLATE)
         f.close()
         logging.fatal("Please update this file with your details.")
@@ -100,7 +99,7 @@ def number_format(i):
             result = ' ' + result
         d3 = i % 1000
         i = i//1000
-        if i:
+        if i: # there will be something else left of that
             result = ('%03d' % d3) + result
         else:
             result = ('%d' % d3) + result
@@ -202,7 +201,11 @@ def members_to_field(members):
     return fields
 
 
-def tdc_compare(oldtdc, newtdc):
+def field_compare(oldmembers, newmembers):
+    
+    oldtdc = members_to_field(oldmembers)
+    newtdc = members_to_field(newmembers)
+
     changes = {}
     txtchanges = []
     for nick in newtdc.keys():
@@ -270,6 +273,11 @@ if __name__ == '__main__':
     parser.add_option('-a', '--alliance',
         dest='alliance',
         help="alliance tag. default is to process player own alliance.")
+    parser.add_option('--dir',
+        dest='directory', default='.fourmizzz',
+        help="Working directory, including config.py, archives and results. "
+             "Relative to HOME unless it starts with /. "
+             "Default is '%default'.")
     parser.add_option('--dry-run',
         action='store_true', dest='dryrun', default=False,
         help="don't store result in archives.")
@@ -282,30 +290,34 @@ if __name__ == '__main__':
     else:
         loglevel = logging.INFO
     
-    RESULT = HOME + '/.fourmizzz/results.log'
-    logging.basicConfig(filename=RESULT, level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S %Z')
+    HOME = os.environ['HOME']
+    if options.directory[0] == '/':
+        BASE_DIRECTORY = options.directory
+    else:
+        BASE_DIRECTORY = os.path.join(HOME, options.directory)
 
-    read_config()
+    result_filename = os.path.join(BASE_DIRECTORY, 'results.log')
+    logging.basicConfig(filename=result_filename, level=loglevel, format='%(asctime)s %(levelname)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S %Z')
 
-    ARCHIVE = HOME + '/.fourmizzz/archive.py'
+    read_config(BASE_DIRECTORY)
+
+    members_filename = os.path.join(BASE_DIRECTORY, 'archive.py')
     oldmembers = None
     try:
-        f = open(ARCHIVE, mode='r+', encoding='utf-8')
+        f = open(members_filename, mode='r+', encoding='utf-8')
     except IOError as err:
         if err.errno == 2: # No such file or directory
             logging.warning("No archive file, creating one.")
-            f = open(ARCHIVE, mode='w+', encoding='utf-8')
+            f = open(members_filename, mode='w+', encoding='utf-8')
         else:
             raise
     else:
         oldmembers = eval(f.read())
-        oldtdc = members_to_field(oldmembers)
     
     newmembers = tdc_get(options.alliance)
-    newtdc = members_to_field(newmembers)
 
-    if oldtdc is not None:
-        tdc_compare(oldtdc, newtdc)
+    if oldmembers is not None:
+        field_compare(oldmembers, newmembers)
     
     # Save archive only after processing, just in case it crashes
     if not options.dryrun: