From d42b66916dfa6186b870f0b8e373dbd4825fbb3e Mon Sep 17 00:00:00 2001 From: Aswa Paul Date: Sat, 8 Sep 2018 19:30:25 +0300 Subject: [PATCH 1/2] Support config file reload Add option allow_no_value Default allow_no_value to True --- pyconfigreader/reader.py | 38 ++++++++++++++++++-------------------- setup.py | 2 +- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/pyconfigreader/reader.py b/pyconfigreader/reader.py index d801f11..af70446 100644 --- a/pyconfigreader/reader.py +++ b/pyconfigreader/reader.py @@ -23,6 +23,8 @@ from io import StringIO as IO CASE_SENSITIVE = False +ALLOW_NO_VALUE = True +DEFAULT_DICT = OrderedDict([('reader', 'configreader')]) def load_defaults(filename, case_sensitive=CASE_SENSITIVE): @@ -37,7 +39,7 @@ def load_defaults(filename, case_sensitive=CASE_SENSITIVE): :rtype: OrderedDict """ configs = OrderedDict() - parser = ConfigParser() + parser = ConfigParser(allow_no_value=ALLOW_NO_VALUE) if case_sensitive: parser.optionxform = str parser.read(filename) @@ -77,21 +79,23 @@ class ConfigReader(object): >>> # config.save() >>> # config.close() - :param str filename: The name of the final config file - :param file_object: A file-like object opened in mode w+ + :param str filename: The name of the final config file. Defaults to `settings.ini`. + :param file_object: A file-like object opened in mode w+. + Defaults to a new StringIO object. :param bool case_sensitive: Determines whether keys should retain their - alphabetic cases or be converted to lowercase + alphabetic cases or be converted to lowercase. Defaults to `True`. :type file_object: Union[_io.TextIOWrapper, TextIO, io.StringIO] + :ivar str filename: Path to the ini file :ivar OrderedDict sections: The sections in the ini file """ - __defaults = OrderedDict([('reader', 'configreader')]) + __defaults = DEFAULT_DICT __default_section = 'main' def __init__(self, filename='settings.ini', file_object=None, case_sensitive=CASE_SENSITIVE): - self.__parser = ConfigParser() + self.__parser = ConfigParser(allow_no_value=ALLOW_NO_VALUE) self.case_sensitive = case_sensitive if case_sensitive: self.__parser.optionxform = str @@ -160,7 +164,7 @@ def _check_file_object(self, file_object): :rtype: Union[StringIO, TextIO] """ if file_object is None: - return IO() + file_object = IO() if not isinstance(file_object, IO): try: mode = file_object.mode @@ -177,6 +181,7 @@ def _check_file_object(self, file_object): raise ModeError("Open file not in mode 'w+'") self.__filename = os.path.abspath(file_object.name) + return file_object @staticmethod @@ -228,6 +233,11 @@ def _write_config(self): self.__file_object.seek(0) self.__parser.write(self.__file_object) + def reload(self): + """Reload the configuration file into memory""" + self.__defaults = DEFAULT_DICT + self._create_config() + def _create_config(self): """Initialise an ini file from the defaults provided @@ -344,7 +354,7 @@ def set(self, key, value, section=None, commit=False): except ValueError: # String interpolation error value = value.replace('%', '%%').replace('%%(', '%(') - self.__parser.set(section, option=key, value=str(value)) + self.__parser.set(section, option=key, value=value) self._write_config() if commit: self.save() @@ -667,18 +677,6 @@ def save(self): self.__file_object.flush() os.fsync(self.__file_object.fileno()) - def to_file(self): - """Same as :func:`~reader.ConfigReader.save` - - .. WARNING:: - This method has been renamed to :func:`~reader.ConfigReader.save`. - This alias will be removed in future versions. - """ - warnings.warn("The method 'to_file' has been renamed to 'save'. " - "This alias will be removed in future versions.", - DeprecationWarning) - self.save() - def close(self, save=False): """Close the file-like object diff --git a/setup.py b/setup.py index 3393e44..6cac57a 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ long_description = 'Usage available at http://github.com/giantas/pyconfigreader' setup(name='pyconfigreader', - version='0.3.2', + version='0.3.3', description='A simple module for handling configurations and config files', long_description=long_description, url='http://github.com/giantas/pyconfigreader', From 0449c7e1a702893481cf4cf1de3b2bd2ac8f964e Mon Sep 17 00:00:00 2001 From: Aswa Paul Date: Mon, 10 Sep 2018 12:13:12 +0300 Subject: [PATCH 2/2] Remove unused import Bump up version to 0.3.3 --- pyconfigreader/reader.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pyconfigreader/reader.py b/pyconfigreader/reader.py index af70446..7526a77 100644 --- a/pyconfigreader/reader.py +++ b/pyconfigreader/reader.py @@ -5,7 +5,6 @@ import ast import json import shutil -import warnings from difflib import SequenceMatcher from pyconfigreader.exceptions import (ModeError, SectionNameNotAllowed, ThresholdError, FileNotFoundError)