In an effort to get away from custom-derived formats, I’ve retired legacy delimited files in favor of more universal JSON and YAML formats (either one!). This will require the PyYaml module to be added in most cases. Using pip :
#sudo pip install --upgrade pip
#sudo pip install pyyaml
The feature list for the next major release will include some sort of dependency management.
The new PokeyConfig class has several functions added to accommodate these formats and convert applications built in the old style:
- load_config(self,conf_type,inpath=None) : called by the __init__() method, this loads the file specified by the fpath parameter and processes according to the file extension, either loading JSON or YAML formatted configuration information into the dictionary PokeyConfig.conf_dict
- convert_delimited(self,inpath,out_type) : this method is invoked by load_config if a legacy PokeyConfig format is detected (with a delimiter passed to the function, or with the old .conf file extension). The user is first prompted to select whether to convert to [J]SON, or [Y]AML. Any input other than [JjYy] returns an [*] Invalid Choice error message and the user is prompted again. The legacy file is read using load_delimited , and that configuration dictionary is written to the file format of the user’s choice, with the same file name as inpath and an appropriate extension. The same file is then read as verification, and the result compared to the existing dictionary. Mismatches are reprocessed, raising an error the second iteration.
- load_delimited(self,inpath,delimiter=',') : loads the passed inpath and reads the data, splitting (by delimiter ) each line that does not start with a # or is blank and adding a dictionary entry for each configuration setting. This function is intended for conversion operations, but could be used to force legacy reading of delimited files. There is no delimited-write support, use the csv operations in PokeyWorks.
- write_config(self,params=False,**kw) : this function writes the current configuration state. If the configuration has been applied as instance attributes, the current instance attribute values will replace the existing conf_dict values.
- Full class
- Example File Conversion
The new class usage offers flexibility in converting from one format to another, and automatic conversion of legacy delimited files. The class is invoked with __init__ :
If using the default JSON format, you simply need to pass a file path and the loaded options will be stored in the dictionary PokeyConfig.conf_dict . If auto_apply is True, the dictionary keys will be set as attributes as well. Once the configuration is applied to the class instance, any call to PokeyConfig.write_config will overwrite the values in configuration dictionary with the attribute values before writing to the file.
This optional “application” of the values allows the configuration to be confined to a single dictionary and edited in place, or applied as attributes of the class instance and accessible via dot referencing. Example invocations of each method follow.
from pokeyworks import PokeyConfig
def __init__(self, config_path, **kwargs):
# Example 1 - access the config dictionary directly
self.config = PokeyConfig(config_path).conf_dict
self.config = PokeyConfig(config_path,PokeyConfig.yaml).conf_dict
# Example 2 - apply to an instance of the class
self.config = PokeyConfig(config_path)
# Config values are now accessible as :
x_dim = self.config.dimension_x
# And can be altered before writing by assignment :
self.config.dimension_z = 5
# YAML - identical to the above except for the invocation
self.config = PokeyConfig(config_path,PokeyConfig.yaml)
# AutoApply example (JSON)
conf = PokeyConfig(config_path,PokeyConfig.json,True)
# Config options are now immediately as dot-referenced
# attributes of the class instance
position = (conf.x, conf.y, conf.z)
Example Conversion (Command-Line Interpreter):
>>> import pokeyworks
>>> config = pokeyworks.PokeyConfig('pokeygame.conf','%')
[*] Legacy PokeyConfig configuration detected!
Convert to [J]SON
Convert to [Y]AML
Selection > j
[*] Conversion complete : pokeygame.json
Please update your configuration paths &
to review the new usage and update your apps