Skip to content

Commit

Permalink
create eg_exec to allow invoking without pip
Browse files Browse the repository at this point in the history
People installing with pip will have eg be global and available in
all of their packages. This is easy, but some people might not like
it. So, people can now run eg by running eg_exec.py.
  • Loading branch information
srsudar committed Apr 14, 2015
1 parent 839f744 commit 9ba821d
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 109 deletions.
110 changes: 2 additions & 108 deletions bin/eg
Original file line number Diff line number Diff line change
@@ -1,113 +1,7 @@
#!/usr/bin/python
import argparse
import sys

from eg import eg_config
from eg import eg_util
from eg import eg_exec


if __name__ == '__main__':

parser = argparse.ArgumentParser(
description='eg provides examples of common command usage.'
)

parser.add_argument(
'-v',
'--version',
action='store_true',
help='Display version information about eg'
)

parser.add_argument(
'--config-file',
help='Path to the .egrc file, if it is not in the default location.'
)

parser.add_argument(
'--examples-dir',
help='The location to the examples/ dir that ships with eg'
)

parser.add_argument(
'--custom-dir',
help='Path to a directory containing user-defined examples.'
)

parser.add_argument(
'--list',
action='store_true',
help='Show all the programs with eg entries.'
)

parser.add_argument(
'--color',
action='store_true',
dest='use_color',
default=None,
help='Colorize output.'
)

parser.add_argument(
'--no-color',
action='store_false',
dest='use_color',
help='Do not colorize output.'
)

parser.add_argument(
'program',
nargs='?',
help='The program for which to display examples.'
)

args = parser.parse_args()

if len(sys.argv) < 2:
parser.print_help()
elif not args.version and not args.list and not args.program:
print 'you must specify a program or pass the --list or --version flags'
else:
config = eg_config.get_resolved_config_items(
egrc_path=args.config_file,
examples_dir=args.examples_dir,
custom_dir=args.custom_dir,
use_color=args.use_color
)

if args.list:
# Show what's available.
supported_programs = eg_util.get_list_of_all_supported_commands(
config
)
msg_line_1 = 'Legend: '
msg_line_2 = (' ' +
eg_util.FLAG_ONLY_CUSTOM +
' only custom files'
)
msg_line_3 = (' ' +
eg_util.FLAG_CUSTOM_AND_DEFAULT +
' custom and default files'
)
msg_line_4 = ' ' + ' only default files (no symbol)'
msg_line_5 = ''
msg_line_6 = 'Programs supported by eg: '

preamble = [
msg_line_1,
msg_line_2,
msg_line_3,
msg_line_4,
msg_line_5,
msg_line_6
]

for line in preamble:
print line

for program in supported_programs:
print program
elif args.version:
print eg_util.VERSION
else:
eg_util.handle_program(args.program, config)
eg_exec.run_eg()
117 changes: 117 additions & 0 deletions eg/eg_exec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import argparse
import eg_config
import eg_util
import sys


def run_eg():

parser = argparse.ArgumentParser(
description='eg provides examples of common command usage.'
)

parser.add_argument(
'-v',
'--version',
action='store_true',
help='Display version information about eg'
)

parser.add_argument(
'--config-file',
help='Path to the .egrc file, if it is not in the default location.'
)

parser.add_argument(
'--examples-dir',
help='The location to the examples/ dir that ships with eg'
)

parser.add_argument(
'--custom-dir',
help='Path to a directory containing user-defined examples.'
)

parser.add_argument(
'--list',
action='store_true',
help='Show all the programs with eg entries.'
)

parser.add_argument(
'--color',
action='store_true',
dest='use_color',
default=None,
help='Colorize output.'
)

parser.add_argument(
'--no-color',
action='store_false',
dest='use_color',
help='Do not colorize output.'
)

parser.add_argument(
'program',
nargs='?',
help='The program for which to display examples.'
)

args = parser.parse_args()

if len(sys.argv) < 2:
parser.print_help()
elif not args.version and not args.list and not args.program:
print 'you must specify a program or pass the --list or --version flags'
else:
config = eg_config.get_resolved_config_items(
egrc_path=args.config_file,
examples_dir=args.examples_dir,
custom_dir=args.custom_dir,
use_color=args.use_color
)

if args.list:
# Show what's available.
supported_programs = eg_util.get_list_of_all_supported_commands(
config
)
msg_line_1 = 'Legend: '
msg_line_2 = (' ' +
eg_util.FLAG_ONLY_CUSTOM +
' only custom files'
)
msg_line_3 = (' ' +
eg_util.FLAG_CUSTOM_AND_DEFAULT +
' custom and default files'
)
msg_line_4 = ' ' + ' only default files (no symbol)'
msg_line_5 = ''
msg_line_6 = 'Programs supported by eg: '

preamble = [
msg_line_1,
msg_line_2,
msg_line_3,
msg_line_4,
msg_line_5,
msg_line_6
]

for line in preamble:
print line

for program in supported_programs:
print program
elif args.version:
print eg_util.VERSION
else:
eg_util.handle_program(args.program, config)


# We want people to be able to use eg without pip, by so we'll allow this to be
# invoked directly.
if __name__ == '__main__':
run_eg()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
'install_requires': ['colorama'],
'test_requires': ['nose', 'mock'],
'packages': ['eg'],
'scripts': ['bin/egg'],
'scripts': ['bin/eg'],
'package_data': {
'eg': ['examples/*']
},
Expand Down

0 comments on commit 9ba821d

Please sign in to comment.