Skip to content

Commit

Permalink
add a geoslurp manager which eases local settins reading, db connecti…
Browse files Browse the repository at this point in the history
…on establishment, etc
  • Loading branch information
strawpants committed Dec 6, 2024
1 parent e0eb686 commit 11fc745
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/geoslurp/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
"""API Documentation for the geoslurp python module"""

2 changes: 1 addition & 1 deletion src/geoslurp/cli/geoslurper.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# License along with Frommle; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

# Author Roelof Rietbroek (roelof@geod.uni-bonn.de), 2018
# Author Roelof Rietbroek (roelof@geod.uni-bonn.de), 2018 -2024

#Command line program to Download and manage Earth science data

Expand Down
3 changes: 2 additions & 1 deletion src/geoslurp/config/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .slurplogger import *
from .slurplogger import *
from .catalogue import DatasetCatalogue
2 changes: 1 addition & 1 deletion src/geoslurp/db/geoslurpdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from geoslurp.db.connector import GeoslurpConnector


def geoslurpConnect(args=None,readonly_user=True,update=False,local_settings=None,dbalias=None):
def geoslurpConnect(args=None,readonly_user=True,local_settings=None,dbalias=None):
"""Convenience wrapper to start a connection with a geoslurpdatabase. Returns a database connection while taking use of stored settings from the users
configuration file ($HOME/.geoslurp_lastused.yaml).
:param args: Class which encapsulates different connection parameters
Expand Down
78 changes: 78 additions & 0 deletions src/geoslurp/manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# This file is part of geoslurp.
# geoslurp is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.

# geoslurp 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
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with Frommle; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

# Author Roelof Rietbroek (r.rietbroek@utwente.nl), 2024


from geoslurp.config.localsettings import readLocalSettings,settingsArgs
from geoslurp.db.connector import GeoslurpConnector
from geoslurp.db import Settings
from geoslurp.config.catalogue import DatasetCatalogue

class GeoslurpManager:
def __init__(self,args=None,readonly_user=True,conf_file=None,dbalias=None):
#if no overruling arguments are supplied
if not args:
#Start with the default arguments
args=settingsArgs()

if conf_file is not None:
#read the local client settings from non-default yaml config file
args.local_settings=conf_file

if dbalias:
#specify a different database alias profile to load from the in the client settings
args.dbalias=dbalias


#Read local settings
self._localconf=readLocalSettings(args=args,readonlyuser=readonly_user)
self._catalogue=DatasetCatalogue()
#note no actual database connection is needed up to now
self._conn=None
self._conf=None #settings loaded from the database

@property
def conn(self)-> GeoslurpConnector :
"""
Lazy loading of the geoslurp connector
Returns
-------
GeoslurpConnector
"""
if self._conn is None:
#initialize connection based on the client profile
self._conn=GeoslurpConnector(host=self._localconf.host,user=self._localconf.user,passwd=self._localconf.password, cache=self._localconf.cache,dataroot=self._localconf.dataroot)


return self._conn

@property
def conf(self):
"""
Lazily loads the user/db configuration from the database
"""
if self._conf is None:
self._conf=Settings(self.conn)
return self._conf

def dataset(self,name:str):

#initialize a dataset class (Note this requires a database connection)
ds=self._catalogue.getDsetClass(self.conf,name)
#return an initialized dataset class
return ds(self.conn)


0 comments on commit 11fc745

Please sign in to comment.