Esempio n. 1
0
 def func(ret, *args, **kwargs):
     rval = ret["response"] and from_pickle(do_unpickle(ret["response"]))
     reca = ret["recached"] and from_pickle(do_unpickle(ret["recached"]))
     # recache all indicated objects
     [clean_object_caches(obj) for obj in reca]
     if f:
         return f(rval, *args, **kwargs)
     else:
         return rval
Esempio n. 2
0
 def func(ret, *args, **kwargs):
     rval = ret["response"] and from_pickle(do_unpickle(
         ret["response"]))
     reca = ret["recached"] and from_pickle(do_unpickle(
         ret["recached"]))
     # recache all indicated objects
     [clean_object_caches(obj) for obj in reca]
     if f:
         return f(rval, *args, **kwargs)
     else:
         return rval
Esempio n. 3
0
    def __init__(self, *args, **kwargs):
        """
         If we have an Attribute, then we'll prepopulate our instance with the fields we'd expect it
         to have based on the Attribute. attr_key, attr_category, attr_value, attr_strvalue, attr_type,
         and attr_lockstring all refer to the corresponding Attribute fields. The initial data of the form fields will
         similarly be populated.

        """
        super(AttributeForm, self).__init__(*args, **kwargs)
        attr_key = None
        attr_category = None
        attr_value = None
        attr_strvalue = None
        attr_type = None
        attr_lockstring = None
        if hasattr(self.instance, 'attribute'):
            attr_key = self.instance.attribute.db_key
            attr_category = self.instance.attribute.db_category
            attr_value = self.instance.attribute.db_value
            attr_strvalue = self.instance.attribute.db_strvalue
            attr_type = self.instance.attribute.db_attrtype
            attr_lockstring = self.instance.attribute.db_lock_storage
            self.fields['attr_key'].initial = attr_key
            self.fields['attr_category'].initial = attr_category
            self.fields['attr_type'].initial = attr_type
            self.fields['attr_value'].initial = attr_value
            self.fields['attr_strvalue'].initial = attr_strvalue
            self.fields['attr_lockstring'].initial = attr_lockstring
        self.instance.attr_key = attr_key
        self.instance.attr_category = attr_category
        self.instance.attr_value = attr_value
        self.instance.deserialized_value = from_pickle(attr_value)
        self.instance.attr_strvalue = attr_strvalue
        self.instance.attr_type = attr_type
        self.instance.attr_lockstring = attr_lockstring
Esempio n. 4
0
 def __init__(self, *args, **kwargs):
     """
      If we have an Attribute, then we'll prepopulate our instance with the fields we'd expect it
      to have based on the Attribute. attr_key, attr_category, attr_value, attr_strvalue, attr_type,
      and attr_lockstring all refer to the corresponding Attribute fields. The initial data of the form fields will
      similarly be populated.
     """
     super(AttributeForm, self).__init__(*args, **kwargs)
     attr_key = None
     attr_category = None
     attr_value = None
     attr_strvalue = None
     attr_type = None
     attr_lockstring = None
     if hasattr(self.instance, 'attribute'):
         attr_key = self.instance.attribute.db_key
         attr_category = self.instance.attribute.db_category
         attr_value = self.instance.attribute.db_value
         attr_strvalue = self.instance.attribute.db_strvalue
         attr_type = self.instance.attribute.db_attrtype
         attr_lockstring = self.instance.attribute.db_lock_storage
         self.fields['attr_key'].initial = attr_key
         self.fields['attr_category'].initial = attr_category
         self.fields['attr_type'].initial = attr_type
         self.fields['attr_value'].initial = attr_value
         self.fields['attr_strvalue'].initial = attr_strvalue
         self.fields['attr_lockstring'].initial = attr_lockstring
     self.instance.attr_key = attr_key
     self.instance.attr_category = attr_category
     self.instance.attr_value = attr_value
     self.instance.deserialized_value = from_pickle(attr_value)
     self.instance.attr_strvalue = attr_strvalue
     self.instance.attr_type = attr_type
     self.instance.attr_lockstring = attr_lockstring
Esempio n. 5
0
 def __value_get(self):
     """
     Getter. Allows for `value = self.value`.
     We cannot cache here since it makes certain cases (such
     as storing a dbobj which is then deleted elsewhere) out-of-sync.
     The overhead of unpickling seems hard to avoid.
     """
     return from_pickle(self.db_value, db_obj=self)
Esempio n. 6
0
 def __value_get(self):
     """
     Getter. Allows for `value = self.value`.
     We cannot cache here since it makes certain cases (such
     as storing a dbobj which is then deleted elsewhere) out-of-sync.
     The overhead of unpickling seems hard to avoid.
     """
     return from_pickle(self.db_value, db_obj=self)
Esempio n. 7
0
def fix_broken_attributes(broken_object):
    """
    Patch to fix objects broken by broken formset for Attributes in django admin, where validation errors convert
    the Attributes to unicode.
    """
    from ast import literal_eval
    from evennia.utils.dbserialize import from_pickle
    for attr in broken_object.attributes.all():
        try:
            attr.value = from_pickle(literal_eval(attr.value))
        except (ValueError, SyntaxError) as err:
            print("Error for attr %s: %s" % (attr.key, err))
            continue
Esempio n. 8
0
 def save(self, commit=True):
     """
     One thing we want to do here is the or None checks, because forms are saved with an empty
     string rather than null from forms, usually, and the Handlers may handle empty strings
     differently than None objects. So for consistency with how things are handled in game,
     we'll try to make sure that empty form fields will be None, rather than ''.
     """
     # we are spoofing an Attribute for the Handler that will be called
     instance = self.instance
     instance.attr_key = self.cleaned_data["attr_key"] or "no_name_entered_for_attribute"
     instance.attr_category = self.cleaned_data["attr_category"] or None
     instance.attr_value = self.cleaned_data["attr_value"]
     # convert the serialized string value into an object, if necessary, for AttributeHandler
     instance.attr_value = from_pickle(instance.attr_value)
     instance.attr_type = self.cleaned_data["attr_type"] or None
     instance.attr_lockstring = self.cleaned_data["attr_lockstring"]
     return instance
Esempio n. 9
0
 def save(self, commit=True):
     """
     One thing we want to do here is the or None checks, because forms are saved with an empty
     string rather than null from forms, usually, and the Handlers may handle empty strings
     differently than None objects. So for consistency with how things are handled in game,
     we'll try to make sure that empty form fields will be None, rather than ''.
     """
     # we are spoofing an Attribute for the Handler that will be called
     instance = self.instance
     instance.attr_key = self.cleaned_data['attr_key'] or "no_name_entered_for_attribute"
     instance.attr_category = self.cleaned_data['attr_category'] or None
     instance.attr_value = self.cleaned_data['attr_value'] or None
     # convert the serialized string value into an object, if necessary, for AttributeHandler
     instance.attr_value = from_pickle(instance.attr_value)
     instance.attr_strvalue = self.cleaned_data['attr_strvalue'] or None
     instance.attr_type = self.cleaned_data['attr_type'] or None
     instance.attr_lockstring = self.cleaned_data['attr_lockstring']
     return instance
Esempio n. 10
0
    def __init__(self, *args, **kwargs):
        """
         If we have an Attribute, then we'll prepopulate our instance with the fields we'd expect it
         to have based on the Attribute. attr_key, attr_category, attr_value, attr_type,
         and attr_lockstring all refer to the corresponding Attribute fields. The initial data of the form fields will
         similarly be populated.

        """
        super().__init__(*args, **kwargs)
        attr_key = None
        attr_category = None
        attr_value = None
        attr_type = None
        attr_lockstring = None
        if hasattr(self.instance, "attribute"):
            attr_key = self.instance.attribute.db_key
            attr_category = self.instance.attribute.db_category
            attr_value = self.instance.attribute.db_value
            attr_type = self.instance.attribute.db_attrtype
            attr_lockstring = self.instance.attribute.db_lock_storage
            self.fields["attr_key"].initial = attr_key
            self.fields["attr_category"].initial = attr_category
            self.fields["attr_type"].initial = attr_type
            self.fields["attr_value"].initial = attr_value
            self.fields["attr_lockstring"].initial = attr_lockstring
        self.instance.attr_key = attr_key
        self.instance.attr_category = attr_category
        self.instance.attr_value = attr_value

        # prevent from being transformed to str
        if isinstance(attr_value, (set, _SaverSet)):
            self.fields["attr_value"].disabled = True

        self.instance.deserialized_value = from_pickle(attr_value)
        self.instance.attr_type = attr_type
        self.instance.attr_lockstring = attr_lockstring
Esempio n. 11
0
 def __value_get(self):
     "Getter. Allows for value = self.value"
     return from_pickle(self.db_value, db_obj=self)
Esempio n. 12
0
    def executecode(self, source, environment):
        """
        Remote code execution

        source - Python code snippet
        environment - pickled dictionary of environment
                      variables. They are stored in
                      two keys "normal" and "objs" where
                      normal holds a dictionary of
                      normally pickled python objects
                      wheras objs points to a dictionary
                      of database represenations ((app,key),id).

        The environment's entries will be made available as
        local variables during the execution. Normal eval
        results will be returned as-is. For more complex
        code snippets (run by exec), the _return function
        is available: All data sent to _return(retval) will
        be returned from this system whenever the system
        finishes. Multiple calls to _return will result in
        a list being return. The return value is pickled
        and thus allows for returning any pickleable data.

        """

        class Ret(object):
            "Helper class for holding returns from exec"
            def __init__(self):
                self.returns = []
            def __call__(self, *args, **kwargs):
                self.returns.extend(list(args))
            def get_returns(self):
                lr = len(self.returns)
                val = lr and (lr == 1 and self.returns[0] or self.returns) or None
                if val not in (None, [], ()):
                    return do_pickle(to_pickle(val))
                else:
                    return ""
        _return = Ret()

        available_vars = {'_return': _return}
        if environment:
            # load environment
            try:
                environment = from_pickle(do_unpickle(environment))
                available_vars.update(environment)
            except Exception:
                logger.log_trace()
        # try to execute with eval first
        try:
            ret = eval(source, {}, available_vars)
            if ret not in (None, [], ()):
                ret = _return.get_returns() or do_pickle(to_pickle(ret))
            else:
                ret = ""
        except Exception:
            # use exec instead
            exec source in available_vars
            ret = _return.get_returns()
        # get the list of affected objects to recache
        objs = PROC_MODIFIED_OBJS.values()
        # we need to include the locations too, to update their content caches
        objs = objs + list(set([o.location for o in objs
                                if hasattr(o, "location") and o.location]))
        #print "objs:", objs
        #print "to_pickle", to_pickle(objs, emptypickle=False, do_pickle=False)
        if objs not in (None, [], ()):
            to_recache = do_pickle(to_pickle(objs))
        else:
            to_recache = ""
        # empty the list without loosing memory reference
        #PROC_MODIFIED_OBJS[:] = []
        PROC_MODIFIED_OBJS.clear() #TODO - is this not messing anything up?
        return {'response': ret,
                'recached': to_recache}
Esempio n. 13
0
    def executecode(self, source, environment):
        """
        Remote code execution

        source - Python code snippet
        environment - pickled dictionary of environment
                      variables. They are stored in
                      two keys "normal" and "objs" where
                      normal holds a dictionary of
                      normally pickled python objects
                      wheras objs points to a dictionary
                      of database represenations ((app,key),id).

        The environment's entries will be made available as
        local variables during the execution. Normal eval
        results will be returned as-is. For more complex
        code snippets (run by exec), the _return function
        is available: All data sent to _return(retval) will
        be returned from this system whenever the system
        finishes. Multiple calls to _return will result in
        a list being return. The return value is pickled
        and thus allows for returning any pickleable data.

        """
        class Ret(object):
            "Helper class for holding returns from exec"

            def __init__(self):
                self.returns = []

            def __call__(self, *args, **kwargs):
                self.returns.extend(list(args))

            def get_returns(self):
                lr = len(self.returns)
                val = lr and (lr == 1 and self.returns[0]
                              or self.returns) or None
                if val not in (None, [], ()):
                    return do_pickle(to_pickle(val))
                else:
                    return ""

        _return = Ret()

        available_vars = {'_return': _return}
        if environment:
            # load environment
            try:
                environment = from_pickle(do_unpickle(environment))
                available_vars.update(environment)
            except Exception:
                logger.log_trace()
        # try to execute with eval first
        try:
            ret = eval(source, {}, available_vars)
            if ret not in (None, [], ()):
                ret = _return.get_returns() or do_pickle(to_pickle(ret))
            else:
                ret = ""
        except Exception:
            # use exec instead
            exec source in available_vars
            ret = _return.get_returns()
        # get the list of affected objects to recache
        objs = PROC_MODIFIED_OBJS.values()
        # we need to include the locations too, to update their content caches
        objs = objs + list(
            set([
                o.location
                for o in objs if hasattr(o, "location") and o.location
            ]))
        #print "objs:", objs
        #print "to_pickle", to_pickle(objs, emptypickle=False, do_pickle=False)
        if objs not in (None, [], ()):
            to_recache = do_pickle(to_pickle(objs))
        else:
            to_recache = ""
        # empty the list without loosing memory reference
        #PROC_MODIFIED_OBJS[:] = []
        PROC_MODIFIED_OBJS.clear()  #TODO - is this not messing anything up?
        return {'response': ret, 'recached': to_recache}