def set_input_type(input_type): global backend global all_data global wiki_storages global subreddit_cache global report_cmds global cache_data global modlog_hist backend = importlib.import_module("modbot.input.%s" % input_type) # Initialize objects that depend on the backend all_data = dsdict("all", "last_seen") # Last seen /r/all subs and comms wiki_storages = {} subreddit_cache = {} cache_data = {} report_cmds = dsdict("mod", "cmds") modlog_hist = dsdict("mod", "modlog")
def launch_threads(fname, nb_threads, cycles): test_dict = dsdict("test", fname) nb_threads = nb_threads for thr in range(nb_threads): print("Launching thread %d", thr) test_th = threading.Thread(name="pmgr_thread", target=do_test, args=(test_dict, cycles)) to_wait.append(test_th)
def __init__(self, reddit_wiki): try: self.content = reddit_wiki.content_md except: self.content = "" # Set permissions to mod only try: reddit_wiki.mod.update(listed=False, permlevel=2) except: pass self.subreddit_name = reddit_wiki.subreddit.display_name self.name = reddit_wiki.name try: self.author = user(reddit_wiki.revision_by) except: self.author = "" try: self.revision_date = int(reddit_wiki.revision_date) except: self.revision_date = 0 # Check if the subreddit has a wiki storage if self.subreddit_name not in wiki_storages: logger.debug("Adding %s/%s to wiki storage" % (self.subreddit_name, self.name)) wiki_storages[self.subreddit_name] = dsdict( self.subreddit_name, "wiki_cache") storage = wiki_storages[self.subreddit_name] storage[self.name] = { "store_date": utcnow(), "subreddit": self.subreddit_name, "name": self.name, "content": self.content, "author": str(self.author), "revision_date": self.revision_date } storage.sync()
def wiki(self, name, force_live=False): if force_live: logger.debug("Getting a fresh copy of %s/%s" % (self.display_name, name)) self.wikis[name] = wiki(backend.get_wiki(self._raw, name)) logger.debug("[%s] Access wiki: %s" % (self, name)) if str(self) not in wiki_storages: wiki_storages[str(self)] = dsdict(str(self), "wiki_cache") # Check if there is a copy of the wiki stored if name in wiki_storages[str(self)] and \ utcnow() - wiki_storages[str(self)][name]["store_date"] < COLD_WIKI_LIMIT: logger.debug("Getting stored copy of %s/%s" % (self.display_name, name)) self.wikis[name] = wiki_stored(wiki_storages[str(self)][name]) else: logger.debug("Getting a fresh copy of %s/%s" % (self.display_name, name)) self.wikis[name] = wiki(backend.get_wiki(self._raw, name)) return self.wikis[name]
def __init__(self, subreddit, plugin): self.sub = subreddit self.subreddit_name = subreddit.display_name self.wiki_page = plugin.wiki_page self.content = "" try: self.content = self.sub.wiki(self.wiki_page).get_content() except prawcore.exceptions.NotFound: logger.debug( "Subreddit %s does not contain wiki %s. Creating it." % (subreddit.display_name, self.wiki_page)) self.sub.wiki[self.wiki_page].edit(EMPTY_WIKI) self.content = EMPTY_WIKI self.old_content = self.content self.last_update = utils.utcnow() self.refresh_interval = plugin.refresh_interval # not used self.notifier = plugin.wiki_change_notifier self.description = plugin.description self.raw_documentation = plugin.documentation self.documentation = None # Provide extra args for upper classes self.args = {} # Create location for small storage self.storage = dsdict(self.subreddit_name, self.wiki_page) self.args["storage"] = self.storage # Initialize wiki r/w self.mode = "" if "r" in plugin.mode: self.mode += "r" if "w" in plugin.mode: self.mode += "w" # Pre generate documentation string if self.raw_documentation: self.documentation = WatchedWiki.DOC_BEGIN split_doc = self.raw_documentation.strip().split("\n") self.documentation += "\n".join( (" ## " + line).rstrip() for line in split_doc) self.documentation += WatchedWiki.DOC_END # Set documentation if not already present self.content = self.content.replace("\r", "") if self.documentation and self.documentation.strip( ) not in self.content: begin = self.content.replace("\r", "").find(WatchedWiki.DOC_BEGIN) end = self.content.replace("\r", "").find(WatchedWiki.DOC_END) if begin != -1 and end != -1: self.content = self.content[0:begin] + self.documentation + \ self.content[end + len(WatchedWiki.DOC_END):] else: self.content = self.documentation + self.content self.set_content(self.content, with_documentation=False)