openwrt — PyCI OpenWRT Module

class ConfigFileOpenWRT(filepath=None)

Class for accessing/saving config files in /etc/config. When initialized with a filepath this class will contain a list of OpenWRTConfig objects representing the contents of filepath.

empty_config()
Returns an empty list/dict object in the event we’re working with an empty or non-existant file.
option_datatype(section, option, validation_file=None)
Returns the datatype value for a given option in a given section. Will be one of: uint, string, host, device, boolean, file, ip4addr, ip6addr, port, directory, integer, macaddr, hostname
read_config_file(filepath=None)
Reads self.filepath and returns a list of OpenWRTConfig objects representing the file.
save()

Saves the config file

The standard OpenWRT config file format is like so: config section option <tab>option someoption someoption ... <tab>option someoption someoption ...

save_raw(text)
Saves the raw (plaintext) version of the config file using ‘text’. This overrides the default save_raw function in order to provide validation.
valid_options(section, option, validation_file=None)
Return a list of valid values for any given option contained within ‘section’ of ‘validation_file’. If no validation_file is provided, this method will look for a matching file in <PyCI dir>/metadata/
validate(validation_file=None)
Validates the current configs against the definitions specified in the given validation file. Returns None if everything validates and raises a ValidationException if there’s a problem. If no validation file is given it tries to be smart about it by looking in <PyCI dir>/metadata for a file that matches the filename in self.filepath.
validate_option(section, option, value, validation_file=None)
Checks if the given section[option] is valid based on the rules specified in validation_file. If no validation_file is given it will use the default (<PyCI dir>/metadata/<config file name>). Returns None if the option is valid. Returns a detailed error message if invalid.
validation_dict(validation_obj)
Returns a ordered dictionary of values representing the required options, dependencies, and such from a given validation object (read_config_file(validation_file) of a file in <PyCI dir>/metadata/.
validation_obj(validation_file=None)
Returns a validation object by reading ‘validation_file’ using self.read_config_file.
class OpenWRTConfig(config, options, list_items=[])

Class to store config sections from /etc/config/* files.

OpenWRTConfig object parameters can be accessed as a dictionary...
>>> config = ConfigFileOpenWRT(filepath='/etc/config/network')
>>> test = config.configs() # test is an OpenWRTConfig object
>>> print test[0]['config']
interface lan
...or directly as attributes:
>>> print test[0].config
interface lan
Parameters can be set the same way:
>>> test[0]['config'] = 'rule'
>>> test[0].config = 'rule'
asList()
Returns our parameters as a list
d_str()

Returns the config file in dependency/validation file format which means... No quotes for 2nd field (option/list_item). No quotes for config (variable, section, or package).

Example:
config package
option title ‘Network configuration’
config section
option named ‘true’ option required ‘true’ option name ‘interface’ option package ‘network’ option title ‘Network interface’ list depends ‘proto=none’
class ValidationDependency(option_requirement, required_option)
Class to store ‘list depends whatever=foo, bar’ objects from luci-style validation files
class ValidationEnumerator(variable, value, title)
Class to store enumerators from a luci-style validation file
class ValidationSection(name, named=False, required=False, title=None, package=None, depends=[], variables=[])
Class to store the various requirements and variables that are associated with any given ‘section’ in a luci-style validation file
class ValidationVariable(name, section, title=None, variable_type=None, required=False, depends=[], valueof=None)
Class to store various requirements associated with any given ‘variable’ in a luci-style validation file.
fw_optional(option_dict, option)
Returns the value of ‘option’ in option_dict if ‘option’ is actually present. Returns None if ‘option’ isn’t in the dict.
fw_redirect(firewall_conf_rule, rules_output=False)
This is a python port of the fw_redirect function inside OpenWRT’s uci_firewall.sh script. The purpose is to output an equivalent iptables command line from a standard UCI firewall config section. It takes a single ‘rule’ section from a ConfigFileOpenWRT object, ‘firewall_conf_rule’ as the only argument and sources it to return a list of one or two (if proto==’tcpudp’) Rule() objects which can be used to generate the iptables lines. if ‘rules_output’ is True, return Rule() objects instead of just strings.
fw_rule(firewall_conf_rule, rules_output=False)
This is a python port of the fw_rule function inside OpenWRT’s uci_firewall.sh script. The purpose is to output an equivalent iptables command line from a standard UCI firewall config section. It takes a single ‘rule’ section from a ConfigFileOpenWRT object, ‘firewall_conf_rule’ as the only argument and sources it to return a list of one or two (if proto==’tcpudp’) Rule() objects which can be used to generate the iptables lines. if ‘rules_output’ is True, return Rule() objects instead of just strings.
new_fw_rule(**kwargs)
Returns a list of one or two Rule() objects from the given keyword arguments. If proto==’tcp’ or proto==’udp’, return one Rule(). if proto==’tcpudp’, return two Rule() objects (one for each of ‘tcp’ and ‘udp’). If proto==None, return one Rule() based on the given keyword arguments.
surround_with_quotes(_list)
Takes a list of items and returns a string with each list item surrounded by single quotes (for UCI stuff)