| 1 |
from coccinelle import Cocci |
| 2 |
from sys import version_info |
| 3 |
|
| 4 |
# from "six" library and |
| 5 |
# http://stackoverflow.com/questions/4843173/how-to-check-if-type-of-a-variable-is-string-in-python |
| 6 |
PY3 = version_info[0] == 3 |
| 7 |
|
| 8 |
if PY3: |
| 9 |
string_types = str |
| 10 |
else: |
| 11 |
string_types = basestring |
| 12 |
|
| 13 |
def string_check(value): |
| 14 |
return isinstance(value, string_types) |
| 15 |
|
| 16 |
class Iteration: |
| 17 |
def __init__(self): |
| 18 |
self.__files = None |
| 19 |
self.__virtual_rules = set() |
| 20 |
self.__virtual_identifiers = {} |
| 21 |
self.__extend_virtual_ids = False |
| 22 |
|
| 23 |
def set_files(self, files): |
| 24 |
if not (isinstance(files, list) |
| 25 |
and all(string_check(value) for value in files)): |
| 26 |
raise TypeError("Iteration.set_files expects a list of strings") |
| 27 |
self.__files = files |
| 28 |
|
| 29 |
def add_virtual_rule(self, rule): |
| 30 |
if not (string_check(rule)): |
| 31 |
raise TypeError("Iteration.add_virtual_rule expects a string") |
| 32 |
self.__virtual_rules.add(rule) |
| 33 |
|
| 34 |
def add_virtual_identifier(self, ident, value): |
| 35 |
if not (string_check(ident) and string_check(value)): |
| 36 |
raise TypeError( |
| 37 |
"Iteration.add_virtual_identifier expects string arguments") |
| 38 |
if ident in self.__virtual_identifiers: |
| 39 |
raise ValueError("multiple values specified for {}".format(ident)) |
| 40 |
self.__virtual_identifiers[ident] = value |
| 41 |
|
| 42 |
def set_extend_virtual_ids(self, value): |
| 43 |
if not (string_check(value)): |
| 44 |
raise TypeError("Iteration.set_extend_virtual_ids expects a string") |
| 45 |
self.__extend_virtual_ids(value) |
| 46 |
|
| 47 |
def register(self): |
| 48 |
Cocci().add_pending_instance( |
| 49 |
self.__files, list(self.__virtual_rules), |
| 50 |
list(self.__virtual_identifiers.items()), self.__extend_virtual_ids) |