Exemple #1
0
 def test_bool_settings(self):
     setting = Setting("test")
     setting.set("bool_test_true", True)
     setting.set("bool_test_false", False)
     assert not setting.get_bool(
         "bool_test_false"), "bool_test_false failed"
     assert setting.get_bool("bool_test_true"), "bool_test_true failed"
     assert setting.get_bool("bool_test_default_True",
                             True), "bool_test_default_True failed"
     assert not setting.get_bool("bool_test_default_False",
                                 False), "bool_test_default_False failed"
Exemple #2
0
 def test_float_settings(self):
     setting = Setting("test")
     setting.set("float_test1", 10.5)
     setting.set("float_test2", -0.5)
     assert setting.get_double("float_test1") == 10.5, "float_test1 failed"
     assert setting.get_double("float_test2") == -0.5, "float_test1 failed"
     assert setting.get_double("float_test_default",
                               -5.5), "float_test_default failed"
        def perform_request(self, url):
            try:
                proxy_setting = Setting('download-client').get_string(
                    'https-proxy')
                if proxy_setting:
                    opener = build_opener(
                        ProxyHandler({'https': proxy_setting}))
                    install_opener(opener)

                r = urlopen(pyNativeStr(url))
                total_size = int(r.headers.get('content-length', 0))
                bytes_sent = 0
                while True:
                    data = r.read(4096)
                    if not data:
                        break
                    raw_bytes = (ctypes.c_ubyte *
                                 len(data)).from_buffer_copy(data)
                    bytes_wrote = core.BNWriteDataForDownloadInstance(
                        self.handle, raw_bytes, len(raw_bytes))
                    if bytes_wrote != len(raw_bytes):
                        core.BNSetErrorForDownloadInstance(
                            self.handle, "Bytes written mismatch!")
                        return -1
                    bytes_sent = bytes_sent + bytes_wrote
                    continue_download = core.BNNotifyProgressForDownloadInstance(
                        self.handle, bytes_sent, total_size)
                    if continue_download is False:
                        core.BNSetErrorForDownloadInstance(
                            self.handle, "Download aborted!")
                        return -1

                if not bytes_sent:
                    core.BNSetErrorForDownloadInstance(self.handle,
                                                       "Received no data!")
                    return -1

            except URLError as e:
                core.BNSetErrorForDownloadInstance(self.handle,
                                                   e.__class__.__name__)
                return -1
            except:
                core.BNSetErrorForDownloadInstance(self.handle,
                                                   "Unknown Exception!")
                log.log_error(traceback.format_exc())
                return -1

            return 0
            def perform_request(self, url):
                try:
                    proxy_setting = Setting('download-client').get_string(
                        'https-proxy')
                    if proxy_setting:
                        proxies = {"https": proxy_setting}
                    else:
                        proxies = None

                    r = requests.get(pyNativeStr(url), proxies=proxies)
                    if not r.ok:
                        core.BNSetErrorForDownloadInstance(
                            self.handle, "Received error from server")
                        return -1
                    data = r.content
                    if len(data) == 0:
                        core.BNSetErrorForDownloadInstance(
                            self.handle, "No data received from server!")
                        return -1
                    raw_bytes = (ctypes.c_ubyte *
                                 len(data)).from_buffer_copy(data)
                    bytes_wrote = core.BNWriteDataForDownloadInstance(
                        self.handle, raw_bytes, len(raw_bytes))
                    if bytes_wrote != len(raw_bytes):
                        core.BNSetErrorForDownloadInstance(
                            self.handle, "Bytes written mismatch!")
                        return -1
                    continue_download = core.BNNotifyProgressForDownloadInstance(
                        self.handle, bytes_wrote, bytes_wrote)
                    if continue_download is False:
                        core.BNSetErrorForDownloadInstance(
                            self.handle, "Download aborted!")
                        return -1
                except requests.RequestException as e:
                    core.BNSetErrorForDownloadInstance(self.handle,
                                                       e.__class__.__name__)
                    return -1
                except:
                    core.BNSetErrorForDownloadInstance(self.handle,
                                                       "Unknown Exception!")
                    log.log_error(traceback.format_exc())
                    return -1

                return 0
Exemple #5
0
 def test_str_list_settings(self):
     setting = Setting("test")
     setting.set("str_list_test1", ["hi"])
     setting.set("str_list_test2", ["hello", "world"])
     setting.set("str_list_test3", [])
     assert setting.get_string_list("str_list_test1") == [
         "hi"
     ], "str_list_test1 failed"
     assert setting.get_string_list("str_list_test2") == [
         "hello", "world"
     ], "str_list_test2 failed"
     assert setting.get_string_list(
         "str_list_test3") == [], "str_list_test3 failed"
     assert setting.get_string_list(
         "str_list_test_default",
         ["hi", "there"]), "str_list_test_default failed"
Exemple #6
0
 def test_int_list_settings(self):
     setting = Setting("test")
     setting.set("int_list_test1", [0x100])
     setting.set("int_list_test2", [1, 2])
     setting.set("int_list_test3", [])
     assert setting.get_integer_list("int_list_test1") == [
         0x100
     ], "int_list_test1 failed"
     assert setting.get_integer_list("int_list_test2") == [
         1, 2
     ], "int_list_test2 failed"
     assert setting.get_integer_list(
         "int_list_test3") == [], "int_list_test3 failed"
     assert setting.get_integer_list("int_list_test_default",
                                     [2, 3]), "int_list_test_default failed"
Exemple #7
0
 def test_str_settings(self):
     setting = Setting("test")
     setting.set("str_test1", "hi")
     setting.set("str_test2", "")
     setting.set("str_test3", "A" * 1000)
     assert setting.get_string("str_test1") == "hi", "str_test1 failed"
     assert setting.get_string("str_test2") == "", "str_test2 failed"
     assert setting.get_string(
         "str_test3") == "A" * 1000, "str_test3 failed"
     assert setting.get_string("str_test_default",
                               "hi") == "hi", "str_test_default failed"
Exemple #8
0
 def test_int_settings(self):
     setting = Setting("test")
     setting.set("int_test1", 0x100)
     setting.set("int_test2", 0)
     setting.set("int_test3", -1)
     assert setting.get_integer("int_test1") == 0x100, "int_test1 failed"
     assert setting.get_integer("int_test2") == 0, "int_test2 failed"
     assert setting.get_integer("int_test3") == -1, "int_test3 failed"
     assert setting.get_integer("int_test_default_1",
                                1) == 1, "int_test_default_1 failed"
Exemple #9
0
 def tearDownClass(cls):
     setting = Setting("test")
     setting.remove_setting_group("test")