Jeremy White : Switch to using configparser.

Alexandre Julliard julliard at winehq.org
Wed Apr 27 14:34:47 CDT 2022


Module: tools
Branch: master
Commit: f34eb662ee91bf662222bee9316ed9f495cd2932
URL:    https://source.winehq.org/git/tools.git/?a=commit;h=f34eb662ee91bf662222bee9316ed9f495cd2932

Author: Jeremy White <jwhite at codeweavers.com>
Date:   Tue Apr 26 15:22:17 2022 -0500

Switch to using configparser.

This simply feels like a more rational way to
configure these applications and will allow the
runtime database to be separate from the code.

NOTE:  This will be a breaking change.

Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 gitlab/gitlab-to-mail/README.md                    |  6 ++---
 gitlab/gitlab-to-mail/db.py                        |  6 ++---
 .../{settings_example.py => example.cfg}           |  6 ++++-
 gitlab/gitlab-to-mail/gitlabtomail.py              |  8 ++----
 gitlab/gitlab-to-mail/gitlabtomail.service         |  2 +-
 gitlab/gitlab-to-mail/mailtogitlab.py              |  7 ++----
 gitlab/gitlab-to-mail/util.py                      | 29 ++++++++++++++++++++++
 7 files changed, 44 insertions(+), 20 deletions(-)

diff --git a/gitlab/gitlab-to-mail/README.md b/gitlab/gitlab-to-mail/README.md
index 993b673..9f3f15b 100644
--- a/gitlab/gitlab-to-mail/README.md
+++ b/gitlab/gitlab-to-mail/README.md
@@ -1,10 +1,10 @@
 # GitLab <-> Mailing List bridge
 
 ## How to use
- 1. Copy `settings_example.py` and name it `setting_projectname.py`.
+ 1. Copy `example.cfg` and name it `projectname.cfg`.
  2. Tweak the values found inside.
- 3. Run `gitlabtomail.py projectname` to generate emails from gitlab changes.
- 4. Run `mailtogitlab.py projectname < email` to generate gitlab changes from an email.
+ 3. Run `gitlabtomail.py /path/to/projectname.cfg` to generate emails from gitlab changes.
+ 4. Run `mailtogitlab.py /path/to/projectname.cfg < email` to generate gitlab changes from an email.
 
 ## Suggested ways to achieve that
  * Setup ingestion IMAP mailbox that subscribes to the mailing list.
diff --git a/gitlab/gitlab-to-mail/db.py b/gitlab/gitlab-to-mail/db.py
index 472026c..9794abf 100644
--- a/gitlab/gitlab-to-mail/db.py
+++ b/gitlab/gitlab-to-mail/db.py
@@ -1,12 +1,10 @@
 from sqlalchemy import create_engine, Column, Integer, Unicode, DateTime, Date
 from sqlalchemy.orm import declarative_base, sessionmaker
+from util import Settings
 
-import importlib
-import os
 import sys
 
-sys.path.append(os.getcwd())
-settings = importlib.import_module(f"settings_{sys.argv[1]}")
+settings = Settings(sys.argv[1])
 
 Base = declarative_base()
 
diff --git a/gitlab/gitlab-to-mail/settings_example.py b/gitlab/gitlab-to-mail/example.cfg
similarity index 92%
rename from gitlab/gitlab-to-mail/settings_example.py
rename to gitlab/gitlab-to-mail/example.cfg
index 1a42216..3cd31d9 100644
--- a/gitlab/gitlab-to-mail/settings_example.py
+++ b/gitlab/gitlab-to-mail/example.cfg
@@ -1,3 +1,5 @@
+[settings]
+
 # Location of the sqlite database file, which will be auto created if not found
 DATABASE= "/home/wine/example/db_example.sqlite"
 
@@ -14,7 +16,9 @@ GITLAB_PROJECT_NAME = "namespace/project"
 
 BRIDGE_FROM_EMAIL = "do.not.reply at example.com"
 BRIDGE_TO_EMAIL = "bridge-list at lists.example.com"
-BRIDGE_TAG = None  # used for [PATCH $TAG 1/1] and [$TAG] for comments
+
+# used for [PATCH $TAG 1/1] and [$TAG] for comments
+#BRIDGE_TAG = mytag
 
 SMTP_DOMAIN = "mail.example.com"
 SMTP_PORT = 587
diff --git a/gitlab/gitlab-to-mail/gitlabtomail.py b/gitlab/gitlab-to-mail/gitlabtomail.py
index c10a0ed..0a5ef4b 100755
--- a/gitlab/gitlab-to-mail/gitlabtomail.py
+++ b/gitlab/gitlab-to-mail/gitlabtomail.py
@@ -5,19 +5,15 @@ from urllib.parse import urljoin, urlparse
 import db
 import re
 import sys
-import os
 import datetime
 import dateutil.parser
 import requests
 import mailbox
 import smtplib
 import email
-import importlib
-from util import fetch_all
+from util import fetch_all, Settings
 
-
-sys.path.append(os.getcwd())
-settings = importlib.import_module(f"settings_{sys.argv[1]}")
+settings = Settings(sys.argv[1])
 
 
 GITLAB_TIME_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
diff --git a/gitlab/gitlab-to-mail/gitlabtomail.service b/gitlab/gitlab-to-mail/gitlabtomail.service
index 0aeed78..3dcde7d 100644
--- a/gitlab/gitlab-to-mail/gitlabtomail.service
+++ b/gitlab/gitlab-to-mail/gitlabtomail.service
@@ -7,7 +7,7 @@ Type=simple
 Restart=always
 RestartSec=60
 User=wine
-ExecStart=/home/wine/gitlab-to-mail/runner.py wine
+ExecStart=/home/wine/gitlab-to-mail/runner.py wine.cfg
 
 [Install]
 WantedBy=multi-user.target
diff --git a/gitlab/gitlab-to-mail/mailtogitlab.py b/gitlab/gitlab-to-mail/mailtogitlab.py
index 7e3c318..7fcb85b 100755
--- a/gitlab/gitlab-to-mail/mailtogitlab.py
+++ b/gitlab/gitlab-to-mail/mailtogitlab.py
@@ -5,14 +5,11 @@ from urllib.parse import urljoin
 
 import re
 import sys
-import os
-import importlib
 import email.utils
 import requests
-from util import fetch_all
+from util import fetch_all, Settings
 
-sys.path.append(os.getcwd())
-settings = importlib.import_module(f"settings_{sys.argv[1]}")
+settings = Settings(sys.argv[1])
 MIN_PYTHON = (3, 8)  # because of the walrus operator use
 
 if sys.version_info < MIN_PYTHON:
diff --git a/gitlab/gitlab-to-mail/util.py b/gitlab/gitlab-to-mail/util.py
index 37a2729..e37ead8 100644
--- a/gitlab/gitlab-to-mail/util.py
+++ b/gitlab/gitlab-to-mail/util.py
@@ -1,4 +1,6 @@
 import requests
+import configparser
+import sys
 
 
 def fetch_all(url, settings):
@@ -13,3 +15,30 @@ def fetch_all(url, settings):
         else:
             url = None
     return result
+
+
+class Settings:
+    def __init__(self, fname):
+        self.cp = configparser.ConfigParser()
+        if len(self.cp.read(fname)) == 0:
+            print(f"Error: invalid configuration in {fname}.", file=sys.stderr)
+            sys.exit(1)
+        for s in ['DATABASE', 'GITLAB_TOKEN', 'GITLAB_URL', 'GITLAB_PROJECT_NAME',
+                  'BRIDGE_FROM_EMAIL', 'BRIDGE_TO_EMAIL', 'BRIDGE_TAG',
+                  'SMTP_DOMAIN', 'SMTP_USER', 'SMTP_PASSWORD']:
+            if s in self.cp['settings']:
+                self.__dict__[s] = self.cp['settings'][s].strip('"')
+            else:
+                self.__dict__[s] = None
+
+        for s in ['DEBUG']:
+            if s in self.cp['settings']:
+                self.__dict__[s] = self.cp['settings'].getboolean(s)
+            else:
+                self.__dict__[s] = None
+
+        for s in ['GITLAB_PROJECT_ID', 'SMTP_PORT', 'INITIAL_BACKLOG_DAYS', 'MAXIMUM_PATCHES']:
+            if s in self.cp['settings']:
+                self.__dict__[s] = self.cp['settings'].getint(s)
+            else:
+                self.__dict__[s] = None




More information about the wine-cvs mailing list