Browse Source

Discovered atexit module. registered cleanup function to remove generated files

pull/69/head
chriskiehl 10 years ago
parent
commit
fa4bf4dc5c
2 changed files with 11 additions and 13 deletions
  1. 20
      gooey/python_bindings/gooey_decorator.py
  2. 4
      gooey/python_bindings/modules.py

20
gooey/python_bindings/gooey_decorator.py

@ -46,8 +46,10 @@ done.
'''
import os
import tempfile
import wx
import source_parser
import atexit
from functools import partial
from gooey.gui.lang import i18n
from gooey.gui.windows import layouts
@ -77,15 +79,16 @@ def Gooey(f=None, advanced=True,
_, filename = os.path.split(main_module_path)
cleaned_source = clean_source(main_module_path)
filepath = os.path.join(create_local_storage(), 'gooey_' + filename)
descriptor, tmp_filepath = tempfile.mkstemp(suffix='.py')
atexit.register(cleanup, descriptor, tmp_filepath)
with open(filepath, 'w') as f:
with open(tmp_filepath, 'w') as f:
f.write(cleaned_source)
if not has_argparse(cleaned_source):
show_config = False
run_cmd = 'python {}'.format(filepath)
run_cmd = 'python {}'.format(tmp_filepath)
# Must be called before anything else
app = wx.App(False)
@ -165,14 +168,9 @@ def has_argparse(source):
bla = ['.parse_args()' in line.lower() for line in source.split('\n')]
return any(bla)
def create_local_storage():
path = os.path.join(os.getcwd(), '_gooey_generated_files_')
try:
os.mkdir(path)
except OSError:
pass # already exists
return path
def cleanup(descriptor, filepath):
os.close(descriptor)
os.remove(filepath)
if __name__ == '__main__':

4
gooey/python_bindings/modules.py

@ -10,10 +10,10 @@ sys.path.append(os.path.dirname(__file__))
def load(module_source):
descriptor, tmpfilepath = tempfile.mkstemp(suffix='.py')
print descriptor, tmpfilepath
tmpfiledir = os.path.dirname(tmpfilepath)
tmpfilename = os.path.splitext(os.path.split(tmpfilepath)[-1])[0]
print tmpfilename
sys.path.append(tmpfiledir)
try:
with open(tmpfilepath, 'w') as f:

Loading…
Cancel
Save