config.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # This program is free software; you can redistribute it and/or modify it under
  2. # the terms of the GNU General Public License as published by the Free Software
  3. # Foundation; either version 2 of the License, or (at your option) any later
  4. # version.
  5. #
  6. # This program is distributed in the hope that it will be useful, but WITHOUT
  7. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
  9. #
  10. # You should have received a copy of the GNU General Public License along with
  11. # this program; if not, write to the Free Software Foundation, Inc.,
  12. # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  13. """ Copyright (c) 2003-2006 LOGILAB S.A. (Paris, FRANCE).
  14. http://www.logilab.fr/ -- mailto:contact@logilab.fr
  15. utilities for PyLint configuration :
  16. _ pylintrc
  17. _ pylint.d (PYLINT_HOME)
  18. """
  19. import pickle
  20. import os
  21. import sys
  22. from os.path import exists, isfile, join, expanduser, abspath, dirname
  23. # pylint home is used to save old runs results ################################
  24. USER_HOME = expanduser('~')
  25. if 'PYLINTHOME' in os.environ:
  26. PYLINT_HOME = os.environ['PYLINTHOME']
  27. if USER_HOME == '~':
  28. USER_HOME = dirname(PYLINT_HOME)
  29. elif USER_HOME == '~':
  30. PYLINT_HOME = ".pylint.d"
  31. else:
  32. PYLINT_HOME = join(USER_HOME, '.pylint.d')
  33. if not exists(PYLINT_HOME):
  34. try:
  35. os.mkdir(PYLINT_HOME)
  36. except OSError:
  37. print >> sys.stderr, 'Unable to create directory %s' % PYLINT_HOME
  38. def get_pdata_path(base_name, recurs):
  39. """return the path of the file which should contain old search data for the
  40. given base_name with the given options values
  41. """
  42. base_name = base_name.replace(os.sep, '_')
  43. return join(PYLINT_HOME, "%s%s%s"%(base_name, recurs, '.stats'))
  44. def load_results(base):
  45. """try to unpickle and return data from file if it exists and is not
  46. corrupted
  47. return an empty dictionary if it doesn't exists
  48. """
  49. data_file = get_pdata_path(base, 1)
  50. try:
  51. return pickle.load(open(data_file))
  52. except:
  53. return {}
  54. if sys.version_info < (3, 0):
  55. _PICK_MOD = 'w'
  56. else:
  57. _PICK_MOD = 'wb'
  58. def save_results(results, base):
  59. """pickle results"""
  60. data_file = get_pdata_path(base, 1)
  61. try:
  62. pickle.dump(results, open(data_file, _PICK_MOD))
  63. except (IOError, OSError), ex:
  64. print >> sys.stderr, 'Unable to create file %s: %s' % (data_file, ex)
  65. # location of the configuration file ##########################################
  66. def find_pylintrc():
  67. """search the pylint rc file and return its path if it find it, else None
  68. """
  69. # is there a pylint rc file in the current directory ?
  70. if exists('pylintrc'):
  71. return abspath('pylintrc')
  72. if isfile('__init__.py'):
  73. curdir = abspath(os.getcwd())
  74. while isfile(join(curdir, '__init__.py')):
  75. curdir = abspath(join(curdir, '..'))
  76. if isfile(join(curdir, 'pylintrc')):
  77. return join(curdir, 'pylintrc')
  78. if 'PYLINTRC' in os.environ and exists(os.environ['PYLINTRC']):
  79. pylintrc = os.environ['PYLINTRC']
  80. else:
  81. user_home = expanduser('~')
  82. if user_home == '~' or user_home == '/root':
  83. pylintrc = ".pylintrc"
  84. else:
  85. pylintrc = join(user_home, '.pylintrc')
  86. if not isfile(pylintrc):
  87. if isfile('/etc/pylintrc'):
  88. pylintrc = '/etc/pylintrc'
  89. else:
  90. pylintrc = None
  91. return pylintrc
  92. PYLINTRC = find_pylintrc()
  93. ENV_HELP = '''
  94. The following environment variables are used :
  95. * PYLINTHOME
  96. path to the directory where data of persistent run will be stored. If not
  97. found, it defaults to ~/.pylint.d/ or .pylint.d (in the current working
  98. directory).
  99. * PYLINTRC
  100. path to the configuration file. If not found, it will use the first
  101. existent file in ~/.pylintrc, /etc/pylintrc.
  102. ''' % globals()
  103. # evaluation messages #########################################################
  104. def get_note_message(note):
  105. """return a message according to note
  106. note is a float < 10 (10 is the highest note)
  107. """
  108. assert note <= 10, "Note is %.2f. Either you cheated, or pylint's \
  109. broken!" % note
  110. if note < 0:
  111. msg = 'You have to do something quick !'
  112. elif note < 1:
  113. msg = 'Hey! This is really dreadful. Or maybe pylint is buggy?'
  114. elif note < 2:
  115. msg = "Come on! You can't be proud of this code"
  116. elif note < 3:
  117. msg = 'Hum... Needs work.'
  118. elif note < 4:
  119. msg = 'Wouldn\'t you be a bit lazy?'
  120. elif note < 5:
  121. msg = 'A little more work would make it acceptable.'
  122. elif note < 6:
  123. msg = 'Just the bare minimum. Give it a bit more polish. '
  124. elif note < 7:
  125. msg = 'This is okay-ish, but I\'m sure you can do better.'
  126. elif note < 8:
  127. msg = 'If you commit now, people should not be making nasty \
  128. comments about you on c.l.py'
  129. elif note < 9:
  130. msg = 'That\'s pretty good. Good work mate.'
  131. elif note < 10:
  132. msg = 'So close to being perfect...'
  133. else:
  134. msg = 'Wow ! Now this deserves our uttermost respect.\nPlease send \
  135. your code to python-projects@logilab.org'
  136. return msg