示例#1
0
 def close(self):
     if self.closed:
         raise Exception(
             "Trying to close an already closed file '%s' of class %s" %
             (self.name, classname(self)))
     self._close()
     self.closed = True
示例#2
0
 def _object(self, x, mode, level):
     "Encode object of an arbitrary class."
     def getstate(x):
         getstate = getattr(x, '__getstate__', None)
         if getstate is None: return None
         if not isbound(getstate): return None               # 'x' can be a class! then __getstate__ won't work
         state = getstate()
         if isdict(state): return state
         return {'__state__': state}                         # wrap up a non-dict state in dict
     
     def getnewargs(x):
         getnewargs = getattr(x, '__getnewargs__', None)
         if getnewargs is None: return ()
         if not isbound(getnewargs): return ()
         return getnewargs()
     
     # discover typename
     typename = classname(x, full=True)
     
     # extract newargs
     newargs = getnewargs(x)
     
     # extract state
     state = getstate(x)                                     # try to pick object's state from __getstate__
     if state is None:                                       # otherwise use __dict__
         try: 
             state = x.__dict__
         except:
             raise Exception("dast.Encoder, can't encode object %s of type <%s>, "
                             "unable to retrieve its __dict__ property" % (repr(x), typename))
     
     fmt = getattr(x, '__dast_format__', {}) if mode == 2 else None
     self._generic_object(mode, level, typename, args2 = newargs, kwargs2 = state, fmt = fmt)
示例#3
0
文件: dast.py 项目: mwojnars/nifty
 def _object(self, x, mode, level):
     """Encode object of an arbitrary class. The following approaches to get the state are tried, in this order:
     - x.__getstate__()
     - x.__dict__; if x.__transient__ list of attribute names is present, these attributes are excluded from the state.
     Regardless of how the state was retrieved, arguments for new() are retrieved from __getnewargs__() 
     if present and serialized as unnamed arguments, too.
     """
     def getstate(x):
         getstate = getattr(x, '__getstate__', None)
         if getstate is None: return None
         if not isbound(getstate): return None               # 'x' can be a class! then __getstate__ won't work
         state = getstate()
         if isdict(state): return state
         return {'__state__': state}                         # wrap up a non-dict state in dict
     
     def getnewargs(x):
         getnewargs = getattr(x, '__getnewargs__', None)
         if getnewargs is None: return ()
         if not isbound(getnewargs): return ()
         return getnewargs()
     
     # discover typename
     typename = classname(x, full=True)
     
     # extract newargs
     newargs = getnewargs(x)
     
     # extract state
     state = getstate(x)                                     # try to pick object's state from __getstate__
     if state is None:                                       # otherwise use __dict__
         try: 
             state = x.__dict__
         except:
             raise Exception("dast.Encoder, can't encode object %s of type <%s>, "
                             "unable to retrieve its __dict__ property" % (repr(x), typename))
         
         trans = getattr(x, "__transient__", None)           # remove attributes declared as transient
         if trans:
             state = state.copy()
             for attr in trans: state.pop(attr, None)
     
     # parse __dast_format__
     if mode == 2:
         fmt = getattr(x, '__dast_format__', {})
         fmt_self = fmt.get('__self__', None)                # what format to use for the object itself
         if fmt_self is not None:
             mode = fmt_self
     else:
         fmt = None
     
     self._generic_object(mode, level, typename, args2 = newargs, kwargs2 = state, fmt = fmt)
示例#4
0
    def _object(self, x, mode, level):
        """Encode object of an arbitrary class. The following approaches to get the state are tried, in this order:
        - x.__getstate__()
        - x.__dict__; if x.__transient__ list of attribute names is present, these attributes are excluded from the state.
        Regardless of how the state was retrieved, arguments for new() are retrieved from __getnewargs__() 
        if present and serialized as unnamed arguments, too.
        """
        def getstate(x):
            getstate = getattr(x, '__getstate__', None)
            if getstate is None: return None
            if not isbound(getstate):
                return None  # 'x' can be a class! then __getstate__ won't work
            state = getstate()
            if isdict(state): return state
            return {'__state__': state}  # wrap up a non-dict state in dict

        def getnewargs(x):
            getnewargs = getattr(x, '__getnewargs__', None)
            if getnewargs is None: return ()
            if not isbound(getnewargs): return ()
            return getnewargs()

        # discover typename
        typename = classname(x, full=True)

        # extract newargs
        newargs = getnewargs(x)

        # extract state
        state = getstate(x)  # try to pick object's state from __getstate__
        if state is None:  # otherwise use __dict__
            try:
                state = x.__dict__
            except:
                raise Exception(
                    "dast.Encoder, can't encode object %s of type <%s>, "
                    "unable to retrieve its __dict__ property" %
                    (repr(x), typename))

            trans = getattr(x, "__transient__",
                            None)  # remove attributes declared as transient
            if trans:
                state = state.copy()
                for attr in trans:
                    state.pop(attr, None)

        # parse __dast_format__
        if mode == 2:
            fmt = getattr(x, '__dast_format__', {})
            fmt_self = fmt.get(
                '__self__', None)  # what format to use for the object itself
            if fmt_self is not None:
                mode = fmt_self
        else:
            fmt = None

        self._generic_object(mode,
                             level,
                             typename,
                             args2=newargs,
                             kwargs2=state,
                             fmt=fmt)
示例#5
0
 def __deepcopy__(self, memo):
     if not self.closed:
         raise Exception("Can't deep copy an open %s object." %
                         classname(self))
     return deepcopy(self, memo)
示例#6
0
 def _prolog(self):
     if self.iterating:
         raise Exception(
             "%s '%s' opened for iteration twice, before previous iteration has completed"
             % (classname(self), self.name))
     self.iterating = True
示例#7
0
文件: files.py 项目: victorliun/nifty
 def __deepcopy__(self, memo):
     if not self.closed: raise Exception("Can't deep copy an open %s object." % classname(self))
     return deepcopy(self, memo)
示例#8
0
文件: files.py 项目: victorliun/nifty
 def _prolog(self):        
     if self.iterating: raise Exception("%s '%s' opened for iteration twice, before previous iteration has completed" % (classname(self), self.name))
     self.iterating = True
示例#9
0
文件: files.py 项目: victorliun/nifty
 def close(self):
     if self.closed: raise Exception("Trying to close an already closed file '%s' of class %s" % (self.name, classname(self)))
     self._close()
     self.closed = True