Conditions | 13 |
Total Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like init_md_checklists() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import os |
||
112 | def init_md_checklists(): |
||
113 | """Converts markdown checklists items to DB.""" |
||
114 | kb_dir = os.path.join(app.root_path, 'markdown/checklists/') |
||
115 | try: |
||
116 | #checklists = ['asvs', 'pcidss', 'custom'] |
||
117 | checklists = ['asvs', 'custom', 'masvs'] |
||
118 | for checklist in checklists: |
||
119 | if checklist == "asvs": |
||
120 | for filename in os.listdir(kb_dir+checklist): |
||
121 | if filename.endswith(".md"): |
||
122 | name_raw = filename.split("-") |
||
123 | level = name_raw[4].replace("_", " ") |
||
124 | kbid_raw = name_raw[6].split(".") |
||
125 | kb_id = kbid_raw[0] |
||
126 | if level == "0": |
||
127 | # For the ASVS categories |
||
128 | file = os.path.join(kb_dir+checklist, filename) |
||
129 | data = open(file, 'r') |
||
130 | file_content = data.read() |
||
131 | data.close() |
||
132 | checklistID_raw = file_content.split(":") |
||
133 | checklistID = checklistID_raw[0] |
||
134 | checklistID = checklistID.lstrip('V') |
||
135 | checklistID = checklistID+".0" |
||
136 | else : |
||
137 | # For the ASVS items |
||
138 | file = os.path.join(kb_dir+checklist, filename) |
||
139 | data = open(file, 'r') |
||
140 | file_content = data.read() |
||
141 | data.close() |
||
142 | checklistID_raw = file_content.split(" ") |
||
143 | checklistID = checklistID_raw[0] |
||
144 | file = os.path.join(kb_dir+checklist, filename) |
||
145 | data = open(file, 'r') |
||
146 | file_content = data.read() |
||
147 | data.close() |
||
148 | content = file_content.split(' ', 1)[1] |
||
149 | content_escaped = content.translate(str.maketrans({"'": r"''", "-": r"", "#": r""})) |
||
150 | query = "INSERT OR REPLACE INTO checklists (checklistID, content, level, kbID) VALUES ('"+checklistID+"', '"+content_escaped+"', '"+level+"', '"+kb_id+"'); \n" |
||
151 | with open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') as myfile: |
||
152 | myfile.write(query) |
||
153 | if checklist == 'masvs': |
||
154 | for filename in os.listdir(kb_dir+checklist): |
||
155 | if filename.endswith(".md"): |
||
156 | name_raw = filename.split("-") |
||
157 | level = name_raw[4].replace("_", " ") |
||
158 | kbid_raw = name_raw[6].split(".") |
||
159 | kb_id = kbid_raw[0] |
||
160 | if level == "0": |
||
161 | # For the MASVS categories |
||
162 | file = os.path.join(kb_dir+checklist, filename) |
||
163 | data = open(file, 'r') |
||
164 | file_content = data.read() |
||
165 | data.close() |
||
166 | checklistID_raw = file_content.split(":") |
||
167 | checklistID = checklistID_raw[0] |
||
168 | checklistID = checklistID.lstrip('V') |
||
169 | checklistID = checklistID+".0" |
||
170 | else : |
||
171 | # For the MASVS items |
||
172 | file = os.path.join(kb_dir+checklist, filename) |
||
173 | data = open(file, 'r') |
||
174 | file_content = data.read() |
||
175 | data.close() |
||
176 | checklistID_raw = file_content.split(" ") |
||
177 | checklistID = checklistID_raw[0] |
||
178 | file = os.path.join(kb_dir+checklist, filename) |
||
179 | data = open(file, 'r') |
||
180 | file_content = data.read() |
||
181 | data.close() |
||
182 | content = file_content.split(' ', 1)[1] |
||
183 | content_escaped = content.translate(str.maketrans({"'": r"''", "-": r"", "#": r""})) |
||
184 | query = "INSERT OR REPLACE INTO checklists (checklistID, content, level, kbID) VALUES ('"+checklistID+"', '"+content_escaped+"', '"+level+"', '"+kb_id+"'); \n" |
||
185 | with open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') as myfile: |
||
186 | myfile.write(query) |
||
187 | print('Initialized the markdown checklists.') |
||
188 | return True |
||
189 | except Exception as e: |
||
190 | print('Exception in file db_tools, method init_md_checklists: ' + e) |
||
191 | return False |
||
192 |