Kaynağa Gözat

start extracting parameters to a configuration file

George C. Privon 8 yıl önce
ebeveyn
işleme
fc1b9656aa
3 değiştirilmiş dosya ile 22 ekleme ve 0 silme
  1. 1 0
      .gitignore
  2. 2 0
      CarcassonneScore.conf.example
  3. 19 0
      manage_database.py

+ 1 - 0
.gitignore

@@ -104,4 +104,5 @@ venv.bak/
 .mypy_cache/
 
 CarcassonneScore.db
+CarcassonneScore.conf
 *.swp

+ 2 - 0
CarcassonneScore.conf.example

@@ -0,0 +1,2 @@
+[CarcassonneScore]
+DBNAME = CarcassonneScore.db

+ 19 - 0
manage_database.py

@@ -21,10 +21,25 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 import sys
 import os
 import argparse
+import configparser
 import re
 import sqlite3
 
 
+def loadConfig(cfile):
+    """
+    Load configuration file
+    """
+
+    if not os.path.isfile(cfile):
+        sys.stderr.write("Error: could not find configuration file '" + cfile + "'\n")
+        sys.exit()
+
+    config = configparser.read(cfile)
+
+    return config
+
+
 def parseArgs():
     """
     Command line arguments
@@ -32,6 +47,8 @@ def parseArgs():
 
     parser = argparse.ArgumentParser(description="Update the Carcassonne \
 scoring database.")
+    parser.add_argument('-c', '--config', default='CarcassonneScore.conf',
+                        help='Location of the configuration file.')
     cmds = parser.add_mutually_exclusive_group(required=True)
     cmds.add_argument('--init', default=False, action='store_true',
                       help='Create a fresh database.')
@@ -270,6 +287,8 @@ def main():
 
     args = parseArgs()
 
+    copts = loadConfig(args.config)
+
     DBNAME = 'CarcassonneScore.db'
     DBVER = 0