Ejemplo n.º 1
0
def _get_arp_infos_linux():
    """Get arp infos.

    :return: arp infos dict
    """
    result = {}

    # ping sweep
    try:
        subprocess.check_output('ping 255.255.255.255 -b -c 1', shell=True)
    except subprocess.CalledProcessError as e:
        Log.error('Network helper: ping sweep: %s' % e)

    output = subprocess.check_output('arp -e', shell=True)
    if not output:
        return result

    for line in output.split(b'\n'):
        if not line or b'gateway' in line or b'incomplete' in line:
            continue

        data = line.split()
        result[data[0].decode()] = {
            'mac': data[2].decode().replace(':', ''),
            'is_dynamic': True,  # entry are remove on inactive
        }

    return result
Ejemplo n.º 2
0
    def diff_percent(self, other_image):
        """Calculate difference percent.

        :param other_image:
        :return: difference percent.
        """
        img1 = self.get_pil()
        img2 = other_image.get_pil()

        if img1.mode != img2.mode:
            Log.debug('Image diff percent: Different kinds of images.')
            return 100

        if img1.size != img2.size:
            Log.debug('Image diff percent: Different sizes.')
            return 100

        pairs = zip(img1.getdata(), img2.getdata())
        if len(img1.getbands()) == 1:
            dif = sum(abs(p1-p2) for p1,p2 in pairs)  # for gray-scale jpegs
        else:
            dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))

        ncomponents = img1.size[0] * img1.size[1] * 3
        return (dif / 255.0 * 100) / ncomponents
Ejemplo n.º 3
0
class UpdateToken:
    def __init__(self, config_vk):
        self.log = Log("UpdateToken")

        self.config = config_vk
        self.client_id = self.config.client_id
        self.finished = Event()

    async def __call__(self) -> str:
        async with RedirectServer() as server:
            redirect_address = server.redirect_address

            url = "https://oauth.vk.com/authorize" \
                  f"?client_id={self.client_id}" \
                  "&display=page" \
                  f"&redirect_uri={redirect_address}" \
                  "&scope=friends,wall,offline,groups" \
                  "&response_type=token" \
                  "&v=5.103"

            webbrowser.open_new(url)

            data = await server()

            self.config.token = data['access_token']
            self.config.user_id = data['user_id']

            self.config.update()
            self.log.info("Token updated")

        self.finished.set()
def __index_modules(reload=False):
    """Add all modules in module_list."""
    global __modules_data

    if __modules_data and not reload:
        return

    dir_list = sorted(os.listdir(MODULES_PATH))
    nb = 0

    __modules_data.clear()
    for module_name in dir_list:

        if is_disabled(module_name):
            continue

        if '__pycache__' in module_name:
            continue

        __modules_data[module_name] = {
            'disabled': False,
            'instance': None,
            'thread': None
        }
        Log.debug('index "%s" module' % module_name)
        nb += 1

    Log.info('%d modules indexed' % nb)
Ejemplo n.º 5
0
    async def leave(self, ctx):
        if ctx.author in self.tournament.participants:
            await ctx.author.remove_roles(self.roles.participant)
            if ctx.author in self.tournament.winners:
                self.tournament.winners.remove(ctx.author)
            self.tournament.remove_participant(ctx.author)
            await ctx.send(
                f"{Emote.leave} {ctx.author.mention} left the tournament.")
            Log("Participant Left",
                description=
                f"{ctx.author.mention} left **{self.tournament.name}**.",
                color=Color.dark_gold())

            embed = UpdatedEmbed(self.tournament)
            await self.tournament.msg.edit(embed=embed)
            await self.checklist.update_text(ctx, self.tournament)

        elif ctx.author in self.tournament.spectators:
            await ctx.author.remove_roles(self.roles.spectator)
            self.tournament.spectators.remove(ctx.author)
            await ctx.send(
                f"{ctx.author.mention} is no longer spectating the tournament."
            )
            Log("Spectator Left",
                description=
                f"{ctx.author.mention} left **{self.tournament.name}**.",
                color=Color.dark_gold())

            embed = UpdatedEmbed(self.tournament)
            await self.tournament.msg.edit(embed=embed)

        else:
            raise commands.BadArgument("You are not in a tournament!")
Ejemplo n.º 6
0
    async def kick(self, ctx, user: Member):
        if user in self.tournament.participants:
            await user.remove_roles(self.roles.participant)
            if user in self.tournament.winners:
                self.tournament.winners.remove(user)
            self.tournament.remove_participant(user)
            await ctx.send(
                f"{Emote.leave} {user.mention} was kicked from the tournament."
            )
            Log("Participant Kicked",
                description=
                f"{user.mention} was kicked from **{self.tournament.name}** by {ctx.author.mention}.",
                color=Color.dark_gold())

            embed = UpdatedEmbed(self.tournament)
            await self.tournament.msg.edit(embed=embed)
            await self.checklist.update_text(ctx, self.tournament)

        elif user in self.tournament.spectators:
            await user.remove_roles(self.roles.spectator)
            self.tournament.spectators.remove(user)
            await ctx.send(
                f"{ctx.author.mention} is no longer spectating the tournament."
            )
            Log("Spectator Kicked",
                description=
                f"{ctx.author.mention} was kicked from **{self.tournament.name}** by {ctx.author.mention}.",
                color=Color.dark_gold())

            embed = UpdatedEmbed(self.tournament)
            await self.tournament.msg.edit(embed=embed)

        else:
            raise commands.BadArgument("This user is not in a tournament!")
Ejemplo n.º 7
0
    def install_module(self, name):
        Log.debug('install %s module' % name)

        if name.startswith('git:'):
            return self.install_git_module(name[4:])

        return self.install_mola_module(name)
Ejemplo n.º 8
0
    def install_module(self, name):
        Log.debug('install %s module' % name)

        if name.startswith('git:'):
            return self.install_git_module(name[4:])

        return self.install_mola_module(name)
def register(event_name, handler):
    """Register a handler."""
    Log.debug('register handler event: %s for %s' % (handler, event_name))

    if event_name not in __event_handlers:
        __event_handlers[event_name] = []

    __event_handlers[event_name].append(handler)
def stop_all():
    """Stop all modules."""
    nb = 0

    for module_name in __modules_data.copy():
        if __modules_data[module_name]['instance'] and stop(module_name):
            nb += 1

    Log.info('%d modules stopped' % nb)
def update_module(module_name):
    """Update a module.

    :param module_name: module name
    :return: is sucess
    """
    if not ModuleManager.get(module_name):
        Log.error("Module not installed")
        return False
Ejemplo n.º 12
0
    def started(self, component):
        self.load_configuration()
        config = self.choice_config()
        if not config:
            return

        self.install_modules(config)
        Log.debug('install sucessfull')
        shutil.rmtree(os.path.join(MODULES_PATH, 'install'))
        Daemon.restart()
Ejemplo n.º 13
0
    def started(self, component):
        self.load_configuration()
        config = self.choice_config()
        if not config:
            return

        self.install_modules(config)
        Log.debug('install sucessfull')
        shutil.rmtree(os.path.join(MODULES_PATH, 'install'))
        Daemon.restart()
Ejemplo n.º 14
0
    def read_config_file(self, path):
        if not os.path.isfile(path):
            return False

        with open(path) as config_file:
            self.add_config(**json.load(config_file))
            return True

        Log.error('config file "%s" not found' % path)
        return False
Ejemplo n.º 15
0
    def read_config_file(self, path):
        if not os.path.isfile(path):
            return False

        with open(path) as config_file:
            self.add_config(**json.load(config_file))
            return True

        Log.error('config file "%s" not found' % path)
        return False
def stop(module_name):
    """Stop module."""
    __active_modules_name.remove(module_name)
    Log.info('stop "%s" module' % module_name)

    __modules_data[module_name]['instance'].stopped()
    __modules_data[module_name]['instance'].is_running = False
    __modules_data.pop(module_name)

    return True
def load_config(module_name):
    """Load module config."""
    if is_disabled(module_name):
        return False

    if call(module_name, 'load_config', c_optional_call=True):
        Log.info('start "%s" module' % module_name)
        return True

    return False
    def __print_pending_change(self, module_name=''):
        path = ROOT_PATH

        if module_name:
            path = os.path.join(path, 'modules', module_name)

        result = subprocess.getoutput('cd "%s" && git status' % path)

        if not 'nothing to commit' in result:
            type =  '"%s" module' % module_name if module_name else 'core'
            Log.debug('change in %s: %s' % (type, result))
Ejemplo n.º 19
0
def stop():
    """Stop daemon."""
    global __is_running

    if not __is_running:
        return

    Log.info('stop deamon')
    __is_running = False
    ModuleManager.stop_all()
    sys.exit(0)
Ejemplo n.º 20
0
def fire(event):
    """ send a event."""
    Log.debug('event: %s' % event)

    if event.name not in __event_handlers:
        return

    for handler in __event_handlers[event.name]:
        threading.Thread(
            None, handler, 'Event: %s at %s' % (event, str(handler)), (event,)
        ).start()
    def __start_mola(self):
        try:
            self.__mola_process = subprocess.Popen(
                'python3 %sdaemon.py' % self.__tmp_path,
                stdout=subprocess.PIPE, 
                shell=True,
                preexec_fn=os.setsid
            )

        except Exception as e:
            Log.crash(e) 
Ejemplo n.º 22
0
    def __init__(self, name: str, ts: InpT, vs: Optional[InpT] = None):
        self.name = name
        self.log = Log(f"TimeSeries:{name}")
        if vs is None:
            self.ts, self.vs = np.unique(ts, return_counts=True)
        else:
            self.ts, self.vs = ts, vs

        assert isinstance(self.ts, np.ndarray)
        assert isinstance(self.vs, np.ndarray)

        assert len(self.ts) == len(self.vs)
Ejemplo n.º 23
0
    def __init__(self, addr, port):
        self.addr = addr
        self.port = port
        self.log = Log(f"Monitoring")
        self._pages: Dict[str, BasePage] = {}

        self.main_page = MainPage("_main", "Info")
        self.add_page(self.main_page)

        self.app: web.Application = None

        self._middleware = []
Ejemplo n.º 24
0
    def __init__(self, servers, leaderID):

        self.servers = servers
        self.sockets = {}
        self.leaderID = leaderID
        self.term = 1
        self.log = Log(None, None, None, True)

        for server in self.servers.items():
            serverID, IP_port = server
            self.createSocket(IP_port[0], IP_port[1], serverID)

        self.initServerThreads()
Ejemplo n.º 25
0
    def __init__(self):
        self.log = Log("RedirectServer")

        self.app: web.Application = None
        self.runner: web.AppRunner = None
        self.site: web.TCPSite = None
        self.template: str = None

        self.data = None
        self.data_received = Event()

        self.address = "localhost"
        self.port = randint(8081, 9000)
Ejemplo n.º 26
0
    def __init__(self, config_vk):
        self.log = Log("VK")

        self.config = config_vk

        self.additional_params = {
            'access_token': self.config.token,
            'lang': 'ru',
            'v': "5.103"
        }

        self.person_fields = ",".join([
            "first_name",
            "last_name",
            "deactivated",
            "verified",
            "sex",
            "bdate",
            "city",  # TODO: https://vk.com/dev/places.getCityById
            "country",  # TODO: https://vk.com/dev/places.getCountryById
            "home_town",
            "photo_400_orig",
            "online",
            "has_mobile",
            "contacts",
            "education",
            "universities",
            "schools",
            "last_seen",
            "occupation",
            "hidden",
        ])

        self.group_info_fields = ",".join([
            'id', 'name', 'type', 'photo_200', 'city', 'description', 'place'
        ])

        self.post_fields = ",".join([])

        self.session: aiohttp.ClientSession = None

        self.last_call = 0
        self.threshold = 1 / 3

        self._update_token = None

        self.query_lock = asyncio.Lock()

        self.stats = VKStats('vk', "VK API")

        self.auth_lock = asyncio.Lock()
    def __scan_modules(self):
        dir_list = sorted(os.listdir(MODULES_PATH))

        for module_name in dir_list:
            dir_path = os.path.join(MODULES_PATH, module_name)

            if '__pycache__' in module_name or not os.path.isdir(dir_path):
                continue

            if not os.path.isdir(os.path.join(dir_path, '.git')):
                Log.debug('Not git repository: %s' % dir_path)
                continue

            self.__print_pending_change(module_name)
Ejemplo n.º 28
0
        async def x(index):
            log = Log(f"{from_}=>{to_}#{index}")

            retries = retries_

            while True:
                found = False

                async for item in db.choose(M, {FIELD_NAME: from_}, {FIELD_NAME: to_}, limit_=limit):
                    log.info(item=item)
                    processed[(from_, to_)] += 1
                    await asyncio.sleep(sleep_time)
                    found = True

                if found:
                    break

                if retries == 0:
                    return

                retries -= 1

                log.warning(retry=retries_ - retries)

                await asyncio.sleep(sleep_time * sleep_coef)
            log.important("STOP")
Ejemplo n.º 29
0
class RedirectServer:
    def __init__(self):
        self.log = Log("RedirectServer")

        self.app: web.Application = None
        self.runner: web.AppRunner = None
        self.site: web.TCPSite = None
        self.template: str = None

        self.data = None
        self.data_received = Event()

        self.address = "localhost"
        self.port = randint(8081, 9000)

    @property
    def redirect_address(self):
        return f"{self.address}:{self.port}/redirect"

    async def warm_up(self):
        async with aiofiles.open("vk_utils/redirect_page.html", mode='rt') as f:
            self.template = await f.read()

        self.app = web.Application()
        self.app.add_routes([
            web.get('/redirect', self._handler_get),
            web.post('/redirect', self._handler_post)
        ])

        self.runner = web.AppRunner(self.app)
        await self.runner.setup()
        self.site = web.TCPSite(self.runner, self.address, self.port)
        self.log.info("Start redirect server")
        await self.site.start()

    async def _handler_get(self, request: BaseRequest):
        return web.Response(body=self.template, content_type="text/html")

    async def _handler_post(self, request: BaseRequest):
        path = (await request.json())['answer']
        self.data = dict(item.split('=') for item in path.split("&"))
        self.data_received.set()
        return web.json_response({'status': 'ok'})

    async def __call__(self):
        await self.data_received.wait()
        self.log.info("Data received!")
        return self.data

    async def __aenter__(self):
        await self.warm_up()
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        self.log.info("Stopping WebServer")
        await self.site.stop()
        await self.runner.cleanup()
        await self.app.cleanup()
        self.log.info("WebServer stopped")
Ejemplo n.º 30
0
    def testInit(self):

        log = Log(10**8, 10**8, None)
        iteratorIDs = [0, 1]
        log.initIterators(iteratorIDs, [])

        self.assertEqual(list(log.ptrs.keys()), [0, 1],
                         "Iterator IDs do not match within log.ptrs")
        self.assertEqual(log.ptrs[0][0].value, 0,
                         "log.first is not None from log.ptrs[0][0]")
        self.assertEqual(log.ptrs[1][0].value, 0,
                         "log.first is not None from log.ptrs[1][0]")
        self.assertEqual(log.ptrs[0][1], 0,
                         "log.ref_point is not 0 from log.ptrs[0][0]")
        self.assertEqual(log.ptrs[1][1], 0,
                         "log.ref_point is not 0 from log.ptrs[1][0]")
Ejemplo n.º 31
0
    async def spectate(self, ctx):
        await self.CheckRequirements(ctx.author)
        if ModifierCheck("SpectatorsAllowed",
                         self.tournament.modifiers) is False:
            raise TournamentJoinException(
                "spectators are not allowed for this tournament!")

        if ctx.author in self.tournament.spectators:
            raise TournamentJoinException("you are already a spectator!")

        spec_count = len(self.tournament.spectators)
        join_msg = f"{Emote.join} {ctx.author.mention} joined the spectators. **{str(spec_count + 1)}** spectators are now watching the tournament."
        if spec_count == 0:  # First join, make singular
            i = join_msg[-35:].index("s are") + len(join_msg) - 35
            join_msg = join_msg[:i] + " is" + join_msg[i + 5:]

        await ctx.author.add_roles(self.roles.spectator)
        await self.channels.t_chat.send(join_msg)
        self.tournament.spectators.append(ctx.author)
        Log("Spectator Joined",
            description=
            f"{ctx.author.mention} joined **{self.tournament.name}**.",
            color=Color.dark_green(),
            fields=[{
                'name': "Spectator Count",
                'value': str(spec_count + 1)
            }])

        embed = UpdatedEmbed(self.tournament)
        await self.tournament.msg.edit(embed=embed)
Ejemplo n.º 32
0
    async def edit(self, ctx, modifier, *, value):
        i = ModifierCheck(modifier, self.tournament.modifiers)

        if i is False:
            raise commands.BadArgument(f"`{modifier}` is not active.")

        modifier = self.tournament.modifiers[i]
        if isinstance(modifier, str):
            raise commands.BadArgument(
                f"The value for `{modifier}` cannot be edited.")

        old_value = modifier['value']
        j = self.modifiers[ModifierCheck(modifier['name'], self.modifiers)]
        modifier['value'] = await ModifierUtils.convert_value(
            ctx, j['value'], value)

        if old_value == modifier['value']:
            await ctx.send("Nothing changed. Silly!")
            return

        await ctx.send(
            f"{Emote.check} The value for `{modifier['name']}` was changed to **{modifier['value']}**."
        )
        fields = [{
            'name': "Old Value",
            'value': old_value
        }, {
            'name': "New Value",
            'value': modifier['value']
        }]
        Log("Modifier Edited",
            description=
            f"{ctx.author.mention} changed the value of `{modifier['name']}` for **{self.tournament.name}**.",
            color=Color.dark_teal(),
            fields=fields)
Ejemplo n.º 33
0
    async def begin(self, ctx):
        if self.tournament.status != Status.Opened:
            raise commands.BadArgument(
                "The tournament cannot be closed right now.")
        if len(self.tournament.get_participants()) < 1:
            raise commands.BadArgument(
                "There are no participants in the tournament.")

        no_ign = ""
        for player in self.tournament.get_participants():
            user = User.fetch_by_id(ctx, player.id)
            if user.ign is None: no_ign += f"**{player.name}**\n"

        if no_ign != "":
            raise commands.BadArgument(
                "Some players do not have an IGN set: \n" + no_ign)

        await ctx.send(
            f"{Emote.check} The tournament has been closed. Players can no longer join!"
        )
        self.tournament.status = Status.Closed
        Log("Tournament Closed",
            description=
            f"{ctx.author.mention} closed **{self.tournament.name}**.",
            color=Color.dark_orange())

        await self.tournament.msg.clear_reactions()
        embed = UpdatedEmbed(self.tournament)
        await self.tournament.msg.edit(embed=embed)
Ejemplo n.º 34
0
    async def ign(self, ctx, ign=None):

        if ign is None:
            await ctx.send(
                "Please provide your Werewolf Online in-game name: `;ign example_name`"
            )
            return

        user = User.fetch_by_id(ctx, ctx.author.id)
        old_ign = user.ign

        user.ign = ign
        self.client.get_cog("TOrganizer").cache_ign(ctx.author, ign)

        await ctx.send(
            f"{Emote.check} {ctx.author.mention}, your Werewolf Online IGN has been set to `{ign}`."
        )

        Log("IGN Set",
            description=f"{ctx.author.mention} has set their IGN to `{ign}`.",
            color=0x6a0dad,
            fields=[{
                'name': 'Previous IGN',
                'value': old_ign
            }])
Ejemplo n.º 35
0
def LoadConfig(file_name: str = None) -> "Config":
    log = Log("LoadConfig")

    file_name = file_name or "private.yaml"

    log.info("Load config", source=file_name)

    with open(file_name, "rt") as f:
        data = yaml.safe_load(f)

    def do_update():
        log.info("Update config file", source=file_name)
        with open(file_name, "wt") as f:
            yaml.safe_dump(data, f)

    return Config(data, do_update, "", source=file_name)
Ejemplo n.º 36
0
 def __init__(self, client: motor.motor_asyncio.AsyncIOMotorClient,
              db_name: str):
     self.log = Log(self.__class__.__name__)
     self.client = client
     self.db_name = db_name
     self.db = self.client[self.db_name]
     self._collections = {}
Ejemplo n.º 37
0
    async def rw(self, ctx, users):
        if self.tournament.status != 4:
            raise commands.BadArgument(
                "The tournament must have begun in order to add winners.")

        u_list = users.split(",")

        for user in u_list:
            user = await self._parse_player(ctx, user)

            if user not in self.tournament.participants and user != self.tournament.host:
                raise commands.BadArgument(
                    f"**{user.name}** is not in the tournament.")

            if user not in self.tournament.winners:
                raise commands.BadArgument(f"**{user.name}** is not a winner.")

            self.tournament.winners.remove(user)
            await ctx.send(
                f":heavy_minus_sign: Removed **{user.name}** from the winners."
            )

            Log("Winner Removed",
                description=
                f"{ctx.author.mention} removed {user.mention} from the winners of **{self.tournament.name}**.",
                color=Color.from_rgb(200, 200, 0))
def install_module(module_name):
    """Install a new module.

    :param module_name: module name
    :return: is sucess
    """
    module_path = os.path.join(MODULES_PATH, module_name)
    if os.path.exists(module_path):
        return False

    infos = get_module_infos(module_name)
    if not infos:
        Log.error('Module "%s" not found' % module_name)
        return False

    # install module
    zip_file = BytesIO(requests.get(infos["zip_url"]).content)
    with ZipFile(zip_file) as z:
        # security + locate path files
        content_dir = ""
        for member in z.namelist():
            if member.startswith("/") or "./" in member:
                msg = 'Security threat on "%s" module install (%s)' % (module_name, member)
                Log.critical(msg)
                EventManager.fire(EventManager.Event("security_thread", __package__, msg=msg))
                return False

            if "Module.py" in member:
                content_dir = member.replace("Module.py", "")

        # unzip
        os.makedirs(module_path)
        for member in z.namelist():
            # skip directories
            if not os.path.basename(member):
                continue

            # unzip
            path = os.path.join(module_path, member.replace(content_dir, ""))
            with z.open(member) as s, open(path, "wb") as t:
                shutil.copyfileobj(s, t)

    # starting module
    ModuleManager.reindex_modules()
    ModuleManager.init(module_name)
    ModuleManager.start(module_name)
    return True
Ejemplo n.º 39
0
async def on_command_error(ctx: commands.Context, error):
    if isinstance(error, commands.errors.BadArgument):  # Bad Argument Raised
        e = str(error).replace('"', '`')
        await ctx.send(f"{Emote.deny} {e}")
        return

    # Missing Required Argument Raised
    if isinstance(error, commands.errors.MissingRequiredArgument):
        await ctx.send(
            f"{Emote.deny} **Missing Required Argument** \nThis command requires more arguments. Try again by "
            f"providing a `{error.param}` parameter this time!")
        return

    if isinstance(error, commands.errors.NotOwner):
        print("Someone tried to use an owner command!")
        return

    if isinstance(error, TournamentJoinException):
        message = f"{Emote.deny} {ctx.author.mention}, {str(error)}"
        await ctx.send(message, delete_after=10)
        Log("Tournament Join Failed",
            color=discord.Color.dark_red(),
            fields=[{'name': "Denial Message", 'value': message}])
        return

    if isinstance(error, commands.errors.CommandNotFound): return

    if isinstance(error, MissingPermissions):

        if ctx.command.qualified_name == "setign":
            await ctx.send("I think the command you are looking for is `;ign`. Try it!")

        return

    if isinstance(error, InvalidChannel):
        await ctx.send(f"You cannot use this command here. Move to {Channel(client).bot_cmds.mention} and try again!")
        return

    if isinstance(error, OperationalError) and error.args[0] == "database is locked":
        await ctx.send("Database is locked. Attempting to fix this, type your command again to retry...")
        client.get_cog("Debug").unlock_db()
        return

    # None of previously mentioned errors
    embed = discord.Embed(title="An error occured!",
                          description="An unexecpted problem was encountered! The issue has automatically been "
                                      "reported. Sorry for the incovenience!",
                          color=discord.Color.red())
    await ctx.send(embed=embed)

    try:
        await send_traceback(ctx, error)
    except Exception as e:
        await Channel(client).error_logs.send(
            "Unable to send to send full traceback: "
            f"\nCause: `{str(type(e))}: {str(e)}`"
            f"\nMinimal Traceback: `{str(type(error))}: {str(error)}`")
    finally:
        traceback.print_exception(type(error), error, error.__traceback__)
Ejemplo n.º 40
0
    def __init__(self):
        work_name = self.__class__.__name__
        self.log = Log(work_name)

        self.log.debug("Register work")
        self.work_ids.setdefault(work_name, -1)
        self.work_ids[work_name] += 1
        work_id = f"{work_name}_{self.work_ids[work_name]}"
        self.log.debug("Work registered", work_id=work_id)

        self.stat = Stats(work_id, work_name)

        self.log.debug("Run task manager")
        self.task_manager = TasksManager(self.PARALLEL)
        self.tasks: List[TaskInfo] = []

        self.state = "Base class initialized"
Ejemplo n.º 41
0
    def started(self, component):
        self.load_configuration()
        options = {}

        try:
            self.__node = Node(
                port=self.__port,
                server_ip=self.__network,
                **options
            ).register(self)

        except socket.error as e:
            Log.error('socket server error: %s' % e)
            self.__node = Node(**options).register(self)

        self.load_peer()
        self.save_configuration()
Ejemplo n.º 42
0
    async def tplan(self, ctx):
        if self.tournament.status != Status.Pending:
            raise commands.BadArgument(
                f"The tournament cannot be scheduled at the moment.")

        req = ['name', 'host', 'time']
        missing = []
        for attr in req:
            if getattr(self.tournament, attr) is None:
                missing.append("`" + attr + "`")
        if missing:
            items = " and ".join(item for item in missing)
            raise commands.BadArgument(
                f"You have not specified a {items} for the tournament.")

        embed = Embed(title=self.tournament.name, color=Color.orange())
        embed.set_author(name="Tournament Scheduled")

        for attr in self.attr:
            val = getattr(self.tournament, attr)
            if attr == 'time':
                val = val.strftime("%A %d %b at %I:%M%p *GMT*")
            if attr == 'host':
                val = val.mention
            if attr not in ['name', 'roles'] and val is not None:
                embed.add_field(name=attr.title(), value=val)
        embed.set_footer(
            text=
            f"This tournament will take place in {TimeUntil(self.tournament.time)}!"
        )

        await ctx.message.add_reaction(Emote.check)
        await self.channels.t_planned.send(
            f"New tournament scheduled! {self.roles.schedule.mention}",
            embed=embed)

        # Add stuff and Log
        Log("Tournament Planned",
            description=
            f"{ctx.author.mention} scheduled **{self.tournament.name}**.",
            color=Color.orange(),
            fields=[{
                "name": "Host",
                "value": self.tournament.host
            }, {
                "name": "Time",
                "value": self.tournament.time
            }])
        self.tournament.status = Status.Scheduled
        self.tournament.save()
        await ctx.send("Tournament ID: " + str(self.tournament.id))
        if self.tournament not in self.queue:
            self.queue.append(self.tournament)
        self.tournament = Tournament()
Ejemplo n.º 43
0
    def choice_config(self):
        if len(self.__configs) == 1:
            return self.__configs[0]

        if not len(self.__configs):
            Log.error('Not config file')
            return {}

        print('')

        while True:
            for id, config in enumerate(self.__configs):
                print('    %d) %s: %s' % 
                    (id+1, config['name'], config['description']))

            id = int(input('Your choice ID? '))

            if id and id > 0 and id <= len(self.__configs):
                return self.__configs[id-1]

            print('Incorrect choice ID')
Ejemplo n.º 44
0
    def choice_config(self):
        if len(self.__configs) == 1:
            return self.__configs[0]

        if not len(self.__configs):
            Log.error('Not config file')
            return {}

        print('')

        while True:
            for id, config in enumerate(self.__configs):
                print('    %d) %s: %s' %
                      (id + 1, config['name'], config['description']))

            id = int(input('Your choice ID? '))

            if id and id > 0 and id <= len(self.__configs):
                return self.__configs[id - 1]

            print('Incorrect choice ID')
Ejemplo n.º 45
0
    def load_peer(self):
        conf_path = '%s/configs/peer/' % \
                    os.path.dirname(os.path.abspath(__file__))
        nb = 0

        if os.path.isdir(conf_path):
            for name in os.listdir(conf_path):
                if not os.path.exists(conf_path + name) \
                        or name[0] == '.':
                    continue

                with open(conf_path + name) as config_file:
                    config = json.load(config_file)

                    if 'name' not in config:
                        config['name'] = name

                    self.add_peer(**config)
                    nb += 1

        Log.debug('%d peer loaded' % nb)
def start(module_name):
    """Start module."""
    if is_disabled(module_name):
        return False

    if module_name not in __modules_data or __modules_data[module_name]['instance'] is None:
        return False

    __modules_data[module_name]['instance'].started()
    __modules_data[module_name]['instance'].is_running = True
    __modules_data[module_name]['thread'] = threading.Thread(
        None,
        __modules_data[module_name]['instance'].run,
        'module: %s' % module_name
    )
    __modules_data[module_name]['thread'].setDaemon(True)
    __modules_data[module_name]['thread'].start()

    __active_modules_name.append(module_name)

    Log.info('start "%s" module' % module_name)
    return True
Ejemplo n.º 47
0
    async def treset(self, ctx):
        if self.tournament.status in (2, 3, 4):
            raise commands.BadArgument(
                f"The tournament is ongoing and cannot be reset. Please try to use `;cancel` instead."
            )

        self.tournament = Tournament()
        await ctx.send(
            f"{Emote.check} The tournament has succesfully been reset.")

        Log("Tournament Reset",
            description=
            f"{ctx.author.mention} has reset the tournament to default values.",
            color=Color.dark_red())
Ejemplo n.º 48
0
    async def join(self, ctx):
        await self.CheckRequirements(ctx.author)

        player_count = len(self.tournament.get_participants())
        mod_index = ModifierCheck("MaxParticipants", self.tournament.modifiers)

        if mod_index is not False:
            max_count = self.tournament.modifiers[mod_index]['value']
        else:
            max_count = 15  # Default
        if player_count >= max_count:
            raise TournamentJoinException(
                f"the tournament is full! The maximum number of participants is **{str(max_count)}**."
            )

        join_msg = f"{Emote.join} {ctx.author.mention} joined. **{str(player_count + 1)}** players are now ready."

        if player_count == 0:  # First join, make singular
            i = join_msg[-35:].index("s are") + len(join_msg) - 35
            join_msg = join_msg[:i] + " is" + join_msg[i + 5:]

        user = User.fetch_by_id(ctx, ctx.author.id)

        if user.participations == 0:
            join_msg += "\nThis is the first tournament they are participating in. Welcome! 🎉"
            await SendFirstTournamentMessage(ctx)
        if user.ign is None:
            join_msg += "\nThis player does not have an IGN set yet."
        else:
            join_msg += f"\nIGN: `{user.ign}`"

        ign_cache[ctx.author.id] = user.ign
        await ctx.author.add_roles(self.roles.participant)
        await ctx.author.remove_roles(self.roles.spectator)
        await self.channels.t_chat.send(join_msg)
        Log("Participant Joined",
            description=
            f"{ctx.author.mention} joined **{self.tournament.name}**.",
            color=Color.dark_green(),
            fields=[{
                'name': "Player Count",
                'value': str(player_count + 1)
            }])
        self.tournament.add_participant(ctx.author)
        if ctx.author in self.tournament.spectators:
            self.tournament.spectators.remove(ctx.author)

        embed = UpdatedEmbed(self.tournament)
        await self.tournament.msg.edit(embed=embed)
        await self.checklist.update_text(ctx, self.tournament)
Ejemplo n.º 49
0
    async def cancel(self, ctx):
        if self.tournament.status not in (1, 3, 4):
            raise commands.BadArgument("The tournament has not started.")

        await ctx.send(
            f"Are you sure you want to cancel **{self.tournament.name}**? `yes`/`no`"
        )

        def check(m):
            return m.author == ctx.message.author and m.channel == ctx.message.channel

        try:
            msg = await self.client.wait_for('message',
                                             check=check,
                                             timeout=30)
        except asyncio.TimeoutError:
            raise commands.BadArgument("Command cancelled.")

        if msg.content.lower() == "no":
            raise commands.BadArgument(
                "Alright, the tournament will not be cancelled.")
        if msg.content.lower() != "yes":
            raise commands.BadArgument(
                "Invalid choice. Please type the command again to retry.")

        cancel_msg = await ctx.send(":flag_white: Cancelling...")

        try:
            self.queue.remove(self.tournament)
        except ValueError:
            pass

        if self.tournament.status in (3, 4):
            await self.cleanup()
            await self.client.get_command("nto").__call__(ctx)
        else:
            await self.update_reminders()

        try:
            await cancel_msg.edit(content=":flag_white: Tournament cancelled.")
        except HTTPException:
            pass

        Log("Tournament Cancelled",
            description=
            f"{ctx.author.mention} cancelled **{self.tournament.name}**.",
            color=Color.from_rgb(40, 40, 40))
        self.tournament.status = Status.Cancelled
        self.tournament.save()
        self.tournament = Tournament()
Ejemplo n.º 50
0
    def testAddEntry(self):

        log = Log(10**8, 10**8, None)
        iteratorIDs = [0, 1]
        log.initIterators(iteratorIDs, [])

        func = print
        args = [1, 2]
        # params = [0, 1, -1, 1, 0, 0]
        params = [0, 1, 2, 3, 4, 5]
        shm_name, entry_size = log.createLogEntry(func, args, params)
        reference = {}
        reference['func'], reference['args'] = func, args
        reference['logIndex'], reference['currentTerm'], reference[
            'prevLogIndex'] = params[0], params[1], params[2]
        reference['prevLogTerm'], reference['commitIndex'], reference[
            'leaderID'] = params[3], params[4], params[5]
        reference['shm_name'], reference['shm_size'], reference[
            'isCommitted'] = shm_name, entry_size, False
        params_extended = params + [entry_size, False]
        log.addEntry(shm_name, params_extended)

        self.assertEqual(len(log), 2, "len(log) should be 2")
        self.assertEqual(log.current(0), 0, "log.first should be 0 by default")
        next_entry = log.next(0, getNode=True)
        self.assertEqualEntry(log, next_entry, reference)
        self.assertEqualEntry(log, log.current(0, getNode=True), reference)
        self.assertEqual(log.ptrs[0][1], 1, 'index should be 1')
        self.assertEqual(len(log.nodeSizes), 2,
                         'log.nodeSizes should have 2 entries')
        self.assertEqual(log.nodeSizes[1], reference['shm_size'],
                         'shm_size does not match for reference')

        shm = multiprocessing.shared_memory.SharedMemory(
            name=next_entry.value[0])
        self.assertEqualEntryBuffer(log, shm.buf, reference)
        utils.freeSharedMemory(shm)
Ejemplo n.º 51
0
    def __init__(self, max_size: int):
        # TODO: Less logs
        self.log = Log("TasksManager")
        self.size = max_size

        self._tasks = asyncio.Queue(self.size)

        self._results = asyncio.Queue(self.size)
        self._exceptions = asyncio.Queue(self.size)

        self.is_run = True

        self.executors = [
            asyncio.create_task(self._executor(i)) for i in range(max_size)
        ]
def call(module_name, method_name, *arg, c_optional_call=False, **kwargs):
    """Call module method."""
    if module_name not in __modules_data:
        if not c_optional_call:
            Log.error('Module "%s" not found' % module_name)
        return False

    if __modules_data[module_name]['instance'] is None:
        if not c_optional_call:
            Log.error('Module "%s" is not instantiated' % module_name)
        return False

    if not hasattr(__modules_data[module_name]['instance'], method_name):
        if not c_optional_call:
            Log.error('Module "%s" has not a "%s" method' % (module_name, method_name))
        return False

    return getattr(__modules_data[module_name]['instance'], method_name)(*arg, **kwargs)
Ejemplo n.º 53
0
 def __client_peer_disconnect(self, connection_name, hostname, port,
                              client_channel, client_obj):
     Log.info('Disconnected to %s (%s:%d)' % (
         connection_name, hostname, port
     ))
Ejemplo n.º 54
0
 def __server_ready(self, server, bind):
     Log.info('Start MoLa network : %s:%d' % bind)
Ejemplo n.º 55
0
 def __server_peer_disconnect(self, sock):
     Log.info('Peer disconect : %s' % str(sock))
Ejemplo n.º 56
0
 def __server_peer_connect(self, sock, host, port):
     Log.info('Peer connect : %s' % host)
 def add_warning(self, text, *args, **kwargs):
     """Add a warning message to logger"""
     text = '%s %s: %s' % (self.name_prefix, self.name, text)
     Log.warning(text, *args, **kwargs)
 def add_info(self, text, *args, **kwargs):
     """Add a message to logger"""
     text = '%s %s: %s' % (self.name_prefix, self.name, text)
     Log.info(text, *args, **kwargs)
 def add_error(self, text, *args, **kwargs):
     """Add a error message to logger"""
     text = '%s %s: %s' % (self.name_prefix, self.name, text)
     Log.error(text, *args, **kwargs)
 def add_debug(self, text, *args, **kwargs):
     """Add a debug message to logger"""
     text = '%s %s: %s' % (self.name_prefix, self.name, text)
     Log.debug(text, *args, **kwargs)