# Path: appAux.py
# def update_progress(progress, text="Progress", logmessage=False):
#     if not stdout_redirect():
#         oh = sys.stdout
#     else:
#         oh = sys.stderr
# 
#     barLength = 25  # Modify this to change the length of the progress bar
#     status = ""
#     if isinstance(progress, int):
#         progress = float(progress)
#     if not isinstance(progress, float):
#         progress = 0
#         status = "error: progress var must be float\r\n"
#     if progress < 0:
#         progress = 0
#         status = "Halt...\r\n"
#     if progress > 1:
#         status = "Progress out of bounds...\r\n"
#     if progress == 1:
#         progress = 1
#         oh.write('\x1b[2K\r')
#         oh.flush()
#         return ""
#     block = int(round(barLength * progress))
#     if logmessage:
#         text = "{0}: [{1}] {2}% {3}\033[K".format(text, "#" * block + "-" * (barLength - block), round(progress * 100, 2), status)
#         return text
#     else:
#         text = "\r{0}: [{1}] {2}% {3}\033[K".format(text, "#" * block + "-" * (barLength - block), round(progress * 100, 2), status)
#         oh.write(text)
#         oh.flush()
# 
# def outputcolum(data):
#     # Calculate terminal size
#     maxStrLength = getTerminalWidth()
#     if settings.rawOutput or stdout_redirect(): maxStrLength = 1000
# 
#     # Calculate number of fields and maxStrLengthField
#     # todo: grab max size per field to do a more intelligent distribution of available term realestate
#     num_fields = len(data[len(data)-1][1])
#     maxStrLengthField = maxStrLength / num_fields
# 
#     # Truncate fields
#     # todo: There has to be a better approach to this
#     for ii in xrange(0, len(data)):
#         tmpList = []
#         for i in xrange(0, len(data[ii][1])):
#             tmpList.append(str(data[ii][1][i])[:maxStrLengthField] + (str(data[ii][1][i])[maxStrLengthField:] and '...'))
#         t1 = []
#         t1.append(data[ii][0])
#         t1.append(tuple(tmpList))
#         data[ii] = t1
#         data[ii][1] = tuple(tmpList)
#     widths = [max(map(len,map(str, col))) for col in zip(*[x[1] for x in data])]
#     for row in data:
#         if settings.rawOutput or stdout_redirect():
#             print ("  ".join((str(val).ljust(width) for val, width in zip(row[1], widths))))
#         else: print colored("  ".join((str(val).ljust(width) for val, width in zip(row[1], widths))), row[0])
#     return data
# 
# def file_size(file_name_fullpath):
#     if ".zip/" in file_name_fullpath:
#         file_object = loadFile(file_name_fullpath)
#         file_object.seek(0, os.SEEK_END)
#         return file_object.tell()
#     else: return os.path.getsize(file_name_fullpath)


import settings
import logging
import multiprocessing
import collections
import os
import re
import time
import ntpath
import appDB
import hashlib
import operator
import glob
from contextlib import closing
from appAux import update_progress, outputcolum, file_size
from datetime import timedelta
from collections import namedtuple
from namedlist import namedlist
        from itertools import islice
                                pass

                        # We dump the data to the output file/s
                        saveSearchData(record, self.searchType, regex_hit_name, text_file, markdown_file)

                    # Update progress counter
                    with self.val.get_lock():
                        self.val.value += 1

        # Dump hit histogram
        time.sleep(0.5)
        for x in sorted(hit_dict.values(), key=operator.itemgetter(0), reverse=True):
            if x[0] > 0:
                self.hitHistogram_queue.put((x[1], x[2], x[0]))

def retrieveSearchData(rowID, DB, search_space):
    queryRecordList = ["HostName","FilePath","FileName","LastModified","LastUpdate","Size","ExecFlag","RowID","EntryType","FirstRun","SHA1","Search_Space"]
    queryRecordFields = namedlist("queryRecordList", queryRecordList, default=None)

    # Grab all fields in the queryRecordList
    selectQuery = ','.join(queryRecordList)
    selectQuery = selectQuery.replace('Search_Space', search_space)
    # Execute the query
    entry = DB.Query("SELECT %s FROM Entries_FilePaths INNER JOIN Hosts \
        ON Entries_FilePaths.HostID = Hosts.HostID WHERE RowID = '%s'" % (selectQuery, rowID))[0]

    # todo: There has to be a more pythonic way to do this
    record = queryRecordFields()
    tmpDict = dict(zip(queryRecordList, entry))
    i = 0

# Path: appAux.py
# def update_progress(progress, text="Progress", logmessage=False):
#     if not stdout_redirect():
#         oh = sys.stdout
#     else:
#         oh = sys.stderr
# 
#     barLength = 25  # Modify this to change the length of the progress bar
#     status = ""
#     if isinstance(progress, int):
#         progress = float(progress)
#     if not isinstance(progress, float):
#         progress = 0
#         status = "error: progress var must be float\r\n"
#     if progress < 0:
#         progress = 0
#         status = "Halt...\r\n"
#     if progress > 1:
#         status = "Progress out of bounds...\r\n"
#     if progress == 1:
#         progress = 1
#         oh.write('\x1b[2K\r')
#         oh.flush()
#         return ""
#     block = int(round(barLength * progress))
#     if logmessage:
#         text = "{0}: [{1}] {2}% {3}\033[K".format(text, "#" * block + "-" * (barLength - block), round(progress * 100, 2), status)
#         return text
#     else:
#         text = "\r{0}: [{1}] {2}% {3}\033[K".format(text, "#" * block + "-" * (barLength - block), round(progress * 100, 2), status)
#         oh.write(text)
#         oh.flush()
# 
# def outputcolum(data):
#     # Calculate terminal size
#     maxStrLength = getTerminalWidth()
#     if settings.rawOutput or stdout_redirect(): maxStrLength = 1000
# 
#     # Calculate number of fields and maxStrLengthField
#     # todo: grab max size per field to do a more intelligent distribution of available term realestate
#     num_fields = len(data[len(data)-1][1])
#     maxStrLengthField = maxStrLength / num_fields
# 
#     # Truncate fields
#     # todo: There has to be a better approach to this
#     for ii in xrange(0, len(data)):
#         tmpList = []
#         for i in xrange(0, len(data[ii][1])):
#             tmpList.append(str(data[ii][1][i])[:maxStrLengthField] + (str(data[ii][1][i])[maxStrLengthField:] and '...'))
#         t1 = []
#         t1.append(data[ii][0])
#         t1.append(tuple(tmpList))
#         data[ii] = t1
#         data[ii][1] = tuple(tmpList)
#     widths = [max(map(len,map(str, col))) for col in zip(*[x[1] for x in data])]
#     for row in data:
#         if settings.rawOutput or stdout_redirect():
#             print ("  ".join((str(val).ljust(width) for val, width in zip(row[1], widths))))
#         else: print colored("  ".join((str(val).ljust(width) for val, width in zip(row[1], widths))), row[0])
#     return data
# 
# def file_size(file_name_fullpath):
#     if ".zip/" in file_name_fullpath:
#         file_object = loadFile(file_name_fullpath)
#         file_object.seek(0, os.SEEK_END)
#         return file_object.tell()
#     else: return os.path.getsize(file_name_fullpath)


# Path: appSearch.py
import settings
import logging
import multiprocessing
import collections
import os
import re
import time
import ntpath
import appDB
import hashlib
import operator
import glob
from contextlib import closing
from appAux import update_progress, outputcolum, file_size
from datetime import timedelta
from collections import namedtuple
from namedlist import namedlist
        from itertools import islice

                                pass

                        # We dump the data to the output file/s
                        saveSearchData(record, self.searchType, regex_hit_name, text_file, markdown_file)

                    # Update progress counter
                    with self.val.get_lock():
                        self.val.value += 1

        # Dump hit histogram
        time.sleep(0.5)
        for x in sorted(hit_dict.values(), key=operator.itemgetter(0), reverse=True):
            if x[0] > 0:
                self.hitHistogram_queue.put((x[1], x[2], x[0]))

def retrieveSearchData(rowID, DB, search_space):
    queryRecordList = ["HostName","FilePath","FileName","LastModified","LastUpdate","Size","ExecFlag","RowID","EntryType","FirstRun","SHA1","Search_Space"]
    queryRecordFields = namedlist("queryRecordList", queryRecordList, default=None)

    # Grab all fields in the queryRecordList
    selectQuery = ','.join(queryRecordList)
    selectQuery = selectQuery.replace('Search_Space', search_space)
    # Execute the query
    entry = DB.Query("SELECT %s FROM Entries_FilePaths INNER JOIN Hosts \
        ON Entries_FilePaths.HostID = Hosts.HostID WHERE RowID = '%s'" % (selectQuery, rowID))[0]

    # todo: There has to be a more pythonic way to do this
    record = queryRecordFields()
    tmpDict = dict(zip(queryRecordList, entry))
    i = 0

    for field in queryRecordList: