from .instruments import Instrument


class Dashboard(object):
    """Librato Dashboard Base class"""

    def __init__(self, connection, name, id=None, instruments=[]):
        self.connection = connection
        self.name = name
        self.instruments = []
        for i in instruments:
            self.add_instrument(i)
        self.id = id

    @classmethod
    def from_dict(cls, connection, data):
        """
        Returns a Dashboard object from a dictionary item,
        which is usually from librato's API
        """
        obj = cls(connection,
                  data['name'],
                  instruments=data['instruments'],
                  id=data['id'])
        return obj

    def get_payload(self):
        return {'name': self.name,
                'instruments': [x.id for x in self.instruments]}

    def add_instrument(self, instrument):
        if isinstance(instrument, Instrument):
            self.instruments.append(instrument)
        # We should handle this better, i.e. allowing instrument instantiation from integers, for consistency.
        elif isinstance(instrument, int):
            # Aaah, cascading GETs!
            instrument = self.connection.get_instrument(instrument)
            # get_instrument throws a librato.exceptions.NotFound: [404] request: Not Found
            self.instruments.append(instrument)

    def save(self):
        self.connection.update_dashboard(self)

@@ -1,41 +1,4 @@
-from .instruments import Instrument%0A%0A
 %0Acla
@@ -123,12 +123,19 @@
 ment
-s=%5B%5D
+_dicts=None
 ):%0A 
@@ -222,15 +222,51 @@
 ment
+_id
 s = %5B%5D%0A
+        self._instruments = None%0A
     
@@ -278,16 +278,17 @@
 or i in 
+(
 instrume
@@ -289,17 +289,29 @@
 strument
-s
+_dicts or %5B%5D)
 :%0A      
@@ -321,20 +321,16 @@
    self.
-add_
 instrume
@@ -331,18 +331,35 @@
 strument
-(i
+_ids.append(i%5B'id'%5D
 )%0A      
@@ -650,16 +650,21 @@
 strument
+_dict
 s=data%5B'
@@ -827,23 +827,8 @@
 ts':
- %5Bx.id for x in
  sel
@@ -839,17 +839,22 @@
 strument
-s
+_ids%5B:
 %5D%7D%0A%0A    
@@ -861,11 +861,11 @@
 def 
-add
+get
 _ins
@@ -875,25 +875,14 @@
 ment
+s
 (self
-, instrument
 ):%0A 
@@ -895,42 +895,33 @@
  if 
-isinstance(instrument, Instrument)
+self._instruments is None
 :%0A  
@@ -922,37 +922,32 @@
 ne:%0A            
-self.
 instruments.appe
@@ -945,163 +945,40 @@
 ents
-.append(instrument)%0A        # We should handle this better, i.e. allowing instrument instantiation from integers, for consistency.%0A        elif isinstance(
+ = %5B%5D%0A            for i in self.
 inst
@@ -987,52 +987,18 @@
 ment
-, int)
+_ids
 :%0A    
-        # Aaah, cascading GETs!%0A
     
@@ -1015,19 +1015,25 @@
 strument
- = 
+s.append(
 self.con
@@ -1056,25 +1056,17 @@
 rument(i
-nstrument
+)
 )%0A      
@@ -1075,13 +1075,13 @@
     
-# get
+self.
 _ins
@@ -1091,71 +1091,24 @@
 ment
- throws a librato.exceptions.NotFound: %5B404%5D request: Not Found
+s = instruments%0A
 %0A   
@@ -1108,33 +1108,37 @@
 ts%0A%0A        
-   
+return
  self.
+_
 instruments.
@@ -1140,27 +1140,11 @@
 ents
-.append(instrument)
+%5B:%5D
 %0A%0A  

rework the way dashboards get instantiated. make the instrument queries happen lazily when requested, rather than during dashboard instantiation