@@ -9,7 +9,10 @@ class ExecutedTool:
99 def __init__ (self , name , kwargs , stdout , stderr ):
1010 self ._name = name
1111 self ._stdout = stdout
12- self ._decoded_stdout = gs .decode (self ._stdout )
12+ if self ._stdout :
13+ self ._decoded_stdout = gs .decode (self ._stdout )
14+ else :
15+ self ._decoded_stdout = ""
1316
1417 @property
1518 def text (self ):
@@ -45,8 +48,14 @@ class SubExecutor:
4548 """use as tools().params(a="x", b="y").g_region()"""
4649
4750 # a and b would be overwrite or stdin
48-
4951 # Can support other envs or all PIPE and encoding read command supports
52+ def __init__ (self , * , tools , env , stdin = None ):
53+ self ._tools = tools
54+ self ._env = env
55+ self ._stdin = stdin
56+
57+ def run (self , name , / , ** kwargs ):
58+ pass
5059
5160
5261class Tools :
@@ -60,6 +69,7 @@ def __init__(
6069 verbose = False ,
6170 superquiet = False ,
6271 freeze_region = False ,
72+ stdin = None ,
6373 ):
6474 # TODO: fix region, so that external g.region call in the middle
6575 # is not a problem
@@ -85,6 +95,7 @@ def __init__(
8595 self ._env ["GRASS_VERBOSE" ] = "1"
8696 if verbose :
8797 self ._env ["GRASS_VERBOSE" ] = "3"
98+ self ._set_stdin (stdin )
8899
89100 # These could be public, not protected.
90101 def _freeze_region (self ):
@@ -94,6 +105,10 @@ def _freeze_region(self):
94105 def _overwrite (self ):
95106 self ._env ["GRASS_OVERWRITE" ] = "1"
96107
108+ def _set_stdin (self , stdin , / ):
109+ print ("_set_stdin" , stdin )
110+ self ._stdin = stdin
111+
97112 @property
98113 def env (self ):
99114 return self ._env
@@ -107,10 +122,23 @@ def run(self, name, /, **kwargs):
107122 :param str module: name of GRASS module
108123 :param `**kwargs`: named arguments passed to run_command()"""
109124 # alternatively use dev null as default or provide it as convenient settings
110- kwargs ["stdout" ] = gs .PIPE
111- kwargs ["stderr" ] = gs .PIPE
112- process = gs .pipe_command (name , env = self ._env , ** kwargs )
113- stdout , stderr = process .communicate ()
125+ stdout_pipe = gs .PIPE
126+ stderr_pipe = gs .PIPE
127+ if self ._stdin :
128+ stdin_pipe = gs .PIPE
129+ stdin = gs .utils .encode (self ._stdin )
130+ else :
131+ stdin_pipe = None
132+ stdin = None
133+ process = gs .start_command (
134+ name ,
135+ env = self ._env ,
136+ ** kwargs ,
137+ stdin = stdin_pipe ,
138+ stdout = stdout_pipe ,
139+ stderr = stderr_pipe ,
140+ )
141+ stdout , stderr = process .communicate (input = stdin )
114142 stderr = gs .utils .decode (stderr )
115143 returncode = process .poll ()
116144 # TODO: instead of printing, do exception right away
@@ -131,6 +159,11 @@ def run(self, name, /, **kwargs):
131159 sys .stderr .write (stderr )
132160 gs .handle_errors (returncode , stdout , [name ], kwargs )
133161 return ExecutedTool (name = name , kwargs = kwargs , stdout = stdout , stderr = stderr )
162+ # executor = SubExecutor(tools=self, env=self._env, stdin=self._stdin)
163+ # return executor.run(name, **kwargs)
164+
165+ def feed_input_to (self , stdin , / ):
166+ return Tools (env = self ._env , stdin = stdin )
134167
135168 def __getattr__ (self , name ):
136169 """Parse attribute to GRASS display module. Attribute should be in
@@ -192,8 +225,27 @@ def _test():
192225 tools .g_region (res = 500 ) # we would do this for another computation elsewhere
193226 print (independent_computation .g_region (flags = "g" ).keyval ["ewres" ])
194227
195- tools_pro = Tools (session = session , freeze_region = True , superquiet = True )
228+ tools_pro = Tools (
229+ session = session , freeze_region = True , overwrite = True , superquiet = True
230+ )
231+ # gs.feed_command("v.in.ascii",
232+ # input="-", output="point", separator=",",
233+ # stdin="13.45,29.96,200", overwrite=True)
196234 tools_pro .r_slope_aspect (elevation = "elevation" , slope = "slope" )
235+ tools_pro .feed_input_to ("13.45,29.96,200" ).v_in_ascii (
236+ input = "-" , output = "point" , separator = ","
237+ )
238+ print (tools_pro .v_info (map = "point" , flags = "t" ).keyval ["points" ])
239+
240+ # try:
241+ tools_pro .feed_input_to ("13.45,29.96,200" ).v_in_ascii (
242+ input = "-" ,
243+ output = "point" ,
244+ format = "xstandard" ,
245+ )
246+ # except gs.CalledModuleError as error:
247+ # print("Exception text:")
248+ # print(error)
197249
198250
199251if __name__ == "__main__" :
0 commit comments