From: "André Erdmann" <dywi@mailerd.de>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/config/
Date: Sat, 9 Feb 2013 20:45:29 +0000 (UTC) [thread overview]
Message-ID: <1360440495.73a23ab7ced4890b5930ff27cc2a4759f3956594.dywi@gentoo> (raw)
commit: 73a23ab7ced4890b5930ff27cc2a4759f3956594
Author: André Erdmann <dywi <AT> mailerd <DOT> de>
AuthorDate: Sat Feb 9 19:20:16 2013 +0000
Commit: André Erdmann <dywi <AT> mailerd <DOT> de>
CommitDate: Sat Feb 9 20:08:15 2013 +0000
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=73a23ab7
config/loader: list of choices, value functions
* added the ability to load a "list of choices"
example config_entry_map entry <<
config_option = dict (
value_type = "list",
choices = frozenset (( "first", "second", )),
)
>>
* added the ability to convert values after validating their value_type via
'f_convert' and 'f_convert_item' in the config entry dict
* added the ability to verify the (final) value of a config option via
'f_verify' in the config entry dict
---
roverlay/config/loader.py | 144 +++++++++++++++++++++++++++++++++++----------
1 files changed, 113 insertions(+), 31 deletions(-)
diff --git a/roverlay/config/loader.py b/roverlay/config/loader.py
index e71770b..71f4074 100644
--- a/roverlay/config/loader.py
+++ b/roverlay/config/loader.py
@@ -22,6 +22,10 @@ from roverlay.config import fielddef
from roverlay.config.util import get_config_path
from roverlay.config.entrymap import CONFIG_ENTRY_MAP
+def listlike ( var ):
+ return hasattr ( var, '__iter__' ) and not isinstance ( var, str )
+# --- end of listlike (...) ---
+
class ConfigLoader ( object ):
"""Loads config data from files."""
@@ -73,41 +77,122 @@ class ConfigLoader ( object ):
* value -- value read from a config file (will be verified here)
* config_root -- ignored;
"""
- # determine the config path
- path = None
- if 'path' in cref:
- path = cref ['path']
- else:
- path = option.split ( '_' )
+ def uppercase_if_in_iterable ( s, iterable ):
+ """Returns s.upper() if s.upper() in iterable else None.
- path = get_config_path ( path )
+ arguments:
+ * s -- string
+ * iterable -- items
+ """
+ s_up = s.upper()
+ return s_up if s_up in iterable else None
+ # --- end of uppercase_if_in_iterable (...) ---
+
+ # determine the config path
+ path = get_config_path (
+ cref.get ( "path", None ) or option.split ( '_' )
+ )
# need a valid path
if path:
# verify and convert value if value_type is set
- if 'value_type' in cref and cref ['value_type']:
+ if cref.get ( "value_type", None ):
value = self._make_and_verify_value (
cref ['value_type'], value
)
-
- if 'choices' in cref and value not in cref ['choices']:
- if 'flags' in cref and 'CAPSLOCK' in cref ['flags']:
- v_up = value.upper()
- value = v_up if v_up in cref ['choices'] else None
- else:
+ # --- end prepare value;
+
+ value_choices = cref.get ( "choices", None )
+
+ if value_choices:
+ if listlike ( value ):
+
+ # create value_invalid for logging
+ # * also used as condition whether value is valid or not
+ value_invalid = None
+
+ if 'flags' in cref and 'CAPSLOCK' in cref ['flags']:
+ value_valid = list (
+ filter (
+ None,
+ (
+ uppercase_if_in_iterable ( v, value_choices )
+ for v in value
+ )
+ )
+ )
+
+ if len ( value_valid ) != len ( value ):
+
+ value_invalid = [
+ v for v in value if v.upper() not in value_choices
+ ]
+ else:
+ value_valid = [ v for v in value if v in value_choices ]
+
+ if len ( value_valid ) != len ( value ):
+ value_invalid = [
+ v for v in value if v not in value_choices
+ ]
+
+
+ if not value_invalid:
+ value = value_valid
+ else:
+ # mark value as invalid
+ self.logger.error (
+ "Option {o!r} has unusable value(s): {v!r}.".format (
+ o=option,
+ v=', '.join ( value_invalid )
+ )
+ )
+ #value = None
+ # return immediately, no need to log about that twice
+ return False
+
+
+ elif 'flags' in cref and 'CAPSLOCK' in cref ['flags']:
+ value = uppercase_if_in_iterable ( value, value_choices )
+
+ elif value not in value_choices:
value = None
+ # else value is valid
+
elif 'flags' in cref and 'CAPSLOCK' in cref ['flags']:
- value = value.upper()
+ if listlike ( value ):
+ value = [ s.upper() for s in value ]
+ # is a list
+ pass
+ else:
+ value = value.upper()
+ # --- end verify choices / apply flags;
# need a valid value
if value is not None:
- self.logger.debug (
- "New config entry %s with path %s and value %s." %
- ( option, path, value )
+ if listlike ( value ) and 'f_convert_item' in cref:
+ # or use map()
+ value = [ cref ['f_convert_item'] ( v ) for v in value ]
+
+ # not elif (use both functions for iterables if specified)
+ if 'f_convert' in cref:
+ # func should expect an iterable if value_type has/is "list"
+ value = cref ['f_convert'] ( value )
+
+ if value is not None and (
+ 'f_verify' not in cref
+ or cref ['f_verify'] (
+ value,
+ logger=self.logger.getChild ( option )
)
+ ):
+
+ self.logger.debug (
+ "New config entry {o} with path {p} and value {v}.".format (
+ o=option, p=path, v=value
+ ) )
# add option/value to the config
self._setval ( path, value )
@@ -115,9 +200,9 @@ class ConfigLoader ( object ):
return True
else:
self.logger.error (
- "Option '%s' has an unusable value '%s'." %
- ( option, value )
- )
+ "Option {o!r} has an unusable value {v!r}.".format (
+ o=option, v=value
+ ) )
return False
# ---
# --- end of _config_enty (...) ---
@@ -161,15 +246,11 @@ class ConfigLoader ( object ):
raise Exception ( "CONFIG_ENTRY_MAP is invalid!" )
# check if config entry is disabled
- if cref is None:
+ if cref is not None:
+ return self._config_entry ( cref, option, value, config_root )
+ else:
# deftly ignored
return True
-
- elif self._config_entry ( cref, option, value, config_root ):
- return True
- else:
- self.logger.error ( "Option '%s' is unusable..." % real_option )
- return False
# ---
self.logger.warning ( "Option '%s' is unknown." % real_option )
@@ -210,12 +291,13 @@ class ConfigLoader ( object ):
option, equal, value = nextline ()
- if fh:
- fh.close ()
-
except IOError as ioerr:
raise
+ finally:
+ if 'fh' in locals() and fh:
+ fh.close()
+
# --- end of load_config (...) ---
def load_field_definition ( self, def_file, lenient=False ):
next reply other threads:[~2013-02-09 20:45 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-02-09 20:45 André Erdmann [this message]
-- strict thread matches above, loose matches on Subject: below --
2018-05-30 5:31 [gentoo-commits] proj/R_overlay:master commit in: roverlay/config/ Benda XU
2018-05-28 8:41 Benda XU
2014-08-23 19:03 André Erdmann
2014-08-23 19:03 André Erdmann
2014-04-01 16:38 André Erdmann
2014-04-01 16:38 André Erdmann
2014-04-01 16:38 André Erdmann
2014-01-26 19:06 André Erdmann
2013-09-13 15:10 André Erdmann
2013-09-11 11:14 André Erdmann
2013-09-11 10:30 André Erdmann
2013-09-11 10:27 André Erdmann
2013-09-11 10:27 André Erdmann
2013-09-10 14:40 André Erdmann
2013-09-10 14:40 André Erdmann
2013-09-06 17:27 André Erdmann
2013-09-04 10:16 André Erdmann
2013-08-14 14:56 André Erdmann
2013-08-12 8:18 André Erdmann
2013-08-09 15:27 André Erdmann
2013-08-09 15:27 André Erdmann
2013-08-06 10:58 André Erdmann
2013-08-05 11:44 André Erdmann
2013-08-02 13:39 André Erdmann
2013-07-30 18:40 André Erdmann
2013-07-29 14:56 André Erdmann
2013-07-24 17:45 André Erdmann
2013-07-16 16:36 André Erdmann
2013-07-12 13:57 André Erdmann
2013-07-12 13:57 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-07-12 13:57 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-07-11 16:29 André Erdmann
2013-07-10 15:10 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-07-10 16:16 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-06-30 15:58 André Erdmann
2013-06-19 18:59 André Erdmann
2013-06-13 16:34 [gentoo-commits] proj/R_overlay:gsoc13/next " André Erdmann
2013-06-18 14:12 ` [gentoo-commits] proj/R_overlay:master " André Erdmann
2013-06-04 21:06 André Erdmann
2013-02-09 20:45 André Erdmann
2013-02-09 20:45 André Erdmann
2013-01-28 23:54 André Erdmann
2013-01-28 23:54 André Erdmann
2012-08-09 9:26 André Erdmann
2012-08-08 23:46 André Erdmann
2012-08-02 15:14 André Erdmann
2012-08-01 21:10 André Erdmann
2012-07-11 18:43 André Erdmann
2012-07-06 22:19 André Erdmann
2012-07-06 22:19 André Erdmann
2012-07-05 16:00 André Erdmann
2012-07-03 17:48 André Erdmann
2012-06-29 22:48 André Erdmann
2012-06-26 15:42 André Erdmann
2012-06-25 18:19 André Erdmann
2012-06-25 18:19 André Erdmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1360440495.73a23ab7ced4890b5930ff27cc2a4759f3956594.dywi@gentoo \
--to=dywi@mailerd.de \
--cc=gentoo-commits@lists.gentoo.org \
--cc=gentoo-dev@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox