From f524254e0c0c5e5954ca3f2e939f6b510667ad8a Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sat, 31 Aug 2019 18:49:29 +0200 Subject: Python/ifconfig: T1557: use read/write helpers to interface with sysfs --- python/vyos/ifconfig.py | 34 +++++++++++++++++++++++++--------- 1 file 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 -- cgit v1.2.3