示例#1
0
    def __init__(self, name, **kwargs):
        self.allow_none = get_bool(kwargs.pop('ignore_case', False))
        self.format = kwargs.pop('format', "%a %b %d %H:%M:%S %Y")
        self.formats = kwargs.pop('formats', [])
        self.min_datetime = kwargs.pop('min_datetime',
            datetime.datetime.min.replace(year=1900))
        self.max_datetime = kwargs.pop('max_datetime', datetime.datetime.max)
        #print self.min_datetime
        XCheck.__init__(self, name, **kwargs)
        self._object_atts.extend(['allow_none', 'format', 'formats',
            'min_datetime', 'max_datetime'])


        if not isinstance(self.min_datetime, datetime.datetime):
            try:
                self.min_datetime = datetime.datetime.strptime(
                    self.min_datetime, self.format)
            except:
                raise self.error("cannot parse minimum date")

        if not isinstance(self.max_datetime, datetime.datetime):
            try:
                self.max_datetime = datetime.datetime.strptime(
                    self.max_datetime, self.format)
            except:
                raise self.error("cannot parse maximum date")

        self.as_datetime =  False
        self.as_struct = False
        self.as_string =  True
        self.as_date = False
示例#2
0
def convert_data_type(value, type_, name=None):
    """Convert from QR string representation to database data type."""
    # Enums are stored as int in the database
    if type_ == 'int':
        return int(value)
    if type_ == 'float':
        return float(value)
    if type_ == 'bool':
        return utils.get_bool(value)
    if type_ == 'str':
        return value  # Value is already a str
    if type_ == 'Enum':
        return get_decompressed_name(value, name)
    raise ValueError(f'Type {type_} not recognized')
示例#3
0
    def save_current_world(self, save_name):
        """ Save the current_world to save name given. 
        
        Parameters
        save_name  String used to generate savefile
        """

        override = True

        if save_name in listdir("Saves"):
            override = get_bool("Do you want to override previous save? ")

        if not override:
            save_name = input("What is the new save name? ")

        with open(f"Saves/{save_name}", "w+") as new_world_save:
            new_world_save.write(self.world.stringify())
示例#4
0
def load_checker(node, namespace=None):
    "takes an elementtree.element node and recreates the checker"
    node = get_elem(node)
    if node.tag not in LOAD_RULES:
        raise XCheckLoadError, "Cannot create checker for %s" % node.tag

    if namespace is None:
        namespace = {}

    new_atts = {}

    # Selection definition node uses delimiter, but selection check doesn't
    delimiter = node.get('delimiter', ',')

    for key in node.keys():
        if key == 'delimiter':
            if node.tag == 'list':
                val = delimiter

        val = node.get(key)

        if key=='values':
            val = map(str.strip, val.split(delimiter))

        if key in INT_ATTRIBUTES:
            val = num_or_inf(val, int)

        if key in BOOL_ATTRIBUTES:
            val = get_bool(val)

        if key in STR_OR_NONE_ATTRIBUTES:
            if val.lower() == 'none':
                val = None

        if key in ['min', 'max', 'min_value', 'max_value']:
            if node.tag == 'int':
                val = num_or_inf(val, int)
            elif node.tag == 'decimal':
                val = num_or_inf(val, float)

        if key in ['error']:
            if val in globals():
                _val = globals()[val]
            elif val in __builtins__:
                _val = __builtins__[val]
            elif val in namespace:
                _val = namespace[val]
            else:
                _val = UnmatchedError
            val = _val

        if key in ['callback']:
            if val in globals():
                _val = globals()[val]
            elif val in locals():
                _val = locals()[val]
            elif val in __builtins__:
                _val = __builtins__[val]
            elif callable(val):
                _val = val
            elif val in namespace:
                _val = namespace[val]
            else:
                try:
                    _val = eval(val)
                except Exception as E:
                    raise BadCallbackError(E)
            val = _val

        new_atts[key] = val

    ch = LOAD_RULES[node.tag](**new_atts)


    attributes = node.find('attributes')
    if attributes is not None:
        for att in attributes:
            ch.addattribute(load_checker(att, namespace))

    children = node.find('children')
    if children is not None:
        for child in children:
            ch.add_child(load_checker(child, namespace))

    return ch