Skip to content

Commit

Permalink
improved imports validation support
Browse files Browse the repository at this point in the history
  • Loading branch information
DamnWidget committed Oct 12, 2014
1 parent 352d9aa commit 70b067d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 64 deletions.
81 changes: 19 additions & 62 deletions anaconda_lib/import_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@
Anaconda imports validator
"""

import os
import sys
import logging
import functools
from jedi import Script

DEBUG = True


def sys_append(func):

@functools.wraps(func)
def wrapper(self, module_line):
sys.path.append(self.filepath)
result = func(self, module_line)
sys.path.remove(sys.path[len(sys.path)-1])
return result

return wrapper
DEBUG = True


class Validator:
Expand All @@ -34,69 +20,40 @@ class Validator:
def __init__(self, source, filename):
self.source = source
self.errors = []
self.filepath = '.'
if filename is not None:
self.filepath = os.path.dirname(filename)
self.filename = filename

def is_valid(self):
"""Determine if the source imports are valid or not
"""

for line, lineno in self._extract_imports():
if not self._validate_import(line):
self.errors.append((line, lineno))
error, valid = self._validate_import(line)
if not valid:
self.errors.append((error, lineno))

return not self.errors

@sys_append
def _validate_import(self, module_line):
"""Try to validate the given import line
"""Try to validate the given iport line
"""

# we don't want to mess with sublime text runtime interpreter
if 'sublime' in module_line:
return True

# maybe we don't want to do QA there
if 'noqa' in module_line:
return True

# relative imports doesn't works so lets do a trick
line = []
parent_append = False
error = []
error_string = 'can\'t import {}'
valid = True
for word in module_line.split():
if word.startswith('..'):
parent_append = True
line.append(word[2:])
elif word.startswith('.'):
line.append(word[1:])
else:
line.append(word)

line = ' '.join(line)
if 'from import' in line: # this happens after strip: from . import ?
line = line.replace('from ', '').strip()

if parent_append is True:
path = self.filepath.rsplit('/', 1)[0]
if path not in sys.path:
sys.path.append(path)

success = True
c = compile(line, '<string>', 'single')
try:
exec(c)
except (ImportError, ValueError, SystemError) as error:
if 'sublime' in error: # don't fuck up ST3 plugins development :)
success = True
else:
if DEBUG:
logging.debug(error)
success = False
if word in ('from', 'import', 'as'):
continue

offset = module_line.find(word) + len(word) / 2
if not Script(module_line, 1, offset, self.filename).goto():
if valid is True:
valid = False
error.append(word)

if parent_append is True:
sys.path.remove(sys.path[len(sys.path)-1])
return success
return '' if valid else error_string.format(' '.join(error)), valid

def _extract_imports(self):
"""Extract imports from the source
Expand Down
2 changes: 1 addition & 1 deletion anaconda_server/commands/import_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _convert(self, validator):
'lineno': lineno,
'offset': 0,
'code': 801,
'raw_error': '[E] ImportValidator (801): Can\'t import module',
'raw_error': '[E] ImportValidator (801): {}'.format(line),
'message': '[E] ImportValidator (%s): %s',
'underline_range': True
})
Expand Down
3 changes: 2 additions & 1 deletion messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"1.3.0": "messages/1.3.0.txt",
"1.3.1": "messages/1.3.1.txt",
"1.3.2": "messages/1.3.2.txt",
"1.3.3": "messages/1.3.3.txt"
"1.3.3": "messages/1.3.3.txt",
"1.3.4": "messages/1.3.4.txt"
}
1 change: 1 addition & 0 deletions messages/1.3.4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.3.4.txt

0 comments on commit 70b067d

Please sign in to comment.