Skip to content
This repository was archived by the owner on Dec 5, 2020. It is now read-only.

Commit 7cddced

Browse files
author
Alex Brandt
committed
add read_configuration_files method to Parameters
It was found that rereading configurations at runtime was not possible with the current invocation of crumbs. I've added a small method that simply rereads all configuration files when invoked. The version is also updated in this commit and another release is imminent.
1 parent 42416a8 commit 7cddced

File tree

5 files changed

+49
-13
lines changed

5 files changed

+49
-13
lines changed

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2014 Alex Brandt
3+
Copyright (c) 2015 Alex Brandt
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

crumbs/__init__.py

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2014 by Alex Brandt <[email protected]>
3+
# Copyright (C) 2015 by Alex Brandt <[email protected]>
44
#
55
# crumbs is freely distributable under the terms of an MIT-style license.
66
# See COPYING or http://www.opensource.org/licenses/mit-license.php.
@@ -93,13 +93,15 @@ class Parameters(object):
9393
9494
**Methods**
9595
96-
:``__getitem__``: Return a parameter's value.
97-
:``__init__``: Initialize and return a ``Parameters`` object.
98-
:``add_configuration_file``: Add a file path to be searched for parameter
99-
values.
100-
:``add_parameter``: Add a parameter to ``Parameters`` object.
101-
:``parse``: Prepare ``Parameters`` for queries and ensure
102-
parameter values can be found.
96+
:``__getitem__``: Return a parameter's value.
97+
:``__init__``: Initialize and return a ``Parameters``
98+
object.
99+
:``add_configuration_file``: Add a file path to be searched for parameter
100+
values.
101+
:``add_parameter``: Add a parameter to ``Parameters`` object.
102+
:``parse``: Prepare ``Parameters`` for queries and ensure
103+
parameter values can be found.
104+
:``read_configuration_files``: Read all configuration files' values.
103105
104106
**Properties**
105107
@@ -497,3 +499,22 @@ def parse(self, only_known = False):
497499
self._group_parsers['default'].parse_known_args(args = args, namespace = self._argument_namespace)
498500
else:
499501
self._group_parsers['default'].parse_args(namespace = self._argument_namespace)
502+
503+
def read_configuration_files(self):
504+
'''Explicitly read the configuration files.
505+
506+
Reads all configuration files in this Parameters object. Even if
507+
inotify is watching or a read has already occurred.
508+
509+
.. note::
510+
511+
The order that the configuration files are read is not guaranteed.
512+
513+
'''
514+
515+
for file_name, configuration_parser in self.configuration_files.items():
516+
if os.access(file_name, os.R_OK):
517+
configuration_parser.read(file_name)
518+
else:
519+
logger.warn('could not read %s', file_name)
520+
warnings.warn('could not read {}'.format(file_name), ResourceWarning)

crumbs/information.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# Copyright (C) 2014 by Alex Brandt <[email protected]>
1+
# Copyright (C) 2015 by Alex Brandt <[email protected]>
22
#
33
# crumbs is freely distributable under the terms of an MIT-style license.
44
# See COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
NAME = 'crumbs'
7-
VERSION = '2.0.2'
7+
VERSION = '2.1.0'
88
DESCRIPTION = 'Generalized all-in-one parameters module.'
99
AUTHOR = 'Alex Brandt'
1010
AUTHOR_EMAIL = '[email protected]'
1111
URL = 'https://github.com/alunduil/crumbs'
1212
LICENSE = 'MIT'
1313

14-
COPYRIGHT = '2014'
14+
COPYRIGHT = '2015'

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#
3-
# Copyright (C) 2014 by Alex Brandt <[email protected]>
3+
# Copyright (C) 2015 by Alex Brandt <[email protected]>
44
#
55
# crumbs is freely distributable under the terms of an MIT-style license.
66
# See COPYING or http://www.opensource.org/licenses/mit-license.php.

test_crumbs/test_functional.py

+15
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,21 @@ def test_add_configuration_file(self):
6464

6565
self._assert_configuration_readable()
6666

67+
def test_add_configuration_file_with_explicit_read(self):
68+
'''Parameters().add_configuration_file()—re-read'''
69+
70+
self.p = Parameters()
71+
72+
self._assert_configuration_readable()
73+
74+
with open(self.file_name, 'a') as fh:
75+
fh.write('bar = foo')
76+
77+
self.p.read_configuration_files()
78+
79+
self.assertEqual('bar', self.p['default.foo'])
80+
self.assertEqual('foo', self.p['default.bar'])
81+
6782
@unittest.skipUnless(_pyinotify_loaded, 'inotify module not available')
6883
def test_add_configuration_file_with_inotify(self):
6984
'''Parameters(inotify = True).add_configuration_file()'''

0 commit comments

Comments
 (0)