Beispiel #1
0
 def __init__(self, driver):
     self.driver = driver
     self.jstransfer=JsTransfer(self.driver)
     print('---init----')
     print(self.jstransfer.data_utils())
     self.driver.execute_script(self.jstransfer.data_utils())
     print(self.jstransfer.fun_utils())
     self.driver.execute_script(self.jstransfer.fun_utils())
Beispiel #2
0
 def __init__(self, driver, database: RjsDatabase):
     # 初始化容器
     self.database_tables = {}
     #
     # 加载 数据库,选择表
     self.driver = driver
     # 工具加载
     self.db_v_name = RjsRandom.random_letter_lower(preffix="RjsDB",
                                                    length_random=4)
     self.table_v_name = RjsRandom.random_letter_lower(
         preffix="RjsTableUtils", length_random=4)
     print("DB唯一码:" + self.db_v_name)
     print("TableUtils唯一码:" + self.table_v_name)
     self.jstransfer = JsTransfer(driver)
     self.driver.execute_script(self.jstransfer.data_utils())
     self.driver.execute_script(self.jstransfer.fun_utils())
     #初始化全局数据,异步等待使用
     self.set_database(database)
     if database.hasTables(): self.set_table(database.get_default_table())
     time.sleep(0.5)
Beispiel #3
0
class CookieStorage:
    def __init__(self, driver):
        self.driver = driver
        self.jstransfer = JsTransfer(self.driver)
        self.driver.execute_script(self.jstransfer.data_utils())
        self.driver.execute_script(self.jstransfer.fun_utils())

    def __len__(self):
        return len(self.get_all())

    def items(self):
        return self.get_all()

    def keys(self):
        res = []
        for c1 in self.get_all():
            res.append(c1['name'])
        return res

    def get(self, key):
        return self.driver.get_cookie(key)

    def get_all(self):
        return self.driver.get_cookies()

    def set_all(self, cookies: list):
        for cookie_item in cookies:
            self.set(cookie_item)

    def set_all_strict(self, cookies: list):
        for cookie_item in cookies:
            self.set_strict(cookie_item)

    def set(self, cookie: dict):
        print('-----添加cookie------')
        print(cookie)
        self.driver.add_cookie(cookie)

    def set_strict(self, cookie: Cookie):
        self.driver.add_cookie({
            'domain':
            cookie.domain if cookie.domain is not None else self.get_domain(),
            'expiry':
            cookie.expiry,
            'httpOnly':
            cookie.httpOnly,
            'name':
            str(cookie.name),
            'path':
            str(cookie.path),
            'sameSite':
            cookie.sameSite,
            'secure':
            cookie.secure,
            'value':
            str(cookie.value)
        })

    def has(self, key):
        return key in self.keys()

    def remove(self, key):
        self.driver.delete_cookie(key)

    def clear(self):
        self.driver.delete_all_cookies()

    def get_url_root(self):
        url_ps = str(self.driver.current_url).split('/')
        return url_ps[0] + '//' + url_ps[2]

    def get_host(self):
        return str(self.driver.current_url).lstrip('https://').lstrip(
            'http://').split('/')[0]

    def get_domain(self):
        host = self.get_host()
        host_strs = host.split('.')
        domain = host_strs.pop()
        return '.' + host_strs.pop() + '.' + domain

    def __getitem__(self, key):
        value = self.get(key)
        if value is None:
            raise KeyError(key)
        return value

    def __setitem__(self, key, value):
        self.set(key, value)

    def __contains__(self, key):
        return key in self.keys()

    def __iter__(self):
        return self.items().__iter__()

    def __repr__(self):
        return self.items().__str__()
Beispiel #4
0
 def __init__(self, driver):
     self.driver = driver
     self.jstransfer = JsTransfer(driver)
     self.driver.execute_script(self.jstransfer.data_utils())
     self.driver.execute_script(self.jstransfer.fun_utils())
Beispiel #5
0
class SessionStorage:
    def __init__(self, driver):
        self.driver = driver
        self.jstransfer = JsTransfer(driver)
        self.driver.execute_script(self.jstransfer.data_utils())
        self.driver.execute_script(self.jstransfer.fun_utils())

    def __len__(self):
        return self.driver.execute_script(
            "return window.sessionStorage.length;")

    def items(self):
        return self.driver.execute_script( \
            "var ls = window.sessionStorage, items = {}; " \
            "for (var i = 0, k; i < ls.length; ++i) " \
            "  items[k = ls.key(i)] = ls.getItem(k); " \
            "return items; ")

    def keys(self):
        return self.driver.execute_script( \
            "var ls = window.sessionStorage, keys = []; " \
            "for (var i = 0; i < ls.length; ++i) " \
            "  keys[i] = ls.key(i); " \
            "return keys; ")

    def get(self, key):
        js_command = 'return window.sessionStorage.getItem(' + self.jstransfer.pv2jv(
            key) + ');'
        print('get:' + key)
        print(js_command)
        return self.driver.execute_script(js_command)

    def get_all(self):
        dic = {}
        for key in self.keys():
            dic[key] = self.get(key)
        return dic

    def set_all(self, maps):
        for key, value in maps.items():
            self.set(key, value)

    def set(self, key, value):
        js_command = 'window.sessionStorage.setItem(' + self.pv2jv(
            key) + ', ' + self.pv2jv(value) + ');'
        print('set(key:' + key)
        print(js_command)
        self.driver.execute_script(js_command)

    def has(self, key):
        return key in self.keys()

    def remove(self, key):
        self.driver.execute_script(
            "window.sessionStorage.removeItem(arguments[0]);", key)

    def clear(self):
        self.driver.execute_script("window.sessionStorage.clear();")

    def pv2jv(self, pv):
        return self.jstransfer.pv2jv(pv)

    def __getitem__(self, key):
        value = self.get(key)
        if value is None:
            raise KeyError(key)
        return value

    def __setitem__(self, key, value):
        self.set(key, value)

    def __contains__(self, key):
        return key in self.keys()

    def __iter__(self):
        return self.items().__iter__()

    def __repr__(self):
        return self.items().__str__()
Beispiel #6
0
class DbStorage:
    ## 初始化 driver和 db name
    def __init__(self, driver, database: RjsDatabase):
        # 初始化容器
        self.database_tables = {}
        #
        # 加载 数据库,选择表
        self.driver = driver
        # 工具加载
        self.db_v_name = RjsRandom.random_letter_lower(preffix="RjsDB",
                                                       length_random=4)
        self.table_v_name = RjsRandom.random_letter_lower(
            preffix="RjsTableUtils", length_random=4)
        print("DB唯一码:" + self.db_v_name)
        print("TableUtils唯一码:" + self.table_v_name)
        self.jstransfer = JsTransfer(driver)
        self.driver.execute_script(self.jstransfer.data_utils())
        self.driver.execute_script(self.jstransfer.fun_utils())
        #初始化全局数据,异步等待使用
        self.set_database(database)
        if database.hasTables(): self.set_table(database.get_default_table())
        time.sleep(0.5)

    def back2hell(self, hell_key):
        while self.driver.execute_script('return window.RjsData["' + hell_key +
                                         '"]') is None:
            print('等待异步回调结果:' + hell_key)
            time.sleep(0.1)
        print('等待结束----获取到返回值----:' + hell_key)
        # 处理 undefined情况
        res = self.driver.execute_script('return window.RjsData["' + hell_key +
                                         '"]')
        if res == "rjsUndefined": return None
        return res

    def switch_table(self, table_name: str):
        print('切换表')
        if self.database.has_table(table_name):
            self.table = self.database.get_table(table_name)

    # 设置当前表
    def set_table(self, table: RjsTable):
        # 重设 table,创建table 及主键,基础索引.当前未处理其他索引.
        self.table = RjsTable(name=str(table.name),
                              primaryKey=table.primaryKey,
                              autoIncrement=table.autoIncrement,
                              version=str(table.version),
                              createUniquePrimary=table.createUniquePrimary)
        print('设置表:' + self.table.name)
        js_command = 'window.' + self.db_v_name + '.onupgradeneeded=function(e){var db=window.' + self.db_v_name + '.result;window.' + self.table_v_name + '=db;' + self.get_table_create_js_str(
        ) + '};window.' + self.db_v_name + '.onsuccess=function(event){window.' + self.table_v_name + '=event.target.result;var transaction=window.' + self.table_v_name + '.transaction(["' + self.table.name + '"],"readwrite");transaction.onsuccess=function(event){console.log("[Transaction] 好了!")};var studentsStore=transaction.objectStore("' + self.table.name + '");studentsStore.count().onsuccess=function(event){window.RjsData[res_token]=event.target.result;console.log("count:",event.target.result)}};window.' + self.db_v_name + '.onerror=function(event){console.log(event.target)};'
        self.table_command = js_command
        #print(js_command)
        self.back2hell(
            self.driver.execute_script('var res_token=Math.random()+"";' +
                                       self.database_command +
                                       self.table_command +
                                       'return res_token;'))

    def get_table_create_js_str(self):
        create_table_str = ''
        if self.database.hasTables():
            for table_1 in self.database.get_tables():
                create_table_str = create_table_str + 'var store=db.createObjectStore("' + str(
                    table_1.name) + '"' + (
                        '' if table_1.primaryKey is None else ',{' +
                        ('keyPath:"' + str(table_1.primaryKey) + '",') +
                        'autoIncrement: ' +
                        ('true' if table_1.autoIncrement else 'false') + '}'
                    ) + ');' + ('store.createIndex("primary_key_id_unqiue","' +
                                str(table_1.primaryKey) + '",{unique:true});'
                                if table_1.createUniquePrimary else ';')
        return create_table_str

    def reselect_table(self):
        self.table = None

    # py对象更新 db设置.重新选择表
    def set_database(self, database: RjsDatabase):
        self.database = database
        # self.reselect_table()
        #执行js代码,打开对应版本的数据库
        print('设置数据库:')
        js_command = 'window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB' + ";window." + self.db_v_name + "=indexedDB.open('" + str(
            self.database.name) + "', " + str(self.database.version) + ");"
        #print(js_command)
        self.database_command = js_command

    def __len__(self):
        return self.back2hell(
            self.driver.execute_script(
                'var res=Math.random()+"";var transaction=window.' +
                self.table_v_name + '.transaction(["' + self.table.name +
                '"],"readwrite");transaction.onsuccess=function(event){console.log("[Transaction] 好了!")};var studentsStore=transaction.objectStore("'
                + self.table.name +
                '");studentsStore.count().onsuccess=function(event){window.RjsData[res]=event.target.result;console.log("success")};return res'
            ))

    # 以map形式返回所有数据
    def items(self):
        dic = {}
        for key in self.keys():
            dic[key] = self.get(key)
        return dic

    def keys(self):
        return self.back2hell(
            self.driver.execute_script(
                'var res=Math.random()+"";var transaction=window.' +
                self.table_v_name + '.transaction(["' + self.table.name +
                '"],"readwrite");transaction.onsuccess=function(event){console.log("[Transaction] 好了!")};var studentsStore=transaction.objectStore("'
                + self.table.name +
                '");studentsStore.getAllKeys().onsuccess=function(event){window.RjsData[res]=event.target.result;console.log("success")};return res'
            ))

    def pv2jv(self, pv):
        if isinstance(pv, bool):
            return 'true' if pv else 'false'
        elif isinstance(pv, int):
            return str(pv)
        elif isinstance(pv, float):
            return str(pv)
        elif isinstance(pv, str):
            return '"' + (pv.replace('"', '\\"')) + '"'
        elif isinstance(pv, TextIOWrapper):
            file_str = ''
            type_f = pv.name.split('__rjs__').pop().replace('.', '/')
            if type_f is None or type_f == '': type_f = 'text/plain'
            for line_str in pv.readlines():
                line_str = line_str.replace('\n', '\\n')
                file_str += self.pv2jv(
                    line_str) if file_str == '' else ',' + self.pv2jv(line_str)
            return 'new Blob([' + file_str + '],{"type":' + self.pv2jv(
                type_f) + '})'
        elif isinstance(pv, BufferedReader):
            file_str = ''
            type_f = pv.name.split('__rjs__').pop().replace('.', '/')
            if type_f is None or type_f == '': type_f = 'text/plain'
            encoding = None
            for line_str in pv.readlines():
                if encoding == None:
                    encoding = chardet.detect(line_str)['encoding']
                if encoding == None: encoding = 'utf-8'
                line_str = str(line_str).lstrip('b').strip("'")
                file_str += self.pv2jv(
                    line_str) if file_str == '' else ',' + self.pv2jv(line_str)
            return 'new Blob([' + file_str + '],{"type":' + self.pv2jv(
                type_f) + '})'
        elif isinstance(pv, set):
            return str(pv)
        elif isinstance(pv, tuple):
            return str(tuple)
        elif type(pv).__name__ == 'dict':
            return self.dic2str(pv)
        elif isinstance(pv, list):
            res = ''
            for v1 in pv:
                res = res + (self.pv2jv(v1) if res == '' else ',' +
                             self.pv2jv(v1))
            return '[' + res + ']'

    def get(self, key):
        print('get(' + key + '):')
        js_command = 'var res=Math.random()+"";var transaction=window.' + self.table_v_name + '.transaction(["' + self.table.name + '"],"readwrite");transaction.onsuccess=function(event){console.log("[Transaction] 好了!")};var studentsStore=transaction.objectStore("' + self.table.name + '");studentsStore.get(' + self.pv2jv(
            key
        ) + ').onsuccess=function(event){window.rjs_any_beauty(event.target.result===undefined?"rjsUndefined":event.target.result,res);window.RjsData[res]=event.target.result;console.log("success")};return res'
        #print(js_command)
        return self.back2hell(self.driver.execute_script(js_command))

    def get_all(self):
        print('getAll():')
        dic = {}
        for key in self.keys():
            dic[key] = self.get(key)
        return dic

    # 字典转js字符串  {id:14,name:"小张2",age:"13"}
    def dic2str(self, dic):
        res = '{'
        for key, value in dic.items():
            res = (res if res == '{' else res +
                   ',') + self.pv2jv(key) + ':' + self.pv2jv(value)
        return res + '}'

    def add_all(self, lists):
        for value in lists:
            self.add(value)

    def add(self, tableEntity, key=None):
        entityStr = self.dic2str(tableEntity)
        print(
            'var transaction=window.' + self.table_v_name + '.transaction(["' +
            self.table.name +
            '"],"readwrite");transaction.onsuccess=function(event){console.log("[Transaction] 好了!")};var studentsStore=transaction.objectStore("'
            + self.table.name + '");studentsStore.add(' + entityStr +
            ('' if key is None else ',' + self.pv2jv(key)) +
            ').onsuccess=function(event){console.log("success")};')
        self.driver.execute_script(
            'var transaction=window.' + self.table_v_name + '.transaction(["' +
            self.table.name +
            '"],"readwrite");transaction.onsuccess=function(event){console.log("[Transaction] 好了!")};var studentsStore=transaction.objectStore("'
            + self.table.name + '");studentsStore.add(' + entityStr +
            ('' if key is None else ',' + self.pv2jv(key)) +
            ').onsuccess=function(event){console.log("success")};')

    def put(self, tableEntity):
        entityStr = self.dic2str(tableEntity)
        self.driver.execute_script(
            'var transaction=window.' + self.table_v_name + '.transaction(["' +
            self.table.name +
            '"],"readwrite");transaction.onsuccess=function(event){console.log("[Transaction] 好了!")};var studentsStore=transaction.objectStore("'
            + self.table.name + '");studentsStore.put(' + entityStr +
            ').onsuccess=function(event){console.log("success")};')

    # 当不设置主键索引时,使用该方法,或想将indexedDB当作kv方式使用
    def put_kv(self, key, value):
        js_command = 'var transaction=window.' + self.table_v_name + '.transaction(["' + self.table.name + '"],"readwrite");transaction.onsuccess=function(event){console.log("[Transaction] 好了!")};var studentsStore=transaction.objectStore("' + self.table.name + '");studentsStore.put(window.rjs_beauty_any(' + self.pv2jv(
            value) + '),' + self.pv2jv(
                key) + ').onsuccess=function(event){console.log("success")};'
        print('添加(key-value):' + key)
        # #print(js_command)
        self.driver.execute_script(js_command)

    def put_kv_all(self, kvs):
        for key, value in kvs.items():
            self.put_kv(key, value)

    def has(self, key):
        return self.get(key) is not None

    def remove(self, key):
        self.driver.execute_script(
            'var transaction=window.' + self.table_v_name + '.transaction(["' +
            self.table.name +
            '"],"readwrite");transaction.onsuccess=function(event){console.log("[Transaction] 好了!")};var studentsStore=transaction.objectStore("'
            + self.table.name + '");studentsStore.delete(' + self.pv2jv(key) +
            ').onsuccess=function(event){console.log("success")};')

    def delete(self, key):
        self.driver.execute_script(
            'var transaction=window.' + self.table_v_name + '.transaction(["' +
            self.table.name +
            '"],"readwrite");transaction.onsuccess=function(event){console.log("[Transaction] 好了!")};var studentsStore=transaction.objectStore("'
            + self.table.name + '");studentsStore.delete(' + self.pv2jv(key) +
            ').onsuccess=function(event){console.log("success")};')
        return True

    def clear(self):
        self.driver.execute_script(
            'var transaction=window.' + self.table_v_name + '.transaction(["' +
            self.table.name +
            '"],"readwrite");transaction.onsuccess=function(event){console.log("[Transaction] 好了!")};var studentsStore=transaction.objectStore("'
            + self.table.name +
            '");studentsStore.clear().onsuccess=function(event){console.log("success")};'
        )

    # 这个方法暂时不好使
    def drop_table(self):
        self.driver.execute_script('')

    def __getitem__(self, key):
        value = self.get(key)
        if value is None:
            raise KeyError(key)
        return value

    def __setitem__(self, key, value):
        self.set(key, value)

    def __contains__(self, key):
        return key in self.keys()

    def __iter__(self):
        return self.items().__iter__()

    def __repr__(self):
        return self.items().__str__()