Browse Source

Using the the message interface in register.py.

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@58372 15284696-431f-4ddb-bdfa-cd5b030d7da7
Soeren Gebbert 11 years ago
parent
commit
1ec62f3642
2 changed files with 97 additions and 50 deletions
  1. 11 4
      lib/python/temporal/core.py
  2. 86 46
      lib/python/temporal/register.py

+ 11 - 4
lib/python/temporal/core.py

@@ -105,12 +105,15 @@ def _set_current_mapset(mapset=None):
 # provides a fast and exit safe interface to the C-library message functions
 # provides a fast and exit safe interface to the C-library message functions
 message_interface=None
 message_interface=None
 
 
-def _init_tgis_message_interface():
+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,
+                             call sys.exit(1) otherwise
     """
     """
     global message_interface
     global message_interface
     from grass.pygrass import messages
     from grass.pygrass import messages
-    message_interface = messages.Messenger()
+    message_interface = messages.Messenger(raise_on_error)
 
 
 def get_tgis_message_interface():
 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
@@ -226,7 +229,7 @@ def get_sql_template_path():
 
 
 ###############################################################################
 ###############################################################################
 
 
-def init():
+def init(raise_on_error=False):
     """!This function set the correct database backend from the environmental variables
     """!This function set the correct database backend from the environmental variables
        and creates the grass location database structure for raster,
        and creates the grass location database structure for raster,
        vector and raster3d maps as well as for the space-time datasets strds,
        vector and raster3d maps as well as for the space-time datasets strds,
@@ -234,6 +237,10 @@ def init():
 
 
         ATTENTION: This functions must be called before any spatio-temporal processing
         ATTENTION: This functions must be called before any spatio-temporal processing
                    can be started
                    can be started
+
+       @param raise_on_error If True raise a FatalError exception in case of a fatal error,
+                             call sys.exit(1) otherwise
+
     """
     """
     # We need to set the correct database backend from the environment variables
     # We need to set the correct database backend from the environment variables
     global tgis_backend
     global tgis_backend
@@ -245,7 +252,7 @@ def init():
     # Set the global variable current_mapset for fast mapset access
     # Set the global variable current_mapset for fast mapset access
     _set_current_mapset(grassenv["MAPSET"])
     _set_current_mapset(grassenv["MAPSET"])
     # Start the GRASS message interface server
     # Start the GRASS message interface server
-    _init_tgis_message_interface()
+    _init_tgis_message_interface(raise_on_error)
     # Start the C-library interface server
     # Start the C-library interface server
     _init_tgis_c_library_interface()
     _init_tgis_c_library_interface()
 
 

+ 86 - 46
lib/python/temporal/register.py

@@ -41,8 +41,32 @@ def register_maps_in_space_time_dataset(
        It takes care of the correct update of the space time datasets from all
        It takes care of the correct update of the space time datasets from all
        registered maps.
        registered maps.
 
 
+       @code
+
+        >>> import grass.script as grass
+        >>> import grass.temporal as tgis
+        >>> grass.use_temp_region()
+        >>> grass.run_command("g.region", n=80.0, s=0.0, e=120.0, w=0.0,
+        ... t=1.0, b=0.0, res=10.0)
+        0
+        >>> grass.run_command("r.mapcalc", overwrite=True, quiet=True, expression="register_map_1 = 1")
+        0
+        >>> grass.run_command("r.mapcalc", overwrite=True, quiet=True, expression="register_map_2 = 2")
+        0
+        >>> grass.run_command("r.mapcalc", overwrite=True, quiet=True, expression="register_map_3 = 3")
+        0
+        >>> grass.run_command("r.mapcalc", overwrite=True, quiet=True, expression="register_map_4 = 4")
+        0
+        >>> tgis.init(True)
+        >>> tgis.register_maps_in_space_time_dataset(type="strds", name=None, 
+        ...               maps="register_map_1,register_map_2,register_map_3,register_map_4",
+        ...               start="2001-01-01", increment="1 day", interval=True)
+
+       @endcode
+
        @param type The type of the maps rast, rast3d or vect
        @param type The type of the maps rast, rast3d or vect
-       @param name The name of the space time dataset
+       @param name The name of the space time dataset. Maps will be registered in the
+                   temporal database if the name was set to None
        @param maps A comma separated list of map names
        @param maps A comma separated list of map names
        @param file Input file, one map per line map with start and optional
        @param file Input file, one map per line map with start and optional
                    end time
                    end time
@@ -65,23 +89,31 @@ def register_maps_in_space_time_dataset(
 
 
     start_time_in_file = False
     start_time_in_file = False
     end_time_in_file = False
     end_time_in_file = False
+    msgr = get_tgis_message_interface()
+
+    # Make sure the arguments are of type string
+    if start != "" and start is not None:
+        start = str(start)
+    if end != "" and end is not None:
+        end = str(end)
+    if increment != "" and increment is not None:
+        increment = str(increment)
 
 
     if maps and file:
     if maps and file:
-        core.fatal(_("%(m)s= and %(f)s= are mutually exclusive") % {'m': "maps",
+        msgr.fatal(_("%(m)s= and %(f)s= are mutually exclusive") % {'m': "maps",
                                                                     'f': "file"})
                                                                     'f': "file"})
 
 
     if end and increment:
     if end and increment:
-        core.fatal(_("%(e)s= and %(i)s= are mutually exclusive") % {'e': "end",
+        msgr.fatal(_("%(e)s= and %(i)s= are mutually exclusive") % {'e': "end",
                    'i': "increment"})
                    'i': "increment"})
 
 
     if end and not start:
     if end and not start:
-        core.fatal(_("Please specify %(st)s= and %(e)s=") % {'st': "start_time",
+        msgr.fatal(_("Please specify %(st)s= and %(e)s=") % {'st': "start_time",
                                                              'e': "end_time"})
                                                              'e': "end_time"})
 
 
     if not maps and not file:
     if not maps and not file:
-        core.fatal(_("Please specify %(m)s= or %(f)s=") % {'m': "maps",
+        msgr.fatal(_("Please specify %(m)s= or %(f)s=") % {'m': "maps",
                                                            'f': "file"})
                                                            'f': "file"})
-
     # We may need the mapset
     # We may need the mapset
     mapset = get_current_mapset()
     mapset = get_current_mapset()
     dbif, connected = init_dbif(None)
     dbif, connected = init_dbif(None)
@@ -92,7 +124,7 @@ def register_maps_in_space_time_dataset(
 
 
         if sp.is_time_relative() and not unit:
         if sp.is_time_relative() and not unit:
             dbif.close()
             dbif.close()
-            core.fatal(_("Space time %(sp)s dataset <%(name)s> with relative"
+            msgr.fatal(_("Space time %(sp)s dataset <%(name)s> with relative"
                          " time found, but no relative unit set for %(sp)s "
                          " time found, but no relative unit set for %(sp)s "
                          "maps") % {
                          "maps") % {
                          'sp': sp.get_new_map_instance(None).get_type(),
                          'sp': sp.get_new_map_instance(None).get_type(),
@@ -158,11 +190,11 @@ def register_maps_in_space_time_dataset(
     # Store the ids of datasets that must be updated
     # Store the ids of datasets that must be updated
     datatsets_to_modify = {}
     datatsets_to_modify = {}
 
 
-    core.message(_("Gathering map informations"))
+    msgr.message(_("Gathering map informations"))
 
 
     for count in range(len(maplist)):
     for count in range(len(maplist)):
         if count % 50 == 0:
         if count % 50 == 0:
-            core.percent(count, num_maps, 1)
+            msgr.percent(count, num_maps, 1)
 
 
         # Get a new instance of the map type
         # Get a new instance of the map type
         map = dataset_factory(type, maplist[count]["id"])
         map = dataset_factory(type, maplist[count]["id"])
@@ -181,13 +213,13 @@ def register_maps_in_space_time_dataset(
             if (start == "" or start is None) and not map.has_grass_timestamp():
             if (start == "" or start is None) and not map.has_grass_timestamp():
                 dbif.close()
                 dbif.close()
                 if map.get_layer():
                 if map.get_layer():
-                    core.fatal(_("Unable to register %(t)s map <%(id)s> with "
+                    msgr.fatal(_("Unable to register %(t)s map <%(id)s> with "
                                  "layer %(l)s. The map has timestamp and "
                                  "layer %(l)s. The map has timestamp and "
                                  "the start time is not set.") % {
                                  "the start time is not set.") % {
                                  't': map.get_type(), 'id': map.get_map_id(),
                                  't': map.get_type(), 'id': map.get_map_id(),
                                  'l': map.get_layer()})
                                  'l': map.get_layer()})
                 else:
                 else:
-                    core.fatal(_("Unable to register %(t)s map <%(id)s>. The"
+                    msgr.fatal(_("Unable to register %(t)s map <%(id)s>. The"
                                  " map has no timestamp and the start time "
                                  " map has no timestamp and the start time "
                                  "is not set.") % {'t': map.get_type(),
                                  "is not set.") % {'t': map.get_type(),
                                                    'id': map.get_map_id()})
                                                    'id': map.get_map_id()})
@@ -195,9 +227,9 @@ def register_maps_in_space_time_dataset(
                 # We need to check if the time is absolute and the unit was specified
                 # We need to check if the time is absolute and the unit was specified
                 time_object = check_datetime_string(start)
                 time_object = check_datetime_string(start)
                 if isinstance(time_object, datetime) and unit:
                 if isinstance(time_object, datetime) and unit:
-                    core.fatal(_("%(u)s= can only be set for relative time") % {'u': "maps"})
+                    msgr.fatal(_("%(u)s= can only be set for relative time") % {'u': "unit"})
                 if not isinstance(time_object, datetime) and not unit:
                 if not isinstance(time_object, datetime) and not unit:
-                    core.fatal(_("%(u)s= must be set in case of relative time stamps") % {'u': "maps"})
+                    msgr.fatal(_("%(u)s= must be set in case of relative time stamps") % {'u': "unit"})
 
 
                 if unit:
                 if unit:
                     map.set_time_to_relative()
                     map.set_time_to_relative()
@@ -209,14 +241,14 @@ def register_maps_in_space_time_dataset(
             # Check the overwrite flag
             # Check the overwrite flag
             if not core.overwrite():
             if not core.overwrite():
                 if map.get_layer():
                 if map.get_layer():
-                    core.warning(_("Map is already registered in temporal "
+                    msgr.warning(_("Map is already registered in temporal "
                                    "database. Unable to update %(t)s map "
                                    "database. Unable to update %(t)s map "
                                    "<%(id)s> with layer %(l)s. Overwrite flag"
                                    "<%(id)s> with layer %(l)s. Overwrite flag"
                                    " is not set.") % {'t': map.get_type(),
                                    " is not set.") % {'t': map.get_type(),
                                                       'id': map.get_map_id(),
                                                       'id': map.get_map_id(),
                                                       'l': str(map.get_layer())})
                                                       'l': str(map.get_layer())})
                 else:
                 else:
-                    core.warning(_("Map is already registered in temporal "
+                    msgr.warning(_("Map is already registered in temporal "
                                    "database. Unable to update %(t)s map "
                                    "database. Unable to update %(t)s map "
                                    "<%(id)s>. Overwrite flag is not set.") % {
                                    "<%(id)s>. Overwrite flag is not set.") % {
                                    't': map.get_type(), 'id': map.get_map_id()})
                                    't': map.get_type(), 'id': map.get_map_id()})
@@ -239,13 +271,13 @@ def register_maps_in_space_time_dataset(
                 if name and map.get_temporal_type() != sp.get_temporal_type():
                 if name and map.get_temporal_type() != sp.get_temporal_type():
                     dbif.close()
                     dbif.close()
                     if map.get_layer():
                     if map.get_layer():
-                        core.fatal(_("Unable to update %(t)s map <%(id)s> "
+                        msgr.fatal(_("Unable to update %(t)s map <%(id)s> "
                                      "with layer %(l)s. The temporal types "
                                      "with layer %(l)s. The temporal types "
                                      "are different.") % {'t': map.get_type(),
                                      "are different.") % {'t': map.get_type(),
                                                         'id': map.get_map_id(),
                                                         'id': map.get_map_id(),
                                                         'l': map.get_layer()})
                                                         'l': map.get_layer()})
                     else:
                     else:
-                        core.fatal(_("Unable to update %(t)s map <%(id)s>. "
+                        msgr.fatal(_("Unable to update %(t)s map <%(id)s>. "
                                      "The temporal types are different.") %
                                      "The temporal types are different.") %
                                      {'t': map.get_type(),
                                      {'t': map.get_type(),
                                       'id': map.get_map_id()})
                                       'id': map.get_map_id()})
@@ -288,26 +320,26 @@ def register_maps_in_space_time_dataset(
         if name:
         if name:
             map_object_list.append(map)
             map_object_list.append(map)
 
 
-    core.percent(num_maps, num_maps, 1)
+    msgr.percent(num_maps, num_maps, 1)
 
 
     if statement is not None and statement != "":
     if statement is not None and statement != "":
-        core.message(_("Register maps in the temporal database"))
+        msgr.message(_("Register maps in the temporal database"))
         dbif.execute_transaction(statement)
         dbif.execute_transaction(statement)
 
 
     # Finally Register the maps in the space time dataset
     # Finally Register the maps in the space time dataset
     if name and map_object_list:
     if name and map_object_list:
         count = 0
         count = 0
         num_maps = len(map_object_list)
         num_maps = len(map_object_list)
-        core.message(_("Register maps in the space time raster dataset"))
+        msgr.message(_("Register maps in the space time raster dataset"))
         for map in map_object_list:
         for map in map_object_list:
             if count % 50 == 0:
             if count % 50 == 0:
-                core.percent(count, num_maps, 1)
+                msgr.percent(count, num_maps, 1)
             sp.register_map(map=map, dbif=dbif)
             sp.register_map(map=map, dbif=dbif)
             count += 1
             count += 1
 
 
     # Update the space time tables
     # Update the space time tables
     if name and map_object_list:
     if name and map_object_list:
-        core.message(_("Update space time raster dataset"))
+        msgr.message(_("Update space time raster dataset"))
         sp.update_from_registered_maps(dbif)
         sp.update_from_registered_maps(dbif)
         sp.update_command_string(dbif=dbif)
         sp.update_command_string(dbif=dbif)
 
 
@@ -326,7 +358,7 @@ def register_maps_in_space_time_dataset(
     if connected == True:
     if connected == True:
         dbif.close()
         dbif.close()
 
 
-    core.percent(num_maps, num_maps, 1)
+    msgr.percent(num_maps, num_maps, 1)
 
 
 
 
 ###############################################################################
 ###############################################################################
@@ -354,10 +386,12 @@ def assign_valid_time_to_map(ttype, map, start, end, unit, increment=None,
                         time and an increment is provided
                         time and an increment is provided
     """
     """
 
 
+    msgr = get_tgis_message_interface()
+
     if ttype == "absolute":
     if ttype == "absolute":
         start_time = string_to_datetime(start)
         start_time = string_to_datetime(start)
         if start_time is None:
         if start_time is None:
-            core.fatal(_("Unable to convert string \"%s\"into a "
+            msgr.fatal(_("Unable to convert string \"%s\"into a "
                          "datetime object") % (start))
                          "datetime object") % (start))
         end_time = None
         end_time = None
 
 
@@ -365,7 +399,7 @@ def assign_valid_time_to_map(ttype, map, start, end, unit, increment=None,
             end_time = string_to_datetime(end)
             end_time = string_to_datetime(end)
             if end_time is None:
             if end_time is None:
                 dbif.close()
                 dbif.close()
-                core.fatal(_("Unable to convert string \"%s\"into a "
+                msgr.fatal(_("Unable to convert string \"%s\"into a "
                              "datetime object") % (end))
                              "datetime object") % (end))
 
 
         # Add the increment
         # Add the increment
@@ -373,21 +407,21 @@ def assign_valid_time_to_map(ttype, map, start, end, unit, increment=None,
             start_time = increment_datetime_by_string(
             start_time = increment_datetime_by_string(
                 start_time, increment, mult)
                 start_time, increment, mult)
             if start_time is None:
             if start_time is None:
-                core.fatal(_("Error in increment computation"))
+                msgr.fatal(_("Error in increment computation"))
             if interval:
             if interval:
                 end_time = increment_datetime_by_string(
                 end_time = increment_datetime_by_string(
                     start_time, increment, 1)
                     start_time, increment, 1)
                 if end_time is None:
                 if end_time is None:
-                    core.fatal(_("Error in increment computation"))
-        # Commented because of performance issue calling g.message thousend times
-        #if map.get_layer():
-        #    core.verbose(_("Set absolute valid time for map <%(id)s> with "
-        #                   "layer %(layer)s to %(start)s - %(end)s") %
-        #                 {'id': map.get_map_id(), 'layer': map.get_layer(),
-        #                  'start': str(start_time), 'end': str(end_time)})
-        #else:
-        #    core.verbose(_("Set absolute valid time for map <%s> to %s - %s") %
-        #                 (map.get_map_id(), str(start_time), str(end_time)))
+                    msgr.fatal(_("Error in increment computation"))
+
+        if map.get_layer():
+            msgr.verbose(_("Set absolute valid time for map <%(id)s> with "
+                           "layer %(layer)s to %(start)s - %(end)s") %
+                         {'id': map.get_map_id(), 'layer': map.get_layer(),
+                          'start': str(start_time), 'end': str(end_time)})
+        else:
+            msgr.verbose(_("Set absolute valid time for map <%s> to %s - %s") %
+                         (map.get_map_id(), str(start_time), str(end_time)))
 
 
         map.set_absolute_time(start_time, end_time, None)
         map.set_absolute_time(start_time, end_time, None)
     else:
     else:
@@ -402,15 +436,21 @@ def assign_valid_time_to_map(ttype, map, start, end, unit, increment=None,
             if interval:
             if interval:
                 end_time = start_time + int(increment)
                 end_time = start_time + int(increment)
 
 
-        # Commented because of performance issue calling g.message thousend times
-        #if map.get_layer():
-        #    core.verbose(_("Set relative valid time for map <%s> with layer %s "
-        #                   "to %i - %s with unit %s") %
-        #                 (map.get_map_id(), map.get_layer(), start_time,
-        #                  str(end_time), unit))
-        #else:
-        #    core.verbose(_("Set relative valid time for map <%s> to %i - %s "
-        #                   "with unit %s") % (map.get_map_id(), start_time,
-        #                                      str(end_time), unit))
+        if map.get_layer():
+            msgr.verbose(_("Set relative valid time for map <%s> with layer %s "
+                           "to %i - %s with unit %s") %
+                         (map.get_map_id(), map.get_layer(), start_time,
+                          str(end_time), unit))
+        else:
+            msgr.verbose(_("Set relative valid time for map <%s> to %i - %s "
+                           "with unit %s") % (map.get_map_id(), start_time,
+                                              str(end_time), unit))
 
 
         map.set_relative_time(start_time, end_time, unit)
         map.set_relative_time(start_time, end_time, unit)
+
+
+###############################################################################
+
+if __name__ == "__main__":
+    import doctest
+    doctest.testmod()