[GRASS-SVN] r61762 - grass/trunk/lib/python/temporal
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Aug 27 12:48:57 PDT 2014
Author: huhabla
Date: 2014-08-27 12:48:57 -0700 (Wed, 27 Aug 2014)
New Revision: 61762
Modified:
grass/trunk/lib/python/temporal/c_libraries_interface.py
grass/trunk/lib/python/temporal/core.py
Log:
python temporal: First steps to allow distributed temporal databases. The C-library interface supports now the request of temporal database driver and name/connection string. Added several TODOs to the SQLDatabaseInterfaceConnection class that must implement mapset specific database connections and cursors.
Modified: grass/trunk/lib/python/temporal/c_libraries_interface.py
===================================================================
--- grass/trunk/lib/python/temporal/c_libraries_interface.py 2014-08-27 19:45:54 UTC (rev 61761)
+++ grass/trunk/lib/python/temporal/c_libraries_interface.py 2014-08-27 19:48:57 UTC (rev 61762)
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
-"""!@package grass.pygrass.massages
+"""@package grass.pygrass.massages
@brief Temporal Framework GRASS C-library interface
@@ -20,11 +20,13 @@
import logging
from ctypes import *
from core import *
+import core as corefunc
import grass.lib.gis as libgis
import grass.lib.raster as libraster
import grass.lib.vector as libvector
import grass.lib.date as libdate
import grass.lib.raster3d as libraster3d
+import grass.lib.temporal as libtgis
###############################################################################
@@ -39,22 +41,60 @@
MAP_EXISTS=6
READ_MAP_INFO=7
AVAILABLE_MAPSETS = 8
+ GET_DRIVER_NAME = 9
+ GET_DATABASE_NAME = 10
TYPE_RASTER=0
TYPE_RASTER3D=1
TYPE_VECTOR=2
###############################################################################
-def available_mapsets(lock, conn, data):
- """!Return all available mapsets the user can access as a list of strings
+
+def _get_driver_name(lock, conn, data):
+ """Return the temporal database driver of a specific mapset
- @param lock A multiprocessing.Lock instance
- @param conn A multiprocessing.Pipe instance used to send True or False
- @param data The list of data entries [function_id]
+ :param lock: A multiprocessing.Lock instance
+ :param conn: A multiprocessing.Pipe instance used to send True or False
+ :param data: The mapset as list entry 1 [function_id, mapset]
- @return Names of available mapsets as list of strings
+ :returns: Name of the driver or None if no temporal database present
"""
+ drstring = libtgis.tgis_get_mapset_driver_name(data[1])
+ conn.send(drstring)
+
+###############################################################################
+
+def _get_database_name(lock, conn, data):
+ """Return the temporal database name of a specific mapset
+
+ :param lock: A multiprocessing.Lock instance
+ :param conn: A multiprocessing.Pipe instance used to send True or False
+ :param data: The mapset as list entry 1 [function_id, mapset]
+
+ :returns: Name of the database or None if no temporal database present
+ """
+ dbstring = libtgis.tgis_get_mapset_database_name(data[1])
+ if dbstring:
+ # We substitute GRASS variables if they are located in the database string
+ # This behavior is in conjunction with db.connect
+ dbstring = dbstring.replace("$GISDBASE", corefunc.current_gisdbase)
+ dbstring = dbstring.replace("$LOCATION_NAME", corefunc.current_location)
+ dbstring = dbstring.replace("$MAPSET", corefunc.current_mapset)
+ conn.send(dbstring)
+
+###############################################################################
+
+def _available_mapsets(lock, conn, data):
+ """Return all available mapsets the user can access as a list of strings
+
+ :param lock: A multiprocessing.Lock instance
+ :param conn: A multiprocessing.Pipe instance used to send True or False
+ :param data: The list of data entries [function_id]
+
+ :returns: Names of available mapsets as list of strings
+ """
+
mapsets = libgis.G_get_available_mapsets()
count = 0
@@ -83,12 +123,12 @@
conn.send(mapset_list)
def _has_timestamp(lock, conn, data):
- """!Check if the file based GRASS timestamp is present and send
+ """Check if the file based GRASS timestamp is present and send
True or False using the provided pipe.
- @param lock A multiprocessing.Lock instance
- @param conn A multiprocessing.Pipe instance used to send True or False
- @param data The list of data entries [function_id, maptype, name, mapset, layer]
+ :param lock: A multiprocessing.Lock instance
+ :param conn: A multiprocessing.Pipe instance used to send True or False
+ :param data: The list of data entries [function_id, maptype, name, mapset, layer]
"""
maptype = data[1]
@@ -110,7 +150,7 @@
###############################################################################
def _read_timestamp(lock, conn, data):
- """!Read the file based GRASS timestamp and send
+ """Read the file based GRASS timestamp and send
the result using the provided pipe.
The tuple to be send via pipe: (return value of G_read_*_timestamp, timestamps).
@@ -126,9 +166,9 @@
The end time may be None in case of a time instance.
- @param lock A multiprocessing.Lock instance
- @param conn A multiprocessing.Pipe instance used to send the result
- @param data The list of data entries [function_id, maptype, name, mapset, layer]
+ :param lock: A multiprocessing.Lock instance
+ :param conn: A multiprocessing.Pipe instance used to send the result
+ :param data: The list of data entries [function_id, maptype, name, mapset, layer]
"""
maptype = data[1]
@@ -150,7 +190,7 @@
###############################################################################
def _write_timestamp(lock, conn, data):
- """!Write the file based GRASS timestamp
+ """Write the file based GRASS timestamp
the return values of the called C-functions using the provided pipe.
The value to be send via pipe is the return value of G_write_*_timestamp.
@@ -159,9 +199,9 @@
G_write_vector_timestamp and G_write_raster3d_timestamp for the return
values description.
- @param lock A multiprocessing.Lock instance
- @param conn A multiprocessing.Pipe instance used to send True or False
- @param data The list of data entries [function_id, maptype, name, mapset, layer, timestring]
+ :param lock: A multiprocessing.Lock instance
+ :param conn: A multiprocessing.Pipe instance used to send True or False
+ :param data: The list of data entries [function_id, maptype, name, mapset, layer, timestring]
"""
maptype = data[1]
name = data[2]
@@ -188,7 +228,7 @@
###############################################################################
def _remove_timestamp(lock, conn, data):
- """!Remove the file based GRASS timestamp
+ """Remove the file based GRASS timestamp
the return values of the called C-functions using the provided pipe.
The value to be send via pipe is the return value of G_remove_*_timestamp.
@@ -197,9 +237,9 @@
G_remove_vector_timestamp and G_remove_raster3d_timestamp for the return
values description.
- @param lock A multiprocessing.Lock instance
- @param conn A multiprocessing.Pipe instance used to send True or False
- @param data The list of data entries [function_id, maptype, name, mapset, layer]
+ :param lock: A multiprocessing.Lock instance
+ :param conn: A multiprocessing.Pipe instance used to send True or False
+ :param data: The list of data entries [function_id, maptype, name, mapset, layer]
"""
maptype = data[1]
@@ -219,14 +259,14 @@
###############################################################################
def _map_exists(lock, conn, data):
- """!Check if a map exists in the spatial database
+ """Check if a map exists in the spatial database
The value to be send via pipe is True in case the map exists and False
if not.
- @param lock A multiprocessing.Lock instance
- @param conn A multiprocessing.Pipe instance used to send True or False
- @param data The list of data entries [function_id, maptype, name, mapset]
+ :param lock: A multiprocessing.Lock instance
+ :param conn: A multiprocessing.Pipe instance used to send True or False
+ :param data: The list of data entries [function_id, maptype, name, mapset]
"""
maptype = data[1]
@@ -248,12 +288,12 @@
###############################################################################
def _read_map_info(lock, conn, data):
- """!Read map specific metadata from the spatial database using C-library
+ """Read map specific metadata from the spatial database using C-library
functions
- @param lock A multiprocessing.Lock instance
- @param conn A multiprocessing.Pipe instance used to send True or False
- @param data The list of data entries [function_id, maptype, name, mapset]
+ :param lock: A multiprocessing.Lock instance
+ :param conn: A multiprocessing.Pipe instance used to send True or False
+ :param data: The list of data entries [function_id, maptype, name, mapset]
"""
maptype = data[1]
name = data[2]
@@ -270,15 +310,15 @@
###############################################################################
def _read_raster_info(name, mapset):
- """!Read the raster map info from the file system and store the content
+ """Read the raster map info from the file system and store the content
into a dictionary
This method uses the ctypes interface to the gis and raster libraries
to read the map metadata information
- @param name The name of the map
- @param mapset The mapset of the map
- @return The key value pairs of the map specific metadata, or None in case of an error
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: The key value pairs of the map specific metadata, or None in case of an error
"""
kvp = {}
@@ -349,15 +389,15 @@
###############################################################################
def _read_raster3d_info(name, mapset):
- """!Read the 3D raster map info from the file system and store the content
+ """Read the 3D raster map info from the file system and store the content
into a dictionary
This method uses the ctypes interface to the gis and raster3d libraries
to read the map metadata information
- @param name The name of the map
- @param mapset The mapset of the map
- @return The key value pairs of the map specific metadata, or None in case of an error
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: The key value pairs of the map specific metadata, or None in case of an error
"""
kvp = {}
@@ -430,15 +470,15 @@
###############################################################################
def _read_vector_info(name, mapset):
- """!Read the vector map info from the file system and store the content
+ """Read the vector map info from the file system and store the content
into a dictionary
This method uses the ctypes interface to the vector libraries
to read the map metadata information
- @param name The name of the map
- @param mapset The mapset of the map
- @return The key value pairs of the map specific metadata, or None in case of an error
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: The key value pairs of the map specific metadata, or None in case of an error
"""
kvp = {}
@@ -530,7 +570,7 @@
###############################################################################
def _convert_timestamp_from_grass(ts):
- """!Convert a GRASS file based timestamp into the temporal framework
+ """Convert a GRASS file based timestamp into the temporal framework
format datetime or integer.
A tuple of two datetime objects (start, end) is returned in case of absolute time.
@@ -540,7 +580,7 @@
Note:
The end time will be set to None in case of a time instance.
- @param ts grass.lib.gis.TimeStamp object created by G_read_*_timestamp
+ :param ts grass.lib.gis.TimeStamp object created by G_read_*_timestamp
"""
dt1 = libgis.DateTime()
@@ -616,14 +656,14 @@
###############################################################################
def c_library_server(lock, conn):
- """!The GRASS C-libraries server function designed to be a target for
+ """The GRASS C-libraries server function designed to be a target for
multiprocessing.Process
- @param lock A multiprocessing.Lock
- @param conn A multiprocessing.Pipe
+ :param lock: A multiprocessing.Lock
+ :param conn: A multiprocessing.Pipe
"""
# Crerate the function array
- functions = [0]*9
+ functions = [0]*11
functions[RPCDefs.STOP] = _stop
functions[RPCDefs.HAS_TIMESTAMP] = _has_timestamp
functions[RPCDefs.WRITE_TIMESTAMP] = _write_timestamp
@@ -631,7 +671,9 @@
functions[RPCDefs.REMOVE_TIMESTAMP] = _remove_timestamp
functions[RPCDefs.READ_MAP_INFO] = _read_map_info
functions[RPCDefs.MAP_EXISTS] = _map_exists
- functions[RPCDefs.AVAILABLE_MAPSETS] = available_mapsets
+ functions[RPCDefs.AVAILABLE_MAPSETS] = _available_mapsets
+ functions[RPCDefs.GET_DRIVER_NAME] = _get_driver_name
+ functions[RPCDefs.GET_DATABASE_NAME] = _get_database_name
libgis.G_gisinit("c_library_server")
libgis.G_debug(1, "Start C-interface server")
@@ -645,7 +687,7 @@
lock.release()
class CLibrariesInterface(object):
- """!Fast and exit-safe interface to GRASS C-libraries functions
+ """Fast and exit-safe interface to GRASS C-libraries functions
This class implements a fast and exit-safe interface to the GRASS
gis, raster, 3D raster and vector C-libraries functions.
@@ -759,6 +801,11 @@
1
>>> ciface.has_vector_timestamp("test", tgis.get_current_mapset())
True
+
+ >>> ciface.get_driver_name()
+ 'sqlite'
+ >>> ciface.get_database_name().split("/")[-1]
+ 'sqlite.db'
>>> gscript.del_temp_region()
@@ -770,9 +817,6 @@
self.server = None
self.start_server()
- def __del__(self):
- self.stop()
-
def start_server(self):
self.client_conn, self.server_conn = Pipe()
self.lock = Lock()
@@ -782,7 +826,7 @@
self.server.start()
def _check_restart_server(self):
- """!Restart the server if it was terminated
+ """Restart the server if it was terminated
"""
if self.server.is_alive() is True:
return
@@ -792,11 +836,11 @@
logging.warning("Needed to restart the libgis server")
def raster_map_exists(self, name, mapset):
- """!Check if a raster map exists in the spatial database
+ """Check if a raster map exists in the spatial database
- @param name The name of the map
- @param mapset The mapset of the map
- @return True if exists, False if not
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: True if exists, False if not
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.MAP_EXISTS, RPCDefs.TYPE_RASTER,
@@ -804,12 +848,12 @@
return self.client_conn.recv()
def read_raster_info(self, name, mapset):
- """!Read the raster map info from the file system and store the content
+ """Read the raster map info from the file system and store the content
into a dictionary
- @param name The name of the map
- @param mapset The mapset of the map
- @return The key value pairs of the map specific metadata, or None in case of an error
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: The key value pairs of the map specific metadata, or None in case of an error
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.READ_MAP_INFO, RPCDefs.TYPE_RASTER,
@@ -817,11 +861,11 @@
return self.client_conn.recv()
def has_raster_timestamp(self, name, mapset):
- """!Check if a file based raster timetamp exists
+ """Check if a file based raster timetamp exists
- @param name The name of the map
- @param mapset The mapset of the map
- @return True if exists, False if not
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: True if exists, False if not
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.HAS_TIMESTAMP, RPCDefs.TYPE_RASTER,
@@ -829,14 +873,14 @@
return self.client_conn.recv()
def remove_raster_timestamp(self, name, mapset):
- """!Remove a file based raster timetamp
+ """Remove a file based raster timetamp
Please have a look at the documentation G_remove_raster_timestamp
for the return values description.
- @param name The name of the map
- @param mapset The mapset of the map
- @return The return value of G_remove_raster_timestamp
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: The return value of G_remove_raster_timestamp
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.REMOVE_TIMESTAMP, RPCDefs.TYPE_RASTER,
@@ -844,7 +888,7 @@
return self.client_conn.recv()
def read_raster_timestamp(self, name, mapset):
- """!Read a file based raster timetamp
+ """Read a file based raster timetamp
Please have a look at the documentation G_read_raster_timestamp
for the return values description.
@@ -856,9 +900,9 @@
The end time may be None in case of a time instance.
- @param name The name of the map
- @param mapset The mapset of the map
- @return The return value of G_read_raster_timestamp
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: The return value of G_read_raster_timestamp
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.READ_TIMESTAMP, RPCDefs.TYPE_RASTER,
@@ -866,7 +910,7 @@
return self.client_conn.recv()
def write_raster_timestamp(self, name, mapset, timestring):
- """!Write a file based raster timetamp
+ """Write a file based raster timetamp
Please have a look at the documentation G_write_raster_timestamp
for the return values description.
@@ -874,10 +918,10 @@
Note:
Only timestamps of maps from the current mapset can written.
- @param name The name of the map
- @param mapset The mapset of the map
- @param timestring A GRASS datetime C-library compatible string
- @return The return value of G_write_raster_timestamp
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :param timestring: A GRASS datetime C-library compatible string
+ :returns: The return value of G_write_raster_timestamp
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.WRITE_TIMESTAMP, RPCDefs.TYPE_RASTER,
@@ -885,11 +929,11 @@
return self.client_conn.recv()
def raster3d_map_exists(self, name, mapset):
- """!Check if a 3D raster map exists in the spatial database
+ """Check if a 3D raster map exists in the spatial database
- @param name The name of the map
- @param mapset The mapset of the map
- @return True if exists, False if not
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: True if exists, False if not
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.MAP_EXISTS, RPCDefs.TYPE_RASTER3D,
@@ -897,12 +941,12 @@
return self.client_conn.recv()
def read_raster3d_info(self, name, mapset):
- """!Read the 3D raster map info from the file system and store the content
+ """Read the 3D raster map info from the file system and store the content
into a dictionary
- @param name The name of the map
- @param mapset The mapset of the map
- @return The key value pairs of the map specific metadata, or None in case of an error
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: The key value pairs of the map specific metadata, or None in case of an error
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.READ_MAP_INFO, RPCDefs.TYPE_RASTER3D,
@@ -910,11 +954,11 @@
return self.client_conn.recv()
def has_raster3d_timestamp(self, name, mapset):
- """!Check if a file based 3D raster timetamp exists
+ """Check if a file based 3D raster timetamp exists
- @param name The name of the map
- @param mapset The mapset of the map
- @return True if exists, False if not
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: True if exists, False if not
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.HAS_TIMESTAMP, RPCDefs.TYPE_RASTER3D,
@@ -922,14 +966,14 @@
return self.client_conn.recv()
def remove_raster3d_timestamp(self, name, mapset):
- """!Remove a file based 3D raster timetamp
+ """Remove a file based 3D raster timetamp
Please have a look at the documentation G_remove_raster3d_timestamp
for the return values description.
- @param name The name of the map
- @param mapset The mapset of the map
- @return The return value of G_remove_raster3d_timestamp
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: The return value of G_remove_raster3d_timestamp
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.REMOVE_TIMESTAMP, RPCDefs.TYPE_RASTER3D,
@@ -937,7 +981,7 @@
return self.client_conn.recv()
def read_raster3d_timestamp(self, name, mapset):
- """!Read a file based 3D raster timetamp
+ """Read a file based 3D raster timetamp
Please have a look at the documentation G_read_raster3d_timestamp
for the return values description.
@@ -949,9 +993,9 @@
The end time may be None in case of a time instance.
- @param name The name of the map
- @param mapset The mapset of the map
- @return The return value of G_read_raster3d_timestamp
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: The return value of G_read_raster3d_timestamp
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.READ_TIMESTAMP, RPCDefs.TYPE_RASTER3D,
@@ -959,7 +1003,7 @@
return self.client_conn.recv()
def write_raster3d_timestamp(self, name, mapset, timestring):
- """!Write a file based 3D raster timetamp
+ """Write a file based 3D raster timetamp
Please have a look at the documentation G_write_raster3d_timestamp
for the return values description.
@@ -967,10 +1011,10 @@
Note:
Only timestamps of maps from the current mapset can written.
- @param name The name of the map
- @param mapset The mapset of the map
- @param timestring A GRASS datetime C-library compatible string
- @return The return value of G_write_raster3d_timestamp
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :param timestring: A GRASS datetime C-library compatible string
+ :returns: The return value of G_write_raster3d_timestamp
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.WRITE_TIMESTAMP, RPCDefs.TYPE_RASTER3D,
@@ -978,11 +1022,11 @@
return self.client_conn.recv()
def vector_map_exists(self, name, mapset):
- """!Check if a vector map exists in the spatial database
+ """Check if a vector map exists in the spatial database
- @param name The name of the map
- @param mapset The mapset of the map
- @return True if exists, False if not
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: True if exists, False if not
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.MAP_EXISTS, RPCDefs.TYPE_VECTOR,
@@ -990,12 +1034,12 @@
return self.client_conn.recv()
def read_vector_info(self, name, mapset):
- """!Read the vector map info from the file system and store the content
+ """Read the vector map info from the file system and store the content
into a dictionary
- @param name The name of the map
- @param mapset The mapset of the map
- @return The key value pairs of the map specific metadata, or None in case of an error
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :returns: The key value pairs of the map specific metadata, or None in case of an error
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.READ_MAP_INFO, RPCDefs.TYPE_VECTOR,
@@ -1003,12 +1047,12 @@
return self.client_conn.recv()
def has_vector_timestamp(self, name, mapset, layer=None):
- """!Check if a file based vector timetamp exists
+ """Check if a file based vector timetamp exists
- @param name The name of the map
- @param mapset The mapset of the map
- @param layer The layer of the vector map
- @return True if exists, False if not
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :param layer: The layer of the vector map
+ :returns: True if exists, False if not
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.HAS_TIMESTAMP, RPCDefs.TYPE_VECTOR,
@@ -1016,15 +1060,15 @@
return self.client_conn.recv()
def remove_vector_timestamp(self, name, mapset, layer=None):
- """!Remove a file based vector timetamp
+ """Remove a file based vector timetamp
Please have a look at the documentation G_remove_vector_timestamp
for the return values description.
- @param name The name of the map
- @param mapset The mapset of the map
- @param layer The layer of the vector map
- @return The return value of G_remove_vector_timestamp
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :param layer The layer of the vector map
+ :returns: The return value of G_remove_vector_timestamp
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.REMOVE_TIMESTAMP, RPCDefs.TYPE_VECTOR,
@@ -1032,7 +1076,7 @@
return self.client_conn.recv()
def read_vector_timestamp(self, name, mapset, layer=None):
- """!Read a file based vector timetamp
+ """Read a file based vector timetamp
Please have a look at the documentation G_read_vector_timestamp
for the return values description.
@@ -1044,10 +1088,10 @@
The end time may be None in case of a time instance.
- @param name The name of the map
- @param mapset The mapset of the map
- @param layer The layer of the vector map
- @return The return value ofG_read_vector_timestamp and the timestamps
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :param layer: The layer of the vector map
+ :returns: The return value ofG_read_vector_timestamp and the timestamps
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.READ_TIMESTAMP, RPCDefs.TYPE_VECTOR,
@@ -1055,7 +1099,7 @@
return self.client_conn.recv()
def write_vector_timestamp(self, name, mapset, timestring, layer=None):
- """!Write a file based vector timetamp
+ """Write a file based vector timestamp
Please have a look at the documentation G_write_vector_timestamp
for the return values description.
@@ -1063,11 +1107,11 @@
Note:
Only timestamps pf maps from the current mapset can written.
- @param name The name of the map
- @param mapset The mapset of the map
- @param timestring A GRASS datetime C-library compatible string
- @param layer The layer of the vector map
- @return The return value of G_write_vector_timestamp
+ :param name: The name of the map
+ :param mapset: The mapset of the map
+ :param timestring: A GRASS datetime C-library compatible string
+ :param layer: The layer of the vector map
+ :returns: The return value of G_write_vector_timestamp
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.WRITE_TIMESTAMP, RPCDefs.TYPE_VECTOR,
@@ -1075,20 +1119,47 @@
return self.client_conn.recv()
def available_mapsets(self):
- """!Return all available mapsets the user can access as a list of strings
-
- @param lock A multiprocessing.Lock instance
- @param conn A multiprocessing.Pipe instance used to send True or False
- @param data Can be None
+ """Return all available mapsets the user can access as a list of strings
- @return Names of available mapsets as list of strings
+ :returns: Names of available mapsets as list of strings
"""
self._check_restart_server()
self.client_conn.send([RPCDefs.AVAILABLE_MAPSETS, ])
return self.client_conn.recv()
+
+ def get_driver_name(self, mapset=None):
+ """Return the temporal database driver of a specific mapset
+
+ :param mapset: Name of the mapset
+
+ :returns: Name of the driver or None if no temporal database present
+ """
+ if mapset is None or mapset is "":
+ mapset = corefunc.get_current_mapset()
+ self._check_restart_server()
+ self.client_conn.send([RPCDefs.GET_DRIVER_NAME, mapset])
+ return self.client_conn.recv()
+
+ def get_database_name(self, mapset=None):
+ """Return the temporal database name of a specific mapset
+
+ :param mapset: Name of the mapset
+
+ :returns: Name of the database or None if no temporal database present
+ """
+
+ if mapset is None or mapset is "":
+ mapset = corefunc.get_current_mapset()
+
+ self._check_restart_server()
+ self.client_conn.send([RPCDefs.GET_DATABASE_NAME, mapset])
+ return self.client_conn.recv()
+
def stop(self):
- """!Stop the messenger server and close the pipe
+ """Stop the messenger server and close the pipe
+
+ This method should be called at exit using the package atexit
"""
if self.server is not None and self.server.is_alive():
self.client_conn.send([0,])
Modified: grass/trunk/lib/python/temporal/core.py
===================================================================
--- grass/trunk/lib/python/temporal/core.py 2014-08-27 19:45:54 UTC (rev 61761)
+++ grass/trunk/lib/python/temporal/core.py 2014-08-27 19:48:57 UTC (rev 61762)
@@ -1,6 +1,6 @@
-"""!@package grass.temporal
+"""package grass.temporal
- at brief GRASS Python scripting module (temporal GIS functions)
+GRASS Python scripting module (temporal GIS functions)
Temporal GIS core functions to be used in library modules and scripts.
@@ -9,7 +9,6 @@
Usage:
- at code
>>> import grass.temporal as tgis
>>> # Create the temporal database
@@ -25,14 +24,13 @@
"SELECT name from raster_base where name = 'precipitation'"
>>> dbif.close()
- at endcode
(C) 2011-2014 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
- at author Soeren Gebbert
+:author: Soeren Gebbert
"""
import sys, traceback
import os
@@ -41,7 +39,7 @@
import gettext
gettext.install('grasslibs', os.path.join(os.getenv("GISBASE"), 'locale'))
-import grass.script.core as core
+import grass.script.core as gcore
from datetime import datetime
from c_libraries_interface import *
# Import all supported database backends
@@ -84,9 +82,9 @@
# It can either be "sqlite" or "pg"
tgis_backend = None
def get_tgis_backend():
- """!Return the temporal GIS backend as string
+ """Return the temporal GIS backend as string
- return either "sqlite" or "pg"
+ :returns: either "sqlite" or "pg"
"""
global tgis_backend
return tgis_backend
@@ -95,7 +93,7 @@
# of the temporal GIS
tgis_database = None
def get_tgis_database():
- """!Return the temporal database string specified with t.connect
+ """Return the temporal database string specified with t.connect
"""
global tgis_database
return tgis_database
@@ -114,9 +112,9 @@
tgis_dbmi_paramstyle = None
def get_tgis_dbmi_paramstyle():
- """!Return the temporal database backend parameter style
+ """Return the temporal database backend parameter style
- @return "qmark" or ""
+ :returns: "qmark" or ""
"""
global tgis_dbmi_paramstyle
return tgis_dbmi_paramstyle
@@ -130,7 +128,7 @@
###############################################################################
def get_current_mapset():
- """!Return the current mapset
+ """Return the current mapset
This is the fastest way to receive the current mapset.
The current mapset is set by init() and stored in a global variable.
@@ -142,7 +140,7 @@
###############################################################################
def get_current_location():
- """!Return the current location
+ """Return the current location
This is the fastest way to receive the current location.
The current location is set by init() and stored in a global variable.
@@ -154,7 +152,7 @@
###############################################################################
def get_current_gisdbase():
- """!Return the current gis database (gisdbase)
+ """Return the current gis database (gisdbase)
This is the fastest way to receive the current gisdbase.
The current gisdbase is set by init() and stored in a global variable.
@@ -181,7 +179,7 @@
enable_timestamp_write = True
def get_enable_mapset_check():
- """!Return True if the mapsets should be checked while insert, updatem delete requests
+ """Return True if the mapsets should be checked while insert, update, delete requests
and space time dataset registration.
If this global variable is set True, then maps can only be registered in space time datasets
@@ -196,7 +194,7 @@
return enable_mapset_check
def get_enable_timestamp_write():
- """!Return True if the map timestamps should be written to the spatial database metadata as well.
+ """Return True if the map timestamps should be written to the spatial database metadata as well.
If this global variable is set True, the timestamps of maps will be written as textfiles
for each map that will be inserted or updated in the temporal database using the C-library
@@ -218,9 +216,9 @@
message_interface=None
def _init_tgis_message_interface(raise_on_error=False):
- """!Initiate the global mesage interface
+ """Initiate the global mesage interface
- @param raise_on_error If True raise a FatalError exception in case of a fatal error,
+ :param raise_on_error: If True raise a FatalError exception in case of a fatal error,
call sys.exit(1) otherwise
"""
global message_interface
@@ -228,7 +226,7 @@
message_interface = messages.get_msgr(raise_on_error=raise_on_error)
def get_tgis_message_interface():
- """!Return the temporal GIS message interface which is of type
+ """Return the temporal GIS message interface which is of type
grass.pyhrass.message.Messenger()
Use this message interface to print messages to stdout using the
@@ -245,7 +243,7 @@
c_library_interface=None
def _init_tgis_c_library_interface():
- """!Set the global C-library interface variable that
+ """Set the global C-library interface variable that
provides a fast and exit safe interface to the C-library libgis,
libraster, libraster3d and libvector functions
"""
@@ -253,7 +251,7 @@
c_library_interface = CLibrariesInterface()
def get_tgis_c_library_interface():
- """!Return the C-library interface that
+ """Return the C-library interface that
provides a fast and exit safe interface to the C-library libgis,
libraster, libraster3d and libvector functions
"""
@@ -267,15 +265,14 @@
raise_on_error = False
def set_raise_on_error(raise_exp=True):
- """!Define behavior on fatal error, invoked using the tgis messenger
+ """Define behavior on fatal error, invoked using the tgis messenger
interface (msgr.fatal())
The messenger interface will be restarted using the new error policy
- @param raise_exp True to raise a FatalError exception instead of calling
- sys.exit(1) when using the tgis messenger interface
+ :param raise_exp: True to raise a FatalError exception instead of calling
+ sys.exit(1) when using the tgis messenger interface
- @code
>>> import grass.temporal as tgis
>>> tgis.init()
@@ -297,9 +294,8 @@
raise FatalError(message)
FatalError: Ohh no no no!
- @endcode
- @return current status
+ :returns: current status
"""
global raise_on_error
tmp_raise = raise_on_error
@@ -315,7 +311,7 @@
def get_raise_on_error():
- """!Return True if a FatalError exception is raised instead of calling
+ """Return True if a FatalError exception is raised instead of calling
sys.exit(1) in case a fatal error was invoked with msgr.fatal()
"""
global raise_on_error
@@ -325,8 +321,8 @@
###############################################################################
def get_tgis_version():
- """!Get the verion number of the temporal framework
- @return The version number of the temporal framework as string
+ """Get the version number of the temporal framework
+ :returns: The version number of the temporal framework as string
"""
global tgis_version
return tgis_version
@@ -334,8 +330,8 @@
###############################################################################
def get_tgis_db_version():
- """!Get the verion number of the temporal framework
- @return The version number of the temporal framework as string
+ """Get the version number of the temporal framework
+ :returns: The version number of the temporal framework as string
"""
global tgis_db_version
return tgis_db_version
@@ -343,11 +339,11 @@
###############################################################################
def get_tgis_metadata(dbif=None):
- """!Return the tgis metadata table as a list of rows (dicts)
+ """Return the tgis metadata table as a list of rows (dicts)
or None if not present
- @param dbif The database interface to be used
- @return The selected rows with key/value comumns or None
+ :param dbif: The database interface to be used
+ :returns: The selected rows with key/value columns or None
"""
dbif, connected = init_dbif(dbif)
@@ -372,7 +368,7 @@
tgis_database_string = None
def get_tgis_database_string():
- """!Return the preprocessed temporal database string
+ """Return the preprocessed temporal database string
This string is the temporal database string set with t.connect
that was processed to substitue location, gisdbase and mapset
@@ -391,7 +387,7 @@
###############################################################################
def stop_subprocesses():
- """!Stop the messenger and C-interface subprocesses
+ """Stop the messenger and C-interface subprocesses
that are started by tgis.init()
"""
global message_interface
@@ -407,7 +403,7 @@
###############################################################################
def init(raise_fatal_error=False):
- """!This function set the correct database backend from GRASS environmental variables
+ """This function set the correct database backend from GRASS environmental variables
and creates the grass temporal database structure for raster,
vector and raster3d maps as well as for the space-time datasets strds,
str3ds and stvds in case it does not exists.
@@ -422,7 +418,7 @@
- TGIS_DISABLE_MAPSET_CHECK
- TGIS_DISABLE_TIMESTAMP_WRITE
- Re-run the script if the following t.connect variables change while the process runs:
+ Re-run this function if the following t.connect variables change while the process runs:
- temporal GIS driver (set by t.connect driver=)
- temporal GIS database (set by t.connect database=)
@@ -433,7 +429,7 @@
ATTENTION: This functions must be called before any spatio-temporal processing
can be started
- @param raise_fatal_error Set this True to assure that the init() function
+ :param raise_fatal_error: Set this True to assure that the init() function
does not kill a persistent process like the GUI.
If set True a grass.pygrass.messages.FatalError
@@ -457,9 +453,8 @@
# We must run t.connect at first to create the temporal database and to
# get the environmental variables
- core.run_command("t.connect", flags="c")
- kv = core.parse_command("t.connect", flags="pg")
- grassenv = core.gisenv()
+ gcore.run_command("t.connect", flags="c")
+ grassenv = gcore.gisenv()
# Set the global variable for faster access
current_mapset = grassenv["MAPSET"]
@@ -472,7 +467,7 @@
# Check if the script library raises on error,
# if so we do the same
- if core.get_raise_on_error() is True:
+ if gcore.get_raise_on_error() is True:
raise_on_error = True
# Start the GRASS message interface server
@@ -481,6 +476,10 @@
_init_tgis_c_library_interface()
msgr = get_tgis_message_interface()
msgr.debug(1, "Initiate the temporal database")
+
+ ciface = get_tgis_c_library_interface()
+ driver_string = ciface.get_driver_name()
+ database_string = ciface.get_database_name()
# Set the mapset check and the timestamp write
if grassenv.has_key("TGIS_DISABLE_MAPSET_CHECK"):
@@ -493,17 +492,17 @@
enable_timestamp_write = False
msgr.warning("TGIS_DISABLE_TIMESTAMP_WRITE is True")
- if "driver" in kv:
- if kv["driver"] == "sqlite":
- tgis_backend = kv["driver"]
+ if driver_string is not None and driver_string is not "":
+ if driver_string == "sqlite":
+ tgis_backend = driver_string
try:
import sqlite3
except ImportError:
msgr.error("Unable to locate the sqlite SQL Python interface module sqlite3.")
raise
dbmi = sqlite3
- elif kv["driver"] == "pg":
- tgis_backend = kv["driver"]
+ elif driver_string == "pg":
+ tgis_backend = driver_string
try:
import psycopg2
except ImportError:
@@ -516,26 +515,15 @@
dbmi = sqlite3
else:
# Set the default sqlite3 connection in case nothing was defined
- core.run_command("t.connect", flags="d")
- kv = core.parse_command("t.connect", flags="pg")
- tgis_backend = kv["driver"]
+ gcore.run_command("t.connect", flags="d")
+ driver_string = ciface.get_driver_name()
+ database_string = ciface.get_database_name()
+ tgis_backend = driver_string
- # Database string from t.connect -pg
- tgis_database = kv["database"]
+ tgis_database_string = database_string
# Set the parameter style
tgis_dbmi_paramstyle = dbmi.paramstyle
- # Create the temporal database string
- if tgis_backend == "sqlite":
- # We substitute GRASS variables if they are located in the database string
- # This behavior is in conjunction with db.connect
- tgis_database_string = tgis_database
- tgis_database_string = tgis_database_string.replace("$GISDBASE", current_gisdbase)
- tgis_database_string = tgis_database_string.replace("$LOCATION_NAME", current_location)
- tgis_database_string = tgis_database_string.replace("$MAPSET", current_mapset)
- elif tgis_backend == "pg":
- tgis_database_string = tgis_database
-
# We do not know if the database already exists
db_exists = False
dbif = SQLDatabaseInterfaceConnection()
@@ -608,12 +596,12 @@
###############################################################################
def create_temporal_database(dbif):
- """!This function will create the temporal database
+ """This function will create the temporal database
It will create all tables and triggers that are needed to run
the temporal GIS
- @param dbif The database interface to be used
+ :param dbif: The database interface to be used
"""
global tgis_backend
global tgis_version
@@ -766,7 +754,7 @@
###############################################################################
class SQLDatabaseInterfaceConnection():
- """!This class represents the database interface connection
+ """This class represents the database interface connection
and provides access to the chisen backend modules.
The following DBMS are supported:
@@ -775,6 +763,13 @@
"""
def __init__(self):
+ """TODO:
+ Create a list of all accessible mapsets that have temporal database definitions
+ Create a database connection for each mapset, reuse existing database connections
+ in case the connection specifications are identical (a single temporal database).
+
+ Database string and river are mapset specific, hence the driver may change with the mapset.
+ """
self.connected = False
global tgis_backend
if tgis_backend == "sqlite":
@@ -786,6 +781,10 @@
self.msgr.debug(1, "SQLDatabaseInterfaceConnection constructor")
def __del__(self):
+ """TODO:
+ Close all mapset specific connections, be aware that different
+ mapsets may have identical connections.
+ """
if self.connected is True:
self.close()
@@ -801,9 +800,15 @@
self.connection.rollback()
def connect(self):
- """!Connect to the DBMI to execute SQL statements
+ """Connect to the DBMI to execute SQL statements
Supported backends are sqlite3 and postgresql
+
+ TODO:
+ Create connections for each mapset that has a temporal database.
+ Open existing connections only once.
+ The dbmi, connection and cursor are mapset specific
+ and must be managed in a dict.
"""
global tgis_database_string
@@ -824,36 +829,45 @@
self.connected = True
except Exception as e:
self.msgr.fatal(_("Unable to connect to %(db)s database: "
- "%(string)s\nException: \"%(ex)s\"\nPlease use t.connect to set a "
- "read- and writable temporal database backend")%({"db":self.dbmi.__name__,
- "string":tgis_database_string,
- "ex":e, }))
+ "%(string)s\nException: \"%(ex)s\"\nPlease use t.connect to set a "
+ "read- and writable temporal database backend")%({"db":self.dbmi.__name__,
+ "string":tgis_database_string,
+ "ex":e,}))
def close(self):
- """!Close the DBMI connection"""
+ """Close the DBMI connection
+ TODO:
+ There may be several temporal databases in a location, hence
+ close all temporal databases that have been opened. Use a dictionary
+ to manage different connections.
+
+ """
self.connection.commit()
self.cursor.close()
self.connected = False
- def mogrify_sql_statement(self, content):
- """!Return the SQL statement and arguments as executable SQL string
+ def mogrify_sql_statement(self, content, mapset=None):
+ """Return the SQL statement and arguments as executable SQL string
+
+ TODO:
+ Use the mapset argument to identify the correct database driver
- @param content The content as tuple with two entries, the first
+ :param content: The content as tuple with two entries, the first
entry is the SQL statement with DBMI specific
place holder (?), the second entry is the argument
- list that should substitue the place holder.
+ list that should substitute the place holder.
+ :param mapset: The mapset of the abstract dataset or temporal
+ database location, if None the current mapset
+ will be used
Usage:
- @code
>>> init()
>>> dbif = SQLDatabaseInterfaceConnection()
>>> dbif.mogrify_sql_statement(["SELECT ctime FROM raster_base WHERE id = ?",
... ["soil at PERMANENT",]])
"SELECT ctime FROM raster_base WHERE id = 'soil at PERMANENT'"
-
- @endcode
"""
sql = content[0]
args = content[1]
@@ -913,11 +927,18 @@
return statement
- def check_table(self, table_name):
- """!Check if a table exists in the temporal database
+ def check_table(self, table_name, mapset=None):
+ """Check if a table exists in the temporal database
- @param table_name The name of the table to be checked for existance
- @return True if the table exists, False otherwise
+ :param table_name: The name of the table to be checked for existence
+ :param mapset: The mapset of the abstract dataset or temporal
+ database location, if None the current mapset
+ will be used
+ :returns: True if the table exists, False otherwise
+
+ TODO:
+ There may be several temporal databases in a location, hence
+ the mapset is used to query the correct temporal database.
"""
table_exists = False
connected = False
@@ -943,14 +964,32 @@
self.close()
return table_exists
+
+ def execute(self, statement, mapset=None):
+ """Execute a SQL statement
- def execute_transaction(self, statement):
- """!Execute a transactional SQL statement
+ :param statement: The executable SQL statement or SQL script
+ :param mapset: The mapset of the abstract dataset or temporal
+ database location, if None the current mapset
+ will be used
+
+ NOTE: not implemented, the purpose of this function is to
+ replace all dbif.cursor.execute() calls in the temporal
+ framework to allow SQL statement execution with distributed
+ temporal databases identified by their mapset name
+ """
+ pass
- The BEGIN and END TRANSACTION statements will be added automatically
- to the sql statement
+ def execute_transaction(self, statement, mapset=None):
+ """Execute a transactional SQL statement
- @param statement The executable SQL statement or SQL script
+ The BEGIN and END TRANSACTION statements will be added automatically
+ to the sql statement
+
+ :param statement: The executable SQL statement or SQL script
+ :param mapset: The mapset of the abstract dataset or temporal
+ database location, if None the current mapset
+ will be used
"""
connected = False
if not self.connected:
@@ -980,14 +1019,14 @@
###############################################################################
def init_dbif(dbif):
- """!This method checks if the database interface connection exists,
+ """This method checks if the database interface connection exists,
if not a new one will be created, connected and True will be returned.
If the database interface exists but is connected, the connection will be established.
- @return the tuple (dbif, True|False)
+ :returns: the tuple (dbif, True|False)
Usage code sample:
- @code
+
dbif, connect = tgis.init_dbif(None)
@@ -998,7 +1037,6 @@
if connect:
dbif.close()
- @endcode
"""
if dbif is None:
dbif = SQLDatabaseInterfaceConnection()
More information about the grass-commit
mailing list