From 8468315ada52a9c75a5e8d57a115d81bc95e7a7a Mon Sep 17 00:00:00 2001 From: monkut <5i!v3r-gh> Date: Tue, 23 May 2017 09:42:17 +0900 Subject: [PATCH 1/7] Attempt to make output formatter user-definable. --- fire/core.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fire/core.py b/fire/core.py index 3a0712e9..333c9f7c 100644 --- a/fire/core.py +++ b/fire/core.py @@ -70,7 +70,7 @@ def main(argv): import six -def Fire(component=None, command=None, name=None): +def Fire(component=None, command=None, name=None, formatter=None): """This function, Fire, is the main entrypoint for Python Fire. Executes a command either from the `command` argument or from sys.argv by @@ -153,7 +153,10 @@ def Fire(component=None, command=None, name=None): file=sys.stderr) raise FireExit(0, component_trace) else: - _PrintResult(component_trace, verbose=component_trace.verbose) + if formatter: + formatter(component_trace, verbose=component_trace.verbose) + else: + _PrintResult(component_trace, verbose=component_trace.verbose) result = component_trace.GetResult() return result From 8094595df1500f96dae43eafa169a9bb062f4007 Mon Sep 17 00:00:00 2001 From: monkut <5i!v3r-gh> Date: Tue, 23 May 2017 14:10:45 +0900 Subject: [PATCH 2/7] Changing formatter signiture, so that only the `result` object is is passed. --- fire/core.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fire/core.py b/fire/core.py index 333c9f7c..4a7a1af2 100644 --- a/fire/core.py +++ b/fire/core.py @@ -85,6 +85,7 @@ def Fire(component=None, command=None, name=None, formatter=None): supplied, then the command is taken from sys.argv instead. name: Optional. The name of the command as entered at the command line. Used in interactive mode and for generating the completion script. + formatter: Optional. Function to be called for output formatting. (result object passed to formatter) Returns: The result of executing the Fire command. Execution begins with the initial target component. The component is updated by using the command arguments @@ -153,11 +154,11 @@ def Fire(component=None, command=None, name=None, formatter=None): file=sys.stderr) raise FireExit(0, component_trace) else: + result = component_trace.GetResult() if formatter: - formatter(component_trace, verbose=component_trace.verbose) + formatter(result) else: _PrintResult(component_trace, verbose=component_trace.verbose) - result = component_trace.GetResult() return result From 659d57886506995d01aac78d4e5a8ba0ce1ae430 Mon Sep 17 00:00:00 2001 From: monkut <5i!v3r-gh> Date: Sat, 17 Jun 2017 00:22:21 +0900 Subject: [PATCH 3/7] attempt to fix issue when no args are given, and help should be displayed --- fire/core.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fire/core.py b/fire/core.py index 4a7a1af2..14b150f2 100644 --- a/fire/core.py +++ b/fire/core.py @@ -155,8 +155,10 @@ def Fire(component=None, command=None, name=None, formatter=None): raise FireExit(0, component_trace) else: result = component_trace.GetResult() - if formatter: + if formatter and args: formatter(result) + elif formatter and not args: + print(helputils.HelpString(result, component_trace, verbose)) else: _PrintResult(component_trace, verbose=component_trace.verbose) return result From d2b0dc0307be3463e8856cf5c5006bdec21e13a8 Mon Sep 17 00:00:00 2001 From: monkut <5i!v3r-gh> Date: Wed, 5 Jul 2017 22:20:57 +0900 Subject: [PATCH 4/7] fixing verbose reference --- fire/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fire/core.py b/fire/core.py index 14b150f2..b33265a0 100644 --- a/fire/core.py +++ b/fire/core.py @@ -158,7 +158,7 @@ def Fire(component=None, command=None, name=None, formatter=None): if formatter and args: formatter(result) elif formatter and not args: - print(helputils.HelpString(result, component_trace, verbose)) + print(helputils.HelpString(result, component_trace, component_trace.verbose)) else: _PrintResult(component_trace, verbose=component_trace.verbose) return result From ea3c57d131b1dc408daed6d68b97296614309dca Mon Sep 17 00:00:00 2001 From: monkut <5i!v3r-gh> Date: Wed, 5 Jul 2017 23:34:19 +0900 Subject: [PATCH 5/7] updating so that package name is used for Usage display when file being run is '__main__.py' (uses filepath of __main__.py to determine the package name) --- fire/helputils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/fire/helputils.py b/fire/helputils.py index 5c23de3a..e65e58b8 100644 --- a/fire/helputils.py +++ b/fire/helputils.py @@ -23,6 +23,7 @@ from __future__ import print_function import inspect +import os from fire import completion from fire import inspectutils @@ -113,12 +114,20 @@ def HelpString(component, trace=None, verbose=False): format_string = '{{field:{max_size}s}} {{value}}'.format(max_size=max_size) lines = [] + source_filepath = None for field in fields: value = _DisplayValue(info, field, padding=max_size + 1) + if field == 'file': + source_filepath = value # assumes that 'file' comes before usage (See 'fields' above) if value: if lines and field == 'usage': lines.append('') # Ensure a blank line before usage. + # use package_name if file called is __main__.py + if source_filepath and source_filepath.endswith('__main__.py'): + package_path, _ = os.path.split(source_filepath) + _, package_name = os.path.split(package_path) + value = value.replace('__main__.py', package_name) lines.append(format_string.format( field=_NormalizeField(field) + ':', value=value, From a2ae69d1ce9676d8582602b5273b9741c4b7a353 Mon Sep 17 00:00:00 2001 From: monkut <5i!v3r-gh> Date: Wed, 5 Jul 2017 23:44:18 +0900 Subject: [PATCH 6/7] updating to support '--' argument and help display --- fire/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fire/core.py b/fire/core.py index b33265a0..f49422d2 100644 --- a/fire/core.py +++ b/fire/core.py @@ -155,7 +155,7 @@ def Fire(component=None, command=None, name=None, formatter=None): raise FireExit(0, component_trace) else: result = component_trace.GetResult() - if formatter and args: + if formatter and args and '--' not in args: formatter(result) elif formatter and not args: print(helputils.HelpString(result, component_trace, component_trace.verbose)) From c5f36a66237e95088bf4e25b1c12995f9dba0c1a Mon Sep 17 00:00:00 2001 From: monkut <5i!v3r-gh> Date: Wed, 5 Jul 2017 23:48:58 +0900 Subject: [PATCH 7/7] updating to include display fields on usage display to ignore unnecessary field output. --- fire/helputils.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/fire/helputils.py b/fire/helputils.py index e65e58b8..143b110c 100644 --- a/fire/helputils.py +++ b/fire/helputils.py @@ -106,7 +106,15 @@ def HelpString(component, trace=None, verbose=False): 'usage', ] + display_fields = [ + 'docstring', + 'init_docstring', + 'class_docstring', + 'call_docstring', + 'length', + 'usage', + ] max_size = max( len(_NormalizeField(field)) + 1 for field in fields @@ -119,7 +127,7 @@ def HelpString(component, trace=None, verbose=False): value = _DisplayValue(info, field, padding=max_size + 1) if field == 'file': source_filepath = value # assumes that 'file' comes before usage (See 'fields' above) - if value: + if value and field in display_fields: if lines and field == 'usage': lines.append('') # Ensure a blank line before usage.