예제 #1
0
파일: bot.py 프로젝트: mc706/SlackBot
 def _dispatcher(self):
     """
     Receive messages and route them to functionality.
     """
     while 1:
         try:
             response = self._incoming_messages.get(block=True)
             self._format_incoming(response)
             now = time.time()
             # match the event to the best command
             for command in self.commands[:]:
                 if command.deadline is not False and command.deadline < now:
                     self.unregister_command(command)  # silently remove him
                     continue
                 match = command.matches(self, response)
                 if match:
                     command(_SlackBotWrapper(self, response), response,
                             *match.groups())
                     if command.activations is not False and command.activations <= 0:
                         self.unregister_command(command)
         except Exception:
             with util.hilite('red'):
                 traceback.print_exc()
                 print(response)
         else:
             try:
                 if u'_logged' not in response:
                     with util.hilite('gray'):
                         print(response)
             except:
                 print("Wow something went REAL wrong.")
예제 #2
0
 def anon(bot, command):
     if len(command) >= 2:
         setting = command[1]
         bot.config[config_id] = setting != '0'
     with util.hilite('gray'):
         print(name, end=": ")
     config_item = bot.config.get(config_id, default)
     with util.hilite('green' if config_item else 'red'):
         print(config_item)
예제 #3
0
def log_presence_change(bot, msg):
    """
    Print to the terminal that a user has changed presence.
    """
    msg[u'_logged'] = True
    with autoflush(bot), util.hilite('gray'):
        print('{} is now '.format(msg[u'user_name']), end='')
        color = {
            u'active': 'green',
            u'away': 'yellow',
        }[msg[u'presence']]
        with util.hilite(color):
            print(msg[u'presence'], end='')
        print(".")
예제 #4
0
def log_new_user(bot, msg):
    msg[u'_logged'] = True
    user = msg[u'user']
    with autoflush(bot):
        with util.hilite('cyan'):
            print(user[u'name'], end=" ")
        print("({}) has joined the team!".format(user[u'real_name']))
예제 #5
0
def log_user_change(bot, msg):
    if msg[u'user'][u'deleted'] is True:
        msg[u'_logged'] = True
        with autoflush(bot):
            with util.hilite('cyan'):
                print(msg[u'name'], end=" ")
            print("has left the team.")
예제 #6
0
def cli_input(bot, msg):
    bot.set_debug_fn(_get_debug_fn(bot))
    current_thread = threading.current_thread()
    _setup_autocompletion(bot)
    command_list = {
        'bot': _bot,
        'channel': _channel,
        'show_typing': _config_boolean('show_typing', "Show typing", False),
        'terminal_ping': _config_boolean('terminal_ping', "Terminal ping",
                                         True),
    }
    while 1:
        try:
            message = raw_input(get_cli_prefix(bot))
            print("\033[A\033[K", end='\r')  # Clear the raw_input line.
            if current_thread.stopped():
                return
            if message[:1] == "/":
                command = message.split()
                command_list.get(command[0][1:], _unknown_command)(bot,
                                                                   command)
            else:
                bot.say(message, channel=bot.config['send_channel'])
        except:
            with util.hilite('red'):
                print("Error on output:")
                traceback.print_exc()
예제 #7
0
def log_starred(bot, msg):
    item = msg[u'item']
    if item[u'type'] in ['message', 'channel', 'file', 'im']:
        with autoflush(bot):
            msg[u'_logged'] = True
            with util.hilite('yellow'):
                print(msg[u'user_name'], end=" starred ")
            _print_star_info(bot, msg)
예제 #8
0
def _channel(bot, command):
    if len(command) == 1:
        print("Currently sending to", end=' ')
    else:
        bot.config['send_channel'] = command[1]
        print("Set channel to", end=' ')
    with util.hilite('cyan'):
        print(bot.config['send_channel'], end='')
    print(".")
예제 #9
0
def log_starred(bot, msg):
    item = msg[u'item']
    if item[u'type'] == 'message':
        msg[u'_logged'] = True
        message = item[u'message']
        with util.hilite('yellow'):
            print(msg[u'user_name'], end=" starred ")
        _log_message(
            bot,
            bot.get_channel_name(item[u'channel']),
            bot.get_nick(message[u'user']),
            message[u'text'],
        )
    elif item[u'type'] == 'channel':
        msg[u'_logged'] = True
        channel = item[u'channel']
        with util.hilite('yellow'):
            print(msg[u'user_name'], end=" starred ")
        with util.hilite('purple'):
            print(bot.get_channel_name(channel))
예제 #10
0
def log_message_delete(bot, msg):
    msg[u'_logged'] = True
    with autoflush(bot):
        with util.hilite('cyan'):
            _log_message(
                bot,
                msg[u'channel_name'],
                "",
                util.hilite_string(
                    'gray',
                    "Deleted message from {}.".format(msg[u'deleted_ts'])),
            )
예제 #11
0
def _print_star_info(bot, msg):
    item = msg[u'item']
    if item[u'type'] == 'message':
        message = item[u'message']
        _log_message(
            bot,
            bot.get_channel_name(item[u'channel']),
            bot.get_nick(message[u'user']),
            message[u'text'],
        )
    elif item[u'type'] in ('channel', 'im'):
        channel = item[u'channel']
        with util.hilite('purple'):
            print(bot.get_channel_name(channel))
    elif item[u'type'] == 'file':
        sent_file = item[u'file']
        message = sent_file[u'permalink_public']
        with util.hilite('purple'):
            print(bot.get_nick(sent_file[u'user']), end=" ")
        with util.hilite('blue'):
            print(message)
예제 #12
0
def log_typing(bot, msg):
    """
    Print to the terminal that a user is typing.

    Note that if the bot config `show_typing` is False
    nothing will be logged.
    """
    msg[u'_logged'] = True
    if bot.config.get('show_typing'):
        with util.hilite('gray'):
            if msg[u'channel_name']:
                print('{} is typing in {}.'.format(msg[u'user_name'], msg[u'channel_name']))
            else:
                print('{} is typing to you.'.format(msg[u'user_name']))
예제 #13
0
def log_message_changed(bot, msg):
    """
    Print to the terminal that a user's message has been edited.

    Note that this does not necessarily mean the user has edited their
    message. Automatic unfurling in particular is handled
    via message_changed.
    """
    msg[u'_logged'] = True
    with util.hilite('gray'):
        print("edited", end=" ")
    message = msg[u'message']
    _log_message(
        bot,
        msg[u'channel_name'],
        bot.get_nick(message[u'user']),
        message[u'text'],
    )
예제 #14
0
def cli_input(bot, msg):
    while 1:
        try:
            message = raw_input()
            if message[:1] == "/":
                command = message.split()
                if command[0] == "/channel":
                    _channel(bot, command)
                elif command[0] == "/show_typing":
                    _show_typing(bot, command)
                elif command[0] == "/bot":
                    _bot(bot, command)
            else:
                bot.say(message, channel=bot.config['send_channel'])
        except:
            with util.hilite('red'):
                print("Error on output:")
                traceback.print_exc()
예제 #15
0
파일: bot.py 프로젝트: mc706/SlackBot
 def load_module(self, name, filename=None):
     """
     Load a module, overwriting the previous module if possible
     Return an Exception, or None if no exception.
     """
     if filename is None:
         filename = self.get_module_path(name)
     cmds = self.commands[:]  # backup the commands (in case of failure)
     module_cmds = None
     if name in modules.register.modules:
         # unload the old module to make room for the new one, but remember it
         # in case something goes wrong when loading the new module.
         module_cmds = self.unload_module(name)
     try:
         # commands are registered automatically
         module = imp.load_source(name, filename)
         # # If you decide to use a setup method uncomment this,
         # # although actually it would probably be better as an
         # # explicit decorator. Ones for 'onload', 'onunload',
         # # stuff like that.
         # if hasattr(module, "setup"):
         #     module.setup()
     except Exception as e:
         with util.hilite('red'):
             traceback.print_exc()
             print("Error loading {}: {} (in bot.py)".format(name, e),
                   file=sys.stderr)
         self.commands = cmds  # replace commands' previous state
         if module_cmds is not None:
             modules.register.load_module(
                 name, module_cmds)  # reload module's previous state
         elif name in modules.register.modules:  # loaded a couple commands
             modules.register.unload_module(name)
         return e
     else:
         commands = modules.register.module(name)
         if commands is not None:
             self.register_commands(commands)
     return None
예제 #16
0
파일: obs.py 프로젝트: vbonvin/POUET
	def compute_observability(self, meteo, cwvalidity=30, cloudscheck=True, verbose=True, displayall=True, future=False):
		"""
		Update the status using :meth:`~obs.Observable.update`. Compute the observability param, a value between 0 and 1 that tells if the target can be observed at a given time. Also define flags for each parameter (moon, wind, etc...)

		The closer to 1 the better
		0 is impossible to observe

		:param meteo: a Meteo object, whose time attribute has been actualized beforehand
		:param cwvalidity: float, current weather validity: time (in minutes) after/before which the allsky cloud coverage and wind are not taken into account in the observability, effectively setting the future variable to True
		:param verbose: boolean, displaying the status of the observable according to the present function
		:param displayall: boolean, if verbose is True, then print also the targets that are not observable.
		:param future: boolean, if set to True then cloud coverage and wind are note taken into account in the observability.
		"""
		if SETTINGS["misc"]["singletargetlogs"] == "True":
			logger.debug("Computing observability for {}...".format(self.name))
			logger.info("Current time is %s" % meteo.time)
		self.update(meteo=meteo)
		observability = 1  # by default, we can observe

		if np.abs(meteo.time - Time.now()).to(u.s).value / 60. > cwvalidity: future=True

		# Let's start with a simple yes/no version
		# We add a small message to display if it's impossible to observe:
		msg = ''
		warnings = ''

		### General conditions:
		# Each condition has an associated bool flag to tell if it is respected or not
		# Not respected conditions decrease the overall observability by a given amount
		# todo: configure the observability amount decrease in the obsprogram files.

		# check the	moondistance:
		self.obs_moondist = True
		if self.angletomoon.degree < self.minangletomoon:
			observability *= 0.8
			self.obs_moondist = False
			msg += '\nMoonDist:%0.1f' % self.angletomoon.degree

		# high airmass
		self.obs_highairmass = True
		if self.airmass > 1.5:
			self.obs_highairmass = False
			observability *= 0.7
			msg += '\nAirmass:%0.2f' % self.airmass

		# check the airmass:
		self.obs_airmass = True
		if self.airmass > self.maxairmass:
			self.obs_airmass = False
			observability = 0
			msg += '\nAirmass:%0.2f' % self.airmass


		# check the wind:
		self.obs_wind, self.obs_wind_info = True, True
		if not future:
			if meteo.windspeed > 0. and meteo.windspeed < 100. and self.angletowind is not None:
				if self.angletowind.degree < 90 and meteo.windspeed >= float(meteo.location.get("weather", "windWarnLevel")):
					self.obs_wind = False
					observability = 0
					msg += '\nWA:%0.1f/WS:%0.1f' % (self.angletowind.degree, meteo.windspeed)

				if meteo.windspeed >= float(meteo.location.get("weather", "windLimitLevel")):
					self.obs_wind = False
					observability = 0
					msg += '\nWS:%s' % meteo.windspeed
			else:
				self.obs_wind_info = False
				warnings += '\nNo wind info'
		else:
			self.obs_wind_info = False

		# check the clouds
		self.obs_clouds, self.obs_clouds_info = cloudscheck, True
		if not future:
			if cloudscheck:
				self.is_cloudfree(meteo)
				if self.cloudfree <= 0.5 :
					self.obs_clouds = False
					warnings += '\nWarning ! It might be cloudy'
					observability = 0
				elif self.cloudfree <= 0.9:
					self.obs_clouds = False
					msg += '\nCould be cloud-free'
					observability *= self.cloudfree
				elif self.cloudfree <= 1.:
					msg += '\nShould be cloud-free'
				else:
					self.obs_clouds_info = False
					warnings += '\nNo cloud info'
					#print("no cloud info")
			else:
				self.obs_clouds_info = False
				warnings += '\nNo cloud info'
		else:
			self.obs_clouds_info = False

		# check the internal observability flag
		self.obs_internal = True
		if hasattr(self, 'internalobs'):
			if self.internalobs == 0:
				self.obs_internal = False
				observability = self.internalobs
				msg += '\nSpreadsheet NO'

		### Program specific conditions:
		pobs, pmsg, pwarn = self.program.observability(self.attributes, meteo.time)
		if pobs == 0: observability = 0
		msg += pmsg
		warnings += pwarn

		# Finally, add the eventuel comment:
		if hasattr(self, 'comment'):
			msg += '\n %s' % self.comment

		to_print = "%s | %s\nalpha=%s, delta=%s\naz=%0.2f, alt=%0.2f%s" % (self.name, meteo.time.iso, self.alpha, self.delta, rad2deg(self.azimuth.value), rad2deg(self.altitude.value), msg)
		if verbose:
			if observability == 1:
				print((util.hilite(to_print, True, True)))
				if not warnings == '': print((util.hilite(warnings, False, False)))
				print(("="*20))
			else:
				if displayall:
					print((util.hilite(to_print, False, False)))
					if not warnings == '': print((util.hilite(warnings, False, False)))
					print(("="*20))
				else:
					pass
		else:
			# then we send the print message to the logger
			if SETTINGS["misc"]["singletargetlogs"] == "True":
				logger.info(to_print)
		self.observability = observability
예제 #17
0
def log_image(bot, msg):
    permalink = msg[u'file'].get(u'permalink_public')
    if permalink:
        msg[u'_logged'] = True
        with autoflush(bot), util.hilite('blue'):
            print(permalink)
예제 #18
0
def _log_message(bot, channel, user, text):
    with util.hilite('cyan'):
        print(channel, end=' ')
    with util.hilite('purple'):
        print(user, end=' ')
    print(_format_text(bot, text))
예제 #19
0
def _unknown_command(bot, command):
    with util.hilite('gray'):
        print("Unknown command '{}'".format(command[0]))