Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conquer the land of arguing snakes #788

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions gooey/__main__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# '''
# Delegates arguments to the main Gooey runner
#
# For use when run directly from command line with the -m (module) flag:
#
# e.g. $ python -m gooey
#
# '''
#
# from gooey import application
#
# application.main()
'''
Delegates arguments to the main Gooey runner.

For use when run directly from command line with the -m (module) flag:

e.g. $ python -m gooey

'''

from gooey.cli import main

if __name__ == '__main__':
main()
41 changes: 41 additions & 0 deletions gooey/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'''The command line interface.'''

import sys
from importlib import import_module
try:
from importlib.metadata import entry_points
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain a bit about these different versions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

importlib.metadata is included in the standard library since Python 3.8 only.
Since you support lower versions, the importlib-metadata package is required (it's a simple backport).
Therefore, we must handle both cases in the code since the import path is not the same (importlib.metadata vs. importlib_metadata).

except ImportError:
from importlib_metadata import entry_points

from gooey import Gooey


def main(args=None):
args = args or sys.argv[1:]
if not args:
# without args we assume we want to run gooey on gooey itself
# TODO: rewrite using argparse
Copy link
Author

@pawamoy pawamoy Feb 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running Gooey on itself requires rewriting this function with an argparse parser. Let me know if this is something you'd like 🙂

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave it up to you ^_^

I'd be fine going sans additional configuration for the initial release of this -- let it "bake" a bit and see if people need the functionality. On the flip side, if you want to throw a parser in there so people can control things that's fine, too. You may be able to even generate a options for all of the params directly from the type definition by looking through its __annotations__ (in theory).

script_name = "gooey"
else:
script_name = args[0]
scripts = {script.name: script for script in entry_points().get('console_scripts')}
if script_name in scripts:
# a valid script was passed
script = scripts[script_name]
module_path = script.module
function_name = script.attr
prog = script_name
elif ':' in script_name:
# the path to a function was passed
module_path, function_name = args[0].split(':')
prog = module_path.split('.', 1)[0]
pawamoy marked this conversation as resolved.
Show resolved Hide resolved
else:
print('usage: gooey [SCRIPT | MODULE:FUNCTION] [-- SCRIPT_ARGS...]', file=sys.stderr)
sys.exit(1)
if len(args) > 1:
if args[1] == '--':
del args[1]
sys.argv = [prog, *args[1:]]
module = import_module(module_path)
function = getattr(module, function_name)
return Gooey(function, use_cmd_args=True)()
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
're-wx>=0.0.9',
'typing-extensions==3.10.0.2',
'wxpython>=4.1.0',
'dataclasses>=0.8; python_version < "3.7"'
'dataclasses>=0.8; python_version < "3.7"',
'importlib-metadata; python_version < "3.8"',
]

setup(
Expand All @@ -33,6 +34,7 @@
install_requires=deps,
include_package_data=True,
dependency_links = ["http://www.wxpython.org/download.php"],
entry_points={'console_scripts': ['gooey = gooey.cli:main']},
classifiers = [
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
Expand Down