Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import mimetypes
import os
import re
from dodgy.checks import check_file_contents
from prospector.encoding import read_py_file, CouldNotHandleEncoding
from prospector.message import Location, Message
from prospector.tools.base import ToolBase
def module_from_path(path):
# note : assumes a relative path
module = re.sub(r'\.py', '', path)
return '.'.join(module.split(os.path.sep)[1:])
class DodgyTool(ToolBase):
def run(self, found_files):
warnings = []
for filepath in found_files.iter_file_paths():
mimetype = mimetypes.guess_type(filepath)
if mimetype[0] is None or not mimetype[0].startswith('text/') or mimetype[1] is not None:
continue
try:
contents = read_py_file(filepath)
except CouldNotHandleEncoding:
continue
for line, code, message in check_file_contents(contents):
warnings.append({
'line': line, 'code': code, 'message': message,
'path': filepath
ratings.Keywords: 'PYR09',
ratings.Author: 'PYR10',
ratings.AuthorEmail: 'PYR11',
ratings.Url: 'PYR12',
ratings.License: 'PYR13',
ratings.LicenceClassifier: 'PYR14',
ratings.ZipSafe: 'PYR15',
ratings.SDist: 'PYR16',
ratings.ValidREST: 'PYR18',
}
PYROMA_TEST_CLASSES = [t.__class__ for t in ratings.ALL_TESTS]
class PyromaTool(ToolBase):
def __init__(self, *args, **kwargs):
super(PyromaTool, self).__init__(*args, **kwargs)
self.ignore_codes = ()
def configure(self, prospector_config, found_files):
self.ignore_codes = prospector_config.get_disabled_messages('pyroma')
def run(self, found_files):
messages = []
for module in found_files.iter_module_paths(include_ignored=True):
dirname, filename = os.path.split(module)
if filename != 'setup.py':
continue
data = projectdata.get_data(dirname)
def flake(self, message):
filename, _, msg = message.message.split(':', 2)
self.record_message(
filename=filename,
line=message.lineno,
character=(message.col + 1),
code=message.type.error_code,
message=msg,
)
def get_messages(self):
return self._messages
class FrostedTool(ToolBase):
def __init__(self, *args, **kwargs):
super(FrostedTool, self).__init__(*args, **kwargs)
self.ignore_codes = ()
def configure(self, prospector_config, _):
self.ignore_codes = prospector_config.get_disabled_messages('frosted')
def run(self, found_files):
reporter = ProspectorReporter(ignore=self.ignore_codes)
for filepath in found_files.iter_module_paths():
# Frosted cannot handle non-utf-8 encoded files at the moment -
# see https://github.com/timothycrosley/frosted/issues/53
# Therefore (since pyflakes overlaps heavily and does not have the same
# problem) we will simply suppress that error. If you do get it working
# correctly, you only end up with a "CannotDecodeFile" error anyway which
vulture_messages = []
for code, template, items in all_items:
for item in items:
try:
filename = item.file
except AttributeError:
filename = item.filename
loc = Location(filename, None, None, item.lineno, -1)
message_text = template % item
message = Message('vulture', code, loc, message_text)
vulture_messages.append(message)
return self._internal_messages + vulture_messages
class VultureTool(ToolBase):
def __init__(self):
ToolBase.__init__(self)
self._vulture = None
self.ignore_codes = ()
def configure(self, prospector_config, found_files):
self.ignore_codes = prospector_config.get_disabled_messages('vulture')
def run(self, found_files):
vulture = ProspectorVulture(found_files)
vulture.scavenge()
return [message
for message in vulture.get_messages()
if message.code not in self.ignore_codes]
def _tool_not_available(name, install_option_name):
class NotAvailableTool(ToolBase):
def run(self, _):
raise FatalProspectorException("\nCannot run tool %s as support was not installed.\n"
"Please install by running 'pip install prospector[%s]'\n\n"
% (name, install_option_name))
return NotAvailableTool
super(ProspectorStyleGuide, self).__init__(*args, **kwargs)
def excluded(self, filename, parent=None):
if super(ProspectorStyleGuide, self).excluded(filename, parent):
return True
# If the file survived pep8's exclusion rules, check it against
# prospector's patterns.
if os.path.isdir(os.path.join(self._files.rootpath, filename)):
return False
fullpath = os.path.join(self._files.rootpath, parent, filename) if parent else filename
return fullpath not in self._module_paths
class Pep8Tool(ToolBase):
def __init__(self, *args, **kwargs):
super(Pep8Tool, self).__init__(*args, **kwargs)
self.checker = None
def configure(self, prospector_config, found_files):
# figure out if we should use a pre-existing config file
# such as setup.cfg or tox.ini
external_config = None
# 'none' means we ignore any external config, so just carry on
use_config = False
if prospector_config.use_external_config('pep8'):
use_config = True
paths = [os.path.join(found_files.rootpath, name) for name in PROJECT_CONFIG]
from __future__ import absolute_import
import ast
from mccabe import PathGraphingAstVisitor
from prospector.encoding import read_py_file, CouldNotHandleEncoding
from prospector.message import Location, Message, make_tool_error_message
from prospector.tools.base import ToolBase
__all__ = (
'McCabeTool',
)
class McCabeTool(ToolBase):
def __init__(self, *args, **kwargs):
super(McCabeTool, self).__init__(*args, **kwargs)
self.ignore_codes = ()
self.max_complexity = 10
def configure(self, prospector_config, _):
self.ignore_codes = prospector_config.get_disabled_messages('mccabe')
options = prospector_config.tool_options('mccabe')
if 'max-complexity' in options:
self.max_complexity = options['max-complexity']
def run(self, found_files):
messages = []
for code_file in found_files.iter_module_paths():
def flake(self, message):
code = _MESSAGE_CODES.get(message.__class__.__name__, 'F999')
self.record_message(
filename=message.filename,
line=message.lineno,
character=(message.col + 1),
code=code,
message=message.message % message.message_args,
)
def get_messages(self):
return self._messages
class PyFlakesTool(ToolBase):
def __init__(self, *args, **kwargs):
super(PyFlakesTool, self).__init__(*args, **kwargs)
self.ignore_codes = ()
def configure(self, prospector_config, _):
ignores = prospector_config.get_disabled_messages('pyflakes')
# convert old style to new
self.ignore_codes = [LEGACY_CODE_MAP.get(code, code) for code in ignores]
def run(self, found_files):
reporter = ProspectorReporter(ignore=self.ignore_codes)
for filepath in found_files.iter_module_paths():
checkPath(filepath, reporter)
return reporter.get_messages()
def __init__(self):
ToolBase.__init__(self)
self._vulture = None
self.ignore_codes = ()
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from prospector.encoding import read_py_file, CouldNotHandleEncoding
from pydocstyle.checker import ConventionChecker as PEP257Checker, AllError
from prospector.message import Location, Message, make_tool_error_message
from prospector.tools.base import ToolBase
__all__ = (
'Pep257Tool',
)
class Pep257Tool(ToolBase):
def __init__(self, *args, **kwargs):
super(Pep257Tool, self).__init__(*args, **kwargs)
self._code_files = []
self.ignore_codes = ()
def configure(self, prospector_config, found_files):
self.ignore_codes = prospector_config.get_disabled_messages('pep257')
def run(self, found_files):
messages = []
checker = PEP257Checker()
for code_file in found_files.iter_module_paths():
try:
for error in checker.check_source(