Configuring the write_output() function

If the output destination is specified by the user, the write_output() function is called. Once invoked, we select the custodian ID from the database using the get_custodian() function, which is called on line 225. If found, we need to build a new query to determine the number of files associated with the custodian using the COUNT() SQL function. If the custodian is not found, an error is logged to alert the user that the custodian was unresponsive, as we can see on lines 234 through 237:

216 def write_output(conn, target, custodian):
217 """
218 The write_output function handles writing either the CSV or
219 HTML reports
220 :param conn: The sqlite3 database connection object
221 :param target: The output filepath
222 :param custodian: Name of the custodian
223 :return: None
224 """
225 custodian_id = get_custodian(conn, custodian)
226 cur = conn.cursor()
227 if custodian_id:
228 custodian_id = custodian_id[0]
229 sql = "SELECT COUNT(id) FROM Files "\
230 "where custodian = {}".format(
231 custodian_id)
232 cur.execute(sql)
233 count = cur.fetchone()
234 else:
235 logger.error(
236 'Could not find custodian in database. Please check '
237 'the input of the custodian name and database path')

If the custodian is found and the number of stored files is greater than zero, we check what type of report to generate. The conditional statements starting on line 239 check the size of count and the extension of the source. If count is not greater than zero or does not contain a value, then an error is logged on line 240. Otherwise, we check for the CSV file extension on line 241 and theĀ HTML file extension on line 243, calling the respective function if we find a match. If the source does not end in either of those file extensions, then an error is logged, stating that the file type could not be determined. Finally, if the code reaches the else statement on line 247, we log the fact that an unknown error occurred. We can see all of this in the following code:

239     if not count or not count[0] > 0:
240 logger.error('Files not found for custodian')
241 elif target.endswith('.csv'):
242 write_csv(conn, target, custodian_id)
243 elif target.endswith('.html'):
244 write_html(conn, target, custodian_id, custodian)
245 elif not (target.endswith('.html')or target.endswith('.csv')):
246 logger.error('Could not determine file type')
247 else:
248 logger.error('Unknown Error Occurred')