-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AnnotationBear: Allow missing language setting
A missing language setting is not any different from a language name which has a Language definition that does not contain the necessary information to correctly parse the file. Use a special 'UnknownLanguage' in this scenario. Also upgrades AnnotationBear from LanguageDefinition to the new Language. Fixes #1012 Closes #1095
- Loading branch information
Showing
4 changed files
with
79 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from coalib.bearlib.languages.LanguageDefinition import LanguageDefinition | ||
from coalib.bearlib.languages.Language import Language | ||
from coalib.bears.LocalBear import LocalBear | ||
from coalib.results.HiddenResult import HiddenResult | ||
from coalib.results.Result import Result, RESULT_SEVERITY | ||
|
@@ -12,7 +12,8 @@ class AnnotationBear(LocalBear): | |
AUTHORS_EMAILS = {'[email protected]'} | ||
LICENSE = 'AGPL-3.0' | ||
|
||
def run(self, filename, file, language: str, coalang_dir: str = None): | ||
def run(self, filename, file, language: str='Unknown', | ||
coalang_dir: str=None): | ||
""" | ||
Finds out all the positions of strings and comments in a file. | ||
The Bear searches for valid comments and strings and yields their | ||
|
@@ -31,19 +32,43 @@ def run(self, filename, file, language: str, coalang_dir: str = None): | |
``u"string"``, the ``u`` will not be in the source range). | ||
""" | ||
try: | ||
lang_dict = LanguageDefinition(language, coalang_dir=coalang_dir) | ||
except FileNotFoundError: | ||
lang = Language[language] | ||
except AttributeError: | ||
content = ('coalang specification for ' + language + | ||
' not found.') | ||
yield HiddenResult(self, content) | ||
return | ||
|
||
string_delimiters = dict(lang_dict['string_delimiters']) | ||
multiline_string_delimiters = dict( | ||
lang_dict['multiline_string_delimiters']) | ||
multiline_comment_delimiters = dict( | ||
lang_dict['multiline_comment_delimiters']) | ||
comment_delimiter = dict(lang_dict['comment_delimiter']) | ||
lang = Language['Unknown'] | ||
|
||
lang = lang.get_default_version() | ||
|
||
if 'string_delimiters' in lang.attributes: | ||
string_delimiters = lang.string_delimiters | ||
else: | ||
string_delimiters = {} | ||
|
||
if 'multiline_string_delimiters' in lang.attributes: | ||
multiline_string_delimiters = lang.multiline_string_delimiters | ||
else: | ||
multiline_string_delimiters = {} | ||
|
||
if 'multiline_comment_delimiters' in lang.attributes: | ||
multiline_comment_delimiters = lang.multiline_comment_delimiters | ||
else: | ||
multiline_comment_delimiters = {} | ||
|
||
if 'comment_delimiter' in lang.attributes: | ||
if isinstance(lang.comment_delimiter, str): | ||
comment_delimiter = {lang.comment_delimiter: ''} | ||
elif isinstance(lang.comment_delimiter, tuple): | ||
comment_delimiter = {item: '' | ||
for item in lang.comment_delimiter} | ||
else: | ||
raise TypeError('%s.comment_delimiter of unknown type %s' | ||
% (lang.__class__.__qualname__, | ||
lang.comment_delimiter.__class__.__name__)) | ||
else: | ||
comment_delimiter = {} | ||
|
||
string_ranges = comment_ranges = () | ||
try: | ||
string_ranges, comment_ranges = self.find_annotation_ranges( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters