|
1 | 1 | import re |
2 | | -from pythonwhat import utils |
3 | | -from pythonwhat import utils_ast |
4 | | -import _ast |
5 | 2 |
|
6 | 3 | class Feedback(object): |
7 | 4 |
|
8 | 5 | def __init__(self, message, astobj = None): |
9 | 6 | self.message = message |
10 | 7 | self.line_info = {} |
11 | 8 | try: |
12 | | - if astobj is not None: |
13 | | - if issubclass(type(astobj), (_ast.Module, _ast.Expression)): |
14 | | - astobj = astobj.body |
15 | | - if isinstance(astobj, list) and len(astobj) > 0: |
16 | | - start = astobj[0] |
17 | | - end = astobj[-1] |
18 | | - else: |
19 | | - start = astobj |
20 | | - end = astobj |
21 | | - if hasattr(start, "lineno") and \ |
22 | | - hasattr(start, "col_offset") and \ |
23 | | - hasattr(end, "end_lineno") and \ |
24 | | - hasattr(end, "end_col_offset"): |
25 | | - self.line_info["line_start"] = start.lineno |
26 | | - self.line_info["column_start"] = start.col_offset |
27 | | - self.line_info["line_end"] = end.end_lineno |
28 | | - self.line_info["column_end"] = end.end_col_offset |
| 9 | + if astobj is not None and \ |
| 10 | + hasattr(astobj, "first_token") and \ |
| 11 | + hasattr(astobj, "last_token"): |
| 12 | + self.line_info["line_start"] = astobj.first_token.start[0] |
| 13 | + self.line_info["column_start"] = astobj.first_token.start[1] |
| 14 | + self.line_info["line_end"] = astobj.last_token.end[0] |
| 15 | + self.line_info["column_end"] = astobj.last_token.end[1] |
29 | 16 | except: |
30 | 17 | pass |
31 | 18 |
|
32 | | -# TODO FILIP: No used for now, come back to this later. |
33 | | -class FeedbackMessage(object): |
34 | | - """Generate feedback. |
35 | | -
|
36 | | - Don't use this yet! |
37 | | -
|
38 | | - This class will hold all functionality which is related to feedback messaging. |
39 | | - At the moment it is NOT used, feedback generation is still HIGLY interwoven with |
40 | | - test_... files. Should be decoupled. |
41 | | -
|
42 | | - Class should be refactored to use .format() instead. |
43 | | -
|
44 | | - Will be documented when it's refactored. |
45 | | - """ |
46 | | - def __init__(self, message_string): |
47 | | - self.set(message_string) |
48 | | - self.information = {} |
49 | | - |
50 | | - def add_information(self, key, value): |
51 | | - if (not(key in self.information)): |
52 | | - self.set_information(key, value) |
53 | | - |
54 | | - def set_information(self, key, value): |
55 | | - self.information[key] = utils.shorten_str(str(value)) |
56 | | - |
57 | | - def remove_information(self, key): |
58 | | - if (key in self.information): |
59 | | - self.information.pop(key) |
60 | | - |
61 | | - def set(self, message_string): |
62 | | - self.message_string = str(message_string) |
63 | | - |
64 | | - def append(self, message_string): |
65 | | - self.message_string += str(message_string) |
66 | | - |
67 | | - def cond_append(self, cond, message_string): |
68 | | - self.message_string += "${{" + \ |
69 | | - str(cond) + " ? " + str(message_string) + "}}" |
70 | | - |
71 | | - def generateString(self): |
72 | | - generated_string = FeedbackMessage.replaceRegularTags( |
73 | | - self.message_string, self.information) |
74 | | - generated_string = FeedbackMessage.replaceConditionalTags( |
75 | | - generated_string, self.information) |
76 | | - return(generated_string) |
77 | | - |
78 | | - def replaceRegularTags(message_string, information): |
79 | | - generated_string = message_string |
80 | | - |
81 | | - pattern = "\${([a-zA-Z]*?)}" |
82 | | - |
83 | | - keywords = re.findall(pattern, generated_string) |
84 | | - for keyword in keywords: |
85 | | - replace = "\${" + keyword + "}" |
86 | | - if (keyword in information): |
87 | | - generated_string = re.sub( |
88 | | - replace, information[keyword], generated_string) |
89 | | - else: |
90 | | - generated_string = re.sub(replace, "", generated_string) |
91 | | - |
92 | | - return(generated_string) |
93 | | - |
94 | | - def replaceConditionalTags(message_string, information): |
95 | | - generated_string = message_string.replace("\n", "\\\\n") |
96 | | - pattern = "\${{([a-zA-Z]*?) \? (.*?)}}" |
97 | | - |
98 | | - cond_keywords = re.findall(pattern, generated_string) |
99 | | - for (keyword, k_string) in cond_keywords: |
100 | | - replace = "\${{" + keyword + " \? " + re.escape(k_string) + "}}" |
101 | | - if (keyword in information): |
102 | | - generated_string = re.sub( |
103 | | - replace, |
104 | | - " " + |
105 | | - FeedbackMessage.replaceRegularTags( |
106 | | - k_string, |
107 | | - information), |
108 | | - generated_string) |
109 | | - else: |
110 | | - generated_string = re.sub(replace, "", generated_string) |
111 | | - |
112 | | - return(generated_string) |
0 commit comments