# Path: lightbulb/core/utils/common.py
# def save_model(save, model):
#     if not isinstance(save, basestring):
#         return
#     if save == "REGEX":
#         regex = model.to_regex()
#         return "Regular Expression", regex
#     elif save == "STRING":
#         string = model.shortest_string()
#         return "Shortest String", string
#     elif save.lower() in ['true', '1', 't', 'y', 'yes']:
#         home = expanduser("~")
#         timestr = time.strftime("%Y%m%d-%H%M%S")
#         if not os.path.exists(home+"/.LightBulb/"):
#             os.makedirs(home+"/.LightBulb/")
#         if not os.path.exists(home+"/.LightBulb/models"):
#             os.makedirs(home+"/.LightBulb/models")
#         model.save(home+"/.LightBulb/models/"+timestr+".fst")
#         ruleset_file_meta = open(home+"/.LightBulb/models/"+timestr+".py", "w")
#         ruleset_file_meta.write(
#             "META = {\n\
#                     'author': '"+getpass.getuser()+"',\n\
#                     'description': 'Saved model on "+timestr+"',\n\
#                     'type':'FST',\n\
#                     'comments': []\n\
#             }")
#         ruleset_file_meta.close()
#         return "Saved Model", home+"/.LightBulb/models/"+timestr+".fst"
#     elif save.lower() in ['False', '2', 'f', 'n', 'no']:
#         return
#     else:
#         model.save(save)
#         return "Saved Model", save
# 
# def findlibrary(path):
#     path = re.sub('.*my_saved_models/', expanduser("~") + "/.LightBulb/models/", path)
#     path = re.sub('.*my_saved_regex/', expanduser("~") + "/.LightBulb/regex/", path)
#     path = re.sub('.*my_saved_trees/', expanduser("~") + "/.LightBulb/trees/", path)
#     path = re.sub('.*my_saved_grammars/', expanduser("~") + "/.LightBulb/grammars/", path)
#     path = path.replace('{library}', imp.find_module('lightbulb')[1]+"/data/")
#     return path


from symautomata.dfa import DFA
from symautomata.flex2fst import Flexparser
from symautomata.alphabet import createalphabet
from lightbulb.core.utils.common import save_model, findlibrary
import json

META = {
    'author': 'George Argyros, Ioannis Stais',
    'description': 'Perform automata operations on learned models or flex files',
    'type':'Distinguisher',
    'options': [
        ('FILE_A', None, True, 'Model A'),
        ('FILE_TYPE_A', "FST", True, 'Possible values: FST, FLEX'),
        ('FILE_B', None, False, 'Model B'),
        ('FILE_TYPE_B', "FST", False, 'Possible values: FST, FLEX'),
        ('OPERATION', None, False, 'Possible values: INTERSECT, UNION, DIFFERENCE, COMPLEMENT, None'),
        ('ALPHABET', None, False, 'File containing the alphabet'),
        ('RESULT', None, False, 'Possible values: STRING, REGEX, or a file path to save model'),
    ],
    'comments': ['Sample comment 1', 'Sample comment 2']
}



class Module():


    def __init__(self, configuration):
        self.mma = self.read_file(findlibrary(configuration['FILE_A']), findlibrary(configuration['FILE_TYPE_A']))
        self.states_a = len(self.mma)

# Path: lightbulb/core/utils/common.py
# def save_model(save, model):
#     if not isinstance(save, basestring):
#         return
#     if save == "REGEX":
#         regex = model.to_regex()
#         return "Regular Expression", regex
#     elif save == "STRING":
#         string = model.shortest_string()
#         return "Shortest String", string
#     elif save.lower() in ['true', '1', 't', 'y', 'yes']:
#         home = expanduser("~")
#         timestr = time.strftime("%Y%m%d-%H%M%S")
#         if not os.path.exists(home+"/.LightBulb/"):
#             os.makedirs(home+"/.LightBulb/")
#         if not os.path.exists(home+"/.LightBulb/models"):
#             os.makedirs(home+"/.LightBulb/models")
#         model.save(home+"/.LightBulb/models/"+timestr+".fst")
#         ruleset_file_meta = open(home+"/.LightBulb/models/"+timestr+".py", "w")
#         ruleset_file_meta.write(
#             "META = {\n\
#                     'author': '"+getpass.getuser()+"',\n\
#                     'description': 'Saved model on "+timestr+"',\n\
#                     'type':'FST',\n\
#                     'comments': []\n\
#             }")
#         ruleset_file_meta.close()
#         return "Saved Model", home+"/.LightBulb/models/"+timestr+".fst"
#     elif save.lower() in ['False', '2', 'f', 'n', 'no']:
#         return
#     else:
#         model.save(save)
#         return "Saved Model", save
# 
# def findlibrary(path):
#     path = re.sub('.*my_saved_models/', expanduser("~") + "/.LightBulb/models/", path)
#     path = re.sub('.*my_saved_regex/', expanduser("~") + "/.LightBulb/regex/", path)
#     path = re.sub('.*my_saved_trees/', expanduser("~") + "/.LightBulb/trees/", path)
#     path = re.sub('.*my_saved_grammars/', expanduser("~") + "/.LightBulb/grammars/", path)
#     path = path.replace('{library}', imp.find_module('lightbulb')[1]+"/data/")
#     return path


# Path: lightbulb/modules/models_op.py
from symautomata.dfa import DFA
from symautomata.flex2fst import Flexparser
from symautomata.alphabet import createalphabet
from lightbulb.core.utils.common import save_model, findlibrary
import json


META = {
    'author': 'George Argyros, Ioannis Stais',
    'description': 'Perform automata operations on learned models or flex files',
    'type':'Distinguisher',
    'options': [
        ('FILE_A', None, True, 'Model A'),
        ('FILE_TYPE_A', "FST", True, 'Possible values: FST, FLEX'),
        ('FILE_B', None, False, 'Model B'),
        ('FILE_TYPE_B', "FST", False, 'Possible values: FST, FLEX'),
        ('OPERATION', None, False, 'Possible values: INTERSECT, UNION, DIFFERENCE, COMPLEMENT, None'),
        ('ALPHABET', None, False, 'File containing the alphabet'),
        ('RESULT', None, False, 'Possible values: STRING, REGEX, or a file path to save model'),
    ],
    'comments': ['Sample comment 1', 'Sample comment 2']
}



class Module():


    def __init__(self, configuration):
        self.mma = self.read_file(findlibrary(configuration['FILE_A']), findlibrary(configuration['FILE_TYPE_A']))
        self.states_a = len(self.mma)

        self.mmb = self.read_file(findlibrary(configuration['FILE_B']), findlibrary(configuration['FILE_TYPE_B']))