<commit_before>#!/usr/bin/env python3
import inspect
import os
import imp
import logging
import time
import json
import getpass

from sleekxmpp import ClientXMPP
from sleekxmpp.exceptions import IqError, IqTimeout
from module import XMPPModule
from collections import OrderedDict


import sys
sys.path.append(".")
import module


class Bot(ClientXMPP):

	jid = ""
	rooms = []
	modules = OrderedDict()
	modavail = {}
	config = None
	mucusers = {}

	def __init__(self, pwd):
		self.load_config()

		ClientXMPP.__init__(self,self.jid,pwd)

		self.add_event_handler("session_start", self.session_start)
		self.add_event_handler("message", self.message)
		self.add_event_handler("groupchat_message", self.groupmsg)
		self.add_event_handler("groupchat_presence", self.muc_presence)

		self.load_modules()


	def load_config(self):
		with open("config.json","r") as f:
			self.config = json.loads(f.read())

		self.jid = self.config["jid"] if "jid" in self.config.keys() else None # TODO: Make this an error

		self.rooms = []
		if "muc" in self.config.keys():
			for r in self.config["muc"]:
				self.rooms.append((r["room"],r["nick"]))
				self.mucusers[r["room"]] = {}

	def write_config(self):
		with open("config.json", "w") as f:
			f.write(json.dumps(self.config, indent=4, sort_keys=True))


	def load_module_registry(self):
		self.modavail = {}
		os.chdir("./modules")

		for d in os.listdir('.'):
			if not os.path.isdir(d) or d[0] == '.':
				continue
			try:
				with open(d + "/module.json", "r") as f:
					foo = json.loads(f.read())

				# TODO: Check for conflictions
				for m in foo["modules"]:
					self.modavail[m["name"]] = "./modules/" + d + "/" + m["path"]

			except Exception as e:
				print("Error loading from '{}': ".format(d) + str(e))
		os.chdir('..')

	def load_module(self, name):
		if name not in self.modavail.keys():
			print("Module '{}' not in registry".format(name))
			return False

		path = self.modavail[name]

		file,pathname,description = imp.find_module(path[:-3])
		try:
			mod = imp.load_module(path[:-3],file,pathname,description)
		except Exception as e:
			print("Failed to load module '{}': ".format(name) + str(e))
			return False

		for name, obj in inspect.getmembers(mod):
			if inspect.isclass(obj) and issubclass(obj,XMPPModule) and name != "XMPPModule":
				if name in self.config["modules"]:
					self.modules[name] = obj(self)

		# OPTIMIZE: Make this not sort for every module loaded
		self.modules = OrderedDict(sorted(self.modules.items(), key = lambda x: x[1].priority))	

		return True


	def load_modules(self):
		self.load_module_registry()

		for m in self.config["modules"]:
			if not self.load_module(m):
				print("Warning: module {} exists in config, but was not loaded!".format(m))
<commit_msg>fixed that stupid bug again<commit_after>#!/usr/bin/env python3
import inspect
import os
import imp
import logging
import time
import json
import getpass

from sleekxmpp import ClientXMPP
from sleekxmpp.exceptions import IqError, IqTimeout
from module import XMPPModule
from collections import OrderedDict


import sys
sys.path.append(".")
import module


class Bot(ClientXMPP):

	jid = ""
	rooms = []
	modules = OrderedDict()
	modavail = {}
	config = None
	mucusers = {}

	def __init__(self, pwd):
		self.load_config()

		ClientXMPP.__init__(self,self.jid,pwd)

		self.add_event_handler("session_start", self.session_start)
		self.add_event_handler("message", self.message)
		self.add_event_handler("groupchat_message", self.groupmsg)
		self.add_event_handler("groupchat_presence", self.muc_presence)

		self.load_modules()


	def load_config(self):
		with open("config.json","r") as f:
			self.config = json.loads(f.read())

		self.jid = self.config["jid"] if "jid" in self.config.keys() else None # TODO: Make this an error

		self.rooms = []
		if "muc" in self.config.keys():
			for r in self.config["muc"]:
				self.rooms.append((r["room"],r["nick"]))
				self.mucusers[r["room"]] = {}

	def write_config(self):
		with open("config.json", "w") as f:
			f.write(json.dumps(self.config, indent=4, sort_keys=True))


	def load_module_registry(self):
		self.modavail = {}
		os.chdir("./modules")

		for d in os.listdir('.'):
			if not os.path.isdir(d) or d[0] == '.':
				continue
			try:
				with open(d + "/module.json", "r") as f:
					foo = json.loads(f.read())

				# TODO: Check for conflictions
				for m in foo["modules"]:
					self.modavail[m["name"]] = "./modules/" + d + "/" + m["path"]

			except Exception as e:
				print("Error loading from '{}': ".format(d) + str(e))
		os.chdir('..')

	def load_module(self, name):
		if name not in self.modavail.keys():
			print("Module '{}' not in registry".format(name))
			return False

		path = self.modavail[name]

		file,pathname,description = imp.find_module(path[:-3])
		try:
			mod = imp.load_module(path[:-3],file,pathname,description)
		except Exception as e:
			print("Failed to load module '{}': ".format(name) + str(e))
			return False

		for name, obj in inspect.getmembers(mod):
			if inspect.isclass(obj) and issubclass(obj,XMPPModule) and name != "XMPPModule":
				if name in self.config["modules"]:
					self.modules[name] = obj(self)

		# OPTIMIZE: Make this not sort for every module loaded
		self.modules = OrderedDict(sorted(self.modules.items(), key = lambda x: x[1].priority))	

		return True


	def load_modules(self):
		for m in self.modules.items():
			m[1].deinit()


		self.load_module_registry()

		for m in self.config["modules"]:
			if not self.load_module(m):
				print("Warning: module {} exists in config, but was not loaded!".format(m))

{'fraction_non_alphanumeric': 0.15257879656160458, 'fraction_numerical': 0.002328080229226361, 'mean_word_length': 4.02248201438849, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1279424', 'n_tokens_mistral': 2200, 'n_tokens_neox': 1945, 'n_words': 481}