OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2002-2010 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 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 16 """ |
| 17 generic classes/functions for pyreverse core/extensions |
| 18 """ |
| 19 |
| 20 import sys |
| 21 import re |
| 22 import os |
| 23 |
| 24 ########### pyreverse option utils ############################## |
| 25 |
| 26 |
| 27 RCFILE = '.pyreverserc' |
| 28 |
| 29 def get_default_options(): |
| 30 """ |
| 31 Read config file and return list of options |
| 32 """ |
| 33 options = [] |
| 34 home = os.environ.get('HOME', '') |
| 35 if home: |
| 36 rcfile = os.path.join(home, RCFILE) |
| 37 try: |
| 38 options = open(rcfile).read().split() |
| 39 except IOError: |
| 40 pass # ignore if no config file found |
| 41 return options |
| 42 |
| 43 def insert_default_options(): |
| 44 """insert default options to sys.argv |
| 45 """ |
| 46 options = get_default_options() |
| 47 options.reverse() |
| 48 for arg in options: |
| 49 sys.argv.insert(1, arg) |
| 50 |
| 51 |
| 52 |
| 53 # astng utilities ########################################################### |
| 54 |
| 55 SPECIAL = re.compile('^__[A-Za-z0-9]+[A-Za-z0-9_]*__$') |
| 56 PRIVATE = re.compile('^__[_A-Za-z0-9]*[A-Za-z0-9]+_?$') |
| 57 PROTECTED = re.compile('^_[_A-Za-z0-9]*$') |
| 58 |
| 59 def get_visibility(name): |
| 60 """return the visibility from a name: public, protected, private or special |
| 61 """ |
| 62 if SPECIAL.match(name): |
| 63 visibility = 'special' |
| 64 elif PRIVATE.match(name): |
| 65 visibility = 'private' |
| 66 elif PROTECTED.match(name): |
| 67 visibility = 'protected' |
| 68 |
| 69 else: |
| 70 visibility = 'public' |
| 71 return visibility |
| 72 |
| 73 ABSTRACT = re.compile('^.*Abstract.*') |
| 74 FINAL = re.compile('^[A-Z_]*$') |
| 75 |
| 76 def is_abstract(node): |
| 77 """return true if the given class node correspond to an abstract class |
| 78 definition |
| 79 """ |
| 80 return ABSTRACT.match(node.name) |
| 81 |
| 82 def is_final(node): |
| 83 """return true if the given class/function node correspond to final |
| 84 definition |
| 85 """ |
| 86 return FINAL.match(node.name) |
| 87 |
| 88 def is_interface(node): |
| 89 # bw compat |
| 90 return node.type == 'interface' |
| 91 |
| 92 def is_exception(node): |
| 93 # bw compat |
| 94 return node.type == 'exception' |
| 95 |
| 96 |
| 97 # Helpers ##################################################################### |
| 98 |
| 99 _CONSTRUCTOR = 1 |
| 100 _SPECIAL = 2 |
| 101 _PROTECTED = 4 |
| 102 _PRIVATE = 8 |
| 103 MODES = { |
| 104 'ALL' : 0, |
| 105 'PUB_ONLY' : _SPECIAL + _PROTECTED + _PRIVATE, |
| 106 'SPECIAL' : _SPECIAL, |
| 107 'OTHER' : _PROTECTED + _PRIVATE, |
| 108 } |
| 109 VIS_MOD = {'special': _SPECIAL, 'protected': _PROTECTED, \ |
| 110 'private': _PRIVATE, 'public': 0 } |
| 111 |
| 112 class FilterMixIn: |
| 113 """filter nodes according to a mode and nodes' visibility |
| 114 """ |
| 115 def __init__(self, mode): |
| 116 "init filter modes" |
| 117 __mode = 0 |
| 118 for nummod in mode.split('+'): |
| 119 try: |
| 120 __mode += MODES[nummod] |
| 121 except KeyError, ex: |
| 122 print >> sys.stderr, 'Unknown filter mode %s' % ex |
| 123 self.__mode = __mode |
| 124 |
| 125 |
| 126 def show_attr(self, node): |
| 127 """return true if the node should be treated |
| 128 """ |
| 129 visibility = get_visibility(getattr(node, 'name', node)) |
| 130 return not (self.__mode & VIS_MOD[visibility] ) |
| 131 |
OLD | NEW |