From 63577088d874292dd0376e4a2a6fb63ac2941287 Mon Sep 17 00:00:00 2001
From: Sascha Silbe <sascha@silbe.org>
Date: Mon, 18 Jan 2010 18:18:28 +0000
Subject: [PATCH 2/2] PEP8 fixes
---
src/sugar/__init__.py | 4 +-
src/sugar/logger.py | 49 ++++++++++++++++-------------
src/sugar/mime.py | 81 +++++++++++++++++++++++++++++++------------------
3 files changed, 80 insertions(+), 54 deletions(-)
diff --git a/src/sugar/__init__.py b/src/sugar/__init__.py
index e915b38..d24d665 100644
a
|
b
|
|
19 | 19 | import os |
20 | 20 | import gettext |
21 | 21 | |
22 | | if os.environ.has_key('SUGAR_PREFIX'): |
| 22 | |
| 23 | if 'SUGAR_PREFIX' in os.environ: |
23 | 24 | prefix = os.environ['SUGAR_PREFIX'] |
24 | 25 | else: |
25 | 26 | prefix = '/usr' |
… |
… |
else: |
27 | 28 | locale_path = os.path.join(prefix, 'share', 'locale') |
28 | 29 | |
29 | 30 | gettext.bindtextdomain('sugar-base', locale_path) |
30 | | |
diff --git a/src/sugar/logger.py b/src/sugar/logger.py
index 226fb4b..275c57d 100644
a
|
b
|
import decorator |
34 | 34 | |
35 | 35 | # traces function calls, use SUGAR_LOGGER_LEVEL=trace to enable |
36 | 36 | TRACE = 5 |
| 37 | _LEVELS = { |
| 38 | 'error': logging.ERROR, |
| 39 | 'warning': logging.WARNING, |
| 40 | 'debug': logging.DEBUG, |
| 41 | 'info': logging.INFO, |
| 42 | 'trace': TRACE, |
| 43 | 'all': 0, |
| 44 | } |
37 | 45 | logging.addLevelName(TRACE, 'TRACE') |
38 | 46 | |
| 47 | |
39 | 48 | def get_logs_dir(): |
40 | 49 | profile = os.environ.get('SUGAR_PROFILE', 'default') |
41 | 50 | logs_dir = os.environ.get('SUGAR_LOGS_DIR', |
… |
… |
def get_logs_dir(): |
43 | 52 | '.sugar', profile, 'logs')) |
44 | 53 | return logs_dir |
45 | 54 | |
46 | | _LEVELS = { 'error' : logging.ERROR, |
47 | | 'warning' : logging.WARNING, |
48 | | 'debug' : logging.DEBUG, |
49 | | 'info' : logging.INFO, |
50 | | 'trace' : TRACE, |
51 | | 'all' : 0, |
52 | | } |
| 55 | |
53 | 56 | def set_level(level): |
54 | 57 | if level in _LEVELS: |
55 | 58 | logging.getLogger('').setLevel(_LEVELS[level]) |
… |
… |
def _except_hook(exctype, value, traceback): |
67 | 70 | # Importing IPython is slow, so we import it lazily. |
68 | 71 | try: |
69 | 72 | from IPython.ultraTB import AutoFormattedTB |
70 | | sys.excepthook = AutoFormattedTB(mode='Verbose', color_scheme='NoColor') |
| 73 | sys.excepthook = AutoFormattedTB(mode='Verbose', |
| 74 | color_scheme='NoColor') |
71 | 75 | except ImportError: |
72 | 76 | sys.excepthook = sys.__excepthook__ |
73 | 77 | |
74 | 78 | sys.excepthook(exctype, value, traceback) |
75 | 79 | |
| 80 | |
76 | 81 | def start(log_filename=None): |
77 | 82 | # remove existing handlers, or logging.basicConfig() won't have no effect. |
78 | 83 | root_logger = logging.getLogger('') |
… |
… |
def start(log_filename=None): |
106 | 111 | format="%(created)f %(levelname)s %(name)s: %(message)s", |
107 | 112 | stream=SafeLogWrapper(sys.stderr)) |
108 | 113 | |
109 | | if os.environ.has_key('SUGAR_LOGGER_LEVEL'): |
| 114 | if 'SUGAR_LOGGER_LEVEL' in os.environ: |
110 | 115 | set_level(os.environ['SUGAR_LOGGER_LEVEL']) |
111 | 116 | |
112 | 117 | if log_filename: |
… |
… |
class TraceRepr(repr_.Repr): |
133 | 138 | # better handling of subclasses of basic types, e.g. for DBus |
134 | 139 | _TYPES = [int, long, bool, tuple, list, array.array, set, frozenset, |
135 | 140 | collections.deque, dict, str] |
| 141 | |
136 | 142 | def repr1(self, x, level): |
137 | 143 | for t in self._TYPES: |
138 | 144 | if isinstance(x, t): |
… |
… |
def trace(logger=None, logger_name=None, skip_args=None, skip_kwargs=None, |
167 | 173 | def _trace(f, *args, **kwargs): |
168 | 174 | # don't do expensive formatting if loglevel TRACE is not enabled |
169 | 175 | enabled = trace_logger.isEnabledFor(TRACE) |
170 | | if enabled: |
171 | | params_formatted = ", ".join( |
172 | | [trace_repr.repr(a) |
173 | | for (idx, a) in enumerate(args) if idx not in skip_args] + \ |
174 | | ['%s=%s' % (k,trace_repr.repr(v)) |
175 | | for (k,v) in kwargs.items() if k not in skip_kwargs]) |
| 176 | if not enabled: |
| 177 | return f(*args, **kwargs) |
| 178 | |
| 179 | params_formatted = ", ".join( |
| 180 | [trace_repr.repr(a) |
| 181 | for (idx, a) in enumerate(args) if idx not in skip_args] + \ |
| 182 | ['%s=%s' % (k, trace_repr.repr(v)) |
| 183 | for (k, v) in kwargs.items() if k not in skip_kwargs]) |
176 | 184 | |
177 | | trace_logger.log(TRACE, "%s(%s) invoked", f.__name__, |
178 | | params_formatted) |
| 185 | trace_logger.log(TRACE, "%s(%s) invoked", f.__name__, |
| 186 | params_formatted) |
179 | 187 | |
180 | 188 | try: |
181 | 189 | res = f(*args, **kwargs) |
… |
… |
def trace(logger=None, logger_name=None, skip_args=None, skip_kwargs=None, |
183 | 191 | trace_logger.exception("Exception occured in %s", f.__name__) |
184 | 192 | raise |
185 | 193 | |
186 | | if enabled: |
187 | | trace_logger.log(TRACE, "%s(%s) returned %s", f.__name__, |
188 | | params_formatted, trace_repr.repr(res)) |
| 194 | trace_logger.log(TRACE, "%s(%s) returned %s", f.__name__, |
| 195 | params_formatted, trace_repr.repr(res)) |
189 | 196 | |
190 | 197 | return res |
191 | 198 | |
192 | 199 | return decorator.decorator(_trace) |
193 | | |
194 | | |
diff --git a/src/sugar/mime.py b/src/sugar/mime.py
index e1a880a..7f3f5ff 100644
a
|
b
|
GENERIC_TYPE_LINK = 'Link' |
38 | 38 | _extensions = {} |
39 | 39 | _globs_timestamps = [] |
40 | 40 | _generic_types = [ |
41 | | { 'id' : GENERIC_TYPE_TEXT, |
42 | | 'name' : _('Text'), |
43 | | 'icon' : 'text-x-generic', |
44 | | 'types' : ['text/plain', 'text/rtf', 'application/pdf', |
45 | | 'application/x-pdf', 'text/html', |
46 | | 'application/vnd.oasis.opendocument.text', |
47 | | 'application/rtf', 'text/rtf', 'application/epub+zip'] |
| 41 | { |
| 42 | 'id': GENERIC_TYPE_TEXT, |
| 43 | 'name': _('Text'), |
| 44 | 'icon': 'text-x-generic', |
| 45 | 'types': [ |
| 46 | 'text/plain', 'text/rtf', 'application/pdf', 'application/x-pdf', |
| 47 | 'text/html', 'application/vnd.oasis.opendocument.text', |
| 48 | 'application/rtf', 'text/rtf', 'application/epub+zip'], |
48 | 49 | }, |
49 | | { 'id' : GENERIC_TYPE_IMAGE, |
50 | | 'name' : _('Image'), |
51 | | 'icon' : 'image-x-generic', |
52 | | 'types' : ['image/png', 'image/gif', 'image/jpeg'] |
| 50 | { |
| 51 | 'id': GENERIC_TYPE_IMAGE, |
| 52 | 'name': _('Image'), |
| 53 | 'icon': 'image-x-generic', |
| 54 | 'types': ['image/png', 'image/gif', 'image/jpeg'], |
53 | 55 | }, |
54 | | { 'id' : GENERIC_TYPE_AUDIO, |
55 | | 'name' : _('Audio'), |
56 | | 'icon' : 'audio-x-generic', |
57 | | 'types' : ['audio/ogg', 'audio/x-wav', 'audio/wav', 'audio/x-vorbis+ogg', |
58 | | 'audio/x-mpegurl', 'audio/mpegurl', 'audio/mpeg', 'audio/x-scpls'] |
| 56 | { |
| 57 | 'id': GENERIC_TYPE_AUDIO, |
| 58 | 'name': _('Audio'), |
| 59 | 'icon': 'audio-x-generic', |
| 60 | 'types': [ |
| 61 | 'audio/ogg', 'audio/x-wav', 'audio/wav', 'audio/x-vorbis+ogg', |
| 62 | 'audio/x-mpegurl', 'audio/mpegurl', 'audio/mpeg', 'audio/x-scpls'], |
59 | 63 | }, |
60 | | { 'id' : GENERIC_TYPE_VIDEO, |
61 | | 'name' : _('Video'), |
62 | | 'icon' : 'video-x-generic', |
63 | | 'types' : ['video/ogg', 'application/ogg', 'video/x-theora+ogg'] |
| 64 | { |
| 65 | 'id': GENERIC_TYPE_VIDEO, |
| 66 | 'name': _('Video'), |
| 67 | 'icon': 'video-x-generic', |
| 68 | 'types': ['video/ogg', 'application/ogg', 'video/x-theora+ogg'], |
64 | 69 | }, |
65 | | { 'id' : GENERIC_TYPE_LINK, |
66 | | 'name' : _('Link'), |
67 | | 'icon' : 'text-uri-list', |
68 | | 'types' : ['text/x-moz-url', 'text/uri-list'] |
| 70 | { |
| 71 | 'id': GENERIC_TYPE_LINK, |
| 72 | 'name': _('Link'), |
| 73 | 'icon': 'text-uri-list', |
| 74 | 'types': ['text/x-moz-url', 'text/uri-list'], |
69 | 75 | }] |
70 | 76 | |
| 77 | |
71 | 78 | class ObjectType(object): |
| 79 | |
72 | 80 | def __init__(self, type_id, name, icon, mime_types): |
73 | 81 | self.type_id = type_id |
74 | 82 | self.name = name |
75 | 83 | self.icon = icon |
76 | 84 | self.mime_types = mime_types |
77 | 85 | |
| 86 | |
78 | 87 | def get_generic_type(type_id): |
79 | 88 | types = get_all_generic_types() |
80 | 89 | for generic_type in types: |
81 | 90 | if type_id == generic_type.type_id: |
82 | 91 | return generic_type |
83 | 92 | |
| 93 | |
84 | 94 | def get_all_generic_types(): |
85 | 95 | types = [] |
86 | 96 | for generic_type in _generic_types: |
… |
… |
def get_all_generic_types(): |
89 | 99 | types.append(object_type) |
90 | 100 | return types |
91 | 101 | |
| 102 | |
92 | 103 | def get_for_file(file_name): |
93 | 104 | if file_name.startswith('file://'): |
94 | 105 | file_name = file_name[7:] |
… |
… |
def get_for_file(file_name): |
104 | 115 | |
105 | 116 | return mime_type |
106 | 117 | |
| 118 | |
107 | 119 | def get_from_file_name(file_name): |
108 | 120 | return _sugarbaseext.get_mime_type_from_file_name(file_name) |
109 | 121 | |
| 122 | |
110 | 123 | def get_mime_icon(mime_type): |
111 | 124 | generic_type = _get_generic_type_for_mime(mime_type) |
112 | 125 | if generic_type: |
… |
… |
def get_mime_icon(mime_type): |
114 | 127 | |
115 | 128 | return mime_type.replace('/', '-') |
116 | 129 | |
| 130 | |
117 | 131 | def get_mime_description(mime_type): |
118 | 132 | generic_type = _get_generic_type_for_mime(mime_type) |
119 | 133 | if generic_type: |
… |
… |
def get_mime_description(mime_type): |
122 | 136 | import gio |
123 | 137 | return gio.content_type_get_description(mime_type) |
124 | 138 | |
| 139 | |
125 | 140 | def get_mime_parents(mime_type): |
126 | 141 | return _sugarbaseext.list_mime_parents(mime_type) |
127 | 142 | |
| 143 | |
128 | 144 | def get_primary_extension(mime_type): |
129 | 145 | global _extensions |
130 | 146 | global _globs_timestamps |
… |
… |
def get_primary_extension(mime_type): |
174 | 190 | else: |
175 | 191 | return None |
176 | 192 | |
177 | | _black_list = [ |
178 | | # Target used only between gtk.TextBuffer instances |
179 | | 'application/x-gtk-text-buffer-rich-text', |
180 | | ] |
| 193 | |
| 194 | _MIME_TYPE_BLACK_LIST = [ |
| 195 | # Target used only between gtk.TextBuffer instances |
| 196 | 'application/x-gtk-text-buffer-rich-text', |
| 197 | ] |
| 198 | |
181 | 199 | |
182 | 200 | def choose_most_significant(mime_types): |
183 | 201 | logging.debug('Choosing between %r.', mime_types) |
… |
… |
def choose_most_significant(mime_types): |
191 | 209 | for mime_type in mime_types: |
192 | 210 | |
193 | 211 | if mime_type.startswith(mime_category) and \ |
194 | | mime_type not in _black_list: |
| 212 | mime_type not in _MIME_TYPE_BLACK_LIST: |
195 | 213 | # skip mozilla private types (second component starts with '_' |
196 | | # or ends with '-priv') |
| 214 | # or ends with '-priv') |
197 | 215 | if mime_type.split('/')[1].startswith('_') or \ |
198 | 216 | mime_type.split('/')[1].endswith('-priv'): |
199 | 217 | continue |
… |
… |
def choose_most_significant(mime_types): |
218 | 236 | logging.debug('Returning first: %r.', mime_types[0]) |
219 | 237 | return mime_types[0] |
220 | 238 | |
| 239 | |
221 | 240 | def split_uri_list(uri_list): |
222 | 241 | return _sugarbaseext.uri_list_extract_uris(uri_list) |
223 | 242 | |
| 243 | |
224 | 244 | def _file_looks_like_text(file_name): |
225 | 245 | f = open(file_name, 'r') |
226 | 246 | try: |
… |
… |
def _file_looks_like_text(file_name): |
240 | 260 | |
241 | 261 | return False |
242 | 262 | |
| 263 | |
243 | 264 | def _get_generic_type_for_mime(mime_type): |
244 | 265 | for generic_type in _generic_types: |
245 | 266 | if mime_type in generic_type['types']: |