from lib.firehose_orm import Analysis, Result, Generator, Sut
#from lib.firehose_noslo

from app import session


### EXCEPTIONS ###

class Http500Error(Exception): pass
class Http404Error(Exception): pass


### MODEL CLASSES ###


def to_dict(elem):
    """
    serializes a SQLAchemy response into a dict
    /!\ backrefs are followed and can do infinite recursion TODO
    """
    if elem is None:
        return None
    
    if isinstance(elem, list):
        return [to_dict(e) for e in elem]
    else:
        res = dict()
        cls = type(elem)
        for attr_name in cls._sa_class_manager.local_attrs:
                attr = getattr(elem, attr_name)
                if type(attr) in [int, float, str, unicode]:#_string_type]
                    res[attr_name] = attr
                else:
                    res[attr_name] = to_dict(attr)
        return res



class FHGeneric(object):
    def __init__(self, fh_class):
        self.fh_class = fh_class
    
    def all(self):
        elem = session.query(self.fh_class).all()
        return to_dict(elem)
    
    def id(self, id):
        elem = session.query(self.fh_class).filter(
            self.fh_class.id == id).first()
        return to_dict(elem)
                


# class MetaInfo(object):
#     def __init__(self):
#         self.generators = Generator_app().all()
#         self.suts = Sut_app().all()
#         self.analyses = Analysis_app().last_ones()

#     def get_generators(self):
#         r = []#dict()
#         for gen in self.generators:
#             r.append(dict(name=gen.name, version=gen.version))
#         return r
#         #return self.get_generators

#     def get_suts(self):
#         r = []
#         for s in self.suts:
#             r.append(dict(name=s.name, version=s.version, type=s.type))
#         return r
    
#     def get_analyses(self):
#         r = []
#         for a in self.analyses:
#             r.append(dict(id = a.id, generator = a.metadata.generator.name))
#                                                    # fixme
#         return r


# class Generator_app(object):
#     def __init__(self):
#         pass
    
#     def all(self):
#         return session.query(Generator).all()

# class Sut_app(object):
#     def __init__(self):
#         pass
    
#     def all(self):
#         return session.query(Sut).all()

# class Analysis_app(object):
#     def __init__(self):
#         pass
    
#     def all(self):
#         self.analyses = session.query(Analysis).all()
#         return self.analyses
    
#     def last_ones(self, n=10):
#         self.last_ones = session.query(
#             Analysis).order_by(Analysis.id.desc()).limit(n)
#         return self.last_ones


# class Result_app(object):
#     def __init__(self, id):
#         self.result = session.query(Result).filter(Result.id == id).first()
#         if not(self.result):
#             raise Http404Error("Result with id %d doesn't exist." % id)
        
#     def infos(self):
#         return self.result


# class Message_app(object):
#     def __init__(self):
#         self.message = session.query(Message).first()
    
#     def __repr__(self):
#         return self.message.__repr__()

@@ -31,16 +31,38 @@
 nalysis,
+ Issue, Failure, Info,
  Result,
@@ -892,106 +892,136 @@
 es%0A%0A
-%0A%0Aclass FHGeneric(object):%0A    def __init__(self, fh_class):%0A        self.fh_class = fh_class%0A    
+fh_types = dict(%0A    result = Result,%0A    issue = Issue,%0A    failure = Failure,%0A    info = Info,%0A    )%0A%0Aclass FHGeneric(object):
 %0A   
@@ -1142,16 +1142,33 @@
 f, id):%0A
+        try:%0A    
         
@@ -1219,24 +1219,28 @@
             
+    
 self.fh_clas
@@ -1271,275 +1271,69 @@
     
-return to_dict(elem)%0A                %0A%0A%0A# class MetaInfo(object):%0A#     def __init__(self):%0A#         self.generators = Generator_app().all()%0A#         self.suts = Sut_app().all()%0A#         self.analyses = Analysis_app().last_ones()%0A%0A#     def get_generators(self)
+except:%0A            raise Http500Error%0A        %0A        try
 :%0A
-#
     
@@ -1341,703 +1341,225 @@
     
-r = %5B%5D#dict()%0A#         for gen in self.generators:%0A#             r.append(dict(name=gen.name, version=gen.version))%0A#         return r%0A#         #return self.get_generators%0A%0A#     def get_suts(self):%0A#         r = %5B%5D%0A#         for s in self.suts:%0A#             r.append(dict(name=s.name, version=s.version, type=s.type))%0A#         return r%0A    %0A#     def get_analyses(self):%0A#         r = %5B%5D%0A#         for a in self.analyses:%0A#             r.append(dict(id = a.id, generator = a.metadata.generator.name))%0A#                                                    # fixme%0A#         return r%0A%0A%0A# class Generator_app(object):%0A#     def __init__(self):%0A#         pass%0A    %0A#     def all(self):%0A#
+   id = elem.id # raises error if the element doesn't exist%0A        except:%0A            raise Http404Error(%22This element (%25s, %25i) doesn't exist.%22%0A                               %25 (self.fh_class.__name__, id))%0A
         
  ret
@@ -1558,140 +1558,55 @@
     
- 
 return 
-session.query(Generator).all()%0A%0A# class Sut_app(object):%0A#     def __init__(self):%0A#         pass%0A    %0A#     def all
+to_dict(elem)%0A%0A    def get_class
 (self):%0A
 #   
@@ -1593,34 +1593,32 @@
 et_class(self):%0A
-# 
         return s
@@ -1622,68 +1622,66 @@
 n se
-ssion.query(Sut).all()%0A%0A# class Analysis_app(object
+lf.fh_class.__name__%0A%0Aclass Generator_app(FHGeneric
 ):%0A
-#
     
- 
 def 
@@ -1696,26 +1696,24 @@
 (self):%0A
-# 
         
 pass%0A   
@@ -1708,174 +1708,62 @@
     
-pass%0A    %0A#     def all(self):%0A#         self.analyses = session.query(Analysis).all()%0A#         return self.analyses%0A    %0A#     def last_ones(self, n=10
+self.fh_class = Generator%0A%0A    def all(self
 ):%0A
-# 
         
 self
@@ -1758,30 +1758,20 @@
         
-s
 el
-f.last_ones
+em
  = sessi
@@ -1783,588 +1783,80 @@
 ery(
-%0A#             Analysis).order_by(Analysis.id.desc()).limit(n)%0A#         return self.last_ones%0A%0A%0A# class Result_app(object):%0A#     def __init__(self, id):%0A#         self.result = session.query(Result).filter(Result.id == id).first()%0A#         if not(self.result):%0A#             raise Http404Error(%22Result with id %25d doesn't exist.%22 %25 id)%0A        %0A#     def infos(self):%0A#         return self.result%0A%0A%0A# class Message_app(object):%0A#     def __init__(self):%0A#         self.message = session.query(Message).first()%0A    %0A#     def __repr__(self):%0A#         return self.message.__repr__()
+self.fh_class).order_by(Generator.name).all()%0A        return to_dict(elem)%0A
 %0A

add Generator_app class