# Path: owade/models.py
# class Value(models.Model):
#     #key = models.CharField(max_length=32)
#     key = models.TextField()
#     value = models.TextField(blank=True, null=True)
# 
#     category = models.ForeignKey(Category)
# 
#     def __unicode__(self):
#         return "%s: %s" % (self.key, self.value)
# 
# Path: owade/models.py
# class Category(models.Model):
#     #name = models.CharField(max_length=32)
#     name = models.TextField()
#     description = models.TextField(blank=True)
# 
#     partition = models.ForeignKey(Partition, null=True)
#     father = models.ForeignKey('self', null=True)
# 
#     def __unicode__(self):
#         if self.description != "":
#             return "%s (%s)" % (self.name, self.description)
#         return "%s" % (self.name)
# 
# Path: owade/launcher.py
# class Launcher(Thread):
#     ## Constructor
#     # @param internLog An instance of the Log class, replace stdout and stderr for the Launcher
#     # @param terminalLog An instance of the Log class, given to subprocess to get binaries stdout and stderr
#     def __init__(self, internLog, terminalLog):
#         Thread.__init__(self)
#         self.internLog_ = internLog
#         self.terminalLog_ = terminalLog
#         self.cmd_ = ""
#         self.process_ = None
#         self.success_ = False
# 
#     ## Launch the unix cmd save in self.cmd_.
#     # This thread keep updating terminalLog_.
#     # Call final method at the end.
#     def run(self):
# 	try:
# 	    self.process_ = Popen(self.cmd_, universal_newlines=True, stderr=PIPE, stdout=PIPE)
# 	except:
# 	    self.internLog_.addLog("".join(self.cmd_) + " Failed to execute" , 2 )
#         while self.process_.returncode is None:
#             self.process_.poll()
#             try:
#                 input,o,e = select([self.process_.stdout, self.process_.stderr], [], [], 1)
#             except:
#                 input = []
#             for s in input:
#                 if s == self.process_.stdout:
#                     line = self.process_.stdout.readline()
#                     self.terminalLog_.addLog(line, 1)
#                 if s == self.process_.stderr:
#                     line = self.process_.stderr.readline()
#                     self.terminalLog_.addLog(line, 2)
#         line = True
#         while line != '':
#             line = self.process_.stdout.readline()
#             self.terminalLog_.addLog(line, 1)
#         line = True
#         while line != '':
#             line = self.process_.stderr.readline()
#             self.terminalLog_.addLog(line, 2)
#         self.final()
#         self.process_ = None
# 
#     ## Abstract method that has to be defined. Define a behavior after the subprocess.
#     # You shound set the success_ attribute to True (if deserved) inside this method, either by
#     # checking the terminal log or the returnvalue of process_.
#     def final(self):
#         raise NotImplementedError("Subclasses should implement this!")
# 
#     ## Return le statut du thread (was originaly designed for more, a bit redundent now with isAlive())
#     # @return False if the Thread is still alive.
#     def available(self):
#         if self.isAlive():
#             return False
#         else:
#             return True
# 
#     ## Interupt the Launcher by killing the subprocess
#     def interupt(self):
#         if self.isAlive():
#             if self.process_ != None:
#                 self.process_.kill()
#                 return True
#         return False
# 
#     ## An instance of the Log class, replace stdout and stderr for a Process
#     internLog_ = None
#     ## An instance of the Log class, only usefull if a Launcher is used
#     terminalLog_ = None
#     ## The unix to be launcher as a subprocess
#     cmd_ = None
#     ## The subprocess (launched or not)
#     process_ = None
#     ## The result of the Launcher (initial = False).
#     # You have to set it to True (if deserved) in the final method.
#     success_ = None


import re
from threading import Thread
from owade.models import Value
from owade.models import Category
from owade.launcher import Launcher
from owade.models import *
from threading import Thread


    def updateDbGenericDic(self, dic, partition):
        try:
            self.recurseUpdateDbGenericDic(dic, partition, None)
        except Exception as exp:
            print exp.message
            self.internLog_.addLog("Update database failed for dic %s" % (str(dic)), 2)
            return False
        self.internLog_.addLog("Update database success", 1)
        return True

    def recurseGetDbGenericDic(self, father):
        dic = {}
        for category in father.category_set.all():
            dic[category.name] = self.recurseGetDbGenericDic(category)
        for value in father.value_set.all():
            dic[value.key] = value.value
        return dic

    def getDbGenericDic(self, name, partition, father=None):
        try:
            father = Category.objects.select_related().get(name__iexact=name, partition=partition, father=father)
        except Exception as exp:
            print exp
            return None
        return self.recurseGetDbGenericDic(father)

    def overWriteCategory(self, partition, name, overWrite):
        try:

# Path: owade/models.py
# class Value(models.Model):
#     #key = models.CharField(max_length=32)
#     key = models.TextField()
#     value = models.TextField(blank=True, null=True)
# 
#     category = models.ForeignKey(Category)
# 
#     def __unicode__(self):
#         return "%s: %s" % (self.key, self.value)
# 
# Path: owade/models.py
# class Category(models.Model):
#     #name = models.CharField(max_length=32)
#     name = models.TextField()
#     description = models.TextField(blank=True)
# 
#     partition = models.ForeignKey(Partition, null=True)
#     father = models.ForeignKey('self', null=True)
# 
#     def __unicode__(self):
#         if self.description != "":
#             return "%s (%s)" % (self.name, self.description)
#         return "%s" % (self.name)
# 
# Path: owade/launcher.py
# class Launcher(Thread):
#     ## Constructor
#     # @param internLog An instance of the Log class, replace stdout and stderr for the Launcher
#     # @param terminalLog An instance of the Log class, given to subprocess to get binaries stdout and stderr
#     def __init__(self, internLog, terminalLog):
#         Thread.__init__(self)
#         self.internLog_ = internLog
#         self.terminalLog_ = terminalLog
#         self.cmd_ = ""
#         self.process_ = None
#         self.success_ = False
# 
#     ## Launch the unix cmd save in self.cmd_.
#     # This thread keep updating terminalLog_.
#     # Call final method at the end.
#     def run(self):
# 	try:
# 	    self.process_ = Popen(self.cmd_, universal_newlines=True, stderr=PIPE, stdout=PIPE)
# 	except:
# 	    self.internLog_.addLog("".join(self.cmd_) + " Failed to execute" , 2 )
#         while self.process_.returncode is None:
#             self.process_.poll()
#             try:
#                 input,o,e = select([self.process_.stdout, self.process_.stderr], [], [], 1)
#             except:
#                 input = []
#             for s in input:
#                 if s == self.process_.stdout:
#                     line = self.process_.stdout.readline()
#                     self.terminalLog_.addLog(line, 1)
#                 if s == self.process_.stderr:
#                     line = self.process_.stderr.readline()
#                     self.terminalLog_.addLog(line, 2)
#         line = True
#         while line != '':
#             line = self.process_.stdout.readline()
#             self.terminalLog_.addLog(line, 1)
#         line = True
#         while line != '':
#             line = self.process_.stderr.readline()
#             self.terminalLog_.addLog(line, 2)
#         self.final()
#         self.process_ = None
# 
#     ## Abstract method that has to be defined. Define a behavior after the subprocess.
#     # You shound set the success_ attribute to True (if deserved) inside this method, either by
#     # checking the terminal log or the returnvalue of process_.
#     def final(self):
#         raise NotImplementedError("Subclasses should implement this!")
# 
#     ## Return le statut du thread (was originaly designed for more, a bit redundent now with isAlive())
#     # @return False if the Thread is still alive.
#     def available(self):
#         if self.isAlive():
#             return False
#         else:
#             return True
# 
#     ## Interupt the Launcher by killing the subprocess
#     def interupt(self):
#         if self.isAlive():
#             if self.process_ != None:
#                 self.process_.kill()
#                 return True
#         return False
# 
#     ## An instance of the Log class, replace stdout and stderr for a Process
#     internLog_ = None
#     ## An instance of the Log class, only usefull if a Launcher is used
#     terminalLog_ = None
#     ## The unix to be launcher as a subprocess
#     cmd_ = None
#     ## The subprocess (launched or not)
#     process_ = None
#     ## The result of the Launcher (initial = False).
#     # You have to set it to True (if deserved) in the final method.
#     success_ = None


# Path: owade/process.py
import re
from threading import Thread
from owade.models import Value
from owade.models import Category
from owade.launcher import Launcher
from owade.models import *
from threading import Thread



    def updateDbGenericDic(self, dic, partition):
        try:
            self.recurseUpdateDbGenericDic(dic, partition, None)
        except Exception as exp:
            print exp.message
            self.internLog_.addLog("Update database failed for dic %s" % (str(dic)), 2)
            return False
        self.internLog_.addLog("Update database success", 1)
        return True

    def recurseGetDbGenericDic(self, father):
        dic = {}
        for category in father.category_set.all():
            dic[category.name] = self.recurseGetDbGenericDic(category)
        for value in father.value_set.all():
            dic[value.key] = value.value
        return dic

    def getDbGenericDic(self, name, partition, father=None):
        try:
            father = Category.objects.select_related().get(name__iexact=name, partition=partition, father=father)
        except Exception as exp:
            print exp
            return None
        return self.recurseGetDbGenericDic(father)

    def overWriteCategory(self, partition, name, overWrite):
        try:

            category = Category.objects.get(partition=partition, name__iexact=name)