-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shared.py
88 lines (81 loc) · 3.08 KB
/
Shared.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Pythagore ("2.0")
# A Python IRC Bot
#
# Shared.py : shared utility classes and functions
#
# Copyright © 2008 Guillaume Seguin <[email protected]>
# Copyright © 2008, 2009 Nicolas Dandrimont <[email protected]>
#
# This file is part of Pythagore.
#
# Pythagore is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License, version 2, as
# published by the Free Software Foundation.
#
# Pythagore is distributed in the hope it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# of FITNESS FOR ANY PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pythagore; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""Shared.py: shared utilities"""
import sqlalchemy as sa
class NoCaseDict(dict):
"""A case-insensitive dictionnary, for use on IRC"""
def __getitem__(self, key):
if isinstance(key, basestring):
key = key.lower()
return super(NoCaseDict, self).__getitem__(key)
def __setitem__(self, key, value):
if isinstance(key, basestring):
key = key.lower()
return super(NoCaseDict, self).__setitem__(key, value)
def __delitem__(self, key):
if isinstance(key, basestring):
key = key.lower()
return super(NoCaseDict, self).__delitem__(key)
def __contains__(self, key):
if isinstance(key, basestring):
key = key.lower()
return super(NoCaseDict, self).__contains__(key)
class SASession(object):
"""Context Manager for clean SQLAlchemy session handling."""
def __init__(self, pythagore):
self.sess = pythagore.sessionmaker()
def __enter__(self):
return self.sess
def __exit__(self, exc_type, exc_val, tb):
if exc_type is None:
self.sess.commit()
self.sess.close()
else:
if isinstance(exc_type, sa.exceptions.SQLAlchemyError):
self.sess.rollback()
self.sess.close()
def to_unicode(txt, should_be="UTF-8", may_be=("ISO8859-15",)):
"""Convert the txt object to a unicode object"""
if hasattr(txt, "__unicode__"):
return unicode(txt)
if isinstance(txt, unicode):
return txt
else:
try:
# We suppose the text is in the "should_be" encoding
return txt.decode(should_be)
except UnicodeDecodeError:
# else we try all encodings in "may_be"
for encoding in may_be:
try:
return txt.decode(encoding)
except UnicodeDecodeError:
continue
else:
# We don't know what to do, so we go for a failproof solution
print ("%r <- decoding failed with encodings "
"%s and %s") % txt, should_be, ','.join(may_be)
return repr(txt).decode()