Skip to content

Commit

Permalink
WIP: Define pgpass as a dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Vanden Broeck committed Nov 28, 2024
1 parent 5c45afe commit 24c5efe
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions pgtoolkit/pgpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@
import warnings
from collections.abc import Callable, Iterable, Iterator
from contextlib import contextmanager
from dataclasses import dataclass, field
from pathlib import Path
from typing import IO
from typing import IO, Literal

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


@dataclass
class PassEntry:
"""Holds a .pgpass entry.
Expand Down Expand Up @@ -190,6 +192,12 @@ class PassEntry:
"""

hostname: str
port: int | Literal["*"] = 5432
database: str = "postgres"
username: str = "postgres"
password: str = ""

@classmethod
def parse(cls, line: str) -> PassEntry:
"""Parse a single line.
Expand All @@ -203,23 +211,9 @@ def parse(cls, line: str) -> PassEntry:
raise ValueError("Invalid line.")
hostname, port, database, username, password = fields
return cls(
hostname, int(port) if port != "*" else port, database, username, password
hostname, int(port) if port != "*" else "*", database, username, password
)

def __init__(
self,
hostname: str,
port: int | str,
database: str,
username: str,
password: str,
) -> None:
self.hostname = hostname
self.port = port
self.database = database
self.username = username
self.password = password

def __eq__(self, other: object) -> bool:
if isinstance(other, PassComment):
try:
Expand Down Expand Up @@ -292,6 +286,7 @@ def matches(self, **attrs: int | str) -> bool:
return True


@dataclass
class PassFile:
"""Holds .pgpass file entries and comments.
Expand All @@ -314,7 +309,7 @@ class PassFile:
"""

lines: list[PassComment | PassEntry]
lines: list[PassComment | PassEntry] = field(default_factory=list, init=False)
path: str | None = None

def __init__(
Expand Down

0 comments on commit 24c5efe

Please sign in to comment.