def _item_frame_numbers(self, tytup, topic, date_time, labels, size, label_str_max): nums = (tytup[1] * size)() dts = (_DateTimeStamp * size)() labs = _gen_str_buffers(label_str_max + 1, size) plabs = _gen_str_buffers_ptrs(labs) _lib_call("TOSDB_GetItemFrame" + tytup[0] + "s", self._name, topic.encode("ascii"), nums, size, plabs if labels else _ppchar_(), label_str_max + 1, dts if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _PTR_(tytup[1]), _uint32_, _ppchar_, _uint32_, _PTR_(_DateTimeStamp))) dat = list(zip(nums, _map_dt(dts)) if date_time else nums) if labels: nt = _gen_namedtuple( _str_clean(topic)[0], _str_clean(*_map_cstr(plabs))) return nt(*dat) else: return dat
def _item_frame_strings(self, topic, date_time, labels, size, data_str_max, label_str_max): strs = _gen_str_buffers(data_str_max + 1, size) labs = _gen_str_buffers(label_str_max + 1, size) pstrs = _gen_str_buffers_ptrs(strs) plabs = _gen_str_buffers_ptrs(labs) dts = (_DateTimeStamp * size)() _lib_call("TOSDB_GetItemFrameStrings", self._name, topic.encode("ascii"), pstrs, size, data_str_max + 1, plabs if labels else _ppchar_(), label_str_max + 1, dts if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _ppchar_, _uint32_, _uint32_, _ppchar_, _uint32_, _PTR_(_DateTimeStamp))) dat = list(_zip_cstr_dt(pstrs, dts) if date_time else _map_cstr(pstrs)) if labels: nt = _gen_namedtuple( _str_clean(topic)[0], _str_clean(*_map_cstr(plabs))) return nt(*dat) else: return dat
def _n_from_marker_strings(self, item, topic, date_time, n, throw_if_data_lost, data_str_max): strs = _gen_str_buffers(data_str_max + 1, n) pstrs = _gen_str_buffers_ptrs(strs) dts = (_DateTimeStamp * n)() g = _long_() _lib_call("TOSDB_GetNStringsFromMarker", self._name, item.encode("ascii"), topic.encode("ascii"), pstrs, n, data_str_max + 1, dts if date_time else _PTR_(_DateTimeStamp)(), _pointer(g), arg_types=(_str_, _str_, _str_, _ppchar_, _uint32_, _uint32_, _PTR_(_DateTimeStamp), _PTR_(_long_))) g = g.value if g == 0: return None elif g < 0: if throw_if_data_lost: raise TOSDB_DataError("data lost behind the 'marker'") else: g *= -1 return list( _zip_cstr_dt(pstrs[:g], dts[:g] ) if date_time else _map_cstr(pstrs[:g]))
def _n_from_marker_numbers(self, tytup, item, topic, date_time, n, throw_if_data_lost): nums = (tytup[1] * n)() dts = (_DateTimeStamp * n)() g = _long_() _lib_call("TOSDB_GetN" + tytup[0] + "sFromMarker", self._name, item.encode("ascii"), topic.encode("ascii"), nums, n, dts if date_time else _PTR_(_DateTimeStamp)(), _pointer(g), arg_types=(_str_, _str_, _str_, _PTR_(tytup[1]), _uint32_, _PTR_(_DateTimeStamp), _PTR_(_long_))) g = g.value if g == 0: return None elif g < 0: if throw_if_data_lost: raise TOSDB_DataError("data lost behind the 'marker'") else: g *= -1 return list(zip(nums[:g], _map_dt(dts[:g])) if date_time else nums[:g])
def get(self, item, topic, date_time=False, indx=0, check_indx=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 indx < 0: indx += self._block_size if indx >= self._block_size: raise TOSDB_IndexError("invalid index value passed to get()") if check_indx and indx >= self.stream_occupancy(item, topic): raise TOSDB_DataError("data not available at this index yet " + "(disable check_indx to avoid this error)") dts = _DateTimeStamp() tbits = type_bits(topic) tytup = _type_switch(tbits) if tytup[0] == "String": ret_str = _BUF_(data_str_max + 1) _lib_call("TOSDB_GetString", self._name, item.encode("ascii"), topic.encode("ascii"), indx, ret_str, data_str_max + 1, _pointer(dts) if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _str_, _long_, _pchar_, _uint32_, _PTR_(_DateTimeStamp))) if date_time : return (ret_str.value.decode(), TOSDB_DateTime(dts)) else: return ret_str.value.decode() else: val = tytup[1]() _lib_call("TOSDB_Get"+tytup[0], self._name, item.encode("ascii"), topic.encode("ascii"), indx, _pointer(val), _pointer(dts) if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _str_, _long_, _PTR_(tytup[1]), _PTR_(_DateTimeStamp))) if date_time: return (val.value, TOSDB_DateTime(dts)) else: return val.value
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 = self._handle_raw_item(item) topic = self._handle_raw_topic(topic) if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") 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") if throw_if_data_lost: # check this first so we don't move marker if dirty 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: 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 tytup = _type_switch(type_bits(topic)) if tytup[0] == "String": return self._stream_snapshot_from_marker_strings( item, topic, date_time, beg, cur_sz + margin_of_safety, throw_if_data_lost, data_str_max) else: return self._stream_snapshot_from_marker_numbers( tytup, item, topic, date_time, beg, cur_sz + margin_of_safety, throw_if_data_lost)
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))
def _get_number(self, tytup, item, topic, date_time, indx): dt = _DateTimeStamp() n = tytup[1]() _lib_call("TOSDB_Get" + tytup[0], self._name, item.encode("ascii"), topic.encode("ascii"), indx, _pointer(n), _pointer(dt) if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _str_, _long_, _PTR_(tytup[1]), _PTR_(_DateTimeStamp))) return (n.value, TOSDB_DateTime(dt)) if date_time else n.value
def _get_item_or_topic_count(self, fname): c = _uint32_() _lib_call("TOSDB_Get" + fname + "Count", self._name, _pointer(c), arg_types=(_str_, _PTR_(_uint32_))) return c.value
def n_from_marker(self, item, topic, date_time=False, n=1, throw_if_data_lost=True, data_str_max=STR_DATA_SZ): item = self._handle_raw_item(item) topic = self._handle_raw_topic(topic) if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") if throw_if_data_lost: # check this first so we don't move marker if dirty 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: raise TOSDB_DataError("marker is already dirty") tytup = _type_switch(type_bits(topic)) if tytup[0] == "String": return self._n_from_marker_strings(item, topic, date_time, n, throw_if_data_lost, data_str_max) else: return self._n_from_marker_numbers(tytup, item, topic, date_time, n, throw_if_data_lost)
def _item_count(self): i_count = _uint32_() _lib_call("TOSDB_GetItemCount", self._name, _pointer(i_count), arg_types=(_str_, _PTR_(_uint32_))) return i_count.value
def _topic_count(self): t_count = _uint32_() _lib_call("TOSDB_GetTopicCount", self._name, _pointer(t_count), arg_types=(_str_, _PTR_(_uint32_))) return t_count.value
def get_block_size(self): b_size = _uint32_() _lib_call("TOSDB_GetBlockSize", self._name, _pointer(b_size), arg_types=(_str_, _PTR_(_uint32_))) return b_size.value
def _get_string(self, item, topic, date_time, indx, data_str_max): dt = _DateTimeStamp() s = _BUF_(data_str_max + 1) _lib_call("TOSDB_GetString", self._name, item.encode("ascii"), topic.encode("ascii"), indx, s, data_str_max + 1, _pointer(dt) if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _str_, _long_, _pchar_, _uint32_, _PTR_(_DateTimeStamp))) s = s.value.decode() return (s, TOSDB_DateTime(dt)) if date_time else s
def _stream_snapshot_numbers(self, tytup, item, topic, date_time, end, beg, size): nums = (tytup[1] * size)() dts = (_DateTimeStamp * size)() _lib_call("TOSDB_GetStreamSnapshot" + tytup[0] + "s", self._name, item.encode("ascii"), topic.encode("ascii"), nums, size, dts if date_time else _PTR_(_DateTimeStamp)(), end, beg, arg_types=(_str_, _str_, _str_, _PTR_(tytup[1]), _uint32_, _PTR_(_DateTimeStamp), _long_, _long_)) return list(zip(nums, _map_dt(dts)) if date_time else nums)
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))
def type_bits(topic): """ Returns the type bits for a particular 'topic' topic: string representing a TOS data field('LAST','ASK', etc) returns -> value that can be logical &'d with type bit contstants (ex. QUAD_BIT) """ b = _uint8_() _lib_call("TOSDB_GetTypeBits", topic.upper().encode("ascii"), _pointer(b), arg_types=(_str_,_PTR_(_uint8_)) ) return b.value
def _stream_snapshot_strings(self, item, topic, date_time, end, beg, size, data_str_max): strs = _gen_str_buffers(data_str_max + 1, size) pstrs = _gen_str_buffers_ptrs(strs) dts = (_DateTimeStamp * size)() _lib_call("TOSDB_GetStreamSnapshotStrings", self._name, item.encode("ascii"), topic.encode("ascii"), pstrs, size, data_str_max + 1, dts if date_time else _PTR_(_DateTimeStamp)(), end, beg, arg_types=(_str_, _str_, _str_, _ppchar_, _uint32_, _uint32_, _PTR_(_DateTimeStamp), _long_, _long_)) return list( _zip_cstr_dt(pstrs, dts) if date_time else _map_cstr(pstrs))
def stream_occupancy(self, item, topic): item = self._handle_raw_item(item) topic = self._handle_raw_topic(topic) occ = _uint32_() _lib_call("TOSDB_GetStreamOccupancy", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(occ), arg_types=(_str_, _str_, _str_, _PTR_(_uint32_))) return occ.value
def topic_frame(self, item, date_time=False, labels=True, data_str_max=STR_DATA_SZ, label_str_max=MAX_STR_SZ): item = self._handle_raw_item(item) if date_time and not self._date_time: raise TOSDB_DateTimeError("date_time not available for this block") size = self._get_item_or_topic_count("Topic") strs = _gen_str_buffers(data_str_max + 1, size) labs = _gen_str_buffers(label_str_max + 1, size) pstrs = _gen_str_buffers_ptrs(strs) plabs = _gen_str_buffers_ptrs(labs) dts = (_DateTimeStamp * size)() _lib_call("TOSDB_GetTopicFrameStrings", self._name, item.encode("ascii"), pstrs, size, data_str_max + 1, plabs if labels else _ppchar_(), label_str_max + 1, dts if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _ppchar_, _uint32_, _uint32_, _ppchar_, _uint32_, _PTR_(_DateTimeStamp))) dat = list(_zip_cstr_dt(pstrs, dts) if date_time else _map_cstr(pstrs)) if labels: nt = _gen_namedtuple( _str_clean(item)[0], _str_clean(*_map_cstr(plabs))) return nt(*dat) else: return dat
def type_bits(topic): """ Returns the type bits for a particular 'topic' topic: string representing a TOS data field('LAST','ASK', etc) returns -> value that can be logical &'d with type bit contstants (ex. QUAD_BIT) """ tybits = _uint8_() _lib_call("TOSDB_GetTypeBits", topic.upper().encode("ascii"), _pointer(tybits), arg_types=(_str_, _PTR_(_uint8_))) return tybits.value
def stream_occupancy(self, item, topic): item = item.upper() topic = topic.upper() self._valid_item(item) self._valid_topic(topic) occ = _uint32_() _lib_call("TOSDB_GetStreamOccupancy", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(occ), arg_types=(_str_, _str_, _str_, _PTR_(_uint32_))) return occ.value
def stream_occupancy(self, item, topic): item = item.upper() topic = topic.upper() self._valid_item(item) self._valid_topic(topic) occ = _ulong_() _lib_call("TOSDB_GetStreamOccupancy", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(occ), arg_list=[_str_, _str_, _str_, _PTR_(_ulong_)]) return occ.value
def stream_occupancy( self, item, topic ): item = item.upper() topic = topic.upper() self._valid_item(item) self._valid_topic(topic) occ = _ulong_() err = _lib_call( "TOSDB_GetStreamOccupancy", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(occ), arg_list = [ _str_, _str_, _str_, _PTR_(_ulong_) ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] returned" + "from library call", "TOSDB_GetStreamOccupancy" ) return occ.value
def stream_occupancy(self, item, topic): item = item.upper() topic = topic.upper() self._valid_item(item) self._valid_topic(topic) occ = _ulong_() err = _lib_call("TOSDB_GetStreamOccupancy", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(occ), arg_list=[_str_, _str_, _str_, _PTR_(_ulong_)]) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] returned" + "from library call", "TOSDB_GetStreamOccupancy") return occ.value
def type_bits(topic): """ Returns the type bits for a particular topic These type bits can be logical &'d with type bit contstants (ex. QUAD_BIT) to determine the underlying type of the topic. type_bits(topic) topic :: str :: topic string ('LAST','ASK', etc) returns -> int throws TOSDB_CLibError """ b = _uint8_() _lib_call("TOSDB_GetTypeBits", topic.upper().encode("ascii"), _pointer(b), arg_types=(_str_, _PTR_(_uint8_))) return b.value
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)
def get( self, item, topic, date_time = False, indx = 0, check_indx = True, data_str_max = STR_DATA_SZ ): """ Return a single data-point 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 indx: index of data-points [0 to block_size), [-block_size to -1] check_indx: throw if datum doesn't exist at that particular index data_str_max: the maximum size of string data returned """ 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 indx < 0: indx += self._block_size if indx >= self._block_size: raise TOSDB_IndexError( "invalid index value passed to get()" ) if check_indx and indx >= self.stream_occupancy( item, topic ): raise TOSDB_DataError( "data not available at this index yet " + "(disable check_indx to avoid this error)" ) dts = _DateTimeStamp() tbits = type_bits( topic ) tytup = _type_switch( tbits ) if tytup[0] == "String": ret_str = _BUF_( data_str_max + 1 ) err =_lib_call( "TOSDB_GetString", self._name, item.encode("ascii"), topic.encode("ascii"), indx, ret_str, data_str_max + 1, ( _pointer(dts) if date_time \ else _PTR_(_DateTimeStamp)() ), arg_list = [ _str_, _str_, _str_, _long_, _pchar_, _ulong_, _PTR_(_DateTimeStamp) ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " + "returned from library call", "TOSDB_GetString" ) if date_time : return (ret_str.value.decode(), TOSDB_DateTime( dts )) else: return ret_str.value.decode() else: val = tytup[1]() err = _lib_call( "TOSDB_Get"+tytup[0], self._name, item.encode("ascii"), topic.encode("ascii"), indx, _pointer(val), ( _pointer(dts) if date_time \ else _PTR_(_DateTimeStamp)() ), arg_list = [ _str_, _str_, _str_, _long_, _PTR_(tytup[1]), _PTR_(_DateTimeStamp) ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " + "returned from library call", "TOSDB_Get"+tytup[0] ) if date_time: return (val.value, TOSDB_DateTime( dts )) else: return val.value
POINTER as _PTR_, \ c_double as _double_, \ 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)$')
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 ]
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]
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]]
def get(self, item, topic, date_time=False, indx=0, check_indx=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 indx < 0: indx += self._block_size if indx >= self._block_size: raise TOSDB_IndexError("invalid index value passed to get()") if check_indx and indx >= self.stream_occupancy(item, topic): raise TOSDB_DataError("data not available at this index yet " + "(disable check_indx to avoid this error)") dts = _DateTimeStamp() tbits = type_bits(topic) tytup = _type_switch(tbits) if tytup[0] == "String": ret_str = _BUF_(data_str_max + 1) _lib_call("TOSDB_GetString", self._name, item.encode("ascii"), topic.encode("ascii"), indx, ret_str, data_str_max + 1, _pointer(dts) if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _str_, _long_, _pchar_, _uint32_, _PTR_(_DateTimeStamp))) if date_time: return (ret_str.value.decode(), TOSDB_DateTime(dts)) else: return ret_str.value.decode() else: val = tytup[1]() _lib_call("TOSDB_Get" + tytup[0], self._name, item.encode("ascii"), topic.encode("ascii"), indx, _pointer(val), _pointer(dts) if date_time else _PTR_(_DateTimeStamp)(), arg_types=(_str_, _str_, _str_, _long_, _PTR_(tytup[1]), _PTR_(_DateTimeStamp))) if date_time: return (val.value, TOSDB_DateTime(dts)) else: return val.value
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]]
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]
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 )
def get_block_size(self): b = _uint32_() _lib_call("TOSDB_GetBlockSize", self._name, _pointer(b), arg_types=(_str_,_PTR_(_uint32_))) return b.value
def _topic_precached_count(self): t = _uint32_() _lib_call("TOSDB_GetPreCachedTopicCount", self._name, _pointer(t), arg_types=(_str_,_PTR_(_uint32_))) return t.value
def _item_precached_count(self): i = _uint32_() _lib_call("TOSDB_GetPreCachedItemCount", self._name, _pointer(i), arg_types=(_str_,_PTR_(_uint32_))) return i.value
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]
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]
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): """ Return multiple data-points(a snapshot) from the data-stream, ending where the last call began It's likely the stream will grow between consecutive calls. This call guarantees to pick up where the last get(), stream_snapshot(), or stream_snapshot_from_marker() call ended (under a few assumptions, see below). Internally the stream maintains a 'marker' that tracks the position of the last value pulled; the act of retreiving data and moving the marker can be thought of as a single, 'atomic' operation. There are three states to be aware of: 1) a 'beg' value that is greater than the marker (even if beg = 0) 2) a marker that moves through the entire stream and hits the bound 3) passing a buffer that is too small for the whole range State (1) can be caused by passing in a beginning index that is past the current marker, or by passing in 0 when the marker has yet to move. 'None' will be returned. State (2) occurs when the marker doesn't get reset before it hits the bound (block_size); as the oldest data is popped of the back of the stream it is lost (the marker can't grow past the end of the stream). State (3) occurs when an inadequately small buffer is used. The call handles buffer sizing for you by calling down to get the marker index, adjusting by 'beg' and 'margin_of_safety'. The latter helps assure the marker doesn't outgrow the buffer by the time the low-level retrieval operation completes. The default value indicates that over 100 push operations would have to take place during this call(highly unlikely). In either case (state (2) or (3)) if throw_if_data_lost is True a TOSDB_DataError will be thrown, otherwise the available data will be returned as normal. 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 beg: index of most recent data-point ( beginning of the snapshot ) margin_of_safety: (True/False) error margin for async stream growth throw_if_data_loss: (True/False) how to handle error states (see above) data_str_max: the maximum length of string data returned if beg > internal marker value: returns -> None 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 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_() err = _lib_call("TOSDB_IsMarkerDirty", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(is_dirty), arg_list=[_str_, _str_, _str_, _PTR_(_uint_)]) if err: raise TOSDB_CLibError( "error value [ " + str(err) + " ] " + "returned from library call", "TOSDB_IsMarkerDirty") if is_dirty and throw_if_data_lost: raise TOSDB_DataError("marker is already dirty") mpos = _longlong_() err2 = _lib_call("TOSDB_GetMarkerPosition", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(mpos), arg_list=[_str_, _str_, _str_, _PTR_(_longlong_)]) if err2: raise TOSDB_CLibError( "error value [ " + str(err2) + " ] " + "returned from library call", "TOSDB_GetMarkerPosition") 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": # store char buffers strs = [_BUF_(data_str_max + 1) for _ in range(safe_sz)] # cast char buffers into (char*)[ ] strs_array = (_pchar_ * safe_sz)(*[cast(s, _pchar_) for s in strs]) err3 = _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_list=[ _str_, _str_, _str_, _ppchar_, _ulong_, _ulong_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_) ]) if err3: raise TOSDB_CLibError( "error value [ " + str(err3) + " ] returned from library call", "TOSDB_GetStreamSnapshotStrings" \ + "FromMarker") 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 [ _ for _ in \ zip( map( lambda x : cast(x, _str_).value.decode(), strs_array[:get_size] ), adj_dts ) ] else: return [ cast(ptr, _str_).value.decode() for ptr in strs_array[:get_size] ] else: num_array = (tytup[1] * safe_sz)() err3 = _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_list = [ _str_, _str_, _str_, _PTR_(tytup[1]), _ulong_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_) ] ) if err3: raise TOSDB_CLibError( "error value of [ " + str(err3) + " ] returned from library call", "TOSDB_GetStreamSnapshot" \ + tytup[0] + "sFromMarker" ) 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 [_ for _ in zip(num_array[:get_size], adj_dts)] else: return [_ for _ in num_array[:get_size]]
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 ]
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]
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 ): """ Return multiple data-points(a snapshot) from the data-stream, ending where the last call began It's likely the stream will grow between consecutive calls. This call guarantees to pick up where the last get(), stream_snapshot(), or stream_snapshot_from_marker() call ended (under a few assumptions, see below). Internally the stream maintains a 'marker' that tracks the position of the last value pulled; the act of retreiving data and moving the marker can be thought of as a single, 'atomic' operation. There are three states to be aware of: 1) a 'beg' value that is greater than the marker (even if beg = 0) 2) a marker that moves through the entire stream and hits the bound 3) passing a buffer that is too small for the whole range State (1) can be caused by passing in a beginning index that is past the current marker, or by passing in 0 when the marker has yet to move. 'None' will be returned. State (2) occurs when the marker doesn't get reset before it hits the bound (block_size); as the oldest data is popped of the back of the stream it is lost (the marker can't grow past the end of the stream). State (3) occurs when an inadequately small buffer is used. The call handles buffer sizing for you by calling down to get the marker index, adjusting by 'beg' and 'margin_of_safety'. The latter helps assure the marker doesn't outgrow the buffer by the time the low-level retrieval operation completes. The default value indicates that over 100 push operations would have to take place during this call(highly unlikely). In either case (state (2) or (3)) if throw_if_data_lost is True a TOSDB_DataError will be thrown, otherwise the available data will be returned as normal. 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 beg: index of most recent data-point ( beginning of the snapshot ) margin_of_safety: (True/False) error margin for async stream growth throw_if_data_loss: (True/False) how to handle error states (see above) data_str_max: the maximum length of string data returned if beg > internal marker value: returns -> None 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 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_() err = _lib_call( "TOSDB_IsMarkerDirty", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(is_dirty), arg_list = [ _str_, _str_, _str_, _PTR_(_uint_) ] ) if err: raise TOSDB_CLibError( "error value [ "+ str(err) + " ] " + "returned from library call", "TOSDB_IsMarkerDirty" ) if is_dirty and throw_if_data_lost: raise TOSDB_DataError("marker is already dirty") mpos = _longlong_() err2 = _lib_call( "TOSDB_GetMarkerPosition", self._name, item.encode("ascii"), topic.encode("ascii"), _pointer(mpos), arg_list = [ _str_, _str_, _str_, _PTR_(_longlong_) ]) if err2: raise TOSDB_CLibError( "error value [ "+ str(err2) + " ] " + "returned from library call", "TOSDB_GetMarkerPosition" ) 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": # store char buffers strs = [ _BUF_( data_str_max +1 ) for _ in range(safe_sz) ] # cast char buffers into (char*)[ ] strs_array = (_pchar_ * safe_sz)(*[ cast(s,_pchar_) for s in strs]) err3 = _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_list = [ _str_, _str_, _str_, _ppchar_, _ulong_, _ulong_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_) ] ) if err3: raise TOSDB_CLibError( "error value [ " + str(err3) + " ] returned from library call", "TOSDB_GetStreamSnapshotStrings" \ + "FromMarker") 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 [ _ for _ in \ zip( map( lambda x : cast(x, _str_).value.decode(), strs_array[:get_size] ), adj_dts ) ] else: return [ cast(ptr,_str_).value.decode() for ptr in strs_array[:get_size] ] else: num_array = (tytup[1] * safe_sz)() err3 = _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_list = [ _str_, _str_, _str_, _PTR_(tytup[1]), _ulong_, _PTR_(_DateTimeStamp), _long_, _PTR_(_long_) ] ) if err3: raise TOSDB_CLibError( "error value of [ " + str(err3) + " ] returned from library call", "TOSDB_GetStreamSnapshot" \ + tytup[0] + "sFromMarker" ) 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 [ _ for _ in zip( num_array[:get_size], adj_dts ) ] else: return [ _ for _ in num_array[:get_size] ]
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]
c_double as _double_, \ 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 + '-' \