'''
 My purpose in life is to sync the mesosite stations table to other
databases.  This will hopefully remove some hackery
'''

import iemdb
import datetime
import sys
import psycopg2
import psycopg2.extras
MESOSITE = iemdb.connect("mesosite", bypass=True)
subscribers = ["iem","coop","hads","asos", "postgis"]

if len(sys.argv) == 2:
    print 'Running laptop syncing from upstream, assume iemdb is localhost!'
    MESOSITE = psycopg2.connect(database='mesosite', host='mesonet.agron.iastate.edu', user='nobody')
    subscribers.append('mesosite')

def sync(dbname):
    """
    Actually do the syncing, please
    """
    # connect to synced database
    dbconn = iemdb.connect( dbname )
    dbcursor = dbconn.cursor()
    # Figure out our latest revision
    dbcursor.execute("""
    SELECT max(modified), max(iemid) from stations
    """)
    row = dbcursor.fetchone()
    maxTS = (row[0] or datetime.datetime(1980,1,1))
    maxID = (row[1] or -1)
    # figure out what has changed!
    cur = MESOSITE.cursor(cursor_factory=psycopg2.extras.DictCursor)
    cur.execute("""SELECT * from stations WHERE modified > %s or iemid > %s""",
    (maxTS, maxID) )
    for row in cur:
        if row['iemid'] > maxID:
            dbcursor.execute("""INSERT into stations(iemid, network, id) 
             VALUES(%s,%s,%s) """, (row['iemid'], row['network'], row['id']))
        # insert queried stations
        dbcursor.execute("""UPDATE stations SET name = %(name)s, 
       state = %(state)s, elevation = %(elevation)s, online = %(online)s, 
       geom = %(geom)s, params = %(params)s, county = %(county)s, 
       plot_name = %(plot_name)s, climate_site = %(climate_site)s,
       wfo = %(wfo)s, archive_begin = %(archive_begin)s, 
       archive_end = %(archive_end)s, remote_id = %(remote_id)s, 
       tzname = %(tzname)s, country = %(country)s, 
       modified = %(modified)s, network = %(network)s, metasite = %(metasite)s,
       sigstage_low = %(sigstage_low)s, sigstage_action = %(sigstage_action)s,
       sigstage_bankfull = %(sigstage_bankfull)s, sigstage_flood = %(sigstage_flood)s,
       sigstage_moderate = %(sigstage_moderate)s, sigstage_major = %(sigstage_major)s,
       sigstage_record = %(sigstage_record)s, ugc_county = %(ugc_county)s,
       ugc_zone = %(ugc_zone)s
       WHERE iemid = %(iemid)s""",
       row)
    print 'Database: %s Modified %s rows TS: %s IEMID: %s' % (dbname, 
       cur.rowcount, maxTS, maxID)
    # close connection
    dbcursor.close()
    dbconn.commit()
    dbconn.close()

for sub in subscribers:
    sync(sub)
MESOSITE.close()

@@ -126,21 +126,8 @@
 ''%0A%0A
-import iemdb%0A
 impo
@@ -199,21 +199,24 @@
 OSITE = 
-iemdb
+psycopg2
 .connect
@@ -216,16 +216,25 @@
 connect(
+database=
 %22mesosit
@@ -241,19 +241,20 @@
 e%22, 
-bypass=True
+host='iemdb'
 )%0Asu
@@ -660,21 +660,24 @@
 bconn = 
-iemdb
+psycopg2
 .connect
@@ -681,16 +681,37 @@
 ect(
- dbname 
+database=dbname, host='iemdb'
 )%0A  
@@ -2367,30 +2367,22 @@
 t 'D
-atabase
+B
 : %25
+-7
 s Mod
-ified
  %25
+4
 s ro
@@ -2436,24 +2436,51 @@
 count, maxTS
+.strftime(%22%25Y/%25m/%25d %25H:%25M%22)
 , maxID)%0A   

remove iemdb usage and cleanup printout