# Path: ovizart/modules/traffic/log/logger.py
# class Logger:
# 
#     def __init__(self, log_name, log_mode):
#         logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
#         self.mode = log_mode
#         self.log = logging.getLogger(log_name)
#         self.log.setLevel(log_mode)
# 
#     def set_log_level(self, level="DEBUG"):
#         self.log.setLevel(level)
# 
#     def message(self, message):
#         if self.mode == "DEBUG":
#             self.log.debug(message)
#         if self.mode == "ERROR":
#             self.log.error(message)
#         if self.mode == "INFO":
#             self.log.info(message)
# 
# Path: ovizart/modules/utils/handler.py
# def generate_name_from_timestame():
#     timestamp = str(time.time())
#     md5 = hashlib.md5(timestamp)
#     return md5.hexdigest()


import datetime
import os
from django.conf import settings
from ovizart.modules.traffic.log.logger import Logger
from ovizart.modules.utils.handler import generate_name_from_timestame
from hachoir_core.cmd_line import unicodeFilename
from hachoir_core.stream import FileInputStream
from hachoir_regex.pattern import PatternMatching
from hachoir_core.stream.input import NullStreamError
        self.log = Logger("File Handler", "DEBUG")

    def create_dir(self):
        now = datetime.datetime.now()
        self.log.message("Now is: %s:" % now)
        directory_name = now.strftime("%d-%m-%y")
        self.log.message("Directory name: %s:" % directory_name)
        directory_path = "/".join([settings.PROJECT_ROOT, "uploads", directory_name])
        self.log.message("Directory path: %s" % directory_path)
        if not os.path.exists(directory_path):
            os.mkdir(directory_path)
            self.log.message("Directory created")
        # we need to create another directory also for each upload
        new_dir = generate_name_from_timestame()
        new_dir_path = "/".join([directory_path, new_dir])
        if not os.path.exists(new_dir_path):
            os.mkdir(new_dir_path)
            self.log.message("Directory created")
        self.upload_dir = new_dir_path

    def save_file(self, f):
        self.file_name = f.name
        self.file_path = "/".join([self.upload_dir, self.file_name])
        destination = open(self.file_path, 'wb+')
        for chunk in f.chunks():
            destination.write(chunk)
        destination.close()

    def search(self, file_path, strings=None):
        try:

# Path: ovizart/modules/traffic/log/logger.py
# class Logger:
# 
#     def __init__(self, log_name, log_mode):
#         logging.basicConfig(format='%(asctime)-6s: %(name)s - %(levelname)s - %(message)s')
#         self.mode = log_mode
#         self.log = logging.getLogger(log_name)
#         self.log.setLevel(log_mode)
# 
#     def set_log_level(self, level="DEBUG"):
#         self.log.setLevel(level)
# 
#     def message(self, message):
#         if self.mode == "DEBUG":
#             self.log.debug(message)
#         if self.mode == "ERROR":
#             self.log.error(message)
#         if self.mode == "INFO":
#             self.log.info(message)
# 
# Path: ovizart/modules/utils/handler.py
# def generate_name_from_timestame():
#     timestamp = str(time.time())
#     md5 = hashlib.md5(timestamp)
#     return md5.hexdigest()


# Path: ovizart/modules/file/handler.py
import datetime
import os
from django.conf import settings
from ovizart.modules.traffic.log.logger import Logger
from ovizart.modules.utils.handler import generate_name_from_timestame
from hachoir_core.cmd_line import unicodeFilename
from hachoir_core.stream import FileInputStream
from hachoir_regex.pattern import PatternMatching
from hachoir_core.stream.input import NullStreamError

        self.log = Logger("File Handler", "DEBUG")

    def create_dir(self):
        now = datetime.datetime.now()
        self.log.message("Now is: %s:" % now)
        directory_name = now.strftime("%d-%m-%y")
        self.log.message("Directory name: %s:" % directory_name)
        directory_path = "/".join([settings.PROJECT_ROOT, "uploads", directory_name])
        self.log.message("Directory path: %s" % directory_path)
        if not os.path.exists(directory_path):
            os.mkdir(directory_path)
            self.log.message("Directory created")
        # we need to create another directory also for each upload
        new_dir = generate_name_from_timestame()
        new_dir_path = "/".join([directory_path, new_dir])
        if not os.path.exists(new_dir_path):
            os.mkdir(new_dir_path)
            self.log.message("Directory created")
        self.upload_dir = new_dir_path

    def save_file(self, f):
        self.file_name = f.name
        self.file_path = "/".join([self.upload_dir, self.file_name])
        destination = open(self.file_path, 'wb+')
        for chunk in f.chunks():
            destination.write(chunk)
        destination.close()

    def search(self, file_path, strings=None):
        try:

            self.stream = FileInputStream(unicodeFilename(file_path), real_filename=file_path)