Skip to content
Browse files

Restore python 3.2 compatibility

  • Loading branch information...
1 parent 99fece4 commit 004a9b32354acb1bd885b383bc394e24c39d72b5 @rnortman rnortman committed
Showing with 56 additions and 56 deletions.
  1. +2 −2 logbook/base.py
  2. +17 −17 logbook/handlers.py
  3. +7 −7 logbook/more.py
  4. +2 −2 logbook/notifiers.py
  5. +2 −2 logbook/queues.py
  6. +9 −9 logbook/ticketing.py
  7. +17 −17 tests/test_logbook.py
View
4 logbook/base.py
@@ -147,10 +147,10 @@ def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
- return u''
+ return u('')
else:
def __missing__(self, key):
- return u''
+ return u('')
def copy(self):
return self.__class__(self)
View
34 logbook/handlers.py
@@ -28,15 +28,15 @@
NOTSET, level_name_property, _missing, lookup_level, \
Flags, ContextObject, ContextStackManager
from logbook.helpers import rename, b, _is_text_stream, is_unicode, PY2, \
- zip, xrange, string_types, integer_types, reraise
+ zip, xrange, string_types, integer_types, reraise, u
DEFAULT_FORMAT_STRING = (
- u'[{record.time:%Y-%m-%d %H:%M}] '
- u'{record.level_name}: {record.channel}: {record.message}'
+ u('[{record.time:%Y-%m-%d %H:%M}] ') +
+ u('{record.level_name}: {record.channel}: {record.message}')
)
-SYSLOG_FORMAT_STRING = u'{record.channel}: {record.message}'
-NTLOG_FORMAT_STRING = u'''\
+SYSLOG_FORMAT_STRING = u('{record.channel}: {record.message}')
+NTLOG_FORMAT_STRING = u('''\
Message Level: {record.level_name}
Location: {record.filename}:{record.lineno}
Module: {record.module}
@@ -46,10 +46,10 @@
Event provided Message:
{record.message}
-'''
+''')
TEST_FORMAT_STRING = \
-u'[{record.level_name}] {record.channel}: {record.message}'
-MAIL_FORMAT_STRING = u'''\
+u('[{record.level_name}] {record.channel}: {record.message}')
+MAIL_FORMAT_STRING = u('''\
Subject: {handler.subject}
Message type: {record.level_name}
@@ -61,14 +61,14 @@
Message:
{record.message}
-'''
-MAIL_RELATED_FORMAT_STRING = u'''\
+''')
+MAIL_RELATED_FORMAT_STRING = u('''\
Message type: {record.level_name}
Location: {record.filename}:{record.lineno}
Module: {record.module}
Function: {record.func_name}
{record.message}
-'''
+''')
SYSLOG_PORT = 514
@@ -374,7 +374,7 @@ def __call__(self, record, handler):
line = self.format_record(record, handler)
exc = self.format_exception(record)
if exc:
- line += u'\n' + exc
+ line += u('\n') + exc
return line
@@ -420,7 +420,7 @@ def hash_record_raw(self, record):
"""Returns a hashlib object with the hash of the record."""
hash = sha1()
hash.update(('%d\x00' % record.level).encode('ascii'))
- hash.update((record.channel or u'').encode('utf-8') + b('\x00'))
+ hash.update((record.channel or u('')).encode('utf-8') + b('\x00'))
hash.update(record.filename.encode('utf-8') + b('\x00'))
hash.update(b(str(record.lineno)))
return hash
@@ -1032,7 +1032,7 @@ class MailHandler(Handler, StringFormatterHandlerMixin,
"""
default_format_string = MAIL_FORMAT_STRING
default_related_format_string = MAIL_RELATED_FORMAT_STRING
- default_subject = u'Server Error in Application'
+ default_subject = u('Server Error in Application')
#: the maximum number of record hashes in the cache for the limiting
#: feature. Afterwards, record_cache_prune percent of the oldest
@@ -1353,10 +1353,10 @@ def encode_priority(self, record):
return (facility << 3) | priority
def emit(self, record):
- prefix = u''
+ prefix = u('')
if self.application_name is not None:
- prefix = self.application_name + u':'
- self.send_to_socket((u'<%d>%s%s\x00' % (
+ prefix = self.application_name + u(':')
+ self.send_to_socket((u('<%d>%s%s\x00') % (
self.encode_priority(record),
prefix,
self.format(record)
View
14 logbook/more.py
@@ -17,7 +17,7 @@
from logbook.handlers import Handler, StringFormatter, \
StringFormatterHandlerMixin, StderrHandler
from logbook._termcolors import colorize
-from logbook.helpers import PY2, string_types, iteritems
+from logbook.helpers import PY2, string_types, iteritems, u
from logbook.ticketing import TicketingHandler as DatabaseHandler
from logbook.ticketing import BackendBase
@@ -29,7 +29,7 @@
_ws_re = re.compile(r'(\s+)(?u)')
TWITTER_FORMAT_STRING = \
-u'[{record.channel}] {record.level_name}: {record.message}'
+u('[{record.channel}] {record.level_name}: {record.message}')
TWITTER_ACCESS_TOKEN_URL = 'https://twitter.com/oauth/access_token'
NEW_TWEET_URL = 'https://api.twitter.com/1/statuses/update.json'
@@ -40,7 +40,7 @@ class CouchDBBackend(BackendBase):
def setup_backend(self):
from couchdb import Server
- uri = self.options.pop('uri', u'')
+ uri = self.options.pop('uri', u(''))
couch = Server(uri)
db_name = self.options.pop('db')
self.database = couch[db_name]
@@ -64,8 +64,8 @@ class TwitterFormatter(StringFormatter):
max_length = 140
def format_exception(self, record):
- return u'%s: %s' % (record.exception_shortname,
- record.exception_message)
+ return u('%s: %s') % (record.exception_shortname,
+ record.exception_message)
def __call__(self, record, handler):
formatted = StringFormatter.__call__(self, record, handler)
@@ -75,10 +75,10 @@ def __call__(self, record, handler):
length += len(piece)
if length > self.max_length:
if length - len(piece) < self.max_length:
- rv.append(u'')
+ rv.append(u(''))
break
rv.append(piece)
- return u''.join(rv)
+ return u('').join(rv)
class TaggingLogger(RecordDispatcher):
View
4 logbook/notifiers.py
@@ -45,7 +45,7 @@ def __init__(self, application_name=None, record_limit=None,
def make_title(self, record):
"""Called to get the title from the record."""
- return u'%s: %s' % (record.channel, record.level_name.title())
+ return u('%s: %s') % (record.channel, record.level_name.title())
def make_text(self, record):
"""Called to get the text of the record."""
@@ -219,7 +219,7 @@ def emit(self, record):
con = http_client.HTTPSConnection('boxcar.io')
con.request('POST', '/notifications/', headers={
'Authorization': 'Basic ' +
- base64.b64encode((u'%s:%s' %
+ base64.b64encode((u('%s:%s') %
(self.email, self.password)).encode('utf-8')).strip(),
}, body=body)
con.close()
View
4 logbook/queues.py
@@ -14,7 +14,7 @@
import platform
from logbook.base import NOTSET, LogRecord, dispatch_record
from logbook.handlers import Handler, WrapperHandler
-from logbook.helpers import PY2
+from logbook.helpers import PY2, u
if PY2:
from Queue import Empty, Queue as ThreadQueue
@@ -412,7 +412,7 @@ def __init__(self, uri=None, context=None, multi=False):
self.socket = self.context.socket(zmq.SUB)
if uri is not None:
self.socket.connect(uri)
- self.socket.setsockopt_unicode(zmq.SUBSCRIBE, u'')
+ self.socket.setsockopt_unicode(zmq.SUBSCRIBE, u(''))
def __del__(self):
try:
View
18 logbook/ticketing.py
@@ -13,7 +13,7 @@
import json
from logbook.base import NOTSET, level_name_property, LogRecord
from logbook.handlers import Handler, HashingHandlerMixin
-from logbook.helpers import cached_property, b, PY2
+from logbook.helpers import cached_property, b, PY2, u
class Ticket(object):
"""Represents a ticket from the database."""
@@ -192,9 +192,9 @@ def record_ticket(self, record, data, hash, app_id):
row = cnx.execute(self.tickets.insert().values(
record_hash=hash,
level=record.level,
- channel=record.channel or u'',
- location=u'%s:%d' % (record.filename, record.lineno),
- module=record.module or u'<unknown>',
+ channel=record.channel or u(''),
+ location=u('%s:%d') % (record.filename, record.lineno),
+ module=record.module or u('<unknown>'),
occurrence_count=0,
solved=False,
app_id=app_id
@@ -287,7 +287,7 @@ def setup_backend(self):
from pymongo.errors import AutoReconnect
_connection = None
- uri = self.options.pop('uri', u'')
+ uri = self.options.pop('uri', u(''))
_connection_attempts = 0
parsed_uri = parse_uri(uri, Connection.PORT)
@@ -336,9 +336,9 @@ def record_ticket(self, record, data, hash, app_id):
doc = {
'record_hash': hash,
'level': record.level,
- 'channel': record.channel or u'',
- 'location': u'%s:%d' % (record.filename, record.lineno),
- 'module': record.module or u'<unknown>',
+ 'channel': record.channel or u(''),
+ 'location': u('%s:%d') % (record.filename, record.lineno),
+ 'module': record.module or u('<unknown>'),
'occurrence_count': 0,
'solved': False,
'app_id': app_id,
@@ -448,7 +448,7 @@ def __init__(self, uri, app_id='generic', level=NOTSET,
filter=None, bubble=False, hash_salt=None, backend=None,
**db_options):
if hash_salt is None:
- hash_salt = u'apphash-' + app_id
+ hash_salt = u('apphash-') + app_id
TicketingBaseHandler.__init__(self, hash_salt, level, filter, bubble)
if backend is None:
backend = self.default_backend
View
34 tests/test_logbook.py
@@ -253,7 +253,7 @@ def test_file_handler(self):
def test_file_handler_unicode(self):
with capturing_stderr_context() as captured:
with self.thread_activation_strategy(logbook.FileHandler(self.filename)) as h:
- self.log.info(u'\u0431')
+ self.log.info(u('\u0431'))
self.assertFalse(captured.getvalue())
def test_file_handler_delay(self):
@@ -352,7 +352,7 @@ def fake_record(message, year, month, day, hour=0,
self.assertEqual(f.readline().rstrip(), '[02:00] Third One')
def test_mail_handler(self):
- subject = u'\xf8nicode'
+ subject = u('\xf8nicode')
handler = make_fake_mail_handler(subject=subject)
with capturing_stderr_context() as fallback:
with self.thread_activation_strategy(handler):
@@ -360,7 +360,7 @@ def test_mail_handler(self):
try:
1 / 0
except Exception:
- self.log.exception(u'Viva la Espa\xf1a')
+ self.log.exception(u('Viva la Espa\xf1a'))
if not handler.mails:
# if sending the mail failed, the reason should be on stderr
@@ -375,7 +375,7 @@ def test_mail_handler(self):
self.assertRegexpMatches(mail, 'Location:.*%s' % __file_without_pyc__)
self.assertRegexpMatches(mail, 'Module:\s+%s' % __name__)
self.assertRegexpMatches(mail, 'Function:\s+test_mail_handler')
- body = u'Message:\n\nViva la Espa\xf1a'
+ body = u('Message:\n\nViva la Espa\xf1a')
if sys.version_info < (3, 0):
body = body.encode('utf-8')
self.assertIn(body, mail)
@@ -478,8 +478,8 @@ def test_syslog_handler(self):
except socket.error:
self.fail('got timeout on socket')
self.assertEqual(rv, (
- u'<12>%stestlogger: Syslog is weird\x00' %
- (app_name and app_name + u':' or u'')).encode('utf-8'))
+ u('<12>%stestlogger: Syslog is weird\x00') %
+ (app_name and app_name + u(':') or u(''))).encode('utf-8'))
def test_handler_processors(self):
handler = make_fake_mail_handler(format_string='''\
@@ -1143,9 +1143,9 @@ def _get_zeromq(self, multi=False):
@require_module('zmq')
def test_zeromq_handler(self):
tests = [
- u'Logging something',
- u'Something with umlauts äöü',
- u'Something else for good measure',
+ u('Logging something'),
+ u('Something with umlauts äöü'),
+ u('Something else for good measure'),
]
handler, subscriber = self._get_zeromq()
for test in tests:
@@ -1158,9 +1158,9 @@ def test_zeromq_handler(self):
@require_module('zmq')
def test_multi_zeromq_handler(self):
tests = [
- u'Logging something',
- u'Something with umlauts äöü',
- u'Something else for good measure',
+ u('Logging something'),
+ u('Something with umlauts äöü'),
+ u('Something else for good measure'),
]
handlers, subscriber = self._get_zeromq(multi=True)
for handler in handlers:
@@ -1404,16 +1404,16 @@ def __str__(self):
rv = to_safe_json([
None,
'foo',
- u'jäger',
+ u('jäger'),
1,
datetime(2000, 1, 1),
- {'jäger1': 1, u'jäger2': 2, Bogus(): 3, 'invalid': object()},
+ {'jäger1': 1, u('jäger2'): 2, Bogus(): 3, 'invalid': object()},
object() # invalid
])
self.assertEqual(
- rv, [None, u'foo', u'jäger', 1, '2000-01-01T00:00:00Z',
- {u('jäger1'): 1, u'jäger2': 2, u'bogus': 3,
- u'invalid': None}, None])
+ rv, [None, u('foo'), u('jäger'), 1, '2000-01-01T00:00:00Z',
+ {u('jäger1'): 1, u('jäger2'): 2, u('bogus'): 3,
+ u('invalid'): None}, None])
def test_datehelpers(self):
from logbook.helpers import format_iso8601, parse_iso8601

0 comments on commit 004a9b3

Please sign in to comment.
Something went wrong with that request. Please try again.