コード例 #1
0
ファイル: main.py プロジェクト: HactarCE/Discord-Bot-Template
    async def load_all_extensions(self, reload=False):
        """Attempt to load all .py files in cogs/ as cog extensions.

        Return a dictionary which maps cog names to a boolean value (True =
        successfully loaded; False = not successfully loaded).
        """
        succeeded = {}
        disabled_extensions = set()
        if not info.DEV:
            disabled_extensions.add('tests')
        for extension in get_extensions(disabled=disabled_extensions):
            try:
                if reload or extension not in self.cogs_loaded:
                    self.load_extension(f'cogs.{extension}')
                    l.info(f"Loaded extension '{extension}'")
                    self.cogs_loaded.add(extension)
                    succeeded[extension] = True
            except Exception as exc:
                l.error(
                    f"Failed to load extension {extension!r} due to {type(exc).__name__}: {exc}"
                )
                if hasattr(exc, 'original'):
                    l.error(
                        f"More details: {type(exc.original).__name__}: {exc.original}"
                    )
                succeeded[extension] = False
        if succeeded:
            l.info(LOG_SEP)
        return succeeded
コード例 #2
0
ファイル: network.py プロジェクト: jakubfi/cbpx
    def run(self):
        l.debug("Running listener")
        while True:
            l.debug("Awaiting new connection")

            try:
                # wait for new connection
                (n_sock, n_addr) = self.sock.accept()
            except Exception, e:
                l.error("Error accepting connection: " + str(e))
                break

            l.debug("New connection from: %s" % str(n_addr))

            # if there are more queued connections than allowed
            qc = conn_q.qsize()
            if qc >= int(params.max_queued_conns):
                l.warning("Queued %i connections, limit is %i" % (qc, int(params.max_queued_conns)))
                # if we were switching, than sorry, but not anymore
                l.info("Enabling relaying")
                relay.set("connection limit reached")

            try:
                conn_q.put([n_sock, n_addr], False)
                cbpx_stats.c_qc += 1
                l.debug("Enqueued connection: %s" % str(n_addr))

            except Full:
                l.error("Queue is full with %i elements!" % qc)
                l.info("Enabling relaying")
                relay.set("connection queue full")

            except Exception, e:
                l.warning("Exception during connection enqueue: %s" % str(e))
コード例 #3
0
ファイル: network.py プロジェクト: jakubfi/cbpx
    def process_connection(self, n_sock, n_addr):
        l.debug("Processing connection from: %s" % str(n_addr))

        try:
            fwd_sock = socket(AF_INET, SOCK_STREAM)
            fwd_sock.connect(relay.get_active())
        except IOError, (errno, strerror):
            l.error("Error extablishing connection to backend: I/O error(%i): %s" % (errno, strerror))
            n_sock.close()
            return
コード例 #4
0
async def on_error(event_method, *args, **kwargs):
    _, exc, _ = sys.exc_info()
    l.error(
        f"'{str(exc)}' encountered during '{event_method}' (args: {args}; kwargs: {kwargs})"
    )
    await report_error(None,
                       exc,
                       *args,
                       bot=bot,
                       event_method=event_method,
                       **kwargs)
コード例 #5
0
ファイル: odl.py プロジェクト: JNDOFFICIAL/ip-rotator
	def delete_flow(self,**kwargs):
		api_url=("%s/controller/nb/v2/flowprogrammer/default/node/OF/%s/staticFlow/%s" %
			(params.odl_server, kwargs['node']['id'], kwargs['name']))

		self.__auth(api_url)

		request = urllib2.Request(api_url)
		request.get_method = lambda: 'DELETE'
		connection = urllib2.urlopen(request)

		if connection.code == 204:
			l.info("Flow has been deleted, name: %s" % kwargs['name'])
		else:
			l.error("Error during deleting flow entry, name: %s, http error: %s" % (kwargs['name'], connection.code))
コード例 #6
0
ファイル: main.py プロジェクト: LyricLy/Esobot
async def on_command_error(ctx, exc):
    if isinstance(exc, commands.CommandInvokeError) and isinstance(
            exc.original, ShowErrorException):
        return

    command_name = ctx.command.qualified_name if ctx.command else "unknown command"
    if isinstance(exc, commands.UserInputError):
        if isinstance(exc, commands.MissingRequiredArgument):
            description = f"Missing required argument `{exc.param.name}`."
        elif isinstance(exc,
                        (commands.BadArgument, commands.BadUnionArgument)):
            description = f"Invalid argument. {str(exc)}"
        else:
            description = f"Unknown user input exception."
        description += f"\n\nRun `{COMMAND_PREFIX}help {command_name}` to view the required arguments."
    elif isinstance(exc, commands.CommandNotFound):
        # description = f"Could not find command `{ctx.invoked_with.split()[0]}`."
        return
    elif isinstance(exc, commands.CheckFailure):
        if isinstance(exc, commands.NoPrivateMessage):
            description = "Cannot be used in a DM."
        elif isinstance(exc, commands.MissingPermissions):
            description = "You don't have permission to do that. "
            missing_perms = ", ".join(exc.missing_permissions)
            description += f"Missing {missing_perms}"
        elif isinstance(exc, commands.NotOwner):
            description = "You have to be the bot owner to do that."
        elif isinstance(exc, commands.MissingRole):
            description = f"You're missing the required role '{exc.missing_role}'."
        elif isinstance(exc, commands.PrivateMessageOnly):
            description = "This command has to be used in a DM."
        else:
            description = "Command check failed. For one reason or another, you're not allowed to run that command in this context."
    elif isinstance(exc, commands.DisabledCommand):
        description = "That command is disabled."
    elif isinstance(exc, commands.CommandOnCooldown):
        description = f"That command is on cooldown. Try again in {exc.retry_after:.1f} seconds."
    elif isinstance(exc, commands.MaxConcurrencyReached):
        description = "This command is currently already in use. Please try again later."
    else:
        description = "Sorry, something went wrong."
        l.error(
            f"Unknown error encountered while executing '{command_name}'\n" +
            "".join(
                traceback.format_exception(type(exc), exc, exc.__traceback__)))
    await ctx.send(embed=make_embed(
        color=colors.EMBED_ERROR, title="Error", description=description))
コード例 #7
0
ファイル: network.py プロジェクト: jakubfi/cbpx
    def run(self):

        rd = []

        l.debug("Running transporter loop")
        while not self.quit:

            # wait for events on all tracked fds
            try:
                rd = self.poller.poll(0.2)
            except Exception, e:
                l.warning("Exception while poll(): %s" % str(e))

            # iterate over all events returned by epoll():
            for f, event in rd:

                # if wata is waiting to be read
                if event & self.DATA_READY:

                    # read the data
                    data = ""
                    try:
                        data = self.fd[f][0].recv(int(params.net_buffer_size))
                    except Exception, e:
                        l.warning("Exception %s while reading data: %s" % (type(e), str(e)))
                        self.dead.add(f)
                        self.dead.add(self.fd[f][2])
                        continue

                    # no data means connection closed
                    if not data:
                        self.dead.add(f)
                        self.dead.add(self.fd[f][2])
                        continue
                    else:
                        # pass the data to the other end
                        try:
                            # TODO: retransmission should be handled better
                            sent = self.fd[f][1].send(data)
                            if sent != len(data):
                                l.error("APOCALYPSE! Transmitted only %i bytes of received %i bytes" % (sent, len(data)))
                        except Exception, e:
                            l.warning("Exception %s while transmitting data: %s" % (type(e), str(e)))
                            self.dead.add(self.fd[f][2])
                            self.dead.add(f)
                            continue
コード例 #8
0
async def on_command_error(ctx, exc, *args, **kwargs):
    if isinstance(exc, commands.CommandInvokeError) and isinstance(
            exc.original, ShowErrorException):
        return

    command_name = ctx.command.name if ctx.command else "unknown command"
    l.error(
        f"'{str(exc)}' encountered while executing '{command_name}' (args: {args}; kwargs: {kwargs})"
    )
    if isinstance(exc, commands.UserInputError):
        if isinstance(exc, commands.MissingRequiredArgument):
            description = f"Missing required argument `{exc.param.name}`."
        elif isinstance(exc, commands.TooManyArguments):
            description = "Too many arguments."
        elif isinstance(exc, commands.BadArgument):
            description = f"Bad argument:\n```\n{str(exc)}\n```"
        else:
            description = f"Bad user input."
        description += f"\n\nRun `{COMMAND_PREFIX}help {command_name}` to view the required arguments."
    elif isinstance(exc, commands.CommandNotFound):
        # description = f"Could not find command `{ctx.invoked_with.split()[0]}`."
        return
    elif isinstance(exc, commands.CheckFailure):
        if isinstance(exc, commands.NoPrivateMessage):
            description = "Cannot be run in a private message channel."
        elif isinstance(exc, commands.MissingPermissions) or isinstance(
                exc, commands.BotMissingPermissions):
            if isinstance(exc, commands.MissingPermissions):
                description = "You don't have permission to do that."
            elif isinstance(exc, commands.BotMissingPermissions):
                description = "I don't have permission to do that."
            missing_perms = "\n".join(exc.missing_perms)
            description += f" Missing:\n```\n{missing_perms}\n```"
        else:
            description = "Command check failed."
    elif isinstance(exc, commands.DisabledCommand):
        description = "That command is disabled."
    elif isinstance(exc, commands.CommandOnCooldown):
        description = "That command is on cooldown."
    else:
        description = "Sorry, something went wrong.\n\nA team of highly trained monkeys has been dispatched to deal with the situation."
        await report_error(ctx, exc.original, *args, **kwargs)
    await ctx.send(embed=make_embed(
        color=colors.EMBED_ERROR, title="Error", description=description))
コード例 #9
0
ファイル: odl.py プロジェクト: JNDOFFICIAL/ip-rotator
	def add_flow(self, **kwargs):
		api_url=("%s/controller/nb/v2/flowprogrammer/default/node/OF/%s/staticFlow/%s" %
			(params.odl_server, kwargs['node']['id'], kwargs['name']))

		self.__auth(api_url)

		request = urllib2.Request(api_url,data=json.dumps(kwargs))
		request.add_header('Content-Type', 'application/json')
		request.get_method = lambda: 'PUT'
		connection = urllib2.urlopen(request)
		
		l.debug("Flow:")
		for k,v in kwargs.items():
			l.debug("%s: %s" %(k,v))

		if connection.code != 200 and connection.code != 201:
			l.error("Error during adding flow, HTTP response: %s" % connection.code)
			raise Exception("Error during adding flow, HTTP response: %s" % connection.code)			

		return connection.code
コード例 #10
0
ファイル: main.py プロジェクト: Quonauts/Quobot
 async def load_all_extensions(self, reload=False):
     """
     Attempts to load all .py files in cogs/ as cog extensions. Returns a
     dictionary which maps cog names to a boolean value (True = successfully
     loaded; False = not successfully loaded).
     """
     succeeded = {}
     for extension in get_extensions():
         try:
             if reload or extension not in self.cogs_loaded:
                 self.load_extension(f'cogs.{extension}')
                 l.info(f"Loaded extension '{extension}'")
                 self.cogs_loaded.add(extension)
                 succeeded[extension] = True
         except Exception as e:
             error = f"{extension}\n {type(e).__name__} : {e}"
             l.error(f"Failed to load extension '{error}'")
             succeeded[extension] = False
     if succeeded:
         l.info(LOG_SEP)
     return succeeded
コード例 #11
0
ファイル: network.py プロジェクト: jakubfi/cbpx
def switch_finalize():

    if not params.switch_script:
        l.info("No switch finalize script to run, finishing switch immediately")
        relay.switch_backend("all connections closed")
        return

    try:
        global script

        devnull = open(os.devnull, "rw")

        (ai, ap) = relay.get_active()
        (si, sp) = relay.get_standby()

        l.info("Running script: '%s' with arguments: %s %i %s %i" % (params.switch_script, ai, ap, si, sp))

        script = subprocess.Popen([params.switch_script, ai, str(ap), si, str(sp)], stdin=devnull, stdout=devnull, stderr=devnull, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
        r = script.wait()
    except Exception, e:
        l.error("Exception while executing switch finalize script (%s): %s" % (params.switch_script, str(e)))
        relay.set("script exec failed: %s" % str(e))
        return
コード例 #12
0
ファイル: network.py プロジェクト: jakubfi/cbpx
            fwd_sock = socket(AF_INET, SOCK_STREAM)
            fwd_sock.connect(relay.get_active())
        except IOError, (errno, strerror):
            l.error("Error extablishing connection to backend: I/O error(%i): %s" % (errno, strerror))
            n_sock.close()
            return
        except Exception, e:
            l.error("Exception while extablishing connection to backend: " + str(e))
            n_sock.close()
            return

        # spawn proxy threads
        try:
            self.transporter.add(fwd_sock, n_sock)
        except Exception, e:
            l.error("Error spawning connection threads: " + str(e))
            n_sock.close()


    # --------------------------------------------------------------------
    def throttle(self):
        active_conns = cbpx_stats.c_endpoints/2
        throttle_step = 0.01
        l.debug("Throttle?: %i connections (%i limit)" % (active_conns, int(params.max_open_conns)))
        while active_conns >= int(params.max_open_conns):
            time.sleep(throttle_step)
            active_conns = cbpx_stats.c_endpoints/2

    # --------------------------------------------------------------------
    def run(self):
        l.debug("Running connector")
コード例 #13
0
ファイル: main.py プロジェクト: LyricLy/Esobot
async def on_error(event_method, *args, **kwargs):
    l.error(
        f"Error encountered during '{event_method}' (args: {args}; kwargs: {kwargs})\n"
        + traceback.format_exc())