Browse Source

Implement lazy-importing of oursql and pytz.

tags/v0.2
Ben Kurtovic 12 years ago
parent
commit
ac6de461bb
2 changed files with 14 additions and 13 deletions
  1. +6
    -5
      earwigbot/commands/time_command.py
  2. +8
    -8
      earwigbot/wiki/site.py

+ 6
- 5
earwigbot/commands/time_command.py View File

@@ -24,10 +24,11 @@ from datetime import datetime
from math import floor from math import floor
from time import time from time import time


import pytz

from earwigbot import importer
from earwigbot.commands import Command from earwigbot.commands import Command


pytz = importer.new("pytz")

class Time(Command): class Time(Command):
"""Report the current time in any timezone (UTC default), or in beats.""" """Report the current time in any timezone (UTC default), or in beats."""
name = "time" name = "time"
@@ -52,12 +53,12 @@ class Time(Command):
self.reply(data, "@{0:0>3}".format(beats)) self.reply(data, "@{0:0>3}".format(beats))


def do_time(self, data, timezone): def do_time(self, data, timezone):
if not pytz:
try:
tzinfo = pytz.timezone(timezone)
except ImportError:
msg = "This command requires the 'pytz' module: http://pytz.sourceforge.net/" msg = "This command requires the 'pytz' module: http://pytz.sourceforge.net/"
self.reply(data, msg) self.reply(data, msg)
return return
try:
tzinfo = pytz.timezone(timezone)
except pytz.exceptions.UnknownTimeZoneError: except pytz.exceptions.UnknownTimeZoneError:
self.reply(data, "Unknown timezone: {0}.".format(timezone)) self.reply(data, "Unknown timezone: {0}.".format(timezone))
return return


+ 8
- 8
earwigbot/wiki/site.py View File

@@ -32,14 +32,14 @@ from urllib import quote_plus, unquote_plus
from urllib2 import build_opener, HTTPCookieProcessor, URLError from urllib2 import build_opener, HTTPCookieProcessor, URLError
from urlparse import urlparse from urlparse import urlparse


import oursql

from earwigbot import exceptions
from earwigbot import exceptions, importer
from earwigbot.wiki import constants from earwigbot.wiki import constants
from earwigbot.wiki.category import Category from earwigbot.wiki.category import Category
from earwigbot.wiki.page import Page from earwigbot.wiki.page import Page
from earwigbot.wiki.user import User from earwigbot.wiki.user import User


oursql = importer.new("oursql")

__all__ = ["Site"] __all__ = ["Site"]


class Site(object): class Site(object):
@@ -514,10 +514,6 @@ class Site(object):
may raise its own exceptions (e.g. oursql.InterfaceError) if it cannot may raise its own exceptions (e.g. oursql.InterfaceError) if it cannot
establish a connection. establish a connection.
""" """
if not oursql:
e = "Module 'oursql' is required for SQL queries."
raise exceptions.SQLError(e)

args = self._sql_data args = self._sql_data
for key, value in kwargs.iteritems(): for key, value in kwargs.iteritems():
args[key] = value args[key] = value
@@ -531,7 +527,11 @@ class Site(object):
if "autoreconnect" not in args: if "autoreconnect" not in args:
args["autoreconnect"] = True args["autoreconnect"] = True


self._sql_conn = oursql.connect(**args)
try:
self._sql_conn = oursql.connect(**args)
except ImportError:
e = "Module 'oursql' is required for SQL queries."
raise exceptions.SQLError(e)


def _get_service_order(self): def _get_service_order(self):
"""Return a preferred order for using services (e.g. the API and SQL). """Return a preferred order for using services (e.g. the API and SQL).


Loading…
Cancel
Save