Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

f-string-ify #150

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test_and_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
runs-on: ubuntu-20.04
strategy:
matrix:
python-version: ['3.5', '3.6', '3.7']
python-version: ['3.6', '3.7']

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"setproctitle": ["setproctitle"],
"dnspython": ["dnspython"],
},
python_requires=">=3.5",
python_requires=">=3.6",
license='GPL v3 or later',
url="https://github.com/dlenski/vpn-slice",
packages=["vpn_slice"],
Expand Down
96 changes: 48 additions & 48 deletions vpn_slice/__main__.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions vpn_slice/freebsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
class ProcfsProvider(PosixProcessProvider):
def pid2exe(self, pid):
try:
return os.readlink('/proc/%d/file' % pid)
return os.readlink(f'/proc/{pid}/file')
except OSError:
return None

def ppid_of(self, pid=None):
if pid is None:
return os.getppid()
try:
return int(next(open('/proc/%d/status' % pid)).split()[3])
return int(next(open(f'/proc/{pid}/status')).split()[3])
except (OSError, ValueError):
return None
4 changes: 2 additions & 2 deletions vpn_slice/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
class ProcfsProvider(PosixProcessProvider):
def pid2exe(self, pid):
try:
return os.readlink('/proc/%d/exe' % pid)
return os.readlink(f'/proc/{pid}/exe')
except OSError:
return None

def ppid_of(self, pid=None):
if pid is None:
return os.getppid()
try:
return int(next(open('/proc/%d/stat' % pid)).split()[3])
return int(next(open(f'/proc/{pid}/stat')).split()[3])
except (OSError, ValueError):
return None

Expand Down
12 changes: 6 additions & 6 deletions vpn_slice/mac.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ def configure_domain_vpn_dns(self, domains, nameservers):
if not os.path.exists('/etc/resolver'):
os.makedirs('/etc/resolver')
for domain in domains:
resolver_file_name = "/etc/resolver/{0}".format(domain)
resolver_file_name = f"/etc/resolver/{domain}"
with open(resolver_file_name, "w") as resolver_file:
for nameserver in nameservers:
resolver_file.write("nameserver {}\n".format(nameserver))
resolver_file.write(f"nameserver {nameserver}\n")

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

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

# reload config file
self._reload_conf()
Expand All @@ -191,7 +191,7 @@ def configure_firewall(self, device):

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

with open(self._PF_CONF_FILE, 'r') as file:
Expand Down
18 changes: 9 additions & 9 deletions vpn_slice/posix.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def lookup_host(self, hostname, keep_going=True):
search_domains = self.search_domains

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

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

Expand All @@ -43,7 +43,7 @@ def lookup_host(self, hostname, keep_going=True):
all_cls = []
if search_domains:
for cl in some_cls:
all_cls.extend(cl + ['+domain={!s}'.format(sd)] + field_requests for sd in search_domains)
all_cls.extend(cl + [f'+domain={sd!s}'] + field_requests for sd in search_domains)
else:
for cl in some_cls:
all_cls.extend([cl + field_requests])
Expand Down Expand Up @@ -71,10 +71,10 @@ def lookup_srv(self, query):
bind_addresses = self.bind_addresses

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion vpn_slice/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def get_executable(path):
path = which(os.path.basename(path)) or path
if not os.access(path, os.X_OK):
raise OSError('cannot execute {}'.format(path))
raise OSError(f'cannot execute {path}')
return path


Expand Down
Loading