def generate(self, line_list, level=4): """ Write out a setting record, and all of its sub settings """ tabs = TAB * level entry = tabs + '<SETTING>' # Write the name record if one exists if self.name is not None: entry = entry + '<NAME>' + self.name + '</NAME>' # Write the value record if one exists if self.value is not None: if is_string(self.value): entry = entry + '<VALUE>' + self.value + '</VALUE>' else: entry = entry + '<VALUE>' for item in self.value: line_list.append(entry + item) entry = '' entry = entry + '</VALUE>' # If there are sub settings, recurse and use # a tabbed /SETTING record if self.subsettings: line_list.append(entry) for item in self.subsettings: item.generate(line_list, level + 1) line_list.append(tabs + '</SETTING>') else: # Close the setting record on the same line line_list.append(entry + '</SETTING>')
def lookup_enums(cmd, enum_dicts, command_dict): """ Look up a set of enum command line options An enumeration table is a dict with each key being a value to enumerate and the value the integer enumeration value. Synonyms are allowed by having previously used integer value. Reverse lookup of the command line switch is performed by scanning the integer values and stopping on the first match. If the key starts with ``_NOT_USED``, it is assumed to be an enumeration that purposefully does not have a command line switch. Args: cmd: list of command line options to append the new entry enum_dicts: dict of enumeration entries command_dict: dict of command entries """ # Read from the master list for key, item in enum_dicts.items(): # Process the enumeration value = command_dict.get(key, None) # Discard phony keys if is_string(value): if value.startswith('_NOT_USED'): value = None if value is None: # Use the default value = item[0] if value is None: continue lookup_enum(cmd, item[1], value)
def add_project(self, project=None, project_type=None): """ Add a project to the list of projects found in this solution. @details Given a new Project class instance, append it to the list of projects that this solution is managing. Args: self: The 'this' reference. project: Reference to an instance of a Project. project_type: Type of project to create. """ if project is None or is_string(project): project = Project(project, project_type=project_type) # Sanity check if not isinstance(project, Project): raise TypeError( "parameter 'project' must be of type Project or name") project.solution = self project.parent = self self.project_list.append(project) return project
def set_value(self, value): """ Update with an index or an enumeration string. Value can be a number, a string or None to set to default. Args: value: None, number, or string. Except: ValueError on input error """ # Reset to default? if value is None: self.value = self.default else: # In case of error, kill value self.value = None # Try the easy way first, is it a number? try: int_value = int(value) # Do a string lookup instead. except ValueError: # Go down the list for int_value, item in enumerate(self.enumerations): # Single item? if is_string(item): # Match? if item == value: break else: # Is it in the list? if value in item: break else: raise ValueError( '"{}": {} was not found in enumeration list'.format( self.name, value)) # Perform a sanity check on the integer value if int_value < 0 or int_value >= len(self.enumerations): raise ValueError( '"{}": Index {} must be between 0 and {} inclusive.'. format(self.name, value, len(self.enumerations) - 1)) # Convert integer to a string self.value = '{}'.format(int_value)
def add_project(self, project): """ Add a dependent project. Args: project: Project to depend on. Exception: TypeError if project is not a Project """ if project is None or is_string(project): project = Project(project) # Sanity check if not isinstance(project, Project): raise TypeError( "parameter 'project' must be of type Project or name") project.solution = self.solution project.parent = self.solution self.project_list.append(project) return project
def __init__(self, *args, **kargs): """ Init defaults. Args: args: name and setting_name for get_configuration_settings(_ kargs: List of defaults. """ # Were there nameless parameters? if args: # Too many parameters? if len(args) >= 3: raise ValueError( 'Only one or two nameless parameters are allowed') # Get the default settings setting_name = None if len(args) == 2: setting_name = args[1] new_args = get_configuration_settings(args[0], setting_name) if new_args is None: new_args = {'name': args[0]} # Were there defaults found? for item in new_args: # Only add, never override if item not in kargs: kargs[item] = new_args[item] # Check the default name if not is_string(kargs.get('name', None)): raise TypeError("string parameter 'name' is required") # Init the base class Attributes.__init__(self, **kargs) ## Project this Configuration is attached to. self.project = None
def validate_string(value): """ Verify a value is a string. Check if the value is a string, if so, return the value as is or None. Args: value: Value to check. Returns: Value is string or None. Exception: ValueError if conversion failed. """ if value is not None: # Convert to bool if not is_string(value): raise ValueError('"{}" must be a string.'.format(value)) return value
def add_configuration(self, configuration): """ Add a configuration to the list of configurations found in this project. @details Given a new Configuration class instance, append it to the list of configurations that this project is managing. Args: self: The 'this' reference. configuration: Reference to an instance of a Configuration. Exception: TypeError if ``configuration`` is not a Configuration """ if configuration is None or is_string(configuration): configuration = Configuration(configuration) # Singular if not isinstance(configuration, Configuration): raise TypeError(("parameter 'configuration' " "must be of type Configuration")) # Set the configuration's parent if configuration.platform is None: configuration.platform = PlatformTypes.default() if configuration.platform.is_expandable(): for platform in configuration.platform.get_expanded(): config = deepcopy(configuration) config.platform = platform config.project = self config.parent = self self.configuration_list.append(config) else: configuration.project = self configuration.parent = self self.configuration_list.append(configuration)