コード例 #1
0
ファイル: maxdepth.py プロジェクト: anonymouzz/lyx2ebook
def facade_factory(stream):
    '''
    Generate a facade class (we need a class so that we can register as
    a subclass of the correct stream interface).
    '''
    class Facade(object):

        __slots__ = ['_Facade__stream', '_Facade__memory', '__weakref__']

        def __init__(self, stream, memory):
            self.__stream = stream
            self.__memory = memory
            memory.deepest = stream

        def __getitem__(self, spec):
            if isinstance(spec, slice) and open_stop(spec):
                return Facade(self.__stream.__getitem__(spec), self.__memory)
            else:
                return self.__stream.__getitem__(spec)

        def __bool__(self):
            return bool(self.__stream)

        def __nonzero__(self):
            return self.__bool__()

        def __len__(self):
            return len(self.__stream)

        def __repr__(self):
            return repr(self.__stream)

        def __str__(self):
            return str(self.__stream)

        def __hash__(self):
            return hash(self.__stream)

        def __eq__(self, other):
            return isinstance(other,
                              Facade) and self.__stream == other.__stream

        @property
        def location(self):
            return self.__stream.location

        @property
        def line_number(self):
            return self.__stream.line_number

        @property
        def line_offset(self):
            return self.__stream.line_offset

        @property
        def character_offset(self):
            return self.__stream.character_offset

        @property
        def text(self):
            # this is a hack needed for inter-op with python regexps, which
            # only accept strings.  it's identical to the hack in Regexp().
            try:
                return self.__stream.text
            except AttributeError:
                return self.__stream

        @property
        def source(self):
            return self.__stream.source

        @property
        def stream(self):
            return self.__stream

    if isinstance(stream, LocationStream):
        LocationStream.register(Facade)
    else:
        SimpleStream.register(Facade)
    memory = Memory(stream)
    facade = Facade(stream, memory)
    return (facade, memory)
コード例 #2
0
ファイル: maxdepth.py プロジェクト: alexmac/ifdef-refactor
def facade_factory(stream):
    '''
    Generate a facade class (we need a class so that we can register as
    a subclass of the correct stream interface).
    '''
    
    class Facade(object):
        
        __slots__ = ['_Facade__stream', '_Facade__memory', '__weakref__']
        
        def __init__(self, stream, memory):
            self.__stream = stream
            self.__memory = memory
            memory.deepest = stream
                
        def __getitem__(self, spec):
            if isinstance(spec, slice) and open_stop(spec):
                return Facade(self.__stream.__getitem__(spec), self.__memory)
            else:
                return self.__stream.__getitem__(spec)

        def __bool__(self):
            return bool(self.__stream)
    
        def __nonzero__(self):
            return self.__bool__() 
    
        def __len__(self):
            return len(self.__stream)

        def __repr__(self):
            return repr(self.__stream)
    
        def __str__(self):
            return str(self.__stream)
    
        def __hash__(self):
            return hash(self.__stream)
        
        def __eq__(self, other):
            return isinstance(other, Facade) and self.__stream == other.__stream
        
        @property
        def location(self):
            return self.__stream.location

        @property
        def line_number(self):
            return self.__stream.line_number
        
        @property
        def line_offset(self):
            return self.__stream.line_offset
        
        @property
        def character_offset(self):
            return self.__stream.character_offset
   
        @property
        def text(self):
            # this is a hack needed for inter-op with python regexps, which
            # only accept strings.  it's identical to the hack in Regexp().
            try:
                return self.__stream.text
            except AttributeError:
                return self.__stream
    
        @property
        def source(self):
            return self.__stream.source
        
        @property
        def stream(self):
            return self.__stream

    if isinstance(stream, LocationStream):
        LocationStream.register(Facade)
    else:
        SimpleStream.register(Facade)
    memory = Memory(stream)
    facade = Facade(stream, memory)
    return (facade, memory)
コード例 #3
0
         return BitString(value, len(value) * 8)
             
     @staticmethod
     def from_str(value, encoding=None, errors=STRICT):
         '''
         Create a BitString from a string.
         '''
         if encoding:
             return BitString.from_bytearray(value.encode(encoding=encoding, 
                                                          errors=errors))
         else:
             return BitString.from_bytearray(value.encode(errors=errors))
         
 # pylint: disable-msg=E1101
 # python 2.6 doesn't know about abcs
 SimpleStream.register(BitString)
         
 def unpack_length(length):
     '''
     Length is in bits, unless a decimal is specified, in which case it
     it has the structure bytes.bits.  Obviously this is ambiguous with float
     values (eg 3.1 or 3.10), but since we only care about bits 0-7 we can
     avoid any issues by requiring that range. 
     '''
     if isinstance(length, str):
         try:
             length = int(length, 0)
         except ValueError:
             length = float(length)
     if isinstance(length, int):
         return length