瀏覽代碼

Add mlist_strings, mlist_pairs
Add flag= option to mlist_*


git-svn-id: https://svn.osgeo.org/grass/grass/trunk@50971 15284696-431f-4ddb-bdfa-cd5b030d7da7

Glynn Clements 13 年之前
父節點
當前提交
2f1f5471c9
共有 1 個文件被更改,包括 42 次插入2 次删除
  1. 42 2
      lib/python/core.py

+ 42 - 2
lib/python/core.py

@@ -706,7 +706,46 @@ def list_strings(type):
 
 # interface to g.mlist
 
-def mlist_grouped(type, pattern = None, check_search_path = True):
+def mlist_strings(type, pattern = None, mapset = None, flag = ''):
+    """!List of elements as strings.
+
+    Returns the output from running g.mlist, as a list of qualified
+    names.
+
+    @param type element type (rast, vect, rast3d, region, ...)
+    @param pattern pattern string
+    @param mapset mapset name (if not given use search path)
+    @param flag pattern type: 'r' (basic regexp), 'e' (extended regexp), or '' (glob pattern)
+
+    @return list of elements
+    """
+    result = list()
+    for line in read_command("g.mlist",
+                             quiet = True,
+                             flags = 'm' + flag,
+                             type = type,
+                             pattern = pattern,
+                             mapset = mapset).splitlines():
+        result.append(line.strip())
+
+    return result
+
+def mlist_pairs(type, pattern = None, mapset = None, flag = ''):
+    """!List of elements as pairs
+
+    Returns the output from running g.mlist, as a list of
+    (name, mapset) pairs
+
+    @param type element type (rast, vect, rast3d, region, ...)
+    @param pattern pattern string
+    @param mapset mapset name (if not given use search path)
+    @param flag pattern type: 'r' (basic regexp), 'e' (extended regexp), or '' (glob pattern)
+
+    @return list of elements
+    """
+    return [tuple(map.split('@', 1)) for map in mlist_strings(type, pattern, mapset, flag)]
+
+def mlist_grouped(type, pattern = None, check_search_path = True, flag = ''):
     """!List of elements grouped by mapsets.
 
     Returns the output from running g.mlist, as a dictionary where the
@@ -721,6 +760,7 @@ def mlist_grouped(type, pattern = None, check_search_path = True):
     @param type element type (rast, vect, rast3d, region, ...)
     @param pattern pattern string
     @param check_search_path True to add mapsets for the search path with no found elements
+    @param flag pattern type: 'r' (basic regexp), 'e' (extended regexp), or '' (glob pattern)
 
     @return directory of mapsets/elements
     """
@@ -730,7 +770,7 @@ def mlist_grouped(type, pattern = None, check_search_path = True):
             result[mapset] = []
     
     mapset = None
-    for line in read_command("g.mlist", quiet = True, flags = "m",
+    for line in read_command("g.mlist", quiet = True, flags = "m" + flag,
                              type = type, pattern = pattern).splitlines():
         try:
             name, mapset = line.split('@')