summaryrefslogtreecommitdiffstats
path: root/glances/config.py
diff options
context:
space:
mode:
authornicolargo <nicolas@nicolargo.com>2017-12-26 17:04:57 +0100
committernicolargo <nicolas@nicolargo.com>2017-12-26 17:04:57 +0100
commit54b0c4e277c14454344e417163ae08aa37302ad8 (patch)
treefc3a70f9d52dc900f9ff667a636793f84c53d104 /glances/config.py
parent3bad3b39b6ed806e434dfda43256a81a1d6b4219 (diff)
Add dynamic fields in all sections of the configuration file #1204
Diffstat (limited to 'glances/config.py')
-rw-r--r--glances/config.py34
1 files changed, 28 insertions, 6 deletions
diff --git a/glances/config.py b/glances/config.py
index 48e5f68b..28d66127 100644
--- a/glances/config.py
+++ b/glances/config.py
@@ -23,8 +23,9 @@ import os
import sys
import multiprocessing
from io import open
+import re
-from glances.compat import ConfigParser, NoOptionError
+from glances.compat import ConfigParser, NoOptionError, system_exec
from glances.globals import BSD, LINUX, MACOS, SUNOS, WINDOWS
from glances.logger import logger
@@ -103,6 +104,9 @@ class Config(object):
self.config_filename = 'glances.conf'
self._loaded_config_file = None
+ # Re patern for optimize research of `foo`
+ self.re_pattern = re.compile('(\`.+?\`)')
+
self.parser = ConfigParser()
self.read()
@@ -257,17 +261,35 @@ class Config(object):
self.set_default(section, header + 'warning', cwc[1])
self.set_default(section, header + 'critical', cwc[2])
- def set_default(self, section, option, default):
+ def set_default(self, section, option,
+ default):
"""If the option did not exist, create a default value."""
if not self.parser.has_option(section, option):
self.parser.set(section, option, default)
- def get_value(self, section, option, default=None):
- """Get the value of an option, if it exists."""
+ def get_value(self, section, option,
+ default=None):
+ """Get the value of an option, if it exists.
+
+ If it did not exist, then return de default value.
+
+ It allows user to define dynamic configuration key (see issue#1204)
+ Dynamic vlaue should starts and end with the ` char
+ Example: prefix=`hostname`
+ """
+ ret = default
try:
- return self.parser.get(section, option)
+ ret = self.parser.get(section, option)
except NoOptionError:
- return default
+ pass
+
+ # Search a substring `foo` and replace it by the result of its exec
+ if ret is not None:
+ match = self.re_pattern.findall(ret)
+ for m in match:
+ ret = ret.replace(m, system_exec(m[1:-1]))
+
+ return ret
def get_int_value(self, section, option, default=0):
"""Get the int value of an option, if it exists."""