Source

flowrate / flowrate / run.py

Diff from to

flowrate/run.py

 import simplejson
 
 import flowrate
-from flowrate import dbutil
+from flowrate import dbutil, ledger, flows
 from flowrate.variables import environment as env
 
 
     start.priority = 70
 
 
-class EnvLoader(plugins.SimplePlugin):
+class DataLoader(plugins.SimplePlugin):
 
     def __init__(self, bus):
         self.bus = bus
 
     def start(self):
+        # Do this before flows since they need variables to obligate/fulfill.
         self.bus.log("Loading Flowrate variables...")
 
         for row in flowrate.db.execute("SELECT * FROM variables;"
                                        ).fetchall():
             env.bind(row['name'], row['source'])
-
         env.calc_all()
 
         #print "Environment:"
         #for name, source in env.variables.iteritems():
         #    print name, source, '=>', env.locals[name]
 
+        self.bus.log("Loading Flowrate accounts...")
+        ledger.Account.load_all()
+
+        # Do this first because Flow.load_all tries to obligate
+        self.bus.log("Loading Flowrate transactions...")
+        ledger.Transaction.load_all()
+
+        self.bus.log("Loading Flowrate flows...")
+        flows.Flow.load_all()
+
+        self.bus.log("Calculating Flowrate obligations...")
+        for flow in flows.flows.itervalues():
+            self.bus.log(flow['description'])
+            flow.obligate()
+
     start.priority = Postgres.start.priority + 5
 
 
 def run(config):
     cherrypy.engine.timeout_monitor.unsubscribe()
     Postgres(cherrypy.engine, config['db_info']).subscribe()
-    EnvLoader(cherrypy.engine).subscribe()
+    DataLoader(cherrypy.engine).subscribe()
     FlowrateApp(cherrypy.engine, config).subscribe()
     cherrypy.engine.start()
     cherrypy.engine.block()