Example #1
0
    def wrapper(*args):
        # 1. Do some preprocessing.
        #print function.__doc__

        # 2. Call 'function' with given arguments.
        _lua.execute(function.__doc__)
        function(*args)
Example #2
0
    def __init__(self, *arg):
        pywikibot.output(u'\03{lightgreen}* Initialization of bot\03{default}')

        pywikibot.botirc.IRCBot.__init__(self, *arg)

        # init environment with minimal changes (try to do as less as possible)
        # - Lua -
        pywikibot.output(u'** Redirecting Lua print in order to catch it')
        lua.execute('__print = print')
        #lua.execute('print = python.builtins().print')
        lua.execute('print = python.globals().pywikibot.output')

        # init constants
        templ = pywikibot.Page(self.site, bot_config['ConfCSSshell'])
        cron = pywikibot.Page(self.site, bot_config['ConfCSScrontab'])

        self.templ = templ.title()
        self.cron = cron.title()
        self.refs = {self.templ: templ,
                     self.cron:  cron, }
        pywikibot.output(u'** Pre-loading all relevant page contents')
        for item in self.refs:
            # security; first check if page is protected, reject any data if not
            if os.path.splitext(self.refs[item].title().lower())[1] not in ['.css', '.js']:
                raise pywikibot.UserActionRefuse(u'Page %s is not secure, e.g. semi-protected!' % self.refs[item])
            self.refs[item].get(force=True)   # load all page contents

        # init background timer
        pywikibot.output(u'** Starting crontab background timer thread')
        self.on_timer()
Example #3
0
    def __init__(self, *arg):
        pywikibot.output(u'\03{lightgreen}* Initialization of bot\03{default}')

        pywikibot.botirc.IRCBot.__init__(self, *arg)

        # init environment with minimal changes (try to do as less as possible)
        # - Lua -
        pywikibot.output(u'** Redirecting Lua print in order to catch it')
        lua.execute('__print = print')
        #lua.execute('print = python.builtins().print')
        lua.execute('print = python.globals().pywikibot.output')

        # init constants
        templ = pywikibot.Page(self.site, bot_config['ConfCSSshell'])
        cron = pywikibot.Page(self.site, bot_config['ConfCSScrontab'])

        self.templ = templ.title()
        self.cron = cron.title()
        self.refs = {self.templ: templ,
                     self.cron:  cron, }
        pywikibot.output(u'** Pre-loading all relevant page contents')
        for item in self.refs:
            # security; first check if page is protected, reject any data if not
            if os.path.splitext(self.refs[item].title().lower())[1] not in ['.css', '.js']:
                raise pywikibot.UserActionRefuse(u'Page %s is not secure, e.g. semi-protected!' % self.refs[item])
            self.refs[item].get(force=True)   # load all page contents

        # init background timer
        pywikibot.output(u'** Starting crontab background timer thread')
        self.on_timer()
Example #4
0
    def wrapper(*args):
        # 1. Do some preprocessing.
        #print function.__doc__

        # 2. Call 'function' with given arguments.
        _lua.execute(function.__doc__)
        function(*args)
Example #5
0
    def __init__(self, *arg):
        pywikibot.output("\03{lightgreen}* Initialization of bot\03{default}")

        pywikibot.botirc.IRCBot.__init__(self, *arg)

        # init environment with minimal changes (try to do as less as possible)
        # - Lua -
        pywikibot.output("** Redirecting Lua print in order to catch it")
        lua.execute("__print = print")
        lua.execute("print = python.globals().pywikibot.output")
        # It may be useful in debugging to install the 'print' builtin
        # as the 'print' function in lua. To do this:
        # lua.execute('print = python.builtins().print')

        # init constants
        templ = pywikibot.Page(self.site, bot_config["ConfCSSshell"])
        cron = pywikibot.Page(self.site, bot_config["ConfCSScrontab"])

        self.templ = templ.title()
        self.cron = cron.title()
        self.refs = {self.templ: templ, self.cron: cron}
        pywikibot.output("** Pre-loading all relevant page contents")
        for item in self.refs:
            # security; first check if page is protected, reject any data if not
            if os.path.splitext(self.refs[item].title().lower())[1] not in [".css", ".js"]:
                raise ValueError(
                    "%s config %s = %s is not a secure page; "
                    "it should be a css or js userpage which are "
                    "automatically semi-protected." % (self.__class__.__name__, item, self.refs[item])
                )
            self.refs[item].get(force=True)  # load all page contents

        # init background timer
        pywikibot.output("** Starting crontab background timer thread")
        self.on_timer()
Example #6
0
    def __init__(self, *arg):
        """Initializer."""
        pywikibot.output(
            color_format('{lightgreen}* Initialization of bot{default}'))

        pywikibot.botirc.IRCBot.__init__(self, *arg)

        # init environment with minimal changes (try to do as less as possible)
        # - Lua -
        pywikibot.output('** Redirecting Lua print in order to catch it')
        lua.execute('__print = print')
        lua.execute('print = python.globals().pywikibot.output')
        # It may be useful in debugging to install the 'print' builtin
        # as the 'print' function in lua. To do this:
        # lua.execute('print = python.builtins().print')

        # init constants
        templ = pywikibot.Page(self.site, bot_config['ConfCSSshell'])
        cron = pywikibot.Page(self.site, bot_config['ConfCSScrontab'])

        self.templ = templ.title()
        self.cron = cron.title()
        self.refs = {
            self.templ: templ,
            self.cron: cron,
        }
        pywikibot.output('** Pre-loading all relevant page contents')
        for item in self.refs:
            # First check if page is protected, reject any data if not
            parts = self.refs[item].title().lower().rsplit('.')
            if len(parts) == 1 or parts[1] not in ['.css', '.js']:
                raise ValueError('{0} config {1} = {2} is not a secure page; '
                                 'it should be a css or js userpage which are '
                                 'automatically semi-protected.'.format(
                                     self.__class__.__name__, item,
                                     self.refs[item]))
            try:
                self.refs[item].get(force=True)  # load all page contents
            except pywikibot.NoPage:
                pywikibot.error(
                    "The configuration page {0} doesn't exists".format(
                        self.refs[item].title(as_link=True)))
                raise
        # init background timer
        pywikibot.output('** Starting crontab background timer thread')
        self.on_timer()
Example #7
0
    def __init__(self, *arg):
        """Constructor."""
        pywikibot.output(color_format(
            '{lightgreen}* Initialization of bot{default}'))

        pywikibot.botirc.IRCBot.__init__(self, *arg)

        # init environment with minimal changes (try to do as less as possible)
        # - Lua -
        pywikibot.output(u'** Redirecting Lua print in order to catch it')
        lua.execute('__print = print')
        lua.execute('print = python.globals().pywikibot.output')
        # It may be useful in debugging to install the 'print' builtin
        # as the 'print' function in lua. To do this:
        # lua.execute('print = python.builtins().print')

        # init constants
        templ = pywikibot.Page(self.site, bot_config['ConfCSSshell'])
        cron = pywikibot.Page(self.site, bot_config['ConfCSScrontab'])

        self.templ = templ.title()
        self.cron = cron.title()
        self.refs = {self.templ: templ,
                     self.cron: cron,
                     }
        pywikibot.output(u'** Pre-loading all relevant page contents')
        for item in self.refs:
            # First check if page is protected, reject any data if not
            parts = self.refs[item].title().lower().rsplit('.')
            if len(parts) == 1 or parts[1] not in ['.css', '.js']:
                raise ValueError('%s config %s = %s is not a secure page; '
                                 'it should be a css or js userpage which are '
                                 'automatically semi-protected.'
                                 % (self.__class__.__name__, item,
                                    self.refs[item]))
            try:
                self.refs[item].get(force=True)   # load all page contents
            except pywikibot.NoPage:
                pywikibot.error("The configuation page %s doesn't exists"
                                % self.refs[item].title(asLink=True))
                raise
        # init background timer
        pywikibot.output(u'** Starting crontab background timer thread')
        self.on_timer()
Example #8
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)
Example #9
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)
Example #10
0
    def __init__(self, *arg):
        """Constructor."""
        pywikibot.output(
            color_format('{lightgreen}* Initialization of bot{default}'))

        pywikibot.botirc.IRCBot.__init__(self, *arg)

        # init environment with minimal changes (try to do as less as possible)
        # - Lua -
        pywikibot.output(u'** Redirecting Lua print in order to catch it')
        lua.execute('__print = print')
        lua.execute('print = python.globals().pywikibot.output')
        # It may be useful in debugging to install the 'print' builtin
        # as the 'print' function in lua. To do this:
        # lua.execute('print = python.builtins().print')

        # init constants
        templ = pywikibot.Page(self.site, bot_config['ConfCSSshell'])
        cron = pywikibot.Page(self.site, bot_config['ConfCSScrontab'])

        self.templ = templ.title()
        self.cron = cron.title()
        self.refs = {
            self.templ: templ,
            self.cron: cron,
        }
        pywikibot.output(u'** Pre-loading all relevant page contents')
        for item in self.refs:
            # security; first check if page is protected, reject any data if not
            if os.path.splitext(
                    self.refs[item].title().lower())[1] not in ['.css', '.js']:
                raise ValueError(
                    u'%s config %s = %s is not a secure page; '
                    u'it should be a css or js userpage which are '
                    u'automatically semi-protected.' %
                    (self.__class__.__name__, item, self.refs[item]))
            self.refs[item].get(force=True)  # load all page contents

        # init background timer
        pywikibot.output(u'** Starting crontab background timer thread')
        self.on_timer()
Example #11
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)
Example #12
0
def profile( name, file='/with/longbeach/conf/core/database.lua', as_type=None ):
    code = """
function run(scriptfile)
    local env = setmetatable({}, {__index=_G})
    assert(pcall(setfenv(assert(loadfile(scriptfile)), env)))
    setmetatable(env, nil)
    return env
end
    """
    import lua
    lua.execute(code)
    env = lua.eval( "run('{0}')".format(file) )
    databases = env.databases
    if name in databases:
        t = databases[name]
        r = {}
        for i in t:
            r[i] = t[i]

        if as_type=='mysqldb':
            return get_dbparams(r)
        elif as_type=='url':
            return "{adapter}://{username}:{password}@{host}/{database}".format(**r)
        return r
Example #13
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))
Example #14
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))
Example #15
0
print "----- import lua -----"
import lua
import numpy as np
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"

print """----- pythonDict = {"coucou": 1, "truc": 2} -----"""
Example #16
0
import lua

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

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

lua = None

import gc

gc.collect()
Example #17
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"

Example #18
0
#!/usr/bin/env python

import consts
import lua
import os

lua.execute("LUA_PATH = '%s/?'" % consts.THEMES_DIR)


def hex2rgb(value):
    """
    Convert a hex string to an rgb tuple.
    """
    value = value.lstrip("#")
    lv = len(value)
    if lv == 3:
        return hex2rgb("".join([2 * value[i] for i in (0, 1, 2)]))
    return tuple(int(value[i : i + lv / 3], 16) for i in range(0, lv, lv / 3))


def color(value):

    # at least  one theme  (dante) uses a  keyword color name,  in which
    # case I just default to black, until I find a better solution
    if value.startswith("#"):
        return r"\color[RGB]{%d,%d,%d}" % hex2rgb(value)

    return r"\color[RGB]{0,0,0}"


def style(rule):
import sys
import gc as gc
import numpy as np
print "----- import lua -----"
import lua
lg = lua.globals()

print "----- ndarray = np.random.rand(3,4) -----"
ndarray = np.random.rand(3,4)
print "Initial refcount: " + str(sys.getrefcount(ndarray))
print "----- lg.tensor = ndarray -----"
lg.tensor = ndarray
print "Refcount should be 2+1: " + str(sys.getrefcount(ndarray))
print "----- lua.execute('print(tensor:size())') -----"
lua.execute('print(tensor:size())')
print "----- ndarray.resize(20) -----"
try:
    ndarray.resize(20)
    print("Error you should not be able to resize this array")
except Exception as e:
    print("Message:")
    print(e)
    print("This is expected")
print "----- lua.execute('tensor:resize(20)') -----"
try:
    print("Message:")
    lua.execute('tensor:resize(20)')
    print("Error you should not be able to resize this tensor")
except Exception as e:
    print(e)
    print("This is expected")
Example #20
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())
Example #21
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())
Example #22
0
    "relics": "http://warframe.wikia.com/wiki/Module:Void/data?action=edit",
    "icons": "http://warframe.wikia.com/wiki/Module:Icon/data?action=edit",
    "modules": "http://warframe.wikia.com/wiki/Module:Mods/data?action=edit",
    "research": "http://warframe.wikia.com/wiki/Module:Research/data?action=edit",
    "focus": "http://warframe.wikia.com/wiki/Module:Focus/data?action=edit",
    "arcanes": "http://warframe.wikia.com/wiki/Module:Arcane/data?action=edit",
    "missions": "http://warframe.wikia.com/wiki/Module:Missions/data?action=edit",
    "abilities": "http://warframe.wikia.com/wiki/Module:Ability/data?action=edit"
}

for category, url in urls.items():
    if os.path.exists("./cache/{}.cache".format(category)):
        with open("./cache/{}.cache".format(category)) as inf:
            data = inf.read()
    else:
        data = requests.get(url).content.decode()
        with open("./cache/{}.cache".format(category),"w") as outf:
            outf.write(data)

    rawdata = data.split(" name=\"wpTextbox1\">")[1].split("</textarea>")[0]

    with open("json.lua") as inf:
        ljson = lua.execute(inf.read())
    d=lua.execute(rawdata)

    with open("../data/{}WikiData.json".format(category),"w") as outf:
        outf.write(ljson.encode(d))

semlar = requests.get("https://semlar.com/buffs")
for image_path in re.findall("<img src=\"(https://semlar.com/textures/.*?\.png)\""):
    if 
Example #23
0
import gym
import lua
import numpy as np

torch = lua.require('torch')
lua.require('trepl')
lua.require('cunn')  # We run the network on GPU
dqn = lua.eval("dofile('dqn/NeuralQLearner.lua')")
tt = lua.eval("dofile('dqn/TransitionTable.lua')")
lua.execute("dofile('dqn/Scale.lua')")  # for the preproc

env = gym.make('Breakout-v0')

possible_actions = lua.toTable({1: 0, 2: 2, 3: 3, 4: 1})

agent = torch.load("out/net-10000.t7")
agent.bestq = 0

observation = env.reset()
action_index = 4
done = False
t = 1
while True:
    t += 1
    env.render()
    observation, reward, done, info = env.step(possible_actions[action_index])
    observation = np.ascontiguousarray(observation.transpose([2, 0, 1]))
    action_index = agent.perceive(agent, reward, observation, done, True)
    if done:
        print "Episode finished after {} timesteps".format(t + 1)
        break
Example #24
0
def inst_callback(data, attr):
	lua.execute("inst_parser('%s')" % data)
Example #25
0
import gym
import lua
import numpy as np

torch = lua.require('torch')
lua.require('trepl')
lua.require('cunn') # We run the network on GPU
dqn = lua.eval("dofile('dqn/NeuralQLearner.lua')")
tt = lua.eval("dofile('dqn/TransitionTable.lua')")
lua.execute("dofile('dqn/Scale.lua')") # for the preproc

env = gym.make('Breakout-v0')

possible_actions = lua.toTable({
        1: 0,
        2: 2,
        3: 3,
        4: 1
    })

agent = torch.load("out/net-10000.t7")
agent.bestq = 0

observation = env.reset()
action_index = 4
done = False
t=1
while True:
    t += 1
    env.render()
    observation, reward, done, info = env.step(possible_actions[action_index])
Example #26
0
#!/usr/bin/env python -i

# Very important for linux, via:
# http://stackoverflow.com/questions/8361437/linker-error-lunatic-python-lua-requiresocket-undefined-symbol-lua-getme
import sys

if sys.platform != "darwin":
	import DLFCN
	sys.setdlopenflags(DLFCN.RTLD_NOW | DLFCN.RTLD_GLOBAL)

import os
# Go to the root directory for includes
CWD = os.getcwd()
HOME = CWD.replace("/ROS","")
os.chdir(HOME)
	
# Import luantic-python
sys.path.append(HOME+"/Modules/python")
import lua
# Have all the framework files/Modules available
lua.execute('dofile"fiddle.lua"')
# Have the globals if needed...
lg = lua.globals()
require = lua.require

# Go back to the current directory
os.chdir(CWD)