Ticket #1718: indentation.patch

File indentation.patch, 32.1 kB (added by multani, 3 years ago)

Patch which fixes indentation warning

  • helpers.py

     
    1 ##      common/helpers.py 
     1##    common/helpers.py 
    22## 
    33## Copyright (C) 2003-2006 Yann Le Boulanger <asterix@lagaule.org> 
    44## Copyright (C) 2005-2006 Nikos Kouremenos <kourem@gmail.com> 
     
    3030from xmpp_stringprep import nodeprep, resourceprep, nameprep 
    3131 
    3232try: 
    33         import winsound # windows-only built-in module for playing wav 
    34         import win32api 
    35         import win32con 
     33    import winsound # windows-only built-in module for playing wav 
     34    import win32api 
     35    import win32con 
    3636except: 
    37         pass 
     37    pass 
    3838 
    3939_ = i18n._ 
    4040Q_ = i18n.Q_ 
    4141 
    4242class InvalidFormat(Exception): 
    43         pass 
     43    pass 
    4444 
    4545def parse_jid(jidstring): 
    46         '''Perform stringprep on all JID fragments from a string 
    47         and return the full jid''' 
    48         # This function comes from http://svn.twistedmatrix.com/cvs/trunk/twisted/words/protocols/jabber/jid.py 
     46    '''Perform stringprep on all JID fragments from a string 
     47    and return the full jid''' 
     48    # This function comes from http://svn.twistedmatrix.com/cvs/trunk/twisted/words/protocols/jabber/jid.py 
    4949 
    50         user = None 
    51         server = None 
    52         resource = None 
     50    user = None 
     51    server = None 
     52    resource = None 
    5353 
    54         # Search for delimiters 
    55         user_sep = jidstring.find('@') 
    56         res_sep  = jidstring.find('/') 
     54    # Search for delimiters 
     55    user_sep = jidstring.find('@') 
     56    res_sep  = jidstring.find('/') 
    5757 
    58         if user_sep == -1: 
    59                 if res_sep == -1: 
    60                         # host 
    61                         server = jidstring 
    62                 else: 
    63                         # host/resource 
    64                         server = jidstring[0:res_sep] 
    65                         resource = jidstring[res_sep + 1:] or None 
    66         else: 
    67                 if res_sep == -1: 
    68                         # user@host 
    69                         user = jidstring[0:user_sep] or None 
    70                         server = jidstring[user_sep + 1:] 
    71                 else: 
    72                         if user_sep < res_sep: 
    73                                 # user@host/resource 
    74                                 user = jidstring[0:user_sep] or None 
    75                                 server = jidstring[user_sep + 1:user_sep + (res_sep - user_sep)] 
    76                                 resource = jidstring[res_sep + 1:] or None 
    77                         else: 
    78                                 # server/resource (with an @ in resource) 
    79                                 server = jidstring[0:res_sep] 
    80                                 resource = jidstring[res_sep + 1:] or None 
     58    if user_sep == -1: 
     59        if res_sep == -1: 
     60            # host 
     61            server = jidstring 
     62        else: 
     63            # host/resource 
     64            server = jidstring[0:res_sep] 
     65            resource = jidstring[res_sep + 1:] or None 
     66    else: 
     67        if res_sep == -1: 
     68            # user@host 
     69            user = jidstring[0:user_sep] or None 
     70            server = jidstring[user_sep + 1:] 
     71        else: 
     72            if user_sep < res_sep: 
     73                # user@host/resource 
     74                user = jidstring[0:user_sep] or None 
     75                server = jidstring[user_sep + 1:user_sep + (res_sep - user_sep)] 
     76                resource = jidstring[res_sep + 1:] or None 
     77            else: 
     78                # server/resource (with an @ in resource) 
     79                server = jidstring[0:res_sep] 
     80                resource = jidstring[res_sep + 1:] or None 
    8181 
    82         return prep(user, server, resource) 
     82    return prep(user, server, resource) 
    8383 
    8484def parse_resource(resource): 
    85         '''Perform stringprep on resource and return it''' 
    86         if resource: 
    87                 try: 
    88                         return resourceprep.prepare(unicode(resource)) 
    89                 except UnicodeError: 
    90                         raise InvalidFormat, 'Invalid character in resource.' 
     85    '''Perform stringprep on resource and return it''' 
     86    if resource: 
     87        try: 
     88            return resourceprep.prepare(unicode(resource)) 
     89        except UnicodeError: 
     90            raise InvalidFormat, 'Invalid character in resource.' 
    9191 
    9292def prep(user, server, resource): 
    93         '''Perform stringprep on all JID fragments and return the full jid''' 
    94         # This function comes from 
    95         #http://svn.twistedmatrix.com/cvs/trunk/twisted/words/protocols/jabber/jid.py 
     93    '''Perform stringprep on all JID fragments and return the full jid''' 
     94    # This function comes from 
     95    #http://svn.twistedmatrix.com/cvs/trunk/twisted/words/protocols/jabber/jid.py 
    9696 
    97         if user: 
    98                 try: 
    99                         user = nodeprep.prepare(unicode(user)) 
    100                 except UnicodeError: 
    101                         raise InvalidFormat, _('Invalid character in username.') 
    102         else: 
    103                 user = None 
     97    if user: 
     98        try: 
     99            user = nodeprep.prepare(unicode(user)) 
     100        except UnicodeError: 
     101            raise InvalidFormat, _('Invalid character in username.') 
     102    else: 
     103        user = None 
    104104 
    105         if not server: 
    106                 raise InvalidFormat, _('Server address required.') 
    107         else: 
    108                 try: 
    109                         server = nameprep.prepare(unicode(server)) 
    110                 except UnicodeError: 
    111                         raise InvalidFormat, _('Invalid character in hostname.') 
     105    if not server: 
     106        raise InvalidFormat, _('Server address required.') 
     107    else: 
     108        try: 
     109            server = nameprep.prepare(unicode(server)) 
     110        except UnicodeError: 
     111            raise InvalidFormat, _('Invalid character in hostname.') 
    112112 
    113         if resource: 
    114                 try: 
    115                         resource = resourceprep.prepare(unicode(resource)) 
    116                 except UnicodeError: 
    117                         raise InvalidFormat, _('Invalid character in resource.') 
    118         else: 
    119                 resource = None 
     113    if resource: 
     114        try: 
     115            resource = resourceprep.prepare(unicode(resource)) 
     116        except UnicodeError: 
     117            raise InvalidFormat, _('Invalid character in resource.') 
     118    else: 
     119        resource = None 
    120120 
    121         if user: 
    122                 if resource: 
    123                         return '%s@%s/%s' % (user, server, resource) 
    124                 else: 
    125                         return '%s@%s' % (user, server) 
    126         else: 
    127                 if resource: 
    128                         return '%s/%s' % (server, resource) 
    129                 else: 
    130                         return server 
     121    if user: 
     122        if resource: 
     123            return '%s@%s/%s' % (user, server, resource) 
     124        else: 
     125            return '%s@%s' % (user, server) 
     126    else: 
     127        if resource: 
     128            return '%s/%s' % (server, resource) 
     129        else: 
     130            return server 
    131131 
    132132def temp_failure_retry(func, *args, **kwargs): 
    133         while True: 
    134                 try: 
    135                         return func(*args, **kwargs) 
    136                 except (os.error, IOError, select.error), ex: 
    137                         if ex.errno == errno.EINTR: 
    138                                 continue 
    139                         else: 
    140                                 raise 
     133    while True: 
     134        try: 
     135            return func(*args, **kwargs) 
     136        except (os.error, IOError, select.error), ex: 
     137            if ex.errno == errno.EINTR: 
     138                continue 
     139            else: 
     140                raise 
    141141 
    142142def convert_bytes(string): 
    143         suffix = '' 
    144         # IEC standard says KiB = 1024 bytes KB = 1000 bytes 
    145         # but do we use the standard? 
    146         use_kib_mib = gajim.config.get('use_kib_mib') 
    147         align = 1024. 
    148         bytes = float(string) 
    149         if bytes >= align: 
    150                 bytes = round(bytes/align, 1) 
    151                 if bytes >= align: 
    152                         bytes = round(bytes/align, 1) 
    153                         if bytes >= align: 
    154                                 bytes = round(bytes/align, 1) 
    155                                 if use_kib_mib: 
    156                                         #GiB means gibibyte 
    157                                         suffix = _('%s GiB') 
    158                                 else: 
    159                                         #GB means gigabyte 
    160                                         suffix = _('%s GB') 
    161                         else: 
    162                                 if use_kib_mib: 
    163                                         #MiB means mibibyte 
    164                                         suffix = _('%s MiB') 
    165                                 else: 
    166                                         #MB means megabyte 
    167                                         suffix = _('%s MB') 
    168                 else: 
    169                         if use_kib_mib: 
    170                                         #KiB means kibibyte 
    171                                         suffix = _('%s KiB') 
    172                         else: 
    173                                 #KB means kilo bytes 
    174                                 suffix = _('%s KB') 
    175         else: 
    176                 #B means bytes 
    177                 suffix = _('%s B') 
    178         return suffix % unicode(bytes) 
     143    suffix = '' 
     144    # IEC standard says KiB = 1024 bytes KB = 1000 bytes 
     145    # but do we use the standard? 
     146    use_kib_mib = gajim.config.get('use_kib_mib') 
     147    align = 1024. 
     148    bytes = float(string) 
     149    if bytes >= align: 
     150        bytes = round(bytes/align, 1) 
     151        if bytes >= align: 
     152            bytes = round(bytes/align, 1) 
     153            if bytes >= align: 
     154                bytes = round(bytes/align, 1) 
     155                if use_kib_mib: 
     156                    #GiB means gibibyte 
     157                    suffix = _('%s GiB') 
     158                else: 
     159                    #GB means gigabyte 
     160                    suffix = _('%s GB') 
     161            else: 
     162                if use_kib_mib: 
     163                    #MiB means mibibyte 
     164                    suffix = _('%s MiB') 
     165                else: 
     166                    #MB means megabyte 
     167                    suffix = _('%s MB') 
     168        else: 
     169            if use_kib_mib: 
     170                    #KiB means kibibyte 
     171                    suffix = _('%s KiB') 
     172            else: 
     173                #KB means kilo bytes 
     174                suffix = _('%s KB') 
     175    else: 
     176        #B means bytes 
     177        suffix = _('%s B') 
     178    return suffix % unicode(bytes) 
    179179 
    180180def get_uf_show(show, use_mnemonic = False): 
    181         '''returns a userfriendly string for dnd/xa/chat 
    182         and makes all strings translatable 
    183         if use_mnemonic is True, it adds _ so GUI should call with True 
    184         for accessibility issues''' 
    185         if show == 'dnd': 
    186                 if use_mnemonic: 
    187                         uf_show = _('_Busy') 
    188                 else: 
    189                         uf_show = _('Busy') 
    190         elif show == 'xa': 
    191                 if use_mnemonic: 
    192                         uf_show = _('_Not Available') 
    193                 else: 
    194                         uf_show = _('Not Available') 
    195         elif show == 'chat': 
    196                 if use_mnemonic: 
    197                         uf_show = _('_Free for Chat') 
    198                 else: 
    199                         uf_show = _('Free for Chat') 
    200         elif show == 'online': 
    201                 if use_mnemonic: 
    202                         uf_show = _('_Available') 
    203                 else: 
    204                         uf_show = _('Available') 
    205         elif show == 'connecting': 
    206                         uf_show = _('Connecting') 
    207         elif show == 'away': 
    208                 if use_mnemonic: 
    209                         uf_show = _('A_way') 
    210                 else: 
    211                         uf_show = _('Away') 
    212         elif show == 'offline': 
    213                 if use_mnemonic: 
    214                         uf_show = _('_Offline') 
    215                 else: 
    216                         uf_show = _('Offline') 
    217         elif show == 'invisible': 
    218                 if use_mnemonic: 
    219                         uf_show = _('_Invisible') 
    220                 else: 
    221                         uf_show = _('Invisible') 
    222         elif show == 'not in roster': 
    223                 uf_show = _('Not in Roster') 
    224         elif show == 'requested': 
    225                 uf_show = Q_('?contact has status:Unknown') 
    226         else: 
    227                 uf_show = Q_('?contact has status:Has errors') 
    228         return unicode(uf_show) 
     181    '''returns a userfriendly string for dnd/xa/chat 
     182    and makes all strings translatable 
     183    if use_mnemonic is True, it adds _ so GUI should call with True 
     184    for accessibility issues''' 
     185    if show == 'dnd': 
     186        if use_mnemonic: 
     187            uf_show = _('_Busy') 
     188        else: 
     189            uf_show = _('Busy') 
     190    elif show == 'xa': 
     191        if use_mnemonic: 
     192            uf_show = _('_Not Available') 
     193        else: 
     194            uf_show = _('Not Available') 
     195    elif show == 'chat': 
     196        if use_mnemonic: 
     197            uf_show = _('_Free for Chat') 
     198        else: 
     199            uf_show = _('Free for Chat') 
     200    elif show == 'online': 
     201        if use_mnemonic: 
     202            uf_show = _('_Available') 
     203        else: 
     204            uf_show = _('Available') 
     205    elif show == 'connecting': 
     206            uf_show = _('Connecting') 
     207    elif show == 'away': 
     208        if use_mnemonic: 
     209            uf_show = _('A_way') 
     210        else: 
     211            uf_show = _('Away') 
     212    elif show == 'offline': 
     213        if use_mnemonic: 
     214            uf_show = _('_Offline') 
     215        else: 
     216            uf_show = _('Offline') 
     217    elif show == 'invisible': 
     218        if use_mnemonic: 
     219            uf_show = _('_Invisible') 
     220        else: 
     221            uf_show = _('Invisible') 
     222    elif show == 'not in roster': 
     223        uf_show = _('Not in Roster') 
     224    elif show == 'requested': 
     225        uf_show = Q_('?contact has status:Unknown') 
     226    else: 
     227        uf_show = Q_('?contact has status:Has errors') 
     228    return unicode(uf_show) 
    229229 
    230230def get_uf_sub(sub): 
    231         if sub == 'none': 
    232                 uf_sub = Q_('?Subscription we already have:None') 
    233         elif sub == 'to': 
    234                 uf_sub = _('To') 
    235         elif sub == 'from': 
    236                 uf_sub = _('From') 
    237         elif sub == 'both': 
    238                 uf_sub = _('Both') 
    239         else: 
    240                 uf_sub = sub 
     231    if sub == 'none': 
     232        uf_sub = Q_('?Subscription we already have:None') 
     233    elif sub == 'to': 
     234        uf_sub = _('To') 
     235    elif sub == 'from': 
     236        uf_sub = _('From') 
     237    elif sub == 'both': 
     238        uf_sub = _('Both') 
     239    else: 
     240        uf_sub = sub 
    241241 
    242         return unicode(uf_sub) 
     242    return unicode(uf_sub) 
    243243 
    244244def get_uf_ask(ask): 
    245         if ask is None: 
    246                 uf_ask = Q_('?Ask (for Subscription):None') 
    247         elif ask == 'subscribe': 
    248                 uf_ask = _('Subscribe') 
    249         else: 
    250                 uf_ask = ask 
     245    if ask is None: 
     246        uf_ask = Q_('?Ask (for Subscription):None') 
     247    elif ask == 'subscribe': 
     248        uf_ask = _('Subscribe') 
     249    else: 
     250        uf_ask = ask 
    251251 
    252         return unicode(uf_ask) 
     252    return unicode(uf_ask) 
    253253 
    254254def get_uf_role(role, plural = False): 
    255         ''' plural determines if you get Moderators or Moderator''' 
    256         if role == 'none': 
    257                 role_name = Q_('?Group Chat Contact Role:None') 
    258         elif role == 'moderator': 
    259                 if plural: 
    260                         role_name = _('Moderators') 
    261                 else: 
    262                         role_name = _('Moderator') 
    263         elif role == 'participant': 
    264                 if plural: 
    265                         role_name = _('Participants') 
    266                 else: 
    267                         role_name = _('Participant') 
    268         elif role == 'visitor': 
    269                 if plural: 
    270                         role_name = _('Visitors') 
    271                 else: 
    272                         role_name = _('Visitor') 
    273         return role_name 
     255    ''' plural determines if you get Moderators or Moderator''' 
     256    if role == 'none': 
     257        role_name = Q_('?Group Chat Contact Role:None') 
     258    elif role == 'moderator': 
     259        if plural: 
     260            role_name = _('Moderators') 
     261        else: 
     262            role_name = _('Moderator') 
     263    elif role == 'participant': 
     264        if plural: 
     265            role_name = _('Participants') 
     266        else: 
     267            role_name = _('Participant') 
     268    elif role == 'visitor': 
     269        if plural: 
     270            role_name = _('Visitors') 
     271        else: 
     272            role_name = _('Visitor') 
     273    return role_name 
    274274 
    275275def get_sorted_keys(adict): 
    276         keys = adict.keys() 
    277         keys.sort() 
    278         return keys 
     276    keys = adict.keys() 
     277    keys.sort() 
     278    return keys 
    279279 
    280280def to_one_line(msg): 
    281         msg = msg.replace('\\', '\\\\') 
    282         msg = msg.replace('\n', '\\n') 
    283         # s1 = 'test\ntest\\ntest' 
    284         # s11 = s1.replace('\\', '\\\\') 
    285         # s12 = s11.replace('\n', '\\n') 
    286         # s12 
    287         # 'test\\ntest\\\\ntest' 
    288         return msg 
     281    msg = msg.replace('\\', '\\\\') 
     282    msg = msg.replace('\n', '\\n') 
     283    # s1 = 'test\ntest\\ntest' 
     284    # s11 = s1.replace('\\', '\\\\') 
     285    # s12 = s11.replace('\n', '\\n') 
     286    # s12 
     287    # 'test\\ntest\\\\ntest' 
     288    return msg 
    289289 
    290290def from_one_line(msg): 
    291         # (?<!\\) is a lookbehind assertion which asks anything but '\' 
    292         # to match the regexp that follows it 
     291    # (?<!\\) is a lookbehind assertion which asks anything but '\' 
     292    # to match the regexp that follows it 
    293293 
    294         # So here match '\\n' but not if you have a '\' before that 
    295         re = sre.compile(r'(?<!\\)\\n') 
    296         msg = re.sub('\n', msg) 
    297         msg = msg.replace('\\\\', '\\') 
    298         # s12 = 'test\\ntest\\\\ntest' 
    299         # s13 = re.sub('\n', s12) 
    300         # s14 s13.replace('\\\\', '\\') 
    301         # s14 
    302         # 'test\ntest\\ntest' 
    303         return msg 
     294    # So here match '\\n' but not if you have a '\' before that 
     295    re = sre.compile(r'(?<!\\)\\n') 
     296    msg = re.sub('\n', msg) 
     297    msg = msg.replace('\\\\', '\\') 
     298    # s12 = 'test\\ntest\\\\ntest' 
     299    # s13 = re.sub('\n', s12) 
     300    # s14 s13.replace('\\\\', '\\') 
     301    # s14 
     302    # 'test\ntest\\ntest' 
     303    return msg 
    304304 
    305305def get_uf_chatstate(chatstate): 
    306         '''removes chatstate jargon and returns user friendly messages''' 
    307         if chatstate == 'active': 
    308                 return _('is paying attention to the conversation') 
    309         elif chatstate == 'inactive': 
    310                 return _('is doing something else') 
    311         elif chatstate == 'composing': 
    312                 return _('is composing a message...') 
    313         elif chatstate == 'paused': 
    314                 #paused means he or she was compoing but has stopped for a while 
    315                 return _('paused composing a message') 
    316         elif chatstate == 'gone': 
    317                 return _('has closed the chat window or tab') 
    318         return '' 
     306    '''removes chatstate jargon and returns user friendly messages''' 
     307    if chatstate == 'active': 
     308        return _('is paying attention to the conversation') 
     309    elif chatstate == 'inactive': 
     310        return _('is doing something else') 
     311    elif chatstate == 'composing': 
     312        return _('is composing a message...') 
     313    elif chatstate == 'paused': 
     314        #paused means he or she was compoing but has stopped for a while 
     315        return _('paused composing a message') 
     316    elif chatstate == 'gone': 
     317        return _('has closed the chat window or tab') 
     318    return '' 
    319319 
    320320def is_in_path(name_of_command, return_abs_path = False): 
    321         # if return_abs_path is True absolute path will be returned 
    322         # for name_of_command 
    323         # on failures False is returned 
    324         is_in_dir = False 
    325         found_in_which_dir = None 
    326         path = os.getenv('PATH').split(':') 
    327         for path_to_directory in path: 
    328                 try: 
    329                         contents = os.listdir(path_to_directory) 
    330                 except OSError: # user can have something in PATH that is not a dir 
    331                         pass 
    332                 else: 
    333                         is_in_dir = name_of_command in contents 
    334                 if is_in_dir: 
    335                         if return_abs_path: 
    336                                 found_in_which_dir = path_to_directory 
    337                         break 
     321    # if return_abs_path is True absolute path will be returned 
     322    # for name_of_command 
     323    # on failures False is returned 
     324    is_in_dir = False 
     325    found_in_which_dir = None 
     326    path = os.getenv('PATH').split(':') 
     327    for path_to_directory in path: 
     328        try: 
     329            contents = os.listdir(path_to_directory) 
     330        except OSError: # user can have something in PATH that is not a dir 
     331            pass 
     332        else: 
     333            is_in_dir = name_of_command in contents 
     334        if is_in_dir: 
     335            if return_abs_path: 
     336                found_in_which_dir = path_to_directory 
     337            break 
    338338 
    339         if found_in_which_dir: 
    340                 abs_path = os.path.join(path_to_directory, name_of_command) 
    341                 return abs_path 
    342         else: 
    343                 return is_in_dir 
     339    if found_in_which_dir: 
     340        abs_path = os.path.join(path_to_directory, name_of_command) 
     341        return abs_path 
     342    else: 
     343        return is_in_dir 
    344344 
    345345def launch_browser_mailer(kind, uri): 
    346         #kind = 'url' or 'mail' 
    347         if os.name == 'nt': 
    348                 try: 
    349                         os.startfile(uri) # if pywin32 is installed we open 
    350                 except: 
    351                         pass 
     346    #kind = 'url' or 'mail' 
     347    if os.name == 'nt': 
     348        try: 
     349            os.startfile(uri) # if pywin32 is installed we open 
     350        except: 
     351            pass 
    352352 
    353         else: 
    354                 if kind == 'mail' and not uri.startswith('mailto:'): 
    355                         uri = 'mailto:' + uri 
     353    else: 
     354        if kind == 'mail' and not uri.startswith('mailto:'): 
     355            uri = 'mailto:' + uri 
    356356 
    357                 if gajim.config.get('openwith') == 'gnome-open': 
    358                         command = 'gnome-open' 
    359                 elif gajim.config.get('openwith') == 'kfmclient exec': 
    360                         command = 'kfmclient exec' 
    361                 elif gajim.config.get('openwith') == 'custom': 
    362                         if kind == 'url': 
    363                                 command = gajim.config.get('custombrowser') 
    364                         if kind == 'mail': 
    365                                 command = gajim.config.get('custommailapp') 
    366                         if command == '': # if no app is configured 
    367                                 return 
    368                 # we add the uri in "" so we have good parsing from shell 
    369                 uri = uri.replace('"', '\\"') # escape " 
    370                 command = command + ' "' + uri + '" &' 
    371                 try: #FIXME: when we require python2.4+ use subprocess module 
    372                         os.system(command) 
    373                 except: 
    374                         pass 
     357        if gajim.config.get('openwith') == 'gnome-open': 
     358            command = 'gnome-open' 
     359        elif gajim.config.get('openwith') == 'kfmclient exec': 
     360            command = 'kfmclient exec' 
     361        elif gajim.config.get('openwith') == 'custom': 
     362            if kind == 'url': 
     363                command = gajim.config.get('custombrowser') 
     364            if kind == 'mail': 
     365                command = gajim.config.get('custommailapp') 
     366            if command == '': # if no app is configured 
     367                return 
     368        # we add the uri in "" so we have good parsing from shell 
     369        uri = uri.replace('"', '\\"') # escape " 
     370        command = command + ' "' + uri + '" &' 
     371        try: #FIXME: when we require python2.4+ use subprocess module 
     372            os.system(command) 
     373        except: 
     374            pass 
    375375 
    376376def launch_file_manager(path_to_open): 
    377         if os.name == 'nt': 
    378                 try: 
    379                     &n