Commits

Jason R. Coombs committed 0609ea4

Moved storage finalization into storage module

Comments (0)

Files changed (8)

 		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'
 import re
 import random
 
-import pmxbot.core
+import pmxbot
 from . import storage
 from .core import command
 
 	@classmethod
 	def initialize(cls):
 		cls.store = cls.from_URI(pmxbot.config.database)
-		pmxbot.core._finalizers.append(cls.finalize)
+		cls._finalizers.append(cls.finalize)
 
 	@classmethod
 	def finalize(cls):

pmxbot/logging.py

 
 import pytz
 
-import pmxbot.core
+import pmxbot
 from . import storage
 from pmxbot.core import command, NoLog
 
 	@classmethod
 	def initialize(cls):
 		cls.store = cls.from_URI(pmxbot.config.database)
-		pmxbot.core._finalizers.append(cls.finalize)
+		cls._finalizers.append(cls.finalize)
 
 	@classmethod
 	def finalize(cls):
 
 import time
 
-import pmxbot.core
+import pmxbot
 from . import storage
 from .core import command, on_join
 
     @classmethod
     def init(cls):
         cls.store = cls.from_URI(pmxbot.config.database)
-        pmxbot.core._finalizers.append(cls.finalize)
+        cls._finalizers.append(cls.finalize)
 
     @classmethod
     def finalize(cls):
 
 import random
 
-import pmxbot.core
+import pmxbot
 from . import storage
 from .core import command
 
 	@classmethod
 	def initialize(cls):
 		cls.store = cls.from_URI(pmxbot.config.database)
-		pmxbot.core._finalizers.append(cls.finalize)
+		cls._finalizers.append(cls.finalize)
 
 	@classmethod
 	def finalize(cls):
 	"""
 
 	def __init__(self):
-		import pmxbot.pmxbot
 		self.feed_interval = pmxbot.config.feed_interval
 		self.feeds = pmxbot.config.feeds
 		self.store = FeedparserDB.from_URI(pmxbot.config.database)
 		timer = timing.Stopwatch()
 		self.seen_feeds = set(self.store.get_seen_feeds())
 		log.info("Loaded feed history in %s", timer.split())
-		pmxbot.pmxbot._finalizers.append(self.finalize)
+		storage.SelectableStorage._finalizers.append(self.finalize)
 
 	def finalize(self):
 		del self.store

pmxbot/storage.py

 from __future__ import absolute_import
 
+import abc
 import itertools
 import urlparse
 import importlib
+import logging
 
 from jaraco.util.classutil import itersubclasses
 
+log = logging.getLogger(__name__)
+
 class SelectableStorage(object):
 	"""
 	A mix-in for storage classes which will construct a suitable subclass based
 	on the URI.
 	"""
+
+	_finalizers = []
+
 	@classmethod
 	def from_URI(cls, URI):
 		candidates = itersubclasses(cls)
 		dest = cls.from_URI(dest_uri)
 		map(dest.import_, source.export_all())
 
+	@classmethod
+	def finalize(cls):
+		"Delete the various persistence objects"
+		for finalizer in cls._finalizers:
+			try:
+				finalizer()
+			except Exception:
+				log.exception("Error in finalizer %s", finalizer)
+
+
 class Storage(object):
-	# ABC
 	@classmethod
-	def uri_matches(cls, uri): return False
+	@abc.abstractmethod
+	def uri_matches(cls, uri):
+		return False
+
 
 class SQLiteStorage(Storage):
 	scheme = 'sqlite'

tests/unit/test_commands.py

 import popquotes.pmxbot
 
 import pmxbot.dictlib
+import pmxbot.storage
 from pmxbot import core
 from pmxbot import logging
 from pmxbot import commands
 
 	@classmethod
 	def teardown_class(cls):
-		pmxbot.core._cleanup()
+		pmxbot.storage.SelectableStorage.finalize()
 		path = os.path.dirname(os.path.abspath(__file__))
 		os.remove(os.path.join(path, 'pmxbot.sqlite'))