confutils Package

confutils Package

config Module

package for organizing program configurations. It can read/write configurations file, parse arguments from command lines, and also parse arguments passed from method/function calling inside python.

Note: for python 2.6, argparse and orderedDict is required, install them with easy_install

class diffpy.confutils.config.ConfigBase(filename=None, args=None, **kwargs)

Bases: object

_optdatalist_default, _optdatalist are metadata used to initialize the options, see below for examples

options presents in –help (in cmd), config file, headers have same order as in these list, so arrange them in right order here.

optional args to control if the options presents in args, config file or file header

‘args’ - default is ‘a’
if ‘a’, this option will be available in self.args if ‘n’, this option will not be available in self.args
‘config’ - default is ‘a’
if ‘f’, this option will present in self.config and be written to config file only in full mode if ‘a’, this option will present in self.config and be written to config file both in full and short mode if ‘n’, this option will not present in self.config
‘header’ - default is ‘a’
if ‘f’, this option will be written to header only in full mode if ‘a’, this option will be written to header both in full and short mode if ‘n’, this option will not be written to header

so in short mode, all options with ‘a’ will be written, in full mode, all options with ‘a’ or ‘f’ will be written

getHeader(title=None, mode='full')

get a header of configurations values,

Parameters:
  • title – str, title of header, if None, try to get it from self.defaultvalue
  • mode – string, ‘short’ or ‘full’ (‘s’ or ‘f’). in short mode, all options with ‘a’ will be written, in full mode, all options with ‘a’ or ‘f’ will be written
Returns:

string, lines with line break that can be directly writen to a text file

classmethod initConfigClass()

init config class and add options to class

IMPORTANT: call this method after you define the metadata of your config class to add options as class attributes!!!

parseArgs(pargs)

parse args and update the value in self.*option*, this will call the self.args() to parse args,

Parameters:pargs – list of string, arguments to parse, usually comming from sys.argv
parseConfigFile(filename)

read a config file and update the self.*option*

Parameters:filename – str, file name of config file (include path)
parseKwargs(**kwargs)

update self.*option* values according to the kwargs

Parameters:kwargs – dict, keywords=value
resetDefault(optnames=None)

reset all values to their default value

Parameters:optnames – list of str, name of options to reset, None for all options
updateConfig(filename=None, args=None, **kwargs)

update config according to config file, args(from sys.argv) or **kwargs

  1. call self._preUpdateConfig()
  2. process file/args/kwargs passed to this method,
  3. read a configfile if specified in args or kwargs
  4. call self._postUpdateConfig()
  5. write config file if specified in args/kwargs
Parameters:
  • filename – str, file name of the config file
  • args – list of str, args passed from cmd,
  • kwargs – dict, optional kwargs
Returns:

True if anything updated, False if nothing updated

writeConfig(filename, mode='short', changeconfigfile=True)

write config to file. the file is compatiable with python package ConfigParser

Parameters:
  • filename – string, name of file
  • mode – string, ‘short’ or ‘full’ (‘s’ or ‘f’). in short mode, all options with ‘a’ will be written, in full mode, all options with ‘a’ or ‘f’ will be written

tools Module

class diffpy.confutils.tools.FakeConfigFile(configfile, endline='###Data###')

Bases: object

A fake configfile object used in reading config from header of data or a real config file.

close()

close the file

readline()

readline function

diffpy.confutils.tools.StrConv(opttype)

get the type (a converter function) according to the opttype

the function doesn’t take list

Parameters:opttype – string, a type of options, could be ‘str’, ‘int’, ‘float’, or ‘bool’
Returns:type (converter function)
diffpy.confutils.tools.checkCRC32(filename)

calculate the crc32 value of file

Parameters:filename – path to the file
Returns:crc32 value of file
diffpy.confutils.tools.checkFileVal(filename)

check file integrity using crc32 and md5. It will read file twice then compare the crc32 and md5. If two results doesn’t match, it will wait until the file is completed written to disk.

Parameters:filename – path to the file
diffpy.confutils.tools.checkMD5(filename, blocksize=65536)

calculate the MD5 value of file

Parameters:filename – path to the file
Returns:md5 value of file
diffpy.confutils.tools.opt2Str(opttype, optvalue)

turn the value of one option to string, according to the option type list of values are truned into “value1, value2, value3...”

Parameters:
  • opttype – string, type of opitons, for example ‘str’ or ‘intlist’
  • optvalue – value of the option
Returns:

string, usually stored in ConfigBase.config

diffpy.confutils.tools.str2Opt(opttype, optvalue)

convert the string to value of one option, according to the option type

Parameters:
  • opttype – string, type of opitons, for example ‘str’ or ‘intlist’
  • optvalue – string, value of the option
Returns:

value of the option, usually stored in ConfigBase.config

diffpy.confutils.tools.str2bool(v)

turn string to bool

version Module

Definition of __version__, __date__, __gitsha__.

Table Of Contents

Previous topic

diffpy Package

Next topic

srxplanar Package

This Page