Skip to content

Commit

Permalink
Allow to run command in background
Browse files Browse the repository at this point in the history
  • Loading branch information
rotten77 committed Jun 11, 2024
1 parent c897079 commit df41987
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ You can add these elements:

<button label="Click me!">run_this_app.exe</button>

* *Attributes:*

* `label`
* `new_console` *[default: True]*: runs the command in a new window (`subprocess.CREATE_NEW_CONSOLE`) or in background (`subprocess.CREATE_NO_WINDOW`)
* `show_output` *[default: 'never']*: shows `stdout` in a modal dialog if `new_console=false`, possible options are:
* `never`
* `always`
* `if_not_empty`

**Text** can be used as a section separator or for some fancy notes.

<text>Some fancy text note</text>
Expand All @@ -43,4 +52,19 @@ Executing PowerShell script with `-NoExit` tag so the window will not close afte

It's also possible to just run commands right from the app (`ipconfig /all` in this case):

<button label="ipconfig">powershell.exe -NoExit -Command "ipconfig /all"</button>
<button label="ipconfig">powershell.exe -NoExit -Command "ipconfig /all"</button>

Run the command in background and show stdout after it's executed:

<button label="Hello World!" new_console="false" show_output="if_not_empty">powershell.exe -Command "Write-Host 'Hello World!'"</button>

## Changelog


*1.1.4*

* Command can be executed in background using new attribute `new_console`

*1.0*

* First version of Sekubu released!
3 changes: 2 additions & 1 deletion sekubu.todo
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ Sekubu:
✔ Resizing not working correctly @done(24-02-06 18:39)
☐ Refactor code to use classes
✔ Config read/write argument should be dict @done(24-02-06 18:39)
✘ Another TK window collision? - generic PyInstaller issue @cancelled(24-02-06 13:56)
✘ Another TK window collision? - generic PyInstaller issue @cancelled(24-02-06 13:56)
✔ Allow to run command in background @done(24-06-02 09:38)
17 changes: 14 additions & 3 deletions sekubu/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import shlex, subprocess
import os, sys

VERSION = '1.0.0'
VERSION = '1.1.4'

def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
Expand Down Expand Up @@ -41,7 +41,18 @@ def run_command(sender):
except Exception as e:
show_info('Exception', f'Parsing the command was not successful: {e}')
try:
subprocess.Popen(args, creationflags=subprocess.CREATE_NEW_CONSOLE)
if result["new_console"] == True:
subprocess.Popen(args, creationflags=subprocess.CREATE_NEW_CONSOLE)
else:
process = subprocess.Popen(args, creationflags=subprocess.CREATE_NO_WINDOW, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result["show_output"] in ['always', 'if_not_empty']:
stdout, stderr = process.communicate()
stdout = stdout.decode('utf-8')

if result["show_output"] == 'if_not_empty' and len(stdout) == 0:
return
show_info(f"{result['label']}:", stdout)

except Exception as e:
show_info('Exception', f'Executing the command was not successful: {e}')

Expand All @@ -55,7 +66,7 @@ def create_gui(data):

theme = create_theme_imgui_dark() if data['config']['theme'] == 'dark' else create_theme_imgui_light()
dpg.bind_theme(theme)

with dpg.window(tag="sekubu_window"):
if data['exception']!="":
dpg.add_text(data['exception'])
Expand Down
2 changes: 1 addition & 1 deletion sekubu/sebuku_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def create_example_config(file_path: str):
<text>This is an example</text>
<separator/>
<button label="Calculator">calc.exe</button>
<button label="Computer name">powershell.exe -NoExit -Command "$env:COMPUTERNAME"</button>
<button label="Computer name" new_console="false" show_output="always">powershell.exe -Command "$env:COMPUTERNAME"</button>
<separator/>
<text>What to do?</text>
<text>1) Close application</text>
Expand Down
20 changes: 19 additions & 1 deletion sekubu/sekubu_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,25 @@ def sekubu_parser(file_path: str):
else:
label = element.get('label')

layout.append({'type': 'button', 'label': label, 'tag': f'button_{i}', 'command': element.text})
new_console = True
if 'new_console' in element.attrib:
new_console_attr = element.get('new_console').lower().strip()
if new_console_attr == "false":
new_console = False


show_output = 'never'
if new_console == False:
if 'show_output' in element.attrib:
show_output = element.get('show_output').lower().strip()
if show_output=='true':
show_output = 'always'
elif show_output=='false':
show_output = 'never'
if show_output not in ['never', 'always', 'if_not_empty']:
show_output = 'never'

layout.append({'type': 'button', 'label': label, 'tag': f'button_{i}', 'command': element.text, 'new_console': new_console, 'show_output': show_output})

return {
'layout': layout,
Expand Down

0 comments on commit df41987

Please sign in to comment.