summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorChristian Poessinger <christian@poessinger.com>2019-08-31 18:49:29 +0200
committerChristian Poessinger <christian@poessinger.com>2019-08-31 18:49:29 +0200
commitf524254e0c0c5e5954ca3f2e939f6b510667ad8a (patch)
tree97f700c0ab1b9b70bb87404159439ec48551364b /python
parent3a77a3f03068d4f7b2a51ecffc5f2aeb578c58ad (diff)
downloadvyos-1x-f524254e0c0c5e5954ca3f2e939f6b510667ad8a.tar.gz
vyos-1x-f524254e0c0c5e5954ca3f2e939f6b510667ad8a.zip
Python/ifconfig: T1557: use read/write helpers to interface with sysfs
Diffstat (limited to 'python')
-rw-r--r--python/vyos/ifconfig.py34
1 files changed, 25 insertions, 9 deletions
diff --git a/python/vyos/ifconfig.py b/python/vyos/ifconfig.py
index 347df3318..7ae1d71bb 100644
--- a/python/vyos/ifconfig.py
+++ b/python/vyos/ifconfig.py
@@ -118,6 +118,28 @@ class Interface:
# add exception handling code
pass
+ def _read_sysfs(self, filename):
+ """
+ Provide a single primitive w/ error checking for reading from sysfs.
+ """
+ var = None
+ with open(filename, 'r') as f:
+ var = f.read().rstrip('\n')
+
+ self._debug_msg('read "{}" <- "{}"'.format(value, filename))
+ return var
+
+
+ def _write_sysfs(self, filename, value):
+ """
+ Provide a single primitive w/ error checking for writing to sysfs.
+ """
+ with open(filename, 'w') as f:
+ f.write(str(value))
+
+ self._debug_msg('write "{}" -> "{}"'.format(value, filename))
+ return None
+
@property
def mtu(self):
@@ -311,11 +333,7 @@ class Interface:
>>> Interface('eth0').ifalias
''
"""
-
- alias = ''
- with open('/sys/class/net/{0}/ifalias'.format(self._ifname), 'r') as f:
- alias = f.read().rstrip('\n')
- return alias
+ return self._read_sysfs('/sys/class/net/{0}/ifalias'.format(self._ifname))
@ifalias.setter
@@ -336,13 +354,11 @@ class Interface:
>>> Interface('eth0').ifalias
''
"""
-
- # clear interface alias
if not ifalias:
+ # clear interface alias
ifalias = '\0'
- with open('/sys/class/net/{0}/ifalias'.format(self._ifname), 'w') as f:
- f.write(str(ifalias))
+ self._write_sysfs('/sys/class/net/{0}/ifalias'.format(self._ifname), ifalias)
@property