Created
April 25, 2018 21:32
-
-
Save jbest/4a8b64b5bd3d7bd980768fc7fe90b3d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
I'm trying to save the results of a session (stored | |
in the session dictionary) after the user has exited | |
the Gooey interface. This worked in a previous version | |
where I was using the click CLI module but when I added | |
Gooey, only the initial values stored in the session | |
dictionary are available upon script termination. | |
Requires: | |
Gooey | |
watchdog | |
Python3 | |
Use: | |
python3 test.py | |
In the Gooey interface, select a folder. | |
Create a file ending in 'txt' | |
The file event should display in the Gooey window | |
and the event count should increment (by 1 or 2 depending | |
on your OS) and the updated event list will display. | |
Press Stop button and confirm by pressing Yes button | |
Expected the full event list (session['events']) to be | |
printed in the terminal. Instead only the initial values | |
are printed. | |
""" | |
import atexit | |
import time | |
import os | |
from gooey import Gooey, GooeyParser | |
from watchdog.observers import Observer | |
from watchdog.events import PatternMatchingEventHandler | |
# Create a test dict which will be modified in each function | |
# except those in the ImageHandler class | |
test_dict = {} | |
test_dict['value'] = 'initial' | |
session = {} | |
# Add fake dict for events key to see if it persists until app terminates | |
session['events'] = [{'event_type':'initial', 'src_path':'initial', 'dest_path':'initial'}] | |
print(session['events']) | |
print(test_dict) | |
class ImageHandler(PatternMatchingEventHandler): | |
def on_any_event(self, event): | |
# I don't think global is needed here, it doesn't help, but testing in case | |
global session | |
if hasattr(event, 'event_type'): | |
event_type = event.event_type | |
else: | |
event_type = None | |
if hasattr(event, 'src_path'): | |
src_path = event.src_path | |
else: | |
src_path = None | |
if hasattr(event, 'dest_path'): | |
dest_path = event.dest_path | |
else: | |
dest_path = None | |
event = {'event_type':event_type, 'src_path':src_path, 'dest_path':dest_path} | |
print('New file event:', event) | |
session['events'].append(event) | |
# Printing to confirm all events are registered | |
print('All file events:') | |
print(session['events']) | |
@Gooey(program_name='Test') | |
def main(): | |
global session | |
test_dict['value'] = 'in main' | |
print(test_dict) | |
client_parser = GooeyParser(description='Test Description') | |
client_parser.add_argument('directory', help='Specify directory.', widget="DirChooser") | |
args = client_parser.parse_args() | |
observed_path = os.path.abspath(args.directory) | |
# Just testing with txt files for now | |
# Generate new file events by creating, renaming, deleting .txt files in chosen directory | |
event_handler = ImageHandler(patterns=['*.txt']) | |
observer = Observer() | |
observer.schedule(event_handler, observed_path, recursive=True) | |
observer.start() | |
try: | |
while True: | |
time.sleep(1) | |
# Printing event count to confirm that session var is in scope | |
print('Event count:{}'.format(len(session['events']))) | |
except KeyboardInterrupt: | |
# This is a holdover from the click CLI | |
# Kept for testing | |
print('Ending by KeyboardInterrupt') | |
def end_session(): | |
test_dict['value'] = 'ending' | |
print(test_dict) | |
print(session['events']) | |
print('Above value is the initial value of session[events], not the expected list generated during execution.') | |
if __name__ == '__main__': | |
# Using atexit because I don't know another way to call a function | |
# when user terminates script via Gooey window. | |
atexit.register(end_session) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment