| Conditions | 13 |
| Total Lines | 80 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 119 | def init_md_checklists(): |
||
| 120 | """Converts markdown checklists items to DB.""" |
||
| 121 | kb_dir = os.path.join(app.root_path, 'markdown/checklists/') |
||
| 122 | try: |
||
| 123 | #checklists = ['asvs', 'pcidss', 'custom'] |
||
| 124 | checklists = ['asvs', 'custom', 'masvs'] |
||
| 125 | for checklist in checklists: |
||
| 126 | if checklist == "asvs": |
||
| 127 | for filename in os.listdir(kb_dir+checklist): |
||
| 128 | if filename.endswith(".md"): |
||
| 129 | name_raw = filename.split("-") |
||
| 130 | level = name_raw[4].replace("_", " ") |
||
| 131 | kbid_raw = name_raw[6].split(".") |
||
| 132 | kb_id = kbid_raw[0] |
||
| 133 | if level == "0": |
||
| 134 | # For the ASVS categories |
||
| 135 | file = os.path.join(kb_dir+checklist, filename) |
||
| 136 | data = open(file, 'r') |
||
| 137 | file_content = data.read() |
||
| 138 | data.close() |
||
| 139 | checklistID_raw = file_content.split(":") |
||
| 140 | checklistID = checklistID_raw[0] |
||
| 141 | checklistID = checklistID.lstrip('V') |
||
| 142 | checklistID = checklistID+".0" |
||
| 143 | else : |
||
| 144 | # For the ASVS items |
||
| 145 | file = os.path.join(kb_dir+checklist, filename) |
||
| 146 | data = open(file, 'r') |
||
| 147 | file_content = data.read() |
||
| 148 | data.close() |
||
| 149 | checklistID_raw = file_content.split(" ") |
||
| 150 | checklistID = checklistID_raw[0] |
||
| 151 | file = os.path.join(kb_dir+checklist, filename) |
||
| 152 | data = open(file, 'r') |
||
| 153 | file_content = data.read() |
||
| 154 | data.close() |
||
| 155 | content = file_content.split(' ', 1)[1] |
||
| 156 | content_escaped = content.translate(str.maketrans({"'": r"''", "-": r"", "#": r""})) |
||
| 157 | query = "INSERT OR REPLACE INTO checklists (checklistID, content, level, kbID) VALUES ('"+checklistID+"', '"+content_escaped+"', '"+level+"', '"+kb_id+"'); \n" |
||
| 158 | with open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') as myfile: |
||
| 159 | myfile.write(query) |
||
| 160 | if checklist == 'masvs': |
||
| 161 | for filename in os.listdir(kb_dir+checklist): |
||
| 162 | if filename.endswith(".md"): |
||
| 163 | name_raw = filename.split("-") |
||
| 164 | level = name_raw[4].replace("_", " ") |
||
| 165 | kbid_raw = name_raw[6].split(".") |
||
| 166 | kb_id = kbid_raw[0] |
||
| 167 | if level == "0": |
||
| 168 | # For the MASVS categories |
||
| 169 | file = os.path.join(kb_dir+checklist, filename) |
||
| 170 | data = open(file, 'r') |
||
| 171 | file_content = data.read() |
||
| 172 | data.close() |
||
| 173 | checklistID_raw = file_content.split(":") |
||
| 174 | checklistID = checklistID_raw[0] |
||
| 175 | checklistID = checklistID.lstrip('V') |
||
| 176 | checklistID = checklistID+".0" |
||
| 177 | else : |
||
| 178 | # For the MASVS items |
||
| 179 | file = os.path.join(kb_dir+checklist, filename) |
||
| 180 | data = open(file, 'r') |
||
| 181 | file_content = data.read() |
||
| 182 | data.close() |
||
| 183 | checklistID_raw = file_content.split(" ") |
||
| 184 | checklistID = checklistID_raw[0] |
||
| 185 | file = os.path.join(kb_dir+checklist, filename) |
||
| 186 | data = open(file, 'r') |
||
| 187 | file_content = data.read() |
||
| 188 | data.close() |
||
| 189 | content = file_content.split(' ', 1)[1] |
||
| 190 | content_escaped = content.translate(str.maketrans({"'": r"''", "-": r"", "#": r""})) |
||
| 191 | query = "INSERT OR REPLACE INTO checklists (checklistID, content, level, kbID) VALUES ('"+checklistID+"', '"+content_escaped+"', '"+level+"', '"+kb_id+"'); \n" |
||
| 192 | with open(os.path.join(app.root_path, 'db.sqlite_schema'), 'a') as myfile: |
||
| 193 | myfile.write(query) |
||
| 194 | print('Initialized the markdown checklists.') |
||
| 195 | return True |
||
| 196 | except Exception as e: |
||
| 197 | print('Exception in file db_tools, method init_md_checklists: ' + e) |
||
| 198 | return False |