Skip to content

Commit

Permalink
Improvement spaces at attrs value (#41)
Browse files Browse the repository at this point in the history
Co-authored-by: luzik <luzikos@gmail.com>
Co-authored-by: Igor Mineev <mineev@cryptopro.ru>
  • Loading branch information
3 people committed Mar 24, 2021
1 parent 5d15105 commit 1f409c2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 61 deletions.
45 changes: 36 additions & 9 deletions debinterface/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,13 @@ def setDnsNameservers(self, nameservers):
""" Set the dns nameservers on the interface.
Args:
nameservers (str): the IP as a string
nameservers (str, list): the IP as a string
Raises:
ValueError: if there is a validation error
"""

if isinstance(nameservers, str):
nameservers = nameservers.split()
self._validator.validate_one(
'dns-nameservers', VALID_OPTS['dns-nameservers'], nameservers)
self._ifAttributes['dns-nameservers'] = nameservers
Expand All @@ -246,11 +247,13 @@ def setDnsSearch(self, searchUri):
""" Set the dns default search URI.
Args:
searchURI (str): The default search domain
searchURI (str, list): The default search domain
Raises:
ValueError: if there is a validation error
"""
if isinstance(searchUri, str):
searchUri = searchUri.split()
self._validator.validate_one(
'dns-search', VALID_OPTS['dns-search'], searchUri)
self._ifAttributes['dns-search'] = searchUri
Expand Down Expand Up @@ -310,15 +313,19 @@ def setUp(self, up):
"""
if isinstance(up, list):
self._ifAttributes['up'] = up
elif isinstance(up, str):
self._ifAttributes['up'] = up.split()
else:
self._ifAttributes['up'] = [up]
raise ValueError("Invalid value type {0}, expected str or List[str]".format(type(up)))

def appendUp(self, cmd):
"""Append a shell command to run when the interface is up.
Args:
cmd (str): a shell command
"""
if isinstance(cmd, str):
cmd = cmd.split()
self._ensure_list(self._ifAttributes, "up", cmd)

def setDown(self, down):
Expand All @@ -329,15 +336,19 @@ def setDown(self, down):
"""
if isinstance(down, list):
self._ifAttributes['down'] = down
elif isinstance(down, str):
self._ifAttributes['down'] = down.split()
else:
self._ifAttributes['down'] = [down]
raise ValueError("Invalid value type {0}, expected str or List[str]".format(type(down)))

def appendDown(self, cmd):
"""Append a shell command to run when the interface is down.
Args:
cmd (str): a shell command
"""
if isinstance(cmd, str):
cmd = cmd.split()
self._ensure_list(self._ifAttributes, "down", cmd)

def setPreUp(self, pre):
Expand All @@ -348,15 +359,19 @@ def setPreUp(self, pre):
"""
if isinstance(pre, list):
self._ifAttributes['pre-up'] = pre
elif isinstance(pre, str):
self._ifAttributes['pre-up'] = pre.split()
else:
self._ifAttributes['pre-up'] = [pre]
raise ValueError("Invalid value type {0}, expected str or List[str]".format(type(pre)))

def appendPreUp(self, cmd):
"""Append a shell command to run when the interface is pre-up.
Args:
cmd (str): a shell command
"""
if isinstance(cmd, str):
cmd = cmd.split()
self._ensure_list(self._ifAttributes, "pre-up", cmd)

def setPreDown(self, pre):
Expand All @@ -367,15 +382,19 @@ def setPreDown(self, pre):
"""
if isinstance(pre, list):
self._ifAttributes['pre-down'] = pre
elif isinstance(pre, str):
self._ifAttributes['pre-down'] = pre.split()
else:
self._ifAttributes['pre-down'] = [pre]
raise ValueError("Invalid value type {0}, expected str or List[str]".format(type(pre)))

def appendPreDown(self, cmd):
"""Append a shell command to run when the interface is pre-down.
Args:
cmd (str): a shell command
"""
if isinstance(cmd, str):
cmd = cmd.split()
self._ensure_list(self._ifAttributes, "pre-down", cmd)

def setPostUp(self, post):
Expand All @@ -386,15 +405,19 @@ def setPostUp(self, post):
"""
if isinstance(post, list):
self._ifAttributes['post-up'] = post
elif isinstance(post, str):
self._ifAttributes['post-up'] = post.split()
else:
self._ifAttributes['post-up'] = [post]
raise ValueError("Invalid value type {0}, expected str or List[str]".format(type(post)))

def appendPostUp(self, cmd):
"""Append a shell command to run when the interface is post-up.
Args:
cmd (str): a shell command
"""
if isinstance(cmd, str):
cmd = cmd.split()
self._ensure_list(self._ifAttributes, "post-up", cmd)

def setPostDown(self, post):
Expand All @@ -405,15 +428,19 @@ def setPostDown(self, post):
"""
if isinstance(post, list):
self._ifAttributes['post-down'] = post
elif isinstance(post, str):
self._ifAttributes['post-down'] = post.split()
else:
self._ifAttributes['post-down'] = [post]
raise ValueError("Invalid value type {0}, expected str or List[str]".format(type(post)))

def appendPostDown(self, cmd):
"""Append a shell command to run when the interface is post-down.
Args:
cmd (str): a shell command
"""
if isinstance(cmd, str):
cmd = cmd.split()
self._ensure_list(self._ifAttributes, "post-down", cmd)

def setUnknown(self, key, val):
Expand Down
88 changes: 36 additions & 52 deletions debinterface/interfacesReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,60 +99,44 @@ def _parse_iface(self, line, current_path):

def _parse_details(self, line):
if line[0].isspace() is True:
sline = [x.strip() for x in line.split()]

if sline[0] == 'address':
self._adapters[self._context].setAddress(sline[1])
elif sline[0] == 'netmask':
self._adapters[self._context].setNetmask(sline[1])
elif sline[0] == 'gateway':
self._adapters[self._context].setGateway(sline[1])
elif sline[0] == 'broadcast':
self._adapters[self._context].setBroadcast(sline[1])
elif sline[0] == 'network':
self._adapters[self._context].setNetwork(sline[1])
elif sline[0] == 'hostapd':
self._adapters[self._context].setHostapd(sline[1])
elif sline[0] == 'wpa-conf':
self._adapters[self._context].setWpaConf(sline[1])
elif sline[0] == 'dns-nameservers':
nameservers = sline
del nameservers[0]
if len(nameservers) == 1:
nameservers = nameservers[0]
self._adapters[self._context].setDnsNameservers(nameservers)
elif sline[0] == 'dns-search':
searchUri = sline
del searchUri[0]
self._adapters[self._context].setDnsSearch(searchUri)
elif sline[0].startswith('bridge') is True:
opt = sline[0].split('_')
sline.pop(0)
ifs = " ".join(sline)
self._adapters[self._context].replaceBropt(opt[1], ifs)
elif (sline[0] == 'up'
or sline[0] == 'down'
or sline[0] == 'pre-up'
or sline[0] == 'pre-down'
or sline[0] == 'post-up'
or sline[0] == 'post-down'):
ud = sline.pop(0)
cmd = ' '.join(sline)
if ud == 'up':
self._adapters[self._context].appendUp(cmd)
elif ud == 'down':
self._adapters[self._context].appendDown(cmd)
elif ud == 'pre-up':
self._adapters[self._context].appendPreUp(cmd)
elif ud == 'pre-down':
self._adapters[self._context].appendPreDown(cmd)
elif ud == 'post-up':
self._adapters[self._context].appendPostUp(cmd)
elif ud == 'post-down':
self._adapters[self._context].appendPostDown(cmd)
keyword, value = line.strip().split(None, 1)

if keyword == 'address':
self._adapters[self._context].setAddress(value)
elif keyword == 'netmask':
self._adapters[self._context].setNetmask(value)
elif keyword == 'gateway':
self._adapters[self._context].setGateway(value)
elif keyword == 'broadcast':
self._adapters[self._context].setBroadcast(value)
elif keyword == 'network':
self._adapters[self._context].setNetwork(value)
elif keyword == 'hostapd':
self._adapters[self._context].setHostapd(value)
elif keyword == 'wpa-conf':
self._adapters[self._context].setWpaConf(value)
elif keyword == 'dns-nameservers':
self._adapters[self._context].setDnsNameservers(value)
elif keyword == 'dns-search':
self._adapters[self._context].setDnsSearch(value)
elif keyword.startswith('bridge'):
_, option = keyword.replace('-', '_').split('_', maxsplit=1)
self._adapters[self._context].replaceBropt(option, value)
elif keyword == 'up':
self._adapters[self._context].appendUp(value)
elif keyword == 'down':
self._adapters[self._context].appendDown(value)
elif keyword == 'pre-up':
self._adapters[self._context].appendPreUp(value)
elif keyword == 'pre-down':
self._adapters[self._context].appendPreDown(value)
elif keyword == 'post-up':
self._adapters[self._context].appendPostUp(value)
elif keyword == 'post-down':
self._adapters[self._context].appendPostDown(value)
else:
# store as if so as not to loose it
self._adapters[self._context].setUnknown(sline[0], sline[1])
self._adapters[self._context].setUnknown(keyword, value)

def _read_auto(self, line):
""" Identify which adapters are flagged auto. """
Expand Down
1 change: 1 addition & 0 deletions test/interfaces.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ iface eth1 inet static
address 10.1.20.1
netmask 255.255.255.0
dns-nameservers 8.8.8.8
some-attr value contains space tab_underscore-dash


##########################################################
Expand Down
9 changes: 9 additions & 0 deletions test/test_interfacesReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,12 @@ def test_source_other_file(self):
self.assertEqual(adapters[1].interfaces_path, INF2_PATH) # in the sourced file
self.assertEqual(adapters[2].interfaces_path, INF2_PATH) # in the sourced file
self.assertEqual(adapters[3].interfaces_path, INF5_PATH) # before the source

def test_attrs_with_spaces(self):
reader = InterfacesReader(INF_PATH)
eth1 = next(
(x for x in reader.parse_interfaces() if x.attributes['name'] == 'eth1'),
None
)
self.assertIsNotNone(eth1)
self.assertEqual(eth1.attributes['unknown']['some-attr'], 'value contains space\ttab_underscore-dash')

0 comments on commit 1f409c2

Please sign in to comment.