2 Commitit ae200646c2 ... 221ef0da61

Tekijä SHA1 Viesti Päivämäärä
  scuti 221ef0da61 ignore text files 3 viikkoa sitten
  scuti 30c26352cd added arg to change log folder 3 viikkoa sitten
2 muutettua tiedostoa jossa 31 lisäystä ja 18 poistoa
  1. 3 2
      .gitignore
  2. 28 16
      scripts/import-from-xon.py

+ 3 - 2
.gitignore

@@ -1,3 +1,4 @@
 *.exe
-
-*.o
+*.o
+*.txt
+*.log

+ 28 - 16
scripts/import-from-xon.py

@@ -3,7 +3,6 @@ import sqlite3 as sql
 import logging
 import logging.handlers
 
-from os           import listdir, mkdir
 from os.path      import isfile, exists
 from urllib.parse import unquote
 
@@ -48,8 +47,10 @@ def get_list_from_server_txt(filename):
             output.append(row)
     return output
 
-def init_logging():
-    filename = "_logs/dbimport-%s.log"
+def init_logging(folder, base_file_name="dbimport-%s.log"):
+    if not exists(folder):
+        return False
+    filename = folder + "/" + base_file_name
     i = 0
     while exists(filename % i):
         i += 1
@@ -163,6 +164,9 @@ def insert_to_database(d, s):
                 con.rollback()
 
 def write_query(out_file, data):
+    if exists(out_file):
+        print("stopped: output file already exists", file=sys.stderr)
+        return False
     times, \
     ranks, \
     ids,   \
@@ -175,7 +179,7 @@ def write_query(out_file, data):
             file_handle.write("INSERT OR REPLACE INTO Cts_ranks VALUES(\'%s\', \'%s\', %s, \'%s\');\n" % tuple(r))
         for i in ids:
             file_handle.write("INSERT OR REPLACE INTO Id2alias VALUES(\'%s\', \'%s\', \'%s\');\n" % tuple(i))
-    return
+    return True
 
 # Test whether repeat rows are added.
 def check_duplicates(database, data):
@@ -225,19 +229,27 @@ if __name__ == "__main__":
     ap.add_argument('-t', '--test', 
         action='store_true',
         help="test database for duplicates")
-    ap.add_argument('-q', '--query', 
+    ap.add_argument('-q', '--export-query', 
         action='store_true',
         help="write query file (as opposed to executing / inserting rows into database)")
+    ap.add_argument('-l', '--log', 
+        type=str,
+        help="set folder to store log files")
     args = ap.parse_args()
-    log_file = init_logging()
-    print("Writing log to ", log_file)
-    if args.test:
-        check_duplicates(args.dest, args.src)
-    if args.query:
-        try:
-            write_query(args.dest, args.src)
-        except FileNotFoundError:
-            traceback.print_exc()
-            print("\n\t Exiting - no file to work with.", file=sys.stderr)
+    log_dir  = args.log or "logs"
+    log_file = init_logging(log_dir)
+    if log_file:
+        print("writing log to folder '%s'," % log_dir, log_file, file=sys.stderr)
     else:
-        insert_to_database(args.dest, args.src)
+        print("exited: logging not initialized (folder '%s' does not exist)" % log_dir, file=sys.stderr)
+        exit()
+    try:
+        if args.test:
+            check_duplicates(args.dest, args.src)
+        if args.export_query:
+            write_query(args.dest, args.src)
+        else:
+            insert_to_database(args.dest, args.src)
+    except FileNotFoundError:
+        traceback.print_exc()
+        print("\n\t exited: no input file to work with.", file=sys.stderr)