Exemplo n.º 1
0
def hash_data(buffer, alg, block_size=16*1024):
    if alg not in _calg_map:
        raise InvalidHashAlgorithm()
    
    block_size = int(block_size)
    if block_size <= 0:
        raise ValueError("'block_size' should be a positive integer")

    with _crypt_context() as hCryptProv:
        with _crypt_hash(hCryptProv, _calg_map[alg]) as hCryptHash:
            buffer_len = len(buffer)
            if block_size > buffer_len:
                block_size = buffer_len

            i = 0
            while i < buffer_len:
                count = block_size if (i + block_size <= buffer_len) else (buffer_len - i)
                if not CryptHashData(hCryptHash, _cast(buffer[i:i+count], PBYTE), DWORD(count), DWORD(0)):
                    raise WinError()
                i += count

            dwHashLen = DWORD(sizeof(DWORD))
            dwHashSize = DWORD()
            if not CryptGetHashParam(hCryptHash, HP_HASHSIZE, _cast(pointer(dwHashSize), PBYTE), pointer(dwHashLen), DWORD(0)):
                raise WinError()

            pbHashVal = (BYTE * dwHashSize.value)()
            dwHashLen.value = sizeof(pbHashVal)
            if not CryptGetHashParam(hCryptHash, HP_HASHVAL, pbHashVal, pointer(dwHashLen), DWORD(0)):
                raise WinError()

            return ''.join(map(lambda b: format(b, '02x'), bytes(pbHashVal)))
Exemplo n.º 2
0
    def topic_frame(self,
                    item,
                    date_time=False,
                    labels=True,
                    data_str_max=STR_DATA_SZ,
                    label_str_max=MAX_STR_SZ):

        item = item.upper()

        if date_time and not self._date_time:
            raise TOSDB_DateTimeError("date_time not available for this block")

        self._valid_item(item)

        size = self._topic_count()
        dtss = (_DateTimeStamp * size)()
        labs = [_BUF_(label_str_max + 1) for _ in range(size)]
        strs = [_BUF_(data_str_max + 1) for _ in range(size)]
        labs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in labs])
        strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs])

        _lib_call("TOSDB_GetTopicFrameStrings",
                  self._name,
                  item.encode("ascii"),
                  strs_array,
                  size,
                  data_str_max + 1,
                  labs_array if labels else _ppchar_(),
                  label_str_max + 1,
                  dtss if date_time else _PTR_(_DateTimeStamp)(),
                  arg_list=[
                      _str_, _str_, _ppchar_, _ulong_, _ulong_, _ppchar_,
                      _ulong_,
                      _PTR_(_DateTimeStamp)
                  ])

        if labels:
            l_map = map(_cast_cstr, labs_array)
            _nt_ = _gen_namedtuple(_str_clean(item)[0], _str_clean(*l_map))
            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss]
                return _nt_(*zip(map(_cast_cstr, strs_array), adj_dts))
            else:
                return _nt_(*map(_cast_cstr, strs_array))
        else:
            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss]
                return list(zip(map(_cast_cstr, strs_array), adj_dts))
            else:
                return list(map(_cast_cstr, strs_array))
Exemplo n.º 3
0
    def topic_frame(self, item, date_time=False, labels=True, 
                    data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ):
       
        item = item.upper()
        
        if date_time and not self._date_time:
            raise TOSDB_DateTimeError("date_time not available for this block")
        
        self._valid_item( item)
        
        size = self._topic_count()
        dtss = (_DateTimeStamp * size)()          
        labs = [_BUF_(label_str_max + 1) for _ in range(size)] 
        strs = [_BUF_(data_str_max + 1) for _ in range(size)]    
        labs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in labs]) 
        strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs])
            
        _lib_call("TOSDB_GetTopicFrameStrings", 
                  self._name, 
                  item.encode("ascii"),
                  strs_array, 
                  size, 
                  data_str_max + 1,                                            
                  labs_array if labels else _ppchar_(), 
                  label_str_max + 1,
                  dtss if date_time else _PTR_(_DateTimeStamp)(),
                  arg_types=(_str_, _str_, _ppchar_, _uint32_, _uint32_,
                             _ppchar_, _uint32_, _PTR_(_DateTimeStamp)))    

        if labels:
            l_map = map(_cast_cstr, labs_array)
            _nt_ = _gen_namedtuple(_str_clean(item)[0], _str_clean(*l_map))        
            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss]
                return _nt_(*zip(map(_cast_cstr, strs_array), adj_dts))                
            else:
                return _nt_(*map(_cast_cstr, strs_array))            
        else:
            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss]
                return list(zip(map(_cast_cstr, strs_array), adj_dts))         
            else:
                return list(map(_cast_cstr, strs_array))
Exemplo n.º 4
0
 def items(self, str_max=MAX_STR_SZ):  
     size = self._item_count()  
     strs = [_BUF_(str_max + 1) for _ in range(size)]      
     strs_array = (_pchar_* size)(*[ _cast(s, _pchar_) for s in strs]) 
     
     _lib_call("TOSDB_GetItemNames", 
               self._name, 
               strs_array, 
               size, 
               str_max + 1, 
               arg_types=(_str_, _ppchar_, _uint32_, _uint32_))
     
     return [_cast_cstr(s) for s in strs_array]            
Exemplo n.º 5
0
 def topics_precached(self,  str_max=MAX_STR_SZ):
     size = self._topic_precached_count()
     strs = [_BUF_(str_max + 1) for _ in range(size)]     
     strs_array = (_pchar_* size)(*[ _cast(s, _pchar_) for s in strs])   
         
     _lib_call("TOSDB_GetPreCachedTopicNames", 
               self._name, 
               strs_array, 
               size, 
               str_max + 1, 
               arg_types=(_str_,  _ppchar_, _uint32_, _uint32_))               
     
     return [_cast_cstr(s) for s in strs_array] 
Exemplo n.º 6
0
 def topics( self,  str_max = MAX_STR_SZ ):
     """ Returns the topics currently in the block (and not pre-cached).
     
     str_max: the maximum length of topic strings returned  
     returns -> list of strings 
     """   
     size = self._topic_count()
     # store char buffers
     strs = [ _BUF_(str_max + 1) for _ in range(size)]  
     # cast char buffers into (char*)[ ]
     strs_array = ( _pchar_* size)( *[ _cast(s, _pchar_) for s in strs] )         
     err =_lib_call( "TOSDB_GetTopicNames", 
                     self._name,
                     strs_array,
                     size,
                     str_max + 1,                    
                     arg_list = [ _str_,  _ppchar_, _ulong_, _ulong_ ] )           
     if err:            
         raise TOSDB_CLibError( "error value [ "+ str(err) + " ] returned" +
                                "from library call", "TOSDB_GetTopicNames" )            
     else:
         return [ _cast(ptr, _str_).value.decode() for ptr in strs_array ] 
Exemplo n.º 7
0
    def items(self, str_max=MAX_STR_SZ):
        size = self._item_count()
        strs = [_BUF_(str_max + 1) for _ in range(size)]
        strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs])

        _lib_call("TOSDB_GetItemNames",
                  self._name,
                  strs_array,
                  size,
                  str_max + 1,
                  arg_list=[_str_, _ppchar_, _ulong_, _ulong_])

        return [_cast_cstr(s) for s in strs_array]
Exemplo n.º 8
0
def hash_data(buffer, alg, block_size=16 * 1024):
    if alg not in _calg_map:
        raise InvalidHashAlgorithm()

    block_size = int(block_size)
    if block_size <= 0:
        raise ValueError("'block_size' should be a positive integer")

    with _crypt_context() as hCryptProv:
        with _crypt_hash(hCryptProv, _calg_map[alg]) as hCryptHash:
            buffer_len = len(buffer)
            if block_size > buffer_len:
                block_size = buffer_len

            i = 0
            while i < buffer_len:
                count = block_size if (i + block_size <= buffer_len) else (
                    buffer_len - i)
                if not CryptHashData(hCryptHash,
                                     _cast(buffer[i:i + count], PBYTE),
                                     DWORD(count), DWORD(0)):
                    raise WinError()
                i += count

            dwHashLen = DWORD(sizeof(DWORD))
            dwHashSize = DWORD()
            if not CryptGetHashParam(hCryptHash, HP_HASHSIZE,
                                     _cast(pointer(dwHashSize), PBYTE),
                                     pointer(dwHashLen), DWORD(0)):
                raise WinError()

            pbHashVal = (BYTE * dwHashSize.value)()
            dwHashLen.value = sizeof(pbHashVal)
            if not CryptGetHashParam(hCryptHash, HP_HASHVAL, pbHashVal,
                                     pointer(dwHashLen), DWORD(0)):
                raise WinError()

            return ''.join(map(lambda b: format(b, '02x'), bytes(pbHashVal)))
Exemplo n.º 9
0
 def topics(self, str_max=MAX_STR_SZ):
     """ Returns the topics currently in the block (and not pre-cached).
     
     str_max: the maximum length of topic strings returned  
     returns -> list of strings 
     """
     size = self._topic_count()
     # store char buffers
     strs = [_BUF_(str_max + 1) for _ in range(size)]
     # cast char buffers into (char*)[ ]
     strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs])
     err = _lib_call("TOSDB_GetTopicNames",
                     self._name,
                     strs_array,
                     size,
                     str_max + 1,
                     arg_list=[_str_, _ppchar_, _ulong_, _ulong_])
     if err:
         raise TOSDB_CLibError(
             "error value [ " + str(err) + " ] returned" +
             "from library call", "TOSDB_GetTopicNames")
     else:
         return [_cast(ptr, _str_).value.decode() for ptr in strs_array]
Exemplo n.º 10
0
                   c_ulong as _ulong_, \
                   c_long as _long_, \
                   c_longlong as _longlong_, \
                   c_char_p as _str_, \
                   c_char as _char_, \
                   c_ubyte as _uchar_, \
                   c_int as _int_, \
                   c_void_p as _pvoid_, \
                   c_uint as _uint_, \
                   c_uint32 as _uint32_, \
                   c_uint8 as _uint8_
                   

_pchar_ = _PTR_(_char_)
_ppchar_ = _PTR_(_pchar_)  
_cast_cstr = lambda x: _cast(x,_str_).value.decode()

DLL_BASE_NAME = "tos-databridge"
DLL_DEPENDS1_NAME = "_tos-databridge"
SYS_ARCH_TYPE = "x64" if (_log(_maxsize * 2, 2) > 33) else "x86"
MIN_MARGIN_OF_SAFETY = 10

_REGEX_NON_ALNUM = _compile("[\W+]")
_REGEX_LETTER = _compile("[a-zA-Z]")
_VER_SFFX = '[\d]{1,2}.[\d]{1,2}'
_REGEX_VER_SFFX = _compile('-' + _VER_SFFX + '-')

_REGEX_DLL_NAME = _compile('^(' + DLL_BASE_NAME + '-)' \
                                + _VER_SFFX + '-' \
                                + SYS_ARCH_TYPE +'(.dll)$')
           
Exemplo n.º 11
0
    def item_frame(self,
                   topic,
                   date_time=False,
                   labels=True,
                   data_str_max=STR_DATA_SZ,
                   label_str_max=MAX_STR_SZ):
        """ Return all the most recent item values for a particular topic.

        topic: any topic string in the block
        date_time: (True/False) attempt to retrieve a TOSDB_DateTime object       
        labels: (True/False) pull the item labels with the values 
        data_str_max: the maximum length of string data returned
        label_str_max: the maximum length of item label strings returned

        if labels and date_time are True: returns-> namedtuple of 2tuple
        if labels is True: returns -> namedtuple
        if date_time is True: returns -> list of 2tuple
        else returns-> list
        """
        topic = topic.upper()

        if date_time and not self._date_time:
            raise TOSDB_DateTimeError("date_time not available for this block")

        self._valid_topic(topic)

        size = self._item_count()
        dtss = (_DateTimeStamp * size)()
        # store char buffers
        labs = [_BUF_(label_str_max + 1) for _ in range(size)]
        # cast char buffers int (char*)[ ]
        labs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in labs])
        tbits = type_bits(topic)
        tytup = _type_switch(tbits)

        if tytup[0] is "String":
            # store char buffers
            strs = [_BUF_(data_str_max + 1) for _ in range(size)]
            strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs])
            # cast char buffers int (char*)[ ]
            err = _lib_call("TOSDB_GetItemFrameStrings",
                            self._name,
                            topic.encode("ascii"),
                            strs_array,
                            size,
                            data_str_max + 1,
                            (labs_array if labels else _ppchar_()),
                            label_str_max + 1,
                            (dtss if date_time else _PTR_(_DateTimeStamp)()),
                            arg_list=[
                                _str_, _str_, _ppchar_, _ulong_, _ulong_,
                                _ppchar_, _ulong_,
                                _PTR_(_DateTimeStamp)
                            ])
            if err:
                raise TOSDB_CLibError(
                    "error value [ " + str(err) + " ] " +
                    "returned from library call", "TOSDB_GetItemFrameStrings")

            s_map = map(lambda x: _cast(x, _str_).value.decode(), strs_array)

            if labels:
                l_map = map(lambda x: _cast(x, _str_).value.decode(),
                            labs_array)
                _nt_ = _gen_namedtuple(
                    _str_clean(topic)[0], _str_clean(*l_map))
                if date_time:
                    adj_dts = [TOSDB_DateTime(x) for x in dtss]
                    return _nt_(*zip(s_map, adj_dts))
                else:
                    return _nt_(*s_map)
            else:
                if date_time:
                    adj_dts = [TOSDB_DateTime(x) for x in dtss]
                    return list(zip(s_map, adj_dts))
                else:
                    return list(s_map)
        else:
            num_array = (tytup[1] * size)()
            err = _lib_call("TOSDB_GetItemFrame" + tytup[0] + "s",
                            self._name,
                            topic.encode("ascii"),
                            num_array,
                            size, (labs_array if labels else _ppchar_()),
                            label_str_max + 1,
                            (dtss if date_time else _PTR_(_DateTimeStamp)()),
                            arg_list=[
                                _str_, _str_,
                                _PTR_(tytup[1]), _ulong_, _ppchar_, _ulong_,
                                _PTR_(_DateTimeStamp)
                            ])
            if err:
                raise TOSDB_CLibError(
                    "error value [ " + str(err) + " ] " +
                    "returned from library call",
                    "TOSDB_GetItemFrame" + tytup[0] + "s")

            if labels:
                l_map = map(lambda x: _cast(x, _str_).value.decode(),
                            labs_array)
                _nt_ = _gen_namedtuple(
                    _str_clean(topic)[0], _str_clean(*l_map))
                if date_time:
                    adj_dts = [TOSDB_DateTime(x) for x in dtss]
                    return _nt_(*zip(num_array, adj_dts))
                else:
                    return _nt_(*num_array)
            else:
                if date_time:
                    adj_dts = [TOSDB_DateTime(x) for x in dtss]
                    return list(zip(num_array, adj_dts))
                else:
                    return [_ for _ in num_array]
Exemplo n.º 12
0
                   c_float as _float_, \
                   c_ulong as _ulong_, \
                   c_long as _long_, \
                   c_longlong as _longlong_, \
                   c_char_p as _str_, \
                   c_char as _char_, \
                   c_ubyte as _uchar_, \
                   c_int as _int_, \
                   c_void_p as _pvoid_, \
                   c_uint as _uint_, \
                   c_uint32 as _uint32_, \
                   c_uint8 as _uint8_

_pchar_ = _PTR_(_char_)
_ppchar_ = _PTR_(_pchar_)
_cast_cstr = lambda x: _cast(x, _str_).value.decode()

DLL_BASE_NAME = "tos-databridge"
DLL_DEPENDS1_NAME = "_tos-databridge"
SYS_ARCH_TYPE = "x64" if (_log(_maxsize * 2, 2) > 33) else "x86"
MIN_MARGIN_OF_SAFETY = 10

_REGEX_NON_ALNUM = _compile("[\W+]")
_REGEX_LETTER = _compile("[a-zA-Z]")
_VER_SFFX = '[\d]{1,2}.[\d]{1,2}'
_REGEX_VER_SFFX = _compile('-' + _VER_SFFX + '-')
_REGEX_DLL_NAME = \
  _compile('^('+DLL_BASE_NAME + '-)' + _VER_SFFX + '-' + SYS_ARCH_TYPE +'(.dll)$')

_dll = None
_dll_depend1 = None
Exemplo n.º 13
0
    def stream_snapshot( self, item, topic, date_time = False, 
                         end = -1, beg = 0, smart_size = True, 
                         data_str_max = STR_DATA_SZ ):
        """ Return multiple data-points(a snapshot) from the data-stream
        
        item: any item string in the block
        topic: any topic string in the block
        date_time: (True/False) attempt to retrieve a TOSDB_DateTime object              
        end: index of least recent data-point ( end of the snapshot )
        beg: index of most recent data-point ( beginning of the snapshot )        
        smart_size: limits amount of returned data by data-stream's occupancy
        data_str_max: the maximum length of string data returned

        if date_time is True: returns-> list of 2tuple
        else: returns -> list              
        """     
        item = item.upper()
        topic = topic.upper()
        
        if date_time and not self._date_time:
            raise TOSDB_DateTimeError("date_time not available for this block")
        
        self._valid_item(item)
        self._valid_topic(topic)
        
        if end < 0:
            end += self._block_size
        if beg < 0:
            beg += self._block_size
        if smart_size:            
            end = min(end, self.stream_occupancy( item, topic ) - 1 )                 
        size = (end - beg) + 1
        if beg < 0 or end < 0 \
           or beg >= self._block_size or end >= self._block_size \
           or size <= 0:
            raise TOSDB_IndexError("invalid 'beg' and/or 'end' index value(s)")
        
        dtss = (_DateTimeStamp * size)()
        tbits = type_bits( topic )
        tytup = _type_switch( tbits )
        
        if tytup[0] == "String":
            # store char buffers
            strs = [ _BUF_(  data_str_max +1 ) for _ in range(size)]   
            # cast char buffers into (char*)[ ]          
            strs_array = ( _pchar_ * size)( *[ _cast(s, _pchar_) for s in strs] ) 
            err = _lib_call( "TOSDB_GetStreamSnapshotStrings", 
                             self._name,
                             item.encode("ascii"),
                             topic.encode("ascii"),
                             strs_array,
                             size,
                             data_str_max + 1,                        
                             ( dtss if date_time else _PTR_(_DateTimeStamp)() ),
                             end,
                             beg,
                             arg_list = [ _str_, _str_, _str_, _ppchar_, _ulong_,
                                          _ulong_, _PTR_(_DateTimeStamp), _long_, 
                                          _long_ ] )
            if err:
                raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " +
                                       "returned from library call",                
                                       "TOSDB_GetStreamSnapshotStrings" )
            
            if date_time:
                adj_dts = [ TOSDB_DateTime(x) for x in dtss ]
                return [ _ for _ in \
                         zip( map(lambda x: _cast(x,_str_).value.decode(),
                              strs_array ), adj_dts ) ]        
            else:
                return [ _cast(ptr,_str_).value.decode() for ptr in strs_array ]
        else:
            num_array = (tytup[1] * size)()   
            err = _lib_call( "TOSDB_GetStreamSnapshot"+tytup[0]+"s", 
                             self._name,
                             item.encode("ascii"),
                             topic.encode("ascii"),
                             num_array,
                             size,
                             ( dtss if date_time else _PTR_(_DateTimeStamp)() ),
                             end,
                             beg,
                             arg_list = [ _str_, _str_, _str_, _PTR_(tytup[1]),
                                          _ulong_, _PTR_(_DateTimeStamp), _long_, 
                                          _long_ ] )
            if err:
                raise TOSDB_CLibError("error value [ "+ str(err) + " ] " +
                                      "returned from library call",
                                      "TOSDB_GetStreamSnapshot"+tytup[0]+"s")
            
            if date_time:
                adj_dts = [ TOSDB_DateTime(x) for x in dtss ]
                return [ _ for _ in zip(num_array,adj_dts) ]       
            else:
                return [ _ for _ in num_array ]
Exemplo n.º 14
0
    def topic_frame(self,
                    item,
                    date_time=False,
                    labels=True,
                    data_str_max=STR_DATA_SZ,
                    label_str_max=MAX_STR_SZ):
        """ Return all the most recent topic values for a particular item:
  
        item: any item string in the block
        date_time: (True/False) attempt to retrieve a TOSDB_DateTime object       
        labels: (True/False) pull the topic labels with the values 
        data_str_max: the maximum length of string data returned
        label_str_max: the maximum length of topic label strings returned

        if labels and date_time are True: returns-> namedtuple of 2tuple
        if labels is True: returns -> namedtuple
        if date_time is True: returns -> list of 2tuple
        else returns-> list
        """
        item = item.upper()

        if date_time and not self._date_time:
            raise TOSDB_DateTimeError("date_time not available for this block")

        self._valid_item(item)

        size = self._topic_count()
        dtss = (_DateTimeStamp * size)()

        # cast char buffers int (char*)[ ]
        labs = [_BUF_(label_str_max + 1) for _ in range(size)]
        strs = [_BUF_(data_str_max + 1) for _ in range(size)]

        # cast char buffers int (char*)[ ]
        labs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in labs])
        strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs])

        err = _lib_call("TOSDB_GetTopicFrameStrings",
                        self._name,
                        item.encode("ascii"),
                        strs_array,
                        size,
                        data_str_max + 1,
                        (labs_array if labels else _ppchar_()),
                        label_str_max + 1,
                        (dtss if date_time else _PTR_(_DateTimeStamp)()),
                        arg_list=[
                            _str_, _str_, _ppchar_, _ulong_, _ulong_, _ppchar_,
                            _ulong_,
                            _PTR_(_DateTimeStamp)
                        ])
        if err:
            raise TOSDB_CLibError(
                "error value [ " + str(err) + " ] " +
                "returned from library call", "TOSDB_GetTopicFrameStrings")

        s_map = map(lambda x: _cast(x, _str_).value.decode(), strs_array)

        if labels:
            l_map = map(lambda x: _cast(x, _str_).value.decode(), labs_array)
            _nt_ = _gen_namedtuple(_str_clean(item)[0], _str_clean(*l_map))
            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss]
                return _nt_(*zip(s_map, adj_dts))
            else:
                return _nt_(*s_map)
        else:
            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss]
                return list(zip(s_map, adj_dts))
            else:
                return list(s_map)
Exemplo n.º 15
0
    def stream_snapshot_from_marker(self, item, topic, date_time=False, beg=0, 
                                    margin_of_safety=100, throw_if_data_lost=True,
                                    data_str_max=STR_DATA_SZ):
      
        item = item.upper()
        topic = topic.upper()
        
        if date_time and not self._date_time:
            raise TOSDB_DateTimeError("date_time not available for this block")

        self._valid_item(item)    
        self._valid_topic(topic)
        
        if beg < 0:
            beg += self._block_size   
        if beg < 0 or beg >= self._block_size:
            raise TOSDB_IndexError("invalid 'beg' index value")
        
        if margin_of_safety < MIN_MARGIN_OF_SAFETY:
            raise TOSDB_ValueError("margin_of_safety < MIN_MARGIN_OF_SAFETY")
        
        is_dirty = _uint_()
        _lib_call("TOSDB_IsMarkerDirty", 
                  self._name,
                  item.encode("ascii"), 
                  topic.encode("ascii"),
                  _pointer(is_dirty), 
                  arg_types=(_str_, _str_, _str_, _PTR_(_uint_))) 

        if is_dirty and throw_if_data_lost:
            raise TOSDB_DataError("marker is already dirty")
        
        mpos = _longlong_()
        _lib_call("TOSDB_GetMarkerPosition", 
                  self._name,
                  item.encode("ascii"), 
                  topic.encode("ascii"),
                  _pointer(mpos), 
                  arg_types=(_str_, _str_, _str_, _PTR_(_longlong_)))    
        
        cur_sz = mpos.value - beg + 1
        if cur_sz < 0:
            return None
        
        safe_sz = cur_sz + margin_of_safety
        dtss = (_DateTimeStamp * safe_sz)()
        tbits = type_bits(topic)
        tytup = _type_switch(tbits)
        get_size = _long_()
        
        if tytup[0] == "String":
            strs = [ _BUF_( data_str_max +1) for _ in range(safe_sz) ]   
            strs_array = (_pchar_ * safe_sz)(*[_cast(s,_pchar_) for s in strs]) 

            _lib_call("TOSDB_GetStreamSnapshotStringsFromMarker", 
                      self._name,
                      item.encode("ascii"), 
                      topic.encode("ascii"),
                      strs_array, 
                      safe_sz, 
                      data_str_max + 1,
                      dtss if date_time else _PTR_(_DateTimeStamp)(),     
                      beg, 
                      _pointer(get_size),
                      arg_types=(_str_, _str_, _str_, _ppchar_, _uint32_, _uint32_,
                                 _PTR_(_DateTimeStamp), _long_, _PTR_(_long_))) 
               
            get_size = get_size.value
            if get_size == 0:
                return None
            elif get_size < 0:
                if throw_if_data_lost:
                    raise TOSDB_DataError("data lost behind the 'marker'")
                else:
                    get_size *= -1
            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss[:get_size]]      
                return [sd for sd in zip(map(_cast_cstr, strs_array[:get_size]), adj_dts)]    
            else:
                return [_cast_cstr(s) for s in strs_array[:get_size]]

        else:
            num_array = (tytup[1] * safe_sz)()   
            _lib_call("TOSDB_GetStreamSnapshot" + tytup[0] + "sFromMarker", 
                      self._name,
                      item.encode("ascii"), 
                      topic.encode("ascii"),
                      num_array, 
                      safe_sz,
                      dtss if date_time else _PTR_(_DateTimeStamp)(),     
                      beg, 
                      _pointer(get_size),
                      arg_types=(_str_, _str_, _str_, _PTR_(tytup[1]), _uint32_, 
                                 _PTR_(_DateTimeStamp), _long_, _PTR_(_long_))) 
          
            get_size = get_size.value
            if get_size == 0:
                return None
            elif get_size < 0:
                if throw_if_data_lost:
                    raise TOSDB_DataError("data lost behind the 'marker'")
                else:
                    get_size *= -1            
            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss[:get_size]]
                return [nd for nd in zip(num_array[:get_size], adj_dts)]       
            else:
                return [n for n in num_array[:get_size]]    
Exemplo n.º 16
0
    def item_frame(self, topic, date_time=False, labels=True, 
                   data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ):
       
        topic = topic.upper()
        
        if date_time and not self._date_time:
            raise TOSDB_DateTimeError("date_time not available for this block")
        
        self._valid_topic(topic)
        
        size = self._item_count()
        dtss = (_DateTimeStamp * size)()  
        labs = [_BUF_(label_str_max+1) for _ in range(size)] 
        labs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in labs])  
        tbits = type_bits(topic)
        tytup = _type_switch(tbits)
        
        if tytup[0] is "String":      
            strs = [_BUF_( data_str_max + 1) for _ in range(size)]
            strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs]) 

            _lib_call("TOSDB_GetItemFrameStrings", 
                      self._name, 
                      topic.encode("ascii"),
                      strs_array, 
                      size, 
                      data_str_max + 1,
                      labs_array if labels else _ppchar_(), 
                      label_str_max + 1,
                      dtss if date_time else _PTR_(_DateTimeStamp)(),
                      arg_types=(_str_, _str_, _ppchar_, _uint32_, _uint32_,
                                 _ppchar_, _uint32_, _PTR_(_DateTimeStamp)))       

            if labels:
                l_map = map(_cast_cstr, labs_array)
                _nt_ = _gen_namedtuple(_str_clean(topic)[0], _str_clean(*l_map)) 
                if date_time:
                    adj_dts = [TOSDB_DateTime(x) for x in dtss]
                    return _nt_(*zip(map(_cast_cstr, strs_array),adj_dts))
                else:
                    return _nt_(*map(_cast_cstr, strs_array))    
            else:
                if date_time:
                    adj_dts = [TOSDB_DateTime(x) for x in dtss]
                    return list(zip(map(_cast_cstr, strs_array),adj_dts))
                else:
                    return list(map(_cast_cstr, strs_array))  
         
        else: 
            num_array = (tytup[1] * size)()   
            _lib_call("TOSDB_GetItemFrame"+tytup[0]+"s", 
                      self._name, 
                      topic.encode("ascii"), 
                      num_array, 
                      size,
                      labs_array if labels else _ppchar_(), 
                      label_str_max + 1,
                      dtss if date_time else _PTR_(_DateTimeStamp)(),       
                      arg_types=(_str_, _str_, _PTR_(tytup[1]), _uint32_, _ppchar_, 
                                 _uint32_, _PTR_(_DateTimeStamp)))    
        
            if labels:   
                l_map = map(_cast_cstr, labs_array)
                _nt_ = _gen_namedtuple(_str_clean(topic)[0], _str_clean(*l_map)) 
                if date_time:
                    adj_dts = [TOSDB_DateTime(x) for x in dtss]
                    return _nt_(*zip(num_array,adj_dts))
                else:
                    return _nt_(*num_array)   
            else:
                if date_time:
                    adj_dts = [TOSDB_DateTime(x) for x in dtss]
                    return list(zip(num_array,adj_dts))
                else:
                    return [n for n in num_array]    
Exemplo n.º 17
0
    def stream_snapshot(self, item, topic, date_time=False, end=-1, beg=0, 
                        smart_size=True, data_str_max=STR_DATA_SZ):

        item = item.upper()
        topic = topic.upper()
        
        if date_time and not self._date_time:
            raise TOSDB_DateTimeError("date_time not available for this block")
        
        self._valid_item(item)
        self._valid_topic(topic)
        
        if end < 0:
            end += self._block_size
        if beg < 0:
            beg += self._block_size         
        size = (end - beg) + 1
        if beg < 0 \
           or end < 0 \
           or beg >= self._block_size \
           or end >= self._block_size \
           or size <= 0:
            raise TOSDB_IndexError("invalid 'beg' and/or 'end' index value(s)")

        if smart_size:
            so = self.stream_occupancy(item, topic)
            if so == 0 or so <= beg:
                return []
            end = min(end, so - 1)
            beg = min(beg, so - 1)
            size = (end - beg) + 1
        
        dtss = (_DateTimeStamp * size)()
        tbits = type_bits(topic)
        tytup = _type_switch(tbits)
        
        if tytup[0] == "String":      
            strs = [_BUF_( data_str_max +1) for _ in range(size)]              
            strs_array = (_pchar_ * size)(*[ _cast(s, _pchar_) for s in strs]) 

            _lib_call("TOSDB_GetStreamSnapshotStrings", 
                      self._name,
                      item.encode("ascii"), 
                      topic.encode("ascii"),
                      strs_array, 
                      size, 
                      data_str_max + 1,                
                      dtss if date_time else _PTR_(_DateTimeStamp)(),
                      end, 
                      beg,
                      arg_types=(_str_, _str_, _str_, _ppchar_, _uint32_, _uint32_, 
                                 _PTR_(_DateTimeStamp), _long_, _long_))   

            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss]         
                return [sd for sd in zip(map(_cast_cstr, strs_array), adj_dts)]        
            else:        
                return [_cast_cstr(s) for s in strs_array]

        else: 
            num_array = (tytup[1] * size)()   
            _lib_call("TOSDB_GetStreamSnapshot"+tytup[0]+"s", 
                      self._name,
                      item.encode("ascii"), 
                      topic.encode("ascii"),
                      num_array, 
                      size,
                      dtss if date_time else _PTR_(_DateTimeStamp)(),
                      end, 
                      beg,
                      arg_types=(_str_, _str_, _str_, _PTR_(tytup[1]), _uint32_, 
                                 _PTR_(_DateTimeStamp), _long_, _long_)) 
           
            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss]
                return [nd for nd in zip(num_array,adj_dts)]       
            else:
                return [n for n in num_array]
Exemplo n.º 18
0
    def item_frame( self, topic, date_time = False, labels = True, 
                    data_str_max = STR_DATA_SZ, label_str_max = MAX_STR_SZ ):
        """ Return all the most recent item values for a particular topic.

        topic: any topic string in the block
        date_time: (True/False) attempt to retrieve a TOSDB_DateTime object       
        labels: (True/False) pull the item labels with the values 
        data_str_max: the maximum length of string data returned
        label_str_max: the maximum length of item label strings returned

        if labels and date_time are True: returns-> namedtuple of 2tuple
        if labels is True: returns -> namedtuple
        if date_time is True: returns -> list of 2tuple
        else returns-> list
        """      
        topic = topic.upper()
        
        if date_time and not self._date_time:
            raise TOSDB_DateTimeError("date_time not available for this block")
        
        self._valid_topic(topic)
        
        size = self._item_count()
        dtss = (_DateTimeStamp * size)()
        # store char buffers
        labs = [ _BUF_(label_str_max+1) for _ in range(size)] 
        # cast char buffers int (char*)[ ]
        labs_array = ( _pchar_ * size)( *[ _cast(s, _pchar_) for s in labs] )  
        tbits = type_bits( topic )
        tytup = _type_switch( tbits )
        
        if tytup[0] is "String":                     
            # store char buffers 
            strs = [ _BUF_(  data_str_max + 1 ) for _ in range(size)]               
            strs_array = ( _pchar_ * size)( *[ _cast(s, _pchar_) for s in strs] ) 
            # cast char buffers int (char*)[ ]                
            err = _lib_call( "TOSDB_GetItemFrameStrings", 
                             self._name,
                             topic.encode("ascii"),
                             strs_array,
                             size,
                             data_str_max + 1,                       
                             ( labs_array if labels else _ppchar_() ),
                             label_str_max + 1,
                             ( dtss if date_time else _PTR_(_DateTimeStamp)() ),
                             arg_list = [ _str_, _str_, _ppchar_, _ulong_,
                                          _ulong_, _ppchar_, _ulong_, 
                                          _PTR_(_DateTimeStamp) ] )
            if err:
                raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " +
                                       "returned from library call",
                                       "TOSDB_GetItemFrameStrings" )
            
            s_map = map( lambda x: _cast(x, _str_).value.decode(), strs_array )
            
            if labels:
                l_map = map(lambda x: _cast(x, _str_).value.decode(),labs_array)
                _nt_ = _gen_namedtuple(_str_clean(topic)[0], _str_clean(*l_map))             
                if date_time:
                    adj_dts = [ TOSDB_DateTime(x) for x in dtss ]
                    return _nt_( *zip(s_map,adj_dts) )                        
                else:
                    return _nt_( *s_map )             
            else:
                if date_time:
                    adj_dts = [ TOSDB_DateTime(x) for x in dtss ]
                    return list( zip(s_map,adj_dts) )
                else:
                    return list( s_map )                  
        else: 
            num_array =  (tytup[1] * size)()   
            err = _lib_call( "TOSDB_GetItemFrame"+tytup[0]+"s", 
                             self._name,
                             topic.encode("ascii"),
                             num_array,
                             size,
                             ( labs_array if labels else _ppchar_() ),
                             label_str_max + 1,
                             ( dtss if date_time else _PTR_(_DateTimeStamp)() ),                           
                             arg_list = [ _str_, _str_, _PTR_(tytup[1]),
                                          _ulong_, _ppchar_, _ulong_, 
                                          _PTR_(_DateTimeStamp) ] )
            if err:
                raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " +
                                       "returned from library call",
                                       "TOSDB_GetItemFrame" + tytup[0] + "s" )
            
            if labels:                                
                l_map = map(lambda x: _cast(x,_str_).value.decode(), labs_array)
                _nt_ = _gen_namedtuple(_str_clean(topic)[0], _str_clean(*l_map))              
                if date_time:
                    adj_dts = [ TOSDB_DateTime(x) for x in dtss ]
                    return _nt_( *zip(num_array,adj_dts) )                        
                else:
                    return _nt_( *num_array )                            
            else:
                if date_time:
                    adj_dts = [ TOSDB_DateTime(x) for x in dtss ]
                    return list( zip(num_array,adj_dts) )
                else:
                    return [ _ for _ in num_array ]    
Exemplo n.º 19
0
    def topic_frame( self, item, date_time = False, labels = True, 
                     data_str_max = STR_DATA_SZ, label_str_max = MAX_STR_SZ ):
        """ Return all the most recent topic values for a particular item:
  
        item: any item string in the block
        date_time: (True/False) attempt to retrieve a TOSDB_DateTime object       
        labels: (True/False) pull the topic labels with the values 
        data_str_max: the maximum length of string data returned
        label_str_max: the maximum length of topic label strings returned

        if labels and date_time are True: returns-> namedtuple of 2tuple
        if labels is True: returns -> namedtuple
        if date_time is True: returns -> list of 2tuple
        else returns-> list
        """      
        item = item.upper()
        
        if date_time and not self._date_time:
            raise TOSDB_DateTimeError("date_time not available for this block")
        
        self._valid_item(  item )
        
        size = self._topic_count()
        dtss = (_DateTimeStamp * size)()
        
        # cast char buffers int (char*)[ ]                
        labs = [ _BUF_( label_str_max + 1 ) for _ in range(size)] 
        strs = [ _BUF_( data_str_max + 1 ) for _ in range(size)]
        
        # cast char buffers int (char*)[ ]                      
        labs_array = ( _pchar_ * size)( *[ _cast(s, _pchar_) for s in labs] ) 
        strs_array = ( _pchar_ * size)( *[ _cast(s, _pchar_) for s in strs] )
        
        err = _lib_call( "TOSDB_GetTopicFrameStrings", 
                         self._name,
                         item.encode("ascii"),
                         strs_array,
                         size,
                         data_str_max + 1,                                            
                         ( labs_array if labels else _ppchar_() ),
                         label_str_max + 1,
                         ( dtss if date_time else _PTR_(_DateTimeStamp)() ),
                         arg_list = [ _str_, _str_, _ppchar_, _ulong_, _ulong_,
                                      _ppchar_, _ulong_, _PTR_(_DateTimeStamp) ] )
        if err:
            raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " +
                                   "returned from library call",
                                   "TOSDB_GetTopicFrameStrings" )
        
        s_map = map( lambda x: _cast(x, _str_).value.decode(), strs_array )
        
        if labels:
            l_map = map( lambda x: _cast(x, _str_).value.decode(), labs_array )
            _nt_ = _gen_namedtuple( _str_clean(item)[0], _str_clean( *l_map ) )            
            if date_time:
                adj_dts = [TOSDB_DateTime( x ) for x in dtss]
                return _nt_( *zip( s_map, adj_dts ) )                        
            else:
                return _nt_( *s_map )                
        else:
            if date_time:
                adj_dts = [TOSDB_DateTime( x ) for x in dtss]
                return list( zip(s_map, adj_dts) )             
            else:
                return list( s_map )
Exemplo n.º 20
0
    def stream_snapshot(self,
                        item,
                        topic,
                        date_time=False,
                        end=-1,
                        beg=0,
                        smart_size=True,
                        data_str_max=STR_DATA_SZ):
        """ Return multiple data-points(a snapshot) from the data-stream
        
        item: any item string in the block
        topic: any topic string in the block
        date_time: (True/False) attempt to retrieve a TOSDB_DateTime object              
        end: index of least recent data-point ( end of the snapshot )
        beg: index of most recent data-point ( beginning of the snapshot )        
        smart_size: limits amount of returned data by data-stream's occupancy
        data_str_max: the maximum length of string data returned

        if date_time is True: returns-> list of 2tuple
        else: returns -> list              
        """
        item = item.upper()
        topic = topic.upper()

        if date_time and not self._date_time:
            raise TOSDB_DateTimeError("date_time not available for this block")

        self._valid_item(item)
        self._valid_topic(topic)

        if end < 0:
            end += self._block_size
        if beg < 0:
            beg += self._block_size
        if smart_size:
            end = min(end, self.stream_occupancy(item, topic) - 1)
        size = (end - beg) + 1
        if beg < 0 or end < 0 \
           or beg >= self._block_size or end >= self._block_size \
           or size <= 0:
            raise TOSDB_IndexError("invalid 'beg' and/or 'end' index value(s)")

        dtss = (_DateTimeStamp * size)()
        tbits = type_bits(topic)
        tytup = _type_switch(tbits)

        if tytup[0] == "String":
            # store char buffers
            strs = [_BUF_(data_str_max + 1) for _ in range(size)]
            # cast char buffers into (char*)[ ]
            strs_array = (_pchar_ * size)(*[_cast(s, _pchar_) for s in strs])
            err = _lib_call("TOSDB_GetStreamSnapshotStrings",
                            self._name,
                            item.encode("ascii"),
                            topic.encode("ascii"),
                            strs_array,
                            size,
                            data_str_max + 1,
                            (dtss if date_time else _PTR_(_DateTimeStamp)()),
                            end,
                            beg,
                            arg_list=[
                                _str_, _str_, _str_, _ppchar_, _ulong_,
                                _ulong_,
                                _PTR_(_DateTimeStamp), _long_, _long_
                            ])
            if err:
                raise TOSDB_CLibError(
                    "error value [ " + str(err) + " ] " +
                    "returned from library call",
                    "TOSDB_GetStreamSnapshotStrings")

            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss]
                return [ _ for _ in \
                         zip( map(lambda x: _cast(x,_str_).value.decode(),
                              strs_array ), adj_dts ) ]
            else:
                return [_cast(ptr, _str_).value.decode() for ptr in strs_array]
        else:
            num_array = (tytup[1] * size)()
            err = _lib_call("TOSDB_GetStreamSnapshot" + tytup[0] + "s",
                            self._name,
                            item.encode("ascii"),
                            topic.encode("ascii"),
                            num_array,
                            size,
                            (dtss if date_time else _PTR_(_DateTimeStamp)()),
                            end,
                            beg,
                            arg_list=[
                                _str_, _str_, _str_,
                                _PTR_(tytup[1]), _ulong_,
                                _PTR_(_DateTimeStamp), _long_, _long_
                            ])
            if err:
                raise TOSDB_CLibError(
                    "error value [ " + str(err) + " ] " +
                    "returned from library call",
                    "TOSDB_GetStreamSnapshot" + tytup[0] + "s")

            if date_time:
                adj_dts = [TOSDB_DateTime(x) for x in dtss]
                return [_ for _ in zip(num_array, adj_dts)]
            else:
                return [_ for _ in num_array]
Exemplo n.º 21
0
                   c_float as _float_, \
                   c_ulong as _ulong_, \
                   c_long as _long_, \
                   c_longlong as _longlong_, \
                   c_char_p as _str_, \
                   c_char as _char_, \
                   c_ubyte as _uchar_, \
                   c_int as _int_, \
                   c_void_p as _pvoid_, \
                   c_uint as _uint_, \
                   c_uint32 as _uint32_, \
                   c_uint8 as _uint8_

_pchar_ = _PTR_(_char_)
_ppchar_ = _PTR_(_pchar_)
_cast_cstr = lambda x: _cast(x, _str_).value.decode()

_gen_str_buffers = lambda sz, n: [_BUF_(sz) for _ in range(n)]
_gen_str_buffers_ptrs = lambda bufs: (_pchar_ * len(bufs))(
    *[_cast(b, _pchar_) for b in bufs])

_map_cstr = _partial(map, _cast_cstr)
_map_dt = _partial(map, TOSDB_DateTime)
_zip_cstr_dt = lambda cstr, dt: zip(_map_cstr(cstr), _map_dt(dt))

DLL_BASE_NAME = "tos-databridge"
DLL_DEPENDS1_NAME = "_tos-databridge"
SYS_ARCH_TYPE = "x64" if (_log(_maxsize * 2, 2) > 33) else "x86"
MIN_MARGIN_OF_SAFETY = 10

_REGEX_NON_ALNUM = _compile("[\W+]")