47 try: |
47 try: |
48 import StringIO as io |
48 import StringIO as io |
49 except (ImportError): |
49 except (ImportError): |
50 import io # __IGNORE_WARNING__ |
50 import io # __IGNORE_WARNING__ |
51 |
51 |
52 import Utilities |
|
53 |
|
54 if not hasattr(tokenize, 'NL'): |
52 if not hasattr(tokenize, 'NL'): |
55 raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") |
53 raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") |
56 |
54 |
57 __all__ = ["check", "NannyNag", "process_tokens"] |
55 __all__ = ["check", "NannyNag", "process_tokens"] |
|
56 |
|
57 |
|
58 def initService(): |
|
59 """ |
|
60 Initialize the service and return the entry point. |
|
61 |
|
62 @return the entry point for the background client (function) |
|
63 """ |
|
64 return check |
58 |
65 |
59 |
66 |
60 class NannyNag(Exception): |
67 class NannyNag(Exception): |
61 """ |
68 """ |
62 Class implementing an exception for indentation issues. |
69 Class implementing an exception for indentation issues. |
112 valid, if the status is True. |
119 valid, if the status is True. |
113 """ |
120 """ |
114 global indents, check_equal |
121 global indents, check_equal |
115 indents = [Whitespace("")] |
122 indents = [Whitespace("")] |
116 check_equal = 0 |
123 check_equal = 0 |
117 |
|
118 if not text: |
124 if not text: |
119 try: |
125 return (True, "1", "Error: source code missing.") |
120 text = Utilities.readEncodedFile(file)[0] |
|
121 text = Utilities.normalizeCode(text) |
|
122 except (UnicodeError, IOError) as msg: |
|
123 return (True, file, "1", "Error: {0}".format(str(msg))) |
|
124 |
126 |
125 source = io.StringIO(text) |
127 source = io.StringIO(text) |
126 try: |
128 try: |
127 process_tokens(tokenize.generate_tokens(source.readline)) |
129 process_tokens(tokenize.generate_tokens(source.readline)) |
128 |
130 |
129 except tokenize.TokenError as msg: |
131 except tokenize.TokenError as msg: |
130 return (True, file, "1", "Token Error: {0}".format(str(msg))) |
132 return (True, "1", "Token Error: {0}".format(str(msg))) |
131 |
133 |
132 except IndentationError as err: |
134 except IndentationError as err: |
133 return (True, file, err.lineno, |
135 return (True, str(err.lineno), |
134 "Indentation Error: {0}".format(str(err.msg))) |
136 "Indentation Error: {0}".format(str(err.msg))) |
135 |
137 |
136 except NannyNag as nag: |
138 except NannyNag as nag: |
137 badline = nag.get_lineno() |
139 badline = nag.get_lineno() |
138 line = nag.get_line() |
140 line = nag.get_line() |
139 return (True, file, str(badline), line) |
141 return (True, str(badline), line) |
140 |
142 |
141 except Exception as err: |
143 except Exception as err: |
142 return (True, file, "1", "Unspecific Error: {0}".format(str(err))) |
144 return (True, "1", "Unspecific Error: {0}".format(str(err))) |
143 |
145 |
144 return (False, None, None, None) |
146 return (False, None, None) |
145 |
147 |
146 |
148 |
147 class Whitespace(object): |
149 class Whitespace(object): |
148 """ |
150 """ |
149 Class implementing the whitespace checker. |
151 Class implementing the whitespace checker. |