Skip to content

Instantly share code, notes, and snippets.

@gamesbook
Created September 24, 2017 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamesbook/8e5478eebf80beb6f762b6e2b38e09e2 to your computer and use it in GitHub Desktop.
Save gamesbook/8e5478eebf80beb6f762b6e2b38e09e2 to your computer and use it in GitHub Desktop.
Extract and print directives from files in a software project
# -*- coding: utf-8 -*-
""" Purpose: Extract and print directives from a software project.
Created: 2017-09-24
Contact: gamesbook@gmail.com
Usage::
python extract_directives.py
python extract_directives.py -d=/path/to/ -i=FIXME -e=js
Notes:
* The `extract_directives` function is taken directly from:
https://github.com/follnoob/extract-todo
LICENSE:
extract_directives.py is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
extract_directives.py is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with extract_directives.py. If not, see <http://www.gnu.org/licenses/>.
"""
__author__ = "Derek Hohls"
__version__ = "0.0.1"
import os
import codecs
import argparse
# GLOBALS
_FILES = {
# Dict with supported files and their comment chars
".tex": '%',
".py": '#',
".h": "//",
".c": "//",
".hpp": "//",
".cpp": "//",
".js": "//"
}
_EXCLUSIONS = [
# List of directories to ignore
'tests', 'migrations'
]
def extract_directives(fname, directive='TODO'):
"""Method for directive extraction.
Parameters
----------
fname : str
Path to file.
Returns
-------
list of str
List with directives.
"""
global _FILES
ext = os.path.splitext(fname)
comm_char = _FILES[ext[1]]
todos = []
with codecs.open(fname, "r", "utf-8") as f:
lineCount = 0
for line in f:
lineCount += 1
if comm_char not in line:
continue
try:
line = line.split(comm_char, 1)[1].strip()
except IndexError:
pass
if line.startswith(directive):
todo = line.split(directive, 1)[1].strip()
todos.append("%s:%d\n\t%s" %
(os.path.basename(fname), lineCount, todo))
return todos
def main(args):
directory = args.directory or "."
directive = args.directive or "TODO"
extension = args.extension or "py"
header = 'All %s in "%s" files under %s' % (directive, extension, directory)
print('='*len(header))
print(header)
print('='*len(header))
for root, dirs, files in os.walk(directory):
path = root.split(os.sep)
exclude = False
for exc in _EXCLUSIONS:
if exc in path:
exclude = True
if not exclude:
for the_file in files:
full_file = os.path.join(root, the_file)
if the_file.endswith('.%s' % extension):
items = extract_directives(
fname=full_file, directive=directive)
if items:
print('\n%s' % full_file)
print('-'*len(full_file))
for item in items:
print(item)
if __name__ == '__main__':
PARSER = argparse.ArgumentParser()
PARSER.add_argument(
'-d', '--directory',
help="Specify directory (default: current)")
PARSER.add_argument(
'-i', '--directive',
help="Specify the directive (default: TODO)")
PARSER.add_argument(
'-e', '--extension',
help="Specify the extension (default: py)")
PARSER.add_argument(
'-ll', '--loglevel', default='WARNING',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help="Set log level for service (default: WARNING)")
ARGS = PARSER.parse_args()
LOG_LEVEL = ARGS.loglevel
main(ARGS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment