Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/virtualenv/config/convert.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
| author | guerler |
|---|---|
| date | Fri, 31 Jul 2020 00:32:28 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 0:d30785e31577 | 1:56ad4e20f292 |
|---|---|
| 1 from __future__ import absolute_import, unicode_literals | |
| 2 | |
| 3 import logging | |
| 4 import os | |
| 5 | |
| 6 | |
| 7 class TypeData(object): | |
| 8 def __init__(self, default_type, as_type): | |
| 9 self.default_type = default_type | |
| 10 self.as_type = as_type | |
| 11 | |
| 12 def __repr__(self): | |
| 13 return "{}(base={}, as={})".format(self.__class__.__name__, self.default_type, self.as_type) | |
| 14 | |
| 15 def convert(self, value): | |
| 16 return self.default_type(value) | |
| 17 | |
| 18 | |
| 19 class BoolType(TypeData): | |
| 20 BOOLEAN_STATES = { | |
| 21 "1": True, | |
| 22 "yes": True, | |
| 23 "true": True, | |
| 24 "on": True, | |
| 25 "0": False, | |
| 26 "no": False, | |
| 27 "false": False, | |
| 28 "off": False, | |
| 29 } | |
| 30 | |
| 31 def convert(self, value): | |
| 32 if value.lower() not in self.BOOLEAN_STATES: | |
| 33 raise ValueError("Not a boolean: %s" % value) | |
| 34 return self.BOOLEAN_STATES[value.lower()] | |
| 35 | |
| 36 | |
| 37 class NoneType(TypeData): | |
| 38 def convert(self, value): | |
| 39 if not value: | |
| 40 return None | |
| 41 return str(value) | |
| 42 | |
| 43 | |
| 44 class ListType(TypeData): | |
| 45 def _validate(self): | |
| 46 """""" | |
| 47 | |
| 48 def convert(self, value, flatten=True): | |
| 49 if isinstance(value, (str, bytes)): | |
| 50 value = filter(None, [x.strip() for x in value.splitlines()]) | |
| 51 values = list(value) | |
| 52 result = [] | |
| 53 for value in values: | |
| 54 sub_values = value.split(os.pathsep) | |
| 55 result.extend(sub_values) | |
| 56 converted = [self.as_type(i) for i in result] | |
| 57 return converted | |
| 58 | |
| 59 | |
| 60 def convert(value, as_type, source): | |
| 61 """Convert the value as a given type where the value comes from the given source""" | |
| 62 try: | |
| 63 return as_type.convert(value) | |
| 64 except Exception as exception: | |
| 65 logging.warning("%s failed to convert %r as %r because %r", source, value, as_type, exception) | |
| 66 raise | |
| 67 | |
| 68 | |
| 69 _CONVERT = {bool: BoolType, type(None): NoneType, list: ListType} | |
| 70 | |
| 71 | |
| 72 def get_type(action): | |
| 73 default_type = type(action.default) | |
| 74 as_type = default_type if action.type is None else action.type | |
| 75 return _CONVERT.get(default_type, TypeData)(default_type, as_type) | |
| 76 | |
| 77 | |
| 78 __all__ = ( | |
| 79 "convert", | |
| 80 "get_type", | |
| 81 ) |
