Best Python code snippet using autotest_python
configuration.py
Source:configuration.py  
1#2# Authors: Robert Abram <robert.abram@entpack.com>3#4# Copyright (C) 2015-2017 EntPack5# see file 'LICENSE' for use and warranty information6#7# This program is free software; you can redistribute it and/or modify8# it under the terms of the GNU General Public License as published by9# the Free Software Foundation, either version 3 of the License, or10# (at your option) any later version.11#12# This program is distributed in the hope that it will be useful,13# but WITHOUT ANY WARRANTY; without even the implied warranty of14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the15# GNU General Public License for more details.16#17# You should have received a copy of the GNU General Public License18# along with this program.  If not, see <http://www.gnu.org/licenses/>.19#20import logging21import os22from collections import OrderedDict23from silentdune_client.utils.misc import determine_config_root, get_active_firewall, rmdir24try:25    from configparser import ConfigParser26except ImportError:27    from ConfigParser import ConfigParser  # ver. < 3.028_logger = logging.getLogger('sd-client')29# Global debug variable30debug = False31class ClientConfiguration(object):32    _config_root = None33    _config_file = None  # Full path and file name for the configuration file.34    _config_section = 'settings'  # This is the default configuration file section name, do not change.35    _config = OrderedDict()36    def __init__(self, config_file=None):37        """38        Initialize the configuration object with the most reasonable values for the default settings.39        :param config_file: Use alternate configuration file.40        """41        self._config_root = determine_config_root()42        # Set the configuration file path and name.43        if config_file:44            self._config_file = config_file45            self._config_root = os.path.split(config_file)[0]46        else:47            self._config_file = os.path.join(self._config_root, 'sdc.conf')48        # Set the default file paths based on if we are running as root or not.49        if os.getuid() == 0:50            self.set(self._config_section, 'pidfile', '/var/run/silentdune/sdc.pid')51            self.set(self._config_section, 'logfile', '/var/log/silentdune.log')52        else:53            self.set(self._config_section, 'pidfile', os.path.join(self._config_root, 'sdc.pid'))54            self.set(self._config_section, 'logfile', os.path.join(self._config_root, 'silentdune.log'))55        # self.set(self._config_section, 'user', 'silentdune')56        # self.set(self._config_section, 'group', 'silentdune')57        # # Set the previous firewall service to the currently running firewall.58        # self.set(self._config_section, 'previous_firewall_service', get_active_firewall())59        # Set the section heading comments60        self.set_comment(self._config_section, 'settings',61                         _('; Silent Dune Client Configuration File\n'  # noqa62                           '; This file was automatically generated by the installer.\n'63                           '; Run "sdc-install --help" to configure this file.\n'))64        # Set the section item comments65        self.set_comment(self._config_section, 'pidfile', _('; The path and file name for the PID file.\n'))  # noqa66        # self.set_comment(self._config_section, 'user', _('; The local user the service should run as.\n'  # noqa67        #                                                  '; Default: silentdune.\n'))68        # self.set_comment(self._config_section, 'group', _('; The local group the service should run as.\n'  # noqa69        #                                                   '; Default: silentdune.\n'))70        # self.set_comment(self._config_section, 'previous_firewall_service',71        #                  _('; The previous firewall service. Warning: Changing this value may\n'  # noqa72        #                    '; compromise security on this system if the Silent Dune client is\n'73        #                    '; uninstalled.\n'))74    def delete(self, section, key):75        """76        Delete a configuration key from the section specified.77        :param section: Configuration section name78        :param key:79        :return:80        """81        if not section or section not in self._config:82            msg = 'Invalid configuration section name ({0}).'.format(section)83            raise ValueError(msg)84        if not key or key not in self._config[section]:85            msg = 'Invalid configuration key name ({0}).'.format(key)86            raise ValueError(msg)87        del self._config[section][key]88        return True89    def set(self, section, key, val):90        """91        Set a configuration value in the section specified.92        :param section: Configuration section name93        :param key: Value key name94        :param val: Value to set95        :return:96        """97        if not section:98            msg = 'Invalid configuration section name ({0}).'.format(section)99            raise ValueError(msg)100        if section not in self._config:101            self._config[section] = OrderedDict()102        self._config[section][key] = val103    def get(self, section, key):104        """105        Retrieve a configuration value.106        :param section: Configuration section name107        :param key: Value key name108        :return Value or None:109        """110        if not section or section not in self._config:111            msg = 'Invalid configuration section name ({0}).'.format(section)112            raise ValueError(msg)113        if not key or key not in self._config[section]:114            msg = 'Invalid configuration key name ({0}).'.format(key)115            raise ValueError(msg)116        return self._config[section][key]117    def set_comment(self, section, key, comment):118        """119        Set the comments for a given key in the section specified.120        """121        if not section:122            msg = 'Invalid configuration section name ({0}).'.format(section)123            raise ValueError(msg)124        if 'comments' not in self._config[section]:125            self._config[section]['comments'] = OrderedDict()126        self._config[section]['comments'][key] = comment127    def validate_config_file(self):128        """129        Check to see if the configuration file exists, is readable and valid.130        """131        if not os.path.exists(self._config_file):132            _logger.debug('Configuration file ({0}) does not exist.'.format(self._config_file))133            return None134        config = ConfigParser(dict_type=OrderedDict)135        try:136            config.read(self._config_file)137        except ConfigParser.Error:138            _logger.debug('Configuration file is invalid.')139            return None140        return config141    def read_config(self):142        """143        Read the configuration file.144        """145        # Return the saved configuration loaded by validate_config_file().146        return self.validate_config_file()147    def write_config(self):148        """149        Write out the configuration to a file, we do NOT use ConfigParser to write out the config.150        :param config: Must be a ConfigParser object.151        """152        try:153            with open(self._config_file, 'w') as handle:154                # Loop through each section155                for name, section in self._config.items():156                    _logger.debug('Config: writing section: {0}'.format(name))157                    # Write out the section name158                    handle.write('[' + name + ']\n')159                    # Write any section header comments160                    if name in section['comments']:161                        comment = section['comments'][name]162                        handle.write(comment.rstrip() + '\n\n')163                    # Write each section item out164                    for (key, value) in section.items():165                        if key == 'comments':166                            continue167                        _logger.debug('Config: Key {0}={1}'.format(key, value))168                        # Write item comments if found.169                        if key in section['comments']:170                            comment = section['comments'][key]171                            handle.write(comment.rstrip() + '\n')172                        handle.write(key + '=' + str(value) + '\n\n')173            os.chmod(self._config_file, 0o640)174        except IOError:175            return False176        return True177    def create_directories(self):178        """179        Make sure directories used by the client exist and are set to the correct permissions.180        :return:181        """182        pidfile = None183        try:184            # user = self.get(self._config_section, 'user')185            # group = self.get(self._config_section, 'group')186            logfile = self.get(self._config_section, 'logfile')187        except ValueError:188            _logger.error('Unable to retrieve configuration values.')189            return False190        # The pidfile configuration item might not exist.191        try:192            pidfile = self.get(self._config_section, 'pidfile')193        except ValueError:194            pass195        # try:196        #     # Get information about the daemon process user and group.197        #     userinfo = pwd.getpwnam(user)198        #     groupinfo = grp.getgrnam(group)199        # except:200        #     _logger.error('Unable to get information about daemon process user and group.')201        #     return False202        # Create configuration root directory.203        try:204            configpath = self._config_root205            _logger.debug('Creating Configuration path ({0})'.format(configpath))206            if not os.path.exists(configpath):207                os.makedirs(configpath, 0o770)208            # os.chown(configpath, userinfo.pw_uid, groupinfo.gr_gid)209        except:210            _logger.error('Unable to create PID file path.')211            return False212        # If needed, create PID file path.213        try:214            if pidfile:215                pidpath = os.path.split(pidfile)[0]216                _logger.debug('Creating PID path ({0})'.format(pidpath))217                if not os.path.exists(pidpath):218                    os.makedirs(pidpath, 0o770)219                # os.chown(pidpath, userinfo.pw_uid, groupinfo.gr_gid)220        except:221            _logger.error('Unable to create PID file path.')222            return False223        # Create Log file path.224        try:225            logpath = os.path.split(logfile)[0]226            _logger.debug('Creating Log file path ({0})'.format(logpath))227            if not os.path.exists(logpath):228                os.makedirs(logpath, 0o770)229            # os.chown(logpath, userinfo.pw_uid, groupinfo.gr_gid)230        except:231            _logger.error('Unable to create LOG file path.')232            return False233        return True234    def delete_directories(self):235        """236        Remove directories used by the daemon process237        :return:238        """239        result = True240        pidfile = None241        try:242            logfile = self.get(self._config_section, 'logfile')243        except ValueError:244            _logger.error('Unable to retrieve directory configuration values.')245            return False246        # The pidfile configuration item might not exist.247        try:248            pidfile = self.get(self._config_section, 'pidfile')249        except ValueError:250            pass251        # Remove log file directory252        logpath = os.path.split(logfile)[0]253        if os.path.exists(logpath) and os.path.realpath(logpath) != '/':254            try:255                rmdir(logpath)256            except:257                _logger.error('Failed to delete logfile directory ({0}).'.format(logpath))258                result = False259        # Remove PID file directory260        if pidfile:261            pidpath = os.path.split(pidfile)[0]262            if os.path.exists(pidpath) and os.path.realpath(pidpath) != '/':263                try:264                    rmdir(pidpath)265                except:266                    _logger.error('Failed to delete PID file directory ({0}).'.format(pidpath))267                    result = False268        # Remove Configuration path269        if os.path.exists(self._config_root):270            try:271                rmdir(self._config_root)272            except:273                _logger.error('Failed to delete config directory ({0}).'.format(logpath))274                result = False...kvstore.py
Source:kvstore.py  
1import configparser2import pathlib3from collections.abc import MutableMapping4class KVStore(MutableMapping):5    """Dictionary-like key-value store backed to disk by a ConfigParser (ini) file6    Basic functionality is that of a dictionary, with the addition of an implicit7    `config_file` and `config_section`:8    >>> getfixture('manage_config_ini')  # This is just a test fixture, please disregard9    >>> d = KVStore({'key1':'value1'}, key2='value2')10    >>> d['key3'] = 'value3'11    >>> d12    KVStore(config_file='config.ini', config_section='KVStore', key1='value1', key2='value2', key3='value3')13    To create a brand new, default KVStore, ignoring anything that may already be on disk:14    >>> d = KVStore(overwrite=True)15    >>> d16    KVStore(config_file='config.ini', config_section='KVStore', )17    KVStore values can reference other values via substitution using the18    `ConfigParser.ExtendedInterpolation` format. When the KVStore is viewed as a dict,19    this substitution happens automatically.20    >>> d = KVStore(root_path='/tmp', data_path='${root_path}/data')21    >>> dict(d)22    {'root_path': '/tmp', 'data_path': '/tmp/data'}23    >>> d['data_path']24    '/tmp/data'25    To see the unparsed (raw) value, examine the object's `data` method; e.g.26    >>> d.data27    {'root_path': '/tmp', 'data_path': '${root_path}/data'}28    This substitution is updated whenever a key changes; e.g.29    >>> d['raw_data_path'] = '${root_path}/raw'30    >>> d['root_path'] = '/tmp2'31    >>> dict(d)32    {'root_path': '/tmp2', 'data_path': '/tmp2/data', 'raw_data_path': '/tmp2/raw'}33    >>> d.data34    {'root_path': '/tmp2', 'data_path': '${root_path}/data', 'raw_data_path': '${root_path}/raw'}35    >>> d['data_path']36    '/tmp2/data'37    Because this object is disk-backed, newly instantiated objects will receive the last set of defaults:38    >>> c = KVStore()39    >>> dict(c)40    {'root_path': '/tmp2', 'data_path': '/tmp2/data', 'raw_data_path': '/tmp2/raw'}41    >>> c.data42    {'root_path': '/tmp2', 'data_path': '${root_path}/data', 'raw_data_path': '${root_path}/raw'}43    We can force overwriting of this disk-backed file using the `overwrite` parameters:44    >>> c = KVStore(overwrite=True)45    >>> dict(c), c.data46    ({}, {})47    """48    def __init__(self, *args,49                 config_file=None, config_section="KVStore", overwrite=False, persistent=True,50                 **kwargs):51        """Create a new disk-backed key-value store52        Arguments53        ---------54        config_file: Path55            path to ini (ConfigParser-formatted) file that will be used to persist the KVStore56        config_section: String57            Section name to be used in the `config_file`58        overwrite: Boolean59            If True, any config file on disk will be overwritten.60            Otherwise, existing values from this file will be used as defaults,61            (unless overridden by explicit key/value pairs in the constructor)62        *args, **kwargs:63            All other arguments will be used as per the standard `dict` constructor64        """65        self._persistent = persistent66        if config_file is None:67            self._config_file = pathlib.Path("config.ini")68        else:69            self._config_file = pathlib.Path(config_file)70        self._config_section = config_section71        self._config = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())72        self.data = dict()73        if self._config_file.exists() and not overwrite:74            self._read()75        else:76            self._config.add_section(config_section)77            self._config.read_dict(self.data)78        self.update({k:v for k,v in self._config.items(self._config_section, raw=True)}) # `update` comes for free from the abc79        self.update(dict(*args, **kwargs))80        self._write()81    def __getitem__(self, key):82        return self._config.get(self._config_section, key)83    def __setitem__(self, key, value):84        self.data[key] = value85        self._config.set(self._config_section, key, value)86        self._write()87    def __delitem__(self, key):88        del self.data[key]89        self._config.remove_option(self._config_section, key)90        self._write()91    def __iter__(self):92        return iter(self.data)93    def __len__(self):94        return len(self.data)95    def _read(self):96        self._config.read(self._config_file)97        if not self._config.has_section(self._config_section):98            # File exists but we are adding to a new section of it99            self._config.add_section(self._config_section)100    def _write(self):101        if self._persistent:102            with open(self._config_file, 'w') as fw:103                self._config.write(fw)104    def __repr__(self):105        kvstr = ", ".join([f"{k}='{v}'" for k,v in self.data.items()])106        return f"KVStore(config_file='{str(self._config_file)}', config_section='{self._config_section}', {kvstr})"107    def __str__(self):108        return str({k:v for k,v in self._config.items(self._config_section, raw=False)})109if __name__ == "__main__":110    import doctest...config.py
Source:config.py  
1#!/usr/bin/env python32# stdlib3import configparser4import os.path5# project6from tool.utils import mkdirp7_CONFIG_PATH = ".aoc"8_CONFIG_FILE = os.path.join(_CONFIG_PATH, "config.cfg")9_CONFIG_SECTION = "preferences"10class Config(object):11    @staticmethod12    def load():13        config = configparser.ConfigParser()14        config[_CONFIG_SECTION] = {"username": "", "language": "py"}15        config.read(_CONFIG_FILE)16        return Config(17            config[_CONFIG_SECTION]["username"], config[_CONFIG_SECTION]["language"]18        )19    def __init__(self, user, language):20        self.user = user21        self.language = language22    def save(self):23        config = configparser.ConfigParser()24        config[_CONFIG_SECTION] = {"username": self.user, "language": self.language}25        mkdirp(_CONFIG_PATH)26        with open(_CONFIG_FILE, "w") as configfile:27            config.write(configfile)28CONFIG = Config.load()29def config(username, language):30    global CONFIG31    if not username:32        username = CONFIG.user33    if not language:34        username = CONFIG.language35    cc = Config(username, language)36    cc.save()...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
