def __init__(self, config): irc.Bot.__init__(self, config.core) self.config = config """The ``Config`` for the current Willie instance.""" self.doc = {} """ *Removed in 3.1.2* A dictionary of module functions to their docstring and example, if declared. As of 3.1.2, this dict will be empty, and not updated. """ self.stats = {} """ A dictionary which maps a tuple of a function name and where it was used to the nuber of times it was used there. """ self.times = {} """ A dictionary mapping lower-case'd nicks to dictionaries which map funtion names to the time which they were last used by that nick. """ self.acivity = {} self.db = WillieDB(config) if self.db.check_table('locales', ['name'], 'name'): self.settings = self.db.locales self.db.preferences = self.db.locales elif self.db.check_table('preferences', ['name'], 'name'): self.settings = self.db.preferences elif self.db.type is not None: self.db.add_table('preferences', ['name'], 'name') self.settings = self.db.preferences self.memory = tools.WillieMemory() """ A thread-safe dict for storage of runtime data to be shared between modules. See `WillieMemory <#tools.Willie.WillieMemory>`_ """ self.scheduler = Willie.JobScheduler(self) self.scheduler.start() #Set up block lists #Default to empty if not self.config.core.nick_blocks: self.config.core.nick_blocks = [] if not self.config.core.nick_blocks: self.config.core.host_blocks = [] #Add nicks blocked under old scheme, if present if self.config.core.other_bots: nicks = self.config.core.get_list('nick_blocks') bots = self.config.core.get_list('other_bots') nicks.extend(bots) self.config.core.nick_blocks = nicks self.config.core.other_bots = False self.config.save() self.setup()
def __init__(self, config): irc.Bot.__init__(self, config.core) self.config = config """The ``Config`` for the current Willie instance.""" self.doc = {} """ *Removed in 3.1.2* A dictionary of module functions to their docstring and example, if declared. As of 3.1.2, this dict will be empty, and not updated. """ self.stats = {} """ A dictionary which maps a tuple of a function name and where it was used to the nuber of times it was used there. """ self.times = {} """ A dictionary mapping lower-case'd nicks to dictionaries which map funtion names to the time which they were last used by that nick. """ self.acivity = {} self.db = WillieDB(config) if self.db.check_table('locales', ['name'], 'name'): self.settings = self.db.locales self.db.preferences = self.db.locales elif self.db.check_table('preferences', ['name'], 'name'): self.settings = self.db.preferences elif self.db.type is not None: self.db.add_table('preferences', ['name'], 'name') self.settings = self.db.preferences self.memory = self.WillieMemory() """ A thread-safe dict for storage of runtime data to be shared between modules. See `WillieMemory <#bot.Willie.WillieMemory>`_ """ #Set up block lists #Default to empty if not self.config.has_option('core', 'nick_blocks'): self.config.core.nick_blocks = '' if not self.config.has_option('core', 'host_blocks'): self.config.core.host_blocks = '' #Make into lists if not isinstance(self.config.core.nick_blocks, list): self.config.core.nick_blocks = self.config.core.nick_blocks.split(',') if not isinstance(self.config.core.host_blocks, list): self.config.core.host_blocks = self.config.core.host_blocks.split(',') #Add nicks blocked under old scheme, if present if self.config.has_option('core', 'other_bots'): nicks = self.config.core.nick_blocks bots = self.config.core.other_bots if isinstance(bots, basestring): bots = bots.split(',') nicks.extend(bots) self.config.core.nick_blocks = nicks self.setup()
def __init__(self, config): irc.Bot.__init__(self, config.core) self.config = config """The ``Config`` for the current Willie instance.""" self.doc = {} """ *Removed in 3.1.2* A dictionary of module functions to their docstring and example, if declared. As of 3.1.2, this dict will be empty, and not updated. """ self.stats = {} """ A dictionary which maps a tuple of a function name and where it was used to the nuber of times it was used there. """ self.times = {} """ A dictionary mapping lower-case'd nicks to dictionaries which map funtion names to the time which they were last used by that nick. """ self.acivity = {} self.db = WillieDB(config) if self.db.check_table("locales", ["name"], "name"): self.settings = self.db.locales self.db.preferences = self.db.locales elif self.db.check_table("preferences", ["name"], "name"): self.settings = self.db.preferences elif self.db.type is not None: self.db.add_table("preferences", ["name"], "name") self.settings = self.db.preferences self.memory = tools.WillieMemory() """ A thread-safe dict for storage of runtime data to be shared between modules. See `WillieMemory <#tools.Willie.WillieMemory>`_ """ self.scheduler = Willie.JobScheduler(self) self.scheduler.start() # Set up block lists # Default to empty if not self.config.has_option("core", "nick_blocks") or not self.config.core.nick_blocks: self.config.core.nick_blocks = [] if not self.config.has_option("core", "host_blocks") or not self.config.core.nick_blocks: self.config.core.host_blocks = [] # Add nicks blocked under old scheme, if present if self.config.has_option("core", "other_bots") and self.config.core.other_bots: nicks = self.config.core.get_list("nick_blocks") bots = self.config.core.get_list("other_bots") nicks.extend(bots) self.config.core.nick_blocks = nicks self.config.core.other_bots = False self.config.save() self.setup()
class Willie(irc.Bot): NOLIMIT = 1 """ *Avalability: 3.2+* Return value for ``callable``\s, which supresses rate limiting for that call. That is, returning this value means the triggering user will not be prevented from triggering the command again within the rate limit. This can be used, for example, to allow a user to rety a failed command immediately. """ def __init__(self, config): irc.Bot.__init__(self, config.core) self.config = config """The ``Config`` for the current Willie instance.""" self.doc = {} """ *Removed in 3.1.2* A dictionary of module functions to their docstring and example, if declared. As of 3.1.2, this dict will be empty, and not updated. """ self.stats = {} """ A dictionary which maps a tuple of a function name and where it was used to the nuber of times it was used there. """ self.times = {} """ A dictionary mapping lower-case'd nicks to dictionaries which map funtion names to the time which they were last used by that nick. """ self.acivity = {} self.db = WillieDB(config) if self.db.check_table('locales', ['name'], 'name'): self.settings = self.db.locales self.db.preferences = self.db.locales elif self.db.check_table('preferences', ['name'], 'name'): self.settings = self.db.preferences elif self.db.type is not None: self.db.add_table('preferences', ['name'], 'name') self.settings = self.db.preferences self.memory = self.WillieMemory() """ A thread-safe dict for storage of runtime data to be shared between modules. See `WillieMemory <#bot.Willie.WillieMemory>`_ """ #Set up block lists #Default to empty if not self.config.has_option('core', 'nick_blocks'): self.config.core.nick_blocks = '' if not self.config.has_option('core', 'host_blocks'): self.config.core.host_blocks = '' #Add nicks blocked under old scheme, if present if self.config.has_option('core', 'other_bots'): nicks = self.config.core.get_list('nick_blocks') bots = self.config.core.get_list('other_bots') nicks.extend(bots) self.config.core.nick_blocks = nicks self.setup() class WillieMemory(dict): """ Availability: 3.1+ A simple thread-safe dict implementation. In order to prevent exceptions when iterating over the values and changing them at the same time from different threads, we use a blocking lock on ``__setitem__`` and ``contains``. """ def __init__(self, *args): dict.__init__(self, *args) self.lock = threading.Lock() def __setitem__(self, key, value): self.lock.acquire() result = dict.__setitem__(self, key, value) self.lock.release() return result def contains(self, key): """ Check if a key is in the dict. Use this instead of the ``in`` keyword if you want to be thread-safe. """ self.lock.acquire() result = (key in self) self.lock.release() return result def setup(self): stderr("\nWelcome to Willie. Loading modules...\n\n") self.variables = {} filenames = enumerate_modules(self.config) filenames.append(os.path.join(this_dir, 'coretasks.py')) self.enumerate_modules = enumerate_modules modules = [] excluded_modules = getattr(self.config, 'exclude', []) error_count = 0 for filename in filenames: name = os.path.basename(filename)[:-3] if name in excluded_modules: continue try: module = imp.load_source(name, filename) except Exception, e: error_count = error_count + 1 stderr("Error loading %s: %s (in bot.py)" % (name, e)) else: try: if hasattr(module, 'setup'): module.setup(self) self.register(vars(module)) modules.append(name) except Exception, e: error_count = error_count + 1 stderr("Error in %s setup procedure: %s (in bot.py)" % (name, e))
class Willie(irc.Bot): NOLIMIT = module.NOLIMIT def __init__(self, config): irc.Bot.__init__(self, config.core) self.config = config """The ``Config`` for the current Willie instance.""" self.doc = {} """ *Removed in 3.1.2* A dictionary of module functions to their docstring and example, if declared. As of 3.1.2, this dict will be empty, and not updated. """ self.stats = {} """ A dictionary which maps a tuple of a function name and where it was used to the nuber of times it was used there. """ self.times = {} """ A dictionary mapping lower-case'd nicks to dictionaries which map funtion names to the time which they were last used by that nick. """ self.acivity = {} self.db = WillieDB(config) if self.db.check_table('locales', ['name'], 'name'): self.settings = self.db.locales self.db.preferences = self.db.locales elif self.db.check_table('preferences', ['name'], 'name'): self.settings = self.db.preferences elif self.db.type is not None: self.db.add_table('preferences', ['name'], 'name') self.settings = self.db.preferences self.memory = tools.WillieMemory() """ A thread-safe dict for storage of runtime data to be shared between modules. See `WillieMemory <#tools.Willie.WillieMemory>`_ """ self.scheduler = Willie.JobScheduler(self) self.scheduler.start() #Set up block lists #Default to empty if not self.config.core.nick_blocks: self.config.core.nick_blocks = [] if not self.config.core.nick_blocks: self.config.core.host_blocks = [] #Add nicks blocked under old scheme, if present if self.config.core.other_bots: nicks = self.config.core.get_list('nick_blocks') bots = self.config.core.get_list('other_bots') nicks.extend(bots) self.config.core.nick_blocks = nicks self.config.core.other_bots = False self.config.save() self.setup() class JobScheduler(threading.Thread): """Calls jobs assigned to it in steady intervals. JobScheduler is a thread that keeps track of Jobs and calls them every X seconds, where X is a property of the Job. It maintains jobs in a priority queue, where the next job to be called is always the first item. Thread safety is maintained with a mutex that is released during long operations, so methods add_job and clear_jobs can be safely called from the main thread. """ min_reaction_time = 30.0 # seconds """How often should scheduler checks for changes in the job list.""" def __init__(self, bot): """Requires bot as argument for logging.""" threading.Thread.__init__(self) self.bot = bot self._jobs = PriorityQueue() # While PriorityQueue it self is thread safe, this mutex is needed # to stop old jobs being put into new queue after clearing the # queue. self._mutex = threading.Lock() # self.cleared is used for more fine grained locking. self._cleared = False def add_job(self, job): """Add a Job to the current job queue.""" self._jobs.put(job) def clear_jobs(self): """Clear current Job queue and start fresh.""" if self._jobs.empty(): # Guards against getting stuck waiting for self._mutex when # thread is waiting for self._jobs to not be empty. return with self._mutex: self._cleared = True self._jobs = PriorityQueue() def run(self): """Run forever.""" while True: try: self._do_next_job() except Exception: # Modules exceptions are caught earlier, so this is a bit # more serious. Options are to either stop the main thread # or continue this thread and hope that it won't happen # again. self.bot.error() # Sleep a bit to guard against busy-looping and filling # the log with useless error messages. time.sleep(10.0) # seconds def _do_next_job(self): """Wait until there is a job and do it.""" with self._mutex: # Wait until the next job should be executed. # This has to be a loop, because signals stop time.sleep(). while True: job = self._jobs.peek() difference = job.next_time - time.time() duration = min(difference, self.min_reaction_time) if duration <= 0: break with released(self._mutex): time.sleep(duration) self._cleared = False job = self._jobs.get() with released(self._mutex): if job.func.thread: t = threading.Thread(target=self._call, args=(job.func, )) t.start() else: self._call(job.func) job.next() # If jobs were cleared during the call, don't put an old job # into the new job queue. if not self._cleared: self._jobs.put(job) def _call(self, func): """Wrapper for collecting errors from modules.""" # Willie.bot.call is way too specialized to be used instead. try: func(self.bot) except Exception: self.bot.error() class Job(object): """ Job is a simple structure that hold information about when a function should be called next. They can be put in a priority queue, in which case the Job that should be executed next is returned. Calling the method next modifies the Job object for the next time it should be executed. Current time is used to decide when the job should be executed next so it should only be called right after the function was called. """ max_catchup = 5 """ This governs how much the scheduling of jobs is allowed to get behind before they are simply thrown out to avoid calling the same function too many times at once. """ def __init__(self, interval, func): """Initialize Job. Args: interval: number of seconds between calls to func func: function to be called """ self.next_time = time.time() + interval self.interval = interval self.func = func def next(self): """Update self.next_time with the assumption func was just called. Returns: A modified job object. """ last_time = self.next_time current_time = time.time() delta = last_time + self.interval - current_time if last_time > current_time + self.interval: # Clock appears to have moved backwards. Reset # the timer to avoid waiting for the clock to # catch up to whatever time it was previously. self.next_time = current_time + self.interval elif delta < 0 and abs(delta) > self.interval * self.max_catchup: # Execution of jobs is too far behind. Give up on # trying to catch up and reset the time, so that # will only be repeated a maximum of # self.max_catchup times. self.next_time = current_time - \ self.interval * self.max_catchup else: self.next_time = last_time + self.interval return self def __cmp__(self, other): """Compare Job objects according to attribute next_time.""" return self.next_time - other.next_time def __str__(self): """Return a string representation of the Job object. Example result: <Job(2013-06-14 11:01:36.884000, 20s, <function upper at 0x02386BF0>)> """ iso_time = str(datetime.fromtimestamp(self.next_time)) return "<Job(%s, %ss, %s)>" % \ (iso_time, self.interval, self.func) def __iter__(self): """This is an iterator. Never stops though.""" return self def setup(self): stderr("\nWelcome to Willie. Loading modules...\n\n") self.callables = set() self.shutdown_methods = set() filenames = self.config.enumerate_modules() # Coretasks is special. No custom user coretasks. this_dir = os.path.dirname(os.path.abspath(__file__)) filenames['coretasks'] = os.path.join(this_dir, 'coretasks.py') modules = [] error_count = 0 for name, filename in filenames.iteritems(): try: module = imp.load_source(name, filename) except Exception, e: error_count = error_count + 1 filename, lineno = tools.get_raising_file_and_line() rel_path = os.path.relpath(filename, os.path.dirname(__file__)) raising_stmt = "%s:%d" % (rel_path, lineno) stderr("Error loading %s: %s (%s)" % (name, e, raising_stmt)) else: try: if hasattr(module, 'setup'): module.setup(self) self.register(vars(module)) modules.append(name) except Exception, e: error_count = error_count + 1 filename, lineno = tools.get_raising_file_and_line() rel_path = os.path.relpath(filename, os.path.dirname(__file__)) raising_stmt = "%s:%d" % (rel_path, lineno) stderr("Error in %s setup procedure: %s (%s)" % (name, e, raising_stmt))
def __init__(self, config): irc.Bot.__init__(self, config.core) self.config = config """The ``Config`` for the current Willie instance.""" self.doc = {} """ A dictionary of command names to their docstring and example, if declared. The first item in a callable's commands list is used as the key in version *3.2* onward. Prior to *3.2*, the name of the function as declared in the source code was used. """ self.stats = {} """ A dictionary which maps a tuple of a function name and where it was used to the nuber of times it was used there. """ self.times = {} """ A dictionary mapping lower-case'd nicks to dictionaries which map funtion names to the time which they were last used by that nick. """ self.acivity = {} self.server_capabilities = set() """A set containing the IRCv3 capabilities that the server supports. For servers that do not support IRCv3, this will be an empty set.""" self.enabled_capabilities = set() """A set containing the IRCv3 capabilities that the bot has enabled.""" self._cap_reqs = dict() """A dictionary of capability requests Maps the capability name to a tuple of the prefix ('-', '=', or ''), the name of the requesting module, and the function to call if the request is rejected.""" self.privileges = dict() """A dictionary of channels to their users and privilege levels The value associated with each channel is a dictionary of Nicks to a bitwise integer value, determined by combining the appropriate constants from `module`.""" self.db = WillieDB(config) if self.db.check_table('locales', ['name'], 'name'): self.settings = self.db.locales self.db.preferences = self.db.locales elif self.db.check_table('preferences', ['name'], 'name'): self.settings = self.db.preferences elif self.db.type is not None: self.db.add_table('preferences', ['name'], 'name') self.settings = self.db.preferences self.memory = tools.WillieMemory() """ A thread-safe dict for storage of runtime data to be shared between modules. See `WillieMemory <#tools.Willie.WillieMemory>`_ """ self.scheduler = Willie.JobScheduler(self) self.scheduler.start() #Set up block lists #Default to empty if not self.config.core.nick_blocks: self.config.core.nick_blocks = [] if not self.config.core.nick_blocks: self.config.core.host_blocks = [] #Add nicks blocked under old scheme, if present if self.config.core.other_bots: nicks = self.config.core.get_list('nick_blocks') bots = self.config.core.get_list('other_bots') nicks.extend(bots) self.config.core.nick_blocks = nicks self.config.core.other_bots = False self.config.save() self.setup()
class Willie(irc.Bot): NOLIMIT = module.NOLIMIT def __init__(self, config): irc.Bot.__init__(self, config.core) self.config = config """The ``Config`` for the current Willie instance.""" self.doc = {} """ A dictionary of command names to their docstring and example, if declared. The first item in a callable's commands list is used as the key in version *3.2* onward. Prior to *3.2*, the name of the function as declared in the source code was used. """ self.stats = {} """ A dictionary which maps a tuple of a function name and where it was used to the nuber of times it was used there. """ self.times = {} """ A dictionary mapping lower-case'd nicks to dictionaries which map funtion names to the time which they were last used by that nick. """ self.acivity = {} self.server_capabilities = set() """A set containing the IRCv3 capabilities that the server supports. For servers that do not support IRCv3, this will be an empty set.""" self.enabled_capabilities = set() """A set containing the IRCv3 capabilities that the bot has enabled.""" self._cap_reqs = dict() """A dictionary of capability requests Maps the capability name to a tuple of the prefix ('-', '=', or ''), the name of the requesting module, and the function to call if the request is rejected.""" self.privileges = dict() """A dictionary of channels to their users and privilege levels The value associated with each channel is a dictionary of Nicks to a bitwise integer value, determined by combining the appropriate constants from `module`.""" self.db = WillieDB(config) if self.db.check_table('locales', ['name'], 'name'): self.settings = self.db.locales self.db.preferences = self.db.locales elif self.db.check_table('preferences', ['name'], 'name'): self.settings = self.db.preferences elif self.db.type is not None: self.db.add_table('preferences', ['name'], 'name') self.settings = self.db.preferences self.memory = tools.WillieMemory() """ A thread-safe dict for storage of runtime data to be shared between modules. See `WillieMemory <#tools.Willie.WillieMemory>`_ """ self.scheduler = Willie.JobScheduler(self) self.scheduler.start() #Set up block lists #Default to empty if not self.config.core.nick_blocks: self.config.core.nick_blocks = [] if not self.config.core.nick_blocks: self.config.core.host_blocks = [] #Add nicks blocked under old scheme, if present if self.config.core.other_bots: nicks = self.config.core.get_list('nick_blocks') bots = self.config.core.get_list('other_bots') nicks.extend(bots) self.config.core.nick_blocks = nicks self.config.core.other_bots = False self.config.save() self.setup() class JobScheduler(threading.Thread): """Calls jobs assigned to it in steady intervals. JobScheduler is a thread that keeps track of Jobs and calls them every X seconds, where X is a property of the Job. It maintains jobs in a priority queue, where the next job to be called is always the first item. Thread safety is maintained with a mutex that is released during long operations, so methods add_job and clear_jobs can be safely called from the main thread. """ min_reaction_time = 30.0 # seconds """How often should scheduler checks for changes in the job list.""" def __init__(self, bot): """Requires bot as argument for logging.""" threading.Thread.__init__(self) self.bot = bot self._jobs = PriorityQueue() # While PriorityQueue it self is thread safe, this mutex is needed # to stop old jobs being put into new queue after clearing the # queue. self._mutex = threading.Lock() # self.cleared is used for more fine grained locking. self._cleared = False def add_job(self, job): """Add a Job to the current job queue.""" self._jobs.put(job) def clear_jobs(self): """Clear current Job queue and start fresh.""" if self._jobs.empty(): # Guards against getting stuck waiting for self._mutex when # thread is waiting for self._jobs to not be empty. return with self._mutex: self._cleared = True self._jobs = PriorityQueue() def run(self): """Run forever.""" while True: try: self._do_next_job() except Exception: # Modules exceptions are caught earlier, so this is a bit # more serious. Options are to either stop the main thread # or continue this thread and hope that it won't happen # again. self.bot.error() # Sleep a bit to guard against busy-looping and filling # the log with useless error messages. time.sleep(10.0) # seconds def _do_next_job(self): """Wait until there is a job and do it.""" with self._mutex: # Wait until the next job should be executed. # This has to be a loop, because signals stop time.sleep(). while True: job = self._jobs.peek() difference = job.next_time - time.time() duration = min(difference, self.min_reaction_time) if duration <= 0: break with released(self._mutex): time.sleep(duration) self._cleared = False job = self._jobs.get() with released(self._mutex): if job.func.thread: t = threading.Thread( target=self._call, args=(job.func,) ) t.start() else: self._call(job.func) job.next() # If jobs were cleared during the call, don't put an old job # into the new job queue. if not self._cleared: self._jobs.put(job) def _call(self, func): """Wrapper for collecting errors from modules.""" # Willie.bot.call is way too specialized to be used instead. try: func(self.bot) except Exception: self.bot.error() class Job(object): """ Job is a simple structure that hold information about when a function should be called next. They can be put in a priority queue, in which case the Job that should be executed next is returned. Calling the method next modifies the Job object for the next time it should be executed. Current time is used to decide when the job should be executed next so it should only be called right after the function was called. """ max_catchup = 5 """ This governs how much the scheduling of jobs is allowed to get behind before they are simply thrown out to avoid calling the same function too many times at once. """ def __init__(self, interval, func): """Initialize Job. Args: interval: number of seconds between calls to func func: function to be called """ self.next_time = time.time() + interval self.interval = interval self.func = func def next(self): """Update self.next_time with the assumption func was just called. Returns: A modified job object. """ last_time = self.next_time current_time = time.time() delta = last_time + self.interval - current_time if last_time > current_time + self.interval: # Clock appears to have moved backwards. Reset # the timer to avoid waiting for the clock to # catch up to whatever time it was previously. self.next_time = current_time + self.interval elif delta < 0 and abs(delta) > self.interval * self.max_catchup: # Execution of jobs is too far behind. Give up on # trying to catch up and reset the time, so that # will only be repeated a maximum of # self.max_catchup times. self.next_time = current_time - \ self.interval * self.max_catchup else: self.next_time = last_time + self.interval return self def __cmp__(self, other): """Compare Job objects according to attribute next_time.""" return self.next_time - other.next_time def __str__(self): """Return a string representation of the Job object. Example result: <Job(2013-06-14 11:01:36.884000, 20s, <function upper at 0x02386BF0>)> """ iso_time = str(datetime.fromtimestamp(self.next_time)) return "<Job(%s, %ss, %s)>" % \ (iso_time, self.interval, self.func) def __iter__(self): """This is an iterator. Never stops though.""" return self def setup(self): stderr(u"\nWelcome to Granota launcher. Loading modules...\n\n") self.callables = set() self.shutdown_methods = set() filenames = self.config.enumerate_modules() # Coretasks is special. No custom user coretasks. this_dir = os.path.dirname(os.path.abspath(__file__)) filenames['coretasks'] = os.path.join(this_dir, 'coretasks.py') modules = [] error_count = 0 for name, filename in filenames.iteritems(): try: module = imp.load_source(name, filename) except Exception, e: error_count = error_count + 1 filename, lineno = tools.get_raising_file_and_line() rel_path = os.path.relpath(filename, os.path.dirname(__file__)) raising_stmt = "%s:%d" % (rel_path, lineno) stderr("Error loading %s: %s (%s)" % (name, e, raising_stmt)) else: try: if hasattr(module, 'setup'): module.setup(self) self.register(vars(module)) modules.append(name) except Exception, e: error_count = error_count + 1 filename, lineno = tools.get_raising_file_and_line() rel_path = os.path.relpath( filename, os.path.dirname(__file__) ) raising_stmt = "%s:%d" % (rel_path, lineno) stderr(u"Error in the configuration protocol %s: %s (%s)" % (name, e, raising_stmt))
class Willie(irc.Bot): NOLIMIT = module.NOLIMIT def __init__(self, config): irc.Bot.__init__(self, config.core) self.config = config """The ``Config`` for the current Willie instance.""" self.doc = {} """ *Removed in 3.1.2* A dictionary of module functions to their docstring and example, if declared. As of 3.1.2, this dict will be empty, and not updated. """ self.stats = {} """ A dictionary which maps a tuple of a function name and where it was used to the nuber of times it was used there. """ self.times = {} """ A dictionary mapping lower-case'd nicks to dictionaries which map funtion names to the time which they were last used by that nick. """ self.acivity = {} self.db = WillieDB(config) if self.db.check_table('locales', ['name'], 'name'): self.settings = self.db.locales self.db.preferences = self.db.locales elif self.db.check_table('preferences', ['name'], 'name'): self.settings = self.db.preferences elif self.db.type is not None: self.db.add_table('preferences', ['name'], 'name') self.settings = self.db.preferences self.memory = self.WillieMemory() """ A thread-safe dict for storage of runtime data to be shared between modules. See `WillieMemory <#bot.Willie.WillieMemory>`_ """ #Set up block lists #Default to empty if not self.config.has_option('core', 'nick_blocks') or not self.config.core.nick_blocks: self.config.core.nick_blocks = [] if not self.config.has_option('core', 'host_blocks') or not self.config.core.nick_blocks: self.config.core.host_blocks = [] #Add nicks blocked under old scheme, if present if self.config.has_option('core', 'other_bots') and self.config.core.other_bots: nicks = self.config.core.get_list('nick_blocks') bots = self.config.core.get_list('other_bots') nicks.extend(bots) self.config.core.nick_blocks = nicks self.config.core.other_bots = False self.config.save() self.setup() class WillieMemory(dict): """ Availability: 3.1+ A simple thread-safe dict implementation. In order to prevent exceptions when iterating over the values and changing them at the same time from different threads, we use a blocking lock on ``__setitem__`` and ``contains``. """ def __init__(self, *args): dict.__init__(self, *args) self.lock = threading.Lock() def __setitem__(self, key, value): self.lock.acquire() result = dict.__setitem__(self, key, value) self.lock.release() return result def contains(self, key): """ Check if a key is in the dict. Use this instead of the ``in`` keyword if you want to be thread-safe. """ self.lock.acquire() result = (key in self) self.lock.release() return result def setup(self): stderr("\nWelcome to Willie. Loading modules...\n\n") self.callables = set() filenames = self.config.enumerate_modules() # Coretasks is special. No custom user coretasks. this_dir = os.path.dirname(os.path.abspath(__file__)) filenames['coretasks'] = os.path.join(this_dir, 'coretasks.py') modules = [] error_count = 0 for name, filename in filenames.iteritems(): try: module = imp.load_source(name, filename) except Exception, e: error_count = error_count + 1 stderr("Error loading %s: %s (in bot.py)" % (name, e)) else: try: if hasattr(module, 'setup'): module.setup(self) self.register(vars(module)) modules.append(name) except Exception, e: error_count = error_count + 1 stderr("Error in %s setup procedure: %s (in bot.py)" % (name, e))
class Willie(irc.Bot): NOLIMIT = module.NOLIMIT def __init__(self, config): irc.Bot.__init__(self, config.core) self.config = config """The ``Config`` for the current Willie instance.""" self.doc = {} """ *Removed in 3.1.2* A dictionary of module functions to their docstring and example, if declared. As of 3.1.2, this dict will be empty, and not updated. """ self.stats = {} """ A dictionary which maps a tuple of a function name and where it was used to the nuber of times it was used there. """ self.times = {} """ A dictionary mapping lower-case'd nicks to dictionaries which map funtion names to the time which they were last used by that nick. """ self.acivity = {} self.db = WillieDB(config) if self.db.check_table('locales', ['name'], 'name'): self.settings = self.db.locales self.db.preferences = self.db.locales elif self.db.check_table('preferences', ['name'], 'name'): self.settings = self.db.preferences elif self.db.type is not None: self.db.add_table('preferences', ['name'], 'name') self.settings = self.db.preferences self.memory = self.WillieMemory() """ A thread-safe dict for storage of runtime data to be shared between modules. See `WillieMemory <#bot.Willie.WillieMemory>`_ """ #Set up block lists #Default to empty if not self.config.has_option( 'core', 'nick_blocks') or not self.config.core.nick_blocks: self.config.core.nick_blocks = [] if not self.config.has_option( 'core', 'host_blocks') or not self.config.core.nick_blocks: self.config.core.host_blocks = [] #Add nicks blocked under old scheme, if present if self.config.has_option( 'core', 'other_bots') and self.config.core.other_bots: nicks = self.config.core.get_list('nick_blocks') bots = self.config.core.get_list('other_bots') nicks.extend(bots) self.config.core.nick_blocks = nicks self.config.core.other_bots = False self.config.save() self.setup() class WillieMemory(dict): """ Availability: 3.1+ A simple thread-safe dict implementation. In order to prevent exceptions when iterating over the values and changing them at the same time from different threads, we use a blocking lock on ``__setitem__`` and ``contains``. """ def __init__(self, *args): dict.__init__(self, *args) self.lock = threading.Lock() def __setitem__(self, key, value): self.lock.acquire() result = dict.__setitem__(self, key, value) self.lock.release() return result def contains(self, key): """ Check if a key is in the dict. Use this instead of the ``in`` keyword if you want to be thread-safe. """ self.lock.acquire() result = (key in self) self.lock.release() return result def setup(self): stderr("\nWelcome to Willie. Loading modules...\n\n") self.callables = set() filenames = self.config.enumerate_modules() # Coretasks is special. No custom user coretasks. this_dir = os.path.dirname(os.path.abspath(__file__)) filenames['coretasks'] = os.path.join(this_dir, 'coretasks.py') modules = [] error_count = 0 for name, filename in filenames.iteritems(): try: module = imp.load_source(name, filename) except Exception, e: error_count = error_count + 1 stderr("Error loading %s: %s (in bot.py)" % (name, e)) else: try: if hasattr(module, 'setup'): module.setup(self) self.register(vars(module)) modules.append(name) except Exception, e: error_count = error_count + 1 stderr("Error in %s setup procedure: %s (in bot.py)" % (name, e))