2 次代碼提交 ae200646c2 ... 221ef0da61

作者 SHA1 備註 提交日期
  scuti 221ef0da61 ignore text files 3 周之前
  scuti 30c26352cd added arg to change log folder 3 周之前
共有 2 個文件被更改,包括 31 次插入18 次删除
  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)