utils: Remove dependency on parent package.

This commit is contained in:
Valentin Lorentz 2015-08-30 17:33:39 +02:00
parent 0c6a88c4ca
commit c01a956a8b
11 changed files with 46 additions and 32 deletions

View File

@ -32,6 +32,7 @@ import time
import socket import socket
import telnetlib import telnetlib
import supybot.conf as conf
import supybot.utils as utils import supybot.utils as utils
from supybot.commands import * from supybot.commands import *
from supybot.utils.iter import any from supybot.utils.iter import any
@ -88,7 +89,10 @@ class Internet(callbacks.Plugin):
irc.errorInvalid(_('domain')) irc.errorInvalid(_('domain'))
return return
try: try:
sock = utils.net.getSocket('%s.whois-servers.net' % usertld) sock = utils.net.getSocket('%s.whois-servers.net' % usertld,
vhost=conf.supybot.protocols.irc.vhost(),
vhostv6=conf.supybot.protocols.irc.vhostv6(),
)
sock.connect(('%s.whois-servers.net' % usertld, 43)) sock.connect(('%s.whois-servers.net' % usertld, 43))
except socket.error as e: except socket.error as e:
irc.error(str(e)) irc.error(str(e))

View File

@ -74,7 +74,9 @@ try:
except OSError: # Does not exist except OSError: # Does not exist
pass pass
fd = open(os.path.join('src', 'version.py'), 'a') fd = open(os.path.join('src', 'version.py'), 'a')
fd.write('import supybot.utils.python\n')
fd.write("version = '0.83.4.1+limnoria %s'\n" % version) fd.write("version = '0.83.4.1+limnoria %s'\n" % version)
fd.write('supybot.utils.python._debug_software_version = version\n')
fd.close() fd.close()
if sys.version_info < (2, 6, 0): if sys.version_info < (2, 6, 0):

View File

@ -30,7 +30,12 @@
from . import dynamicScope from . import dynamicScope
from . import i18n
builtins = (__builtins__ if isinstance(__builtins__, dict) else __builtins__.__dict__)
builtins['supybotInternationalization'] = i18n.PluginInternationalization()
from . import utils from . import utils
del builtins['supybotInternationalization']
(__builtins__ if isinstance(__builtins__, dict) else __builtins__.__dict__)['format'] = utils.str.format (__builtins__ if isinstance(__builtins__, dict) else __builtins__.__dict__)['format'] = utils.str.format

View File

@ -370,6 +370,12 @@ registerGroup(supybot, 'reply')
registerGroup(supybot.reply, 'format') registerGroup(supybot.reply, 'format')
registerChannelValue(supybot.reply.format, 'url', registerChannelValue(supybot.reply.format, 'url',
registry.String('<%s>', _("""Determines how urls should be formatted."""))) registry.String('<%s>', _("""Determines how urls should be formatted.""")))
def url(s):
if s:
return supybot.reply.format.url() % s
else:
return ''
utils.str.url = url
registerChannelValue(supybot.reply.format, 'time', registerChannelValue(supybot.reply.format, 'time',
registry.String('%Y-%m-%dT%H:%M:%S%z', _("""Determines how timestamps registry.String('%Y-%m-%dT%H:%M:%S%z', _("""Determines how timestamps
printed for human reading should be formatted. Refer to the Python printed for human reading should be formatted. Refer to the Python

View File

@ -263,7 +263,10 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin):
drivers.log.connect(self.currentServer) drivers.log.connect(self.currentServer)
try: try:
self.conn = utils.net.getSocket(address, port=port, self.conn = utils.net.getSocket(address, port=port,
socks_proxy=socks_proxy) socks_proxy=socks_proxy,
vhost=conf.supybot.protocols.irc.vhost(),
vhostv6=conf.supybot.protocols.irc.vhostv6(),
)
except socket.error as e: except socket.error as e:
drivers.log.connectError(self.currentServer, e) drivers.log.connectError(self.currentServer, e)
self.scheduleReconnect() self.scheduleReconnect()

View File

@ -48,13 +48,17 @@ def split(s):
csv.join = join csv.join = join
csv.split = split csv.split = split
builtins = (__builtins__ if isinstance(__builtins__, dict) else __builtins__.__dict__)
# We use this often enough that we're going to stick it in builtins. # We use this often enough that we're going to stick it in builtins.
def force(x): def force(x):
if callable(x): if callable(x):
return x() return x()
else: else:
return x return x
(__builtins__ if isinstance(__builtins__, dict) else __builtins__.__dict__)['force'] = force builtins['force'] = force
internationalization = builtins.get('supybotInternationalization', None)
# These imports need to happen below the block above, so things get put into # These imports need to happen below the block above, so things get put into
# __builtins__ appropriately. # __builtins__ appropriately.

View File

@ -47,9 +47,7 @@ from .str import format
from .file import mktemp from .file import mktemp
from .iter import imap from .iter import imap
from . import minisix from . import minisix
from . import internationalization as _
from supybot.i18n import PluginInternationalization
_ = PluginInternationalization()
def warn_non_constant_time(f): def warn_non_constant_time(f):
@functools.wraps(f) @functools.wraps(f)

View File

@ -32,6 +32,7 @@
from __future__ import division from __future__ import division
import sys import sys
import warnings
if sys.version_info[0] >= 3: if sys.version_info[0] >= 3:
PY2 = False PY2 = False
@ -85,8 +86,7 @@ else:
L = lambda x:long(x) L = lambda x:long(x)
def make_datetime_utc(dt): def make_datetime_utc(dt):
from .. import log warnings.warn('Timezones are not available on this version of '
log.warning('Timezones are not available on this version of '
'Python and may lead to incorrect results. You should ' 'Python and may lead to incorrect results. You should '
'consider upgrading to Python 3.') 'consider upgrading to Python 3.')
return dt.replace(tzinfo=None) return dt.replace(tzinfo=None)
@ -98,9 +98,8 @@ else:
return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6
def datetime__timestamp(dt): def datetime__timestamp(dt):
from .. import log
import datetime import datetime
log.warning('Timezones are not available on this version of ' warnings.warn('Timezones are not available on this version of '
'Python and may lead to incorrect results. You should ' 'Python and may lead to incorrect results. You should '
'consider upgrading to Python 3.') 'consider upgrading to Python 3.')
return timedelta__totalseconds(dt - datetime.datetime(1970, 1, 1)) return timedelta__totalseconds(dt - datetime.datetime(1970, 1, 1))

View File

@ -62,18 +62,13 @@ def getSocket(host, port=None, socks_proxy=None, vhost=None, vhostv6=None):
s.setproxy(socks.PROXY_TYPE_SOCKS5, hostname, int(port), s.setproxy(socks.PROXY_TYPE_SOCKS5, hostname, int(port),
rdns=True) rdns=True)
return s return s
import supybot.conf as conf
if isIPV4(host): if isIPV4(host):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if not vhost:
vhost = conf.supybot.protocols.irc.vhost()
if vhost: if vhost:
s.bind((vhost, 0)) s.bind((vhost, 0))
return s return s
elif isIPV6(host): elif isIPV6(host):
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
if not vhostv6:
vhostv6 = conf.supybot.protocols.irc.vhostv6()
if vhostv6: if vhostv6:
s.bind((vhostv6, 0)) s.bind((vhostv6, 0))
return s return s

View File

@ -116,6 +116,8 @@ def glob2re(g):
return fnmatch.translate(g)[:-7] return fnmatch.translate(g)[:-7]
_debug_software_name = 'Limnoria'
_debug_software_version = None
# From http://code.activestate.com/recipes/52215-get-more-information-from-tracebacks/ # From http://code.activestate.com/recipes/52215-get-more-information-from-tracebacks/
def collect_extra_debug_data(): def collect_extra_debug_data():
""" """
@ -133,11 +135,11 @@ def collect_extra_debug_data():
finally: finally:
del tb del tb
try: if _debug_software_version:
from supybot.version import version data += '%s version: %s\n\n' % \
data += 'Supybot version: %s\n\n' % version (_debug_software_name, _debug_software_version)
except: else:
data += '(Cannot get Supybot version.)\n\n' data += '(Cannot get %s version.)\n\n' % _debug_software_name
data += 'Locals by frame, innermost last:\n' data += 'Locals by frame, innermost last:\n'
for frame in stack: for frame in stack:

View File

@ -43,8 +43,7 @@ from . import minisix
from .iter import all, any from .iter import all, any
from .structures import TwoWayDictionary from .structures import TwoWayDictionary
from supybot.i18n import PluginInternationalization from . import internationalization as _
_ = PluginInternationalization()
internationalizeFunction = _.internationalizeFunction internationalizeFunction = _.internationalizeFunction
try: try:
@ -480,6 +479,8 @@ def timestamp(t):
if t is None: if t is None:
t = time.time() t = time.time()
return time.ctime(t) return time.ctime(t)
def url(url):
return url
_formatRe = re.compile('%((?:\d+)?\.\d+f|[bfhiLnpqrsStTuv%])') _formatRe = re.compile('%((?:\d+)?\.\d+f|[bfhiLnpqrsStTuv%])')
def format(s, *args, **kwargs): def format(s, *args, **kwargs):
@ -578,12 +579,7 @@ def format(s, *args, **kwargs):
from .gen import timeElapsed from .gen import timeElapsed
return timeElapsed(args.pop()) return timeElapsed(args.pop())
elif char == 'u': elif char == 'u':
import supybot.conf as conf return url(args.pop())
url = args.pop()
if url:
return conf.supybot.reply.format.url() % url
else:
return ''
elif char == 'v': elif char == 'v':
args.pop() args.pop()
return '' return ''