import multiprocessing
import os
import string

from os import listdir
from os.path import isfile, join, isdir
from xmlrpc.client import Boolean


class MuffetConf:
    input = ''
    debug = False
    workersCount = multiprocessing.cpu_count()






class TFile():
    path = None
    subroot = None
    subpath = None
    mimetype = None
    extension = None

    def __init__(self, subroot, file):
        self.path = file
        self.subroot = subroot
        self.mimetype = 'amog/amog'
        filename, self.extension = os.path.splitext(file)
        self.extension = self.extension[1:]
        self.subpath = self.clear_subpath(file, subroot)

    def clear_subpath(self, full_path: string, new_subroot: string) -> string:
        a = os.path.abspath(full_path)
        b = os.path.abspath(new_subroot)
        return a[len(b) + 1:]



class ScanFilter:
    def is_target(self, file : TFile):
        return False

class Muffet:

    def __init__(self, conf):
        self.scanResults = []
        self.conf = None
        self.tasks = []
        self.workers = []

        self.conf = conf

    def run(self):
        for t in self.tasks:
            t.run()

    def scan(self, scan_filter : ScanFilter, target=None):

        if target is None: target = self.conf.input

        if not os.path.exists(target):
            return None

        files = listdir(target)

        if files is None or len(files) == 0: return;

        r = []
        for f in files:
            f = target + "/" + f
            if isfile(f):
                tf = TFile(self.conf.input, f)
                if scan_filter.is_target(tf):
                    r.append(tf)

            if isdir(f):

                self.scan(scan_filter, f)
        self.scanResults = self.scanResults + r
