Comment Stripper

When writing code I like to make sure it is commented properly for future me. I also like to include features such as doc strings. The problem I find is that when working on this code the comments take up a lot of space and make it difficult to see large portions of actual code that is not a comment on the screen. I've written a program that will extract the comments from a file and write a new file without the comments. This program acts as a watcher that continually waits for a change to made to the file with comments and writes to the comment-less file. Now this may be a feature that is available in some IDEs, but I use ATOM and I couldn't find anything that would do this for me. The way I set up ATOM is with two windows, one on the left with the file including comments (which I make edits in) and one on the right which contains the comment less code and is updated as I work. Comment stripper currently supports python, c, haskell, javascript, html, clojure and perl. It can be used to convert a file to one without comments using this example command on a test c programme.

python commentStripper.py -i test.c -o test_out.c

it can also be used in watch mode where the programme output is continually updated by running the following example command on a test python programme.

python commentStripper.py -w -i test.py -o test_out.py

The code is on my github page and an explanation follows.

First make the necessary imports. optparse for dealing with command line options, time for delays, os for file management, and re for regular expressions.

The find_file_type function takes a static as input splits it into its name and its extension and checks the extension against a dictionary of extensions it knows how to deal with.

The check_time function checks to see if it has been more than five seconds since a file was modified using the os.stat function.

The gen_strip function does all the hard work. It reads through the file line by line looking for comment characters and strings. It takes as input filename and output filename and regular expressions for the start and end of single line comments and multi-line comments. These are defined in the individual functions for each programming language.

For example, the strip_python function contains the regular expressions that define the single and multi line comments and calls the gen__strip function with these.

The if statements just make sure the correct function is called depending on the programming language that needs to be stripped.

The main function uses optparser to read the options from the command line. The -i option specifies input files to be stripped. The -o option specifies the corresponding output filenames.

If -w is specified then the programme goes into an infinite loop and updates the files every 5 seconds, otherwise they are just stripped of comments and an output written.

Comments