Skip to content

Commit 24c5efe

Browse files
author
Julian Vanden Broeck
committed
WIP: Define pgpass as a dataclass
1 parent 5c45afe commit 24c5efe

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

pgtoolkit/pgpass.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@
7575
import warnings
7676
from collections.abc import Callable, Iterable, Iterator
7777
from contextlib import contextmanager
78+
from dataclasses import dataclass, field
7879
from pathlib import Path
79-
from typing import IO
80+
from typing import IO, Literal
8081

8182
from ._helpers import open_or_stdin
8283
from .errors import ParseError
@@ -158,6 +159,7 @@ def matches(self, **attrs: int | str) -> bool:
158159
return False
159160

160161

162+
@dataclass
161163
class PassEntry:
162164
"""Holds a .pgpass entry.
163165
@@ -190,6 +192,12 @@ class PassEntry:
190192
191193
"""
192194

195+
hostname: str
196+
port: int | Literal["*"] = 5432
197+
database: str = "postgres"
198+
username: str = "postgres"
199+
password: str = ""
200+
193201
@classmethod
194202
def parse(cls, line: str) -> PassEntry:
195203
"""Parse a single line.
@@ -203,23 +211,9 @@ def parse(cls, line: str) -> PassEntry:
203211
raise ValueError("Invalid line.")
204212
hostname, port, database, username, password = fields
205213
return cls(
206-
hostname, int(port) if port != "*" else port, database, username, password
214+
hostname, int(port) if port != "*" else "*", database, username, password
207215
)
208216

209-
def __init__(
210-
self,
211-
hostname: str,
212-
port: int | str,
213-
database: str,
214-
username: str,
215-
password: str,
216-
) -> None:
217-
self.hostname = hostname
218-
self.port = port
219-
self.database = database
220-
self.username = username
221-
self.password = password
222-
223217
def __eq__(self, other: object) -> bool:
224218
if isinstance(other, PassComment):
225219
try:
@@ -292,6 +286,7 @@ def matches(self, **attrs: int | str) -> bool:
292286
return True
293287

294288

289+
@dataclass
295290
class PassFile:
296291
"""Holds .pgpass file entries and comments.
297292
@@ -314,7 +309,7 @@ class PassFile:
314309
315310
"""
316311

317-
lines: list[PassComment | PassEntry]
312+
lines: list[PassComment | PassEntry] = field(default_factory=list, init=False)
318313
path: str | None = None
319314

320315
def __init__(

0 commit comments

Comments
 (0)