Ticket #1663: 0002-PEP8-fixes.patch

File 0002-PEP8-fixes.patch, 10.1 KB (added by sascha_silbe, 11 years ago)

PEP8 fixes

  • src/sugar/__init__.py

    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  
    1919import os
    2020import gettext
    2121
    22 if os.environ.has_key('SUGAR_PREFIX'):
     22
     23if 'SUGAR_PREFIX' in os.environ:
    2324    prefix = os.environ['SUGAR_PREFIX']
    2425else:
    2526    prefix = '/usr'
    else: 
    2728locale_path = os.path.join(prefix, 'share', 'locale')
    2829
    2930gettext.bindtextdomain('sugar-base', locale_path)
    30 
  • src/sugar/logger.py

    diff --git a/src/sugar/logger.py b/src/sugar/logger.py
    index 226fb4b..275c57d 100644
    a b import decorator 
    3434
    3535# traces function calls, use SUGAR_LOGGER_LEVEL=trace to enable
    3636TRACE = 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}
    3745logging.addLevelName(TRACE, 'TRACE')
    3846
     47
    3948def get_logs_dir():
    4049    profile = os.environ.get('SUGAR_PROFILE', 'default')
    4150    logs_dir = os.environ.get('SUGAR_LOGS_DIR',
    def get_logs_dir(): 
    4352                                           '.sugar', profile, 'logs'))
    4453    return logs_dir
    4554
    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
    5356def set_level(level):
    5457    if level in _LEVELS:
    5558        logging.getLogger('').setLevel(_LEVELS[level])
    def _except_hook(exctype, value, traceback): 
    6770    # Importing IPython is slow, so we import it lazily.
    6871    try:
    6972        from IPython.ultraTB import AutoFormattedTB
    70         sys.excepthook = AutoFormattedTB(mode='Verbose', color_scheme='NoColor')
     73        sys.excepthook = AutoFormattedTB(mode='Verbose',
     74            color_scheme='NoColor')
    7175    except ImportError:
    7276        sys.excepthook = sys.__excepthook__
    7377
    7478    sys.excepthook(exctype, value, traceback)
    7579
     80
    7681def start(log_filename=None):
    7782    # remove existing handlers, or logging.basicConfig() won't have no effect.
    7883    root_logger = logging.getLogger('')
    def start(log_filename=None): 
    106111            format="%(created)f %(levelname)s %(name)s: %(message)s",
    107112                        stream=SafeLogWrapper(sys.stderr))
    108113
    109     if os.environ.has_key('SUGAR_LOGGER_LEVEL'):
     114    if 'SUGAR_LOGGER_LEVEL' in os.environ:
    110115        set_level(os.environ['SUGAR_LOGGER_LEVEL'])
    111116
    112117    if log_filename:
    class TraceRepr(repr_.Repr): 
    133138    # better handling of subclasses of basic types, e.g. for DBus
    134139    _TYPES = [int, long, bool, tuple, list, array.array, set, frozenset,
    135140        collections.deque, dict, str]
     141
    136142    def repr1(self, x, level):
    137143        for t in self._TYPES:
    138144            if isinstance(x, t):
    def trace(logger=None, logger_name=None, skip_args=None, skip_kwargs=None, 
    167173    def _trace(f, *args, **kwargs):
    168174        # don't do expensive formatting if loglevel TRACE is not enabled
    169175        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])
    176184
    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)
    179187
    180188        try:
    181189            res = f(*args, **kwargs)
    def trace(logger=None, logger_name=None, skip_args=None, skip_kwargs=None, 
    183191            trace_logger.exception("Exception occured in %s", f.__name__)
    184192            raise
    185193
    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))
    189196
    190197        return res
    191198
    192199    return decorator.decorator(_trace)
    193 
    194 
  • src/sugar/mime.py

    diff --git a/src/sugar/mime.py b/src/sugar/mime.py
    index e1a880a..7f3f5ff 100644
    a b GENERIC_TYPE_LINK = 'Link' 
    3838_extensions = {}
    3939_globs_timestamps = []
    4040_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'],
    4849},
    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'],
    5355},
    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'],
    5963},
    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'],
    6469},
    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'],
    6975}]
    7076
     77
    7178class ObjectType(object):
     79
    7280    def __init__(self, type_id, name, icon, mime_types):
    7381        self.type_id = type_id
    7482        self.name = name
    7583        self.icon = icon
    7684        self.mime_types = mime_types
    7785
     86
    7887def get_generic_type(type_id):
    7988    types = get_all_generic_types()
    8089    for generic_type in types:
    8190        if type_id == generic_type.type_id:
    8291            return generic_type
    8392
     93
    8494def get_all_generic_types():
    8595    types = []
    8696    for generic_type in _generic_types:
    def get_all_generic_types(): 
    8999        types.append(object_type)
    90100    return types
    91101
     102
    92103def get_for_file(file_name):
    93104    if file_name.startswith('file://'):
    94105        file_name = file_name[7:]
    def get_for_file(file_name): 
    104115
    105116    return mime_type
    106117
     118
    107119def get_from_file_name(file_name):
    108120    return _sugarbaseext.get_mime_type_from_file_name(file_name)
    109121
     122
    110123def get_mime_icon(mime_type):
    111124    generic_type = _get_generic_type_for_mime(mime_type)
    112125    if generic_type:
    def get_mime_icon(mime_type): 
    114127
    115128    return mime_type.replace('/', '-')
    116129
     130
    117131def get_mime_description(mime_type):
    118132    generic_type = _get_generic_type_for_mime(mime_type)
    119133    if generic_type:
    def get_mime_description(mime_type): 
    122136    import gio
    123137    return gio.content_type_get_description(mime_type)
    124138
     139
    125140def get_mime_parents(mime_type):
    126141    return _sugarbaseext.list_mime_parents(mime_type)
    127142
     143
    128144def get_primary_extension(mime_type):
    129145    global _extensions
    130146    global _globs_timestamps
    def get_primary_extension(mime_type): 
    174190    else:
    175191        return None
    176192
    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
    181199
    182200def choose_most_significant(mime_types):
    183201    logging.debug('Choosing between %r.', mime_types)
    def choose_most_significant(mime_types): 
    191209        for mime_type in mime_types:
    192210
    193211            if mime_type.startswith(mime_category) and \
    194                mime_type not in _black_list:
     212               mime_type not in _MIME_TYPE_BLACK_LIST:
    195213                # skip mozilla private types (second component starts with '_'
    196                 # or ends with '-priv') 
     214                # or ends with '-priv')
    197215                if mime_type.split('/')[1].startswith('_') or \
    198216                   mime_type.split('/')[1].endswith('-priv'):
    199217                    continue
    def choose_most_significant(mime_types): 
    218236    logging.debug('Returning first: %r.', mime_types[0])
    219237    return mime_types[0]
    220238
     239
    221240def split_uri_list(uri_list):
    222241    return _sugarbaseext.uri_list_extract_uris(uri_list)
    223242
     243
    224244def _file_looks_like_text(file_name):
    225245    f = open(file_name, 'r')
    226246    try:
    def _file_looks_like_text(file_name): 
    240260
    241261    return False
    242262
     263
    243264def _get_generic_type_for_mime(mime_type):
    244265    for generic_type in _generic_types:
    245266        if mime_type in generic_type['types']: