The metadata_parser.py script contains a single function, main(), on line 45 that handles coordinating logic between our plugins and writers. At the top of the script, we call our imports we will use for this chapter. On lines 8 and 9, we specifically import our plugins and writers directories that we've created as follows:
001 """EXIF, ID3, and Office Metadata parser."""
002 from __future__ import print_function
003 import argparse
004 import os
005 import sys
006 import logging
007
008 import plugins
009 import writers
...
045 def main(input_dir, output_dir):
On line 133, we set up the arguments for our program. This script takes two positional arguments, an input and output directory, and an optional log argument to change the directory and name of the log file. Lines 142 through 154 focus on setting up the log, as in previous chapters. The lines are as follows:
131 if __name__ == '__main__':
132
133 parser = argparse.ArgumentParser(description=__description__,
134 epilog='Developed by ' +
135 __author__ + ' on ' +
136 __date__)
137 parser.add_argument('INPUT_DIR', help='Input Directory')
138 parser.add_argument('OUTPUT_DIR', help='Output Directory')
139 parser.add_argument('-l', help='File path of log file.')
140 args = parser.parse_args()
141
142 if args.l:
143 if not os.path.exists(args.l):
144 os.makedirs(args.l)
145 log_path = os.path.join(args.l, 'metadata_parser.log')
146 else:
147 log_path = 'metadata_parser.log'
148 logging.basicConfig(filename=log_path, level=logging.DEBUG,
149 format=('%(asctime)s | %(levelname)s | '
150 '%(message)s'), filemode='a')
151
152 logging.info('Starting Metadata_Parser')
153 logging.debug('System ' + sys.platform)
154 logging.debug('Version ' + sys.version)
On line 156, we create our output directory if the supplied output directory doesn't exist. This output directory is created with the makedirs() function. This function accepts a string representing the file path to a directory and creates the directory and any intermediate directories that don't exist in the file path. On line 159, we check whether the supplied input is a directory and whether it exists. If so, on line 161, the main() function is called, and the input and output directory arguments are passed. If the input doesn't exist or isn't a directory, we log and print the error and exit with status code 1. We have the following code:
156 if not os.path.exists(args.OUTPUT_DIR):
157 os.makedirs(args.OUTPUT_DIR)
158
159 if(os.path.exists(args.INPUT_DIR) and
160 os.path.isdir(args.INPUT_DIR)):
161 main(args.INPUT_DIR, args.OUTPUT_DIR)
162 else:
163 msg =('Supplied input directory doesn't exist or is'
164 'not a directory')
165 print('[-]', msg)
166 logging.error(msg)
167 sys.exit(1)