Пример #1
0
 def on_get(self):
     if self._get_str is None:
         # exceptions.DriplineMethodNotSupportedError
         raise ThrowReply('message_error_invalid_method',
                          f"endpoint '{self.name}' does not support get")
     result = self.service.send_to_device([self._get_str])
     logger.debug(f'result is: {result}')
     if self._extract_raw_regex is not None:
         first_result = result
         matches = re.search(self._extract_raw_regex, first_result)
         if matches is None:
             logger.error('matching returned none')
             # exceptions.DriplineValueError
             raise ThrowReply(
                 'resource_error',
                 'device returned unparsable result, [{}] has no match to input regex [{}]'
                 .format(first_result, self._extract_raw_regex))
         logger.debug(f"matches are: {matches.groupdict()}")
         result = matches.groupdict()['value_raw']
     return result
Пример #2
0
 def on_set(self, value):
     if self._set_str is None:
         # exceptions.DriplineMethodNotSupportedError
         raise ThrowReply('service_error',
                          f"endpoint '{self.name}' does not support set")
     if isinstance(value, str) and self._set_value_lowercase:
         value = value.lower()
     if self._set_value_map is None:
         mapped_value = value
     elif isinstance(self._set_value_map, dict):
         mapped_value = self._set_value_map[value]
     elif isinstance(self._set_value_map, str):
         mapped_value = self.evaluator(self._set_value_map.format(value))
     logger.debug(f'value is {value}; mapped value is: {mapped_value}')
     return self.service.send_to_device(
         [self._set_str.format(mapped_value)])
Пример #3
0
 def _insert_with_return(self, insert_kv_dict, return_col_names_list):
     try:
         ins = self.table.insert().values(**insert_kv_dict)
         if return_col_names_list:
             ins = ins.returning(*[
                 self.table.c[col_name]
                 for col_name in return_col_names_list
             ])
         insert_result = ins.execute()
         if return_col_names_list:
             return_values = insert_result.first()
         else:
             return_values = []
     except sqlalchemy.exc.IntegrityError as err:
         raise ThrowReply('resource_error',
                          f"database integreity error: '{repr(err)}'")
     except Exception as err:
         logger.critical(
             'received an unexpected SQL error while trying to insert:\n{}'.
             format(str(ins) % insert_kv_dict))
         logger.info('traceback is:\n{}'.format(traceback.format_exc()))
         return
     return dict(zip(return_col_names_list, return_values))
Пример #4
0
 def do_insert(self, *args, **kwargs):
     '''
     '''
     # make sure that all provided insert values are expected
     for col in list(kwargs.keys()):
         if not col in self._column_map.keys():
             logger.debug(f'got an unexpected insert column <{col}>')
             kwargs.pop(col)
     # make sure that all required columns are present
     for col in self._required_insert_names:
         if not col['payload_key'] in kwargs.keys():
             raise ThrowReply(
                 'service_error_invalid_value',
                 f'a value for <{col}> is required!\ngot: {kwargs}')
     # build the insert dict
     this_insert = self._default_insert_dict.copy()
     this_insert.update(
         {self._column_map[key]: value
          for key, value in kwargs.items()})
     return_vals = self._insert_with_return(
         this_insert,
         self._return_names,
     )
     return return_vals
Пример #5
0
 def on_get(self):
     # exceptions.DriplineMethodNotSupportedError
     raise ThrowReply('message_error_invalid_method',
                      f"endpoint '{self.name}' does not support get")
Пример #6
0
 def on_set(self, value):
     raise ThrowReply('message_error_invalid_method',
                      f"endpoint '{self.name}' does not support set")
 def throw_something(self):
     logger.warning("in throw_something")
     raise ThrowReply('resource_error', "in throw_something method, rasing resource_error")