コード例 #1
0
ファイル: account.py プロジェクト: GammaC0de/pyload
 def parse_traffic(self, string):  # returns kbyte
     return parse.bytesize(string) >> 10
コード例 #2
0
 def parse_traffic(self, string):  # returns kbyte
     return parse.bytesize(string) >> 10
コード例 #3
0
ファイル: parser.py プロジェクト: pyblub/notpyload
class ConfigOption(object):

    __slots__ = [
        'allowed_values', 'default', 'desc', 'label', 'parser', 'type', 'value'
    ]

    DEFAULT_TYPE = InputType.Str

    _convert_map = {
        InputType.NA:
        lambda x: x,
        InputType.Str:
        lambda x: "" if x is None else str(x),
        InputType.Int:
        lambda x: 0 if x is None else int(x),
        InputType.File:
        lambda x: "" if x is None else fullpath(x),
        InputType.Folder:
        lambda x: "" if x is None else os.path.dirname(fullpath(x)),
        InputType.Password:
        lambda x: "" if x is None else str(x),
        InputType.Bool:
        lambda x: boolean(x) if isinstance(x, str) else bool(x),
        InputType.Float:
        lambda x: 0.0 if x is None else float(x),
        InputType.Tristate:
        lambda x: x if x is None else bool(x),
        InputType.Octal:
        lambda x: 0 if x is None else oct(x),
        InputType.Size:
        lambda x: 0 if x is None else (-1 if str(x) == '-1' else bytesize(x)),
        InputType.Address:
        lambda x: (None, None)
        if x is None else (endpoint if isendpoint(x) else socket)(x),
        InputType.Bytes:
        lambda x: b"" if x is None else bytes(x),
        InputType.StrList:
        lambda l: [str(x) for x in l] if isiterable(l) else entries(l)
    }

    def __init__(self,
                 parser,
                 value,
                 label=None,
                 desc=None,
                 allowed_values=None,
                 input_type=None):
        self.parser = parser

        self.type = None
        self.value = None
        self.default = None
        self.label = None
        self.desc = None
        self.allowed_values = ()

        self._set_type(input_type)
        self._set_value(value)
        self._set_allowed(allowed_values)
        self._set_info(label, desc)

    def _set_info(self, label, desc):
        self.label = "" if label is None else str(label)
        self.desc = "" if desc is None else str(desc)

    def _set_type(self, input_type):
        if not input_type:
            input_type = self.DEFAULT_TYPE
        if input_type not in InputType:
            raise InvalidValueError(input_type)
        self.type = input_type

    def _set_value(self, value):
        self.value = self.default = self._normalize_value(value)

    def _set_allowed(self, allowed):
        if not allowed:
            self.allowed_values = ()
            return

        self.allowed_values = tuple(self._normalize_value(v) for v in allowed)

    def _normalize_value(self, value):
        return self._convert_map[self.type](value)

    def reset(self):
        self.value = self.default

    def get(self):
        return self.value

    def get_default(self):
        return self.default

    def set(self, value, store=True):
        norm_value = self._normalize_value(value)
        if self.allowed_values and norm_value not in self.allowed_values:
            raise InvalidValueError(value)
        if self.value == norm_value:
            return None
        self.value = norm_value
        if store:
            self.parser.store()