def __get_json_field(self, get_field_from_json: Callable, field_name: str,
                      *metric_source_ids: str) -> float:
     """ Retrieve the JSON and use the callback to get the right field from the JSON structure. """
     result = 0
     for metric_source_id in metric_source_ids:
         url = self.__url.format(metric_source_id=metric_source_id)
         try:
             json_string = self.__url_opener.url_read(url)
         except UrlOpener.url_open_exceptions as reason:
             return -1
         try:
             json = eval_json(json_string)
         except (ValueError, TypeError, KeyError) as reason:
             logging.error("Couldn't evaluate JSON retrieved from %s: %s",
                           url, reason)
             logging.error("JSON received: %s", json_string)
             return -1
         try:
             result += float(get_field_from_json(json))
         except (KeyError, ValueError) as reason:
             logging.error(
                 "Couldn't get %s from JSON retrieved from %s: %s",
                 field_name, url, reason)
             logging.error("JSON received: %s", json)
             return -1
     return result
Пример #2
0
 def team_spirit(self, team_id: str) -> str:
     """ Return the team spirit of the team. Team spirit is either :-), :-|, or :-( """
     try:
         json = utils.eval_json(self.__url_read(self.__api_url()))
     except UrlOpener.url_open_exceptions:
         logging.warning("Could not open %s to read spirit of team %s", self.__api_url(), team_id)
         return ''
     try:
         return {'1': ':-(', '2': ':-(', '3': ':-|', '4': ':-)', '5': ':-)'}[json[-1]['smiley']]
     except (KeyError, IndexError) as reason:
         logging.error("Could not find smiley for %s in %s: %s", team_id, self.__api_url(), reason)
         return ''
Пример #3
0
 def datetime(self, *team_ids: str) -> DateTime:
     """ Return the date that the team spirit of the team was last measured. """
     try:
         json = utils.eval_json(self.__url_read(self.__api_url()))
     except UrlOpener.url_open_exceptions:
         logging.warning("Could not open %s to read date of least spirit measurement of team %s", self.__api_url(),
                         team_ids[0])
         return datetime.datetime.min
     try:
         return self.__parse_date(json[-1]['datum'])
     except (KeyError, IndexError) as reason:
         logging.error("Could not find smiley for %s in %s: %s", team_ids[0], self.__api_url(), reason)
         return datetime.datetime.min
Пример #4
0
 def datetime(self, *team_ids: str) -> DateTime:
     """ Return the date that the team spirit of the team was last measured. """
     try:
         json = utils.eval_json(self.__url_read(self.__api_url()))
     except UrlOpener.url_open_exceptions:
         logging.warning(
             "Could not open %s to read date of least spirit measurement of team %s",
             self.__api_url(), team_ids[0])
         return datetime.datetime.min
     try:
         return self.__parse_date(json[-1]['datum'])
     except (KeyError, IndexError) as reason:
         logging.error("Could not find smiley for %s in %s: %s",
                       team_ids[0], self.__api_url(), reason)
         return datetime.datetime.min
Пример #5
0
 def team_spirit(self, team_id: str) -> str:
     """ Return the team spirit of the team. Team spirit is either :-), :-|, or :-( """
     try:
         json = utils.eval_json(self.__url_read(self.__api_url()))
     except UrlOpener.url_open_exceptions:
         logging.warning("Could not open %s to read spirit of team %s",
                         self.__api_url(), team_id)
         return ''
     try:
         return {
             '1': ':-(',
             '2': ':-(',
             '3': ':-|',
             '4': ':-)',
             '5': ':-)'
         }[json[-1]['smiley']]
     except (KeyError, IndexError) as reason:
         logging.error("Could not find smiley for %s in %s: %s", team_id,
                       self.__api_url(), reason)
         return ''
 def __get_json_field(self, get_field_from_json: Callable, field_name: str, *metric_source_ids: str) -> float:
     """ Retrieve the JSON and use the callback to get the right field from the JSON structure. """
     result = 0
     for metric_source_id in metric_source_ids:
         url = self.__url.format(metric_source_id=metric_source_id)
         try:
             json_string = self.__url_opener.url_read(url)
         except UrlOpener.url_open_exceptions as reason:
             return -1
         try:
             json = eval_json(json_string)
         except (ValueError, TypeError, KeyError) as reason:
             logging.error("Couldn't evaluate JSON retrieved from %s: %s", url, reason)
             logging.error("JSON received: %s", json_string)
             return -1
         try:
             result += float(get_field_from_json(json))
         except (KeyError, ValueError) as reason:
             logging.error("Couldn't get %s from JSON retrieved from %s: %s", field_name, url, reason)
             logging.error("JSON received: %s", json)
             return -1
     return result
Пример #7
0
 def test_decode_bytes(self):
     """ Test that bytes are first decoded. """
     self.assertEqual(dict(bla=1), utils.eval_json(b'{"bla": 1}'))
Пример #8
0
 def test_eval_float(self):
     """ Test that a float number is evaluated. """
     self.assertEqual(dict(bla=1.5), utils.eval_json('{"bla": 1.5}'))
Пример #9
0
 def test_eval_decimal(self):
     """ Test that a decimal number is evaluated. """
     self.assertEqual(dict(bla=1), utils.eval_json('{"bla": 1}'))
Пример #10
0
 def test_eval_null(self):
     """ Test that 'null' is evaluated. """
     self.assertEqual(dict(bla=None), utils.eval_json('{"bla": null}'))
Пример #11
0
 def test_eval_false(self):
     """ Test that 'false' is evaluated. """
     self.assertEqual(dict(bla=False), utils.eval_json('{"bla" : false}'))
Пример #12
0
 def test_eval_true(self):
     """ Test that 'true' is evaluated. """
     self.assertEqual(dict(bla=True), utils.eval_json('{"bla": true}'))
Пример #13
0
 def test_empty_json(self):
     """ Test that an empty JSON dict results in a Python dict. """
     self.assertEqual(dict(), utils.eval_json('{}'))