Skip to content

Commit

Permalink
fixup! Lint and format code with Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
pgiraud committed Nov 27, 2024
1 parent 60596b4 commit 00d496c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ ignore = [

[lint.isort]
known-first-party = ["pgtoolkit"]

[format]
docstring-code-format = true
37 changes: 20 additions & 17 deletions pgtoolkit/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,33 +351,34 @@ def __str__(self) -> str:
class EntriesProxy(dict[str, Entry]):
"""Proxy object used during Configuration edition.
>>> p = EntriesProxy(port=Entry('port', '5432'),
... shared_buffers=Entry('shared_buffers', '1GB'))
>>> p = EntriesProxy(
... port=Entry("port", "5432"), shared_buffers=Entry("shared_buffers", "1GB")
... )
Existing entries can be edited:
>>> p['port'].value = '5433'
>>> p["port"].value = "5433"
New entries can be added as:
>>> p.add('listen_addresses', '*', commented=True, comment='IP address')
>>> p.add("listen_addresses", "*", commented=True, comment="IP address")
>>> p # doctest: +NORMALIZE_WHITESPACE
{'port': Entry(name='port', _value=5433, commented=False, comment=None),
'shared_buffers': Entry(name='shared_buffers', _value='1GB', commented=False, comment=None),
'listen_addresses': Entry(name='listen_addresses', _value='*', commented=True, comment='IP address')}
>>> del p['shared_buffers']
>>> del p["shared_buffers"]
>>> p # doctest: +NORMALIZE_WHITESPACE
{'port': Entry(name='port', _value=5433, commented=False, comment=None),
'listen_addresses': Entry(name='listen_addresses', _value='*', commented=True, comment='IP address')}
Adding an existing entry fails:
>>> p.add('port', 5433)
>>> p.add("port", 5433)
Traceback (most recent call last):
...
ValueError: 'port' key already present
So does adding a value to the underlying dict:
>>> p['bonjour_name'] = 'pgserver'
>>> p["bonjour_name"] = "pgserver"
Traceback (most recent call last):
...
TypeError: cannot set a key
Expand Down Expand Up @@ -407,16 +408,16 @@ class Configuration:
You can access parameter using attribute or dictionary syntax.
>>> conf = parse(['port=5432\n', 'pg_stat_statement.min_duration = 3s\n'])
>>> conf = parse(["port=5432\n", "pg_stat_statement.min_duration = 3s\n"])
>>> conf.port
5432
>>> conf.port = 5433
>>> conf.port
5433
>>> conf['port'] = 5434
>>> conf["port"] = 5434
>>> conf.port
5434
>>> conf['pg_stat_statement.min_duration'].total_seconds()
>>> conf["pg_stat_statement.min_duration"].total_seconds()
3.0
>>> conf.get("ssl")
>>> conf.get("ssl", False)
Expand Down Expand Up @@ -621,12 +622,14 @@ def edit(self) -> Iterator[EntriesProxy]:
>>> import sys
>>> cfg = Configuration()
>>> includes = cfg.parse([
... "#listen_addresses = 'localhost' # what IP address(es) to listen on;\n",
... " # comma-separated list of addresses;\n",
... "port = 5432 # (change requires restart)\n",
... "max_connections = 100 # (change requires restart)\n",
... ])
>>> includes = cfg.parse(
... [
... "#listen_addresses = 'localhost' # what IP address(es) to listen on;\n",
... " # comma-separated list of addresses;\n",
... "port = 5432 # (change requires restart)\n",
... "max_connections = 100 # (change requires restart)\n",
... ]
... )
>>> list(includes)
[]
>>> cfg.save(sys.stdout)
Expand All @@ -638,7 +641,7 @@ def edit(self) -> Iterator[EntriesProxy]:
>>> with cfg.edit() as entries:
... entries["port"].value = 2345
... entries["port"].comment = None
... entries["listen_addresses"].value = '*'
... entries["listen_addresses"].value = "*"
... del entries["max_connections"]
... entries.add(
... "unix_socket_directories",
Expand Down
8 changes: 4 additions & 4 deletions pgtoolkit/ctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,13 @@ def parse_control_data(lines: Sequence[str]) -> dict[str, str]:
def num_version(text_version: str) -> int:
"""Return PostgreSQL numeric version as defined by LibPQ PQserverVersion
>>> num_version('pg_ctl (PostgreSQL) 9.6.3')
>>> num_version("pg_ctl (PostgreSQL) 9.6.3")
90603
>>> num_version('pg_ctl (PostgreSQL) 9.2.0')
>>> num_version("pg_ctl (PostgreSQL) 9.2.0")
90200
>>> num_version('pg_ctl (PostgreSQL) 11.10')
>>> num_version("pg_ctl (PostgreSQL) 11.10")
110010
>>> num_version('pg_ctl (PostgreSQL) 11.1')
>>> num_version("pg_ctl (PostgreSQL) 11.1")
110001
>>> num_version("pg_ctl (PostgreSQL) 14devel")
140000
Expand Down
6 changes: 3 additions & 3 deletions pgtoolkit/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ class Service(dict[str, Parameter]):
Each parameters can be accessed either as a dictionary entry or as an
attributes.
>>> myservice = Service('myservice', {'dbname': 'mydb'}, host='myhost')
>>> myservice = Service("myservice", {"dbname": "mydb"}, host="myhost")
>>> myservice.name
'myservice'
>>> myservice.dbname
'mydb'
>>> myservice['dbname']
>>> myservice["dbname"]
'mydb'
>>> myservice.user = 'myuser'
>>> myservice.user = "myuser"
>>> list(sorted(myservice.items()))
[('dbname', 'mydb'), ('host', 'myhost'), ('user', 'myuser')]
Expand Down
3 changes: 1 addition & 2 deletions tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def test_parse_value():
assert "user=foo password=secret'" == parse_value("'user=foo password=secret'''")
assert (
# this one does not work in parse_dsn()
"user=foo password='secret"
== parse_value("'user=foo password=''secret'")
"user=foo password='secret" == parse_value("'user=foo password=''secret'")
)
assert "%m [%p] %q%u@%d " == parse_value(r"'%m [%p] %q%u@%d '")
assert "124.7MB" == parse_value("124.7MB")
Expand Down
4 changes: 1 addition & 3 deletions tests/test_hba.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def test_parse_local_line():
with pytest.raises(AttributeError):
record.address

wanted = (
"local all all trust" # noqa
)
wanted = "local all all trust" # noqa
assert wanted == str(record)


Expand Down
16 changes: 4 additions & 12 deletions tests/test_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def test_parse():
\tORDER BY 1;
2018-06-15 10:49:26.088 UTC [8420]: [4-1] app=psql,db=postgres,client=[local],user=postgres LOG: disconnection: session time: 0:00:00.006 user=postgres database=postgres host=[local]
BAD PREFIX 10:49:31.140 UTC [8423]: [1-1] app=[unknown],db=[unknown],client=[local],user=[unknown] LOG: connection received: host=[local]
""".splitlines(
True
) # noqa
""".splitlines(True) # noqa

log_line_prefix = "%m [%p]: [%l-1] app=%a,db=%d,client=%h,user=%u "
records = list(parse(lines, prefix_fmt=log_line_prefix))
Expand Down Expand Up @@ -57,9 +55,7 @@ def test_group_lines():
\tORDER BY 1;
2018-06-15 10:49:26.088 UTC [8420]: [4-1] app=psql,db=postgres,client=[local],user=postgres LOG: disconnection: session time: 0:00:00.006 user=postgres database=postgres host=[local]
2018-06-15 10:49:31.140 UTC [8423]: [1-1] app=[unknown],db=[unknown],client=[local],user=[unknown] LOG: connection received: host=[local]
""".splitlines(
True
) # noqa
""".splitlines(True) # noqa

groups = list(group_lines(lines))
assert 7 == len(groups)
Expand Down Expand Up @@ -128,9 +124,7 @@ def test_record_stage1_ok():
\t pg_catalog.array_to_string(d.datacl, E'\\n') AS "Access privileges"
\tFROM pg_catalog.pg_database d
\tORDER BY 1;
""".splitlines(
True
) # noqa
""".splitlines(True) # noqa

record = Record.parse_stage1(lines)
assert "LOG" in repr(record)
Expand Down Expand Up @@ -173,9 +167,7 @@ def test_filters():
stage1 LOG: duration: 1002.209 ms statement: select pg_sleep(1);
stage2 LOG: duration: 0.223 ms statement: show log_timezone;
stage3 LOG: connection authorized: user=postgres database=postgres
""".splitlines(
True
) # noqa
""".splitlines(True) # noqa

class MyFilters(NoopFilters):
def stage1(self, record):
Expand Down

0 comments on commit 00d496c

Please sign in to comment.