class ConfigError(BaseCClass): TYPE_NAME = "config_error" _free = ConfigPrototype("void config_error_free(config_error)") _count = ConfigPrototype("int config_error_count(config_error)") _iget = ConfigPrototype("char* config_error_iget(config_error, int)") def __init__(self): raise NotImplementedError("Class can not be instantiated directly!") def __getitem__(self, index): """ @rtype: str """ if not isinstance(index, int): raise TypeError("Expected an integer") size = len(self) if index >= size: raise IndexError("Index out of range: %d < %d" % (index, size)) return self._iget(index) def __len__(self): """ @rtype: int """ return self._count() def free(self): self._free()
class ContentItem(BaseCClass): TYPE_NAME = "content_item" _alloc = ConfigPrototype( "void* config_content_item_alloc( schema_item , void* )", bind=False) _size = ConfigPrototype("int config_content_item_get_size( content_item )") _iget_content_node = ConfigPrototype( "content_node_ref config_content_item_iget_node( content_item , int)") _free = ConfigPrototype("void config_content_item_free( content_item )") def __init__(self, schema_item): path_elm = None c_ptr = self._alloc(schema_item, path_elm) super(ContentItem, self).__init__(c_ptr) def __len__(self): return self._size() def __getitem__(self, index): if isinstance(index, int): if index < 0: index += len(self) if (index >= 0) and (index < len(self)): return self._iget_content_node(index).setParent(self) else: raise IndexError("Expected 0 <= index < %d, was 0 <= %d < %d" % (len(self), index, len(self))) else: raise TypeError("[] operator must have integer index") def last(self): return self[-1] def getValue(self, item_index=-1, node_index=0): node = self[item_index] return node[node_index] def free(self): self._free()
class SchemaItem(BaseCClass): TYPE_NAME = "schema_item" _alloc = ConfigPrototype("void* config_schema_item_alloc( char* , bool )", bind=False) _free = ConfigPrototype("void config_schema_item_free( schema_item )") _iget_type = ConfigPrototype("config_content_type_enum config_schema_item_iget_type( schema_item, int)") _iset_type = ConfigPrototype("void config_schema_item_iset_type( schema_item , int , config_content_type_enum)") _set_argc_minmax = ConfigPrototype("void config_schema_item_set_argc_minmax( schema_item , int , int)") _add_alternative = ConfigPrototype("void config_schema_item_add_indexed_alternative(schema_item , int , char*)") _set_deprecated = ConfigPrototype("void config_schema_item_set_deprecated(schema_item , char*)") def __init__(self, keyword, required=False): c_ptr = self._alloc(keyword, required) super(SchemaItem, self).__init__(c_ptr) def iget_type( self, index): """ @rtype: ContentTypeEnum """ return self._iget_type(index) def iset_type( self, index, schema_type ): """ @type schema_type: ContentTypeEnum """ assert isinstance(schema_type, ContentTypeEnum) self._iset_type(index, schema_type) def set_argc_minmax(self, minimum, maximum): self._set_argc_minmax(minimum, maximum) def initSelection(self , index , alternatives): for alt in alternatives: self.addAlternative( index , alt ) def addAlternative(self , index , alt): self._add_alternative( index , alt ) def setDeprecated(self , msg): """This method can be used to mark this item as deprecated. If the deprecated item is used in a configuration file the @msg will be added to the warnings of the ConfigContent object, """ self._set_deprecated( msg ) def free(self): self._free()
class ConfigPathElm(BaseCClass): TYPE_NAME = "config_path_elm" _free = ConfigPrototype("void config_path_elm_free(config_path_elm)") _rel_path = ConfigPrototype( "char* config_path_elm_get_relpath(config_path_elm)") _abs_path = ConfigPrototype( "char* config_path_elm_get_abspath(config_path_elm)") def __init__(self): raise NotImplementedError( "Not possible to instantiate ConfigPathElm directly.") def free(self): self._free() @property def rel_path(self): return self._rel_path() @property def abs_path(self): return self._abs_path()
class ConfigContent(BaseCClass): TYPE_NAME = "config_content" _free = ConfigPrototype("void config_content_free( config_content )") _is_valid = ConfigPrototype( "bool config_content_is_valid( config_content )") _has_key = ConfigPrototype( "bool config_content_has_item( config_content , char*)") _get_item = ConfigPrototype( "content_item_ref config_content_get_item( config_content , char*)") _get_errors = ConfigPrototype( "config_error_ref config_content_get_errors( config_content )") _get_warnings = ConfigPrototype( "stringlist_ref config_content_get_warnings( config_content )") def __init__(self): raise NotImplementedError("Class can not be instantiated directly!") def __contains__(self, key): return self._has_key(key) def setParser(self, parser): self._parser = parser def __getitem__(self, key): if key in self: item = self._get_item(key) item.setParent(self) return item else: if key in self._parser: schema_item = SchemaItem(key) return ContentItem(schema_item) else: raise KeyError("No such key: %s" % key) def hasKey(self, key): return key in self def getValue(self, key, item_index=-1, node_index=0): item = self[key] return item.getValue(item_index, node_index) def isValid(self): return self._is_valid() def free(self): self._free() def getErrors(self): """ @rtype: ConfigError """ return self._get_errors() def getWarnings(self): """ @rtype: ConfigError """ return self._get_warnings()
class ContentTypeEnum(BaseCEnum): TYPE_NAME = "config_content_type_enum" CONFIG_STRING = None CONFIG_INT = None CONFIG_FLOAT = None CONFIG_PATH = None CONFIG_EXISTING_PATH = None CONFIG_BOOL = None CONFIG_CONFIG = None CONFIG_BYTESIZE = None CONFIG_EXECUTABLE = None CONFIG_ISODATE = None CONFIG_INVALID = None CONFIG_RUNTIME_FILE = None CONFIG_RUNTIME_INT = None _valid_string = ConfigPrototype( "bool config_schema_item_valid_string(config_content_type_enum , char*, bool)" ) _sscanf_bool = EclPrototype("bool util_sscanf_bool( char* , bool*)", bind=False) def valid_string(self, string, runtime=False): return self._valid_string(string, runtime) def convert_string(self, string): if not self.valid_string(string, runtime=True): raise ValueError("Can not convert %s to %s" % (string, self)) if self == ContentTypeEnum.CONFIG_INT: return int(string) if self == ContentTypeEnum.CONFIG_FLOAT: return float(string) if self == ContentTypeEnum.CONFIG_BOOL: bool_value = ctypes.c_bool() ContentTypeEnum._sscanf_bool(string, ctypes.byref(bool_value)) return bool_value.value return string
class ConfigParser(BaseCClass): TYPE_NAME = "config_parser" _alloc = ConfigPrototype("void* config_alloc()", bind=False) _add = ConfigPrototype( "schema_item_ref config_add_schema_item(config_parser, char*, bool)") _free = ConfigPrototype("void config_free(config_parser)") _parse = ConfigPrototype( "config_content_obj config_parse(config_parser, char*, char*, char*, char*, hash, config_unrecognized_enum, bool)" ) _size = ConfigPrototype("int config_get_schema_size(config_parser)") _get_schema_item = ConfigPrototype( "schema_item_ref config_get_schema_item(config_parser, char*)") _has_schema_item = ConfigPrototype( "bool config_has_schema_item(config_parser, char*)") _add_key_value = ConfigPrototype( "bool config_parser_add_key_values(config_parser, config_content, char*, stringlist, config_path_elm, char*, config_unrecognized_enum)" ) _validate = ConfigPrototype( "void config_validate(config_parser, config_content)") def __init__(self): c_ptr = self._alloc() super(ConfigParser, self).__init__(c_ptr) def __contains__(self, keyword): return self._has_schema_item(keyword) def __len__(self): return self._size() def __repr__(self): return self._create_repr('size=%d' % len(self)) def add(self, keyword, required=False, value_type=None): item = self._add(keyword, required).setParent(self) if value_type: item.iset_type(0, value_type) return item def getSchemaItem(self, keyword): warnings.warn('deprecated, use conf[kw]', DeprecationWarning) return self[keyword] def __getitem__(self, keyword): if keyword in self: item = self._get_schema_item(keyword) item.setParent(self) return item else: raise KeyError("Config parser does not have item:%s" % keyword) def parse(self, config_file, comment_string="--", include_kw="INCLUDE", define_kw="DEFINE", pre_defined_kw_map=None, unrecognized=UnrecognizedEnum.CONFIG_UNRECOGNIZED_WARN, validate=True): """ @rtype: ConfigContent """ assert isinstance(unrecognized, UnrecognizedEnum) if not os.path.exists(config_file): raise IOError("File: %s does not exists" % config_file) config_content = self._parse(config_file, comment_string, include_kw, define_kw, pre_defined_kw_map, unrecognized, validate) config_content.setParser(self) if validate and not config_content.isValid(): sys.stderr.write("Errors parsing:%s \n" % config_file) for count, error in enumerate(config_content.getErrors()): sys.stderr.write(" %02d:%s\n" % (count, error)) raise ValueError("Parsing:%s failed" % config_file) return config_content def free(self): self._free() def validate(self, config_content): self._validate(config_content) def add_key_value( self, config_content, key, value, path_elm=None, config_filename=None, unrecognized_action=UnrecognizedEnum.CONFIG_UNRECOGNIZED_WARN): return self._add_key_value(config_content, key, value, path_elm, config_filename, unrecognized_action)
class ContentNode(BaseCClass): TYPE_NAME = "content_node" _iget = ConfigPrototype( "char* config_content_node_iget( content_node , int)") _size = ConfigPrototype("int config_content_node_get_size( content_node )") _get_full_string = ConfigPrototype( "char* config_content_node_get_full_string( content_node , char* )") _iget_type = ConfigPrototype( "config_content_type_enum config_content_node_iget_type( content_node , int)" ) _iget_as_abspath = ConfigPrototype( "char* config_content_node_iget_as_abspath( content_node , int)") _iget_as_relpath = ConfigPrototype( "char* config_content_node_iget_as_relpath( content_node , int)") _iget_as_string = ConfigPrototype( "char* config_content_node_iget( content_node , int)") _iget_as_int = ConfigPrototype( "int config_content_node_iget_as_int( content_node , int)") _iget_as_double = ConfigPrototype( "double config_content_node_iget_as_double( content_node , int)") _iget_as_path = ConfigPrototype( "char* config_content_node_iget_as_path( content_node , int)") _iget_as_bool = ConfigPrototype( "bool config_content_node_iget_as_bool( content_node , int)") _iget_as_isodate = ConfigPrototype( "time_t config_content_node_iget_as_isodate( content_node , int)") typed_get = { ContentTypeEnum.CONFIG_STRING: _iget_as_string, ContentTypeEnum.CONFIG_INT: _iget_as_int, ContentTypeEnum.CONFIG_FLOAT: _iget_as_double, ContentTypeEnum.CONFIG_PATH: _iget_as_path, ContentTypeEnum.CONFIG_EXISTING_PATH: _iget_as_path, ContentTypeEnum.CONFIG_BOOL: _iget_as_bool, ContentTypeEnum.CONFIG_ISODATE: _iget_as_isodate } def __init__(self): raise NotImplementedError("Class can not be instantiated directly!") def __len__(self): return self._size() def __assertIndex(self, index): if isinstance(index, int): if index < 0: index += len(self) if not 0 <= index < len(self): raise IndexError return index else: raise TypeError("Invalid argument type: %s" % index) def __getitem__(self, index): index = self.__assertIndex(index) content_type = self._iget_type(index) typed_get = self.typed_get[content_type] return typed_get(self, index) def getPath(self, index=0, absolute=True, relative_start=None): index = self.__assertIndex(index) content_type = self._iget_type(index) if content_type in [ ContentTypeEnum.CONFIG_EXISTING_PATH, ContentTypeEnum.CONFIG_PATH ]: if absolute: return self._iget_as_abspath(index) else: if relative_start is None: return self._iget_as_relpath(index) else: abs_path = self._iget_as_abspath(index) return os.path.relpath(abs_path, relative_start) else: raise TypeError( "The getPath() method can only be called on PATH items") def content(self, sep=" "): return self._get_full_string(sep) def igetString(self, index): index = self.__assertIndex(index) return self._iget(index) def asList(self): return [x for x in self]
class ConfigContent(BaseCClass): TYPE_NAME = "config_content" _alloc = ConfigPrototype("void* config_content_alloc(char*)", bind=False) _free = ConfigPrototype("void config_content_free( config_content )") _is_valid = ConfigPrototype( "bool config_content_is_valid( config_content )") _has_key = ConfigPrototype( "bool config_content_has_item( config_content , char*)") _get_item = ConfigPrototype( "content_item_ref config_content_get_item( config_content , char*)") _get_errors = ConfigPrototype( "config_error_ref config_content_get_errors( config_content )") _get_warnings = ConfigPrototype( "stringlist_ref config_content_get_warnings( config_content )") _get_config_path = ConfigPrototype( "char* config_content_get_config_path( config_content )") _create_path_elm = ConfigPrototype( "config_path_elm_ref config_content_add_path_elm(config_content, char*)" ) _add_define = ConfigPrototype( "void config_content_add_define(config_content, char*, char*)") _size = ConfigPrototype("int config_content_get_size(config_content)") _keys = ConfigPrototype( "stringlist_obj config_content_alloc_keys(config_content)") def __init__(self, filename): c_ptr = self._alloc(filename) if c_ptr: super(ConfigContent, self).__init__(c_ptr) else: raise ValueError( 'Failed to construct ConfigContent instance from config file %s.' % filename) def __len__(self): return self._size() def __contains__(self, key): return self._has_key(key) def setParser(self, parser): self._parser = parser def __getitem__(self, key): if key in self: item = self._get_item(key) item.setParent(self) return item else: if key in self._parser: schema_item = SchemaItem(key) return ContentItem(schema_item) else: raise KeyError("No such key: %s" % key) def hasKey(self, key): return key in self def getValue(self, key, item_index=-1, node_index=0): item = self[key] return item.getValue(item_index, node_index) def isValid(self): return self._is_valid() def free(self): self._free() def getErrors(self): """ @rtype: ConfigError """ return self._get_errors() def getWarnings(self): """ @rtype: ConfigError """ return self._get_warnings() def get_config_path(self): return self._get_config_path() def create_path_elm(self, path): return self._create_path_elm(path) def add_define(self, key, value): self._add_define(key, value) def keys(self): return self._keys() def as_dict(self): d = {} for key in self.keys(): d[key] = [] item = self[key] for node in item: d[key].append([x for x in node]) return d
class ConfigSettings(BaseCClass): TYPE_NAME = "config_settings" _alloc = ConfigPrototype("void* config_settings_alloc(char*)", bind = False) _free = ConfigPrototype("void config_settings_free(config_settings)") _add_setting = ConfigPrototype("bool config_settings_add_setting(config_settings , char* , config_content_type_enum, char*)") _add_double_setting = ConfigPrototype("void config_settings_add_double_setting(config_settings , char* , double)") _add_int_setting = ConfigPrototype("void config_settings_add_int_setting(config_settings , char* , int)") _add_string_setting = ConfigPrototype("void config_settings_add_string_setting(config_settings , char* , char*)") _add_bool_setting = ConfigPrototype("void config_settings_add_bool_setting(config_settings , char* , bool)") _has_key = ConfigPrototype("bool config_settings_has_key(config_settings , char*)") _get_type = ConfigPrototype("config_content_type_enum config_settings_get_value_type(config_settings, char*)") _init_parser = ConfigPrototype("void config_settings_init_parser(config_settings, config_parser, bool)") _apply = ConfigPrototype("void config_settings_apply(config_settings, config_content)") _alloc_keys = ConfigPrototype("stringlist_obj config_settings_alloc_keys(config_settings)") _get = ConfigPrototype("char* config_settings_get_value(config_settings, char*)") _get_int = ConfigPrototype("int config_settings_get_int_value(config_settings, char*)") _get_double = ConfigPrototype("double config_settings_get_double_value(config_settings, char*)") _get_bool = ConfigPrototype("bool config_settings_get_bool_value(config_settings, char*)") _set = ConfigPrototype("bool config_settings_set_value(config_settings, char*, char*)") _set_int = ConfigPrototype("bool config_settings_set_int_value(config_settings, char*, int)") _set_double = ConfigPrototype("bool config_settings_set_double_value(config_settings, char*, double)") _set_bool = ConfigPrototype("bool config_settings_set_bool_value(config_settings, char*, bool)") def __init__(self , root_key): c_ptr = ConfigSettings._alloc( root_key ) super(ConfigSettings, self).__init__(c_ptr) def __contains__(self, key): return self._has_key( key ) def __getitem__(self,key): if key in self: value_type = self._get_type( key ) if value_type == ContentTypeEnum.CONFIG_INT: return self._get_int( key ) if value_type == ContentTypeEnum.CONFIG_FLOAT: return self._get_double( key ) if value_type == ContentTypeEnum.CONFIG_BOOL: return self._get_bool( key ) return self._get( key ) else: raise KeyError("Settings object does not support key:%s" % key) def __setitem__(self, key, value): if key in self: value_type = self._get_type( key ) if value_type == ContentTypeEnum.CONFIG_INT: self._set_int( key , value) return if value_type == ContentTypeEnum.CONFIG_FLOAT: self._set_double( key , value) return if value_type == ContentTypeEnum.CONFIG_BOOL: if type(value) is bool: self._set_bool( key , value ) return else: raise TypeError("Type of %s should be boolean" % key) if not self._set( key , value ): raise TypeError("Setting %s=%s failed \n" % (key , value)) else: raise KeyError("Settings object does not support key:%s" % key) def free(self): self._free( ) def addSetting(self, key , value_type, initial_value): if not self._add_setting( key , value_type , str(initial_value) ): raise TypeError("Failed to add setting %s with initial value %s" % (key , initial_value)) def addIntSetting(self, key , initial_value): self._add_int_setting( key , initial_value ) def addDoubleSetting(self, key , initial_value): self._add_double_setting( key , initial_value ) def addStringSetting(self, key , initial_value): self._add_string_setting( key , initial_value ) def addBoolSetting(self, key , initial_value): self._add_bool_setting( key , initial_value ) def initParser(self , parser, required = False): self._init_parser( parser , required ) def apply(self , config_content): self._apply( config_content ) def keys(self): return self._alloc_keys( ) def getType(self , key): if key in self: return self._get_type( key ) else: raise KeyError("No such key:%s" % key)
class SchemaItem(BaseCClass): TYPE_NAME = "schema_item" _alloc = ConfigPrototype("void* config_schema_item_alloc( char* , bool )", bind=False) _free = ConfigPrototype("void config_schema_item_free( schema_item )") _iget_type = ConfigPrototype( "config_content_type_enum config_schema_item_iget_type( schema_item, int)" ) _iset_type = ConfigPrototype( "void config_schema_item_iset_type( schema_item , int , config_content_type_enum)" ) _set_argc_minmax = ConfigPrototype( "void config_schema_item_set_argc_minmax( schema_item , int , int)") _add_alternative = ConfigPrototype( "void config_schema_item_add_indexed_alternative(schema_item , int , char*)" ) _set_deprecated = ConfigPrototype( "void config_schema_item_set_deprecated(schema_item , char*)") _valid_string = ConfigPrototype( "bool config_schema_item_valid_string(config_content_type_enum , char*)", bind=False) _sscanf_bool = UtilPrototype("bool util_sscanf_bool( char* , bool*)", bind=False) def __init__(self, keyword, required=False): c_ptr = self._alloc(keyword, required) super(SchemaItem, self).__init__(c_ptr) @classmethod def validString(cls, value_type, value): return cls._valid_string(value_type, value) @classmethod def convert(cls, value_type, value_string): if cls.validString(value_type, value_string): if value_type == ContentTypeEnum.CONFIG_INT: return int(value_string) if value_type == ContentTypeEnum.CONFIG_FLOAT: return float(value_string) if value_type == ContentTypeEnum.CONFIG_BOOL: value = ctypes.c_bool() SchemaItem._sscanf_bool(value_string, ctypes.byref(value)) return value.value return value_string else: raise ValueError("Invalid string value: %s" % value_string) def iget_type(self, index): """ @rtype: ContentTypeEnum """ return self._iget_type(index) def iset_type(self, index, schema_type): """ @type schema_type: ContentTypeEnum """ assert isinstance(schema_type, ContentTypeEnum) self._iset_type(index, schema_type) def set_argc_minmax(self, minimum, maximum): self._set_argc_minmax(minimum, maximum) def initSelection(self, index, alternatives): for alt in alternatives: self.addAlternative(index, alt) def addAlternative(self, index, alt): self._add_alternative(index, alt) def setDeprecated(self, msg): """This method can be used to mark this item as deprecated. If the deprecated item is used in a configuration file the @msg will be added to the warnings of the ConfigContent object, """ self._set_deprecated(msg) def free(self): self._free()