Exemplo n.º 1
0
def start():
	global sock
	print "PyPol: initing game..."
	sock = SocketWrapper(SERVER_IP, SERVER_PORT, SELF_IP, SELF_PORT, inst_callback)
	lua.globals()['_URD_ENVTYPE_'] = 'LUATICPY'
	lua.globals().dofile('test.lua')
	lua.globals().init(SELF_PORT)
	sock.run()
Exemplo n.º 2
0
 def _lua_to_python(lval):
     """
     Convert Lua object(s) into Python object(s), as at times Lua object(s)
     are not compatible with Python functions
     """
     import lua
     lua_globals = lua.globals()
     if lval is None:
         # Lua None --> Python None
         return None
     if lua_globals.type(lval) == "table":
         # Lua table --> Python list
         pval = []
         for i in lval:
             pval.append(Script._lua_to_python(lval[i]))
         return pval
     elif isinstance(lval, long):
         # Lua number --> Python long
         return long(lval)
     elif isinstance(lval, float):
         # Lua number --> Python float
         return float(lval)
     elif lua_globals.type(lval) == "userdata":
         # Lua userdata --> Python string
         return str(lval)
     elif lua_globals.type(lval) == "string":
         # Lua string --> Python string
         return lval
     elif lua_globals.type(lval) == "boolean":
         # Lua boolean --> Python bool
         return bool(lval)
     raise RuntimeError("Invalid Lua type: " + str(lua_globals.type(lval)))
Exemplo n.º 3
0
 def _lua_to_python(lval):
     """
     Convert Lua object(s) into Python object(s), as at times Lua object(s)
     are not compatible with Python functions
     """
     import lua
     lua_globals = lua.globals()
     if lval is None:
         # Lua None --> Python None
         return None
     if lua_globals.type(lval) == "table":
         # Lua table --> Python list
         pval = []
         for i in lval:
             pval.append(Script._lua_to_python(lval[i]))
         return pval
     elif isinstance(lval, long):
         # Lua number --> Python long
         return long(lval)
     elif isinstance(lval, float):
         # Lua number --> Python float
         return float(lval)
     elif lua_globals.type(lval) == "userdata":
         # Lua userdata --> Python string
         return str(lval)
     elif lua_globals.type(lval) == "string":
         # Lua string --> Python string
         return lval
     elif lua_globals.type(lval) == "boolean":
         # Lua boolean --> Python bool
         return bool(lval)
     raise RuntimeError("Invalid Lua type: " + str(lua_globals.type(lval)))
Exemplo n.º 4
0
    def _validate_update(self, inputs, tx, txhexdigest):
        self._context()
        js_output = {}
        js_input = {}

        def js_setoutput(idx, s):
            k = '%s:%d' % (txhexdigest, idx)
            js_output[SHA256.new(k).hexdigest()] = s

        def js_setinput(k, s):
            js_input[k] = s

        lua.globals().setoutput = js_setoutput
        lua.globals().setinput = js_setinput
        f = lua.eval(tx['update'])
        f(inputs, tx)
        return js_input, js_output
Exemplo n.º 5
0
 def __call__(self, text):
     CurrentDir = os.getcwd()
     os.chdir('quirks')
     try:
         return self.module.commands[self.name](lua.globals().tostring(text))
     except:
         return None
     finally:
         os.chdir(CurrentDir)
Exemplo n.º 6
0
 def __call__(self, text):
     CurrentDir = os.getcwd()
     os.chdir('quirks')
     try:
         return self.module.commands[self.name](lua.globals().tostring(text))
     except:
         return None
     finally:
         os.chdir(CurrentDir)
Exemplo n.º 7
0
    def _python_to_lua(pval, call_args=None):
        """
        Convert Python object(s) into Lua object(s), as at times Python object(s)
        are not compatible with Lua functions
        """
        import lua
        if pval is None:
            # Python None --> Lua None
            return lua.eval("")
        if isinstance(pval, (list, tuple, set)):
            # Python list --> Lua table
            # e.g.: in lrange
            #     in Python returns: [v1, v2, v3]
            #     in Lua returns: {v1, v2, v3}
            lua_list = lua.eval("{}")
            lua_table = lua.eval("table")
            for item in pval:
                lua_table.insert(lua_list, Script._python_to_lua(item))
            return lua_list
        elif isinstance(pval, dict):
            # Python dict --> Lua dict
            # e.g.: in hgetall
            #     in Python returns: {k1:v1, k2:v2, k3:v3}
            #     in Lua returns: {k1, v1, k2, v2, k3, v3}
            # else in Python: {k1: v1, k2:v2}
            # in Lua: {[k1]: v1, [k2]: v2}
            command = call_args[0] if call_args else None
            if command == 'HGETALL':
                lua_dict = lua.eval("{}")
                lua_table = lua.eval("table")
                for k, v in pval.iteritems():
                    lua_table.insert(lua_dict, Script._python_to_lua(k))
                    lua_table.insert(lua_dict, Script._python_to_lua(v))
            else:
                comps = ['["%s"] = "%s"' % (Script._python_to_lua(k), Script._python_to_lua(v)) for k, v in pval.iteritems()]
                lua_dict = lua.eval('{%s}' % ','.join(comps))
            return lua_dict
        elif isinstance(pval, (str, unicode)):
            # Python string --> Lua userdata
            try:
                return str(pval)
            except UnicodeEncodeError:
                return unicode(pval)
        elif isinstance(pval, bool):
            command = call_args[0] if call_args else None
            # Python bool--> Lua boolean
            if command == "EXISTS":
                return lua.eval({True: '1', False: '0'}[pval])
            return lua.eval(str(pval).lower())
        elif isinstance(pval, (int, long, float)):
            # Python int --> Lua number
            lua_globals = lua.globals()
            return lua_globals.tonumber(str(pval))

        raise RuntimeError("Invalid Python type: " + str(type(pval)))
Exemplo n.º 8
0
    def testCreateNodes(self):

        lg = lua.globals()

        luaClasses = [
            c for c in fromLua(lg.getclassesbypattern('.+'))
            if lg.classisclassof(c, 'Node')
        ]
        with ModificationContext() as mod:
            for lc in luaClasses:
                #print lc
                mod.createNode('foo', lc)
Exemplo n.º 9
0
 def loadModule(self, name, filename):
     if lua is None:
         return None
     lua.globals().package.loaded[name] = None
     CurrentDir = os.getcwd()
     os.chdir('quirks')
     try:
         return lua.require(name)
     except Error as e:
         print(e)
         return None
     finally:
         os.chdir(CurrentDir)
Exemplo n.º 10
0
    def _import_lua():
        """
        Import lua and dependencies.

        :raises: RuntimeError if LUA is not available
        """
        try:
            import lua
        except ImportError:
            raise RuntimeError("LUA not installed")

        lua_globals = lua.globals()
        Script._import_lua_dependencies(lua, lua_globals)
        return lua, lua_globals
Exemplo n.º 11
0
    def loadModule(self, name, filename):
        if lua is None:
            return None

        lua.globals().package.loaded[name] = None

        CurrentDir = os.getcwd()
        os.chdir('quirks')
        try:
            return lua.require(name)
        except Error as e:
            logging.error(e)
            return None
        finally:
            os.chdir(CurrentDir)
Exemplo n.º 12
0
    def _execute_lua(self, keys, args, client):
        """
        Sets KEYS and ARGV alongwith redis.call() function in lua globals
        and executes the lua redis script
        """
        try:
            import lua
        except ImportError:
            raise RuntimeError("LUA not installed")

        lua_globals = lua.globals()
        lua_globals.KEYS = self._create_lua_array(keys)
        lua_globals.ARGV = self._create_lua_array(args)
        lua_globals.redis = {"call": client.call}
        return lua.execute(self.script)
Exemplo n.º 13
0
    def _import_lua(load_dependencies=True):
        """
        Import lua and dependencies.

        :param load_dependencies: should Lua library dependencies be loaded?
        :raises: RuntimeError if Lua is not available
        """
        try:
            import lua
        except ImportError:
            raise RuntimeError("Lua not installed")

        lua_globals = lua.globals()
        if load_dependencies:
            Script._import_lua_dependencies(lua, lua_globals)
        return lua, lua_globals
Exemplo n.º 14
0
    def _import_lua(load_dependencies=True):
        """
        Import lua and dependencies.

        :param load_dependencies: should Lua library dependencies be loaded?
        :raises: RuntimeError if Lua is not available
        """
        try:
            import lua
        except ImportError:
            raise RuntimeError("Lua not installed")

        lua_globals = lua.globals()
        if load_dependencies:
            Script._import_lua_dependencies(lua, lua_globals)
        return lua, lua_globals
Exemplo n.º 15
0
    def _context(self):
        def js_assert(condition, assertion=None):
            assert condition, assertion

        def js_clone(obj):
            return json.loads(json.dumps(obj))

        def js_hexdigest(s):
            return SHA256.new(s).hexdigest()

        def js_validatesig(key, digest, sig):
            RSA.importKey(key).validate(digest, sig)

        lua.globals().dict = dict
        lua.globals().len = len
        lua.globals().str = str
        lua.globals().iter = iter
        lua.globals().has_key = dict_has_key
        lua.globals()['assert'] = js_assert
        lua.globals().clone = js_clone
        lua.globals().hexdigest = js_hexdigest
        lua.globals().validatesig = js_validatesig
Exemplo n.º 16
0
    def test_lua_type_sanity(self):
        import lua

        test_filename = os.path.join(os.path.dirname(__file__), 'testing',
                                     'type_test.lua')

        lua_globals = lua.globals()
        lua_globals.text1 = to_lua_string('hi')
        lua_globals.text2 = to_lua_string('hé')
        lua_globals.text3 = to_lua_string('狗')
        lua_globals.num1 = to_lua_type(42)
        lua_globals.bool1 = to_lua_type(True)
        lua_globals.bool2 = to_lua_type(False)

        with open(test_filename, 'rb') as in_file:
            lua.execute(in_file.read())

        self.assertEquals('猫', lua_globals.text4)
        self.assertEquals(42, lua_globals.num2)
Exemplo n.º 17
0
    def test_lua_type_sanity(self):
        import lua

        test_filename = os.path.join(os.path.dirname(__file__),
            'testing', 'type_test.lua')

        lua_globals = lua.globals()
        lua_globals.text1 = to_lua_string('hi')
        lua_globals.text2 = to_lua_string('hé')
        lua_globals.text3 = to_lua_string('狗')
        lua_globals.num1 = to_lua_type(42)
        lua_globals.bool1 = to_lua_type(True)
        lua_globals.bool2 = to_lua_type(False)

        with open(test_filename, 'rb') as in_file:
            lua.execute(in_file.read())

        self.assertEquals('猫', lua_globals.text4)
        self.assertEquals(42, lua_globals.num2)
Exemplo n.º 18
0
    def _python_to_lua(pval):
        """
        Convert Python object(s) into Lua object(s), as at times Python object(s)
        are not compatible with Lua functions
        """
        import lua
        if pval is None:
            # Python None --> Lua None
            return lua.eval("")
        if isinstance(pval, (list, tuple, set)):
            # Python list --> Lua table
            # e.g.: in lrange
            #     in Python returns: [v1, v2, v3]
            #     in Lua returns: {v1, v2, v3}
            lua_list = lua.eval("{}")
            lua_table = lua.eval("table")
            for item in pval:
                lua_table.insert(lua_list, Script._python_to_lua(item))
            return lua_list
        elif isinstance(pval, dict):
            # Python dict --> Lua dict
            # e.g.: in hgetall
            #     in Python returns: {k1:v1, k2:v2, k3:v3}
            #     in Lua returns: {k1, v1, k2, v2, k3, v3}
            lua_dict = lua.eval("{}")
            lua_table = lua.eval("table")
            for k, v in pval.iteritems():
                lua_table.insert(lua_dict, Script._python_to_lua(k))
                lua_table.insert(lua_dict, Script._python_to_lua(v))
            return lua_dict
        elif isinstance(pval, str):
            # Python string --> Lua userdata
            return pval
        elif isinstance(pval, bool):
            # Python bool--> Lua boolean
            return lua.eval(str(pval).lower())
        elif isinstance(pval, (int, long, float)):
            # Python int --> Lua number
            lua_globals = lua.globals()
            return lua_globals.tonumber(str(pval))

        raise RuntimeError("Invalid Python type: " + str(type(pval)))
Exemplo n.º 19
0
def _python_to_lua(pval):
    """
    Patch MockRedis+Lua for Python 3 compatibility
    """
    # noinspection PyUnresolvedReferences
    import lua
    if pval is None:
        # Python None --> Lua None
        return lua.eval('')
    if isinstance(pval, (list, tuple, set)):
        # Python list --> Lua table
        # e.g.: in lrange
        #     in Python returns: [v1, v2, v3]
        #     in Lua returns: {v1, v2, v3}
        lua_list = lua.eval('{}')
        lua_table = lua.eval('table')
        for item in pval:
            lua_table.insert(lua_list, Script._python_to_lua(item))
        return lua_list
    elif isinstance(pval, dict):
        # Python dict --> Lua dict
        # e.g.: in hgetall
        #     in Python returns: {k1:v1, k2:v2, k3:v3}
        #     in Lua returns: {k1, v1, k2, v2, k3, v3}
        lua_dict = lua.eval('{}')
        lua_table = lua.eval('table')
        for k, v in six.iteritems(pval):
            lua_table.insert(lua_dict, Script._python_to_lua(k))
            lua_table.insert(lua_dict, Script._python_to_lua(v))
        return lua_dict
    elif isinstance(pval, tuple(set(six.string_types + (six.binary_type, )))):  # type: ignore
        # Python string --> Lua userdata
        return pval
    elif isinstance(pval, bool):
        # Python bool--> Lua boolean
        return lua.eval(str(pval).lower())
    elif isinstance(pval, six.integer_types + (float, )):  # type: ignore
        # Python int --> Lua number
        lua_globals = lua.globals()
        return lua_globals.tonumber(str(pval))

    raise RuntimeError('Invalid Python type: ' + str(type(pval)))
Exemplo n.º 20
0
    def _python_to_lua(pval):
        """
        Convert Python object(s) into Lua object(s), as at times Python object(s)
        are not compatible with Lua functions
        """
        import lua
        if pval is None:
            # Python None --> Lua None
            return lua.eval("")
        if isinstance(pval, (list, tuple, set)):
            # Python list --> Lua table
            # e.g.: in lrange
            #     in Python returns: [v1, v2, v3]
            #     in Lua returns: {v1, v2, v3}
            lua_list = lua.eval("{}")
            lua_table = lua.eval("table")
            for item in pval:
                lua_table.insert(lua_list, Script._python_to_lua(item))
            return lua_list
        elif isinstance(pval, dict):
            # Python dict --> Lua dict
            # e.g.: in hgetall
            #     in Python returns: {k1:v1, k2:v2, k3:v3}
            #     in Lua returns: {k1, v1, k2, v2, k3, v3}
            lua_dict = lua.eval("{}")
            lua_table = lua.eval("table")
            for k, v in pval.iteritems():
                lua_table.insert(lua_dict, Script._python_to_lua(k))
                lua_table.insert(lua_dict, Script._python_to_lua(v))
            return lua_dict
        elif isinstance(pval, str):
            # Python string --> Lua userdata
            return pval
        elif isinstance(pval, bool):
            # Python bool--> Lua boolean
            return lua.eval(str(pval).lower())
        elif isinstance(pval, (int, long, float)):
            # Python int --> Lua number
            lua_globals = lua.globals()
            return lua_globals.tonumber(str(pval))

        raise RuntimeError("Invalid Python type: " + str(type(pval)))
Exemplo n.º 21
0
    def _execute_lua(self, keys, args, client):
        """
        Sets KEYS and ARGV alongwith redis.call() function in lua globals
        and executes the lua redis script
        """
        try:
            import lua
        except ImportError:
            raise RuntimeError("LUA not installed")

        lua_globals = lua.globals()
        self._import_lua_dependencies(lua, lua_globals)
        lua_globals.KEYS = self._python_to_lua(keys)
        lua_globals.ARGV = self._python_to_lua(args)

        def _call(*call_args):
            response = client.call(*call_args)
            return self._python_to_lua(response)

        lua_globals.redis = {"call": _call}
        return self._lua_to_python(lua.execute(self.script))
Exemplo n.º 22
0
    def _execute_lua(self, keys, args, client):
        """
        Sets KEYS and ARGV alongwith redis.call() function in lua globals
        and executes the lua redis script
        """
        try:
            import lua
        except ImportError:
            raise RuntimeError("LUA not installed")

        lua_globals = lua.globals()
        self._import_lua_dependencies(lua, lua_globals)
        lua_globals.KEYS = self._python_to_lua(keys)
        lua_globals.ARGV = self._python_to_lua(args)

        def _call(*call_args):
            response = client.call(*call_args)
            return self._python_to_lua(response)

        lua_globals.redis = {"call": _call}
        return self._lua_to_python(lua.execute(self.script))
Exemplo n.º 23
0
def _lua_to_python(lval, return_status=False):
    """
    Patch MockRedis+Lua for Python 3 compatibility
    """
    # noinspection PyUnresolvedReferences
    import lua
    lua_globals = lua.globals()
    if lval is None:
        # Lua None --> Python None
        return None
    if lua_globals.type(lval) == 'table':
        # Lua table --> Python list
        pval = []
        for i in lval:
            if return_status:
                if i == 'ok':
                    return lval[i]
                if i == 'err':
                    raise ResponseError(lval[i])
            pval.append(Script._lua_to_python(lval[i]))
        return pval
    elif isinstance(lval, six.integer_types):
        # Lua number --> Python long
        return six.integer_types[-1](lval)
    elif isinstance(lval, float):
        # Lua number --> Python float
        return float(lval)
    elif lua_globals.type(lval) == 'userdata':
        # Lua userdata --> Python string
        return str(lval)
    elif lua_globals.type(lval) == 'string':
        # Lua string --> Python string
        return lval
    elif lua_globals.type(lval) == 'boolean':
        # Lua boolean --> Python bool
        return bool(lval)
    raise RuntimeError('Invalid Lua type: ' + str(lua_globals.type(lval)))
Exemplo n.º 24
0
def install(lua_script_path):
    class CallbacksAdapter(object):
        AVAILABLE_VERSIONS = to_lua_number(
            wpull_hook.callbacks.AVAILABLE_VERSIONS)

        @property
        def version(self):
            return wpull_hook.version

        @version.setter
        def version(self, num):
            wpull_hook.version = num

        engine_run = NotImplemented
        resolve_dns = NotImplemented
        accept_url = NotImplemented
        queued_url = NotImplemented
        dequeued_url = NotImplemented
        handle_pre_response = NotImplemented
        handle_response = NotImplemented
        handle_error = NotImplemented
        get_urls = NotImplemented
        wait_time = NotImplemented
        finishing_statistics = NotImplemented
        exit_status = NotImplemented

    callbacks = CallbacksAdapter()

    class HookEnvironmentAdapter(object):
        factory = wpull_hook.factory
        actions = wpull_hook.actions

        @staticmethod
        def engine_run():
            if callbacks.engine_run is not NotImplemented:
                callbacks.engine_run()

        @staticmethod
        def resolve_dns(host):
            if callbacks.resolve_dns is not NotImplemented:
                return callbacks.resolve_dns(to_lua_type(host))

        @staticmethod
        def accept_url(url_info, record_info, verdict, reasons):
            if callbacks.accept_url is not NotImplemented:
                return callbacks.accept_url(to_lua_type(url_info),
                                            to_lua_type(record_info), verdict,
                                            to_lua_type(reasons))

        @staticmethod
        def queued_url(url_info):
            if callbacks.queued_url is not NotImplemented:
                callbacks.queued_url(to_lua_type(url_info))

        @staticmethod
        def dequeued_url(url_info, record_info):
            if callbacks.dequeued_url is not NotImplemented:
                callbacks.dequeued_url(to_lua_type(url_info),
                                       to_lua_type(record_info))

        @staticmethod
        def handle_pre_response(url_info, record_info, http_info):
            if callbacks.handle_pre_response is not NotImplemented:
                return callbacks.handle_pre_response(to_lua_type(url_info),
                                                     to_lua_type(record_info),
                                                     to_lua_type(http_info))
            else:
                return 'normal'

        @staticmethod
        def handle_response(url_info, record_info, http_info):
            if callbacks.handle_response is not NotImplemented:
                return callbacks.handle_response(to_lua_type(url_info),
                                                 to_lua_type(record_info),
                                                 to_lua_type(http_info))
            else:
                return 'normal'

        @staticmethod
        def handle_error(url_info, record_info, error_info):
            if callbacks.handle_error is not NotImplemented:
                return callbacks.handle_error(to_lua_type(url_info),
                                              to_lua_type(record_info),
                                              to_lua_type(error_info))
            else:
                return 'normal'

        @staticmethod
        def get_urls(filename, url_info, document_info):
            if callbacks.get_urls is not NotImplemented:
                result = (callbacks.get_urls(to_lua_type(filename),
                                             to_lua_type(url_info),
                                             to_lua_type(document_info)))

                if result:
                    lua_items = from_lua_table_to_list(result)

                    items = []

                    for lua_item in lua_items:
                        item = {
                            'url':
                            get_from_lua_table_as_dict(lua_item, 'url'),
                            'link_type':
                            get_from_lua_table_as_dict(lua_item, 'link_type'),
                            'inline':
                            get_from_lua_table_as_dict(lua_item, 'inline'),
                            'post_data':
                            get_from_lua_table_as_dict(lua_item, 'post_data'),
                            'replace':
                            get_from_lua_table_as_dict(lua_item, 'replace'),
                        }
                        items.append(item)

                    return items

        @staticmethod
        def wait_time(seconds):
            if callbacks.wait_time is not NotImplemented:
                return callbacks.wait_time(to_lua_type(seconds))
            else:
                return seconds

        @staticmethod
        def finishing_statistics(start_time, end_time, num_urls,
                                 bytes_downloaded):
            if callbacks.finishing_statistics is not NotImplemented:
                callbacks.finishing_statistics(to_lua_type(start_time),
                                               to_lua_type(end_time),
                                               to_lua_type(num_urls),
                                               to_lua_type(bytes_downloaded))

        @staticmethod
        def exit_status(exit_code):
            if callbacks.exit_status is not NotImplemented:
                return callbacks.exit_status(to_lua_type(exit_code))
            else:
                return exit_code

    wpull_hook.callbacks.engine_run = HookEnvironmentAdapter.engine_run
    wpull_hook.callbacks.resolve_dns = HookEnvironmentAdapter.resolve_dns
    wpull_hook.callbacks.accept_url = HookEnvironmentAdapter.accept_url
    wpull_hook.callbacks.queued_url = HookEnvironmentAdapter.queued_url
    wpull_hook.callbacks.dequeued_url = HookEnvironmentAdapter.dequeued_url
    wpull_hook.callbacks.handle_pre_response = HookEnvironmentAdapter.handle_pre_response
    wpull_hook.callbacks.handle_response = HookEnvironmentAdapter.handle_response
    wpull_hook.callbacks.handle_error = HookEnvironmentAdapter.handle_error
    wpull_hook.callbacks.get_urls = HookEnvironmentAdapter.get_urls
    wpull_hook.callbacks.wait_time = HookEnvironmentAdapter.wait_time
    wpull_hook.callbacks.finishing_statistics = HookEnvironmentAdapter.finishing_statistics
    wpull_hook.callbacks.exit_status = HookEnvironmentAdapter.exit_status

    global lua
    lua = load_lua()
    lua_globals = lua.globals()
    lua_globals.wpull_hook = HookEnvironmentAdapter()
    lua_globals.wpull_hook.callbacks = callbacks

    with open(lua_script_path, 'rb') as in_file:
        lua.execute(in_file.read())
Exemplo n.º 25
0
    def setUp(self):
        self.redis = MockRedis()
        self.LPOP_SCRIPT_SHA = sha1(LPOP_SCRIPT).hexdigest()
        import lua
        self.lua = lua
        self.lua_globals = lua.globals()

        assert_equal_list = """
        function compare_list(list1, list2)
            if #list1 ~= #list2 then
                return false
            end
            for i, item1 in ipairs(list1) do
                if item1 ~= list2[i] then
                    return false
                end
            end
            return true
        end

        function assert_equal_list(list1, list2)
            assert(compare_list(list1, list2))
        end
        return assert_equal_list
        """
        self.lua_assert_equal_list = self.lua.execute(assert_equal_list)

        assert_equal_list_with_pairs = """
        function pair_exists(list1, key, value)
            i = 1
            for i, item1 in ipairs(list1) do
                if i%2 == 1 then
                    if (list1[i] == key) and (list1[i + 1] == value) then
                        return true
                    end
                end
            end
            return false
        end

        function compare_list_with_pairs(list1, list2)
            if #list1 ~= #list2 or #list1 % 2 == 1 then
                return false
            end
            for i = 1, #list1, 2 do
                if not pair_exists(list2, list1[i], list1[i + 1]) then
                    return false
                end
            end
            return true
        end

        function assert_equal_list_with_pairs(list1, list2)
            assert(compare_list_with_pairs(list1, list2))
        end
        return assert_equal_list_with_pairs
        """
        self.lua_assert_equal_list_with_pairs = self.lua.execute(assert_equal_list_with_pairs)

        compare_val = """
        function compare_val(var1, var2)
            return var1 == var2
        end
        return compare_val
        """
        self.lua_compare_val = self.lua.execute(compare_val)
Exemplo n.º 26
0
def install(lua_script_path):
    class CallbacksAdapter(object):
        AVAILABLE_VERSIONS = to_lua_number(wpull_hook.callbacks.AVAILABLE_VERSIONS)

        @property
        def version(self):
            return wpull_hook.version

        @version.setter
        def version(self, num):
            wpull_hook.version = num

        engine_run = NotImplemented
        resolve_dns = NotImplemented
        accept_url = NotImplemented
        queued_url = NotImplemented
        dequeued_url = NotImplemented
        handle_pre_response = NotImplemented
        handle_response = NotImplemented
        handle_error = NotImplemented
        get_urls = NotImplemented
        wait_time = NotImplemented
        finishing_statistics = NotImplemented
        exit_status = NotImplemented

    callbacks = CallbacksAdapter()

    class HookEnvironmentAdapter(object):
        factory = wpull_hook.factory
        actions = wpull_hook.actions

        @staticmethod
        def engine_run():
            if callbacks.engine_run is not NotImplemented:
                callbacks.engine_run()

        @staticmethod
        def resolve_dns(host):
            if callbacks.resolve_dns is not NotImplemented:
                return callbacks.resolve_dns(to_lua_type(host))

        @staticmethod
        def accept_url(url_info, record_info, verdict, reasons):
            if callbacks.accept_url is not NotImplemented:
                return callbacks.accept_url(
                    to_lua_type(url_info),
                    to_lua_type(record_info),
                    verdict,
                    to_lua_type(reasons)
                    )

        @staticmethod
        def queued_url(url_info):
            if callbacks.queued_url is not NotImplemented:
                callbacks.queued_url(to_lua_type(url_info))

        @staticmethod
        def dequeued_url(url_info, record_info):
            if callbacks.dequeued_url is not NotImplemented:
                callbacks.dequeued_url(
                    to_lua_type(url_info), to_lua_type(record_info))

        @staticmethod
        def handle_pre_response(url_info, record_info, http_info):
            if callbacks.handle_pre_response is not NotImplemented:
                return callbacks.handle_pre_response(
                    to_lua_type(url_info),
                    to_lua_type(record_info),
                    to_lua_type(http_info)
                    )
            else:
                return 'normal'

        @staticmethod
        def handle_response(url_info, record_info, http_info):
            if callbacks.handle_response is not NotImplemented:
                return callbacks.handle_response(
                    to_lua_type(url_info),
                    to_lua_type(record_info),
                    to_lua_type(http_info)
                    )
            else:
                return 'normal'

        @staticmethod
        def handle_error(url_info, record_info, error_info):
            if callbacks.handle_error is not NotImplemented:
                return callbacks.handle_error(
                    to_lua_type(url_info),
                    to_lua_type(record_info),
                    to_lua_type(error_info)
                    )
            else:
                return 'normal'

        @staticmethod
        def get_urls(filename, url_info, document_info):
            if callbacks.get_urls is not NotImplemented:
                result = (callbacks.get_urls(
                    to_lua_type(filename),
                    to_lua_type(url_info),
                    to_lua_type(document_info)
                    ))

                if result:
                    lua_items = from_lua_table_to_list(result)

                    items = []

                    for lua_item in lua_items:
                        item = {
                            'url': get_from_lua_table_as_dict(lua_item, 'url'),
                            'link_type': get_from_lua_table_as_dict(
                                lua_item, 'link_type'),
                            'inline': get_from_lua_table_as_dict(
                                lua_item, 'inline'),
                            'post_data': get_from_lua_table_as_dict(
                                lua_item, 'post_data'),
                            'replace': get_from_lua_table_as_dict(
                                lua_item, 'replace'),
                        }
                        items.append(item)

                    return items

        @staticmethod
        def wait_time(seconds):
            if callbacks.wait_time is not NotImplemented:
                return callbacks.wait_time(to_lua_type(seconds))
            else:
                return seconds

        @staticmethod
        def finishing_statistics(start_time, end_time, num_urls, bytes_downloaded):
            if callbacks.finishing_statistics is not NotImplemented:
                callbacks.finishing_statistics(
                    to_lua_type(start_time),
                    to_lua_type(end_time),
                    to_lua_type(num_urls),
                    to_lua_type(bytes_downloaded)
                    )

        @staticmethod
        def exit_status(exit_code):
            if callbacks.exit_status is not NotImplemented:
                return callbacks.exit_status(to_lua_type(exit_code))
            else:
                return exit_code

    wpull_hook.callbacks.engine_run = HookEnvironmentAdapter.engine_run
    wpull_hook.callbacks.resolve_dns = HookEnvironmentAdapter.resolve_dns
    wpull_hook.callbacks.accept_url = HookEnvironmentAdapter.accept_url
    wpull_hook.callbacks.queued_url = HookEnvironmentAdapter.queued_url
    wpull_hook.callbacks.dequeued_url = HookEnvironmentAdapter.dequeued_url
    wpull_hook.callbacks.handle_pre_response = HookEnvironmentAdapter.handle_pre_response
    wpull_hook.callbacks.handle_response = HookEnvironmentAdapter.handle_response
    wpull_hook.callbacks.handle_error = HookEnvironmentAdapter.handle_error
    wpull_hook.callbacks.get_urls = HookEnvironmentAdapter.get_urls
    wpull_hook.callbacks.wait_time = HookEnvironmentAdapter.wait_time
    wpull_hook.callbacks.finishing_statistics = HookEnvironmentAdapter.finishing_statistics
    wpull_hook.callbacks.exit_status = HookEnvironmentAdapter.exit_status

    global lua
    lua = load_lua()
    lua_globals = lua.globals()
    lua_globals.wpull_hook = HookEnvironmentAdapter()
    lua_globals.wpull_hook.callbacks = callbacks

    with open(lua_script_path, 'rb') as in_file:
        lua.execute(in_file.read())
Exemplo n.º 27
0
print "----- import lua -----"
import lua
print "----- lg = lua.globals() -----"
lg = lua.globals()
print "lg:", lg
print "lg._G:", lg._G
print "lg['_G']:", lg['_G']
print "----- lg.foo = \"bar\" -----"
lg.foo = 'bar'
print "----- lg.tmp = [] -----"
lg.tmp = []
print "----- print lg.tmp -----"
print lg.tmp
print "----- lua.execute(\"xxx = {1,2,3,foo={4,5}}\") -----"
lua.execute("xxx = {1,2,3,foo={4,5}}")
print "----- print lg.xxx[1] -----"
print lg.xxx[1]
print "----- print lg.xxx[2] -----"
print lg.xxx[2]
print "----- print lg.xxx[3] -----"
print lg.xxx[3]
print "----- print lg.xxx['foo'][1] -----"
print lg.xxx['foo'][1]
print "lua.require =", lua.require
try:
    lua.require("foo")
except:
    print "lua.require('foo') raised an exception"

Exemplo n.º 28
0
    def setUp(self):
        self.redis = MockRedis()
        self.LPOP_SCRIPT_SHA = sha1(LPOP_SCRIPT).hexdigest()
        import lua
        self.lua = lua
        self.lua_globals = lua.globals()

        assert_equal_list = """
        function compare_list(list1, list2)
            if #list1 ~= #list2 then
                return false
            end
            for i, item1 in ipairs(list1) do
                if item1 ~= list2[i] then
                    return false
                end
            end
            return true
        end

        function assert_equal_list(list1, list2)
            assert(compare_list(list1, list2))
        end
        return assert_equal_list
        """
        self.lua_assert_equal_list = self.lua.execute(assert_equal_list)

        assert_equal_list_with_pairs = """
        function pair_exists(list1, key, value)
            i = 1
            for i, item1 in ipairs(list1) do
                if i%2 == 1 then
                    if (list1[i] == key) and (list1[i + 1] == value) then
                        return true
                    end
                end
            end
            return false
        end

        function compare_list_with_pairs(list1, list2)
            if #list1 ~= #list2 or #list1 % 2 == 1 then
                return false
            end
            for i = 1, #list1, 2 do
                if not pair_exists(list2, list1[i], list1[i + 1]) then
                    return false
                end
            end
            return true
        end

        function assert_equal_list_with_pairs(list1, list2)
            assert(compare_list_with_pairs(list1, list2))
        end
        return assert_equal_list_with_pairs
        """
        self.lua_assert_equal_list_with_pairs = self.lua.execute(
            assert_equal_list_with_pairs)

        compare_val = """
        function compare_val(var1, var2)
            return var1 == var2
        end
        return compare_val
        """
        self.lua_compare_val = self.lua.execute(compare_val)
Exemplo n.º 29
0
# python
# coding: UTF-8

import sys
from socket_wrapper import SocketWrapper

import lua

luaenv = lua.globals()

SERVER_IP = '172.27.35.1'
SERVER_PORT = 31000

SELF_IP = '172.27.35.3'
SELF_PORT = 31001

sock = None

def inst_callback(data, attr):
	lua.execute("inst_parser('%s')" % data)

def exit_app():
	print "PyPol: exiting..."
	if sock: sock.close()
	sys.exit(0)

def lua_break_test():
	print "PyPol: lua_break_test called, pausing session..."
	sock.pause()

def lua_restart():
Exemplo n.º 30
0
import lua

print(lua.globals())
print("-----")

lua.execute(r"""
print(require("rapidjson"))
print(require("pb"))
""")

lua = None

import gc

gc.collect()
Exemplo n.º 31
0
 def __call__(self, text):
     return self.module.commands[self.name](lua.globals().tostring(text))
Exemplo n.º 32
0
 def loadModule(self, name, filename):
     if lua is None:
         return None
     fullname = os.path.join('quirks', name)
     lua.globals().package.loaded[fullname] = None
     return lua.require(fullname)