From a7d32718838596b8c24c1bdd0d64bc634189e81d Mon Sep 17 00:00:00 2001
From: Auke Klazema <klazema@astron.nl>
Date: Tue, 5 Mar 2019 12:35:26 +0000
Subject: [PATCH] SW-598: Add username, password, port options to
 load_hba_rotations.py

---
 .../data/Coordinates/load_hba_rotations.py    | 116 ++++++++++++------
 1 file changed, 79 insertions(+), 37 deletions(-)

diff --git a/MAC/Deployment/data/Coordinates/load_hba_rotations.py b/MAC/Deployment/data/Coordinates/load_hba_rotations.py
index 16d6d9aaa9e..08fe531a692 100755
--- a/MAC/Deployment/data/Coordinates/load_hba_rotations.py
+++ b/MAC/Deployment/data/Coordinates/load_hba_rotations.py
@@ -1,77 +1,119 @@
 #!/usr/bin/env python
-#coding: iso-8859-15
-import re,sys,pgdb,pg
-from math import *
-from database import *
+# coding: iso-8859-15
+import sys
+import pgdb
+import pg
+from math import pi
+from optparse import OptionParser
+import getpass
 
-# get info from database.py
-dbName=getDBname()
-dbHost=getDBhost()
-
-db1 = pgdb.connect(user="postgres", host=dbHost, database=dbName)
-cursor = db1.cursor()
-
-# calling stored procedures only works from the pg module for some reason.
-db2 = pg.connect(user="postgres", host=dbHost, dbname=dbName)
 
 #
 # getRotationLines
 #
-def getRotationLines(filename):
+def get_rotation_lines(filename):
     """
     Returns a list containing all lines with rotations
     """
-    f = open(filename,'r')
+    f = open(filename, 'r')
     lines = f.readlines()
     f.close()
-    return [ line.strip().split(',') for line in lines[1:]]
+    return [line.strip().split(',') for line in lines[1:]]
+
 
 ##
-def getRotation(line):
+def get_rotation(line):
     hba0 = hba1 = None
     station = str(line[0]).upper()
     if line[1] != '':
         hba0 = (int(line[1])/360.) * 2. * pi
     if line[2] != '':
         hba1 = (int(line[2])/360.) * 2. * pi
-    return(station,hba0,hba1)
+    return(station, hba0, hba1)
+
 
 #
 # MAIN
 #
 if __name__ == '__main__':
+    parser = OptionParser("Usage: %prog [options] datafile")
+
+    parser.add_option("-D", "--database",
+                      dest="dbName",
+                      type="string",
+                      default="StationCoordinates",
+                      help="Name of StationCoordinates database to use")
+
+    parser.add_option("-H", "--host",
+                      dest="dbHost",
+                      type="string",
+                      default="sasdb.control.lofar",
+                      help="Hostname of StationCoordinates database")
+
+    parser.add_option("-P", "--port",
+                      dest="dbPort",
+                      type="int",
+                      default="5432",
+                      help="Port of StationCoordinates database")
+
+    parser.add_option("-U", "--user",
+                      dest="dbUser",
+                      type="string",
+                      default="postgres",
+                      help="Username of StationCoordinates database")
+
+    # parse arguments
+
+    (options, args) = parser.parse_args()
 
-    # check syntax of invocation
-    # Expected syntax: load_measurement stationname objecttypes datafile
-    #
-    if (len(sys.argv) != 2):
-        print "Syntax: %s datafile" % sys.argv[0]
+    dbName = options.dbName
+    dbHost = options.dbHost
+    dbPort = options.dbPort
+    dbUser = options.dbUser
+
+    dbPassword = getpass.getpass()
+
+    host = "{}:{}".format(dbHost, dbPort)
+
+    db1 = pgdb.connect(user=dbUser, host=host, database=dbName, password=dbPassword)
+    cursor = db1.cursor()
+
+    # calling stored procedures only works from the pg module for some reason.
+    db2 = pg.connect(user=dbUser, host=dbHost, dbname=dbName, port=dbPort, passwd=dbPassword)
+
+    # print sys.argv
+    if len(args) != 1:
+        parser.print_help()
         sys.exit(1)
-    filename = str(sys.argv[1])
-    
-    lines = getRotationLines(filename)
+
+    filename = str(args[0])
+
+    lines = get_rotation_lines(filename)
     for line in lines:
-        (stationname,rotation0,rotation1) = getRotation(line)
-                
+        (stationname, rotation0, rotation1) = get_rotation(line)
+
         # check stationname
         cursor.execute("select name from station")
         stations = cursor.fetchall()
-        
+
         station = []
         station.append(stationname)
         if station not in stations:
             print "station %s is not a legal stationame" % stationname
             sys.exit(1)
         try:
-            if rotation1 == None:
-                db2.query("select * from add_field_rotation('%s','HBA',%s)" %( stationname, rotation0))
-                print 'station %s  rotation=%f' %(stationname,rotation0)
-            if rotation0 != None and rotation1 != None:
-                db2.query("select * from add_field_rotation('%s','HBA0',%s)" %( stationname, rotation0))
-                db2.query("select * from add_field_rotation('%s','HBA1',%s)" %( stationname, rotation1))
-                print 'station %s  rotation0=%f  rotation1=%f' %(stationname,rotation0, rotation1)
+            if rotation1 is None:
+                db2.query("select * from add_field_rotation('%s','HBA',%s)" %
+                          (stationname, rotation0))
+                print 'station %s  rotation=%f' % (stationname, rotation0)
+            if rotation0 is not None and rotation1 is not None:
+                db2.query("select * from add_field_rotation('%s','HBA0',%s)" %
+                          (stationname, rotation0))
+                db2.query("select * from add_field_rotation('%s','HBA1',%s)" %
+                          (stationname, rotation1))
+                print 'station %s  rotation0=%f  rotation1=%f' % (stationname, rotation0, rotation1)
         except:
-            print 'WARN, station %s has no HBA types defined yet' %(stationname)
+            print 'WARN, station %s has no HBA types defined yet' % (stationname)
     print ' Done'
     db1.close()
     db2.close()
-- 
GitLab