Skip to content

Commit e0dcdc6

Browse files
committedJul 25, 2024
feat: use a pydantic model to parse config file
1 parent ecdca3d commit e0dcdc6

File tree

6 files changed

+53
-36
lines changed

6 files changed

+53
-36
lines changed
 

‎config.ini.example

-8
This file was deleted.

‎config.toml.example

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[pulse]
2+
userid = "<username>"
3+
host = "pulse.mozilla.org"
4+
port = 5671
5+
exchange = "exchange/<username>/test"
6+
routing_key = ""
7+
queue = "queue/<username>/test"
8+
password = "<pulse-password>"

‎git_hg_sync/__main__.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from kombu import Connection, Exchange, Queue
66
from mozlog import commandline
77

8-
from git_hg_sync import config
8+
from git_hg_sync.config import Config, get_repos_config
99
from git_hg_sync.pulse_worker import PulseWorker
1010
from git_hg_sync.repo_synchronizer import RepoSynchronyzer
1111

@@ -19,21 +19,21 @@ def get_parser():
1919

2020
def get_connection(config):
2121
return Connection(
22-
hostname=config["host"],
23-
port=config["port"],
24-
userid=config["userid"],
25-
password=config["password"],
22+
hostname=config.host,
23+
port=config.port,
24+
userid=config.userid,
25+
password=config.password,
2626
heartbeat=10,
2727
ssl=True,
2828
)
2929

3030

3131
def get_queue(config):
32-
exchange = Exchange(config["exchange"], type="topic")
32+
exchange = Exchange(config.exchange, type="topic")
3333
return Queue(
34-
name=config["queue"],
34+
name=config.queue,
3535
exchange=exchange,
36-
routing_key=config["routing_key"],
36+
routing_key=config.routing_key,
3737
exclusive=False,
3838
)
3939

@@ -43,10 +43,11 @@ def main():
4343
commandline.add_logging_group(parser)
4444
args = parser.parse_args()
4545
logger = commandline.setup_logging("service", args, {"raw": sys.stdout})
46-
pulse_config = config.get_pulse_config(HERE.parent / "config.ini")["pulse"]
46+
config = Config.from_file(HERE.parent / "config.toml")
47+
pulse_config = config.pulse
4748
connection = get_connection(pulse_config)
4849

49-
repos_config = config.get_repos_config(HERE.parent / "repos.json")
50+
repos_config = get_repos_config(HERE.parent / "repos.json")
5051

5152
queue = get_queue(pulse_config)
5253
repo_synchronyzer = RepoSynchronyzer(repos_config=repos_config)

‎git_hg_sync/config.py

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
import configparser
1+
import pathlib
2+
import tomllib
23
import json
34

5+
import pydantic
46

5-
def get_pulse_config(config_file_path):
6-
assert config_file_path.exists(), f"config file {config_file_path} doesn't exists"
7-
config = configparser.ConfigParser()
8-
config.read(config_file_path)
9-
return config
7+
class PulseConfig(pydantic.BaseModel):
8+
userid: str
9+
host: str
10+
port: int
11+
exchange: str
12+
routing_key: str
13+
queue: str
14+
password: str
1015

1116

12-
def get_repos_config(repo_file_path):
17+
class Config(pydantic.BaseModel):
18+
pulse: PulseConfig
19+
20+
@staticmethod
21+
def from_file(file_path: pathlib.Path) -> "Config":
22+
assert file_path.exists(), f"config file {file_path} doesn't exists"
23+
with open(file_path, "rb") as config_file:
24+
config = tomllib.load(config_file)
25+
return Config(**config)
26+
27+
28+
def get_repos_config(repo_file_path: pathlib.Path):
1329
assert repo_file_path.exists(), f"config file {repo_file_path} doesn't exists"
1430
with open(repo_file_path) as f:
1531
repos = json.load(f)

‎pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ name = "git-hg-sync"
77
readme = "README.md"
88
requires-python = ">=3.10"
99
version = "0.1"
10-
dependencies = ['kombu', 'mozillapulse', 'GitPython', 'mozlog']
10+
dependencies = ['kombu', 'mozillapulse', 'GitPython', 'mozlog', "pydantic"]
1111

1212
[tool.ruff]
1313
line-length = 100

‎tests/pulse_utils.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import kombu
55

6-
from git_hg_sync import config
6+
from git_hg_sync.config import Config
77

88
HERE = Path(__file__).parent
99

@@ -14,13 +14,13 @@ def send_pulse_message(pulse_config, payload):
1414
The Pulse message will be constructed from the specified payload
1515
and sent to the requested exchange.
1616
"""
17-
userid = pulse_config["userid"]
18-
password = pulse_config["password"]
19-
routing_key = pulse_config["routing_key"]
20-
host = pulse_config["host"]
21-
port = pulse_config["port"]
22-
exchange = pulse_config["exchange"]
23-
queue = pulse_config["queue"]
17+
userid = pulse_config.userid
18+
password = pulse_config.password
19+
routing_key = pulse_config.routing_key
20+
host = pulse_config.host
21+
port = pulse_config.port
22+
exchange = pulse_config.exchange
23+
queue = pulse_config.queue
2424
print(f"connecting to pulse at {host}:{port} as {userid}")
2525

2626
connection = kombu.Connection(
@@ -74,5 +74,5 @@ def send_pulse_message(pulse_config, payload):
7474
"user": "user",
7575
"push_json_url": "push_json_url",
7676
}
77-
pulse_conf = config.get_pulse_config(HERE.parent / "config.ini")["pulse"]
78-
send_pulse_message(pulse_conf, payload)
77+
config = Config.from_file(HERE.parent / "config.toml")
78+
send_pulse_message(config.pulse, payload)

0 commit comments

Comments
 (0)
Failed to load comments.