Skip to content

Commit

Permalink
Version 0.3.3
Browse files Browse the repository at this point in the history
Merge branch 'dev'
  • Loading branch information
omushpapa committed Sep 10, 2018
2 parents 02d378d + 0449c7e commit 03e1338
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
39 changes: 18 additions & 21 deletions pyconfigreader/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import ast
import json
import shutil
import warnings
from difflib import SequenceMatcher
from pyconfigreader.exceptions import (ModeError, SectionNameNotAllowed,
ThresholdError, FileNotFoundError)
Expand All @@ -23,6 +22,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):
Expand All @@ -37,7 +38,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)
Expand Down Expand Up @@ -77,21 +78,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
Expand Down Expand Up @@ -160,7 +163,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
Expand All @@ -177,6 +180,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
Expand Down Expand Up @@ -228,6 +232,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
Expand Down Expand Up @@ -344,7 +353,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()
Expand Down Expand Up @@ -667,18 +676,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
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 03e1338

Please sign in to comment.