mirror of https://github.com/chriskiehl/Gooey.git
Browse Source
Fixed bug in parser that was preventing different argparse import styles from being recognized
Fixed bug in parser that was preventing different argparse import styles from being recognized
added ast_inspector.py - utility for inspecting the properties of ast objects moved additional code formatting tasks into code_prep.py added code_prep_unittest.py Updated monkey_parser.py to use new parser fixespull/34/head
8 changed files with 319 additions and 116 deletions
Split View
Diff Options
-
63gooey/code_prep.py
-
57gooey/code_prep_unittest.py
-
51gooey/dev_utils/ast_inspector.py
-
2gooey/mockapplications/example_argparse_souce_in_main.py
-
52gooey/mockapplications/mockapp_import_argparse.py
-
25gooey/monkey_parser.py
-
140gooey/source_parser.py
-
45gooey/source_parser_unittest.py
@ -1 +1,64 @@ |
|||
__author__ = 'Chris' |
|||
|
|||
""" |
|||
Preps the extracted Python code so that it can be evaled by the |
|||
monkey_parser |
|||
""" |
|||
|
|||
from itertools import * |
|||
|
|||
source = ''' |
|||
import sys |
|||
import os |
|||
import doctest |
|||
import cProfile |
|||
import pstats |
|||
from argparse import ArgumentParser |
|||
from argparse import RawDescriptionHelpFormatter |
|||
from gooey import Gooey |
|||
parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter) |
|||
parser.add_argument('filename', help='filename') |
|||
parser.add_argument('-r', '--recursive', dest='recurse', action='store_true', help='recurse into subfolders [default: %(default)s]') |
|||
parser.add_argument('-v', '--verbose', dest='verbose', action='count', help='set verbosity level [default: %(default)s]') |
|||
parser.add_argument('-i', '--include', action='append', help='only include paths matching this regex pattern. Note: exclude is given preference over include. [default: %(default)s]', metavar='RE') |
|||
parser.add_argument('-m', '--mycoolargument', help='mycoolargument') |
|||
parser.add_argument('-e', '--exclude', dest='exclude', help='exclude paths matching this regex pattern. [default: %(default)s]', metavar='RE') |
|||
parser.add_argument('-V', '--version', action='version') |
|||
parser.add_argument('-T', '--tester', choices=['yes', 'no']) |
|||
parser.add_argument(dest='paths', help='paths to folder(s) with source file(s) [default: %(default)s]', metavar='path', nargs='+') |
|||
''' |
|||
|
|||
def take_imports(code): |
|||
return takewhile(lambda line: 'import' in line, code) |
|||
|
|||
def drop_imports(code): |
|||
return dropwhile(lambda line: 'import' in line, code) |
|||
|
|||
def split_line(line): |
|||
# splits an assignment statement into varname and command strings |
|||
# in: "parser = ArgumentParser(description='Example Argparse Program')" |
|||
# out: "parser", "= parser = ArgumentParser(description='Example Argparse Program" |
|||
# take/dropwhile used to avoid splitting on multiple '=' signs |
|||
not_equal_sign = lambda x: x != '=' |
|||
varname = ''.join(takewhile(not_equal_sign, line)).strip() |
|||
command = ''.join(dropwhile(not_equal_sign, line))[2:] |
|||
return varname, command |
|||
|
|||
def update_parser_varname(new_varname, code): |
|||
# lines = source.split('\n')[1:] |
|||
lines = filter(lambda x: x != '', code) |
|||
|
|||
argparse_code = dropwhile(lambda line: 'import' in line, lines) |
|||
old_argparser_varname, _ = split_line(argparse_code.next()) |
|||
|
|||
updated_code = [line.replace(old_argparser_varname, new_varname) |
|||
for line in lines] |
|||
return updated_code |
|||
|
|||
if __name__ == '__main__': |
|||
pass |
|||
|
|||
|
|||
|
|||
|
|||
|
@ -0,0 +1,57 @@ |
|||
__author__ = 'Chris' |
|||
|
|||
import unittest |
|||
import code_prep |
|||
|
|||
class TestCodePrep(unittest.TestCase): |
|||
|
|||
def test_split_line(self): |
|||
line = "parser = ArgumentParser(description='Example Argparse Program')" |
|||
self.assertEqual("parser", code_prep.split_line(line)[0]) |
|||
self.assertEqual("= ArgumentParser(description='Example Argparse Program')", code_prep.split_line(line)[1]) |
|||
|
|||
def test_update_parser_varname_assigns_new_name_to_parser_var(self): |
|||
line = ["parser = ArgumentParser(description='Example Argparse Program')"] |
|||
self.assertEqual( |
|||
"jarser = ArgumentParser(description='Example Argparse Program')", |
|||
code_prep.update_parser_varname('jarser', line)[0] |
|||
) |
|||
|
|||
def test_update_parser_varname_assigns_new_name_to_parser_var__multiline(self): |
|||
lines = ''' |
|||
import argparse |
|||
from argparse import ArgumentParser |
|||
parser = ArgumentParser(description='Example Argparse Program') |
|||
parser.parse_args() |
|||
'''.split('\n') |
|||
|
|||
self.assertEqual( |
|||
"jarser = ArgumentParser(description='Example Argparse Program')", |
|||
code_prep.update_parser_varname('jarser', lines)[2] |
|||
) |
|||
|
|||
def test_take_imports_drops_all_non_imports_statements(self): |
|||
lines = ''' |
|||
import argparse |
|||
from argparse import ArgumentParser |
|||
parser = ArgumentParser(description='Example Argparse Program') |
|||
parser.parse_args() |
|||
'''.split('\n')[1:] |
|||
|
|||
self.assertEqual(2, len(list(code_prep.take_imports(lines)))) |
|||
self.assertEqual('import argparse', list(code_prep.take_imports(lines))[0]) |
|||
|
|||
def test_drop_imports_excludes_all_imports_statements(self): |
|||
lines = ''' |
|||
import argparse |
|||
from argparse import ArgumentParser |
|||
parser = ArgumentParser(description='Example Argparse Program') |
|||
parser.parse_args() |
|||
'''.split('\n')[1:] |
|||
|
|||
self.assertEqual(2, len(list(code_prep.take_imports(lines)))) |
|||
self.assertEqual('parser.parse_args()', list(code_prep.drop_imports(lines))[1]) |
|||
|
|||
if __name__ == "__main__": |
|||
#import sys;sys.argv = ['', 'Test.testName'] |
|||
unittest.main() |
@ -1 +1,52 @@ |
|||
from gooey import source_parser |
|||
|
|||
__author__ = 'Chris' |
|||
|
|||
""" |
|||
Pretty Printing util for inspecting the various ast objects |
|||
""" |
|||
|
|||
import ast |
|||
from _ast import Assign, Call |
|||
|
|||
def pretty_print(node, indent): |
|||
d = node.__dict__ |
|||
for k, v in d.iteritems(): |
|||
if isinstance(v, list): |
|||
print '-' * indent, k, ": " |
|||
for i in v: |
|||
pretty_print(i, indent + 2) |
|||
elif 'ast' in str(type(v)): |
|||
pretty_print(v, indent + 2) |
|||
else: |
|||
print '-' * indent, k, ": ", v |
|||
|
|||
|
|||
if __name__ == '__main__': |
|||
lines = ''' |
|||
def main(): |
|||
x = 1 |
|||
y = 2 |
|||
foo, doo = ("poo", "poo") |
|||
smarser = argparse.ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter) |
|||
random_junk = 123412353454356 |
|||
smarser.add_argument("filename", help="Name of the file you want to read") # positional' |
|||
smarser.add_argument("outfile", help="Name of the file where you'll save the output") # positional |
|||
bar = x + y |
|||
baz = random_junk * 5 |
|||
''' |
|||
|
|||
lines2 = ''' |
|||
def main(): |
|||
try: |
|||
foo, doo = ("poo", "poo") |
|||
smarser = argparse.ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter) |
|||
smarser.add_argument("filename", help="Name of the file you want to read") # positional' |
|||
smarser.add_argument("outfile", help="Name of the file where you'll save the output") # positional |
|||
smarser.parse_args() |
|||
except: |
|||
pass |
|||
''' |
|||
nodes = ast.parse(open(r'C:\Users\Chris\Dropbox\pretty_gui\Gooey\gooey\mockapplications\mockapp_import_argparse.py').read()) |
|||
|
|||
pretty_print(nodes, 1) |
@ -1 +1,51 @@ |
|||
__author__ = 'Chris' |
|||
''' |
|||
Created on Dec 21, 2013 |
|||
|
|||
@author: Chris |
|||
''' |
|||
import sys |
|||
import hashlib |
|||
from time import time as _time |
|||
from time import sleep as _sleep |
|||
import argparse |
|||
|
|||
from gooey import Gooey |
|||
|
|||
|
|||
@Gooey |
|||
def main(): |
|||
my_cool_parser = argparse.ArgumentParser(description="Mock application to test Gooey's functionality") |
|||
my_cool_parser.add_argument("filename", help="Name of the file you want to read") # positional |
|||
my_cool_parser.add_argument("outfile", help="Name of the file where you'll save the output") # positional |
|||
my_cool_parser.add_argument('-c', '--countdown', default=10, type=int, help='sets the time to count down from') |
|||
my_cool_parser.add_argument("-s", "--showtime", action="store_true", help="display the countdown timer") |
|||
my_cool_parser.add_argument("-d", "--delay", action="store_true", help="Delay execution for a bit") |
|||
my_cool_parser.add_argument('--verbose', '-v', action='count') |
|||
my_cool_parser.add_argument("-o", "--obfuscate", action="store_true", help="obfuscate the countdown timer!") |
|||
my_cool_parser.add_argument('-r', '--recursive', choices=['yes', 'no'], help='Recurse into subfolders') |
|||
my_cool_parser.add_argument("-w", "--writelog", default="No, NOT whatevs", help="write log to some file or something") |
|||
my_cool_parser.add_argument("-e", "--expandAll", action="store_true", help="expand all processes") |
|||
|
|||
print 'inside of main(), my_cool_parser =', my_cool_parser |
|||
args = my_cool_parser.parse_args() |
|||
|
|||
print sys.argv |
|||
print args.countdown |
|||
print args.showtime |
|||
|
|||
start_time = _time() |
|||
print 'Counting down from %s' % args.countdown |
|||
while _time() - start_time < args.countdown: |
|||
if args.showtime: |
|||
print 'printing message at: %s' % _time() |
|||
else: |
|||
print 'printing message at: %s' % hashlib.md5(str(_time())).hexdigest() |
|||
_sleep(.5) |
|||
print 'Finished running the program. Byeeeeesss!' |
|||
|
|||
# raise ValueError("Something has gone wrong! AHHHHHHHHHHH") |
|||
|
|||
if __name__ == '__main__': |
|||
# sys.argv.extend('asdf -c 5 -s'.split()) |
|||
# print sys.argv |
|||
main() |
Write
Preview
Loading…
Cancel
Save