Commits

Jason R. Coombs committed 9b7d7f2

Moved remaining code from pmxbot.pmxbot to pmxbot.core

Comments (0)

Files changed (10)

 import textwrap
 import functools
 import importlib
+import argparse
+import logging as pylogging
 
 import irc.bot
+import pkg_resources
 
 import pmxbot.itertools
+import pmxbot.dictlib
 
 logging = None # pmxbot.logging module
 
+log = pylogging.getLogger('pmxbot')
+
 class WarnHistory(dict):
 	warn_every = datetime.timedelta(seconds=60)
 	warn_message = textwrap.dedent("""
 		_join_registry.append(func)
 		return func
 	return deco
+
+def get_args():
+	parser = argparse.ArgumentParser()
+	parser.add_argument('config', type=pmxbot.dictlib.ConfigDict.from_yaml)
+	return parser.parse_args()
+
+def run():
+	initialize(get_args().config).start()
+
+def initialize(config):
+	"""
+	Initialize the bot with a dictionary of config items
+	"""
+	pmxbot.config.update(config)
+	config = pmxbot.config
+
+	pylogging.basicConfig(level=pylogging.INFO, format="%(message)s")
+	_load_library_extensions()
+
+	class_ = SilentCommandBot if config.silent_bot else LoggingCommandBot
+
+	return class_(config.database, config.server_host, config.server_port,
+		config.bot_nickname, config.log_channels, config.other_channels,
+		use_ssl=config.use_ssl, password=config.password)
+
+_finalizers = []
+
+def _cleanup():
+	"Delete the various persistence objects"
+	for finalizer in _finalizers:
+		try:
+			finalizer()
+		except Exception:
+			log.exception("Error in finalizer %s", finalizer)
+
+def _load_library_extensions():
+	"""
+	Locate all setuptools entry points by the name 'pmxbot_handlers'
+	and initialize them.
+	Any third-party library may register an entry point by adding the
+	following to their setup.py::
+
+		entry_points = {
+			'pmxbot_handlers': [
+				'plugin_name = mylib.mymodule:initialize_func',
+			],
+		},
+
+	`plugin_name` can be anything, and is only used to display the name
+	of the plugin at initialization time.
+	"""
+	group = 'pmxbot_handlers'
+	entry_points = pkg_resources.iter_entry_points(group=group)
+	for ep in entry_points:
+		try:
+			log.info('Loading %s', ep.name)
+			init_func = ep.load()
+			if callable(init_func):
+				init_func()
+		except Exception:
+			log.exception("Error initializing plugin %s." % ep)
 import re
 import random
 
-import pmxbot
+import pmxbot.core
 from . import storage
 from .core import command
 
 class Karma(storage.SelectableStorage):
 	@classmethod
 	def initialize(cls):
-		import pmxbot.pmxbot
 		cls.store = cls.from_URI(pmxbot.config.database)
-		pmxbot.pmxbot._finalizers.append(cls.finalize)
+		pmxbot.core._finalizers.append(cls.finalize)
 
 	@classmethod
 	def finalize(cls):

pmxbot/logging.py

 
 import pytz
 
+import pmxbot.core
 from . import storage
 from pmxbot.core import command, NoLog
 
 
 	@classmethod
 	def initialize(cls):
-		import pmxbot.pmxbot
 		cls.store = cls.from_URI(pmxbot.config.database)
-		pmxbot.pmxbot._finalizers.append(cls.finalize)
+		pmxbot.core._finalizers.append(cls.finalize)
 
 	@classmethod
 	def finalize(cls):
 
 import time
 
-import pmxbot
+import pmxbot.core
 from . import storage
 from .core import command, on_join
 
 class Notify(storage.SelectableStorage):
     @classmethod
     def init(cls):
-        import pmxbot.pmxbot
         cls.store = cls.from_URI(pmxbot.config.database)
-        pmxbot.pmxbot._finalizers.append(cls.finalize)
+        pmxbot.core._finalizers.append(cls.finalize)
 
     @classmethod
     def finalize(cls):

pmxbot/pmxbot.py

-# -*- coding: utf-8 -*-
-# vim:ts=4:sw=4:noexpandtab
-# c-basic-indent: 4; tab-width: 4; indent-tabs-mode: true;
-from __future__ import absolute_import, division
-
-import argparse
-import logging
-
-import pmxbot
-from . import dictlib
-from . import core
-
-log = logging.getLogger(__name__)
-
-def get_args():
-	parser = argparse.ArgumentParser()
-	parser.add_argument('config', type=dictlib.ConfigDict.from_yaml)
-	return parser.parse_args()
-
-def run():
-	initialize(get_args().config).start()
-
-def initialize(config):
-	"""
-	Initialize the bot with a dictionary of config items
-	"""
-	pmxbot.config.update(config)
-
-	_setup_logging()
-	_load_library_extensions()
-
-	class_ = (core.LoggingCommandBot
-		if not pmxbot.config.silent_bot else core.SilentCommandBot)
-
-	return class_(config.database, config.server_host, config.server_port,
-		config.bot_nickname, config.log_channels, config.other_channels,
-		use_ssl=pmxbot.config.use_ssl, password=pmxbot.config.password)
-
-
-_finalizers = []
-
-def _cleanup():
-	"Delete the various persistence objects"
-	for finalizer in _finalizers:
-		try:
-			finalizer()
-		except Exception:
-			log.exception("Error in finalizer %s", finalizer)
-
-def _setup_logging():
-	logging.basicConfig(level=logging.INFO, format="%(message)s")
-
-def _load_library_extensions():
-	"""
-	Locate all setuptools entry points by the name 'pmxbot_handlers'
-	and initialize them.
-	Any third-party library may register an entry point by adding the
-	following to their setup.py::
-
-		entry_points = {
-			'pmxbot_handlers': [
-				'plugin_name = mylib.mymodule:initialize_func',
-			],
-		},
-
-	`plugin_name` can be anything, and is only used to display the name
-	of the plugin at initialization time.
-	"""
-
-	try:
-		import pkg_resources
-	except ImportError:
-		log.warning('setuptools not available - entry points cannot be '
-			'loaded')
-		return
-
-	group = 'pmxbot_handlers'
-	entry_points = pkg_resources.iter_entry_points(group=group)
-	for ep in entry_points:
-		try:
-			log.info('Loading %s', ep.name)
-			init_func = ep.load()
-			if callable(init_func):
-				init_func()
-		except Exception:
-			log.exception("Error initializing plugin %s." % ep)
 
 import random
 
-import pmxbot
+import pmxbot.core
 from . import storage
 from .core import command
 
 
 	@classmethod
 	def initialize(cls):
-		import pmxbot.pmxbot
 		cls.store = cls.from_URI(pmxbot.config.database)
-		pmxbot.pmxbot._finalizers.append(cls.finalize)
+		pmxbot.core._finalizers.append(cls.finalize)
 
 	@classmethod
 	def finalize(cls):

pmxbotweb/viewer.py

 import pytz
 from jaraco.util.numbers import ordinalth as th_it
 
+import pmxbot.core
 import pmxbot.logging
 import pmxbot.util
 
 		if not self.run:
 			self.commands = []
 			self.contains = []
-			import pmxbot.pmxbot as p
-			p.run(configInput = context['config'], start=False)
+			pmxbot.core.initialize(context['config'])
 			for typ, name, f, doc, channels, exclude, rate, priority in \
-					sorted(p._handler_registry, key=lambda x: x[1]):
+					sorted(pmxbot.core._handler_registry, key=lambda x: x[1]):
 				if typ == 'command':
 					aliases = sorted([x[1]
 						for x in p._handler_registry
 	include_package_data=True,
 	entry_points=dict(
 		console_scripts = [
-			'pmxbot=pmxbot.pmxbot:run',
+			'pmxbot=pmxbot.core:run',
 			'pmxbotweb=pmxbotweb.pmxbotweb:run',
 		],
 		pmxbot_handlers = [

tests/functional/__init__.py

 			#  the console entry point, which can't be properly
 			#  .terminate()d on Windows.
 			cls.bot = subprocess.Popen([sys.executable, '-c',
-				'from pmxbot.pmxbot import run; run()', configfile],
+				'from pmxbot.core import run; run()', configfile],
 				env=env)
 		except OSError:
 			py.test.skip("Unable to launch pmxbot (pmxbot must be installed)")

tests/unit/test_commands.py

 import pytest
 import popquotes.pmxbot
 
+import pmxbot.dictlib
 from pmxbot import core
 from pmxbot import logging
-from pmxbot import pmxbot
 from pmxbot import commands
 from pmxbot import karma
 from pmxbot import quotes
 		path = os.path.dirname(os.path.abspath(__file__))
 		configfile = os.path.join(path, 'testconf.yaml')
 		config = pmxbot.dictlib.ConfigDict.from_yaml(configfile)
-		cls.bot = pmxbot.initialize(config)
+		cls.bot = core.initialize(config)
 		logging.Logger.store.message("logged", "testrunner", "some text")
 
 	@classmethod
 	def teardown_class(cls):
-		pmxbot._cleanup()
+		pmxbot.core._cleanup()
 		path = os.path.dirname(os.path.abspath(__file__))
 		os.remove(os.path.join(path, 'pmxbot.sqlite'))