Пример #1
0
    def loadfile(self, filename):
        try:
            self.filename = filename
            with open(filename) as f:
                data = f.read()
            compiled = compile_file(data, filename)
            exec(compiled, self.globals)

            try:
                self.name = self.globals['__module_name__']
            except KeyError:
                lib.hexchat_print(
                    lib.ph,
                    b'Failed to load module: __module_name__ must be set')
                return False

            self.version = self.globals.get('__module_version__', '')
            self.description = self.globals.get('__module_description__', '')
            self.ph = lib.hexchat_plugingui_add(lib.ph, filename.encode(),
                                                self.name.encode(),
                                                self.description.encode(),
                                                self.version.encode(),
                                                ffi.NULL)
        except Exception as e:
            lib.hexchat_print(lib.ph,
                              'Failed to load module: {}'.format(e).encode())
            traceback.print_exc()
            return False
        return True
Пример #2
0
def _on_say_command(word, word_eol, userdata):
    channel = ffi.string(lib.hexchat_get_info(lib.ph, b'channel'))
    if channel == b'>>python<<':
        python = ffi.string(word_eol[1])
        lib.hexchat_print(lib.ph, b'>>> ' + python)
        exec_in_interp(__decode(python))
    return 0
Пример #3
0
    def loadfile(self, filename):
        try:
            self.filename = filename
            with open(filename) as f:
                data = f.read()
            compiled = compile_file(data, filename)
            exec(compiled, self.globals)

            try:
                self.name = self.globals['__module_name__']

            except KeyError:
                lib.hexchat_print(lib.ph, b'Failed to load module: __module_name__ must be set')

                return False

            self.version = self.globals.get('__module_version__', '')
            self.description = self.globals.get('__module_description__', '')
            self.ph = lib.hexchat_plugingui_add(lib.ph, filename.encode(), self.name.encode(),
                                                self.description.encode(), self.version.encode(), ffi.NULL)

        except Exception as e:
            lib.hexchat_print(lib.ph, 'Failed to load module: {}'.format(e).encode())
            traceback.print_exc()
            return False

        return True
Пример #4
0
def _on_say_command(word, word_eol, userdata):
    channel = ffi.string(lib.hexchat_get_info(lib.ph, b'channel'))
    if channel == b'>>python<<':
        python = ffi.string(word_eol[1])
        lib.hexchat_print(lib.ph, b'>>> ' + python)
        exec_in_interp(__decode(python))
        return 1
    return 0
Пример #5
0
    def __change_context(self):
        old_ctx = lib.hexchat_get_context(lib.ph)
        if not self.set():
            # XXX: Behavior change, previously used wrong context
            lib.hexchat_print(lib.ph, b'Context object refers to closed context, ignoring call')
            return

        yield
        lib.hexchat_set_context(lib.ph, old_ctx)
Пример #6
0
 def write(self, string):
     string = string.encode()
     idx = string.rfind(b'\n')
     if idx != -1:
         self.buffer += string[:idx]
         lib.hexchat_print(lib.ph, bytes(self.buffer))
         self.buffer = bytearray(string[idx + 1:])
     else:
         self.buffer += string
Пример #7
0
    def __change_context(self):
        old_ctx = lib.hexchat_get_context(lib.ph)
        if not self.set():
            # XXX: Behavior change, previously used wrong context
            lib.hexchat_print(lib.ph, b'Context object refers to closed context, ignoring call')
            return

        yield
        lib.hexchat_set_context(lib.ph, old_ctx)
Пример #8
0
 def write(self, string):
     string = string.encode()
     idx = string.rfind(b'\n')
     if idx is not -1:
         self.buffer += string[:idx]
         lib.hexchat_print(lib.ph, bytes(self.buffer))
         self.buffer = bytearray(string[idx + 1:])
     else:
         self.buffer += string
Пример #9
0
def list_plugins():
    if not plugins:
        lib.hexchat_print(lib.ph, b'No python modules loaded')
        return

    lib.hexchat_print(lib.ph, b'Name         Version  Filename             Description')
    lib.hexchat_print(lib.ph, b'----         -------  --------             -----------')
    for plugin in plugins:
        basename = os.path.basename(plugin.filename).encode()
        name = plugin.name.encode()
        version = plugin.version.encode() if plugin.version else b'<none>'
        description = plugin.description.encode() if plugin.description else b'<none>'
        string = b'%-12s %-8s %-20s %-10s' %(name, version, basename, description)
        lib.hexchat_print(lib.ph, string)
    lib.hexchat_print(lib.ph, b'')
Пример #10
0
def list_plugins():
    if not plugins:
        lib.hexchat_print(lib.ph, b'No python modules loaded')
        return

    lib.hexchat_print(lib.ph, b'Name         Version  Filename             Description')
    lib.hexchat_print(lib.ph, b'----         -------  --------             -----------')
    for plugin in plugins:
        basename = os.path.basename(plugin.filename).encode()
        name = plugin.name.encode()
        version = plugin.version.encode() if plugin.version else b'<none>'
        description = plugin.description.encode() if plugin.description else b'<none>'
        string = b'%-12s %-8s %-20s %-10s' %(name, version, basename, description)
        lib.hexchat_print(lib.ph, string)
    lib.hexchat_print(lib.ph, b'')
Пример #11
0
def _on_plugin_init(plugin_name, plugin_desc, plugin_version, arg, libdir):
    global hexchat
    global hexchat_stdout

    signal.signal(signal.SIGINT, signal.SIG_DFL)

    plugin_name[0] = PLUGIN_NAME
    plugin_desc[0] = PLUGIN_DESC
    plugin_version[0] = PLUGIN_VERSION

    try:
        libdir = __decode(ffi.string(libdir))
        modpath = os.path.join(libdir, '..', 'python')
        sys.path.append(os.path.abspath(modpath))
        hexchat = importlib.import_module('hexchat')

    except (UnicodeDecodeError, ImportError) as e:
        lib.hexchat_print(lib.ph,
                          b'Failed to import module: ' + repr(e).encode())

        return 0

    hexchat_stdout = Stdout()
    sys.stdout = hexchat_stdout
    sys.stderr = hexchat_stdout
    pydoc.help = pydoc.Helper(HelpEater(), HelpEater())

    lib.hexchat_hook_command(lib.ph, b'', 0, lib._on_say_command, ffi.NULL,
                             ffi.NULL)
    lib.hexchat_hook_command(lib.ph, b'LOAD', 0, lib._on_load_command,
                             ffi.NULL, ffi.NULL)
    lib.hexchat_hook_command(lib.ph, b'UNLOAD', 0, lib._on_unload_command,
                             ffi.NULL, ffi.NULL)
    lib.hexchat_hook_command(lib.ph, b'RELOAD', 0, lib._on_reload_command,
                             ffi.NULL, ffi.NULL)
    lib.hexchat_hook_command(
        lib.ph, b'PY', 0, lib._on_py_command, b'''Usage: /PY LOAD   <filename>
           UNLOAD <filename|name>
           RELOAD <filename|name>
           LIST
           EXEC <command>
           CONSOLE
           ABOUT''', ffi.NULL)

    lib.hexchat_print(lib.ph, b'Python interface loaded')
    autoload()
    return 1
Пример #12
0
def exec_in_interp(python):
    global local_interp

    if not python:
        return

    if local_interp is None:
        local_interp = Plugin()
        local_interp.locals = {}
        local_interp.globals['hexchat'] = hexchat

    code = compile_line(python)
    try:
        ret = eval(code, local_interp.globals, local_interp.locals)
        if ret is not None:
            lib.hexchat_print(lib.ph, '{}'.format(ret).encode())
    except Exception as e:
        traceback.print_exc(file=hexchat_stdout)
Пример #13
0
def exec_in_interp(python):
    global local_interp

    if not python:
        return

    if local_interp is None:
        local_interp = Plugin()
        local_interp.locals = {}
        local_interp.globals['hexchat'] = hexchat

    code = compile_line(python)
    try:
        ret = eval(code, local_interp.globals, local_interp.locals)
        if ret is not None:
            lib.hexchat_print(lib.ph, '{}'.format(ret).encode())
    except Exception as e:
        traceback.print_exc(file=hexchat_stdout)
Пример #14
0
def _on_plugin_init(plugin_name, plugin_desc, plugin_version, arg, libdir):
    global hexchat
    global hexchat_stdout

    signal.signal(signal.SIGINT, signal.SIG_DFL)

    plugin_name[0] = PLUGIN_NAME
    plugin_desc[0] = PLUGIN_DESC
    plugin_version[0] = PLUGIN_VERSION

    try:
        libdir = __decode(ffi.string(libdir))
        modpath = os.path.join(libdir, '..', 'python')
        sys.path.append(os.path.abspath(modpath))
        hexchat = importlib.import_module('hexchat')

    except (UnicodeDecodeError, ImportError) as e:
        lib.hexchat_print(lib.ph, b'Failed to import module: ' + repr(e).encode())

        return 0

    hexchat_stdout = Stdout()
    sys.stdout = hexchat_stdout
    sys.stderr = hexchat_stdout
    pydoc.help = pydoc.Helper(HelpEater(), HelpEater())

    lib.hexchat_hook_command(lib.ph, b'', 0, lib._on_say_command, ffi.NULL, ffi.NULL)
    lib.hexchat_hook_command(lib.ph, b'LOAD', 0, lib._on_load_command, ffi.NULL, ffi.NULL)
    lib.hexchat_hook_command(lib.ph, b'UNLOAD', 0, lib._on_unload_command, ffi.NULL, ffi.NULL)
    lib.hexchat_hook_command(lib.ph, b'RELOAD', 0, lib._on_reload_command, ffi.NULL, ffi.NULL)
    lib.hexchat_hook_command(lib.ph, b'PY', 0, lib._on_py_command, b'''Usage: /PY LOAD   <filename>
           UNLOAD <filename|name>
           RELOAD <filename|name>
           LIST
           EXEC <command>
           CONSOLE
           ABOUT''', ffi.NULL)

    lib.hexchat_print(lib.ph, b'Python interface loaded')
    autoload()
    return 1
Пример #15
0
def _on_py_command(word, word_eol, userdata):
    subcmd = __decode(ffi.string(word[2])).lower()

    if subcmd == 'exec':
        python = __decode(ffi.string(word_eol[3]))
        exec_in_interp(python)

    elif subcmd == 'load':
        filename = __decode(ffi.string(word[3]))
        load_filename(filename)

    elif subcmd == 'unload':
        name = __decode(ffi.string(word[3]))
        if not unload_name(name):
            lib.hexchat_print(lib.ph, b'Can\'t find a python plugin with that name')

    elif subcmd == 'reload':
        name = __decode(ffi.string(word[3]))
        if not reload_name(name):
            lib.hexchat_print(lib.ph, b'Can\'t find a python plugin with that name')

    elif subcmd == 'console':
        lib.hexchat_command(lib.ph, b'QUERY >>python<<')

    elif subcmd == 'list':
        list_plugins()

    elif subcmd == 'about':
        lib.hexchat_print(lib.ph, b'HexChat Python interface version ' + VERSION)

    else:
        lib.hexchat_command(lib.ph, b'HELP PY')

    return 3
Пример #16
0
def list_plugins():
    if not plugins:
        lib.hexchat_print(lib.ph, b'No python modules loaded')
        return

    tbl_headers = [b'Name', b'Version', b'Filename', b'Description']
    tbl = [
        tbl_headers,
        [(b'-' * len(s)) for s in tbl_headers]
    ]

    for plugin in plugins:
        basename = os.path.basename(plugin.filename).encode()
        name = plugin.name.encode()
        version = plugin.version.encode() if plugin.version else b'<none>'
        description = plugin.description.encode() if plugin.description else b'<none>'
        tbl.append((name, version, basename, description))

    column_sizes = [
        max(len(item) for item in column)
        for column in zip(*tbl)
    ]

    for row in tbl:
        lib.hexchat_print(lib.ph, b' '.join(item.ljust(column_sizes[i])
                                            for i, item in enumerate(row)))
    lib.hexchat_print(lib.ph, b'')
Пример #17
0
def _on_py_command(word, word_eol, userdata):
    subcmd = __decode(ffi.string(word[2])).lower()

    if subcmd == 'exec':
        python = __decode(ffi.string(word_eol[3]))
        exec_in_interp(python)
    elif subcmd == 'load':
        filename = __decode(ffi.string(word[3]))
        load_filename(filename)
    elif subcmd == 'unload':
        name = __decode(ffi.string(word[3]))
        if not unload_name(name):
            lib.hexchat_print(lib.ph,
                              b'Can\'t find a python plugin with that name')
    elif subcmd == 'reload':
        name = __decode(ffi.string(word[3]))
        if not reload_name(name):
            lib.hexchat_print(lib.ph,
                              b'Can\'t find a python plugin with that name')
    elif subcmd == 'console':
        lib.hexchat_command(lib.ph, b'QUERY >>python<<')
    elif subcmd == 'list':
        list_plugins()
    elif subcmd == 'about':
        lib.hexchat_print(lib.ph,
                          b'HexChat Python interface version ' + VERSION)
    else:
        lib.hexchat_command(lib.ph, b'HELP PY')

    return 3
Пример #18
0
def list_plugins():
    if not plugins:
        lib.hexchat_print(lib.ph, b'No python modules loaded')
        return

    tbl_headers = [b'Name', b'Version', b'Filename', b'Description']
    tbl = [
        tbl_headers,
        [(b'-' * len(s)) for s in tbl_headers]
    ]

    for plugin in plugins:
        basename = os.path.basename(plugin.filename).encode()
        name = plugin.name.encode()
        version = plugin.version.encode() if plugin.version else b'<none>'
        description = plugin.description.encode() if plugin.description else b'<none>'
        tbl.append((name, version, basename, description))

    column_sizes = [
        max(len(item) for item in column)
        for column in zip(*tbl)
    ]

    for row in tbl:
        lib.hexchat_print(lib.ph, b' '.join(item.ljust(column_sizes[i])
                                            for i, item in enumerate(row)))
    lib.hexchat_print(lib.ph, b'')
Пример #19
0
def prnt(string):
    lib.hexchat_print(lib.ph, string.encode())
Пример #20
0
def prnt(string):
    lib.hexchat_print(lib.ph, string.encode())