Browse Source

f-strings for EtcHostsProvider

pull/768/head
Ross McFarland 4 years ago
parent
commit
4be12860d5
No known key found for this signature in database GPG Key ID: 943B179E15D3B22A
1 changed files with 10 additions and 11 deletions
  1. +10
    -11
      octodns/provider/etc_hosts.py

+ 10
- 11
octodns/provider/etc_hosts.py View File

@ -29,7 +29,7 @@ class EtcHostsProvider(BaseProvider):
SUPPORTS = set(('A', 'AAAA', 'ALIAS', 'CNAME')) SUPPORTS = set(('A', 'AAAA', 'ALIAS', 'CNAME'))
def __init__(self, id, directory, *args, **kwargs): def __init__(self, id, directory, *args, **kwargs):
self.log = logging.getLogger('EtcHostsProvider[{}]'.format(id))
self.log = logging.getLogger(f'EtcHostsProvider[{id}]')
self.log.debug('__init__: id=%s, directory=%s', id, directory) self.log.debug('__init__: id=%s, directory=%s', id, directory)
super(EtcHostsProvider, self).__init__(id, *args, **kwargs) super(EtcHostsProvider, self).__init__(id, *args, **kwargs)
self.directory = directory self.directory = directory
@ -67,24 +67,25 @@ class EtcHostsProvider(BaseProvider):
if not isdir(self.directory): if not isdir(self.directory):
makedirs(self.directory) makedirs(self.directory)
filename = '{}hosts'.format(path.join(self.directory, desired.name))
filepath = path.join(self.directory, desired.name)
filename = f'{filepath}hosts'
self.log.info('_apply: filename=%s', filename) self.log.info('_apply: filename=%s', filename)
with open(filename, 'w') as fh: with open(filename, 'w') as fh:
fh.write('##################################################\n') fh.write('##################################################\n')
fh.write('# octoDNS {} {}\n'.format(self.id, desired.name))
fh.write(f'# octoDNS {self.id} {desired.name}\n')
fh.write('##################################################\n\n') fh.write('##################################################\n\n')
if values: if values:
fh.write('## A & AAAA\n\n') fh.write('## A & AAAA\n\n')
for fqdn, value in sorted(values.items()): for fqdn, value in sorted(values.items()):
if fqdn[0] == '*': if fqdn[0] == '*':
fh.write('# ') fh.write('# ')
fh.write('{}\t{}\n\n'.format(value, fqdn))
fh.write(f'{value}\t{fqdn}\n\n')
if cnames: if cnames:
fh.write('\n## CNAME (mapped)\n\n') fh.write('\n## CNAME (mapped)\n\n')
for fqdn, value in sorted(cnames.items()): for fqdn, value in sorted(cnames.items()):
# Print out a comment of the first level # Print out a comment of the first level
fh.write('# {} -> {}\n'.format(fqdn, value))
fh.write(f'# {fqdn} -> {value}\n')
seen = set() seen = set()
while True: while True:
seen.add(value) seen.add(value)
@ -92,7 +93,7 @@ class EtcHostsProvider(BaseProvider):
value = values[value] value = values[value]
# If we're here we've found the target, print it # If we're here we've found the target, print it
# and break the loop # and break the loop
fh.write('{}\t{}\n'.format(value, fqdn))
fh.write(f'{value}\t{fqdn}\n')
break break
except KeyError: except KeyError:
# Try and step down one level # Try and step down one level
@ -102,15 +103,13 @@ class EtcHostsProvider(BaseProvider):
if value: if value:
if value in seen: if value in seen:
# We'd loop here, break it # We'd loop here, break it
fh.write('# {} -> {} **loop**\n'
.format(orig, value))
fh.write(f'# {orig} -> {value} **loop**\n')
break break
else: else:
fh.write('# {} -> {}\n'
.format(orig, value))
fh.write(f'# {orig} -> {value}\n')
else: else:
# Don't have anywhere else to go # Don't have anywhere else to go
fh.write('# {} -> **unknown**\n'.format(orig))
fh.write(f'# {orig} -> **unknown**\n')
break break
fh.write('\n') fh.write('\n')

Loading…
Cancel
Save