~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/plugins/grass/scripts/db.connect-login.pg.py

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
############################################################################
 
4
#
 
5
# MODULE:       qgis.db.connect-login.py
 
6
# AUTHOR(S):    Radim Blazek
 
7
#
 
8
# PURPOSE:      Connect to Postgresql
 
9
# COPYRIGHT:    (C) 2009 by Radim Blazek
 
10
#
 
11
#               This program is free software under the GNU General Public
 
12
#               License (>=v2). Read the file COPYING that comes with GRASS
 
13
#               for details.
 
14
#
 
15
#############################################################################
 
16
 
 
17
#%Module
 
18
#% description: Make connection to PostgreSQL database and login.
 
19
#% keywords: database
 
20
#%End
 
21
 
 
22
#%option
 
23
#% key: host
 
24
#% type: string
 
25
#% label: Host
 
26
#% description: Host name of the machine on which the server is running. 
 
27
#% required : no
 
28
#%end
 
29
 
 
30
#%option
 
31
#% key: port
 
32
#% type: integer
 
33
#% label: Port
 
34
#% description: TCP port on which the server is listening, usually 5432.
 
35
#% required : no
 
36
#%end
 
37
 
 
38
#%option
 
39
#% key: database
 
40
#% type: string
 
41
#% key_desc : name
 
42
#% gisprompt: old_dbname,dbname,dbname
 
43
#% label: Database
 
44
#% description: Database name
 
45
#% required : yes
 
46
#%end
 
47
 
 
48
#%option
 
49
#% key: schema
 
50
#% type: string
 
51
#% label: Schema
 
52
#% description: Database schema.
 
53
#% required : no
 
54
#%end
 
55
 
 
56
#%option
 
57
#% key: user
 
58
#% type: string
 
59
#% label: User
 
60
#% description: Connect to the database as the user username instead of the  default. 
 
61
#% required : no
 
62
#%end
 
63
 
 
64
#%option
 
65
#% key: password
 
66
#% type: string
 
67
#% label: Password
 
68
#% description: Password will be stored in file!
 
69
#% required : no
 
70
#%end
 
71
 
 
72
import sys
 
73
import os
 
74
import string
 
75
try:
 
76
    from grass.script import core as grass
 
77
except ImportError:
 
78
    import grass
 
79
except:
 
80
    raise Exception ("Cannot find 'grass' Python module. Python is supported by GRASS from version >= 6.4" )
 
81
 
 
82
def main():
 
83
    host = options['host']
 
84
    port = options['port']
 
85
    database = options['database']
 
86
    schema = options['schema']
 
87
    user = options['user']
 
88
    password = options['password']
 
89
 
 
90
    # Test connection
 
91
    conn = "dbname=" + database
 
92
    if host: conn += ",host=" + host
 
93
    if port: conn += ",port=" + port
 
94
 
 
95
    # Unfortunately we cannot test untill user/password is set 
 
96
    if user or password:
 
97
        print "Setting login (db.login) ... "
 
98
        sys.stdout.flush()
 
99
        if grass.run_command('db.login', driver = "pg", database = conn, user = user, password = password) != 0:
 
100
            grass.fatal("Cannot login")
 
101
 
 
102
    # Try to connect
 
103
    print "Testing connection ..."
 
104
    sys.stdout.flush()
 
105
    if grass.run_command('db.select', quiet = True, flags='c', driver= "pg", database=conn, sql="select version()" ) != 0:
 
106
        if user or password:
 
107
            print "Deleting login (db.login) ..."
 
108
            sys.stdout.flush()
 
109
            if grass.run_command('db.login', quiet = True, driver = "pg", database = conn, user = "", password = "") != 0:
 
110
                print "Cannot delete login."
 
111
                sys.stdout.flush()
 
112
        grass.fatal("Cannot connect to database.")
 
113
  
 
114
    if grass.run_command('db.connect', driver = "pg", database = conn, schema = schema) != 0:
 
115
        grass.fatal("Cannot connect to database.")
 
116
        
 
117
if __name__ == "__main__":
 
118
    options, flags = grass.parser()
 
119
    main()