OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2002-2013 LOGILAB S.A. (Paris, FRANCE). |
| 2 # http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 3 # |
| 4 # This program is free software; you can redistribute it and/or modify it under |
| 5 # the terms of the GNU General Public License as published by the Free Software |
| 6 # Foundation; either version 2 of the License, or (at your option) any later |
| 7 # version. |
| 8 # |
| 9 # This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 # |
| 13 # You should have received a copy of the GNU General Public License along with |
| 14 # this program; if not, write to the Free Software Foundation, Inc., |
| 15 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 """ |
| 17 generic classes/functions for pyreverse core/extensions |
| 18 """ |
| 19 from __future__ import print_function |
| 20 |
| 21 import sys |
| 22 import re |
| 23 import os |
| 24 |
| 25 ########### pyreverse option utils ############################## |
| 26 |
| 27 |
| 28 RCFILE = '.pyreverserc' |
| 29 |
| 30 def get_default_options(): |
| 31 """ |
| 32 Read config file and return list of options |
| 33 """ |
| 34 options = [] |
| 35 home = os.environ.get('HOME', '') |
| 36 if home: |
| 37 rcfile = os.path.join(home, RCFILE) |
| 38 try: |
| 39 options = open(rcfile).read().split() |
| 40 except IOError: |
| 41 pass # ignore if no config file found |
| 42 return options |
| 43 |
| 44 def insert_default_options(): |
| 45 """insert default options to sys.argv |
| 46 """ |
| 47 options = get_default_options() |
| 48 options.reverse() |
| 49 for arg in options: |
| 50 sys.argv.insert(1, arg) |
| 51 |
| 52 |
| 53 |
| 54 # astroid utilities ########################################################### |
| 55 |
| 56 SPECIAL = re.compile('^__[A-Za-z0-9]+[A-Za-z0-9_]*__$') |
| 57 PRIVATE = re.compile('^__[_A-Za-z0-9]*[A-Za-z0-9]+_?$') |
| 58 PROTECTED = re.compile('^_[_A-Za-z0-9]*$') |
| 59 |
| 60 def get_visibility(name): |
| 61 """return the visibility from a name: public, protected, private or special |
| 62 """ |
| 63 if SPECIAL.match(name): |
| 64 visibility = 'special' |
| 65 elif PRIVATE.match(name): |
| 66 visibility = 'private' |
| 67 elif PROTECTED.match(name): |
| 68 visibility = 'protected' |
| 69 |
| 70 else: |
| 71 visibility = 'public' |
| 72 return visibility |
| 73 |
| 74 ABSTRACT = re.compile('^.*Abstract.*') |
| 75 FINAL = re.compile('^[A-Z_]*$') |
| 76 |
| 77 def is_abstract(node): |
| 78 """return true if the given class node correspond to an abstract class |
| 79 definition |
| 80 """ |
| 81 return ABSTRACT.match(node.name) |
| 82 |
| 83 def is_final(node): |
| 84 """return true if the given class/function node correspond to final |
| 85 definition |
| 86 """ |
| 87 return FINAL.match(node.name) |
| 88 |
| 89 def is_interface(node): |
| 90 # bw compat |
| 91 return node.type == 'interface' |
| 92 |
| 93 def is_exception(node): |
| 94 # bw compat |
| 95 return node.type == 'exception' |
| 96 |
| 97 |
| 98 # Helpers ##################################################################### |
| 99 |
| 100 _CONSTRUCTOR = 1 |
| 101 _SPECIAL = 2 |
| 102 _PROTECTED = 4 |
| 103 _PRIVATE = 8 |
| 104 MODES = { |
| 105 'ALL' : 0, |
| 106 'PUB_ONLY' : _SPECIAL + _PROTECTED + _PRIVATE, |
| 107 'SPECIAL' : _SPECIAL, |
| 108 'OTHER' : _PROTECTED + _PRIVATE, |
| 109 } |
| 110 VIS_MOD = {'special': _SPECIAL, 'protected': _PROTECTED, |
| 111 'private': _PRIVATE, 'public': 0} |
| 112 |
| 113 class FilterMixIn(object): |
| 114 """filter nodes according to a mode and nodes' visibility |
| 115 """ |
| 116 def __init__(self, mode): |
| 117 "init filter modes" |
| 118 __mode = 0 |
| 119 for nummod in mode.split('+'): |
| 120 try: |
| 121 __mode += MODES[nummod] |
| 122 except KeyError as ex: |
| 123 print('Unknown filter mode %s' % ex, file=sys.stderr) |
| 124 self.__mode = __mode |
| 125 |
| 126 |
| 127 def show_attr(self, node): |
| 128 """return true if the node should be treated |
| 129 """ |
| 130 visibility = get_visibility(getattr(node, 'name', node)) |
| 131 return not self.__mode & VIS_MOD[visibility] |
| 132 |
OLD | NEW |