# Path: settings.py
# NETWORK_ADAPTATER = {
#     0: "Ethernet 802.3",
#     1: "Token Ring 802.5",
#     2: "Fiber Distributed Data Interface (FDDI)",
#     3: "Wide Area Network (WAN)",
#     4: "LocalTalk",
#     5: "Ethernet using DIX header format",
#     6: "ARCNET",
#     7: "ARCNET (878.2)",
#     8: "ATM",
#     9: "Wireless",
#     10: "Infrared Wireless",
#     11: "Bpc",
#     13: "CoWan",
#     14: "1394",
#     15: "Tunnel"
# }
# 
# Path: utils/utils.py
# def write_to_output(str_to_write, output, logger):
#     """Writes content to a file, encoding the string in UTF-8 for compatibility issues"""
#     try:
#         output.write(str_to_write.encode("utf-8"))
#     except UnicodeError:
#         logger.error(traceback.format_exc())
# 
# def get_csv_writer(csvfile):
#     return UnicodeWriter(csvfile, quoting=csv.QUOTE_ALL)
# 
# def write_to_json(header, arr_data, json_writer):
#     """Write data in json files using UTF-8"""
#     try:
#         json_writer.dump_json({
#             h: arr_data[index] for index, h in enumerate(header)
#         })
#     except IndexError:
#         json_writer.dump_json({
#             header[index]: d for index, d in enumerate(arr_data)
#         })
# 
# def close_json_writer(json_writer):
#     json_writer.close_current()
# 
# def get_json_writer(json_file):
#     return UnicodeJsonDumper(json_file)
# 
# def write_list_to_json(arr_data, json_writer):
#     """Write list data in json files using UTF-8"""
#     json_writer.list_to_json(arr_data)
# 
# def write_to_csv(arr_data, csv_writer):
#     """Writes contents to a CSV file using UTF-8"""
#     csv_writer.writerow(arr_data)
# 
# def get_terminal_decoded_string(string):
#     return string.decode(sys.stdout.encoding, "ignore")
# 
# def record_sha256_logs(fr, fw):
#     with open(fw, "a") as hash_file:
#         m = process_sha256(fr)
#         hash_file.write(fr + "," + m + "\n")
#         hash_file.close()
# 
# def process_md5(path):
#     with open(path, "rb") as f:
#         return hashlib.md5(f.read()).hexdigest()
# 
# def process_sha1(path):
#     with open(path, "rb") as f:
#         return hashlib.sha1(f.read()).hexdigest()


import os
import subprocess
import traceback
import psutil
import win32process
import re
import wmi
import datetime
from settings import NETWORK_ADAPTATER
from utils.utils import write_to_output, get_csv_writer, write_to_json, close_json_writer, get_json_writer,\
     write_list_to_json, write_to_csv, get_terminal_decoded_string, record_sha256_logs, process_md5, process_sha1

            record_sha256_logs(self.output_dir + self.computer_name + '_hash_processes' + self.rand_ext,
                               self.output_dir + self.computer_name + '_sha256.log')

    def _csv_list_share(self, share):
        self.logger.info("Health : Listing shares")
        with open(self.output_dir + '%s_shares' % self.computer_name + self.rand_ext, 'wb') as fw:
            csv_writer = get_csv_writer(fw)
            write_to_csv(["COMPUTER_NAME", "TYPE", "SHARE_NAME", "SHARE_PATH"], csv_writer)
            for name, path in share:
                write_to_csv([self.computer_name, 'shares', name, path], csv_writer)
        record_sha256_logs(self.output_dir + self.computer_name + '_shares' + self.rand_ext,
                           self.output_dir + self.computer_name + '_sha256.log')

    def _json_list_share(self, share):
        self.logger.info("Health : Listing shares")
        if self.destination == 'local':
            with open(self.output_dir + '%s_shares' % self.computer_name + self.rand_ext, 'wb') as fw:
                json_writer = get_json_writer(fw)
                to_write = [["COMPUTER_NAME", "TYPE", "SHARE_NAME", "SHARE_PATH"]]
                to_write += [[self.computer_name, 'shares', name, path] for name, path in share]
                write_list_to_json(to_write, json_writer)
            record_sha256_logs(self.output_dir + self.computer_name + '_shares' + self.rand_ext,
                               self.output_dir + self.computer_name + '_sha256.log')

    def _csv_list_drives(self, drives):
        self.logger.info("Health : Listing drives")
        with open(self.output_dir + '%s_list_drives' % self.computer_name + self.rand_ext, 'wb') as fw:
            csv_writer = get_csv_writer(fw)
            write_to_csv(["COMPUTER_NAME", "TYPE", "FAB", "PARTITIONS", "DISK", "FILESYSTEM"], csv_writer)
            for phCapt, partCapt, logicalCapt, fs in drives:

# Path: settings.py
# NETWORK_ADAPTATER = {
#     0: "Ethernet 802.3",
#     1: "Token Ring 802.5",
#     2: "Fiber Distributed Data Interface (FDDI)",
#     3: "Wide Area Network (WAN)",
#     4: "LocalTalk",
#     5: "Ethernet using DIX header format",
#     6: "ARCNET",
#     7: "ARCNET (878.2)",
#     8: "ATM",
#     9: "Wireless",
#     10: "Infrared Wireless",
#     11: "Bpc",
#     13: "CoWan",
#     14: "1394",
#     15: "Tunnel"
# }
# 
# Path: utils/utils.py
# def write_to_output(str_to_write, output, logger):
#     """Writes content to a file, encoding the string in UTF-8 for compatibility issues"""
#     try:
#         output.write(str_to_write.encode("utf-8"))
#     except UnicodeError:
#         logger.error(traceback.format_exc())
# 
# def get_csv_writer(csvfile):
#     return UnicodeWriter(csvfile, quoting=csv.QUOTE_ALL)
# 
# def write_to_json(header, arr_data, json_writer):
#     """Write data in json files using UTF-8"""
#     try:
#         json_writer.dump_json({
#             h: arr_data[index] for index, h in enumerate(header)
#         })
#     except IndexError:
#         json_writer.dump_json({
#             header[index]: d for index, d in enumerate(arr_data)
#         })
# 
# def close_json_writer(json_writer):
#     json_writer.close_current()
# 
# def get_json_writer(json_file):
#     return UnicodeJsonDumper(json_file)
# 
# def write_list_to_json(arr_data, json_writer):
#     """Write list data in json files using UTF-8"""
#     json_writer.list_to_json(arr_data)
# 
# def write_to_csv(arr_data, csv_writer):
#     """Writes contents to a CSV file using UTF-8"""
#     csv_writer.writerow(arr_data)
# 
# def get_terminal_decoded_string(string):
#     return string.decode(sys.stdout.encoding, "ignore")
# 
# def record_sha256_logs(fr, fw):
#     with open(fw, "a") as hash_file:
#         m = process_sha256(fr)
#         hash_file.write(fr + "," + m + "\n")
#         hash_file.close()
# 
# def process_md5(path):
#     with open(path, "rb") as f:
#         return hashlib.md5(f.read()).hexdigest()
# 
# def process_sha1(path):
#     with open(path, "rb") as f:
#         return hashlib.sha1(f.read()).hexdigest()


# Path: health/statemachine.py
import os
import subprocess
import traceback
import psutil
import win32process
import re
import wmi
import datetime
from settings import NETWORK_ADAPTATER
from utils.utils import write_to_output, get_csv_writer, write_to_json, close_json_writer, get_json_writer,\
     write_list_to_json, write_to_csv, get_terminal_decoded_string, record_sha256_logs, process_md5, process_sha1

            record_sha256_logs(self.output_dir + self.computer_name + '_hash_processes' + self.rand_ext,
                               self.output_dir + self.computer_name + '_sha256.log')

    def _csv_list_share(self, share):
        self.logger.info("Health : Listing shares")
        with open(self.output_dir + '%s_shares' % self.computer_name + self.rand_ext, 'wb') as fw:
            csv_writer = get_csv_writer(fw)
            write_to_csv(["COMPUTER_NAME", "TYPE", "SHARE_NAME", "SHARE_PATH"], csv_writer)
            for name, path in share:
                write_to_csv([self.computer_name, 'shares', name, path], csv_writer)
        record_sha256_logs(self.output_dir + self.computer_name + '_shares' + self.rand_ext,
                           self.output_dir + self.computer_name + '_sha256.log')

    def _json_list_share(self, share):
        self.logger.info("Health : Listing shares")
        if self.destination == 'local':
            with open(self.output_dir + '%s_shares' % self.computer_name + self.rand_ext, 'wb') as fw:
                json_writer = get_json_writer(fw)
                to_write = [["COMPUTER_NAME", "TYPE", "SHARE_NAME", "SHARE_PATH"]]
                to_write += [[self.computer_name, 'shares', name, path] for name, path in share]
                write_list_to_json(to_write, json_writer)
            record_sha256_logs(self.output_dir + self.computer_name + '_shares' + self.rand_ext,
                               self.output_dir + self.computer_name + '_sha256.log')

    def _csv_list_drives(self, drives):
        self.logger.info("Health : Listing drives")
        with open(self.output_dir + '%s_list_drives' % self.computer_name + self.rand_ext, 'wb') as fw:
            csv_writer = get_csv_writer(fw)
            write_to_csv(["COMPUTER_NAME", "TYPE", "FAB", "PARTITIONS", "DISK", "FILESYSTEM"], csv_writer)
            for phCapt, partCapt, logicalCapt, fs in drives:

                write_to_csv([self.computer_name, 'list_drives', phCapt, partCapt, logicalCapt, fs], csv_writer)
