|
1 | 1 | #!/usr/bin/env python3 |
2 | | -import re |
3 | | -import ast |
4 | | -from string import Template |
5 | | -class MethodDocGenerator: |
6 | 2 |
|
7 | | - def __init__(self, signature): |
8 | | - self.signature = signature |
| 3 | +class InvalidSyntax(Exception): |
| 4 | + pass |
| 5 | + |
| 6 | +class VimEnviroment: |
| 7 | + import vim |
| 8 | + |
| 9 | + @property |
| 10 | + def indent(self): |
| 11 | + return self.vars['g:python_indent'] |
| 12 | + |
| 13 | + def append_after_line(self, line_nr, text): |
| 14 | + for line in reversed(text.split('\n')): |
| 15 | + vim.current.buffer.append(line, line_nr) |
| 16 | + |
| 17 | + def lines_following_cursor(self): |
| 18 | + import vim |
| 19 | + lines = [] |
| 20 | + buffer = vim.current.buffer |
| 21 | + cursor_row = vim.current.window.cursor[0]-1 |
| 22 | + current_row = cursor_row |
| 23 | + while True: |
| 24 | + current_row += 1 |
| 25 | + yield current_row, buffer[current_row] |
| 26 | + |
| 27 | +class Method: |
| 28 | + |
| 29 | + def __init__(self, vim_env, templater, max_lines = 30, style='google'): |
| 30 | + self.starting_line = vim_env.current.window.cursor[0] - 1 |
| 31 | + self.max_lines = max_lines |
| 32 | + self.vim_env = vim_env |
| 33 | + self.templater = templater |
| 34 | + |
| 35 | + def write_docstring(): |
| 36 | + func_indent, args = self._method_data() |
| 37 | + self.templater.template(func_indent, args) |
| 38 | + |
| 39 | + def _method_data(): |
| 40 | + lines = [] |
| 41 | + valid = False |
| 42 | + lines_it = self.vim_env.lines_following_cursor() |
| 43 | + counter = 0 |
| 44 | + while not valid: |
| 45 | + if counter == self.max_lines: |
| 46 | + raise InvalidSyntax('The method either invalid or it is on > {} lines.'.format(str(self.max_lines))) |
| 47 | + last_row, line = next(lines_it) |
| 48 | + lines.append(line) |
| 49 | + valid, tree = self._is_valid(''.join(lines)) |
| 50 | + |
| 51 | + arguments = self._arguments(tree) |
| 52 | + func_indent = re.findall('^(\s*)', lines[0])[0] |
| 53 | + |
| 54 | + return func_indent, arguments |
| 55 | + |
| 56 | + def _arguments(tree): |
| 57 | + import ast |
| 58 | + try: |
| 59 | + args = [] |
| 60 | + for arg in tree.body[0].args.args: |
| 61 | + args.append(arg.arg) |
| 62 | + return args |
| 63 | + except SyntaxError as e: |
| 64 | + raise InvalidSyntax('The method has invalid syntax.') |
| 65 | + |
| 66 | + def _is_valid(lines): |
| 67 | + import ast |
| 68 | + func = ''.join(lines.lstrip(), '\n pass') |
| 69 | + try: |
| 70 | + tree = ast.parse(func) |
| 71 | + return True, tree |
| 72 | + except SyntaxError as e: |
| 73 | + return False, None |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +class Templater: |
| 78 | + from string import Template |
| 79 | + import re |
| 80 | + import os |
| 81 | + |
| 82 | + def __init__(self, location, indent, style='google'): |
| 83 | + self.style='google' |
| 84 | + self.indent = indent |
| 85 | + with open(os.path.join(location, '{}.txt'.format(self.style)), 'r') as f: |
| 86 | + self.template = Template(f.read()) |
| 87 | + |
| 88 | + def _substitute_list(self, list_name, template, list_): |
| 89 | + result_lines = [] |
| 90 | + for line in template.split('\n'): |
| 91 | + match = re.match('#(\w+):\{(.*)\}', line) |
| 92 | + if match: |
| 93 | + list_to_substitute, old_line = match.groups() |
| 94 | + if list_to_substitute == list_name: |
| 95 | + new_lines = [] |
| 96 | + for item in list_: |
| 97 | + new_line = re.sub('(\$\w+)', item, old_line) |
| 98 | + new_line = bytes(new_line, 'utf-8').decode('unicode_escape') |
| 99 | + new_lines.append(new_line) |
| 100 | + result_lines.append(''.join(new_lines)) |
| 101 | + else: |
| 102 | + result_lines.append(line) |
| 103 | + return '\n'.join(result_lines) |
| 104 | + |
| 105 | + def template(self, funcdef_indent, arguments): |
| 106 | + list_not_sub = self.template.safe_substitute(i=funcdef_indent, i2=self.indent) |
| 107 | + return self._substitute_list('args', list_not_sub, arguments) |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +class MethodDocGenerator: |
| 112 | + def __init__(self, style): |
| 113 | + self.style = style |
| 114 | + self.method = Method(style) |
9 | 115 |
|
10 | 116 | def new_docstring(self): # generates new docstring no matter what |
11 | 117 | pass |
12 | 118 |
|
13 | 119 | def get_doctring(self, context): # perform suitable docstring action |
14 | 120 | pass |
| 121 | + |
| 122 | + |
| 123 | +def final_call(): |
| 124 | + vim_env = VimEnviroment() |
| 125 | + templater = Templater('.', indent, style) |
| 126 | + method = Method(vim_env, templater) |
| 127 | + |
| 128 | +if __name__ == '__main__': |
| 129 | + print(google_template(['arg1','arg2'],' '*4, ' '*4)) |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
0 commit comments