From 7154510adc124f0d08a33f766a107977e9be9eb4 Mon Sep 17 00:00:00 2001 From: Neal McBurnett Date: Wed, 5 Apr 2017 12:24:27 -0600 Subject: [PATCH] fire.py: Use fire on arbitrary file or module. (#35) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fire.py: Use fire on arbitrary file or module. Based on the discussion at [Use fire without editing code directly · Issue #29 · google/python-fire](https://github.com/google/python-fire/issues/29) Of course, still needs better error handling, documentation, installation support, etc. * rename fire.py to fire_script.py in repo Should fix the build problem for python 2. Later, get it installed as "fire" via setup.py * rename fire_script.py __main__.py Following advice from Python Apps the Right Way: entry points and scripts | Chris Warrick https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/ --- fire/__main__.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 fire/__main__.py diff --git a/fire/__main__.py b/fire/__main__.py new file mode 100755 index 00000000..512f2b97 --- /dev/null +++ b/fire/__main__.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +"""Run fire on the given module or file + +Usage: fire [-m module] [-f file] [fire-style arguments to desired python object] + +Example: + +$ fire -m random Random gauss 3 4 +5.056813212816271 + +Documentation on fire: + https://github.com/google/python-fire/blob/master/doc/guide.md + +Discussion: https://github.com/google/python-fire/issues/29 +""" + +import fire +import imp +import sys + +if __name__ == '__main__': + if sys.argv[1] == '-m': + (fileobj, pathname, description) = imp.find_module(sys.argv[2]) + module = imp.load_module('module', fileobj, pathname, description) + sys.argv = sys.argv[2:] + elif sys.argv[1] == '-f': + module = imp.load_source('module', sys.argv[2]) + sys.argv = sys.argv[2:] + else: + print("Usage: fire [-m module] [-f file] [fire-style arguments to desired python object]") + sys.exit(1) + + from module import * + + fire.Fire()