PyUtilib / pyutilib

A collection of general Python utilities, including logging and file IO, subprocess management, plugin systems, and workflow management.
BSD 3-Clause "New" or "Revised" License
34 stars 20 forks source link

Adding an ImmutableConfigValue class #95

Closed michaelbynum closed 4 years ago

michaelbynum commented 4 years ago

Changes proposed in this PR:

This PR adds a class derived from ConfigValue which can be made temporarily immutable by setting the _mutable attribute to False.

Legal Acknowledgement

By contributing to this software project, I agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.
michaelbynum commented 4 years ago

@jsiirola Please let me know what you think of this implementation. The idea is that you just set the _mutable flag to False when you want the config value to be immutable.

coveralls commented 4 years ago

Coverage Status

Coverage increased (+0.1%) to 61.194% when pulling 9cc57f0d42443f6961e6b480393b3e3063dccd97 on michaelbynum:immutable-config into ad6043c52aff125d135413f36e0db39dcd54bf83 on PyUtilib:master.

jsiirola commented 4 years ago

What about this:

class ImmutableConfigValue(ConfigValue):
    def set_value(self, value):
        raise RuntimeError(str(self) + ' is currently immutable.')

class MarkImmutable(object):
    def __init__(self, *args):
        self._locked = []
        try:
            for arg in args:
                if type(arg) is not ConfigValue:
                    raise ValueError()
                arg.__class__ = ImmutableConfigValue
                self._locked.append(arg)
        except:
            self.release_lock()

    def release_lock(self):
        for arg in self._locked:
            arg.__class__ = ConfigValue
        self._locked = []

...this would allow you to lock configurations that you didn't actually create. With a little more work, we could make it so that the locks are either exclusive (or support nested).

michaelbynum commented 4 years ago

@jsiirola Good suggestion.

codecov-io commented 4 years ago

Codecov Report

Merging #95 into master will increase coverage by 0.08%. The diff coverage is 86.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #95      +/-   ##
==========================================
+ Coverage   63.13%   63.21%   +0.08%     
==========================================
  Files          87       87              
  Lines        8788     8818      +30     
==========================================
+ Hits         5548     5574      +26     
- Misses       3240     3244       +4     
Impacted Files Coverage Δ
pyutilib/misc/config.py 92.27% <86.66%> (-0.24%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update ad6043c...9cc57f0. Read the comment docs.