Skip to content

API documentation

conf_path()

Returns the path found for the pg_service.conf on the system as string.

Returns:

Type Description
Path

path to the pg_service.conf file as string

Source code in pgserviceparser/__init__.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def conf_path() -> Path:
    """Returns the path found for the pg_service.conf on the system as string.

    Returns:
        path to the pg_service.conf file as string
    """
    pg_config_path = None
    if getenv("PGSERVICEFILE"):
        pg_config_path = Path(getenv("PGSERVICEFILE"))
    elif getenv("PGSYSCONFDIR"):
        pg_config_path = Path(getenv("PGSYSCONFDIR")) / "pg_service.conf"
    else:
        if platform.system() == "Windows":
            pg_config_path = Path(getenv("APPDATA")) / "postgresql/.pg_service.conf"
        else:  # Linux or Darwin (Mac)
            pg_config_path = Path("~/.pg_service.conf").expanduser()

    return pg_config_path

full_config(conf_file_path=None)

Returns full pgservice config as configparser.ConfigParser().

Parameters:

Name Type Description Default
conf_file_path Optional[Path]

path to configuration file to load. If None the conf_path is used, defaults to None

None

Returns:

Type Description
ConfigParser

pg services loaded as ConfigParser

Raises:

Type Description
ServiceFileNotFound

when the service file is not found

Source code in pgserviceparser/__init__.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
def full_config(conf_file_path: Optional[Path] = None) -> configparser.ConfigParser:
    """Returns full pgservice config as configparser.ConfigParser().

    Args:
        conf_file_path: path to configuration file to load. If None the `conf_path` is used, defaults to None

    Returns:
        pg services loaded as ConfigParser

    Raises:
        ServiceFileNotFound: when the service file is not found
    """
    if conf_file_path is None:
        conf_file_path = conf_path()
    else:
        conf_file_path = Path(conf_file_path)

    if not conf_file_path.exists():
        raise ServiceFileNotFound(conf_file_path)

    config = configparser.ConfigParser()
    config.read(conf_file_path)
    return config

service_config(service_name, conf_file_path=None)

Returns the config from the given service name as a dict.

Parameters:

Name Type Description Default
service_name str

service name

required
conf_file_path Optional[Path]

path to the pg_service.conf. If None the conf_path is used, defaults to None

None

Returns:

Type Description
dict

service settings as dictionary

Raises:

Type Description
ServiceNotFound

when the service is not found

Source code in pgserviceparser/__init__.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def service_config(service_name: str, conf_file_path: Optional[Path] = None) -> dict:
    """Returns the config from the given service name as a dict.

    Args:
        service_name: service name
        conf_file_path: path to the pg_service.conf. If None the `conf_path` is used, defaults to None

    Returns:
        service settings as dictionary

    Raises:
        ServiceNotFound: when the service is not found
    """
    config = full_config(conf_file_path)

    if service_name not in config:
        raise ServiceNotFound

    return dict(config[service_name])

service_names(conf_file_path=None)

Returns all service names in a list.

Parameters:

Name Type Description Default
conf_file_path Optional[Path]

path to the pg_service.conf. If None the conf_path() is used, defaults to None

None

Returns:

Type Description
list[str]

list of every service registered

Raises:

Type Description
ServiceFileNotFound

when the service file is not found

Source code in pgserviceparser/__init__.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
def service_names(conf_file_path: Optional[Path] = None) -> list[str]:
    """Returns all service names in a list.

    Args:
        conf_file_path: path to the pg_service.conf. If None the `conf_path()` is used, defaults to None

    Returns:
        list of every service registered

    Raises:
        ServiceFileNotFound: when the service file is not found
    """

    config = full_config(conf_file_path)
    return config.sections()

write_service(service_name, settings, conf_file_path=None)

Writes a complete service to the service file.

Parameters:

Name Type Description Default
service_name str

service name

required
settings dict

settings dict defining the service config

required
conf_file_path Optional[Path]

path to the pg_service.conf. If None the conf_path() is used, defaults to None

None

Raises:

Type Description
ServiceFileNotFound

when the service file is not found

ServiceNotFound

when the service is not found

Source code in pgserviceparser/__init__.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def write_service(
    service_name: str,
    settings: dict,
    conf_file_path: Optional[Path] = None,
):
    """Writes a complete service to the service file.

    Args:
        service_name: service name
        settings: settings dict defining the service config
        conf_file_path: path to the pg_service.conf. If None the `conf_path()` is used, defaults to None

    Raises:
        ServiceFileNotFound: when the service file is not found
        ServiceNotFound: when the service is not found
    """
    config = full_config(conf_file_path)
    if service_name not in config:
        raise ServiceNotFound(service_name)

    config[service_name] = settings.copy()
    with open(conf_file_path or conf_path(), "w") as configfile:
        config.write(configfile, space_around_delimiters=False)

write_service_setting(service_name, setting_key, setting_value, conf_file_path=None)

Writes a service setting to the service file.

Parameters:

Name Type Description Default
service_name str

service name

required
setting_key str

key

required
setting_value str

value

required
conf_file_path Optional[Path]

path to the pg_service.conf. If None the conf_path() is used, defaults to None

None

Raises:

Type Description
ServiceFileNotFound

when the service file is not found

ServiceNotFound

when the service is not found

Source code in pgserviceparser/__init__.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def write_service_setting(
    service_name: str,
    setting_key: str,
    setting_value: str,
    conf_file_path: Optional[Path] = None,
):
    """Writes a service setting to the service file.

    Args:
        service_name: service name
        setting_key: key
        setting_value: value
        conf_file_path: path to the pg_service.conf. If None the `conf_path()` is used, defaults to None

    Raises:
        ServiceFileNotFound: when the service file is not found
        ServiceNotFound: when the service is not found
    """

    config = full_config(conf_file_path)
    if service_name not in config:
        raise ServiceNotFound(service_name)

    config[service_name][setting_key] = setting_value
    with open(conf_file_path or conf_path(), "w") as configfile:
        config.write(configfile, space_around_delimiters=False)