Skip to content

Commit 13d5f68

Browse files
committed
f-string-ify
Mostly-ish automated with https://github.com/ikamensh/flynt
1 parent bb05c06 commit 13d5f68

File tree

8 files changed

+70
-70
lines changed

8 files changed

+70
-70
lines changed

.github/workflows/test_and_release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
runs-on: ubuntu-20.04
4242
strategy:
4343
matrix:
44-
python-version: ['3.5', '3.6', '3.7']
44+
python-version: ['3.6', '3.7']
4545

4646
steps:
4747
- uses: actions/checkout@v3

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"setproctitle": ["setproctitle"],
3232
"dnspython": ["dnspython"],
3333
},
34-
python_requires=">=3.5",
34+
python_requires=">=3.6",
3535
license='GPL v3 or later',
3636
url="https://github.com/dlenski/vpn-slice",
3737
packages=["vpn_slice"],

vpn_slice/__main__.py

+48-48
Large diffs are not rendered by default.

vpn_slice/freebsd.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
class ProcfsProvider(PosixProcessProvider):
77
def pid2exe(self, pid):
88
try:
9-
return os.readlink('/proc/%d/file' % pid)
9+
return os.readlink(f'/proc/{pid}/file')
1010
except OSError:
1111
return None
1212

1313
def ppid_of(self, pid=None):
1414
if pid is None:
1515
return os.getppid()
1616
try:
17-
return int(next(open('/proc/%d/status' % pid)).split()[3])
17+
return int(next(open(f'/proc/{pid}/status')).split()[3])
1818
except (OSError, ValueError):
1919
return None

vpn_slice/linux.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
class ProcfsProvider(PosixProcessProvider):
1111
def pid2exe(self, pid):
1212
try:
13-
return os.readlink('/proc/%d/exe' % pid)
13+
return os.readlink(f'/proc/{pid}/exe')
1414
except OSError:
1515
return None
1616

1717
def ppid_of(self, pid=None):
1818
if pid is None:
1919
return os.getppid()
2020
try:
21-
return int(next(open('/proc/%d/stat' % pid)).split()[3])
21+
return int(next(open(f'/proc/{pid}/stat')).split()[3])
2222
except (OSError, ValueError):
2323
return None
2424

vpn_slice/mac.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ def configure_domain_vpn_dns(self, domains, nameservers):
119119
if not os.path.exists('/etc/resolver'):
120120
os.makedirs('/etc/resolver')
121121
for domain in domains:
122-
resolver_file_name = "/etc/resolver/{0}".format(domain)
122+
resolver_file_name = f"/etc/resolver/{domain}"
123123
with open(resolver_file_name, "w") as resolver_file:
124124
for nameserver in nameservers:
125-
resolver_file.write("nameserver {}\n".format(nameserver))
125+
resolver_file.write(f"nameserver {nameserver}\n")
126126

127127
def deconfigure_domain_vpn_dns(self, domains, nameservers):
128128
for domain in domains:
129-
resolver_file_name = "/etc/resolver/{0}".format(domain)
129+
resolver_file_name = f"/etc/resolver/{domain}"
130130
if os.path.exists(resolver_file_name):
131131
os.remove(resolver_file_name)
132132
if not len(os.listdir('/etc/resolver')):
@@ -167,10 +167,10 @@ def configure_firewall(self, device):
167167
if not enable_token:
168168
print("WARNING: failed to get pf enable reference token, packet filter might not shutdown correctly")
169169

170-
anchor = '{}/{}'.format(self._PF_ANCHOR, device)
170+
anchor = f'{self._PF_ANCHOR}/{device}'
171171
# add anchor to generate rules with
172172
with open(self._PF_CONF_FILE, 'a') as file:
173-
file.write('anchor "{}" # vpn-slice-{} AUTOCREATED {}\n'.format(anchor, device, enable_token))
173+
file.write(f'anchor "{anchor}" # vpn-slice-{device} AUTOCREATED {enable_token}\n')
174174

175175
# reload config file
176176
self._reload_conf()
@@ -191,7 +191,7 @@ def configure_firewall(self, device):
191191

192192
def deconfigure_firewall(self, device):
193193
# disable anchor
194-
anchor = '{}/{}'.format(self._PF_ANCHOR, device)
194+
anchor = f'{self._PF_ANCHOR}/{device}'
195195
subprocess.check_call([self.pfctl, '-a', anchor, '-F', 'all'])
196196

197197
with open(self._PF_CONF_FILE, 'r') as file:

vpn_slice/posix.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def lookup_host(self, hostname, keep_going=True):
2020
search_domains = self.search_domains
2121

2222
if not bind_addresses:
23-
some_cls = [self.base_cl + ['@{!s}'.format(dns) for dns in dns_servers]]
23+
some_cls = [self.base_cl + [f'@{dns!s}' for dns in dns_servers]]
2424
field_requests = [hostname, 'A', hostname, 'AAAA']
2525
else:
2626
some_cls = []
@@ -32,7 +32,7 @@ def lookup_host(self, hostname, keep_going=True):
3232
field_requests.extend([hostname, ('AAAA' if bind.version == 6 else 'A')])
3333

3434
# We can only do a lookup via DNS-over-IPv[X] if we have an IPv[X] address to bind to.
35-
matching_dns = ['@{!s}'.format(dns) for dns in dns_servers if dns.version == bind.version]
35+
matching_dns = [f'@{dns!s}' for dns in dns_servers if dns.version == bind.version]
3636
if matching_dns:
3737
some_cls.append(self.base_cl + ['-b', str(bind)] + matching_dns)
3838

@@ -43,7 +43,7 @@ def lookup_host(self, hostname, keep_going=True):
4343
all_cls = []
4444
if search_domains:
4545
for cl in some_cls:
46-
all_cls.extend(cl + ['+domain={!s}'.format(sd)] + field_requests for sd in search_domains)
46+
all_cls.extend(cl + [f'+domain={sd!s}'] + field_requests for sd in search_domains)
4747
else:
4848
for cl in some_cls:
4949
all_cls.extend([cl + field_requests])
@@ -71,10 +71,10 @@ def lookup_srv(self, query):
7171
bind_addresses = self.bind_addresses
7272

7373
if not bind_addresses:
74-
all_cls = [self.base_cl + ['@{!s}'.format(dns) for dns in dns_servers] + [query, 'SRV']]
74+
all_cls = [self.base_cl + [f'@{dns!s}' for dns in dns_servers] + [query, 'SRV']]
7575
else:
7676
all_cls = [self.base_cl + ['-b', str(bind)] +
77-
['@{!s}'.format(n) for n in dns_servers if n.version == bind.version] +
77+
[f'@{n!s}' for n in dns_servers if n.version == bind.version] +
7878
[query, 'SRV'] for bind in bind_addresses]
7979

8080
result = set()
@@ -95,18 +95,18 @@ class HostsFileProvider(HostsProvider):
9595
def __init__(self, path):
9696
self.path = path
9797
if not os.access(path, os.R_OK | os.W_OK):
98-
raise OSError('Cannot read/write {}'.format(path))
98+
raise OSError(f'Cannot read/write {path}')
9999

100100
def write_hosts(self, host_map, name):
101-
tag = 'vpn-slice-{} AUTOCREATED'.format(name)
101+
tag = f'vpn-slice-{name} AUTOCREATED'
102102
with open(self.path, 'r+') as hostf:
103103
fcntl.flock(hostf, fcntl.LOCK_EX) # POSIX only, obviously
104104
lines = hostf.readlines()
105-
keeplines = [l for l in lines if not l.endswith('# %s\n' % tag)]
105+
keeplines = [l for l in lines if not l.endswith(f'# {tag}\n')]
106106
hostf.seek(0, 0)
107107
hostf.writelines(keeplines)
108108
for ip, names in host_map:
109-
print('%s %s\t\t# %s' % (ip, ' '.join(names), tag), file=hostf)
109+
print(f"{ip} {' '.join(names)}\t\t# {tag}", file=hostf)
110110
hostf.truncate()
111111
return len(host_map) or len(lines) - len(keeplines)
112112

vpn_slice/util.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def get_executable(path):
77
path = which(os.path.basename(path)) or path
88
if not os.access(path, os.X_OK):
9-
raise OSError('cannot execute {}'.format(path))
9+
raise OSError(f'cannot execute {path}')
1010
return path
1111

1212

0 commit comments

Comments
 (0)