| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -95,7 +95,10 @@ class SectionConstraint(object): | |||
| 95 | 95 | """Constrains a ConfigParser to only option commands which are constrained to | |
| 96 | 96 | always use the section we have been initialized with. | |
| 97 | 97 | ||
| 98 | - It supports all ConfigParser methods that operate on an option""" | ||
| 98 | + It supports all ConfigParser methods that operate on an option. | ||
| 99 | + | ||
| 100 | + :note: | ||
| 101 | + If used as a context manager, will release the wrapped ConfigParser.""" | ||
| 99 | 102 | __slots__ = ("_config", "_section_name") | |
| 100 | 103 | _valid_attrs_ = ("get_value", "set_value", "get", "set", "getint", "getfloat", "getboolean", "has_option", | |
| 101 | 104 | "remove_section", "remove_option", "options") | |
@@ -129,6 +132,12 @@ def release(self): | |||
| 129 | 132 | """Equivalent to GitConfigParser.release(), which is called on our underlying parser instance""" | |
| 130 | 133 | return self._config.release() | |
| 131 | 134 | ||
| 135 | + def __enter__(self): | ||
| 136 | + return self | ||
| 137 | + | ||
| 138 | + def __exit__(self, exception_type, exception_value, traceback): | ||
| 139 | + self.release() | ||
| 140 | + | ||
| 132 | 141 | ||
| 133 | 142 | class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, object)): | |
| 134 | 143 | ||
@@ -145,7 +154,9 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje | |||
| 145 | 154 | ||
| 146 | 155 | :note: | |
| 147 | 156 | The config is case-sensitive even when queried, hence section and option names | |
| 148 | - must match perfectly.""" | ||
| 157 | + must match perfectly. | ||
| 158 | + If used as a context manager, will release the locked file. This parser cannot | ||
| 159 | + be used afterwards.""" | ||
| 149 | 160 | ||
| 150 | 161 | #{ Configuration | |
| 151 | 162 | # The lock type determines the type of lock to use in new configuration readers. | |
@@ -216,6 +227,12 @@ def __del__(self): | |||
| 216 | 227 | # NOTE: only consistent in PY2 | |
| 217 | 228 | self.release() | |
| 218 | 229 | ||
| 230 | + def __enter__(self): | ||
| 231 | + return self | ||
| 232 | + | ||
| 233 | + def __exit__(self, exception_type, exception_value, traceback): | ||
| 234 | + self.release() | ||
| 235 | + | ||
| 219 | 236 | def release(self): | |
| 220 | 237 | """Flush changes and release the configuration write lock. This instance must not be used anymore afterwards. | |
| 221 | 238 | In Python 3, it's required to explicitly release locks and flush changes, as __del__ is not called | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -403,7 +403,7 @@ def config_writer(self, config_level="repository"): | |||
| 403 | 403 | """ | |
| 404 | 404 | :return: | |
| 405 | 405 | GitConfigParser allowing to write values of the specified configuration file level. | |
| 406 | - Config writers should be retrieved, used to change the configuration ,and written | ||
| 406 | + Config writers should be retrieved, used to change the configuration, and written | ||
| 407 | 407 | right away as they will lock the configuration file in question and prevent other's | |
| 408 | 408 | to write it. | |
| 409 | 409 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -139,57 +139,49 @@ def check_test_value(cr, value): | |||
| 139 | 139 | ||
| 140 | 140 | # PREPARE CONFIG FILE A | |
| 141 | 141 | fpa = os.path.join(rw_dir, 'a') | |
| 142 | - cw = GitConfigParser(fpa, read_only=False) | ||
| 143 | - write_test_value(cw, 'a') | ||
| 144 | - | ||
| 145 | - fpb = os.path.join(rw_dir, 'b') | ||
| 146 | - fpc = os.path.join(rw_dir, 'c') | ||
| 147 | - cw.set_value('include', 'relative_path_b', 'b') | ||
| 148 | - cw.set_value('include', 'doesntexist', 'foobar') | ||
| 149 | - cw.set_value('include', 'relative_cycle_a_a', 'a') | ||
| 150 | - cw.set_value('include', 'absolute_cycle_a_a', fpa) | ||
| 151 | - cw.release() | ||
| 142 | + with GitConfigParser(fpa, read_only=False) as cw: | ||
| 143 | + write_test_value(cw, 'a') | ||
| 144 | + | ||
| 145 | + fpb = os.path.join(rw_dir, 'b') | ||
| 146 | + fpc = os.path.join(rw_dir, 'c') | ||
| 147 | + cw.set_value('include', 'relative_path_b', 'b') | ||
| 148 | + cw.set_value('include', 'doesntexist', 'foobar') | ||
| 149 | + cw.set_value('include', 'relative_cycle_a_a', 'a') | ||
| 150 | + cw.set_value('include', 'absolute_cycle_a_a', fpa) | ||
| 152 | 151 | assert os.path.exists(fpa) | |
| 153 | 152 | ||
| 154 | 153 | # PREPARE CONFIG FILE B | |
| 155 | - cw = GitConfigParser(fpb, read_only=False) | ||
| 156 | - write_test_value(cw, 'b') | ||
| 157 | - cw.set_value('include', 'relative_cycle_b_a', 'a') | ||
| 158 | - cw.set_value('include', 'absolute_cycle_b_a', fpa) | ||
| 159 | - cw.set_value('include', 'relative_path_c', 'c') | ||
| 160 | - cw.set_value('include', 'absolute_path_c', fpc) | ||
| 161 | - cw.release() | ||
| 154 | + with GitConfigParser(fpb, read_only=False) as cw: | ||
| 155 | + write_test_value(cw, 'b') | ||
| 156 | + cw.set_value('include', 'relative_cycle_b_a', 'a') | ||
| 157 | + cw.set_value('include', 'absolute_cycle_b_a', fpa) | ||
| 158 | + cw.set_value('include', 'relative_path_c', 'c') | ||
| 159 | + cw.set_value('include', 'absolute_path_c', fpc) | ||
| 162 | 160 | ||
| 163 | 161 | # PREPARE CONFIG FILE C | |
| 164 | - cw = GitConfigParser(fpc, read_only=False) | ||
| 165 | - write_test_value(cw, 'c') | ||
| 166 | - cw.release() | ||
| 162 | + with GitConfigParser(fpc, read_only=False) as cw: | ||
| 163 | + write_test_value(cw, 'c') | ||
| 167 | 164 | ||
| 168 | - cr = GitConfigParser(fpa, read_only=True) | ||
| 169 | - for tv in ('a', 'b', 'c'): | ||
| 170 | - check_test_value(cr, tv) | ||
| 171 | - # end for each test to verify | ||
| 172 | - assert len(cr.items('include')) == 8, "Expected all include sections to be merged" | ||
| 173 | - cr.release() | ||
| 165 | + with GitConfigParser(fpa, read_only=True) as cr: | ||
| 166 | + for tv in ('a', 'b', 'c'): | ||
| 167 | + check_test_value(cr, tv) | ||
| 168 | + # end for each test to verify | ||
| 169 | + assert len(cr.items('include')) == 8, "Expected all include sections to be merged" | ||
| 174 | 170 | ||
| 175 | 171 | # test writable config writers - assure write-back doesn't involve includes | |
| 176 | - cw = GitConfigParser(fpa, read_only=False, merge_includes=True) | ||
| 177 | - tv = 'x' | ||
| 178 | - write_test_value(cw, tv) | ||
| 179 | - cw.release() | ||
| 172 | + with GitConfigParser(fpa, read_only=False, merge_includes=True) as cw: | ||
| 173 | + tv = 'x' | ||
| 174 | + write_test_value(cw, tv) | ||
| 180 | 175 | ||
| 181 | - cr = GitConfigParser(fpa, read_only=True) | ||
| 182 | - self.failUnlessRaises(cp.NoSectionError, check_test_value, cr, tv) | ||
| 183 | - cr.release() | ||
| 176 | + with GitConfigParser(fpa, read_only=True) as cr: | ||
| 177 | + self.failUnlessRaises(cp.NoSectionError, check_test_value, cr, tv) | ||
| 184 | 178 | ||
| 185 | 179 | # But can make it skip includes alltogether, and thus allow write-backs | |
| 186 | - cw = GitConfigParser(fpa, read_only=False, merge_includes=False) | ||
| 187 | - write_test_value(cw, tv) | ||
| 188 | - cw.release() | ||
| 180 | + with GitConfigParser(fpa, read_only=False, merge_includes=False) as cw: | ||
| 181 | + write_test_value(cw, tv) | ||
| 189 | 182 | ||
| 190 | - cr = GitConfigParser(fpa, read_only=True) | ||
| 191 | - check_test_value(cr, tv) | ||
| 192 | - cr.release() | ||
| 183 | + with GitConfigParser(fpa, read_only=True) as cr: | ||
| 184 | + check_test_value(cr, tv) | ||
| 193 | 185 | ||
| 194 | 186 | def test_rename(self): | |
| 195 | 187 | file_obj = self._to_memcache(fixture_path('git_config')) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,3 +1,5 @@ | |||
| 1 | + -r requirements.txt | ||
| 2 | + | ||
| 1 | 3 | coverage | |
| 2 | 4 | flake8 | |
| 3 | 5 | nose | |
| Back | FazBrowse Home | New Git URL |
0 commit comments