Add examples for call external programs

This commit is contained in:
Mauricio Baeza 2021-06-21 22:36:48 -05:00
parent 27ae3fe66b
commit abfab3d254
2 changed files with 61 additions and 9 deletions

View File

@ -344,3 +344,52 @@ Execute macro in other thread
.. code-block:: python
app.call_macro(args, True)
Call external program
^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
app_name = 'gnome-calculator'
app.run(app_name)
app.msgbox('ok')
Call command line and capture output
.. code-block:: python
args = 'ls -lh ~'
result = app.run(args, True)
app.debug(result)
.. code-block:: bash
21/06/2021 22:27:22 - DEBUG - total 1.3M
drwxr-xr-x 5 mau mau 4.0K Jun 17 13:09 Desktop
drwxr-xr-x 6 mau mau 4.0K Jun 15 12:35 Documents
drwxr-xr-x 2 mau mau 4.0K Jun 21 20:26 Downloads
drwxr-xr-x 2 mau mau 4.0K Jun 21 16:18 Pictures
drwxr-xr-x 13 mau mau 4.0K Jun 21 15:34 Projects
drwxr-xr-x 2 mau mau 4.0K May 11 18:48 Templates
drwxr-xr-x 2 mau mau 4.0K Jun 20 23:27 Videos
Call command line and capture output line by line.
.. code-block:: python
args = 'ls -lh /home/mau'
for line in app.popen(args):
app.debug(line)
.. code-block:: bash
21/06/2021 22:34:42 - DEBUG - total 1.3M
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 5 mau mau 4.0K Jun 17 13:09 Desktop
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 6 mau mau 4.0K Jun 15 12:35 Documents
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 2 mau mau 4.0K Jun 21 20:26 Downloads
21/06/2021 22:34:42 - DEBUG - -rw-r----- 1 mau mau 1.3M Jun 14 11:53 out.png
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 2 mau mau 4.0K Jun 21 16:18 Pictures
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 13 mau mau 4.0K Jun 21 15:34 Projects
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 2 mau mau 4.0K May 11 18:48 Templates
21/06/2021 22:34:42 - DEBUG - drwxr-xr-x 2 mau mau 4.0K Jun 20 23:27 Videos

View File

@ -597,17 +597,20 @@ def call_macro(args, in_thread=False):
result = _call_macro(args)
return result
# ~ TODO
def run(command, capture=False, split=True):
if not split:
return subprocess.check_output(command, shell=True).decode()
cmd = shlex.split(command)
result = subprocess.run(cmd, capture_output=capture, text=True, shell=IS_WIN)
if capture:
result = result.stdout
def run(command, capture=False, split=False):
if split:
cmd = shlex.split(command)
result = subprocess.run(cmd, capture_output=capture, text=True, shell=IS_WIN)
if capture:
result = result.stdout
else:
result = result.returncode
else:
result = result.returncode
if capture:
result = subprocess.check_output(command, shell=True).decode()
else:
result = subprocess.Popen(command)
return result