From 440d0fb1115bbfb428132ef91804ae213ffca30f Mon Sep 17 00:00:00 2001 From: fmertz Date: Sun, 28 Jun 2020 21:21:27 +0000 Subject: system display: T2564: py code cleanup * src/conf_mode/system-display.py cleanup use of dictionary * data/templates/system-display/lcdproc.conf.tmpl cleanup, removed extra DateTime --- data/templates/system-display/lcdproc.conf.tmpl | 12 ++-- src/conf_mode/system-display.py | 75 +++++++++++++------------ 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/data/templates/system-display/lcdproc.conf.tmpl b/data/templates/system-display/lcdproc.conf.tmpl index 2fe491e63..92aee8efe 100644 --- a/data/templates/system-display/lcdproc.conf.tmpl +++ b/data/templates/system-display/lcdproc.conf.tmpl @@ -37,7 +37,6 @@ ReportToSyslog=true DisplayName="{%- if show['title'] %}{{ show['title'] }}{%- else %}VyOS{%- endif %}" {%- if show['host'] %} -{#%- for s in show['host'] %#} [CPU] # Show screen @@ -77,7 +76,6 @@ Active={%- if 'proc' in show['host'] %}true{%- else %}false{%- endif %} [Disk] # Show screen Active={%- if 'disk' in show['host'] %}true{%- else %}false{%- endif %} -{#%- endfor %#} {%- else %} {# if show['host'] #} {# Turn off sections that default active #} @@ -90,8 +88,6 @@ Active=false [Load] Active=false -[TimeDate] -Active=false {%- endif %} {# if show['host'] #} [TimeDate] @@ -118,10 +114,10 @@ TimeFormat="%H:%M" Active={%- if show['network']['interface'] %}true{%- else %}false{%- endif %} {%- for i in show['network']['interface'] %} # Show stats for Interface {{ i }} -Interface0={{ i }} +Interface{{ loop.index0 }}={{ i }} {%- if show['network']['interface'][i]['alias'] %} # Interface alias name to display [default: ] -Alias0={{ show['network']['interface'][i]['alias'] }} +Alias{{ loop.index0 }}={{ show['network']['interface'][i]['alias'] }} {%- endif %} {%- endfor %} @@ -139,7 +135,7 @@ unit=bit #transfer=TRUE {%- endif %} {# if show['network'] #} -{%- else %} {# if show #} +{%- else %}{# if show #} {# Turn off sections that default active #} [CPU] @@ -153,7 +149,7 @@ Active=false [TimeDate] Active=false -{%- endif %} +{%- endif %}{# if show #} [Battery] # Show screen diff --git a/src/conf_mode/system-display.py b/src/conf_mode/system-display.py index e30d591cc..557b7cf6a 100755 --- a/src/conf_mode/system-display.py +++ b/src/conf_mode/system-display.py @@ -15,7 +15,6 @@ # along with this program. If not, see . import os -import re from sys import exit @@ -28,53 +27,57 @@ from vyos import airbag airbag.enable() def get_config(): - c = Config() + # Return a (possibly empty) configuration dictionary + return Config().get_config_dict(['system', 'display']) - if not c.exists('system display'): - return None - - c.set_level('system display') - - return c.get_config_dict([]) - -def generate(c): - if c == None: +def generate(config_dict): + if not config_dict: return None # Render config file for daemon LCDd - render('/etc/LCDd.conf', 'system-display/LCDd.conf.tmpl', c) + render('/etc/LCDd.conf', 'system-display/LCDd.conf.tmpl', config_dict) # Render config file for client lcdproc - render('/etc/lcdproc.conf', 'system-display/lcdproc.conf.tmpl', c) + render('/etc/lcdproc.conf', 'system-display/lcdproc.conf.tmpl', config_dict) return None -def verify(c): - if c == None: +def verify(config_dict): + if not config_dict: return None - if c.get('model') == None: - raise ConfigError('For system display, a model is [REQUIRED]') + if 'model' not in config_dict: + raise ConfigError('Display model is [REQUIRED]') - if c.get('show') == None: - raise ConfigError('For system display, show cannot be empty') + if ( 'show' not in config_dict + or ( 'clock' not in config_dict['show'] + and 'network' not in config_dict['show'] + and 'host' not in config_dict['show'] + ) + ): + raise ConfigError('Display show must have a clock, host or network') - if 'network' in c['show'] and 'interface' not in c['show']['network']: - raise ConfigError('system display show network must have at least one interface') + if ( 'network' in config_dict['show'] + and 'interface' not in config_dict['show']['network'] + ): + raise ConfigError('Display show network must have an interface') - if 'network' in c['show'] and 'interface' in c['show']['network'] and len(c['show']['network']['interface']) > 3: - raise ConfigError('system display show network interface cannot have more than 3 entries') + if ( 'network' in config_dict['show'] + and 'interface' in config_dict['show']['network'] + and len(config_dict['show']['network']['interface']) > 3 + ): + raise ConfigError('Display show network cannot have > 3 interfaces') return None -def apply(c): - if not c or 'disabled' in c.keys(): - # Stop client first - run('systemctl stop lcdproc.service') - # Stop server next - return run('systemctl stop LCDd.service') - - # Stop client first +def apply(config_dict): + # Stop client run('systemctl stop lcdproc.service') - # Restart server next + + if not config_dict or 'disabled' in config_dict: + # Stop server + run('systemctl stop LCDd.service') + return None + + # Restart server run('systemctl restart LCDd.service') # Start client run('systemctl start lcdproc.service') @@ -83,10 +86,10 @@ def apply(c): if __name__ == '__main__': try: - c = get_config() - verify(c) - generate(c) - apply(c) + config_dict = get_config() + verify(config_dict) + generate(config_dict) + apply(config_dict) except ConfigError as e: print(e) exit(1) -- cgit v1.2.3