Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
LOFAR
Manage
Activity
Members
Labels
Plan
Issues
Wiki
Jira issues
Open Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RadioObservatory
LOFAR
Commits
bb1b3d1c
Commit
bb1b3d1c
authored
9 years ago
by
Jan David Mol
Browse files
Options
Downloads
Patches
Plain Diff
Task #8899: Added dbcredentials module to marshall database credentials from configuration files.
parent
683a4e77
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
LCS/PyCommon/CMakeLists.txt
+1
-0
1 addition, 0 deletions
LCS/PyCommon/CMakeLists.txt
LCS/PyCommon/dbcredentials.py
+144
-0
144 additions, 0 deletions
LCS/PyCommon/dbcredentials.py
with
145 additions
and
0 deletions
LCS/PyCommon/CMakeLists.txt
+
1
−
0
View file @
bb1b3d1c
...
...
@@ -8,6 +8,7 @@ include(PythonInstall)
#add_subdirectory(test)
set
(
_py_files
dbcredentials.py
factory.py
util.py
datetimeutils.py
)
...
...
This diff is collapsed.
Click to expand it.
LCS/PyCommon/dbcredentials.py
0 → 100644
+
144
−
0
View file @
bb1b3d1c
#!/usr/bin/python
# Copyright (C) 2012-2015 ASTRON (Netherlands Institute for Radio Astronomy)
# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands
#
# This file is part of the LOFAR software suite.
# The LOFAR software suite is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The LOFAR software suite is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
# $Id$
from
glob
import
glob
from
os
import
environ
from
ConfigParser
import
SafeConfigParser
,
NoSectionError
,
DuplicateSectionError
__all__
=
[
"
DBCredentials
"
]
def
findfiles
(
pattern
):
"""
Returns a list of files matched by `pattern
'
.
The pattern can include environment variables using the
{VAR} notation.
"""
try
:
return
glob
(
pattern
.
format
(
**
environ
))
except
KeyError
:
return
[]
class
DBCredentials
:
defaults
=
{
# Flavour of database (postgres, mysql, oracle, sqlite)
"
type
"
:
"
postgres
"
,
# Connection information
"
host
"
:
"
localhost
"
,
"
port
"
:
""
,
# Authentication
"
user
"
:
"
{USER}
"
.
format
(
**
environ
),
"
password
"
:
""
,
# Database selection
"
database
"
:
""
,
}
def
__init__
(
self
,
filepatterns
=
None
):
"""
Read database credentials from all configuration files matched by any of the patterns
"""
if
filepatterns
is
None
:
filepatterns
=
[
"
{LOFARROOT}/etc/dbcredentials/*.ini
"
,
"
{HOME}/.lofar/dbcredentials/*.ini
"
,
]
self
.
config
=
SafeConfigParser
(
defaults
=
self
.
defaults
)
self
.
files
=
sum
([
findfiles
(
p
)
for
p
in
filepatterns
],[])
self
.
config
.
read
(
self
.
files
)
def
get
(
self
,
database
):
"""
Return credentials for a given database.
"""
try
:
return
dict
(
self
.
config
.
items
(
self
.
_section
(
database
)))
except
NoSectionError
:
return
self
.
config
.
defaults
()
def
set
(
self
,
database
,
credentials
):
"""
Add or overwrite credentials for a given database.
"""
section
=
self
.
_section
(
database
)
# create section if needed
try
:
self
.
config
.
add_section
(
section
)
except
DuplicateSectionError
:
pass
# set or override credentials
for
k
,
v
in
credentials
.
iteritems
():
self
.
config
.
set
(
section
,
k
,
v
)
def
list
(
self
):
"""
Return a list of databases for which credentials are available.
"""
sections
=
self
.
config
.
sections
()
return
[
s
[
9
:]
for
s
in
sections
if
s
.
startsWith
(
"
database:
"
)]
def
_section
(
self
,
database
):
return
"
database:%s
"
%
(
database
,)
if
__name__
==
"
__main__
"
:
import
sys
from
optparse
import
OptionParser
parser
=
OptionParser
(
"
%prog [options]
"
)
parser
.
add_option
(
"
-D
"
,
"
--database
"
,
dest
=
"
database
"
,
type
=
"
string
"
,
default
=
""
,
help
=
"
Name of the database
"
)
parser
.
add_option
(
"
-L
"
,
"
--list
"
,
dest
=
"
list
"
,
action
=
"
store_true
"
,
default
=
False
,
help
=
"
List known databases
"
)
parser
.
add_option
(
"
-F
"
,
"
--files
"
,
dest
=
"
files
"
,
action
=
"
store_true
"
,
default
=
False
,
help
=
"
List names of parsed configuration files
"
)
(
options
,
args
)
=
parser
.
parse_args
()
if
not
options
.
database
and
not
options
.
list
and
not
options
.
files
:
print
"
Missing database name
"
parser
.
print_help
()
sys
.
exit
(
1
)
dbc
=
DBCredentials
()
if
options
.
files
:
"""
Print list of configuration files that we
'
ve read.
"""
if
dbc
.
files
:
print
"
\n
"
.
join
(
dbc
.
files
)
sys
.
exit
(
0
)
if
options
.
list
:
"""
Print list of databases.
"""
databases
=
dbc
.
list
()
if
databases
:
print
"
\n
"
.
join
(
databases
)
sys
.
exit
(
0
)
"""
Print credentials of a specific database.
"""
print
"
\n
"
.
join
([
"
%s %s
"
%
(
k
,
v
)
for
k
,
v
in
dbc
.
get
(
options
.
database
).
iteritems
()])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment