8
8
9
9
10
10
class Pygemony (object ):
11
+ """
12
+ The main driver of pygemony, pulls the seperate pieces together.
13
+ """
11
14
def __init__ (self , user = None , token = None , owner = None , repo = None ):
12
15
# todo_found contains a list of the following layout:
13
16
# ['file_path', 'line_number', 'todo_message', 'md5 of todo']
@@ -17,23 +20,14 @@ def __init__(self, user=None, token=None, owner=None, repo=None):
17
20
# TODO(ian): Add support for parsing more than one file type
18
21
self .language = self .lookup_language ()
19
22
20
- def find_end_comment (self , f ):
21
- # TODO(ian): Remove this function as we no longer support multiline TODO
22
- todo_content = []
23
- x = f
24
- count = 0
25
- for line in x .readlines ():
26
- todo_content .append (line )
27
- if self .language .multi_comment [1 ] in line :
28
- return todo_content
29
-
30
- if count > 20 :
31
- return None
32
-
33
- todo_content .append (line )
34
- count += 1
35
-
36
23
def _sanitize_todo_line (self , lines ):
24
+ """
25
+ Strips tab, newline, and comment characters form the TODO line.
26
+
27
+ :param str lines: The found line containing a TODO
28
+ :rtype: str
29
+ :return: The sanitized TODO line.
30
+ """
37
31
# We're mainly aiming to remove newlines and tab characters here.
38
32
lines = lines .replace ('\n ' , '' )
39
33
while ' ' in lines or '\t ' in lines :
@@ -44,23 +38,55 @@ def _sanitize_todo_line(self, lines):
44
38
45
39
@staticmethod
46
40
def hash_todo (todo_content , file_name ):
41
+ """
42
+ Hashes the TODO line with the file name
43
+
44
+ :param str todo_content: The line in the file containing TODO
45
+ :param str file_name: The file name containing the TODO line.
46
+ :rtype: str
47
+ :return: The MD5 hash of the `todo_content` and `file_name`
48
+ """
47
49
m = hashlib .md5 ()
48
50
m .update ('{0}-{1}' .format (todo_content , file_name ))
49
51
return str (m .hexdigest ())
50
52
51
53
def parse_for_todo (self , f , file_ ):
54
+ """
55
+ Searches (line-by-line) through a file's content and and looks for
56
+ lines containing TODO.
57
+
58
+ :param file_handle f: The handle to the file that is currently being
59
+ searched
60
+ :param str file_: The name of the file currently being searched
61
+ """
52
62
for i , line in enumerate (f .readlines ()):
53
63
if "TODO" in line and self ._starts_with_comment (line ):
54
64
line = self ._sanitize_todo_line (line )
55
65
self .todo_found .append ([file_ , i , line , self .hash_todo (line , file_ )])
56
66
57
67
def parse_by_extension (self , files ):
68
+ """
69
+ Parses the list of the directory for files with an acceptable
70
+ extension. The extension is determined by data returned from github on
71
+ the languages used in the project.
72
+
73
+ :param list files: The list of all files in the current repository
74
+ :rtype: generator(str)
75
+ :return: Generates a list of acceptable-to-parse files.
76
+ """
58
77
for lang in self .language :
59
78
for ext in lang .file_exts :
60
79
for file_ in fn_filter (files , ext ):
61
80
yield file_
62
81
63
82
def find_all_files (self , root ):
83
+ """
84
+ Walks the current repository directory and determines viable files
85
+
86
+ :param str root: The root directory
87
+ :rtype: list
88
+ :return: The list of files found and determined to be viable.
89
+ """
64
90
files_found = []
65
91
66
92
for roots , _ , files in walk (root ):
@@ -73,6 +99,12 @@ def find_all_files(self, root):
73
99
return files_found
74
100
75
101
def file_handler (self ):
102
+ """
103
+ Handles IO with the file
104
+
105
+ :rtype: list
106
+ :return: The list of files found
107
+ """
76
108
# First we need to remove any non-text files
77
109
files_found = self .find_all_files ('./' )
78
110
# TODO(ian): filter() over files to parse out by mimetype
@@ -96,10 +128,20 @@ def file_handler(self):
96
128
return files_found
97
129
98
130
def run (self ):
131
+ """
132
+ Starts the process of finding TODOs
133
+ """
99
134
self .file_handler ()
100
135
self .github .commit (self .todo_found )
101
136
102
137
def lookup_language (self ):
138
+ """
139
+ Constructs langauge classes based on what is found in github data.
140
+
141
+ :rtype: list
142
+ :return: A list of language classes that will be found in a github
143
+ repo.
144
+ """
103
145
lang_map = {'cpp' : LanguageCPP ,
104
146
'python' : LanguagePython ,
105
147
'javascript' : LanguageJavascript ,
@@ -114,12 +156,28 @@ def lookup_language(self):
114
156
return [lang_map [str (langs [0 ][0 ]).lower ()]()]
115
157
116
158
def _starts_with_comment (self , line ):
159
+ """
160
+ Verifies a line (containing the word TODO) starts with a comment, if it
161
+ does, we deem it to be commit-viable.
162
+
163
+ :param str line: The line that contains "TODO"
164
+
165
+ :rtype: bool
166
+ :return: True if line starts with a comment (is a valid TODO statement)
167
+ """
117
168
comments = self ._create_comment_start_list ()
118
169
for comment in comments :
119
170
if line .startswith (comment ):
120
171
return True
121
172
122
173
def _create_comment_start_list (self ):
174
+ """
175
+ Create a list of comments from each language class associated with the
176
+ current repo.
177
+
178
+ :rtype: list
179
+ :return: A list of strings containing all line-start comments.
180
+ """
123
181
comments = []
124
182
for lang in self .language :
125
183
comments .append (lang .single_comment )
0 commit comments