Skip to content

Commit 920d28d

Browse files
author
Julian Vanden Broeck
committed
WIP: Define pgpass as a dataclass
1 parent f9fa11b commit 920d28d

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

pgtoolkit/pgpass.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@
7676
from collections.abc import Iterable, Iterator
7777
from contextlib import contextmanager
7878
from pathlib import Path
79-
from typing import IO, Callable
79+
from typing import IO, Callable, Literal
80+
from dataclasses import dataclass, field
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,22 +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
222217

223218
def __eq__(self, other: object) -> bool:
224219
if isinstance(other, PassComment):
@@ -292,6 +287,7 @@ def matches(self, **attrs: int | str) -> bool:
292287
return True
293288

294289

290+
@dataclass
295291
class PassFile:
296292
"""Holds .pgpass file entries and comments.
297293
@@ -314,7 +310,7 @@ class PassFile:
314310
315311
"""
316312

317-
lines: list[PassComment | PassEntry]
313+
lines: list[PassComment | PassEntry] = field(default_factory=list, init=False)
318314
path: str | None = None
319315

320316
def __init__(

0 commit comments

Comments
 (0)