From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1SbxaT-000628-F9 for garchives@archives.gentoo.org; Tue, 05 Jun 2012 17:30:33 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 79AFDE05B9; Tue, 5 Jun 2012 17:30:25 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 3725CE05B9 for ; Tue, 5 Jun 2012 17:30:25 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 6BC3A1B4022 for ; Tue, 5 Jun 2012 17:30:24 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id 352D2E5404 for ; Tue, 5 Jun 2012 17:30:23 +0000 (UTC) From: "André Erdmann" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "André Erdmann" Message-ID: <1338916714.85bca473d98e6c1e9217b299d54fa3499b407e3c.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/config.py X-VCS-Directories: roverlay/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: 85bca473d98e6c1e9217b299d54fa3499b407e3c X-VCS-Branch: master Date: Tue, 5 Jun 2012 17:30:23 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: 5a324e14-5739-4a87-9810-141b6634de13 X-Archives-Hash: a65bc1eedf1f3debfd562f7ef6ba8069 commit: 85bca473d98e6c1e9217b299d54fa3499b407e3c Author: Andr=C3=A9 Erdmann mailerd de> AuthorDate: Tue Jun 5 17:18:34 2012 +0000 Commit: Andr=C3=A9 Erdmann mailerd de> CommitDate: Tue Jun 5 17:18:34 2012 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/R_overlay.git= ;a=3Dcommit;h=3D85bca473 config: comments and config entry map modified: roverlay/config.py --- roverlay/config.py | 133 +++++++++++++++++++++++++++++++++++++++++++++-= ------ 1 files changed, 116 insertions(+), 17 deletions(-) diff --git a/roverlay/config.py b/roverlay/config.py index ab6dfec..424e69f 100644 --- a/roverlay/config.py +++ b/roverlay/config.py @@ -2,7 +2,6 @@ # Copyright 2006-2012 Gentoo Foundation # Distributed under the terms of the GNU General Public License v2 =20 -import copy import os.path import re import sys @@ -89,13 +88,21 @@ class ConfigTree: ), ebuild_header =3D dict ( value_type =3D 'fs_file', - ) + ), + overlay_dir =3D dict ( + value_type =3D 'fs_dir', + ), + distfiles_dir =3D dict ( + value_type =3D 'fs_dir', + ), =20 ) =20 + # often used regexes DEFAULT_LIST_REGEX =3D re.compile ( '\s*[,;]{1}\s*' ) WHITESPACE =3D re.compile ( '\s+' ) =20 + def __init__ ( self, import_const=3DTrue ): """Initializes an ConfigTree, which is a container for options/config = values. values can be stored directly (such as the field_definitions) or in a @@ -122,6 +129,17 @@ class ConfigTree: =20 =20 def _findpath ( self, path, root=3DNone, create=3DFalse, value=3DNone )= : + """All-in-one method that searches for a config path. + It is able to create the path if non-existent and to assign a + value to it. + + arguments: + * path -- config path as path list ([a,b,c]) or as path str (a.b.c) + * root -- config root (dict expected). Uses self._config if None (the = default) + * create -- create path if nonexistent + * value -- assign value to the last path element + an empty dict will be created if this is None and create is= True + """ if path is None: return root elif isinstance ( path, str ): @@ -166,10 +184,36 @@ class ConfigTree: # --- end of get (...) --- =20 def _add_entry ( self, option, value=3DNone, config_root=3DNone ): + """Adds an option to the config. + + arguments: + * option -- name of the option as it appears in the (main) config file + * value -- value to assign, defaults to None + * config_root -- root of the config (a dict), defaults to None which i= s + later understood as self._config + """ =20 def make_and_verify_value ( value_type, value, entryconfig_ref ): + """Prepares the value of a config option so that it can be used + in the ConfigTree. + + arguments: + * value_type -- type of the value, look above for explanation concern= ing this + * value -- value to verify and transform + * entryconfig_ref -- reference to the config entry config + """ =20 def to_int ( val, fallback_value=3D-1 ): + """Tries to convert val to an int, returning a fallback value + on any error. + + arguments: + * val -- + * fallback_value -- + + catches: ValueError in case of an unsuccesful int conversion + raises: nothing + """ try: ret =3D int ( val ) return ret @@ -178,6 +222,13 @@ class ConfigTree: # --- end of to_int (...) --- =20 def yesno ( val ): + """Tries to canonize an yes or no value to its integer + representation. Returns 1 if val means 'yes', 0 if 'no' and + -1 otherwise. + + arguments: + * val -- + """ if not val is None: to_check =3D str ( val ) . lower () if to_check in [ 'y', 'yes', '1', 'true', 'enabled', 'on' ]: @@ -190,10 +241,21 @@ class ConfigTree: # --- end of yesno (...) --- =20 def fs_path ( val ): + """val is a filesystem path - returns expanded path (~ -> HOME). + + arguments: + * val -- + """ return os.path.expanduser ( val ) if val else None # --- end of fs_path (...) --- =20 def fs_file ( val ): + """"val is a file - returns expanded path if it is an existent + file or it does not exist. + + arguments: + * val -- + """ if val: retval =3D os.path.expanduser ( val ) if os.path.isfile ( retval ) or not os.path.exists ( retval ): @@ -202,8 +264,25 @@ class ConfigTree: return None # --- end of fs_file (...) --- =20 + def fs_dir ( val ): + """val is a directory -- returns expanded path if it is an existent + dir or it does not exist. + + arguments: + * val -- + """ + if val: + retval =3D os.path.expanduser ( val ) + if os.path.isdir ( retval ) or not os.path.exists ( retval ): + return retval + + return None + # --- end of fs_dir (...) --- + + # replace whitespace with a single ' ' value =3D ConfigTree.WHITESPACE.sub ( ' ', value ) =20 + # convert value_type into a list of value types if not value_type: return value elif isinstance ( value_type, list ): @@ -227,7 +306,6 @@ class ConfigTree: # dofunc ( function f, v) calls f(x) for every str in v dofunc =3D lambda f, v : [ f(x) for x in v ] if isinstance ( v, list = ) else f(v) =20 - retval =3D value =20 for vtype in vtypes: @@ -236,26 +314,43 @@ class ConfigTree: else: self.logger.warning ( "unknown value type '" + vtype + "'." ) =20 - return retval # --- end of make_and_verify_value (...) --- =20 =20 real_option =3D option low_option =3D option.lower() + + # known option? if option and low_option in ConfigTree.CONFIG_ENTRY_MAP: - cref =3D ConfigTree.CONFIG_ENTRY_MAP [low_option] =20 - if isinstance ( cref, str ) and cref in ConfigTree.CONFIG_ENTRY_MAP: - option =3D low_option =3D cref - cref =3D ConfigTree.CONFIG_ENTRY_MAP [cref] + original_cref =3D cref =3D ConfigTree.CONFIG_ENTRY_MAP [low_option] + cref_level =3D 0 + + # check if cref is a link to another entry in CONFIG_ENTRY_MAP + while isinstance ( cref, str ): + if cref =3D=3D original_cref and cref_level: + self.logger.critical ( "CONFIG_ENTRY_MAP is invalid! circular cref = detected." ) + raise Exception ( "CONFIG_ENTRY_MAP is invalid!" ) + + elif cref in ConfigTree.CONFIG_ENTRY_MAP: + option =3D low_option =3D cref + cref =3D ConfigTree.CONFIG_ENTRY_MAP [cref] + cref_level +=3D 1 + else: + self.logger.critical ( + "CONFIG_ENTRY_MAP is invalid! last cref =3D " + option + + ", current cref =3D " + cref + "." + ) + raise Exception ( "CONFIG_ENTRY_MAP is invalid!" ) =20 + # check if config entry is disabled if cref is None: # deftly ignored return True =20 =20 - + # determine the config path path =3D None if 'path' in cref: path =3D cref ['path'] @@ -264,12 +359,14 @@ class ConfigTree: for n in range ( len ( path ) - 1 ): path [n] =3D path [n].upper() =20 - + # need a valid path if path: =20 + # verify and convert value if value_type is set if 'value_type' in cref: value =3D make_and_verify_value ( cref ['value_type'], value, cref = ) =20 + # need a valid value if value: =20 self.logger.debug ( @@ -278,6 +375,7 @@ class ConfigTree: " and value " + str ( value ) + "." ) =20 + # add option/value to the config self._findpath ( path, config_root, True, value ) =20 return True @@ -286,7 +384,11 @@ class ConfigTree: "Option '" + str ( real_option ) + "' has an unusable value '" + str ( value ) + "'." ) + return False # --- + + self.logger.error ( "Option '" + str ( real_option ) + "' is unusable= ..." ) + return False # --- =20 self.logger.warning ( "Option '" + str ( real_option ) + "' is unknown= ." ) @@ -313,10 +415,11 @@ class ConfigTree: # load file =20 try: - fh =3D open ( config_file, 'r' ) - reader =3D shlex.shlex ( fh ) + reader.wordchars +=3D ' ./$()[]:+-@*~' + fh =3D open ( config_file, 'r' ) + reader =3D shlex.shlex ( fh ) reader.whitespace_split =3D False - reader.wordchars +=3D ' ./$()[]:+-@*~' + =20 nextline =3D lambda : ( reader.get_token() for n in range (3) ) =20 @@ -333,13 +436,9 @@ class ConfigTree: =20 option, equal, value =3D nextline () =20 - - if fh: fh.close () =20 - # - except IOError as ioerr: raise =20