当前位置: 首页>>代码示例>>Python>>正文


Python Dependency.debug方法代码示例

本文整理汇总了Python中common.utilities.inversion_of_control.Dependency.debug方法的典型用法代码示例。如果您正苦于以下问题:Python Dependency.debug方法的具体用法?Python Dependency.debug怎么用?Python Dependency.debug使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在common.utilities.inversion_of_control.Dependency的用法示例。


在下文中一共展示了Dependency.debug方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BAOnlineAuthContext

# 需要导入模块: from common.utilities.inversion_of_control import Dependency [as 别名]
# 或者: from common.utilities.inversion_of_control.Dependency import debug [as 别名]

#.........这里部分代码省略.........
                if "error" in response_rec:

                    # if try again, get a new token and try again
                    if try_again:
                        BAOnlineAuthContext.token = ""
                        self.__get_token()
                        return self.generate_report_with_post(request_format, url, False)

                    # otherwise raise an error
                    raise RuntimeError("BA Online Error - get_report_templates(): %s." % response_rec["error"])

            return response

    def download_file(self, url):
        """
        Adaptor method to make this fit into ArcGIS_report_helper.py
        """
        self.__log_request(url)
        return self.rest_provider.download_file(url)

    def __log_request(self, url):
        self.logger.info("BAOnline Request: %s" % url)



    def __get_token(self):
        """
        Authenticate with Business Analyst Online.
        docs: http://help.arcgis.com/en/businessanalyst/apis/rest/reference/GetToken.html
        Store the token in a local plain text file, and read it back from that file on subsequent calls.
        Note that we don't have to use a file for storing the token -- if we use a singleton we can keep it in memory.
        Or we could use something like Redis or MongoDB.
        """

        if not BAOnlineAuthContext.token:
            self.logger.debug("BA Online - Getting new token.")

            url = "https://baoapi.esri.com/rest/authentication"
            payload = {"request": "getToken",
                       "username": self.user_name,
                       "password": self.password,
                       "f": "JSON"}

            response = self.post(url, payload)
            self.num_auth_requests += 1

            if not response.ok:
                #inject potentially useful stuff in the response: the url and POST data payload
                response.reason += " | url: %s | POST data: %s" % (url, payload)
                response.raise_for_status()
            else:
                response_rec = response.json()

                if "error" in response_rec:
                    # check for bad password
                    # example: {u'error': u'Invalid user name / password pair.'}
                    if {"Invalid", "user", "password"} <= set(response_rec["error"].split(" ")):
                        # perhaps we could have different error handling if user/pwd is invalid?
                        raise AuthenticationError("BA Online - Authentication Failed! %s | user_name: %s | password: %s",
                                                  response_rec["error"], self.user_name, self.password)
                    else:
                        raise AuthenticationError("BA Online - Authentication Failed! %s." % response_rec["error"])
                else:
                    # example result:
                    # {u'results': {u'token': u'QCU89-M62iADv0TcQ5IP1Ck22sBuqcFxxiv5RSyWMdfPR2X6wFG3kIH8GnvUZQ3e'}}

                    token = response_rec["results"]["token"]
                    self.logger.debug("BA Online - Got new token: %s", token)

                    # save to instance and return
                    BAOnlineAuthContext.token = token


    def __get_datasets(self):
        """
        Returns the list of datasets that we are authorized for in Business Analyst Online.
        An example dataset is "USACensus2010".
        docs: http://help.arcgis.com/en/businessanalyst/apis/rest/reference/GetDatasets.html
        """
        url = "https://baoapi.esri.com/rest/report/GetDatasets"

        params = {"Token": BAOnlineAuthContext.token, "f": "JSON"}

        response = self.post(url, params)

        if not response.ok:
            #inject potentially useful stuff in the response: the url and params
            response.reason += " | url: %s | GET data: %s" % (url, params)
            response.raise_for_status()
        else:
            response_rec = response.json()
            if "error" in response_rec:
                # catch errors like: u'{"error":"(498) Token is invalid."}'
                if {"Token", "invalid."} <= set(response_rec["error"].split(" ")):
                    raise InvalidTokenError("BA Online Error - get_datasets(): %s." % response_rec["error"])
                else:
                    raise RuntimeError("BA Online Error - get_datasets(): %s." % response_rec["error"])
            else:
                datasets = response_rec["Result"]
                return datasets
开发者ID:erezrubinstein,项目名称:aa,代码行数:104,代码来源:ba_online_helper.py

示例2: TestSQLLoggingHandler

# 需要导入模块: from common.utilities.inversion_of_control import Dependency [as 别名]
# 或者: from common.utilities.inversion_of_control.Dependency import debug [as 别名]
class TestSQLLoggingHandler(unittest.TestCase):

    def setUp(self):
        # register mocks
        register_mock_dependencies("DEBUG")

        # get other dependencies
        self.data_repository = Dependency("DataRepository").value

        # create config and change values before registering sql handler
        self._config = Dependency("Config").value
        # set the flush to be .5 seconds so that we can test quickly
        self._config.sql_logging_insert_timer = .1
        self._config.app_version = "9.9.9.9"
        self._config.environment = "unit test"

        # create logger with only SQLLoggingHandler
        self.logger = Dependency("LogManager").value
        self.logger.clear_logging_handlers()
        self.sql_handler = self.logger.add_sql_handler()

    def tearDown(self):
        self.sql_handler.wait_for_threads_to_finish()
        dependencies.clear()

    def test_basic_message(self):
        self.logger.debug("test message")

        # you need to sleep for one second so that the logger flushes correctly
        sleep(.3)

        # make sure only one record is present
        self.assertEqual(len(self.data_repository.logging_records), 1)

        # make sure values are correct
        record = self.data_repository.logging_records[0]
        self.assertEqual(record.log_entry_type_id, 5)
        self.assertEqual(record.version, "9.9.9.9")
        self.assertEqual(record.environment, "unit test")
        # process id is 234234234_process
        self.assertRegexpMatches(record.process_id, "\d*_.*")
        self.assertEqual(record.message.strip(), "test message")
        # compare timestamp without seconds
        utc = str(datetime.utcnow())
        self.assertEqual(utc.split()[0], record.time.split()[0])
        self.assertEqual(record.function_name, "test_sql_logging_handler.py__test_basic_message")
        self.assertIsNone(record.elapsed_time)

    def test_basic_message_with_elapsed_time(self):
        self.logger.debug("test message", elapsed_time=20.1)

        # you need to sleep for one second so that the logger flushes correctly
        sleep(.3)

        # make sure only one record is present
        self.assertEqual(len(self.data_repository.logging_records), 1)

        # make sure values are correct
        record = self.data_repository.logging_records[0]
        self.assertEqual(record.log_entry_type_id, 5)
        self.assertEqual(record.version, "9.9.9.9")
        self.assertEqual(record.environment, "unit test")
        # process id is 234234234_process
        self.assertRegexpMatches(record.process_id, "\d*_.*")
        self.assertEqual(record.message.strip(), "test message")
        # compare timestamp without seconds
        utc = str(datetime.utcnow())
        self.assertEqual(utc.split()[0], record.time.split()[0])
        self.assertEqual(record.function_name, "test_sql_logging_handler.py__test_basic_message_with_elapsed_time")
        self.assertEqual(record.elapsed_time, 20.1)

    def test_all_message_levels(self):
        """
        This test combines critical, error, warning, info, and debug together.
        This is done to save time since we have to wait for the flushing operation
        """
        self.logger.debug("test debug", elapsed_time=1.1)
        self.logger.info("test info", elapsed_time=2.2)
        self.logger.warning("test warning", elapsed_time=3.3)
        self.logger.error("test error", elapsed_time=4.4)
        self.logger.critical("test critical", elapsed_time=5.5)

        # you need to sleep for one second so that the logger flushes correctly
        sleep(.3)

        # make sure we have 5 records
        self.assertEqual(len(self.data_repository.logging_records), 5)

        # make sure every record's type is correct
        self.assertEqual(self.data_repository.logging_records[0].log_entry_type_id, 5)
        self.assertEqual(self.data_repository.logging_records[0].message.strip(), "test debug")
        self.assertEqual(self.data_repository.logging_records[0].elapsed_time, 1.1)
        self.assertEqual(self.data_repository.logging_records[1].log_entry_type_id, 4)
        self.assertEqual(self.data_repository.logging_records[1].message.strip(), "test info")
        self.assertEqual(self.data_repository.logging_records[1].elapsed_time, 2.2)
        self.assertEqual(self.data_repository.logging_records[2].log_entry_type_id, 3)
        self.assertEqual(self.data_repository.logging_records[2].message.strip(), "test warning")
        self.assertEqual(self.data_repository.logging_records[2].elapsed_time, 3.3)
        self.assertEqual(self.data_repository.logging_records[3].log_entry_type_id, 2)
        self.assertEqual(self.data_repository.logging_records[3].message.strip(), "test error")
#.........这里部分代码省略.........
开发者ID:erezrubinstein,项目名称:aa,代码行数:103,代码来源:test_sql_logging_handler.py


注:本文中的common.utilities.inversion_of_control.Dependency.debug方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。