Skip to content

Commit f257bcd

Browse files
committed
lint and update
1 parent 11242b1 commit f257bcd

File tree

9 files changed

+91
-281
lines changed

9 files changed

+91
-281
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ jobs:
3434
poetry run pylama dnscatz
3535
- name: Pytest
3636
run: |
37-
poetry run pytest --isort --black
37+
poetry run pytest --isort --ruff --ruff-format

dnscatz/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import pkg_resources
1+
from importlib.metadata import version
22

3-
__version__ = pkg_resources.get_distribution("dnscatz").version
3+
__version__ = version("dnscatz")

dnscatz/catz.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import logging
23
import os
34
import re
@@ -70,10 +71,8 @@ def parse_config_catalog_zone(
7071
zone = None
7172
if zonefile := zone_dict.get("zonefile"):
7273
zonefile = os.path.join(cwd, zonefile) if cwd else zonefile
73-
try:
74+
with contextlib.suppress(FileNotFoundError):
7475
zone = dns.zone.from_file(zonefile, origin=name)
75-
except FileNotFoundError:
76-
pass
7776
else:
7877
zonefile = None
7978

dnscatz/catz2nsd.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"""
1717

1818
import argparse
19+
import contextlib
1920
import logging
2021
import os
2122
import re
@@ -71,14 +72,13 @@ def parse_config(
7172
def get_current_zones(filename: str) -> Dict[str, str]:
7273
"""Get dictionary of current zones and patterns"""
7374
res = {}
74-
try:
75-
for line in open(filename).readlines():
76-
if line.startswith("#"):
77-
continue
78-
if match := re.match(r"^add (\S+) (\w+)$", line.rstrip()):
79-
res[match.group(1).lower()] = match.group(2)
80-
except FileNotFoundError:
81-
pass
75+
with contextlib.suppress(FileNotFoundError): # noqa
76+
with open(filename) as fp:
77+
for line in fp.readlines():
78+
if line.startswith("#"):
79+
continue
80+
if match := re.match(r"^add (\S+) (\w+)$", line.rstrip()):
81+
res[match.group(1).lower()] = match.group(2)
8282
return res
8383

8484

dnscatz/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
def read_multidicts(filename: str) -> List[dict]:
88
"""Read multiple YAML dictionaries from file, return list of them"""
9-
return parse_multidicts(open(filename).read())
9+
with open(filename) as fp:
10+
return parse_multidicts(fp.read())
1011

1112

1213
def parse_multidicts(config: str) -> List[dict]:

dnscatz/zones2catz.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626

2727
def generate_catalog_zone(
28-
origin: str, zones: List[str] = [], zonelist: Optional[str] = None
28+
origin: str, zones: Optional[List[str]] = None, zonelist: Optional[str] = None
2929
) -> str:
3030
buf = StringIO()
3131
serial = int(time.time())
@@ -52,14 +52,14 @@ def generate_catalog_zone(
5252
print(f"{origin} {DEFAULT_TTL} IN NS invalid.")
5353
print(f'version.{origin} {DEFAULT_TTL} IN TXT "{CATZ_VERSION}"')
5454

55-
for zone in zones:
55+
for zone in zones or []:
5656
if not zone.endswith("."):
5757
zone += "."
5858
zone_id = uuid.uuid5(uuid.NAMESPACE_DNS, zone)
5959
print(f"{zone_id}.zones.{origin} {DEFAULT_TTL} IN PTR {zone}")
6060

6161
if zonelist:
62-
with open(zonelist, mode="r") as csv_file:
62+
with open(zonelist) as csv_file:
6363
csv_reader = csv.DictReader(csv_file, fieldnames=["zone", "group"])
6464
for row in csv_reader:
6565
zone = row["zone"].strip()
@@ -108,7 +108,7 @@ def main() -> None:
108108
catalog_zone_str = generate_catalog_zone(origin=origin, zonelist=args.zonelist)
109109

110110
if args.output:
111-
with open(args.output, "wt") as output_file:
111+
with open(args.output, "w") as output_file:
112112
output_file.write(catalog_zone_str)
113113
else:
114114
print(catalog_zone_str)

0 commit comments

Comments
 (0)