| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c05ef0e commit 7d00fa4
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -346,3 +346,36 @@ def read_only(self): | |||
| 346 | 346 | True if this instance may change the configuration file | |
| 347 | 347 | """ | |
| 348 | 348 | return self._read_only | |
| 349 | + | ||
| 350 | + def get_value(self, section, option): | ||
| 351 | + """ | ||
| 352 | + Returns | ||
| 353 | + a properly typed value, either int, float or string | ||
| 354 | + Raises TypeError in case the value could not be understood | ||
| 355 | + """ | ||
| 356 | + valuestr = self.get(section, option) | ||
| 357 | + types = ( long, float ) | ||
| 358 | + for numtype in types: | ||
| 359 | + try: | ||
| 360 | + val = numtype( valuestr ) | ||
| 361 | + | ||
| 362 | + # truncated value ? | ||
| 363 | + if val != float( valuestr ): | ||
| 364 | + continue | ||
| 365 | + | ||
| 366 | + return val | ||
| 367 | + except (ValueError,TypeError): | ||
| 368 | + continue | ||
| 369 | + # END for each numeric type | ||
| 370 | + | ||
| 371 | + # try boolean values as git uses them | ||
| 372 | + vl = valuestr.lower() | ||
| 373 | + if vl == 'false': | ||
| 374 | + return False | ||
| 375 | + if vl == 'true': | ||
| 376 | + return True | ||
| 377 | + | ||
| 378 | + if not isinstance( valuestr, basestring ): | ||
| 379 | + raise TypeError( "Invalid value type: only int, long, float and str are allowed", valuestr ) | ||
| 380 | + | ||
| 381 | + return valuestr | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -72,6 +72,8 @@ def test_base(self): | |||
| 72 | 72 | for option in r_config.options(section): | |
| 73 | 73 | num_options += 1 | |
| 74 | 74 | val = r_config.get(section, option) | |
| 75 | + val_typed = r_config.get_value(section, option) | ||
| 76 | + assert isinstance(val_typed, (bool, long, float, basestring)) | ||
| 75 | 77 | assert val | |
| 76 | 78 | assert "\n" not in option | |
| 77 | 79 | assert "\n" not in val | |
| Back | FazBrowse Home | New Git URL |
0 commit comments