diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index 8be46672..00000000 --- a/.github/dependabot.yml +++ /dev/null @@ -1,31 +0,0 @@ -# Basic dependabot.yml file with minimum configuration for two package managers - -version: 2 -updates: - # Enable version updates for python - - package-ecosystem: "pip" - directory: "/" - schedule: - interval: "monthly" - labels: ["dependabot"] - pull-request-branch-name: - separator: "-" - open-pull-requests-limit: 5 - reviewers: - - "dbieber" - - # Enable version updates for GitHub Actions - - package-ecosystem: "github-actions" - directory: "/" - schedule: - interval: "monthly" - groups: - gh-actions: - patterns: - - "*" # Check all dependencies - labels: ["dependabot"] - pull-request-branch-name: - separator: "-" - open-pull-requests-limit: 5 - reviewers: - - "dbieber" diff --git a/.github/scripts/build.sh b/.github/scripts/build.sh deleted file mode 100755 index d9207dfe..00000000 --- a/.github/scripts/build.sh +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -#!/usr/bin/env bash - -# Exit when any command fails. -set -e - -PYTHON_VERSION=${PYTHON_VERSION:-3.7} - -pip install -e .[test] -python -m pytest # Run the tests without IPython. -pip install ipython -python -m pytest # Now run the tests with IPython. -pylint fire --ignore=test_components_py3.py,parser_fuzz_test.py,console -if [[ ${PYTHON_VERSION} == 3.12 ]]; then - # Run type-checking - pip install ty - python -m ty check --python $(which python) --exclude fire/test_components_py3.py --exclude fire/console/ --exclude fire/formatting_windows.py -fi diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 6b9d1eae..00000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,38 +0,0 @@ -name: Python Fire - -on: - push: - branches: ["master"] - pull_request: - branches: ["master"] - -defaults: - run: - shell: bash - -jobs: - build: - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: ["macos-latest", "ubuntu-latest"] - python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13", "3.14.0-rc.2"] - include: - - {os: "ubuntu-22.04", python-version: "3.7"} - - steps: - # Checkout the repo. - - name: Checkout Python Fire repository - uses: actions/checkout@v4 - - # Set up Python environment. - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v5 - with: - python-version: ${{ matrix.python-version }} - - # Build Python Fire using the build.sh script. - - name: Run build script - run: ./.github/scripts/build.sh - env: - PYTHON_VERSION: ${{ matrix.python-version }} diff --git a/.gitignore b/.gitignore deleted file mode 100644 index a2166684..00000000 --- a/.gitignore +++ /dev/null @@ -1,103 +0,0 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class - -# C extensions -*.so - -# Distribution / packaging -.Python -env/ -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -*.egg-info/ -.installed.cfg -*.egg - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.coverage -.coverage.* -.cache -nosetests.xml -coverage.xml -*,cover -.hypothesis/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# pyenv -.python-version - -# celery beat schedule file -celerybeat-schedule - -# SageMath parsed files -*.sage.py - -# dotenv -.env - -# virtualenv -.venv -venv/ -ENV/ - -# Spyder project settings -.spyderproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# PyCharm IDE -.idea/ - -# Type-checking -.pytype/ diff --git a/examples/__init__.py b/.nojekyll similarity index 100% rename from examples/__init__.py rename to .nojekyll diff --git a/404.html b/404.html new file mode 100644 index 00000000..fbece880 --- /dev/null +++ b/404.html @@ -0,0 +1,129 @@ + + + + + + + + Python Fire + + + + + + + + + + + +
+ + +
+ +
+
+
    +
  • +
  • +
  • +
+
+
+
+
+ + +

404

+ +

Page not found

+ + +
+
+ +
+
+ +
+ +
+ +
+ + + + + +
+ + + + + + + + + diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index b5d67c96..00000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,59 +0,0 @@ -# How to contribute - -We'd love to accept your patches and contributions to this project. There are -just a few small guidelines you need to follow. - -First, read these guidelines. -Before you begin making changes, state your intent to do so in an Issue. -Then, fork the project. Make changes in your copy of the repository. -Then open a pull request once your changes are ready. -If this is your first contribution, sign the Contributor License Agreement. -A discussion about your change will follow, and if accepted your contribution -will be incorporated into the Python Fire codebase. - -## Contributor License Agreement - -Contributions to this project must be accompanied by a Contributor License -Agreement. You (or your employer) retain the copyright to your contribution, -this simply gives us permission to use and redistribute your contributions as -part of the project. Head over to to see -your current agreements on file or to sign a new one. - -You generally only need to submit a CLA once, so if you've already submitted one -(even if it was for a different project), you probably don't need to do it -again. - -## Code reviews - -All submissions, including submissions by project members, require review. -For changes introduced by non-Googlers, we use GitHub pull requests for this -purpose. Consult [GitHub Help] for more information on using pull requests. - -[GitHub Help]: https://help.github.com/articles/about-pull-requests/ - -## Code style - -In general, Python Fire follows the guidelines in the -[Google Python Style Guide]. - -In addition, the project follows a convention of: -- Maximum line length: 80 characters -- Indentation: 2 spaces (4 for line continuation) -- PascalCase for function and method names. -- Single quotes around strings, three double quotes around docstrings. - -[Google Python Style Guide]: http://google.github.io/styleguide/pyguide.html - -## Testing - -Python Fire uses [GitHub Actions](https://github.com/google/python-fire/actions) to run tests on each pull request. You can run -these tests yourself as well. To do this, first install the test dependencies -listed in setup.py (e.g. pytest, mock, termcolor, and hypothesis). -Then run the tests by running `pytest` in the root directory of the repository. - -## Linting - -Please run lint on your pull requests to make accepting the requests easier. -To do this, run `pylint fire` in the root directory of the repository. -Note that even if lint is passing, additional style changes to your submission -may be made during merging. diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 035adf95..00000000 --- a/LICENSE +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2017 Google Inc. All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/README.md b/README.md deleted file mode 100644 index 1482d56d..00000000 --- a/README.md +++ /dev/null @@ -1,118 +0,0 @@ -# Python Fire [![PyPI](https://img.shields.io/pypi/pyversions/fire.svg?style=plastic)](https://github.com/google/python-fire) - -_Python Fire is a library for automatically generating command line interfaces -(CLIs) from absolutely any Python object._ - -- Python Fire is a simple way to create a CLI in Python. - [[1]](docs/benefits.md#simple-cli) -- Python Fire is a helpful tool for developing and debugging Python code. - [[2]](docs/benefits.md#debugging) -- Python Fire helps with exploring existing code or turning other people's - code into a CLI. [[3]](docs/benefits.md#exploring) -- Python Fire makes transitioning between Bash and Python easier. - [[4]](docs/benefits.md#bash) -- Python Fire makes using a Python REPL easier by setting up the REPL with the - modules and variables you'll need already imported and created. - [[5]](docs/benefits.md#repl) - -## Installation - -To install Python Fire with pip, run: `pip install fire` - -To install Python Fire with conda, run: `conda install fire -c conda-forge` - -To install Python Fire from source, first clone the repository and then run: -`python setup.py install` - -## Basic Usage - -You can call `Fire` on any Python object:
-functions, classes, modules, objects, dictionaries, lists, tuples, etc. -They all work! - -Here's an example of calling Fire on a function. - -```python -import fire - -def hello(name="World"): - return "Hello %s!" % name - -if __name__ == '__main__': - fire.Fire(hello) -``` - -Then, from the command line, you can run: - -```bash -python hello.py # Hello World! -python hello.py --name=David # Hello David! -python hello.py --help # Shows usage information. -``` - -Here's an example of calling Fire on a class. - -```python -import fire - -class Calculator(object): - """A simple calculator class.""" - - def double(self, number): - return 2 * number - -if __name__ == '__main__': - fire.Fire(Calculator) -``` - -Then, from the command line, you can run: - -```bash -python calculator.py double 10 # 20 -python calculator.py double --number=15 # 30 -``` - -To learn how Fire behaves on functions, objects, dicts, lists, etc, and to learn -about Fire's other features, see the [Using a Fire CLI page](docs/using-cli.md). - -For additional examples, see [The Python Fire Guide](docs/guide.md). - -## Why is it called Fire? - -When you call `Fire`, it fires off (executes) your command. - -## Where can I learn more? - -Please see [The Python Fire Guide](docs/guide.md). - -## Reference - -| Setup | Command | Notes -| :------ | :------------------ | :--------- -| install | `pip install fire` | - -| Creating a CLI | Command | Notes -| :--------------| :--------------------- | :--------- -| import | `import fire` | -| Call | `fire.Fire()` | Turns the current module into a Fire CLI. -| Call | `fire.Fire(component)` | Turns `component` into a Fire CLI. - -| Using a CLI | Command | Notes -| :---------------------------------------------- | :-------------------------------------- | :---- -| [Help](docs/using-cli.md#help-flag) | `command --help` or `command -- --help` | -| [REPL](docs/using-cli.md#interactive-flag) | `command -- --interactive` | Enters interactive mode. -| [Separator](docs/using-cli.md#separator-flag) | `command -- --separator=X` | Sets the separator to `X`. The default separator is `-`. -| [Completion](docs/using-cli.md#completion-flag) | `command -- --completion [shell]` | Generates a completion script for the CLI. -| [Trace](docs/using-cli.md#trace-flag) | `command -- --trace` | Gets a Fire trace for the command. -| [Verbose](docs/using-cli.md#verbose-flag) | `command -- --verbose` | - -_Note that these flags are separated from the Fire command by an isolated `--`._ - -## License - -Licensed under the -[Apache 2.0](https://github.com/google/python-fire/blob/master/LICENSE) License. - -## Disclaimer - -This is not an official Google product. diff --git a/api/index.html b/api/index.html new file mode 100644 index 00000000..1d278d6b --- /dev/null +++ b/api/index.html @@ -0,0 +1,265 @@ + + + + + + + + Reference - Python Fire + + + + + + + + + + + + + +
+ + +
+ +
+
+
    +
  • + +
  • +
  • +
+
+
+
+
+ +

Python Fire Quick Reference

+ + + + + + + + + + + + + + + +
SetupCommandNotes
installpip install fireInstalls fire from pypi
+ + + + + + + + + + + + + + + + + + + + + + + + + +
Creating a CLICommandNotes
importimport fire
Callfire.Fire()Turns the current module into a Fire CLI.
Callfire.Fire(component)Turns component into a Fire CLI.
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Using a CLICommandNotes
Helpcommand --helpShow the help screen.
REPLcommand -- --interactiveEnters interactive mode.
Separatorcommand -- --separator=XThis sets the separator to X. The default separator is -.
Completioncommand -- --completion [shell]Generate a completion script for the CLI.
Tracecommand -- --traceGets a Fire trace for the command.
Verbosecommand -- --verbose
+

Note that flags are separated from the Fire command by an isolated -- arg. +Help is an exception; the isolated -- is optional for getting help.

+

Arguments for Calling fire.Fire()

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ArgumentUsageNotes
componentfire.Fire(component)If omitted, defaults to a dict of all locals and globals.
commandfire.Fire(command='hello --name=5')Either a string or a list of arguments. If a string is provided, it is split to determine the arguments. If a list or tuple is provided, they are the arguments. If command is omitted, then sys.argv[1:] (the arguments from the command line) are used by default.
namefire.Fire(name='tool')The name of the CLI, ideally the name users will enter to run the CLI. This name will be used in the CLI's help screens. If the argument is omitted, it will be inferred automatically.
serializefire.Fire(serialize=custom_serializer)If omitted, simple types are serialized via their builtin str method, and any objects that define a custom __str__ method are serialized with that. If specified, all objects are serialized to text via the provided method.
+

Using a Fire CLI without modifying any code

+

You can use Python Fire on a module without modifying the code of the module. +The syntax for this is:

+

python -m fire <module> <arguments>

+

or

+

python -m fire <filepath> <arguments>

+

For example, python -m fire calendar -h will treat the built in calendar +module as a CLI and provide its help.

+ +
+
+ +
+
+ +
+ +
+ +
+ + + + « Previous + + + +
+ + + + + + + + + diff --git a/benefits/index.html b/benefits/index.html new file mode 100644 index 00000000..198a8492 --- /dev/null +++ b/benefits/index.html @@ -0,0 +1,199 @@ + + + + + + + + Benefits - Python Fire + + + + + + + + + + + + + +
+ + +
+ +
+
+
    +
  • + +
  • +
  • +
+
+
+
+
+ +

Benefits of Python Fire

+

+

Create CLIs in Python

+

It's dead simple. Simply write the functionality you want exposed at the command +line as a function / module / class, and then call Fire. With this addition of a +single-line call to Fire, your CLI is ready to go.

+

+

Develop and debug Python code

+

When you're writing a Python library, you probably want to try it out as you go. +You could write a main method to check the functionality you're interested in, +but then you have to change the main method with every new experiment you're +interested in testing, and constantly updating the main method is a hassle. +You could also open an IPython REPL and import your library there and test it, +but then you have to deal with reloading your imports every time you change +something.

+

If you simply call Fire in your library, then you can run all of it's +functionality from the command line without having to keep making changes to +a main method. And if you use the --interactive flag to enter an IPython REPL +then you don't need to load the imports or create your variables; they'll +already be ready for use as soon as you start the REPL.

+

+

Explore existing code; turn other people's code into a CLI

+

You can take an existing module, maybe even one that you don't have access to +the source code for, and call Fire on it. This lets you easily see what +functionality this code exposes, without you having to read through all the +code.

+

This technique can be a very simple way to create very powerful CLIs. Call +Fire on the difflib library and you get a powerful diffing tool. Call Fire +on the Python Imaging Library (PIL) module and you get a powerful image +manipulation command line tool, very similar in nature to ImageMagick.

+

The auto-generated help strings that Fire provides when you run a Fire CLI +allow you to see all the functionality these modules provide in a concise +manner.

+

+

Transition between Bash and Python

+

Using Fire lets you call Python directly from Bash. So you can mix your Python +functions with the unix tools you know and love, like grep, xargs, wc, +etc.

+

Additionally since writing CLIs in Python requires only a single call to Fire, +it is now easy to write even one-off scripts that would previously have been in +Bash, in Python.

+

+

Explore code in a Python REPL

+

When you use the --interactive flag to enter an IPython REPL, it starts with +variables and modules already defined for you. You don't need to waste time +importing the modules you care about or defining the variables you're going to +use, since Fire has already done so for you.

+ +
+
+ +
+
+ +
+ +
+ +
+ + + + « Previous + + + Next » + + +
+ + + + + + + + + diff --git a/css/fonts/Roboto-Slab-Bold.woff b/css/fonts/Roboto-Slab-Bold.woff new file mode 100644 index 00000000..6cb60000 Binary files /dev/null and b/css/fonts/Roboto-Slab-Bold.woff differ diff --git a/css/fonts/Roboto-Slab-Bold.woff2 b/css/fonts/Roboto-Slab-Bold.woff2 new file mode 100644 index 00000000..7059e231 Binary files /dev/null and b/css/fonts/Roboto-Slab-Bold.woff2 differ diff --git a/css/fonts/Roboto-Slab-Regular.woff b/css/fonts/Roboto-Slab-Regular.woff new file mode 100644 index 00000000..f815f63f Binary files /dev/null and b/css/fonts/Roboto-Slab-Regular.woff differ diff --git a/css/fonts/Roboto-Slab-Regular.woff2 b/css/fonts/Roboto-Slab-Regular.woff2 new file mode 100644 index 00000000..f2c76e5b Binary files /dev/null and b/css/fonts/Roboto-Slab-Regular.woff2 differ diff --git a/css/fonts/fontawesome-webfont.eot b/css/fonts/fontawesome-webfont.eot new file mode 100644 index 00000000..e9f60ca9 Binary files /dev/null and b/css/fonts/fontawesome-webfont.eot differ diff --git a/css/fonts/fontawesome-webfont.svg b/css/fonts/fontawesome-webfont.svg new file mode 100644 index 00000000..855c845e --- /dev/null +++ b/css/fonts/fontawesome-webfont.svg @@ -0,0 +1,2671 @@ + + + + +Created by FontForge 20120731 at Mon Oct 24 17:37:40 2016 + By ,,, +Copyright Dave Gandy 2016. All rights reserved. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/css/fonts/fontawesome-webfont.ttf b/css/fonts/fontawesome-webfont.ttf new file mode 100644 index 00000000..35acda2f Binary files /dev/null and b/css/fonts/fontawesome-webfont.ttf differ diff --git a/css/fonts/fontawesome-webfont.woff b/css/fonts/fontawesome-webfont.woff new file mode 100644 index 00000000..400014a4 Binary files /dev/null and b/css/fonts/fontawesome-webfont.woff differ diff --git a/css/fonts/fontawesome-webfont.woff2 b/css/fonts/fontawesome-webfont.woff2 new file mode 100644 index 00000000..4d13fc60 Binary files /dev/null and b/css/fonts/fontawesome-webfont.woff2 differ diff --git a/css/fonts/lato-bold-italic.woff b/css/fonts/lato-bold-italic.woff new file mode 100644 index 00000000..88ad05b9 Binary files /dev/null and b/css/fonts/lato-bold-italic.woff differ diff --git a/css/fonts/lato-bold-italic.woff2 b/css/fonts/lato-bold-italic.woff2 new file mode 100644 index 00000000..c4e3d804 Binary files /dev/null and b/css/fonts/lato-bold-italic.woff2 differ diff --git a/css/fonts/lato-bold.woff b/css/fonts/lato-bold.woff new file mode 100644 index 00000000..c6dff51f Binary files /dev/null and b/css/fonts/lato-bold.woff differ diff --git a/css/fonts/lato-bold.woff2 b/css/fonts/lato-bold.woff2 new file mode 100644 index 00000000..bb195043 Binary files /dev/null and b/css/fonts/lato-bold.woff2 differ diff --git a/css/fonts/lato-normal-italic.woff b/css/fonts/lato-normal-italic.woff new file mode 100644 index 00000000..76114bc0 Binary files /dev/null and b/css/fonts/lato-normal-italic.woff differ diff --git a/css/fonts/lato-normal-italic.woff2 b/css/fonts/lato-normal-italic.woff2 new file mode 100644 index 00000000..3404f37e Binary files /dev/null and b/css/fonts/lato-normal-italic.woff2 differ diff --git a/css/fonts/lato-normal.woff b/css/fonts/lato-normal.woff new file mode 100644 index 00000000..ae1307ff Binary files /dev/null and b/css/fonts/lato-normal.woff differ diff --git a/css/fonts/lato-normal.woff2 b/css/fonts/lato-normal.woff2 new file mode 100644 index 00000000..3bf98433 Binary files /dev/null and b/css/fonts/lato-normal.woff2 differ diff --git a/css/theme.css b/css/theme.css new file mode 100644 index 00000000..ad773009 --- /dev/null +++ b/css/theme.css @@ -0,0 +1,13 @@ +/* + * This file is copied from the upstream ReadTheDocs Sphinx + * theme. To aid upgradability this file should *not* be edited. + * modifications we need should be included in theme_extra.css. + * + * https://github.com/readthedocs/sphinx_rtd_theme + */ + + /* sphinx_rtd_theme version 1.2.0 | MIT license */ +html{box-sizing:border-box}*,:after,:before{box-sizing:inherit}article,aside,details,figcaption,figure,footer,header,hgroup,nav,section{display:block}audio,canvas,video{display:inline-block;*display:inline;*zoom:1}[hidden],audio:not([controls]){display:none}*{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}html{font-size:100%;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}body{margin:0}a:active,a:hover{outline:0}abbr[title]{border-bottom:1px dotted}b,strong{font-weight:700}blockquote{margin:0}dfn{font-style:italic}ins{background:#ff9;text-decoration:none}ins,mark{color:#000}mark{background:#ff0;font-style:italic;font-weight:700}.rst-content code,.rst-content tt,code,kbd,pre,samp{font-family:monospace,serif;_font-family:courier new,monospace;font-size:1em}pre{white-space:pre}q{quotes:none}q:after,q:before{content:"";content:none}small{font-size:85%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}dl,ol,ul{margin:0;padding:0;list-style:none;list-style-image:none}li{list-style:none}dd{margin:0}img{border:0;-ms-interpolation-mode:bicubic;vertical-align:middle;max-width:100%}svg:not(:root){overflow:hidden}figure,form{margin:0}label{cursor:pointer}button,input,select,textarea{font-size:100%;margin:0;vertical-align:baseline;*vertical-align:middle}button,input{line-height:normal}button,input[type=button],input[type=reset],input[type=submit]{cursor:pointer;-webkit-appearance:button;*overflow:visible}button[disabled],input[disabled]{cursor:default}input[type=search]{-webkit-appearance:textfield;-moz-box-sizing:content-box;-webkit-box-sizing:content-box;box-sizing:content-box}textarea{resize:vertical}table{border-collapse:collapse;border-spacing:0}td{vertical-align:top}.chromeframe{margin:.2em 0;background:#ccc;color:#000;padding:.2em 0}.ir{display:block;border:0;text-indent:-999em;overflow:hidden;background-color:transparent;background-repeat:no-repeat;text-align:left;direction:ltr;*line-height:0}.ir br{display:none}.hidden{display:none!important;visibility:hidden}.visuallyhidden{border:0;clip:rect(0 0 0 0);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px}.visuallyhidden.focusable:active,.visuallyhidden.focusable:focus{clip:auto;height:auto;margin:0;overflow:visible;position:static;width:auto}.invisible{visibility:hidden}.relative{position:relative}big,small{font-size:100%}@media print{body,html,section{background:none!important}*{box-shadow:none!important;text-shadow:none!important;filter:none!important;-ms-filter:none!important}a,a:visited{text-decoration:underline}.ir a:after,a[href^="#"]:after,a[href^="javascript:"]:after{content:""}blockquote,pre{page-break-inside:avoid}thead{display:table-header-group}img,tr{page-break-inside:avoid}img{max-width:100%!important}@page{margin:.5cm}.rst-content .toctree-wrapper>p.caption,h2,h3,p{orphans:3;widows:3}.rst-content .toctree-wrapper>p.caption,h2,h3{page-break-after:avoid}}.btn,.fa:before,.icon:before,.rst-content .admonition,.rst-content .admonition-title:before,.rst-content .admonition-todo,.rst-content .attention,.rst-content .caution,.rst-content .code-block-caption .headerlink:before,.rst-content .danger,.rst-content .eqno .headerlink:before,.rst-content .error,.rst-content .hint,.rst-content .important,.rst-content .note,.rst-content .seealso,.rst-content .tip,.rst-content .warning,.rst-content code.download span:first-child:before,.rst-content dl dt .headerlink:before,.rst-content h1 .headerlink:before,.rst-content h2 .headerlink:before,.rst-content h3 .headerlink:before,.rst-content h4 .headerlink:before,.rst-content h5 .headerlink:before,.rst-content h6 .headerlink:before,.rst-content p.caption .headerlink:before,.rst-content p .headerlink:before,.rst-content table>caption .headerlink:before,.rst-content tt.download span:first-child:before,.wy-alert,.wy-dropdown .caret:before,.wy-inline-validate.wy-inline-validate-danger .wy-input-context:before,.wy-inline-validate.wy-inline-validate-info .wy-input-context:before,.wy-inline-validate.wy-inline-validate-success .wy-input-context:before,.wy-inline-validate.wy-inline-validate-warning .wy-input-context:before,.wy-menu-vertical li.current>a button.toctree-expand:before,.wy-menu-vertical li.on a button.toctree-expand:before,.wy-menu-vertical li button.toctree-expand:before,input[type=color],input[type=date],input[type=datetime-local],input[type=datetime],input[type=email],input[type=month],input[type=number],input[type=password],input[type=search],input[type=tel],input[type=text],input[type=time],input[type=url],input[type=week],select,textarea{-webkit-font-smoothing:antialiased}.clearfix{*zoom:1}.clearfix:after,.clearfix:before{display:table;content:""}.clearfix:after{clear:both}/*! + * Font Awesome 4.7.0 by @davegandy - http://fontawesome.io - @fontawesome + * License - http://fontawesome.io/license (Font: SIL OFL 1.1, CSS: MIT License) + */@font-face{font-family:FontAwesome;src:url(fonts/fontawesome-webfont.eot?674f50d287a8c48dc19ba404d20fe713);src:url(fonts/fontawesome-webfont.eot?674f50d287a8c48dc19ba404d20fe713?#iefix&v=4.7.0) format("embedded-opentype"),url(fonts/fontawesome-webfont.woff2?af7ae505a9eed503f8b8e6982036873e) format("woff2"),url(fonts/fontawesome-webfont.woff?fee66e712a8a08eef5805a46892932ad) format("woff"),url(fonts/fontawesome-webfont.ttf?b06871f281fee6b241d60582ae9369b9) format("truetype"),url(fonts/fontawesome-webfont.svg?912ec66d7572ff821749319396470bde#fontawesomeregular) format("svg");font-weight:400;font-style:normal}.fa,.icon,.rst-content .admonition-title,.rst-content .code-block-caption .headerlink,.rst-content .eqno .headerlink,.rst-content code.download span:first-child,.rst-content dl dt .headerlink,.rst-content h1 .headerlink,.rst-content h2 .headerlink,.rst-content h3 .headerlink,.rst-content h4 .headerlink,.rst-content h5 .headerlink,.rst-content h6 .headerlink,.rst-content p.caption .headerlink,.rst-content p .headerlink,.rst-content table>caption .headerlink,.rst-content tt.download span:first-child,.wy-menu-vertical li.current>a button.toctree-expand,.wy-menu-vertical li.on a button.toctree-expand,.wy-menu-vertical li button.toctree-expand{display:inline-block;font:normal normal normal 14px/1 FontAwesome;font-size:inherit;text-rendering:auto;-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale}.fa-lg{font-size:1.33333em;line-height:.75em;vertical-align:-15%}.fa-2x{font-size:2em}.fa-3x{font-size:3em}.fa-4x{font-size:4em}.fa-5x{font-size:5em}.fa-fw{width:1.28571em;text-align:center}.fa-ul{padding-left:0;margin-left:2.14286em;list-style-type:none}.fa-ul>li{position:relative}.fa-li{position:absolute;left:-2.14286em;width:2.14286em;top:.14286em;text-align:center}.fa-li.fa-lg{left:-1.85714em}.fa-border{padding:.2em .25em .15em;border:.08em solid #eee;border-radius:.1em}.fa-pull-left{float:left}.fa-pull-right{float:right}.fa-pull-left.icon,.fa.fa-pull-left,.rst-content .code-block-caption .fa-pull-left.headerlink,.rst-content .eqno .fa-pull-left.headerlink,.rst-content .fa-pull-left.admonition-title,.rst-content code.download span.fa-pull-left:first-child,.rst-content dl dt .fa-pull-left.headerlink,.rst-content h1 .fa-pull-left.headerlink,.rst-content h2 .fa-pull-left.headerlink,.rst-content h3 .fa-pull-left.headerlink,.rst-content h4 .fa-pull-left.headerlink,.rst-content h5 .fa-pull-left.headerlink,.rst-content h6 .fa-pull-left.headerlink,.rst-content p .fa-pull-left.headerlink,.rst-content table>caption .fa-pull-left.headerlink,.rst-content tt.download span.fa-pull-left:first-child,.wy-menu-vertical li.current>a button.fa-pull-left.toctree-expand,.wy-menu-vertical li.on a button.fa-pull-left.toctree-expand,.wy-menu-vertical li button.fa-pull-left.toctree-expand{margin-right:.3em}.fa-pull-right.icon,.fa.fa-pull-right,.rst-content .code-block-caption .fa-pull-right.headerlink,.rst-content .eqno .fa-pull-right.headerlink,.rst-content .fa-pull-right.admonition-title,.rst-content code.download span.fa-pull-right:first-child,.rst-content dl dt .fa-pull-right.headerlink,.rst-content h1 .fa-pull-right.headerlink,.rst-content h2 .fa-pull-right.headerlink,.rst-content h3 .fa-pull-right.headerlink,.rst-content h4 .fa-pull-right.headerlink,.rst-content h5 .fa-pull-right.headerlink,.rst-content h6 .fa-pull-right.headerlink,.rst-content p .fa-pull-right.headerlink,.rst-content table>caption .fa-pull-right.headerlink,.rst-content tt.download span.fa-pull-right:first-child,.wy-menu-vertical li.current>a button.fa-pull-right.toctree-expand,.wy-menu-vertical li.on a button.fa-pull-right.toctree-expand,.wy-menu-vertical li button.fa-pull-right.toctree-expand{margin-left:.3em}.pull-right{float:right}.pull-left{float:left}.fa.pull-left,.pull-left.icon,.rst-content .code-block-caption .pull-left.headerlink,.rst-content .eqno .pull-left.headerlink,.rst-content .pull-left.admonition-title,.rst-content code.download span.pull-left:first-child,.rst-content dl dt .pull-left.headerlink,.rst-content h1 .pull-left.headerlink,.rst-content h2 .pull-left.headerlink,.rst-content h3 .pull-left.headerlink,.rst-content h4 .pull-left.headerlink,.rst-content h5 .pull-left.headerlink,.rst-content h6 .pull-left.headerlink,.rst-content p .pull-left.headerlink,.rst-content table>caption .pull-left.headerlink,.rst-content tt.download span.pull-left:first-child,.wy-menu-vertical li.current>a button.pull-left.toctree-expand,.wy-menu-vertical li.on a button.pull-left.toctree-expand,.wy-menu-vertical li button.pull-left.toctree-expand{margin-right:.3em}.fa.pull-right,.pull-right.icon,.rst-content .code-block-caption .pull-right.headerlink,.rst-content .eqno .pull-right.headerlink,.rst-content .pull-right.admonition-title,.rst-content code.download span.pull-right:first-child,.rst-content dl dt .pull-right.headerlink,.rst-content h1 .pull-right.headerlink,.rst-content h2 .pull-right.headerlink,.rst-content h3 .pull-right.headerlink,.rst-content h4 .pull-right.headerlink,.rst-content h5 .pull-right.headerlink,.rst-content h6 .pull-right.headerlink,.rst-content p .pull-right.headerlink,.rst-content table>caption .pull-right.headerlink,.rst-content tt.download span.pull-right:first-child,.wy-menu-vertical li.current>a button.pull-right.toctree-expand,.wy-menu-vertical li.on a button.pull-right.toctree-expand,.wy-menu-vertical li button.pull-right.toctree-expand{margin-left:.3em}.fa-spin{-webkit-animation:fa-spin 2s linear infinite;animation:fa-spin 2s linear infinite}.fa-pulse{-webkit-animation:fa-spin 1s steps(8) infinite;animation:fa-spin 1s steps(8) infinite}@-webkit-keyframes fa-spin{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}to{-webkit-transform:rotate(359deg);transform:rotate(359deg)}}@keyframes fa-spin{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}to{-webkit-transform:rotate(359deg);transform:rotate(359deg)}}.fa-rotate-90{-ms-filter:"progid:DXImageTransform.Microsoft.BasicImage(rotation=1)";-webkit-transform:rotate(90deg);-ms-transform:rotate(90deg);transform:rotate(90deg)}.fa-rotate-180{-ms-filter:"progid:DXImageTransform.Microsoft.BasicImage(rotation=2)";-webkit-transform:rotate(180deg);-ms-transform:rotate(180deg);transform:rotate(180deg)}.fa-rotate-270{-ms-filter:"progid:DXImageTransform.Microsoft.BasicImage(rotation=3)";-webkit-transform:rotate(270deg);-ms-transform:rotate(270deg);transform:rotate(270deg)}.fa-flip-horizontal{-ms-filter:"progid:DXImageTransform.Microsoft.BasicImage(rotation=0, mirror=1)";-webkit-transform:scaleX(-1);-ms-transform:scaleX(-1);transform:scaleX(-1)}.fa-flip-vertical{-ms-filter:"progid:DXImageTransform.Microsoft.BasicImage(rotation=2, mirror=1)";-webkit-transform:scaleY(-1);-ms-transform:scaleY(-1);transform:scaleY(-1)}:root .fa-flip-horizontal,:root .fa-flip-vertical,:root .fa-rotate-90,:root .fa-rotate-180,:root .fa-rotate-270{filter:none}.fa-stack{position:relative;display:inline-block;width:2em;height:2em;line-height:2em;vertical-align:middle}.fa-stack-1x,.fa-stack-2x{position:absolute;left:0;width:100%;text-align:center}.fa-stack-1x{line-height:inherit}.fa-stack-2x{font-size:2em}.fa-inverse{color:#fff}.fa-glass:before{content:""}.fa-music:before{content:""}.fa-search:before,.icon-search:before{content:""}.fa-envelope-o:before{content:""}.fa-heart:before{content:""}.fa-star:before{content:""}.fa-star-o:before{content:""}.fa-user:before{content:""}.fa-film:before{content:""}.fa-th-large:before{content:""}.fa-th:before{content:""}.fa-th-list:before{content:""}.fa-check:before{content:""}.fa-close:before,.fa-remove:before,.fa-times:before{content:""}.fa-search-plus:before{content:""}.fa-search-minus:before{content:""}.fa-power-off:before{content:""}.fa-signal:before{content:""}.fa-cog:before,.fa-gear:before{content:""}.fa-trash-o:before{content:""}.fa-home:before,.icon-home:before{content:""}.fa-file-o:before{content:""}.fa-clock-o:before{content:""}.fa-road:before{content:""}.fa-download:before,.rst-content code.download span:first-child:before,.rst-content tt.download span:first-child:before{content:""}.fa-arrow-circle-o-down:before{content:""}.fa-arrow-circle-o-up:before{content:""}.fa-inbox:before{content:""}.fa-play-circle-o:before{content:""}.fa-repeat:before,.fa-rotate-right:before{content:""}.fa-refresh:before{content:""}.fa-list-alt:before{content:""}.fa-lock:before{content:""}.fa-flag:before{content:""}.fa-headphones:before{content:""}.fa-volume-off:before{content:""}.fa-volume-down:before{content:""}.fa-volume-up:before{content:""}.fa-qrcode:before{content:""}.fa-barcode:before{content:""}.fa-tag:before{content:""}.fa-tags:before{content:""}.fa-book:before,.icon-book:before{content:""}.fa-bookmark:before{content:""}.fa-print:before{content:""}.fa-camera:before{content:""}.fa-font:before{content:""}.fa-bold:before{content:""}.fa-italic:before{content:""}.fa-text-height:before{content:""}.fa-text-width:before{content:""}.fa-align-left:before{content:""}.fa-align-center:before{content:""}.fa-align-right:before{content:""}.fa-align-justify:before{content:""}.fa-list:before{content:""}.fa-dedent:before,.fa-outdent:before{content:""}.fa-indent:before{content:""}.fa-video-camera:before{content:""}.fa-image:before,.fa-photo:before,.fa-picture-o:before{content:""}.fa-pencil:before{content:""}.fa-map-marker:before{content:""}.fa-adjust:before{content:""}.fa-tint:before{content:""}.fa-edit:before,.fa-pencil-square-o:before{content:""}.fa-share-square-o:before{content:""}.fa-check-square-o:before{content:""}.fa-arrows:before{content:""}.fa-step-backward:before{content:""}.fa-fast-backward:before{content:""}.fa-backward:before{content:""}.fa-play:before{content:""}.fa-pause:before{content:""}.fa-stop:before{content:""}.fa-forward:before{content:""}.fa-fast-forward:before{content:""}.fa-step-forward:before{content:""}.fa-eject:before{content:""}.fa-chevron-left:before{content:""}.fa-chevron-right:before{content:""}.fa-plus-circle:before{content:""}.fa-minus-circle:before{content:""}.fa-times-circle:before,.wy-inline-validate.wy-inline-validate-danger .wy-input-context:before{content:""}.fa-check-circle:before,.wy-inline-validate.wy-inline-validate-success .wy-input-context:before{content:""}.fa-question-circle:before{content:""}.fa-info-circle:before{content:""}.fa-crosshairs:before{content:""}.fa-times-circle-o:before{content:""}.fa-check-circle-o:before{content:""}.fa-ban:before{content:""}.fa-arrow-left:before{content:""}.fa-arrow-right:before{content:""}.fa-arrow-up:before{content:""}.fa-arrow-down:before{content:""}.fa-mail-forward:before,.fa-share:before{content:""}.fa-expand:before{content:""}.fa-compress:before{content:""}.fa-plus:before{content:""}.fa-minus:before{content:""}.fa-asterisk:before{content:""}.fa-exclamation-circle:before,.rst-content .admonition-title:before,.wy-inline-validate.wy-inline-validate-info .wy-input-context:before,.wy-inline-validate.wy-inline-validate-warning .wy-input-context:before{content:""}.fa-gift:before{content:""}.fa-leaf:before{content:""}.fa-fire:before,.icon-fire:before{content:""}.fa-eye:before{content:""}.fa-eye-slash:before{content:""}.fa-exclamation-triangle:before,.fa-warning:before{content:""}.fa-plane:before{content:""}.fa-calendar:before{content:""}.fa-random:before{content:""}.fa-comment:before{content:""}.fa-magnet:before{content:""}.fa-chevron-up:before{content:""}.fa-chevron-down:before{content:""}.fa-retweet:before{content:""}.fa-shopping-cart:before{content:""}.fa-folder:before{content:""}.fa-folder-open:before{content:""}.fa-arrows-v:before{content:""}.fa-arrows-h:before{content:""}.fa-bar-chart-o:before,.fa-bar-chart:before{content:""}.fa-twitter-square:before{content:""}.fa-facebook-square:before{content:""}.fa-camera-retro:before{content:""}.fa-key:before{content:""}.fa-cogs:before,.fa-gears:before{content:""}.fa-comments:before{content:""}.fa-thumbs-o-up:before{content:""}.fa-thumbs-o-down:before{content:""}.fa-star-half:before{content:""}.fa-heart-o:before{content:""}.fa-sign-out:before{content:""}.fa-linkedin-square:before{content:""}.fa-thumb-tack:before{content:""}.fa-external-link:before{content:""}.fa-sign-in:before{content:""}.fa-trophy:before{content:""}.fa-github-square:before{content:""}.fa-upload:before{content:""}.fa-lemon-o:before{content:""}.fa-phone:before{content:""}.fa-square-o:before{content:""}.fa-bookmark-o:before{content:""}.fa-phone-square:before{content:""}.fa-twitter:before{content:""}.fa-facebook-f:before,.fa-facebook:before{content:""}.fa-github:before,.icon-github:before{content:""}.fa-unlock:before{content:""}.fa-credit-card:before{content:""}.fa-feed:before,.fa-rss:before{content:""}.fa-hdd-o:before{content:""}.fa-bullhorn:before{content:""}.fa-bell:before{content:""}.fa-certificate:before{content:""}.fa-hand-o-right:before{content:""}.fa-hand-o-left:before{content:""}.fa-hand-o-up:before{content:""}.fa-hand-o-down:before{content:""}.fa-arrow-circle-left:before,.icon-circle-arrow-left:before{content:""}.fa-arrow-circle-right:before,.icon-circle-arrow-right:before{content:""}.fa-arrow-circle-up:before{content:""}.fa-arrow-circle-down:before{content:""}.fa-globe:before{content:""}.fa-wrench:before{content:""}.fa-tasks:before{content:""}.fa-filter:before{content:""}.fa-briefcase:before{content:""}.fa-arrows-alt:before{content:""}.fa-group:before,.fa-users:before{content:""}.fa-chain:before,.fa-link:before,.icon-link:before{content:""}.fa-cloud:before{content:""}.fa-flask:before{content:""}.fa-cut:before,.fa-scissors:before{content:""}.fa-copy:before,.fa-files-o:before{content:""}.fa-paperclip:before{content:""}.fa-floppy-o:before,.fa-save:before{content:""}.fa-square:before{content:""}.fa-bars:before,.fa-navicon:before,.fa-reorder:before{content:""}.fa-list-ul:before{content:""}.fa-list-ol:before{content:""}.fa-strikethrough:before{content:""}.fa-underline:before{content:""}.fa-table:before{content:""}.fa-magic:before{content:""}.fa-truck:before{content:""}.fa-pinterest:before{content:""}.fa-pinterest-square:before{content:""}.fa-google-plus-square:before{content:""}.fa-google-plus:before{content:""}.fa-money:before{content:""}.fa-caret-down:before,.icon-caret-down:before,.wy-dropdown .caret:before{content:""}.fa-caret-up:before{content:""}.fa-caret-left:before{content:""}.fa-caret-right:before{content:""}.fa-columns:before{content:""}.fa-sort:before,.fa-unsorted:before{content:""}.fa-sort-desc:before,.fa-sort-down:before{content:""}.fa-sort-asc:before,.fa-sort-up:before{content:""}.fa-envelope:before{content:""}.fa-linkedin:before{content:""}.fa-rotate-left:before,.fa-undo:before{content:""}.fa-gavel:before,.fa-legal:before{content:""}.fa-dashboard:before,.fa-tachometer:before{content:""}.fa-comment-o:before{content:""}.fa-comments-o:before{content:""}.fa-bolt:before,.fa-flash:before{content:""}.fa-sitemap:before{content:""}.fa-umbrella:before{content:""}.fa-clipboard:before,.fa-paste:before{content:""}.fa-lightbulb-o:before{content:""}.fa-exchange:before{content:""}.fa-cloud-download:before{content:""}.fa-cloud-upload:before{content:""}.fa-user-md:before{content:""}.fa-stethoscope:before{content:""}.fa-suitcase:before{content:""}.fa-bell-o:before{content:""}.fa-coffee:before{content:""}.fa-cutlery:before{content:""}.fa-file-text-o:before{content:""}.fa-building-o:before{content:""}.fa-hospital-o:before{content:""}.fa-ambulance:before{content:""}.fa-medkit:before{content:""}.fa-fighter-jet:before{content:""}.fa-beer:before{content:""}.fa-h-square:before{content:""}.fa-plus-square:before{content:""}.fa-angle-double-left:before{content:""}.fa-angle-double-right:before{content:""}.fa-angle-double-up:before{content:""}.fa-angle-double-down:before{content:""}.fa-angle-left:before{content:""}.fa-angle-right:before{content:""}.fa-angle-up:before{content:""}.fa-angle-down:before{content:""}.fa-desktop:before{content:""}.fa-laptop:before{content:""}.fa-tablet:before{content:""}.fa-mobile-phone:before,.fa-mobile:before{content:""}.fa-circle-o:before{content:""}.fa-quote-left:before{content:""}.fa-quote-right:before{content:""}.fa-spinner:before{content:""}.fa-circle:before{content:""}.fa-mail-reply:before,.fa-reply:before{content:""}.fa-github-alt:before{content:""}.fa-folder-o:before{content:""}.fa-folder-open-o:before{content:""}.fa-smile-o:before{content:""}.fa-frown-o:before{content:""}.fa-meh-o:before{content:""}.fa-gamepad:before{content:""}.fa-keyboard-o:before{content:""}.fa-flag-o:before{content:""}.fa-flag-checkered:before{content:""}.fa-terminal:before{content:""}.fa-code:before{content:""}.fa-mail-reply-all:before,.fa-reply-all:before{content:""}.fa-star-half-empty:before,.fa-star-half-full:before,.fa-star-half-o:before{content:""}.fa-location-arrow:before{content:""}.fa-crop:before{content:""}.fa-code-fork:before{content:""}.fa-chain-broken:before,.fa-unlink:before{content:""}.fa-question:before{content:""}.fa-info:before{content:""}.fa-exclamation:before{content:""}.fa-superscript:before{content:""}.fa-subscript:before{content:""}.fa-eraser:before{content:""}.fa-puzzle-piece:before{content:""}.fa-microphone:before{content:""}.fa-microphone-slash:before{content:""}.fa-shield:before{content:""}.fa-calendar-o:before{content:""}.fa-fire-extinguisher:before{content:""}.fa-rocket:before{content:""}.fa-maxcdn:before{content:""}.fa-chevron-circle-left:before{content:""}.fa-chevron-circle-right:before{content:""}.fa-chevron-circle-up:before{content:""}.fa-chevron-circle-down:before{content:""}.fa-html5:before{content:""}.fa-css3:before{content:""}.fa-anchor:before{content:""}.fa-unlock-alt:before{content:""}.fa-bullseye:before{content:""}.fa-ellipsis-h:before{content:""}.fa-ellipsis-v:before{content:""}.fa-rss-square:before{content:""}.fa-play-circle:before{content:""}.fa-ticket:before{content:""}.fa-minus-square:before{content:""}.fa-minus-square-o:before,.wy-menu-vertical li.current>a button.toctree-expand:before,.wy-menu-vertical li.on a button.toctree-expand:before{content:""}.fa-level-up:before{content:""}.fa-level-down:before{content:""}.fa-check-square:before{content:""}.fa-pencil-square:before{content:""}.fa-external-link-square:before{content:""}.fa-share-square:before{content:""}.fa-compass:before{content:""}.fa-caret-square-o-down:before,.fa-toggle-down:before{content:""}.fa-caret-square-o-up:before,.fa-toggle-up:before{content:""}.fa-caret-square-o-right:before,.fa-toggle-right:before{content:""}.fa-eur:before,.fa-euro:before{content:""}.fa-gbp:before{content:""}.fa-dollar:before,.fa-usd:before{content:""}.fa-inr:before,.fa-rupee:before{content:""}.fa-cny:before,.fa-jpy:before,.fa-rmb:before,.fa-yen:before{content:""}.fa-rouble:before,.fa-rub:before,.fa-ruble:before{content:""}.fa-krw:before,.fa-won:before{content:""}.fa-bitcoin:before,.fa-btc:before{content:""}.fa-file:before{content:""}.fa-file-text:before{content:""}.fa-sort-alpha-asc:before{content:""}.fa-sort-alpha-desc:before{content:""}.fa-sort-amount-asc:before{content:""}.fa-sort-amount-desc:before{content:""}.fa-sort-numeric-asc:before{content:""}.fa-sort-numeric-desc:before{content:""}.fa-thumbs-up:before{content:""}.fa-thumbs-down:before{content:""}.fa-youtube-square:before{content:""}.fa-youtube:before{content:""}.fa-xing:before{content:""}.fa-xing-square:before{content:""}.fa-youtube-play:before{content:""}.fa-dropbox:before{content:""}.fa-stack-overflow:before{content:""}.fa-instagram:before{content:""}.fa-flickr:before{content:""}.fa-adn:before{content:""}.fa-bitbucket:before,.icon-bitbucket:before{content:""}.fa-bitbucket-square:before{content:""}.fa-tumblr:before{content:""}.fa-tumblr-square:before{content:""}.fa-long-arrow-down:before{content:""}.fa-long-arrow-up:before{content:""}.fa-long-arrow-left:before{content:""}.fa-long-arrow-right:before{content:""}.fa-apple:before{content:""}.fa-windows:before{content:""}.fa-android:before{content:""}.fa-linux:before{content:""}.fa-dribbble:before{content:""}.fa-skype:before{content:""}.fa-foursquare:before{content:""}.fa-trello:before{content:""}.fa-female:before{content:""}.fa-male:before{content:""}.fa-gittip:before,.fa-gratipay:before{content:""}.fa-sun-o:before{content:""}.fa-moon-o:before{content:""}.fa-archive:before{content:""}.fa-bug:before{content:""}.fa-vk:before{content:""}.fa-weibo:before{content:""}.fa-renren:before{content:""}.fa-pagelines:before{content:""}.fa-stack-exchange:before{content:""}.fa-arrow-circle-o-right:before{content:""}.fa-arrow-circle-o-left:before{content:""}.fa-caret-square-o-left:before,.fa-toggle-left:before{content:""}.fa-dot-circle-o:before{content:""}.fa-wheelchair:before{content:""}.fa-vimeo-square:before{content:""}.fa-try:before,.fa-turkish-lira:before{content:""}.fa-plus-square-o:before,.wy-menu-vertical li button.toctree-expand:before{content:""}.fa-space-shuttle:before{content:""}.fa-slack:before{content:""}.fa-envelope-square:before{content:""}.fa-wordpress:before{content:""}.fa-openid:before{content:""}.fa-bank:before,.fa-institution:before,.fa-university:before{content:""}.fa-graduation-cap:before,.fa-mortar-board:before{content:""}.fa-yahoo:before{content:""}.fa-google:before{content:""}.fa-reddit:before{content:""}.fa-reddit-square:before{content:""}.fa-stumbleupon-circle:before{content:""}.fa-stumbleupon:before{content:""}.fa-delicious:before{content:""}.fa-digg:before{content:""}.fa-pied-piper-pp:before{content:""}.fa-pied-piper-alt:before{content:""}.fa-drupal:before{content:""}.fa-joomla:before{content:""}.fa-language:before{content:""}.fa-fax:before{content:""}.fa-building:before{content:""}.fa-child:before{content:""}.fa-paw:before{content:""}.fa-spoon:before{content:""}.fa-cube:before{content:""}.fa-cubes:before{content:""}.fa-behance:before{content:""}.fa-behance-square:before{content:""}.fa-steam:before{content:""}.fa-steam-square:before{content:""}.fa-recycle:before{content:""}.fa-automobile:before,.fa-car:before{content:""}.fa-cab:before,.fa-taxi:before{content:""}.fa-tree:before{content:""}.fa-spotify:before{content:""}.fa-deviantart:before{content:""}.fa-soundcloud:before{content:""}.fa-database:before{content:""}.fa-file-pdf-o:before{content:""}.fa-file-word-o:before{content:""}.fa-file-excel-o:before{content:""}.fa-file-powerpoint-o:before{content:""}.fa-file-image-o:before,.fa-file-photo-o:before,.fa-file-picture-o:before{content:""}.fa-file-archive-o:before,.fa-file-zip-o:before{content:""}.fa-file-audio-o:before,.fa-file-sound-o:before{content:""}.fa-file-movie-o:before,.fa-file-video-o:before{content:""}.fa-file-code-o:before{content:""}.fa-vine:before{content:""}.fa-codepen:before{content:""}.fa-jsfiddle:before{content:""}.fa-life-bouy:before,.fa-life-buoy:before,.fa-life-ring:before,.fa-life-saver:before,.fa-support:before{content:""}.fa-circle-o-notch:before{content:""}.fa-ra:before,.fa-rebel:before,.fa-resistance:before{content:""}.fa-empire:before,.fa-ge:before{content:""}.fa-git-square:before{content:""}.fa-git:before{content:""}.fa-hacker-news:before,.fa-y-combinator-square:before,.fa-yc-square:before{content:""}.fa-tencent-weibo:before{content:""}.fa-qq:before{content:""}.fa-wechat:before,.fa-weixin:before{content:""}.fa-paper-plane:before,.fa-send:before{content:""}.fa-paper-plane-o:before,.fa-send-o:before{content:""}.fa-history:before{content:""}.fa-circle-thin:before{content:""}.fa-header:before{content:""}.fa-paragraph:before{content:""}.fa-sliders:before{content:""}.fa-share-alt:before{content:""}.fa-share-alt-square:before{content:""}.fa-bomb:before{content:""}.fa-futbol-o:before,.fa-soccer-ball-o:before{content:""}.fa-tty:before{content:""}.fa-binoculars:before{content:""}.fa-plug:before{content:""}.fa-slideshare:before{content:""}.fa-twitch:before{content:""}.fa-yelp:before{content:""}.fa-newspaper-o:before{content:""}.fa-wifi:before{content:""}.fa-calculator:before{content:""}.fa-paypal:before{content:""}.fa-google-wallet:before{content:""}.fa-cc-visa:before{content:""}.fa-cc-mastercard:before{content:""}.fa-cc-discover:before{content:""}.fa-cc-amex:before{content:""}.fa-cc-paypal:before{content:""}.fa-cc-stripe:before{content:""}.fa-bell-slash:before{content:""}.fa-bell-slash-o:before{content:""}.fa-trash:before{content:""}.fa-copyright:before{content:""}.fa-at:before{content:""}.fa-eyedropper:before{content:""}.fa-paint-brush:before{content:""}.fa-birthday-cake:before{content:""}.fa-area-chart:before{content:""}.fa-pie-chart:before{content:""}.fa-line-chart:before{content:""}.fa-lastfm:before{content:""}.fa-lastfm-square:before{content:""}.fa-toggle-off:before{content:""}.fa-toggle-on:before{content:""}.fa-bicycle:before{content:""}.fa-bus:before{content:""}.fa-ioxhost:before{content:""}.fa-angellist:before{content:""}.fa-cc:before{content:""}.fa-ils:before,.fa-shekel:before,.fa-sheqel:before{content:""}.fa-meanpath:before{content:""}.fa-buysellads:before{content:""}.fa-connectdevelop:before{content:""}.fa-dashcube:before{content:""}.fa-forumbee:before{content:""}.fa-leanpub:before{content:""}.fa-sellsy:before{content:""}.fa-shirtsinbulk:before{content:""}.fa-simplybuilt:before{content:""}.fa-skyatlas:before{content:""}.fa-cart-plus:before{content:""}.fa-cart-arrow-down:before{content:""}.fa-diamond:before{content:""}.fa-ship:before{content:""}.fa-user-secret:before{content:""}.fa-motorcycle:before{content:""}.fa-street-view:before{content:""}.fa-heartbeat:before{content:""}.fa-venus:before{content:""}.fa-mars:before{content:""}.fa-mercury:before{content:""}.fa-intersex:before,.fa-transgender:before{content:""}.fa-transgender-alt:before{content:""}.fa-venus-double:before{content:""}.fa-mars-double:before{content:""}.fa-venus-mars:before{content:""}.fa-mars-stroke:before{content:""}.fa-mars-stroke-v:before{content:""}.fa-mars-stroke-h:before{content:""}.fa-neuter:before{content:""}.fa-genderless:before{content:""}.fa-facebook-official:before{content:""}.fa-pinterest-p:before{content:""}.fa-whatsapp:before{content:""}.fa-server:before{content:""}.fa-user-plus:before{content:""}.fa-user-times:before{content:""}.fa-bed:before,.fa-hotel:before{content:""}.fa-viacoin:before{content:""}.fa-train:before{content:""}.fa-subway:before{content:""}.fa-medium:before{content:""}.fa-y-combinator:before,.fa-yc:before{content:""}.fa-optin-monster:before{content:""}.fa-opencart:before{content:""}.fa-expeditedssl:before{content:""}.fa-battery-4:before,.fa-battery-full:before,.fa-battery:before{content:""}.fa-battery-3:before,.fa-battery-three-quarters:before{content:""}.fa-battery-2:before,.fa-battery-half:before{content:""}.fa-battery-1:before,.fa-battery-quarter:before{content:""}.fa-battery-0:before,.fa-battery-empty:before{content:""}.fa-mouse-pointer:before{content:""}.fa-i-cursor:before{content:""}.fa-object-group:before{content:""}.fa-object-ungroup:before{content:""}.fa-sticky-note:before{content:""}.fa-sticky-note-o:before{content:""}.fa-cc-jcb:before{content:""}.fa-cc-diners-club:before{content:""}.fa-clone:before{content:""}.fa-balance-scale:before{content:""}.fa-hourglass-o:before{content:""}.fa-hourglass-1:before,.fa-hourglass-start:before{content:""}.fa-hourglass-2:before,.fa-hourglass-half:before{content:""}.fa-hourglass-3:before,.fa-hourglass-end:before{content:""}.fa-hourglass:before{content:""}.fa-hand-grab-o:before,.fa-hand-rock-o:before{content:""}.fa-hand-paper-o:before,.fa-hand-stop-o:before{content:""}.fa-hand-scissors-o:before{content:""}.fa-hand-lizard-o:before{content:""}.fa-hand-spock-o:before{content:""}.fa-hand-pointer-o:before{content:""}.fa-hand-peace-o:before{content:""}.fa-trademark:before{content:""}.fa-registered:before{content:""}.fa-creative-commons:before{content:""}.fa-gg:before{content:""}.fa-gg-circle:before{content:""}.fa-tripadvisor:before{content:""}.fa-odnoklassniki:before{content:""}.fa-odnoklassniki-square:before{content:""}.fa-get-pocket:before{content:""}.fa-wikipedia-w:before{content:""}.fa-safari:before{content:""}.fa-chrome:before{content:""}.fa-firefox:before{content:""}.fa-opera:before{content:""}.fa-internet-explorer:before{content:""}.fa-television:before,.fa-tv:before{content:""}.fa-contao:before{content:""}.fa-500px:before{content:""}.fa-amazon:before{content:""}.fa-calendar-plus-o:before{content:""}.fa-calendar-minus-o:before{content:""}.fa-calendar-times-o:before{content:""}.fa-calendar-check-o:before{content:""}.fa-industry:before{content:""}.fa-map-pin:before{content:""}.fa-map-signs:before{content:""}.fa-map-o:before{content:""}.fa-map:before{content:""}.fa-commenting:before{content:""}.fa-commenting-o:before{content:""}.fa-houzz:before{content:""}.fa-vimeo:before{content:""}.fa-black-tie:before{content:""}.fa-fonticons:before{content:""}.fa-reddit-alien:before{content:""}.fa-edge:before{content:""}.fa-credit-card-alt:before{content:""}.fa-codiepie:before{content:""}.fa-modx:before{content:""}.fa-fort-awesome:before{content:""}.fa-usb:before{content:""}.fa-product-hunt:before{content:""}.fa-mixcloud:before{content:""}.fa-scribd:before{content:""}.fa-pause-circle:before{content:""}.fa-pause-circle-o:before{content:""}.fa-stop-circle:before{content:""}.fa-stop-circle-o:before{content:""}.fa-shopping-bag:before{content:""}.fa-shopping-basket:before{content:""}.fa-hashtag:before{content:""}.fa-bluetooth:before{content:""}.fa-bluetooth-b:before{content:""}.fa-percent:before{content:""}.fa-gitlab:before,.icon-gitlab:before{content:""}.fa-wpbeginner:before{content:""}.fa-wpforms:before{content:""}.fa-envira:before{content:""}.fa-universal-access:before{content:""}.fa-wheelchair-alt:before{content:""}.fa-question-circle-o:before{content:""}.fa-blind:before{content:""}.fa-audio-description:before{content:""}.fa-volume-control-phone:before{content:""}.fa-braille:before{content:""}.fa-assistive-listening-systems:before{content:""}.fa-american-sign-language-interpreting:before,.fa-asl-interpreting:before{content:""}.fa-deaf:before,.fa-deafness:before,.fa-hard-of-hearing:before{content:""}.fa-glide:before{content:""}.fa-glide-g:before{content:""}.fa-sign-language:before,.fa-signing:before{content:""}.fa-low-vision:before{content:""}.fa-viadeo:before{content:""}.fa-viadeo-square:before{content:""}.fa-snapchat:before{content:""}.fa-snapchat-ghost:before{content:""}.fa-snapchat-square:before{content:""}.fa-pied-piper:before{content:""}.fa-first-order:before{content:""}.fa-yoast:before{content:""}.fa-themeisle:before{content:""}.fa-google-plus-circle:before,.fa-google-plus-official:before{content:""}.fa-fa:before,.fa-font-awesome:before{content:""}.fa-handshake-o:before{content:""}.fa-envelope-open:before{content:""}.fa-envelope-open-o:before{content:""}.fa-linode:before{content:""}.fa-address-book:before{content:""}.fa-address-book-o:before{content:""}.fa-address-card:before,.fa-vcard:before{content:""}.fa-address-card-o:before,.fa-vcard-o:before{content:""}.fa-user-circle:before{content:""}.fa-user-circle-o:before{content:""}.fa-user-o:before{content:""}.fa-id-badge:before{content:""}.fa-drivers-license:before,.fa-id-card:before{content:""}.fa-drivers-license-o:before,.fa-id-card-o:before{content:""}.fa-quora:before{content:""}.fa-free-code-camp:before{content:""}.fa-telegram:before{content:""}.fa-thermometer-4:before,.fa-thermometer-full:before,.fa-thermometer:before{content:""}.fa-thermometer-3:before,.fa-thermometer-three-quarters:before{content:""}.fa-thermometer-2:before,.fa-thermometer-half:before{content:""}.fa-thermometer-1:before,.fa-thermometer-quarter:before{content:""}.fa-thermometer-0:before,.fa-thermometer-empty:before{content:""}.fa-shower:before{content:""}.fa-bath:before,.fa-bathtub:before,.fa-s15:before{content:""}.fa-podcast:before{content:""}.fa-window-maximize:before{content:""}.fa-window-minimize:before{content:""}.fa-window-restore:before{content:""}.fa-times-rectangle:before,.fa-window-close:before{content:""}.fa-times-rectangle-o:before,.fa-window-close-o:before{content:""}.fa-bandcamp:before{content:""}.fa-grav:before{content:""}.fa-etsy:before{content:""}.fa-imdb:before{content:""}.fa-ravelry:before{content:""}.fa-eercast:before{content:""}.fa-microchip:before{content:""}.fa-snowflake-o:before{content:""}.fa-superpowers:before{content:""}.fa-wpexplorer:before{content:""}.fa-meetup:before{content:""}.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);border:0}.sr-only-focusable:active,.sr-only-focusable:focus{position:static;width:auto;height:auto;margin:0;overflow:visible;clip:auto}.fa,.icon,.rst-content .admonition-title,.rst-content .code-block-caption .headerlink,.rst-content .eqno .headerlink,.rst-content code.download span:first-child,.rst-content dl dt .headerlink,.rst-content h1 .headerlink,.rst-content h2 .headerlink,.rst-content h3 .headerlink,.rst-content h4 .headerlink,.rst-content h5 .headerlink,.rst-content h6 .headerlink,.rst-content p.caption .headerlink,.rst-content p .headerlink,.rst-content table>caption .headerlink,.rst-content tt.download span:first-child,.wy-dropdown .caret,.wy-inline-validate.wy-inline-validate-danger .wy-input-context,.wy-inline-validate.wy-inline-validate-info .wy-input-context,.wy-inline-validate.wy-inline-validate-success .wy-input-context,.wy-inline-validate.wy-inline-validate-warning .wy-input-context,.wy-menu-vertical li.current>a button.toctree-expand,.wy-menu-vertical li.on a button.toctree-expand,.wy-menu-vertical li button.toctree-expand{font-family:inherit}.fa:before,.icon:before,.rst-content .admonition-title:before,.rst-content .code-block-caption .headerlink:before,.rst-content .eqno .headerlink:before,.rst-content code.download span:first-child:before,.rst-content dl dt .headerlink:before,.rst-content h1 .headerlink:before,.rst-content h2 .headerlink:before,.rst-content h3 .headerlink:before,.rst-content h4 .headerlink:before,.rst-content h5 .headerlink:before,.rst-content h6 .headerlink:before,.rst-content p.caption .headerlink:before,.rst-content p .headerlink:before,.rst-content table>caption .headerlink:before,.rst-content tt.download span:first-child:before,.wy-dropdown .caret:before,.wy-inline-validate.wy-inline-validate-danger .wy-input-context:before,.wy-inline-validate.wy-inline-validate-info .wy-input-context:before,.wy-inline-validate.wy-inline-validate-success .wy-input-context:before,.wy-inline-validate.wy-inline-validate-warning .wy-input-context:before,.wy-menu-vertical li.current>a button.toctree-expand:before,.wy-menu-vertical li.on a button.toctree-expand:before,.wy-menu-vertical li button.toctree-expand:before{font-family:FontAwesome;display:inline-block;font-style:normal;font-weight:400;line-height:1;text-decoration:inherit}.rst-content .code-block-caption a .headerlink,.rst-content .eqno a .headerlink,.rst-content a .admonition-title,.rst-content code.download a span:first-child,.rst-content dl dt a .headerlink,.rst-content h1 a .headerlink,.rst-content h2 a .headerlink,.rst-content h3 a .headerlink,.rst-content h4 a .headerlink,.rst-content h5 a .headerlink,.rst-content h6 a .headerlink,.rst-content p.caption a .headerlink,.rst-content p a .headerlink,.rst-content table>caption a .headerlink,.rst-content tt.download a span:first-child,.wy-menu-vertical li.current>a button.toctree-expand,.wy-menu-vertical li.on a button.toctree-expand,.wy-menu-vertical li a button.toctree-expand,a .fa,a .icon,a .rst-content .admonition-title,a .rst-content .code-block-caption .headerlink,a .rst-content .eqno .headerlink,a .rst-content code.download span:first-child,a .rst-content dl dt .headerlink,a .rst-content h1 .headerlink,a .rst-content h2 .headerlink,a .rst-content h3 .headerlink,a .rst-content h4 .headerlink,a .rst-content h5 .headerlink,a .rst-content h6 .headerlink,a .rst-content p.caption .headerlink,a .rst-content p .headerlink,a .rst-content table>caption .headerlink,a .rst-content tt.download span:first-child,a .wy-menu-vertical li button.toctree-expand{display:inline-block;text-decoration:inherit}.btn .fa,.btn .icon,.btn .rst-content .admonition-title,.btn .rst-content .code-block-caption .headerlink,.btn .rst-content .eqno .headerlink,.btn .rst-content code.download span:first-child,.btn .rst-content dl dt .headerlink,.btn .rst-content h1 .headerlink,.btn .rst-content h2 .headerlink,.btn .rst-content h3 .headerlink,.btn .rst-content h4 .headerlink,.btn .rst-content h5 .headerlink,.btn .rst-content h6 .headerlink,.btn .rst-content p .headerlink,.btn .rst-content table>caption .headerlink,.btn .rst-content tt.download span:first-child,.btn .wy-menu-vertical li.current>a button.toctree-expand,.btn .wy-menu-vertical li.on a button.toctree-expand,.btn .wy-menu-vertical li button.toctree-expand,.nav .fa,.nav .icon,.nav .rst-content .admonition-title,.nav .rst-content .code-block-caption .headerlink,.nav .rst-content .eqno .headerlink,.nav .rst-content code.download span:first-child,.nav .rst-content dl dt .headerlink,.nav .rst-content h1 .headerlink,.nav .rst-content h2 .headerlink,.nav .rst-content h3 .headerlink,.nav .rst-content h4 .headerlink,.nav .rst-content h5 .headerlink,.nav .rst-content h6 .headerlink,.nav .rst-content p .headerlink,.nav .rst-content table>caption .headerlink,.nav .rst-content tt.download span:first-child,.nav .wy-menu-vertical li.current>a button.toctree-expand,.nav .wy-menu-vertical li.on a button.toctree-expand,.nav .wy-menu-vertical li button.toctree-expand,.rst-content .btn .admonition-title,.rst-content .code-block-caption .btn .headerlink,.rst-content .code-block-caption .nav .headerlink,.rst-content .eqno .btn .headerlink,.rst-content .eqno .nav .headerlink,.rst-content .nav .admonition-title,.rst-content code.download .btn span:first-child,.rst-content code.download .nav span:first-child,.rst-content dl dt .btn .headerlink,.rst-content dl dt .nav .headerlink,.rst-content h1 .btn .headerlink,.rst-content h1 .nav .headerlink,.rst-content h2 .btn .headerlink,.rst-content h2 .nav .headerlink,.rst-content h3 .btn .headerlink,.rst-content h3 .nav .headerlink,.rst-content h4 .btn .headerlink,.rst-content h4 .nav .headerlink,.rst-content h5 .btn .headerlink,.rst-content h5 .nav .headerlink,.rst-content h6 .btn .headerlink,.rst-content h6 .nav .headerlink,.rst-content p .btn .headerlink,.rst-content p .nav .headerlink,.rst-content table>caption .btn .headerlink,.rst-content table>caption .nav .headerlink,.rst-content tt.download .btn span:first-child,.rst-content tt.download .nav span:first-child,.wy-menu-vertical li .btn button.toctree-expand,.wy-menu-vertical li.current>a .btn button.toctree-expand,.wy-menu-vertical li.current>a .nav button.toctree-expand,.wy-menu-vertical li .nav button.toctree-expand,.wy-menu-vertical li.on a .btn button.toctree-expand,.wy-menu-vertical li.on a .nav button.toctree-expand{display:inline}.btn .fa-large.icon,.btn .fa.fa-large,.btn .rst-content .code-block-caption .fa-large.headerlink,.btn .rst-content .eqno .fa-large.headerlink,.btn .rst-content .fa-large.admonition-title,.btn .rst-content code.download span.fa-large:first-child,.btn .rst-content dl dt .fa-large.headerlink,.btn .rst-content h1 .fa-large.headerlink,.btn .rst-content h2 .fa-large.headerlink,.btn .rst-content h3 .fa-large.headerlink,.btn .rst-content h4 .fa-large.headerlink,.btn .rst-content h5 .fa-large.headerlink,.btn .rst-content h6 .fa-large.headerlink,.btn .rst-content p .fa-large.headerlink,.btn .rst-content table>caption .fa-large.headerlink,.btn .rst-content tt.download span.fa-large:first-child,.btn .wy-menu-vertical li button.fa-large.toctree-expand,.nav .fa-large.icon,.nav .fa.fa-large,.nav .rst-content .code-block-caption .fa-large.headerlink,.nav .rst-content .eqno .fa-large.headerlink,.nav .rst-content .fa-large.admonition-title,.nav .rst-content code.download span.fa-large:first-child,.nav .rst-content dl dt .fa-large.headerlink,.nav .rst-content h1 .fa-large.headerlink,.nav .rst-content h2 .fa-large.headerlink,.nav .rst-content h3 .fa-large.headerlink,.nav .rst-content h4 .fa-large.headerlink,.nav .rst-content h5 .fa-large.headerlink,.nav .rst-content h6 .fa-large.headerlink,.nav .rst-content p .fa-large.headerlink,.nav .rst-content table>caption .fa-large.headerlink,.nav .rst-content tt.download span.fa-large:first-child,.nav .wy-menu-vertical li button.fa-large.toctree-expand,.rst-content .btn .fa-large.admonition-title,.rst-content .code-block-caption .btn .fa-large.headerlink,.rst-content .code-block-caption .nav .fa-large.headerlink,.rst-content .eqno .btn .fa-large.headerlink,.rst-content .eqno .nav .fa-large.headerlink,.rst-content .nav .fa-large.admonition-title,.rst-content code.download .btn span.fa-large:first-child,.rst-content code.download .nav span.fa-large:first-child,.rst-content dl dt .btn .fa-large.headerlink,.rst-content dl dt .nav .fa-large.headerlink,.rst-content h1 .btn .fa-large.headerlink,.rst-content h1 .nav .fa-large.headerlink,.rst-content h2 .btn .fa-large.headerlink,.rst-content h2 .nav .fa-large.headerlink,.rst-content h3 .btn .fa-large.headerlink,.rst-content h3 .nav .fa-large.headerlink,.rst-content h4 .btn .fa-large.headerlink,.rst-content h4 .nav .fa-large.headerlink,.rst-content h5 .btn .fa-large.headerlink,.rst-content h5 .nav .fa-large.headerlink,.rst-content h6 .btn .fa-large.headerlink,.rst-content h6 .nav .fa-large.headerlink,.rst-content p .btn .fa-large.headerlink,.rst-content p .nav .fa-large.headerlink,.rst-content table>caption .btn .fa-large.headerlink,.rst-content table>caption .nav .fa-large.headerlink,.rst-content tt.download .btn span.fa-large:first-child,.rst-content tt.download .nav span.fa-large:first-child,.wy-menu-vertical li .btn button.fa-large.toctree-expand,.wy-menu-vertical li .nav button.fa-large.toctree-expand{line-height:.9em}.btn .fa-spin.icon,.btn .fa.fa-spin,.btn .rst-content .code-block-caption .fa-spin.headerlink,.btn .rst-content .eqno .fa-spin.headerlink,.btn .rst-content .fa-spin.admonition-title,.btn .rst-content code.download span.fa-spin:first-child,.btn .rst-content dl dt .fa-spin.headerlink,.btn .rst-content h1 .fa-spin.headerlink,.btn .rst-content h2 .fa-spin.headerlink,.btn .rst-content h3 .fa-spin.headerlink,.btn .rst-content h4 .fa-spin.headerlink,.btn .rst-content h5 .fa-spin.headerlink,.btn .rst-content h6 .fa-spin.headerlink,.btn .rst-content p .fa-spin.headerlink,.btn .rst-content table>caption .fa-spin.headerlink,.btn .rst-content tt.download span.fa-spin:first-child,.btn .wy-menu-vertical li button.fa-spin.toctree-expand,.nav .fa-spin.icon,.nav .fa.fa-spin,.nav .rst-content .code-block-caption .fa-spin.headerlink,.nav .rst-content .eqno .fa-spin.headerlink,.nav .rst-content .fa-spin.admonition-title,.nav .rst-content code.download span.fa-spin:first-child,.nav .rst-content dl dt .fa-spin.headerlink,.nav .rst-content h1 .fa-spin.headerlink,.nav .rst-content h2 .fa-spin.headerlink,.nav .rst-content h3 .fa-spin.headerlink,.nav .rst-content h4 .fa-spin.headerlink,.nav .rst-content h5 .fa-spin.headerlink,.nav .rst-content h6 .fa-spin.headerlink,.nav .rst-content p .fa-spin.headerlink,.nav .rst-content table>caption .fa-spin.headerlink,.nav .rst-content tt.download span.fa-spin:first-child,.nav .wy-menu-vertical li button.fa-spin.toctree-expand,.rst-content .btn .fa-spin.admonition-title,.rst-content .code-block-caption .btn .fa-spin.headerlink,.rst-content .code-block-caption .nav .fa-spin.headerlink,.rst-content .eqno .btn .fa-spin.headerlink,.rst-content .eqno .nav .fa-spin.headerlink,.rst-content .nav .fa-spin.admonition-title,.rst-content code.download .btn span.fa-spin:first-child,.rst-content code.download .nav span.fa-spin:first-child,.rst-content dl dt .btn .fa-spin.headerlink,.rst-content dl dt .nav .fa-spin.headerlink,.rst-content h1 .btn .fa-spin.headerlink,.rst-content h1 .nav .fa-spin.headerlink,.rst-content h2 .btn .fa-spin.headerlink,.rst-content h2 .nav .fa-spin.headerlink,.rst-content h3 .btn .fa-spin.headerlink,.rst-content h3 .nav .fa-spin.headerlink,.rst-content h4 .btn .fa-spin.headerlink,.rst-content h4 .nav .fa-spin.headerlink,.rst-content h5 .btn .fa-spin.headerlink,.rst-content h5 .nav .fa-spin.headerlink,.rst-content h6 .btn .fa-spin.headerlink,.rst-content h6 .nav .fa-spin.headerlink,.rst-content p .btn .fa-spin.headerlink,.rst-content p .nav .fa-spin.headerlink,.rst-content table>caption .btn .fa-spin.headerlink,.rst-content table>caption .nav .fa-spin.headerlink,.rst-content tt.download .btn span.fa-spin:first-child,.rst-content tt.download .nav span.fa-spin:first-child,.wy-menu-vertical li .btn button.fa-spin.toctree-expand,.wy-menu-vertical li .nav button.fa-spin.toctree-expand{display:inline-block}.btn.fa:before,.btn.icon:before,.rst-content .btn.admonition-title:before,.rst-content .code-block-caption .btn.headerlink:before,.rst-content .eqno .btn.headerlink:before,.rst-content code.download span.btn:first-child:before,.rst-content dl dt .btn.headerlink:before,.rst-content h1 .btn.headerlink:before,.rst-content h2 .btn.headerlink:before,.rst-content h3 .btn.headerlink:before,.rst-content h4 .btn.headerlink:before,.rst-content h5 .btn.headerlink:before,.rst-content h6 .btn.headerlink:before,.rst-content p .btn.headerlink:before,.rst-content table>caption .btn.headerlink:before,.rst-content tt.download span.btn:first-child:before,.wy-menu-vertical li button.btn.toctree-expand:before{opacity:.5;-webkit-transition:opacity .05s ease-in;-moz-transition:opacity .05s ease-in;transition:opacity .05s ease-in}.btn.fa:hover:before,.btn.icon:hover:before,.rst-content .btn.admonition-title:hover:before,.rst-content .code-block-caption .btn.headerlink:hover:before,.rst-content .eqno .btn.headerlink:hover:before,.rst-content code.download span.btn:first-child:hover:before,.rst-content dl dt .btn.headerlink:hover:before,.rst-content h1 .btn.headerlink:hover:before,.rst-content h2 .btn.headerlink:hover:before,.rst-content h3 .btn.headerlink:hover:before,.rst-content h4 .btn.headerlink:hover:before,.rst-content h5 .btn.headerlink:hover:before,.rst-content h6 .btn.headerlink:hover:before,.rst-content p .btn.headerlink:hover:before,.rst-content table>caption .btn.headerlink:hover:before,.rst-content tt.download span.btn:first-child:hover:before,.wy-menu-vertical li button.btn.toctree-expand:hover:before{opacity:1}.btn-mini .fa:before,.btn-mini .icon:before,.btn-mini .rst-content .admonition-title:before,.btn-mini .rst-content .code-block-caption .headerlink:before,.btn-mini .rst-content .eqno .headerlink:before,.btn-mini .rst-content code.download span:first-child:before,.btn-mini .rst-content dl dt .headerlink:before,.btn-mini .rst-content h1 .headerlink:before,.btn-mini .rst-content h2 .headerlink:before,.btn-mini .rst-content h3 .headerlink:before,.btn-mini .rst-content h4 .headerlink:before,.btn-mini .rst-content h5 .headerlink:before,.btn-mini .rst-content h6 .headerlink:before,.btn-mini .rst-content p .headerlink:before,.btn-mini .rst-content table>caption .headerlink:before,.btn-mini .rst-content tt.download span:first-child:before,.btn-mini .wy-menu-vertical li button.toctree-expand:before,.rst-content .btn-mini .admonition-title:before,.rst-content .code-block-caption .btn-mini .headerlink:before,.rst-content .eqno .btn-mini .headerlink:before,.rst-content code.download .btn-mini span:first-child:before,.rst-content dl dt .btn-mini .headerlink:before,.rst-content h1 .btn-mini .headerlink:before,.rst-content h2 .btn-mini .headerlink:before,.rst-content h3 .btn-mini .headerlink:before,.rst-content h4 .btn-mini .headerlink:before,.rst-content h5 .btn-mini .headerlink:before,.rst-content h6 .btn-mini .headerlink:before,.rst-content p .btn-mini .headerlink:before,.rst-content table>caption .btn-mini .headerlink:before,.rst-content tt.download .btn-mini span:first-child:before,.wy-menu-vertical li .btn-mini button.toctree-expand:before{font-size:14px;vertical-align:-15%}.rst-content .admonition,.rst-content .admonition-todo,.rst-content .attention,.rst-content .caution,.rst-content .danger,.rst-content .error,.rst-content .hint,.rst-content .important,.rst-content .note,.rst-content .seealso,.rst-content .tip,.rst-content .warning,.wy-alert{padding:12px;line-height:24px;margin-bottom:24px;background:#e7f2fa}.rst-content .admonition-title,.wy-alert-title{font-weight:700;display:block;color:#fff;background:#6ab0de;padding:6px 12px;margin:-12px -12px 12px}.rst-content .danger,.rst-content .error,.rst-content .wy-alert-danger.admonition,.rst-content .wy-alert-danger.admonition-todo,.rst-content .wy-alert-danger.attention,.rst-content .wy-alert-danger.caution,.rst-content .wy-alert-danger.hint,.rst-content .wy-alert-danger.important,.rst-content .wy-alert-danger.note,.rst-content .wy-alert-danger.seealso,.rst-content .wy-alert-danger.tip,.rst-content .wy-alert-danger.warning,.wy-alert.wy-alert-danger{background:#fdf3f2}.rst-content .danger .admonition-title,.rst-content .danger .wy-alert-title,.rst-content .error .admonition-title,.rst-content .error .wy-alert-title,.rst-content .wy-alert-danger.admonition-todo .admonition-title,.rst-content .wy-alert-danger.admonition-todo .wy-alert-title,.rst-content .wy-alert-danger.admonition .admonition-title,.rst-content .wy-alert-danger.admonition .wy-alert-title,.rst-content .wy-alert-danger.attention .admonition-title,.rst-content .wy-alert-danger.attention .wy-alert-title,.rst-content .wy-alert-danger.caution .admonition-title,.rst-content .wy-alert-danger.caution .wy-alert-title,.rst-content .wy-alert-danger.hint .admonition-title,.rst-content .wy-alert-danger.hint .wy-alert-title,.rst-content .wy-alert-danger.important .admonition-title,.rst-content .wy-alert-danger.important .wy-alert-title,.rst-content .wy-alert-danger.note .admonition-title,.rst-content .wy-alert-danger.note .wy-alert-title,.rst-content .wy-alert-danger.seealso .admonition-title,.rst-content .wy-alert-danger.seealso .wy-alert-title,.rst-content .wy-alert-danger.tip .admonition-title,.rst-content .wy-alert-danger.tip .wy-alert-title,.rst-content .wy-alert-danger.warning .admonition-title,.rst-content .wy-alert-danger.warning .wy-alert-title,.rst-content .wy-alert.wy-alert-danger .admonition-title,.wy-alert.wy-alert-danger .rst-content .admonition-title,.wy-alert.wy-alert-danger .wy-alert-title{background:#f29f97}.rst-content .admonition-todo,.rst-content .attention,.rst-content .caution,.rst-content .warning,.rst-content .wy-alert-warning.admonition,.rst-content .wy-alert-warning.danger,.rst-content .wy-alert-warning.error,.rst-content .wy-alert-warning.hint,.rst-content .wy-alert-warning.important,.rst-content .wy-alert-warning.note,.rst-content .wy-alert-warning.seealso,.rst-content .wy-alert-warning.tip,.wy-alert.wy-alert-warning{background:#ffedcc}.rst-content .admonition-todo .admonition-title,.rst-content .admonition-todo .wy-alert-title,.rst-content .attention .admonition-title,.rst-content .attention .wy-alert-title,.rst-content .caution .admonition-title,.rst-content .caution .wy-alert-title,.rst-content .warning .admonition-title,.rst-content .warning .wy-alert-title,.rst-content .wy-alert-warning.admonition .admonition-title,.rst-content .wy-alert-warning.admonition .wy-alert-title,.rst-content .wy-alert-warning.danger .admonition-title,.rst-content .wy-alert-warning.danger .wy-alert-title,.rst-content .wy-alert-warning.error .admonition-title,.rst-content .wy-alert-warning.error .wy-alert-title,.rst-content .wy-alert-warning.hint .admonition-title,.rst-content .wy-alert-warning.hint .wy-alert-title,.rst-content .wy-alert-warning.important .admonition-title,.rst-content .wy-alert-warning.important .wy-alert-title,.rst-content .wy-alert-warning.note .admonition-title,.rst-content .wy-alert-warning.note .wy-alert-title,.rst-content .wy-alert-warning.seealso .admonition-title,.rst-content .wy-alert-warning.seealso .wy-alert-title,.rst-content .wy-alert-warning.tip .admonition-title,.rst-content .wy-alert-warning.tip .wy-alert-title,.rst-content .wy-alert.wy-alert-warning .admonition-title,.wy-alert.wy-alert-warning .rst-content .admonition-title,.wy-alert.wy-alert-warning .wy-alert-title{background:#f0b37e}.rst-content .note,.rst-content .seealso,.rst-content .wy-alert-info.admonition,.rst-content .wy-alert-info.admonition-todo,.rst-content .wy-alert-info.attention,.rst-content .wy-alert-info.caution,.rst-content .wy-alert-info.danger,.rst-content .wy-alert-info.error,.rst-content .wy-alert-info.hint,.rst-content .wy-alert-info.important,.rst-content .wy-alert-info.tip,.rst-content .wy-alert-info.warning,.wy-alert.wy-alert-info{background:#e7f2fa}.rst-content .note .admonition-title,.rst-content .note .wy-alert-title,.rst-content .seealso .admonition-title,.rst-content .seealso .wy-alert-title,.rst-content .wy-alert-info.admonition-todo .admonition-title,.rst-content .wy-alert-info.admonition-todo .wy-alert-title,.rst-content .wy-alert-info.admonition .admonition-title,.rst-content .wy-alert-info.admonition .wy-alert-title,.rst-content .wy-alert-info.attention .admonition-title,.rst-content .wy-alert-info.attention .wy-alert-title,.rst-content .wy-alert-info.caution .admonition-title,.rst-content .wy-alert-info.caution .wy-alert-title,.rst-content .wy-alert-info.danger .admonition-title,.rst-content .wy-alert-info.danger .wy-alert-title,.rst-content .wy-alert-info.error .admonition-title,.rst-content .wy-alert-info.error .wy-alert-title,.rst-content .wy-alert-info.hint .admonition-title,.rst-content .wy-alert-info.hint .wy-alert-title,.rst-content .wy-alert-info.important .admonition-title,.rst-content .wy-alert-info.important .wy-alert-title,.rst-content .wy-alert-info.tip .admonition-title,.rst-content .wy-alert-info.tip .wy-alert-title,.rst-content .wy-alert-info.warning .admonition-title,.rst-content .wy-alert-info.warning .wy-alert-title,.rst-content .wy-alert.wy-alert-info .admonition-title,.wy-alert.wy-alert-info .rst-content .admonition-title,.wy-alert.wy-alert-info .wy-alert-title{background:#6ab0de}.rst-content .hint,.rst-content .important,.rst-content .tip,.rst-content .wy-alert-success.admonition,.rst-content .wy-alert-success.admonition-todo,.rst-content .wy-alert-success.attention,.rst-content .wy-alert-success.caution,.rst-content .wy-alert-success.danger,.rst-content .wy-alert-success.error,.rst-content .wy-alert-success.note,.rst-content .wy-alert-success.seealso,.rst-content .wy-alert-success.warning,.wy-alert.wy-alert-success{background:#dbfaf4}.rst-content .hint .admonition-title,.rst-content .hint .wy-alert-title,.rst-content .important .admonition-title,.rst-content .important .wy-alert-title,.rst-content .tip .admonition-title,.rst-content .tip .wy-alert-title,.rst-content .wy-alert-success.admonition-todo .admonition-title,.rst-content .wy-alert-success.admonition-todo .wy-alert-title,.rst-content .wy-alert-success.admonition .admonition-title,.rst-content .wy-alert-success.admonition .wy-alert-title,.rst-content .wy-alert-success.attention .admonition-title,.rst-content .wy-alert-success.attention .wy-alert-title,.rst-content .wy-alert-success.caution .admonition-title,.rst-content .wy-alert-success.caution .wy-alert-title,.rst-content .wy-alert-success.danger .admonition-title,.rst-content .wy-alert-success.danger .wy-alert-title,.rst-content .wy-alert-success.error .admonition-title,.rst-content .wy-alert-success.error .wy-alert-title,.rst-content .wy-alert-success.note .admonition-title,.rst-content .wy-alert-success.note .wy-alert-title,.rst-content .wy-alert-success.seealso .admonition-title,.rst-content .wy-alert-success.seealso .wy-alert-title,.rst-content .wy-alert-success.warning .admonition-title,.rst-content .wy-alert-success.warning .wy-alert-title,.rst-content .wy-alert.wy-alert-success .admonition-title,.wy-alert.wy-alert-success .rst-content .admonition-title,.wy-alert.wy-alert-success .wy-alert-title{background:#1abc9c}.rst-content .wy-alert-neutral.admonition,.rst-content .wy-alert-neutral.admonition-todo,.rst-content .wy-alert-neutral.attention,.rst-content .wy-alert-neutral.caution,.rst-content .wy-alert-neutral.danger,.rst-content .wy-alert-neutral.error,.rst-content .wy-alert-neutral.hint,.rst-content .wy-alert-neutral.important,.rst-content .wy-alert-neutral.note,.rst-content .wy-alert-neutral.seealso,.rst-content .wy-alert-neutral.tip,.rst-content .wy-alert-neutral.warning,.wy-alert.wy-alert-neutral{background:#f3f6f6}.rst-content .wy-alert-neutral.admonition-todo .admonition-title,.rst-content .wy-alert-neutral.admonition-todo .wy-alert-title,.rst-content .wy-alert-neutral.admonition .admonition-title,.rst-content .wy-alert-neutral.admonition .wy-alert-title,.rst-content .wy-alert-neutral.attention .admonition-title,.rst-content .wy-alert-neutral.attention .wy-alert-title,.rst-content .wy-alert-neutral.caution .admonition-title,.rst-content .wy-alert-neutral.caution .wy-alert-title,.rst-content .wy-alert-neutral.danger .admonition-title,.rst-content .wy-alert-neutral.danger .wy-alert-title,.rst-content .wy-alert-neutral.error .admonition-title,.rst-content .wy-alert-neutral.error .wy-alert-title,.rst-content .wy-alert-neutral.hint .admonition-title,.rst-content .wy-alert-neutral.hint .wy-alert-title,.rst-content .wy-alert-neutral.important .admonition-title,.rst-content .wy-alert-neutral.important .wy-alert-title,.rst-content .wy-alert-neutral.note .admonition-title,.rst-content .wy-alert-neutral.note .wy-alert-title,.rst-content .wy-alert-neutral.seealso .admonition-title,.rst-content .wy-alert-neutral.seealso .wy-alert-title,.rst-content .wy-alert-neutral.tip .admonition-title,.rst-content .wy-alert-neutral.tip .wy-alert-title,.rst-content .wy-alert-neutral.warning .admonition-title,.rst-content .wy-alert-neutral.warning .wy-alert-title,.rst-content .wy-alert.wy-alert-neutral .admonition-title,.wy-alert.wy-alert-neutral .rst-content .admonition-title,.wy-alert.wy-alert-neutral .wy-alert-title{color:#404040;background:#e1e4e5}.rst-content .wy-alert-neutral.admonition-todo a,.rst-content .wy-alert-neutral.admonition a,.rst-content .wy-alert-neutral.attention a,.rst-content .wy-alert-neutral.caution a,.rst-content .wy-alert-neutral.danger a,.rst-content .wy-alert-neutral.error a,.rst-content .wy-alert-neutral.hint a,.rst-content .wy-alert-neutral.important a,.rst-content .wy-alert-neutral.note a,.rst-content .wy-alert-neutral.seealso a,.rst-content .wy-alert-neutral.tip a,.rst-content .wy-alert-neutral.warning a,.wy-alert.wy-alert-neutral a{color:#2980b9}.rst-content .admonition-todo p:last-child,.rst-content .admonition p:last-child,.rst-content .attention p:last-child,.rst-content .caution p:last-child,.rst-content .danger p:last-child,.rst-content .error p:last-child,.rst-content .hint p:last-child,.rst-content .important p:last-child,.rst-content .note p:last-child,.rst-content .seealso p:last-child,.rst-content .tip p:last-child,.rst-content .warning p:last-child,.wy-alert p:last-child{margin-bottom:0}.wy-tray-container{position:fixed;bottom:0;left:0;z-index:600}.wy-tray-container li{display:block;width:300px;background:transparent;color:#fff;text-align:center;box-shadow:0 5px 5px 0 rgba(0,0,0,.1);padding:0 24px;min-width:20%;opacity:0;height:0;line-height:56px;overflow:hidden;-webkit-transition:all .3s ease-in;-moz-transition:all .3s ease-in;transition:all .3s ease-in}.wy-tray-container li.wy-tray-item-success{background:#27ae60}.wy-tray-container li.wy-tray-item-info{background:#2980b9}.wy-tray-container li.wy-tray-item-warning{background:#e67e22}.wy-tray-container li.wy-tray-item-danger{background:#e74c3c}.wy-tray-container li.on{opacity:1;height:56px}@media screen and (max-width:768px){.wy-tray-container{bottom:auto;top:0;width:100%}.wy-tray-container li{width:100%}}button{font-size:100%;margin:0;vertical-align:baseline;*vertical-align:middle;cursor:pointer;line-height:normal;-webkit-appearance:button;*overflow:visible}button::-moz-focus-inner,input::-moz-focus-inner{border:0;padding:0}button[disabled]{cursor:default}.btn{display:inline-block;border-radius:2px;line-height:normal;white-space:nowrap;text-align:center;cursor:pointer;font-size:100%;padding:6px 12px 8px;color:#fff;border:1px solid rgba(0,0,0,.1);background-color:#27ae60;text-decoration:none;font-weight:400;font-family:Lato,proxima-nova,Helvetica Neue,Arial,sans-serif;box-shadow:inset 0 1px 2px -1px hsla(0,0%,100%,.5),inset 0 -2px 0 0 rgba(0,0,0,.1);outline-none:false;vertical-align:middle;*display:inline;zoom:1;-webkit-user-drag:none;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;-webkit-transition:all .1s linear;-moz-transition:all .1s linear;transition:all .1s linear}.btn-hover{background:#2e8ece;color:#fff}.btn:hover{background:#2cc36b;color:#fff}.btn:focus{background:#2cc36b;outline:0}.btn:active{box-shadow:inset 0 -1px 0 0 rgba(0,0,0,.05),inset 0 2px 0 0 rgba(0,0,0,.1);padding:8px 12px 6px}.btn:visited{color:#fff}.btn-disabled,.btn-disabled:active,.btn-disabled:focus,.btn-disabled:hover,.btn:disabled{background-image:none;filter:progid:DXImageTransform.Microsoft.gradient(enabled = false);filter:alpha(opacity=40);opacity:.4;cursor:not-allowed;box-shadow:none}.btn::-moz-focus-inner{padding:0;border:0}.btn-small{font-size:80%}.btn-info{background-color:#2980b9!important}.btn-info:hover{background-color:#2e8ece!important}.btn-neutral{background-color:#f3f6f6!important;color:#404040!important}.btn-neutral:hover{background-color:#e5ebeb!important;color:#404040}.btn-neutral:visited{color:#404040!important}.btn-success{background-color:#27ae60!important}.btn-success:hover{background-color:#295!important}.btn-danger{background-color:#e74c3c!important}.btn-danger:hover{background-color:#ea6153!important}.btn-warning{background-color:#e67e22!important}.btn-warning:hover{background-color:#e98b39!important}.btn-invert{background-color:#222}.btn-invert:hover{background-color:#2f2f2f!important}.btn-link{background-color:transparent!important;color:#2980b9;box-shadow:none;border-color:transparent!important}.btn-link:active,.btn-link:hover{background-color:transparent!important;color:#409ad5!important;box-shadow:none}.btn-link:visited{color:#9b59b6}.wy-btn-group .btn,.wy-control .btn{vertical-align:middle}.wy-btn-group{margin-bottom:24px;*zoom:1}.wy-btn-group:after,.wy-btn-group:before{display:table;content:""}.wy-btn-group:after{clear:both}.wy-dropdown{position:relative;display:inline-block}.wy-dropdown-active .wy-dropdown-menu{display:block}.wy-dropdown-menu{position:absolute;left:0;display:none;float:left;top:100%;min-width:100%;background:#fcfcfc;z-index:100;border:1px solid #cfd7dd;box-shadow:0 2px 2px 0 rgba(0,0,0,.1);padding:12px}.wy-dropdown-menu>dd>a{display:block;clear:both;color:#404040;white-space:nowrap;font-size:90%;padding:0 12px;cursor:pointer}.wy-dropdown-menu>dd>a:hover{background:#2980b9;color:#fff}.wy-dropdown-menu>dd.divider{border-top:1px solid #cfd7dd;margin:6px 0}.wy-dropdown-menu>dd.search{padding-bottom:12px}.wy-dropdown-menu>dd.search input[type=search]{width:100%}.wy-dropdown-menu>dd.call-to-action{background:#e3e3e3;text-transform:uppercase;font-weight:500;font-size:80%}.wy-dropdown-menu>dd.call-to-action:hover{background:#e3e3e3}.wy-dropdown-menu>dd.call-to-action .btn{color:#fff}.wy-dropdown.wy-dropdown-up .wy-dropdown-menu{bottom:100%;top:auto;left:auto;right:0}.wy-dropdown.wy-dropdown-bubble .wy-dropdown-menu{background:#fcfcfc;margin-top:2px}.wy-dropdown.wy-dropdown-bubble .wy-dropdown-menu a{padding:6px 12px}.wy-dropdown.wy-dropdown-bubble .wy-dropdown-menu a:hover{background:#2980b9;color:#fff}.wy-dropdown.wy-dropdown-left .wy-dropdown-menu{right:0;left:auto;text-align:right}.wy-dropdown-arrow:before{content:" ";border-bottom:5px solid #f5f5f5;border-left:5px solid transparent;border-right:5px solid transparent;position:absolute;display:block;top:-4px;left:50%;margin-left:-3px}.wy-dropdown-arrow.wy-dropdown-arrow-left:before{left:11px}.wy-form-stacked select{display:block}.wy-form-aligned .wy-help-inline,.wy-form-aligned input,.wy-form-aligned label,.wy-form-aligned select,.wy-form-aligned textarea{display:inline-block;*display:inline;*zoom:1;vertical-align:middle}.wy-form-aligned .wy-control-group>label{display:inline-block;vertical-align:middle;width:10em;margin:6px 12px 0 0;float:left}.wy-form-aligned .wy-control{float:left}.wy-form-aligned .wy-control label{display:block}.wy-form-aligned .wy-control select{margin-top:6px}fieldset{margin:0}fieldset,legend{border:0;padding:0}legend{width:100%;white-space:normal;margin-bottom:24px;font-size:150%;*margin-left:-7px}label,legend{display:block}label{margin:0 0 .3125em;color:#333;font-size:90%}input,select,textarea{font-size:100%;margin:0;vertical-align:baseline;*vertical-align:middle}.wy-control-group{margin-bottom:24px;max-width:1200px;margin-left:auto;margin-right:auto;*zoom:1}.wy-control-group:after,.wy-control-group:before{display:table;content:""}.wy-control-group:after{clear:both}.wy-control-group.wy-control-group-required>label:after{content:" *";color:#e74c3c}.wy-control-group .wy-form-full,.wy-control-group .wy-form-halves,.wy-control-group .wy-form-thirds{padding-bottom:12px}.wy-control-group .wy-form-full input[type=color],.wy-control-group .wy-form-full input[type=date],.wy-control-group .wy-form-full input[type=datetime-local],.wy-control-group .wy-form-full input[type=datetime],.wy-control-group .wy-form-full input[type=email],.wy-control-group .wy-form-full input[type=month],.wy-control-group .wy-form-full input[type=number],.wy-control-group .wy-form-full input[type=password],.wy-control-group .wy-form-full input[type=search],.wy-control-group .wy-form-full input[type=tel],.wy-control-group .wy-form-full input[type=text],.wy-control-group .wy-form-full input[type=time],.wy-control-group .wy-form-full input[type=url],.wy-control-group .wy-form-full input[type=week],.wy-control-group .wy-form-full select,.wy-control-group .wy-form-halves input[type=color],.wy-control-group .wy-form-halves input[type=date],.wy-control-group .wy-form-halves input[type=datetime-local],.wy-control-group .wy-form-halves input[type=datetime],.wy-control-group .wy-form-halves input[type=email],.wy-control-group .wy-form-halves input[type=month],.wy-control-group .wy-form-halves input[type=number],.wy-control-group .wy-form-halves input[type=password],.wy-control-group .wy-form-halves input[type=search],.wy-control-group .wy-form-halves input[type=tel],.wy-control-group .wy-form-halves input[type=text],.wy-control-group .wy-form-halves input[type=time],.wy-control-group .wy-form-halves input[type=url],.wy-control-group .wy-form-halves input[type=week],.wy-control-group .wy-form-halves select,.wy-control-group .wy-form-thirds input[type=color],.wy-control-group .wy-form-thirds input[type=date],.wy-control-group .wy-form-thirds input[type=datetime-local],.wy-control-group .wy-form-thirds input[type=datetime],.wy-control-group .wy-form-thirds input[type=email],.wy-control-group .wy-form-thirds input[type=month],.wy-control-group .wy-form-thirds input[type=number],.wy-control-group .wy-form-thirds input[type=password],.wy-control-group .wy-form-thirds input[type=search],.wy-control-group .wy-form-thirds input[type=tel],.wy-control-group .wy-form-thirds input[type=text],.wy-control-group .wy-form-thirds input[type=time],.wy-control-group .wy-form-thirds input[type=url],.wy-control-group .wy-form-thirds input[type=week],.wy-control-group .wy-form-thirds select{width:100%}.wy-control-group .wy-form-full{float:left;display:block;width:100%;margin-right:0}.wy-control-group .wy-form-full:last-child{margin-right:0}.wy-control-group .wy-form-halves{float:left;display:block;margin-right:2.35765%;width:48.82117%}.wy-control-group .wy-form-halves:last-child,.wy-control-group .wy-form-halves:nth-of-type(2n){margin-right:0}.wy-control-group .wy-form-halves:nth-of-type(odd){clear:left}.wy-control-group .wy-form-thirds{float:left;display:block;margin-right:2.35765%;width:31.76157%}.wy-control-group .wy-form-thirds:last-child,.wy-control-group .wy-form-thirds:nth-of-type(3n){margin-right:0}.wy-control-group .wy-form-thirds:nth-of-type(3n+1){clear:left}.wy-control-group.wy-control-group-no-input .wy-control,.wy-control-no-input{margin:6px 0 0;font-size:90%}.wy-control-no-input{display:inline-block}.wy-control-group.fluid-input input[type=color],.wy-control-group.fluid-input input[type=date],.wy-control-group.fluid-input input[type=datetime-local],.wy-control-group.fluid-input input[type=datetime],.wy-control-group.fluid-input input[type=email],.wy-control-group.fluid-input input[type=month],.wy-control-group.fluid-input input[type=number],.wy-control-group.fluid-input input[type=password],.wy-control-group.fluid-input input[type=search],.wy-control-group.fluid-input input[type=tel],.wy-control-group.fluid-input input[type=text],.wy-control-group.fluid-input input[type=time],.wy-control-group.fluid-input input[type=url],.wy-control-group.fluid-input input[type=week]{width:100%}.wy-form-message-inline{padding-left:.3em;color:#666;font-size:90%}.wy-form-message{display:block;color:#999;font-size:70%;margin-top:.3125em;font-style:italic}.wy-form-message p{font-size:inherit;font-style:italic;margin-bottom:6px}.wy-form-message p:last-child{margin-bottom:0}input{line-height:normal}input[type=button],input[type=reset],input[type=submit]{-webkit-appearance:button;cursor:pointer;font-family:Lato,proxima-nova,Helvetica Neue,Arial,sans-serif;*overflow:visible}input[type=color],input[type=date],input[type=datetime-local],input[type=datetime],input[type=email],input[type=month],input[type=number],input[type=password],input[type=search],input[type=tel],input[type=text],input[type=time],input[type=url],input[type=week]{-webkit-appearance:none;padding:6px;display:inline-block;border:1px solid #ccc;font-size:80%;font-family:Lato,proxima-nova,Helvetica Neue,Arial,sans-serif;box-shadow:inset 0 1px 3px #ddd;border-radius:0;-webkit-transition:border .3s linear;-moz-transition:border .3s linear;transition:border .3s linear}input[type=datetime-local]{padding:.34375em .625em}input[disabled]{cursor:default}input[type=checkbox],input[type=radio]{padding:0;margin-right:.3125em;*height:13px;*width:13px}input[type=checkbox],input[type=radio],input[type=search]{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}input[type=search]::-webkit-search-cancel-button,input[type=search]::-webkit-search-decoration{-webkit-appearance:none}input[type=color]:focus,input[type=date]:focus,input[type=datetime-local]:focus,input[type=datetime]:focus,input[type=email]:focus,input[type=month]:focus,input[type=number]:focus,input[type=password]:focus,input[type=search]:focus,input[type=tel]:focus,input[type=text]:focus,input[type=time]:focus,input[type=url]:focus,input[type=week]:focus{outline:0;outline:thin dotted\9;border-color:#333}input.no-focus:focus{border-color:#ccc!important}input[type=checkbox]:focus,input[type=file]:focus,input[type=radio]:focus{outline:thin dotted #333;outline:1px auto #129fea}input[type=color][disabled],input[type=date][disabled],input[type=datetime-local][disabled],input[type=datetime][disabled],input[type=email][disabled],input[type=month][disabled],input[type=number][disabled],input[type=password][disabled],input[type=search][disabled],input[type=tel][disabled],input[type=text][disabled],input[type=time][disabled],input[type=url][disabled],input[type=week][disabled]{cursor:not-allowed;background-color:#fafafa}input:focus:invalid,select:focus:invalid,textarea:focus:invalid{color:#e74c3c;border:1px solid #e74c3c}input:focus:invalid:focus,select:focus:invalid:focus,textarea:focus:invalid:focus{border-color:#e74c3c}input[type=checkbox]:focus:invalid:focus,input[type=file]:focus:invalid:focus,input[type=radio]:focus:invalid:focus{outline-color:#e74c3c}input.wy-input-large{padding:12px;font-size:100%}textarea{overflow:auto;vertical-align:top;width:100%;font-family:Lato,proxima-nova,Helvetica Neue,Arial,sans-serif}select,textarea{padding:.5em .625em;display:inline-block;border:1px solid #ccc;font-size:80%;box-shadow:inset 0 1px 3px #ddd;-webkit-transition:border .3s linear;-moz-transition:border .3s linear;transition:border .3s linear}select{border:1px solid #ccc;background-color:#fff}select[multiple]{height:auto}select:focus,textarea:focus{outline:0}input[readonly],select[disabled],select[readonly],textarea[disabled],textarea[readonly]{cursor:not-allowed;background-color:#fafafa}input[type=checkbox][disabled],input[type=radio][disabled]{cursor:not-allowed}.wy-checkbox,.wy-radio{margin:6px 0;color:#404040;display:block}.wy-checkbox input,.wy-radio input{vertical-align:baseline}.wy-form-message-inline{display:inline-block;*display:inline;*zoom:1;vertical-align:middle}.wy-input-prefix,.wy-input-suffix{white-space:nowrap;padding:6px}.wy-input-prefix .wy-input-context,.wy-input-suffix .wy-input-context{line-height:27px;padding:0 8px;display:inline-block;font-size:80%;background-color:#f3f6f6;border:1px solid #ccc;color:#999}.wy-input-suffix .wy-input-context{border-left:0}.wy-input-prefix .wy-input-context{border-right:0}.wy-switch{position:relative;display:block;height:24px;margin-top:12px;cursor:pointer}.wy-switch:before{left:0;top:0;width:36px;height:12px;background:#ccc}.wy-switch:after,.wy-switch:before{position:absolute;content:"";display:block;border-radius:4px;-webkit-transition:all .2s ease-in-out;-moz-transition:all .2s ease-in-out;transition:all .2s ease-in-out}.wy-switch:after{width:18px;height:18px;background:#999;left:-3px;top:-3px}.wy-switch span{position:absolute;left:48px;display:block;font-size:12px;color:#ccc;line-height:1}.wy-switch.active:before{background:#1e8449}.wy-switch.active:after{left:24px;background:#27ae60}.wy-switch.disabled{cursor:not-allowed;opacity:.8}.wy-control-group.wy-control-group-error .wy-form-message,.wy-control-group.wy-control-group-error>label{color:#e74c3c}.wy-control-group.wy-control-group-error input[type=color],.wy-control-group.wy-control-group-error input[type=date],.wy-control-group.wy-control-group-error input[type=datetime-local],.wy-control-group.wy-control-group-error input[type=datetime],.wy-control-group.wy-control-group-error input[type=email],.wy-control-group.wy-control-group-error input[type=month],.wy-control-group.wy-control-group-error input[type=number],.wy-control-group.wy-control-group-error input[type=password],.wy-control-group.wy-control-group-error input[type=search],.wy-control-group.wy-control-group-error input[type=tel],.wy-control-group.wy-control-group-error input[type=text],.wy-control-group.wy-control-group-error input[type=time],.wy-control-group.wy-control-group-error input[type=url],.wy-control-group.wy-control-group-error input[type=week],.wy-control-group.wy-control-group-error textarea{border:1px solid #e74c3c}.wy-inline-validate{white-space:nowrap}.wy-inline-validate .wy-input-context{padding:.5em .625em;display:inline-block;font-size:80%}.wy-inline-validate.wy-inline-validate-success .wy-input-context{color:#27ae60}.wy-inline-validate.wy-inline-validate-danger .wy-input-context{color:#e74c3c}.wy-inline-validate.wy-inline-validate-warning .wy-input-context{color:#e67e22}.wy-inline-validate.wy-inline-validate-info .wy-input-context{color:#2980b9}.rotate-90{-webkit-transform:rotate(90deg);-moz-transform:rotate(90deg);-ms-transform:rotate(90deg);-o-transform:rotate(90deg);transform:rotate(90deg)}.rotate-180{-webkit-transform:rotate(180deg);-moz-transform:rotate(180deg);-ms-transform:rotate(180deg);-o-transform:rotate(180deg);transform:rotate(180deg)}.rotate-270{-webkit-transform:rotate(270deg);-moz-transform:rotate(270deg);-ms-transform:rotate(270deg);-o-transform:rotate(270deg);transform:rotate(270deg)}.mirror{-webkit-transform:scaleX(-1);-moz-transform:scaleX(-1);-ms-transform:scaleX(-1);-o-transform:scaleX(-1);transform:scaleX(-1)}.mirror.rotate-90{-webkit-transform:scaleX(-1) rotate(90deg);-moz-transform:scaleX(-1) rotate(90deg);-ms-transform:scaleX(-1) rotate(90deg);-o-transform:scaleX(-1) rotate(90deg);transform:scaleX(-1) rotate(90deg)}.mirror.rotate-180{-webkit-transform:scaleX(-1) rotate(180deg);-moz-transform:scaleX(-1) rotate(180deg);-ms-transform:scaleX(-1) rotate(180deg);-o-transform:scaleX(-1) rotate(180deg);transform:scaleX(-1) rotate(180deg)}.mirror.rotate-270{-webkit-transform:scaleX(-1) rotate(270deg);-moz-transform:scaleX(-1) rotate(270deg);-ms-transform:scaleX(-1) rotate(270deg);-o-transform:scaleX(-1) rotate(270deg);transform:scaleX(-1) rotate(270deg)}@media only screen and (max-width:480px){.wy-form button[type=submit]{margin:.7em 0 0}.wy-form input[type=color],.wy-form input[type=date],.wy-form input[type=datetime-local],.wy-form input[type=datetime],.wy-form input[type=email],.wy-form input[type=month],.wy-form input[type=number],.wy-form input[type=password],.wy-form input[type=search],.wy-form input[type=tel],.wy-form input[type=text],.wy-form input[type=time],.wy-form input[type=url],.wy-form input[type=week],.wy-form label{margin-bottom:.3em;display:block}.wy-form input[type=color],.wy-form input[type=date],.wy-form input[type=datetime-local],.wy-form input[type=datetime],.wy-form input[type=email],.wy-form input[type=month],.wy-form input[type=number],.wy-form input[type=password],.wy-form input[type=search],.wy-form input[type=tel],.wy-form input[type=time],.wy-form input[type=url],.wy-form input[type=week]{margin-bottom:0}.wy-form-aligned .wy-control-group label{margin-bottom:.3em;text-align:left;display:block;width:100%}.wy-form-aligned .wy-control{margin:1.5em 0 0}.wy-form-message,.wy-form-message-inline,.wy-form .wy-help-inline{display:block;font-size:80%;padding:6px 0}}@media screen and (max-width:768px){.tablet-hide{display:none}}@media screen and (max-width:480px){.mobile-hide{display:none}}.float-left{float:left}.float-right{float:right}.full-width{width:100%}.rst-content table.docutils,.rst-content table.field-list,.wy-table{border-collapse:collapse;border-spacing:0;empty-cells:show;margin-bottom:24px}.rst-content table.docutils caption,.rst-content table.field-list caption,.wy-table caption{color:#000;font:italic 85%/1 arial,sans-serif;padding:1em 0;text-align:center}.rst-content table.docutils td,.rst-content table.docutils th,.rst-content table.field-list td,.rst-content table.field-list th,.wy-table td,.wy-table th{font-size:90%;margin:0;overflow:visible;padding:8px 16px}.rst-content table.docutils td:first-child,.rst-content table.docutils th:first-child,.rst-content table.field-list td:first-child,.rst-content table.field-list th:first-child,.wy-table td:first-child,.wy-table th:first-child{border-left-width:0}.rst-content table.docutils thead,.rst-content table.field-list thead,.wy-table thead{color:#000;text-align:left;vertical-align:bottom;white-space:nowrap}.rst-content table.docutils thead th,.rst-content table.field-list thead th,.wy-table thead th{font-weight:700;border-bottom:2px solid #e1e4e5}.rst-content table.docutils td,.rst-content table.field-list td,.wy-table td{background-color:transparent;vertical-align:middle}.rst-content table.docutils td p,.rst-content table.field-list td p,.wy-table td p{line-height:18px}.rst-content table.docutils td p:last-child,.rst-content table.field-list td p:last-child,.wy-table td p:last-child{margin-bottom:0}.rst-content table.docutils .wy-table-cell-min,.rst-content table.field-list .wy-table-cell-min,.wy-table .wy-table-cell-min{width:1%;padding-right:0}.rst-content table.docutils .wy-table-cell-min input[type=checkbox],.rst-content table.field-list .wy-table-cell-min input[type=checkbox],.wy-table .wy-table-cell-min input[type=checkbox]{margin:0}.wy-table-secondary{color:grey;font-size:90%}.wy-table-tertiary{color:grey;font-size:80%}.rst-content table.docutils:not(.field-list) tr:nth-child(2n-1) td,.wy-table-backed,.wy-table-odd td,.wy-table-striped tr:nth-child(2n-1) td{background-color:#f3f6f6}.rst-content table.docutils,.wy-table-bordered-all{border:1px solid #e1e4e5}.rst-content table.docutils td,.wy-table-bordered-all td{border-bottom:1px solid #e1e4e5;border-left:1px solid #e1e4e5}.rst-content table.docutils tbody>tr:last-child td,.wy-table-bordered-all tbody>tr:last-child td{border-bottom-width:0}.wy-table-bordered{border:1px solid #e1e4e5}.wy-table-bordered-rows td{border-bottom:1px solid #e1e4e5}.wy-table-bordered-rows tbody>tr:last-child td{border-bottom-width:0}.wy-table-horizontal td,.wy-table-horizontal th{border-width:0 0 1px;border-bottom:1px solid #e1e4e5}.wy-table-horizontal tbody>tr:last-child td{border-bottom-width:0}.wy-table-responsive{margin-bottom:24px;max-width:100%;overflow:auto}.wy-table-responsive table{margin-bottom:0!important}.wy-table-responsive table td,.wy-table-responsive table th{white-space:nowrap}a{color:#2980b9;text-decoration:none;cursor:pointer}a:hover{color:#3091d1}a:visited{color:#9b59b6}html{height:100%}body,html{overflow-x:hidden}body{font-family:Lato,proxima-nova,Helvetica Neue,Arial,sans-serif;font-weight:400;color:#404040;min-height:100%;background:#edf0f2}.wy-text-left{text-align:left}.wy-text-center{text-align:center}.wy-text-right{text-align:right}.wy-text-large{font-size:120%}.wy-text-normal{font-size:100%}.wy-text-small,small{font-size:80%}.wy-text-strike{text-decoration:line-through}.wy-text-warning{color:#e67e22!important}a.wy-text-warning:hover{color:#eb9950!important}.wy-text-info{color:#2980b9!important}a.wy-text-info:hover{color:#409ad5!important}.wy-text-success{color:#27ae60!important}a.wy-text-success:hover{color:#36d278!important}.wy-text-danger{color:#e74c3c!important}a.wy-text-danger:hover{color:#ed7669!important}.wy-text-neutral{color:#404040!important}a.wy-text-neutral:hover{color:#595959!important}.rst-content .toctree-wrapper>p.caption,h1,h2,h3,h4,h5,h6,legend{margin-top:0;font-weight:700;font-family:Roboto Slab,ff-tisa-web-pro,Georgia,Arial,sans-serif}p{line-height:24px;font-size:16px;margin:0 0 24px}h1{font-size:175%}.rst-content .toctree-wrapper>p.caption,h2{font-size:150%}h3{font-size:125%}h4{font-size:115%}h5{font-size:110%}h6{font-size:100%}hr{display:block;height:1px;border:0;border-top:1px solid #e1e4e5;margin:24px 0;padding:0}.rst-content code,.rst-content tt,code{white-space:nowrap;max-width:100%;background:#fff;border:1px solid #e1e4e5;font-size:75%;padding:0 5px;font-family:SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,Courier,monospace;color:#e74c3c;overflow-x:auto}.rst-content tt.code-large,code.code-large{font-size:90%}.rst-content .section ul,.rst-content .toctree-wrapper ul,.rst-content section ul,.wy-plain-list-disc,article ul{list-style:disc;line-height:24px;margin-bottom:24px}.rst-content .section ul li,.rst-content .toctree-wrapper ul li,.rst-content section ul li,.wy-plain-list-disc li,article ul li{list-style:disc;margin-left:24px}.rst-content .section ul li p:last-child,.rst-content .section ul li ul,.rst-content .toctree-wrapper ul li p:last-child,.rst-content .toctree-wrapper ul li ul,.rst-content section ul li p:last-child,.rst-content section ul li ul,.wy-plain-list-disc li p:last-child,.wy-plain-list-disc li ul,article ul li p:last-child,article ul li ul{margin-bottom:0}.rst-content .section ul li li,.rst-content .toctree-wrapper ul li li,.rst-content section ul li li,.wy-plain-list-disc li li,article ul li li{list-style:circle}.rst-content .section ul li li li,.rst-content .toctree-wrapper ul li li li,.rst-content section ul li li li,.wy-plain-list-disc li li li,article ul li li li{list-style:square}.rst-content .section ul li ol li,.rst-content .toctree-wrapper ul li ol li,.rst-content section ul li ol li,.wy-plain-list-disc li ol li,article ul li ol li{list-style:decimal}.rst-content .section ol,.rst-content .section ol.arabic,.rst-content .toctree-wrapper ol,.rst-content .toctree-wrapper ol.arabic,.rst-content section ol,.rst-content section ol.arabic,.wy-plain-list-decimal,article ol{list-style:decimal;line-height:24px;margin-bottom:24px}.rst-content .section ol.arabic li,.rst-content .section ol li,.rst-content .toctree-wrapper ol.arabic li,.rst-content .toctree-wrapper ol li,.rst-content section ol.arabic li,.rst-content section ol li,.wy-plain-list-decimal li,article ol li{list-style:decimal;margin-left:24px}.rst-content .section ol.arabic li ul,.rst-content .section ol li p:last-child,.rst-content .section ol li ul,.rst-content .toctree-wrapper ol.arabic li ul,.rst-content .toctree-wrapper ol li p:last-child,.rst-content .toctree-wrapper ol li ul,.rst-content section ol.arabic li ul,.rst-content section ol li p:last-child,.rst-content section ol li ul,.wy-plain-list-decimal li p:last-child,.wy-plain-list-decimal li ul,article ol li p:last-child,article ol li ul{margin-bottom:0}.rst-content .section ol.arabic li ul li,.rst-content .section ol li ul li,.rst-content .toctree-wrapper ol.arabic li ul li,.rst-content .toctree-wrapper ol li ul li,.rst-content section ol.arabic li ul li,.rst-content section ol li ul li,.wy-plain-list-decimal li ul li,article ol li ul li{list-style:disc}.wy-breadcrumbs{*zoom:1}.wy-breadcrumbs:after,.wy-breadcrumbs:before{display:table;content:""}.wy-breadcrumbs:after{clear:both}.wy-breadcrumbs>li{display:inline-block;padding-top:5px}.wy-breadcrumbs>li.wy-breadcrumbs-aside{float:right}.rst-content .wy-breadcrumbs>li code,.rst-content .wy-breadcrumbs>li tt,.wy-breadcrumbs>li .rst-content tt,.wy-breadcrumbs>li code{all:inherit;color:inherit}.breadcrumb-item:before{content:"/";color:#bbb;font-size:13px;padding:0 6px 0 3px}.wy-breadcrumbs-extra{margin-bottom:0;color:#b3b3b3;font-size:80%;display:inline-block}@media screen and (max-width:480px){.wy-breadcrumbs-extra,.wy-breadcrumbs li.wy-breadcrumbs-aside{display:none}}@media print{.wy-breadcrumbs li.wy-breadcrumbs-aside{display:none}}html{font-size:16px}.wy-affix{position:fixed;top:1.618em}.wy-menu a:hover{text-decoration:none}.wy-menu-horiz{*zoom:1}.wy-menu-horiz:after,.wy-menu-horiz:before{display:table;content:""}.wy-menu-horiz:after{clear:both}.wy-menu-horiz li,.wy-menu-horiz ul{display:inline-block}.wy-menu-horiz li:hover{background:hsla(0,0%,100%,.1)}.wy-menu-horiz li.divide-left{border-left:1px solid #404040}.wy-menu-horiz li.divide-right{border-right:1px solid #404040}.wy-menu-horiz a{height:32px;display:inline-block;line-height:32px;padding:0 16px}.wy-menu-vertical{width:300px}.wy-menu-vertical header,.wy-menu-vertical p.caption{color:#55a5d9;height:32px;line-height:32px;padding:0 1.618em;margin:12px 0 0;display:block;font-weight:700;text-transform:uppercase;font-size:85%;white-space:nowrap}.wy-menu-vertical ul{margin-bottom:0}.wy-menu-vertical li.divide-top{border-top:1px solid #404040}.wy-menu-vertical li.divide-bottom{border-bottom:1px solid #404040}.wy-menu-vertical li.current{background:#e3e3e3}.wy-menu-vertical li.current a{color:grey;border-right:1px solid #c9c9c9;padding:.4045em 2.427em}.wy-menu-vertical li.current a:hover{background:#d6d6d6}.rst-content .wy-menu-vertical li tt,.wy-menu-vertical li .rst-content tt,.wy-menu-vertical li code{border:none;background:inherit;color:inherit;padding-left:0;padding-right:0}.wy-menu-vertical li button.toctree-expand{display:block;float:left;margin-left:-1.2em;line-height:18px;color:#4d4d4d;border:none;background:none;padding:0}.wy-menu-vertical li.current>a,.wy-menu-vertical li.on a{color:#404040;font-weight:700;position:relative;background:#fcfcfc;border:none;padding:.4045em 1.618em}.wy-menu-vertical li.current>a:hover,.wy-menu-vertical li.on a:hover{background:#fcfcfc}.wy-menu-vertical li.current>a:hover button.toctree-expand,.wy-menu-vertical li.on a:hover button.toctree-expand{color:grey}.wy-menu-vertical li.current>a button.toctree-expand,.wy-menu-vertical li.on a button.toctree-expand{display:block;line-height:18px;color:#333}.wy-menu-vertical li.toctree-l1.current>a{border-bottom:1px solid #c9c9c9;border-top:1px solid #c9c9c9}.wy-menu-vertical .toctree-l1.current .toctree-l2>ul,.wy-menu-vertical .toctree-l2.current .toctree-l3>ul,.wy-menu-vertical .toctree-l3.current .toctree-l4>ul,.wy-menu-vertical .toctree-l4.current .toctree-l5>ul,.wy-menu-vertical .toctree-l5.current .toctree-l6>ul,.wy-menu-vertical .toctree-l6.current .toctree-l7>ul,.wy-menu-vertical .toctree-l7.current .toctree-l8>ul,.wy-menu-vertical .toctree-l8.current .toctree-l9>ul,.wy-menu-vertical .toctree-l9.current .toctree-l10>ul,.wy-menu-vertical .toctree-l10.current .toctree-l11>ul{display:none}.wy-menu-vertical .toctree-l1.current .current.toctree-l2>ul,.wy-menu-vertical .toctree-l2.current .current.toctree-l3>ul,.wy-menu-vertical .toctree-l3.current .current.toctree-l4>ul,.wy-menu-vertical .toctree-l4.current .current.toctree-l5>ul,.wy-menu-vertical .toctree-l5.current .current.toctree-l6>ul,.wy-menu-vertical .toctree-l6.current .current.toctree-l7>ul,.wy-menu-vertical .toctree-l7.current .current.toctree-l8>ul,.wy-menu-vertical .toctree-l8.current .current.toctree-l9>ul,.wy-menu-vertical .toctree-l9.current .current.toctree-l10>ul,.wy-menu-vertical .toctree-l10.current .current.toctree-l11>ul{display:block}.wy-menu-vertical li.toctree-l3,.wy-menu-vertical li.toctree-l4{font-size:.9em}.wy-menu-vertical li.toctree-l2 a,.wy-menu-vertical li.toctree-l3 a,.wy-menu-vertical li.toctree-l4 a,.wy-menu-vertical li.toctree-l5 a,.wy-menu-vertical li.toctree-l6 a,.wy-menu-vertical li.toctree-l7 a,.wy-menu-vertical li.toctree-l8 a,.wy-menu-vertical li.toctree-l9 a,.wy-menu-vertical li.toctree-l10 a{color:#404040}.wy-menu-vertical li.toctree-l2 a:hover button.toctree-expand,.wy-menu-vertical li.toctree-l3 a:hover button.toctree-expand,.wy-menu-vertical li.toctree-l4 a:hover button.toctree-expand,.wy-menu-vertical li.toctree-l5 a:hover button.toctree-expand,.wy-menu-vertical li.toctree-l6 a:hover button.toctree-expand,.wy-menu-vertical li.toctree-l7 a:hover button.toctree-expand,.wy-menu-vertical li.toctree-l8 a:hover button.toctree-expand,.wy-menu-vertical li.toctree-l9 a:hover button.toctree-expand,.wy-menu-vertical li.toctree-l10 a:hover button.toctree-expand{color:grey}.wy-menu-vertical li.toctree-l2.current li.toctree-l3>a,.wy-menu-vertical li.toctree-l3.current li.toctree-l4>a,.wy-menu-vertical li.toctree-l4.current li.toctree-l5>a,.wy-menu-vertical li.toctree-l5.current li.toctree-l6>a,.wy-menu-vertical li.toctree-l6.current li.toctree-l7>a,.wy-menu-vertical li.toctree-l7.current li.toctree-l8>a,.wy-menu-vertical li.toctree-l8.current li.toctree-l9>a,.wy-menu-vertical li.toctree-l9.current li.toctree-l10>a,.wy-menu-vertical li.toctree-l10.current li.toctree-l11>a{display:block}.wy-menu-vertical li.toctree-l2.current>a{padding:.4045em 2.427em}.wy-menu-vertical li.toctree-l2.current li.toctree-l3>a{padding:.4045em 1.618em .4045em 4.045em}.wy-menu-vertical li.toctree-l3.current>a{padding:.4045em 4.045em}.wy-menu-vertical li.toctree-l3.current li.toctree-l4>a{padding:.4045em 1.618em .4045em 5.663em}.wy-menu-vertical li.toctree-l4.current>a{padding:.4045em 5.663em}.wy-menu-vertical li.toctree-l4.current li.toctree-l5>a{padding:.4045em 1.618em .4045em 7.281em}.wy-menu-vertical li.toctree-l5.current>a{padding:.4045em 7.281em}.wy-menu-vertical li.toctree-l5.current li.toctree-l6>a{padding:.4045em 1.618em .4045em 8.899em}.wy-menu-vertical li.toctree-l6.current>a{padding:.4045em 8.899em}.wy-menu-vertical li.toctree-l6.current li.toctree-l7>a{padding:.4045em 1.618em .4045em 10.517em}.wy-menu-vertical li.toctree-l7.current>a{padding:.4045em 10.517em}.wy-menu-vertical li.toctree-l7.current li.toctree-l8>a{padding:.4045em 1.618em .4045em 12.135em}.wy-menu-vertical li.toctree-l8.current>a{padding:.4045em 12.135em}.wy-menu-vertical li.toctree-l8.current li.toctree-l9>a{padding:.4045em 1.618em .4045em 13.753em}.wy-menu-vertical li.toctree-l9.current>a{padding:.4045em 13.753em}.wy-menu-vertical li.toctree-l9.current li.toctree-l10>a{padding:.4045em 1.618em .4045em 15.371em}.wy-menu-vertical li.toctree-l10.current>a{padding:.4045em 15.371em}.wy-menu-vertical li.toctree-l10.current li.toctree-l11>a{padding:.4045em 1.618em .4045em 16.989em}.wy-menu-vertical li.toctree-l2.current>a,.wy-menu-vertical li.toctree-l2.current li.toctree-l3>a{background:#c9c9c9}.wy-menu-vertical li.toctree-l2 button.toctree-expand{color:#a3a3a3}.wy-menu-vertical li.toctree-l3.current>a,.wy-menu-vertical li.toctree-l3.current li.toctree-l4>a{background:#bdbdbd}.wy-menu-vertical li.toctree-l3 button.toctree-expand{color:#969696}.wy-menu-vertical li.current ul{display:block}.wy-menu-vertical li ul{margin-bottom:0;display:none}.wy-menu-vertical li ul li a{margin-bottom:0;color:#d9d9d9;font-weight:400}.wy-menu-vertical a{line-height:18px;padding:.4045em 1.618em;display:block;position:relative;font-size:90%;color:#d9d9d9}.wy-menu-vertical a:hover{background-color:#4e4a4a;cursor:pointer}.wy-menu-vertical a:hover button.toctree-expand{color:#d9d9d9}.wy-menu-vertical a:active{background-color:#2980b9;cursor:pointer;color:#fff}.wy-menu-vertical a:active button.toctree-expand{color:#fff}.wy-side-nav-search{display:block;width:300px;padding:.809em;margin-bottom:.809em;z-index:200;background-color:#2980b9;text-align:center;color:#fcfcfc}.wy-side-nav-search input[type=text]{width:100%;border-radius:50px;padding:6px 12px;border-color:#2472a4}.wy-side-nav-search img{display:block;margin:auto auto .809em;height:45px;width:45px;background-color:#2980b9;padding:5px;border-radius:100%}.wy-side-nav-search .wy-dropdown>a,.wy-side-nav-search>a{color:#fcfcfc;font-size:100%;font-weight:700;display:inline-block;padding:4px 6px;margin-bottom:.809em;max-width:100%}.wy-side-nav-search .wy-dropdown>a:hover,.wy-side-nav-search>a:hover{background:hsla(0,0%,100%,.1)}.wy-side-nav-search .wy-dropdown>a img.logo,.wy-side-nav-search>a img.logo{display:block;margin:0 auto;height:auto;width:auto;border-radius:0;max-width:100%;background:transparent}.wy-side-nav-search .wy-dropdown>a.icon img.logo,.wy-side-nav-search>a.icon img.logo{margin-top:.85em}.wy-side-nav-search>div.version{margin-top:-.4045em;margin-bottom:.809em;font-weight:400;color:hsla(0,0%,100%,.3)}.wy-nav .wy-menu-vertical header{color:#2980b9}.wy-nav .wy-menu-vertical a{color:#b3b3b3}.wy-nav .wy-menu-vertical a:hover{background-color:#2980b9;color:#fff}[data-menu-wrap]{-webkit-transition:all .2s ease-in;-moz-transition:all .2s ease-in;transition:all .2s ease-in;position:absolute;opacity:1;width:100%;opacity:0}[data-menu-wrap].move-center{left:0;right:auto;opacity:1}[data-menu-wrap].move-left{right:auto;left:-100%;opacity:0}[data-menu-wrap].move-right{right:-100%;left:auto;opacity:0}.wy-body-for-nav{background:#fcfcfc}.wy-grid-for-nav{position:absolute;width:100%;height:100%}.wy-nav-side{position:fixed;top:0;bottom:0;left:0;padding-bottom:2em;width:300px;overflow-x:hidden;overflow-y:hidden;min-height:100%;color:#9b9b9b;background:#343131;z-index:200}.wy-side-scroll{width:320px;position:relative;overflow-x:hidden;overflow-y:scroll;height:100%}.wy-nav-top{display:none;background:#2980b9;color:#fff;padding:.4045em .809em;position:relative;line-height:50px;text-align:center;font-size:100%;*zoom:1}.wy-nav-top:after,.wy-nav-top:before{display:table;content:""}.wy-nav-top:after{clear:both}.wy-nav-top a{color:#fff;font-weight:700}.wy-nav-top img{margin-right:12px;height:45px;width:45px;background-color:#2980b9;padding:5px;border-radius:100%}.wy-nav-top i{font-size:30px;float:left;cursor:pointer;padding-top:inherit}.wy-nav-content-wrap{margin-left:300px;background:#fcfcfc;min-height:100%}.wy-nav-content{padding:1.618em 3.236em;height:100%;max-width:800px;margin:auto}.wy-body-mask{position:fixed;width:100%;height:100%;background:rgba(0,0,0,.2);display:none;z-index:499}.wy-body-mask.on{display:block}footer{color:grey}footer p{margin-bottom:12px}.rst-content footer span.commit tt,footer span.commit .rst-content tt,footer span.commit code{padding:0;font-family:SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,Courier,monospace;font-size:1em;background:none;border:none;color:grey}.rst-footer-buttons{*zoom:1}.rst-footer-buttons:after,.rst-footer-buttons:before{width:100%;display:table;content:""}.rst-footer-buttons:after{clear:both}.rst-breadcrumbs-buttons{margin-top:12px;*zoom:1}.rst-breadcrumbs-buttons:after,.rst-breadcrumbs-buttons:before{display:table;content:""}.rst-breadcrumbs-buttons:after{clear:both}#search-results .search li{margin-bottom:24px;border-bottom:1px solid #e1e4e5;padding-bottom:24px}#search-results .search li:first-child{border-top:1px solid #e1e4e5;padding-top:24px}#search-results .search li a{font-size:120%;margin-bottom:12px;display:inline-block}#search-results .context{color:grey;font-size:90%}.genindextable li>ul{margin-left:24px}@media screen and (max-width:768px){.wy-body-for-nav{background:#fcfcfc}.wy-nav-top{display:block}.wy-nav-side{left:-300px}.wy-nav-side.shift{width:85%;left:0}.wy-menu.wy-menu-vertical,.wy-side-nav-search,.wy-side-scroll{width:auto}.wy-nav-content-wrap{margin-left:0}.wy-nav-content-wrap .wy-nav-content{padding:1.618em}.wy-nav-content-wrap.shift{position:fixed;min-width:100%;left:85%;top:0;height:100%;overflow:hidden}}@media screen and (min-width:1100px){.wy-nav-content-wrap{background:rgba(0,0,0,.05)}.wy-nav-content{margin:0;background:#fcfcfc}}@media print{.rst-versions,.wy-nav-side,footer{display:none}.wy-nav-content-wrap{margin-left:0}}.rst-versions{position:fixed;bottom:0;left:0;width:300px;color:#fcfcfc;background:#1f1d1d;font-family:Lato,proxima-nova,Helvetica Neue,Arial,sans-serif;z-index:400}.rst-versions a{color:#2980b9;text-decoration:none}.rst-versions .rst-badge-small{display:none}.rst-versions .rst-current-version{padding:12px;background-color:#272525;display:block;text-align:right;font-size:90%;cursor:pointer;color:#27ae60;*zoom:1}.rst-versions .rst-current-version:after,.rst-versions .rst-current-version:before{display:table;content:""}.rst-versions .rst-current-version:after{clear:both}.rst-content .code-block-caption .rst-versions .rst-current-version .headerlink,.rst-content .eqno .rst-versions .rst-current-version .headerlink,.rst-content .rst-versions .rst-current-version .admonition-title,.rst-content code.download .rst-versions .rst-current-version span:first-child,.rst-content dl dt .rst-versions .rst-current-version .headerlink,.rst-content h1 .rst-versions .rst-current-version .headerlink,.rst-content h2 .rst-versions .rst-current-version .headerlink,.rst-content h3 .rst-versions .rst-current-version .headerlink,.rst-content h4 .rst-versions .rst-current-version .headerlink,.rst-content h5 .rst-versions .rst-current-version .headerlink,.rst-content h6 .rst-versions .rst-current-version .headerlink,.rst-content p .rst-versions .rst-current-version .headerlink,.rst-content table>caption .rst-versions .rst-current-version .headerlink,.rst-content tt.download .rst-versions .rst-current-version span:first-child,.rst-versions .rst-current-version .fa,.rst-versions .rst-current-version .icon,.rst-versions .rst-current-version .rst-content .admonition-title,.rst-versions .rst-current-version .rst-content .code-block-caption .headerlink,.rst-versions .rst-current-version .rst-content .eqno .headerlink,.rst-versions .rst-current-version .rst-content code.download span:first-child,.rst-versions .rst-current-version .rst-content dl dt .headerlink,.rst-versions .rst-current-version .rst-content h1 .headerlink,.rst-versions .rst-current-version .rst-content h2 .headerlink,.rst-versions .rst-current-version .rst-content h3 .headerlink,.rst-versions .rst-current-version .rst-content h4 .headerlink,.rst-versions .rst-current-version .rst-content h5 .headerlink,.rst-versions .rst-current-version .rst-content h6 .headerlink,.rst-versions .rst-current-version .rst-content p .headerlink,.rst-versions .rst-current-version .rst-content table>caption .headerlink,.rst-versions .rst-current-version .rst-content tt.download span:first-child,.rst-versions .rst-current-version .wy-menu-vertical li button.toctree-expand,.wy-menu-vertical li .rst-versions .rst-current-version button.toctree-expand{color:#fcfcfc}.rst-versions .rst-current-version .fa-book,.rst-versions .rst-current-version .icon-book{float:left}.rst-versions .rst-current-version.rst-out-of-date{background-color:#e74c3c;color:#fff}.rst-versions .rst-current-version.rst-active-old-version{background-color:#f1c40f;color:#000}.rst-versions.shift-up{height:auto;max-height:100%;overflow-y:scroll}.rst-versions.shift-up .rst-other-versions{display:block}.rst-versions .rst-other-versions{font-size:90%;padding:12px;color:grey;display:none}.rst-versions .rst-other-versions hr{display:block;height:1px;border:0;margin:20px 0;padding:0;border-top:1px solid #413d3d}.rst-versions .rst-other-versions dd{display:inline-block;margin:0}.rst-versions .rst-other-versions dd a{display:inline-block;padding:6px;color:#fcfcfc}.rst-versions.rst-badge{width:auto;bottom:20px;right:20px;left:auto;border:none;max-width:300px;max-height:90%}.rst-versions.rst-badge .fa-book,.rst-versions.rst-badge .icon-book{float:none;line-height:30px}.rst-versions.rst-badge.shift-up .rst-current-version{text-align:right}.rst-versions.rst-badge.shift-up .rst-current-version .fa-book,.rst-versions.rst-badge.shift-up .rst-current-version .icon-book{float:left}.rst-versions.rst-badge>.rst-current-version{width:auto;height:30px;line-height:30px;padding:0 6px;display:block;text-align:center}@media screen and (max-width:768px){.rst-versions{width:85%;display:none}.rst-versions.shift{display:block}}.rst-content .toctree-wrapper>p.caption,.rst-content h1,.rst-content h2,.rst-content h3,.rst-content h4,.rst-content h5,.rst-content h6{margin-bottom:24px}.rst-content img{max-width:100%;height:auto}.rst-content div.figure,.rst-content figure{margin-bottom:24px}.rst-content div.figure .caption-text,.rst-content figure .caption-text{font-style:italic}.rst-content div.figure p:last-child.caption,.rst-content figure p:last-child.caption{margin-bottom:0}.rst-content div.figure.align-center,.rst-content figure.align-center{text-align:center}.rst-content .section>a>img,.rst-content .section>img,.rst-content section>a>img,.rst-content section>img{margin-bottom:24px}.rst-content abbr[title]{text-decoration:none}.rst-content.style-external-links a.reference.external:after{font-family:FontAwesome;content:"\f08e";color:#b3b3b3;vertical-align:super;font-size:60%;margin:0 .2em}.rst-content blockquote{margin-left:24px;line-height:24px;margin-bottom:24px}.rst-content pre.literal-block{white-space:pre;margin:0;padding:12px;font-family:SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,Courier,monospace;display:block;overflow:auto}.rst-content div[class^=highlight],.rst-content pre.literal-block{border:1px solid #e1e4e5;overflow-x:auto;margin:1px 0 24px}.rst-content div[class^=highlight] div[class^=highlight],.rst-content pre.literal-block div[class^=highlight]{padding:0;border:none;margin:0}.rst-content div[class^=highlight] td.code{width:100%}.rst-content .linenodiv pre{border-right:1px solid #e6e9ea;margin:0;padding:12px;font-family:SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,Courier,monospace;user-select:none;pointer-events:none}.rst-content div[class^=highlight] pre{white-space:pre;margin:0;padding:12px;display:block;overflow:auto}.rst-content div[class^=highlight] pre .hll{display:block;margin:0 -12px;padding:0 12px}.rst-content .linenodiv pre,.rst-content div[class^=highlight] pre,.rst-content pre.literal-block{font-family:SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,Courier,monospace;font-size:12px;line-height:1.4}.rst-content div.highlight .gp,.rst-content div.highlight span.linenos{user-select:none;pointer-events:none}.rst-content div.highlight span.linenos{display:inline-block;padding-left:0;padding-right:12px;margin-right:12px;border-right:1px solid #e6e9ea}.rst-content .code-block-caption{font-style:italic;font-size:85%;line-height:1;padding:1em 0;text-align:center}@media print{.rst-content .codeblock,.rst-content div[class^=highlight],.rst-content div[class^=highlight] pre{white-space:pre-wrap}}.rst-content .admonition,.rst-content .admonition-todo,.rst-content .attention,.rst-content .caution,.rst-content .danger,.rst-content .error,.rst-content .hint,.rst-content .important,.rst-content .note,.rst-content .seealso,.rst-content .tip,.rst-content .warning{clear:both}.rst-content .admonition-todo .last,.rst-content .admonition-todo>:last-child,.rst-content .admonition .last,.rst-content .admonition>:last-child,.rst-content .attention .last,.rst-content .attention>:last-child,.rst-content .caution .last,.rst-content .caution>:last-child,.rst-content .danger .last,.rst-content .danger>:last-child,.rst-content .error .last,.rst-content .error>:last-child,.rst-content .hint .last,.rst-content .hint>:last-child,.rst-content .important .last,.rst-content .important>:last-child,.rst-content .note .last,.rst-content .note>:last-child,.rst-content .seealso .last,.rst-content .seealso>:last-child,.rst-content .tip .last,.rst-content .tip>:last-child,.rst-content .warning .last,.rst-content .warning>:last-child{margin-bottom:0}.rst-content .admonition-title:before{margin-right:4px}.rst-content .admonition table{border-color:rgba(0,0,0,.1)}.rst-content .admonition table td,.rst-content .admonition table th{background:transparent!important;border-color:rgba(0,0,0,.1)!important}.rst-content .section ol.loweralpha,.rst-content .section ol.loweralpha>li,.rst-content .toctree-wrapper ol.loweralpha,.rst-content .toctree-wrapper ol.loweralpha>li,.rst-content section ol.loweralpha,.rst-content section ol.loweralpha>li{list-style:lower-alpha}.rst-content .section ol.upperalpha,.rst-content .section ol.upperalpha>li,.rst-content .toctree-wrapper ol.upperalpha,.rst-content .toctree-wrapper ol.upperalpha>li,.rst-content section ol.upperalpha,.rst-content section ol.upperalpha>li{list-style:upper-alpha}.rst-content .section ol li>*,.rst-content .section ul li>*,.rst-content .toctree-wrapper ol li>*,.rst-content .toctree-wrapper ul li>*,.rst-content section ol li>*,.rst-content section ul li>*{margin-top:12px;margin-bottom:12px}.rst-content .section ol li>:first-child,.rst-content .section ul li>:first-child,.rst-content .toctree-wrapper ol li>:first-child,.rst-content .toctree-wrapper ul li>:first-child,.rst-content section ol li>:first-child,.rst-content section ul li>:first-child{margin-top:0}.rst-content .section ol li>p,.rst-content .section ol li>p:last-child,.rst-content .section ul li>p,.rst-content .section ul li>p:last-child,.rst-content .toctree-wrapper ol li>p,.rst-content .toctree-wrapper ol li>p:last-child,.rst-content .toctree-wrapper ul li>p,.rst-content .toctree-wrapper ul li>p:last-child,.rst-content section ol li>p,.rst-content section ol li>p:last-child,.rst-content section ul li>p,.rst-content section ul li>p:last-child{margin-bottom:12px}.rst-content .section ol li>p:only-child,.rst-content .section ol li>p:only-child:last-child,.rst-content .section ul li>p:only-child,.rst-content .section ul li>p:only-child:last-child,.rst-content .toctree-wrapper ol li>p:only-child,.rst-content .toctree-wrapper ol li>p:only-child:last-child,.rst-content .toctree-wrapper ul li>p:only-child,.rst-content .toctree-wrapper ul li>p:only-child:last-child,.rst-content section ol li>p:only-child,.rst-content section ol li>p:only-child:last-child,.rst-content section ul li>p:only-child,.rst-content section ul li>p:only-child:last-child{margin-bottom:0}.rst-content .section ol li>ol,.rst-content .section ol li>ul,.rst-content .section ul li>ol,.rst-content .section ul li>ul,.rst-content .toctree-wrapper ol li>ol,.rst-content .toctree-wrapper ol li>ul,.rst-content .toctree-wrapper ul li>ol,.rst-content .toctree-wrapper ul li>ul,.rst-content section ol li>ol,.rst-content section ol li>ul,.rst-content section ul li>ol,.rst-content section ul li>ul{margin-bottom:12px}.rst-content .section ol.simple li>*,.rst-content .section ol.simple li ol,.rst-content .section ol.simple li ul,.rst-content .section ul.simple li>*,.rst-content .section ul.simple li ol,.rst-content .section ul.simple li ul,.rst-content .toctree-wrapper ol.simple li>*,.rst-content .toctree-wrapper ol.simple li ol,.rst-content .toctree-wrapper ol.simple li ul,.rst-content .toctree-wrapper ul.simple li>*,.rst-content .toctree-wrapper ul.simple li ol,.rst-content .toctree-wrapper ul.simple li ul,.rst-content section ol.simple li>*,.rst-content section ol.simple li ol,.rst-content section ol.simple li ul,.rst-content section ul.simple li>*,.rst-content section ul.simple li ol,.rst-content section ul.simple li ul{margin-top:0;margin-bottom:0}.rst-content .line-block{margin-left:0;margin-bottom:24px;line-height:24px}.rst-content .line-block .line-block{margin-left:24px;margin-bottom:0}.rst-content .topic-title{font-weight:700;margin-bottom:12px}.rst-content .toc-backref{color:#404040}.rst-content .align-right{float:right;margin:0 0 24px 24px}.rst-content .align-left{float:left;margin:0 24px 24px 0}.rst-content .align-center{margin:auto}.rst-content .align-center:not(table){display:block}.rst-content .code-block-caption .headerlink,.rst-content .eqno .headerlink,.rst-content .toctree-wrapper>p.caption .headerlink,.rst-content dl dt .headerlink,.rst-content h1 .headerlink,.rst-content h2 .headerlink,.rst-content h3 .headerlink,.rst-content h4 .headerlink,.rst-content h5 .headerlink,.rst-content h6 .headerlink,.rst-content p.caption .headerlink,.rst-content p .headerlink,.rst-content table>caption .headerlink{opacity:0;font-size:14px;font-family:FontAwesome;margin-left:.5em}.rst-content .code-block-caption .headerlink:focus,.rst-content .code-block-caption:hover .headerlink,.rst-content .eqno .headerlink:focus,.rst-content .eqno:hover .headerlink,.rst-content .toctree-wrapper>p.caption .headerlink:focus,.rst-content .toctree-wrapper>p.caption:hover .headerlink,.rst-content dl dt .headerlink:focus,.rst-content dl dt:hover .headerlink,.rst-content h1 .headerlink:focus,.rst-content h1:hover .headerlink,.rst-content h2 .headerlink:focus,.rst-content h2:hover .headerlink,.rst-content h3 .headerlink:focus,.rst-content h3:hover .headerlink,.rst-content h4 .headerlink:focus,.rst-content h4:hover .headerlink,.rst-content h5 .headerlink:focus,.rst-content h5:hover .headerlink,.rst-content h6 .headerlink:focus,.rst-content h6:hover .headerlink,.rst-content p.caption .headerlink:focus,.rst-content p.caption:hover .headerlink,.rst-content p .headerlink:focus,.rst-content p:hover .headerlink,.rst-content table>caption .headerlink:focus,.rst-content table>caption:hover .headerlink{opacity:1}.rst-content p a{overflow-wrap:anywhere}.rst-content .wy-table td p,.rst-content .wy-table td ul,.rst-content .wy-table th p,.rst-content .wy-table th ul,.rst-content table.docutils td p,.rst-content table.docutils td ul,.rst-content table.docutils th p,.rst-content table.docutils th ul,.rst-content table.field-list td p,.rst-content table.field-list td ul,.rst-content table.field-list th p,.rst-content table.field-list th ul{font-size:inherit}.rst-content .btn:focus{outline:2px solid}.rst-content table>caption .headerlink:after{font-size:12px}.rst-content .centered{text-align:center}.rst-content .sidebar{float:right;width:40%;display:block;margin:0 0 24px 24px;padding:24px;background:#f3f6f6;border:1px solid #e1e4e5}.rst-content .sidebar dl,.rst-content .sidebar p,.rst-content .sidebar ul{font-size:90%}.rst-content .sidebar .last,.rst-content .sidebar>:last-child{margin-bottom:0}.rst-content .sidebar .sidebar-title{display:block;font-family:Roboto Slab,ff-tisa-web-pro,Georgia,Arial,sans-serif;font-weight:700;background:#e1e4e5;padding:6px 12px;margin:-24px -24px 24px;font-size:100%}.rst-content .highlighted{background:#f1c40f;box-shadow:0 0 0 2px #f1c40f;display:inline;font-weight:700}.rst-content .citation-reference,.rst-content .footnote-reference{vertical-align:baseline;position:relative;top:-.4em;line-height:0;font-size:90%}.rst-content .citation-reference>span.fn-bracket,.rst-content .footnote-reference>span.fn-bracket{display:none}.rst-content .hlist{width:100%}.rst-content dl dt span.classifier:before{content:" : "}.rst-content dl dt span.classifier-delimiter{display:none!important}html.writer-html4 .rst-content table.docutils.citation,html.writer-html4 .rst-content table.docutils.footnote{background:none;border:none}html.writer-html4 .rst-content table.docutils.citation td,html.writer-html4 .rst-content table.docutils.citation tr,html.writer-html4 .rst-content table.docutils.footnote td,html.writer-html4 .rst-content table.docutils.footnote tr{border:none;background-color:transparent!important;white-space:normal}html.writer-html4 .rst-content table.docutils.citation td.label,html.writer-html4 .rst-content table.docutils.footnote td.label{padding-left:0;padding-right:0;vertical-align:top}html.writer-html5 .rst-content dl.citation,html.writer-html5 .rst-content dl.field-list,html.writer-html5 .rst-content dl.footnote{display:grid;grid-template-columns:auto minmax(80%,95%)}html.writer-html5 .rst-content dl.citation>dt,html.writer-html5 .rst-content dl.field-list>dt,html.writer-html5 .rst-content dl.footnote>dt{display:inline-grid;grid-template-columns:max-content auto}html.writer-html5 .rst-content aside.citation,html.writer-html5 .rst-content aside.footnote,html.writer-html5 .rst-content div.citation{display:grid;grid-template-columns:auto auto minmax(.65rem,auto) minmax(40%,95%)}html.writer-html5 .rst-content aside.citation>span.label,html.writer-html5 .rst-content aside.footnote>span.label,html.writer-html5 .rst-content div.citation>span.label{grid-column-start:1;grid-column-end:2}html.writer-html5 .rst-content aside.citation>span.backrefs,html.writer-html5 .rst-content aside.footnote>span.backrefs,html.writer-html5 .rst-content div.citation>span.backrefs{grid-column-start:2;grid-column-end:3;grid-row-start:1;grid-row-end:3}html.writer-html5 .rst-content aside.citation>p,html.writer-html5 .rst-content aside.footnote>p,html.writer-html5 .rst-content div.citation>p{grid-column-start:4;grid-column-end:5}html.writer-html5 .rst-content dl.citation,html.writer-html5 .rst-content dl.field-list,html.writer-html5 .rst-content dl.footnote{margin-bottom:24px}html.writer-html5 .rst-content dl.citation>dt,html.writer-html5 .rst-content dl.field-list>dt,html.writer-html5 .rst-content dl.footnote>dt{padding-left:1rem}html.writer-html5 .rst-content dl.citation>dd,html.writer-html5 .rst-content dl.citation>dt,html.writer-html5 .rst-content dl.field-list>dd,html.writer-html5 .rst-content dl.field-list>dt,html.writer-html5 .rst-content dl.footnote>dd,html.writer-html5 .rst-content dl.footnote>dt{margin-bottom:0}html.writer-html5 .rst-content dl.citation,html.writer-html5 .rst-content dl.footnote{font-size:.9rem}html.writer-html5 .rst-content dl.citation>dt,html.writer-html5 .rst-content dl.footnote>dt{margin:0 .5rem .5rem 0;line-height:1.2rem;word-break:break-all;font-weight:400}html.writer-html5 .rst-content dl.citation>dt>span.brackets:before,html.writer-html5 .rst-content dl.footnote>dt>span.brackets:before{content:"["}html.writer-html5 .rst-content dl.citation>dt>span.brackets:after,html.writer-html5 .rst-content dl.footnote>dt>span.brackets:after{content:"]"}html.writer-html5 .rst-content dl.citation>dt>span.fn-backref,html.writer-html5 .rst-content dl.footnote>dt>span.fn-backref{text-align:left;font-style:italic;margin-left:.65rem;word-break:break-word;word-spacing:-.1rem;max-width:5rem}html.writer-html5 .rst-content dl.citation>dt>span.fn-backref>a,html.writer-html5 .rst-content dl.footnote>dt>span.fn-backref>a{word-break:keep-all}html.writer-html5 .rst-content dl.citation>dt>span.fn-backref>a:not(:first-child):before,html.writer-html5 .rst-content dl.footnote>dt>span.fn-backref>a:not(:first-child):before{content:" "}html.writer-html5 .rst-content dl.citation>dd,html.writer-html5 .rst-content dl.footnote>dd{margin:0 0 .5rem;line-height:1.2rem}html.writer-html5 .rst-content dl.citation>dd p,html.writer-html5 .rst-content dl.footnote>dd p{font-size:.9rem}html.writer-html5 .rst-content aside.citation,html.writer-html5 .rst-content aside.footnote,html.writer-html5 .rst-content div.citation{padding-left:1rem;padding-right:1rem;font-size:.9rem;line-height:1.2rem}html.writer-html5 .rst-content aside.citation p,html.writer-html5 .rst-content aside.footnote p,html.writer-html5 .rst-content div.citation p{font-size:.9rem;line-height:1.2rem;margin-bottom:12px}html.writer-html5 .rst-content aside.citation span.backrefs,html.writer-html5 .rst-content aside.footnote span.backrefs,html.writer-html5 .rst-content div.citation span.backrefs{text-align:left;font-style:italic;margin-left:.65rem;word-break:break-word;word-spacing:-.1rem;max-width:5rem}html.writer-html5 .rst-content aside.citation span.backrefs>a,html.writer-html5 .rst-content aside.footnote span.backrefs>a,html.writer-html5 .rst-content div.citation span.backrefs>a{word-break:keep-all}html.writer-html5 .rst-content aside.citation span.backrefs>a:not(:first-child):before,html.writer-html5 .rst-content aside.footnote span.backrefs>a:not(:first-child):before,html.writer-html5 .rst-content div.citation span.backrefs>a:not(:first-child):before{content:" "}html.writer-html5 .rst-content aside.citation span.label,html.writer-html5 .rst-content aside.footnote span.label,html.writer-html5 .rst-content div.citation span.label{line-height:1.2rem}html.writer-html5 .rst-content aside.citation-list,html.writer-html5 .rst-content aside.footnote-list,html.writer-html5 .rst-content div.citation-list{margin-bottom:24px}html.writer-html5 .rst-content dl.option-list kbd{font-size:.9rem}.rst-content table.docutils.footnote,html.writer-html4 .rst-content table.docutils.citation,html.writer-html5 .rst-content aside.footnote,html.writer-html5 .rst-content aside.footnote-list aside.footnote,html.writer-html5 .rst-content div.citation-list>div.citation,html.writer-html5 .rst-content dl.citation,html.writer-html5 .rst-content dl.footnote{color:grey}.rst-content table.docutils.footnote code,.rst-content table.docutils.footnote tt,html.writer-html4 .rst-content table.docutils.citation code,html.writer-html4 .rst-content table.docutils.citation tt,html.writer-html5 .rst-content aside.footnote-list aside.footnote code,html.writer-html5 .rst-content aside.footnote-list aside.footnote tt,html.writer-html5 .rst-content aside.footnote code,html.writer-html5 .rst-content aside.footnote tt,html.writer-html5 .rst-content div.citation-list>div.citation code,html.writer-html5 .rst-content div.citation-list>div.citation tt,html.writer-html5 .rst-content dl.citation code,html.writer-html5 .rst-content dl.citation tt,html.writer-html5 .rst-content dl.footnote code,html.writer-html5 .rst-content dl.footnote tt{color:#555}.rst-content .wy-table-responsive.citation,.rst-content .wy-table-responsive.footnote{margin-bottom:0}.rst-content .wy-table-responsive.citation+:not(.citation),.rst-content .wy-table-responsive.footnote+:not(.footnote){margin-top:24px}.rst-content .wy-table-responsive.citation:last-child,.rst-content .wy-table-responsive.footnote:last-child{margin-bottom:24px}.rst-content table.docutils th{border-color:#e1e4e5}html.writer-html5 .rst-content table.docutils th{border:1px solid #e1e4e5}html.writer-html5 .rst-content table.docutils td>p,html.writer-html5 .rst-content table.docutils th>p{line-height:1rem;margin-bottom:0;font-size:.9rem}.rst-content table.docutils td .last,.rst-content table.docutils td .last>:last-child{margin-bottom:0}.rst-content table.field-list,.rst-content table.field-list td{border:none}.rst-content table.field-list td p{line-height:inherit}.rst-content table.field-list td>strong{display:inline-block}.rst-content table.field-list .field-name{padding-right:10px;text-align:left;white-space:nowrap}.rst-content table.field-list .field-body{text-align:left}.rst-content code,.rst-content tt{color:#000;font-family:SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,Courier,monospace;padding:2px 5px}.rst-content code big,.rst-content code em,.rst-content tt big,.rst-content tt em{font-size:100%!important;line-height:normal}.rst-content code.literal,.rst-content tt.literal{color:#e74c3c;white-space:normal}.rst-content code.xref,.rst-content tt.xref,a .rst-content code,a .rst-content tt{font-weight:700;color:#404040;overflow-wrap:normal}.rst-content kbd,.rst-content pre,.rst-content samp{font-family:SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,Courier,monospace}.rst-content a code,.rst-content a tt{color:#2980b9}.rst-content dl{margin-bottom:24px}.rst-content dl dt{font-weight:700;margin-bottom:12px}.rst-content dl ol,.rst-content dl p,.rst-content dl table,.rst-content dl ul{margin-bottom:12px}.rst-content dl dd{margin:0 0 12px 24px;line-height:24px}.rst-content dl dd>ol:last-child,.rst-content dl dd>p:last-child,.rst-content dl dd>table:last-child,.rst-content dl dd>ul:last-child{margin-bottom:0}html.writer-html4 .rst-content dl:not(.docutils),html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple){margin-bottom:24px}html.writer-html4 .rst-content dl:not(.docutils)>dt,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple)>dt{display:table;margin:6px 0;font-size:90%;line-height:normal;background:#e7f2fa;color:#2980b9;border-top:3px solid #6ab0de;padding:6px;position:relative}html.writer-html4 .rst-content dl:not(.docutils)>dt:before,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple)>dt:before{color:#6ab0de}html.writer-html4 .rst-content dl:not(.docutils)>dt .headerlink,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple)>dt .headerlink{color:#404040;font-size:100%!important}html.writer-html4 .rst-content dl:not(.docutils) dl:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple)>dt,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) dl:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple)>dt{margin-bottom:6px;border:none;border-left:3px solid #ccc;background:#f0f0f0;color:#555}html.writer-html4 .rst-content dl:not(.docutils) dl:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple)>dt .headerlink,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) dl:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple)>dt .headerlink{color:#404040;font-size:100%!important}html.writer-html4 .rst-content dl:not(.docutils)>dt:first-child,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple)>dt:first-child{margin-top:0}html.writer-html4 .rst-content dl:not(.docutils) code.descclassname,html.writer-html4 .rst-content dl:not(.docutils) code.descname,html.writer-html4 .rst-content dl:not(.docutils) tt.descclassname,html.writer-html4 .rst-content dl:not(.docutils) tt.descname,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) code.descclassname,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) code.descname,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) tt.descclassname,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) tt.descname{background-color:transparent;border:none;padding:0;font-size:100%!important}html.writer-html4 .rst-content dl:not(.docutils) code.descname,html.writer-html4 .rst-content dl:not(.docutils) tt.descname,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) code.descname,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) tt.descname{font-weight:700}html.writer-html4 .rst-content dl:not(.docutils) .optional,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) .optional{display:inline-block;padding:0 4px;color:#000;font-weight:700}html.writer-html4 .rst-content dl:not(.docutils) .property,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) .property{display:inline-block;padding-right:8px;max-width:100%}html.writer-html4 .rst-content dl:not(.docutils) .k,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) .k{font-style:italic}html.writer-html4 .rst-content dl:not(.docutils) .descclassname,html.writer-html4 .rst-content dl:not(.docutils) .descname,html.writer-html4 .rst-content dl:not(.docutils) .sig-name,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) .descclassname,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) .descname,html.writer-html5 .rst-content dl[class]:not(.option-list):not(.field-list):not(.footnote):not(.citation):not(.glossary):not(.simple) .sig-name{font-family:SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,Courier,monospace;color:#000}.rst-content .viewcode-back,.rst-content .viewcode-link{display:inline-block;color:#27ae60;font-size:80%;padding-left:24px}.rst-content .viewcode-back{display:block;float:right}.rst-content p.rubric{margin-bottom:12px;font-weight:700}.rst-content code.download,.rst-content tt.download{background:inherit;padding:inherit;font-weight:400;font-family:inherit;font-size:inherit;color:inherit;border:inherit;white-space:inherit}.rst-content code.download span:first-child,.rst-content tt.download span:first-child{-webkit-font-smoothing:subpixel-antialiased}.rst-content code.download span:first-child:before,.rst-content tt.download span:first-child:before{margin-right:4px}.rst-content .guilabel{border:1px solid #7fbbe3;background:#e7f2fa;font-size:80%;font-weight:700;border-radius:4px;padding:2.4px 6px;margin:auto 2px}.rst-content :not(dl.option-list)>:not(dt):not(kbd):not(.kbd)>.kbd,.rst-content :not(dl.option-list)>:not(dt):not(kbd):not(.kbd)>kbd{color:inherit;font-size:80%;background-color:#fff;border:1px solid #a6a6a6;border-radius:4px;box-shadow:0 2px grey;padding:2.4px 6px;margin:auto 0}.rst-content .versionmodified{font-style:italic}@media screen and (max-width:480px){.rst-content .sidebar{width:100%}}span[id*=MathJax-Span]{color:#404040}.math{text-align:center}@font-face{font-family:Lato;src:url(fonts/lato-normal.woff2?bd03a2cc277bbbc338d464e679fe9942) format("woff2"),url(fonts/lato-normal.woff?27bd77b9162d388cb8d4c4217c7c5e2a) format("woff");font-weight:400;font-style:normal;font-display:block}@font-face{font-family:Lato;src:url(fonts/lato-bold.woff2?cccb897485813c7c256901dbca54ecf2) format("woff2"),url(fonts/lato-bold.woff?d878b6c29b10beca227e9eef4246111b) format("woff");font-weight:700;font-style:normal;font-display:block}@font-face{font-family:Lato;src:url(fonts/lato-bold-italic.woff2?0b6bb6725576b072c5d0b02ecdd1900d) format("woff2"),url(fonts/lato-bold-italic.woff?9c7e4e9eb485b4a121c760e61bc3707c) format("woff");font-weight:700;font-style:italic;font-display:block}@font-face{font-family:Lato;src:url(fonts/lato-normal-italic.woff2?4eb103b4d12be57cb1d040ed5e162e9d) format("woff2"),url(fonts/lato-normal-italic.woff?f28f2d6482446544ef1ea1ccc6dd5892) format("woff");font-weight:400;font-style:italic;font-display:block}@font-face{font-family:Roboto Slab;font-style:normal;font-weight:400;src:url(fonts/Roboto-Slab-Regular.woff2?7abf5b8d04d26a2cafea937019bca958) format("woff2"),url(fonts/Roboto-Slab-Regular.woff?c1be9284088d487c5e3ff0a10a92e58c) format("woff");font-display:block}@font-face{font-family:Roboto Slab;font-style:normal;font-weight:700;src:url(fonts/Roboto-Slab-Bold.woff2?9984f4a9bda09be08e83f2506954adbe) format("woff2"),url(fonts/Roboto-Slab-Bold.woff?bed5564a116b05148e3b3bea6fb1162a) format("woff");font-display:block} diff --git a/css/theme_extra.css b/css/theme_extra.css new file mode 100644 index 00000000..ab0631a1 --- /dev/null +++ b/css/theme_extra.css @@ -0,0 +1,197 @@ +/* + * Wrap inline code samples otherwise they shoot of the side and + * can't be read at all. + * + * https://github.com/mkdocs/mkdocs/issues/313 + * https://github.com/mkdocs/mkdocs/issues/233 + * https://github.com/mkdocs/mkdocs/issues/834 + */ +.rst-content code { + white-space: pre-wrap; + word-wrap: break-word; + padding: 2px 5px; +} + +/** + * Make code blocks display as blocks and give them the appropriate + * font size and padding. + * + * https://github.com/mkdocs/mkdocs/issues/855 + * https://github.com/mkdocs/mkdocs/issues/834 + * https://github.com/mkdocs/mkdocs/issues/233 + */ +.rst-content pre code { + white-space: pre; + word-wrap: normal; + display: block; + padding: 12px; + font-size: 12px; +} + +/** + * Fix code colors + * + * https://github.com/mkdocs/mkdocs/issues/2027 + */ +.rst-content code { + color: #E74C3C; +} + +.rst-content pre code { + color: #000; + background: #f8f8f8; +} + +/* + * Fix link colors when the link text is inline code. + * + * https://github.com/mkdocs/mkdocs/issues/718 + */ +a code { + color: #2980B9; +} +a:hover code { + color: #3091d1; +} +a:visited code { + color: #9B59B6; +} + +/* + * The CSS classes from highlight.js seem to clash with the + * ReadTheDocs theme causing some code to be incorrectly made + * bold and italic. + * + * https://github.com/mkdocs/mkdocs/issues/411 + */ +pre .cs, pre .c { + font-weight: inherit; + font-style: inherit; +} + +/* + * Fix some issues with the theme and non-highlighted code + * samples. Without and highlighting styles attached the + * formatting is broken. + * + * https://github.com/mkdocs/mkdocs/issues/319 + */ +.rst-content .no-highlight { + display: block; + padding: 0.5em; + color: #333; +} + + +/* + * Additions specific to the search functionality provided by MkDocs + */ + +.search-results { + margin-top: 23px; +} + +.search-results article { + border-top: 1px solid #E1E4E5; + padding-top: 24px; +} + +.search-results article:first-child { + border-top: none; +} + +form .search-query { + width: 100%; + border-radius: 50px; + padding: 6px 12px; + border-color: #D1D4D5; +} + +/* + * Improve inline code blocks within admonitions. + * + * https://github.com/mkdocs/mkdocs/issues/656 + */ + .rst-content .admonition code { + color: #404040; + border: 1px solid #c7c9cb; + border: 1px solid rgba(0, 0, 0, 0.2); + background: #f8fbfd; + background: rgba(255, 255, 255, 0.7); +} + +/* + * Account for wide tables which go off the side. + * Override borders to avoid weirdness on narrow tables. + * + * https://github.com/mkdocs/mkdocs/issues/834 + * https://github.com/mkdocs/mkdocs/pull/1034 + */ +.rst-content .section .docutils { + width: 100%; + overflow: auto; + display: block; + border: none; +} + +td, th { + border: 1px solid #e1e4e5 !important; + border-collapse: collapse; +} + +/* + * Without the following amendments, the navigation in the theme will be + * slightly cut off. This is due to the fact that the .wy-nav-side has a + * padding-bottom of 2em, which must not necessarily align with the font-size of + * 90 % on the .rst-current-version container, combined with the padding of 12px + * above and below. These amendments fix this in two steps: First, make sure the + * .rst-current-version container has a fixed height of 40px, achieved using + * line-height, and then applying a padding-bottom of 40px to this container. In + * a second step, the items within that container are re-aligned using flexbox. + * + * https://github.com/mkdocs/mkdocs/issues/2012 + */ + .wy-nav-side { + padding-bottom: 40px; +} + +/* For section-index only */ +.wy-menu-vertical .current-section p { + background-color: #e3e3e3; + color: #404040; +} + +/* + * The second step of above amendment: Here we make sure the items are aligned + * correctly within the .rst-current-version container. Using flexbox, we + * achieve it in such a way that it will look like the following: + * + * [No repo_name] + * Next >> // On the first page + * << Previous Next >> // On all subsequent pages + * + * [With repo_name] + * Next >> // On the first page + * << Previous Next >> // On all subsequent pages + * + * https://github.com/mkdocs/mkdocs/issues/2012 + */ +.rst-versions .rst-current-version { + padding: 0 12px; + display: flex; + font-size: initial; + justify-content: space-between; + align-items: center; + line-height: 40px; +} + +/* + * Please note that this amendment also involves removing certain inline-styles + * from the file ./mkdocs/themes/readthedocs/versions.html. + * + * https://github.com/mkdocs/mkdocs/issues/2012 + */ +.rst-current-version span { + flex: 1; + text-align: center; +} diff --git a/docs/api.md b/docs/api.md deleted file mode 100644 index aae92cd6..00000000 --- a/docs/api.md +++ /dev/null @@ -1,46 +0,0 @@ -## Python Fire Quick Reference - -| Setup | Command | Notes -| ------- | ------------------- | ---------- -| install | `pip install fire` | Installs fire from pypi - -| Creating a CLI | Command | Notes -| ---------------| ---------------------- | ---------- -| import | `import fire` | -| Call | `fire.Fire()` | Turns the current module into a Fire CLI. -| Call | `fire.Fire(component)` | Turns `component` into a Fire CLI. - -| Using a CLI | Command | Notes | -| ------------------------------------------ | ----------------- | -------------- | -| [Help](using-cli.md#help-flag) | `command --help` | Show the help screen. | -| [REPL](using-cli.md#interactive-flag) | `command -- --interactive` | Enters interactive mode. | -| [Separator](using-cli.md#separator-flag) | `command -- --separator=X` | This sets the separator to `X`. The default separator is `-`. | -| [Completion](using-cli.md#completion-flag) | `command -- --completion [shell]` | Generate a completion script for the CLI. | -| [Trace](using-cli.md#trace-flag) | `command -- --trace` | Gets a Fire trace for the command. | -| [Verbose](using-cli.md#verbose-flag) | `command -- --verbose` | | - -_Note that flags are separated from the Fire command by an isolated `--` arg. -Help is an exception; the isolated `--` is optional for getting help._ - -## Arguments for Calling fire.Fire() - -| Argument | Usage | Notes | -| --------- | ------------------------- | ------------------------------------ | -| component | `fire.Fire(component)` | If omitted, defaults to a dict of all locals and globals. | -| command | `fire.Fire(command='hello --name=5')` | Either a string or a list of arguments. If a string is provided, it is split to determine the arguments. If a list or tuple is provided, they are the arguments. If `command` is omitted, then `sys.argv[1:]` (the arguments from the command line) are used by default. | -| name | `fire.Fire(name='tool')` | The name of the CLI, ideally the name users will enter to run the CLI. This name will be used in the CLI's help screens. If the argument is omitted, it will be inferred automatically.| -| serialize | `fire.Fire(serialize=custom_serializer)` | If omitted, simple types are serialized via their builtin str method, and any objects that define a custom `__str__` method are serialized with that. If specified, all objects are serialized to text via the provided method. | - -## Using a Fire CLI without modifying any code - -You can use Python Fire on a module without modifying the code of the module. -The syntax for this is: - -`python -m fire ` - -or - -`python -m fire ` - -For example, `python -m fire calendar -h` will treat the built in `calendar` -module as a CLI and provide its help. diff --git a/docs/benefits.md b/docs/benefits.md deleted file mode 100644 index ac09f0be..00000000 --- a/docs/benefits.md +++ /dev/null @@ -1,61 +0,0 @@ -# Benefits of Python Fire - - -## Create CLIs in Python - -It's dead simple. Simply write the functionality you want exposed at the command -line as a function / module / class, and then call Fire. With this addition of a -single-line call to Fire, your CLI is ready to go. - - -## Develop and debug Python code - -When you're writing a Python library, you probably want to try it out as you go. -You could write a main method to check the functionality you're interested in, -but then you have to change the main method with every new experiment you're -interested in testing, and constantly updating the main method is a hassle. -You could also open an IPython REPL and import your library there and test it, -but then you have to deal with reloading your imports every time you change -something. - -If you simply call Fire in your library, then you can run all of it's -functionality from the command line without having to keep making changes to -a main method. And if you use the `--interactive` flag to enter an IPython REPL -then you don't need to load the imports or create your variables; they'll -already be ready for use as soon as you start the REPL. - - -## Explore existing code; turn other people's code into a CLI - -You can take an existing module, maybe even one that you don't have access to -the source code for, and call `Fire` on it. This lets you easily see what -functionality this code exposes, without you having to read through all the -code. - -This technique can be a very simple way to create very powerful CLIs. Call -`Fire` on the difflib library and you get a powerful diffing tool. Call `Fire` -on the Python Imaging Library (PIL) module and you get a powerful image -manipulation command line tool, very similar in nature to ImageMagick. - -The auto-generated help strings that Fire provides when you run a Fire CLI -allow you to see all the functionality these modules provide in a concise -manner. - - -## Transition between Bash and Python - -Using Fire lets you call Python directly from Bash. So you can mix your Python -functions with the unix tools you know and love, like `grep`, `xargs`, `wc`, -etc. - -Additionally since writing CLIs in Python requires only a single call to Fire, -it is now easy to write even one-off scripts that would previously have been in -Bash, in Python. - - -## Explore code in a Python REPL - -When you use the `--interactive` flag to enter an IPython REPL, it starts with -variables and modules already defined for you. You don't need to waste time -importing the modules you care about or defining the variables you're going to -use, since Fire has already done so for you. diff --git a/docs/guide.md b/docs/guide.md deleted file mode 100644 index 444a76ff..00000000 --- a/docs/guide.md +++ /dev/null @@ -1,780 +0,0 @@ -## The Python Fire Guide - -### Introduction - -Welcome to the Python Fire guide! Python Fire is a Python library that will turn -any Python component into a command line interface with just a single call to -`Fire`. - -Let's get started! - -### Installation - -To install Python Fire from pypi, run: - -`pip install fire` - -Alternatively, to install Python Fire from source, clone the source and run: - -`python setup.py install` - -### Hello World - -##### Version 1: `fire.Fire()` - -The easiest way to use Fire is to take any Python program, and then simply call -`fire.Fire()` at the end of the program. This will expose the full contents of -the program to the command line. - -```python -import fire - -def hello(name): - return f'Hello {name}!' - -if __name__ == '__main__': - fire.Fire() -``` - -Here's how we can run our program from the command line: - -```bash -$ python example.py hello World -Hello World! -``` - -##### Version 2: `fire.Fire()` - -Let's modify our program slightly to only expose the `hello` function to the -command line. - -```python -import fire - -def hello(name): - return f'Hello {name}!' - -if __name__ == '__main__': - fire.Fire(hello) -``` - -Here's how we can run this from the command line: - -```bash -$ python example.py World -Hello World! -``` - -Notice we no longer have to specify to run the `hello` function, because we -called `fire.Fire(hello)`. - -##### Version 3: Using a main - -We can alternatively write this program like this: - -```python -import fire - -def hello(name): - return f'Hello {name}!' - -def main(): - fire.Fire(hello) - -if __name__ == '__main__': - main() -``` - -Or if we're using -[entry points](https://setuptools.readthedocs.io/en/latest/pkg_resources.html#entry-points), -then simply this: - -```python -import fire - -def hello(name): - return f'Hello {name}!' - -def main(): - fire.Fire(hello) -``` - -##### Version 4: Fire Without Code Changes - -If you have a file `example.py` that doesn't even import fire: - -```python -def hello(name): - return f'Hello {name}!' -``` - -Then you can use it with Fire like this: - -```bash -$ python -m fire example hello --name=World -Hello World! -``` - -You can also specify the filepath of example.py rather than its module path, -like so: - -```bash -$ python -m fire example.py hello --name=World -Hello World! -``` - -### Exposing Multiple Commands - -In the previous example, we exposed a single function to the command line. Now -we'll look at ways of exposing multiple functions to the command line. - -##### Version 1: `fire.Fire()` - -The simplest way to expose multiple commands is to write multiple functions, and -then call Fire. - -```python -import fire - -def add(x, y): - return x + y - -def multiply(x, y): - return x * y - -if __name__ == '__main__': - fire.Fire() -``` - -We can use this like so: - -```bash -$ python example.py add 10 20 -30 -$ python example.py multiply 10 20 -200 -``` - -You'll notice that Fire correctly parsed `10` and `20` as numbers, rather than -as strings. Read more about [argument parsing here](#argument-parsing). - -##### Version 2: `fire.Fire()` - -In version 1 we exposed all the program's functionality to the command line. By -using a dict, we can selectively expose functions to the command line. - -```python -import fire - -def add(x, y): - return x + y - -def multiply(x, y): - return x * y - -if __name__ == '__main__': - fire.Fire({ - 'add': add, - 'multiply': multiply, - }) -``` - -We can use this in the same way as before: - -```bash -$ python example.py add 10 20 -30 -$ python example.py multiply 10 20 -200 -``` - -##### Version 3: `fire.Fire()` - -Fire also works on objects, as in this variant. This is a good way to expose -multiple commands. - -```python -import fire - -class Calculator(object): - - def add(self, x, y): - return x + y - - def multiply(self, x, y): - return x * y - -if __name__ == '__main__': - calculator = Calculator() - fire.Fire(calculator) -``` - -We can use this in the same way as before: - -```bash -$ python example.py add 10 20 -30 -$ python example.py multiply 10 20 -200 -``` - - -##### Version 4: `fire.Fire()` - -Fire also works on classes. This is another good way to expose multiple -commands. - -```python -import fire - -class Calculator(object): - - def add(self, x, y): - return x + y - - def multiply(self, x, y): - return x * y - -if __name__ == '__main__': - fire.Fire(Calculator) -``` - -We can use this in the same way as before: - -```bash -$ python example.py add 10 20 -30 -$ python example.py multiply 10 20 -200 -``` - -Why might you prefer a class over an object? One reason is that you can pass -arguments for constructing the class too, as in this broken calculator example. - -```python -import fire - -class BrokenCalculator(object): - - def __init__(self, offset=1): - self._offset = offset - - def add(self, x, y): - return x + y + self._offset - - def multiply(self, x, y): - return x * y + self._offset - -if __name__ == '__main__': - fire.Fire(BrokenCalculator) -``` - -When you use a broken calculator, you get wrong answers: - -```bash -$ python example.py add 10 20 -31 -$ python example.py multiply 10 20 -201 -``` - -But you can always fix it: - -```bash -$ python example.py add 10 20 --offset=0 -30 -$ python example.py multiply 10 20 --offset=0 -200 -``` - -Unlike calling ordinary functions, which can be done both with positional -arguments and named arguments (--flag syntax), arguments to \_\_init\_\_ -functions must be passed with the --flag syntax. See the section on -[calling functions](#calling-functions) for more. - -### Grouping Commands - -Here's an example of how you might make a command line interface with grouped -commands. - -```python -class IngestionStage(object): - - def run(self): - return 'Ingesting! Nom nom nom...' - -class DigestionStage(object): - - def run(self, volume=1): - return ' '.join(['Burp!'] * volume) - - def status(self): - return 'Satiated.' - -class Pipeline(object): - - def __init__(self): - self.ingestion = IngestionStage() - self.digestion = DigestionStage() - - def run(self): - ingestion_output = self.ingestion.run() - digestion_output = self.digestion.run() - return [ingestion_output, digestion_output] - -if __name__ == '__main__': - fire.Fire(Pipeline) -``` - -Here's how this looks at the command line: - -```bash -$ python example.py run -Ingesting! Nom nom nom... -Burp! -$ python example.py ingestion run -Ingesting! Nom nom nom... -$ python example.py digestion run -Burp! -$ python example.py digestion status -Satiated. -``` - -You can nest your commands in arbitrarily complex ways, if you're feeling grumpy -or adventurous. - - -### Accessing Properties - -In the examples we've looked at so far, our invocations of `python example.py` -have all run some function from the example program. In this example, we simply -access a property. - -```python -from airports import airports - -import fire - -class Airport(object): - - def __init__(self, code): - self.code = code - self.name = dict(airports).get(self.code) - self.city = self.name.split(',')[0] if self.name else None - -if __name__ == '__main__': - fire.Fire(Airport) -``` - -Now we can use this program to learn about airport codes! - -```bash -$ python example.py --code=JFK code -JFK -$ python example.py --code=SJC name -San Jose-Sunnyvale-Santa Clara, CA - Norman Y. Mineta San Jose International (SJC) -$ python example.py --code=ALB city -Albany-Schenectady-Troy -``` - -By the way, you can find this -[airports module here](https://github.com/trendct-data/airports.py). - -### Chaining Function Calls - -When you run a Fire CLI, you can take all the same actions on the _result_ of -the call to Fire that you can take on the original object passed in. - -For example, we can use our Airport CLI from the previous example like this: - -```bash -$ python example.py --code=ALB city upper -ALBANY-SCHENECTADY-TROY -``` - -This works since `upper` is a method on all strings. - -So, if you want to set up your functions to chain nicely, all you have to do is -have a class whose methods return self. Here's an example. - -```python -import fire - -class BinaryCanvas(object): - """A canvas with which to make binary art, one bit at a time.""" - - def __init__(self, size=10): - self.pixels = [[0] * size for _ in range(size)] - self._size = size - self._row = 0 # The row of the cursor. - self._col = 0 # The column of the cursor. - - def __str__(self): - return '\n'.join(' '.join(str(pixel) for pixel in row) for row in self.pixels) - - def show(self): - print(self) - return self - - def move(self, row, col): - self._row = row % self._size - self._col = col % self._size - return self - - def on(self): - return self.set(1) - - def off(self): - return self.set(0) - - def set(self, value): - self.pixels[self._row][self._col] = value - return self - -if __name__ == '__main__': - fire.Fire(BinaryCanvas) -``` - -Now we can draw stuff :). - -```bash -$ python example.py move 3 3 on move 3 6 on move 6 3 on move 6 6 on move 7 4 on move 7 5 on -0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 -0 0 0 1 0 0 1 0 0 0 -0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 -0 0 0 1 0 0 1 0 0 0 -0 0 0 0 1 1 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 -0 0 0 0 0 0 0 0 0 0 -``` - -It's supposed to be a smiley face. - -### Custom Serialization - -You'll notice in the BinaryCanvas example, the canvas with the smiley face was -printed to the screen. You can determine how a component will be serialized by -defining its `__str__` method. - -If a custom `__str__` method is present on the final component, the object is -serialized and printed. If there's no custom `__str__` method, then the help -screen for the object is shown instead. - -### Can we make an even simpler example than Hello World? - -Yes, this program is even simpler than our original Hello World example. - -```python -import fire -english = 'Hello World' -spanish = 'Hola Mundo' -fire.Fire() -``` - -You can use it like this: - -```bash -$ python example.py english -Hello World -$ python example.py spanish -Hola Mundo -``` - -### Calling Functions - -Arguments to a constructor are passed by name using flag syntax `--name=value`. - -For example, consider this simple class: - -```python -import fire - -class Building(object): - - def __init__(self, name, stories=1): - self.name = name - self.stories = stories - - def climb_stairs(self, stairs_per_story=10): - for story in range(self.stories): - for stair in range(1, stairs_per_story): - yield stair - yield 'Phew!' - yield 'Done!' - -if __name__ == '__main__': - fire.Fire(Building) -``` - -We can instantiate it as follows: `python example.py --name="Sherrerd Hall"` - -Arguments to other functions may be passed positionally or by name using flag -syntax. - -To instantiate a `Building` and then run the `climb_stairs` function, the -following commands are all valid: - -```bash -$ python example.py --name="Sherrerd Hall" --stories=3 climb_stairs 10 -$ python example.py --name="Sherrerd Hall" climb_stairs --stairs_per_story=10 -$ python example.py --name="Sherrerd Hall" climb_stairs --stairs-per-story 10 -$ python example.py climb-stairs --stairs-per-story 10 --name="Sherrerd Hall" -``` - -You'll notice that hyphens and underscores (`-` and `_`) are interchangeable in -member names and flag names. - -You'll also notice that the constructor's arguments can come after the -function's arguments or before the function. - -You'll also notice that the equal sign between the flag name and its value is -optional. - -##### Functions with `*varargs` and `**kwargs` - -Fire supports functions that take \*varargs or \*\*kwargs. Here's an example: - -```python -import fire - -def order_by_length(*items): - """Orders items by length, breaking ties alphabetically.""" - sorted_items = sorted(items, key=lambda item: (len(str(item)), str(item))) - return ' '.join(sorted_items) - -if __name__ == '__main__': - fire.Fire(order_by_length) -``` - -To use it, we run: - -```bash -$ python example.py dog cat elephant -cat dog elephant -``` - -You can use a separator to indicate that you're done providing arguments to a -function. All arguments after the separator will be used to process the result -of the function, rather than being passed to the function itself. The default -separator is the hyphen `-`. - -Here's an example where we use a separator. - -```bash -$ python example.py dog cat elephant - upper -CAT DOG ELEPHANT -``` - -Without the separator, upper would have been treated as another argument. - -```bash -$ python example.py dog cat elephant upper -cat dog upper elephant -``` - -You can change the separator with the `--separator` flag. Flags are always -separated from your Fire command by an isolated `--`. Here's an example where we -change the separator. - -```bash -$ python example.py dog cat elephant X upper -- --separator=X -CAT DOG ELEPHANT -``` - -Separators can be useful when a function accepts \*varargs, \*\*kwargs, or -default values that you don't want to specify. It is also important to remember -to change the separator if you want to pass `-` as an argument. - - -##### Async Functions - -Fire supports calling async functions too. Here's a simple example. - -```python -import asyncio - -async def count_to_ten(): - for i in range(1, 11): - await asyncio.sleep(1) - print(i) - -if __name__ == '__main__': - fire.Fire(count_to_ten) -``` - -Whenever fire encounters a coroutine function, it runs it, blocking until it completes. - - -### Argument Parsing - -The types of the arguments are determined by their values, rather than by the -function signature where they're used. You can pass any Python literal from the -command line: numbers, strings, tuples, lists, dictionaries, (sets are only -supported in some versions of Python). You can also nest the collections -arbitrarily as long as they only contain literals. - -To demonstrate this, we'll make a small example program that tells us the type -of any argument we give it: - -```python -import fire -fire.Fire(lambda obj: type(obj).__name__) -``` - -And we'll use it like so: - -```bash -$ python example.py 10 -int -$ python example.py 10.0 -float -$ python example.py hello -str -$ python example.py '(1,2)' -tuple -$ python example.py [1,2] -list -$ python example.py True -bool -$ python example.py {name:David} -dict -``` - -You'll notice in that last example that bare-words are automatically replaced -with strings. - -Be careful with your quotes! If you want to pass the string `"10"`, rather than -the int `10`, you'll need to either escape or quote your quotes. Otherwise Bash -will eat your quotes and pass an unquoted `10` to your Python program, where -Fire will interpret it as a number. - - -```bash -$ python example.py 10 -int -$ python example.py "10" -int -$ python example.py '"10"' -str -$ python example.py "'10'" -str -$ python example.py \"10\" -str -``` - -Be careful with your quotes! Remember that Bash processes your arguments first, -and then Fire parses the result of that. -If you wanted to pass the dict `{"name": "David Bieber"}` to your program, you -might try this: - -```bash -$ python example.py '{"name": "David Bieber"}' # Good! Do this. -dict -$ python example.py {"name":'"David Bieber"'} # Okay. -dict -$ python example.py {"name":"David Bieber"} # Wrong. This is parsed as a string. -str -$ python example.py {"name": "David Bieber"} # Wrong. This isn't even treated as a single argument. - -$ python example.py '{"name": "Justin Bieber"}' # Wrong. This is not the Bieber you're looking for. (The syntax is fine though :)) -dict -``` - -##### Boolean Arguments - -The tokens `True` and `False` are parsed as boolean values. - -You may also specify booleans via flag syntax `--name` and `--noname`, which set -`name` to `True` and `False` respectively. - -Continuing the previous example, we could run any of the following: - -```bash -$ python example.py --obj=True -bool -$ python example.py --obj=False -bool -$ python example.py --obj -bool -$ python example.py --noobj -bool -``` - -Be careful with boolean flags! If a token other than another flag immediately -follows a flag that's supposed to be a boolean, the flag will take on the value -of the token rather than the boolean value. You can resolve this: by putting a -separator after your last flag, by explicitly stating the value of the boolean -flag (as in `--obj=True`), or by making sure there's another flag after any -boolean flag argument. - - -### Using Fire Flags - -Fire CLIs all come with a number of flags. These flags should be separated from -the Fire command by an isolated `--`. If there is at least one isolated `--` -argument, then arguments after the final isolated `--` are treated as flags, -whereas all arguments before the final isolated `--` are considered part of the -Fire command. - -One useful flag is the `--interactive` flag. Use the `--interactive` flag on any -CLI to enter a Python REPL with all the modules and variables used in the -context where `Fire` was called already available to you for use. Other useful -variables, such as the result of the Fire command will also be available. Use -this feature like this: `python example.py -- --interactive`. - -You can add the help flag to any command to see help and usage information. Fire -incorporates your docstrings into the help and usage information that it -generates. Fire will try to provide help even if you omit the isolated `--` -separating the flags from the Fire command, but may not always be able to, since -`help` is a valid argument name. Use this feature like this: `python -example.py -- --help` or `python example.py --help` (or even `python example.py --h`). - -The complete set of flags available is shown below, in the reference section. - - -### Reference - -| Setup | Command | Notes -| :------ | :------------------ | :--------- -| install | `pip install fire` | - -##### Creating a CLI - -| Creating a CLI | Command | Notes -| :--------------| :--------------------- | :--------- -| import | `import fire` | -| Call | `fire.Fire()` | Turns the current module into a Fire CLI. -| Call | `fire.Fire(component)` | Turns `component` into a Fire CLI. - -##### Flags - -| Using a CLI | Command | Notes -| :------------- | :------------------------- | :--------- -| [Help](using-cli.md#help-flag) | `command -- --help` | Show help and usage information for the command. -| [REPL](using-cli.md#interactive-flag) | `command -- --interactive` | Enter interactive mode. -| [Separator](using-cli.md#separator-flag) | `command -- --separator=X` | This sets the separator to `X`. The default separator is `-`. -| [Completion](using-cli.md#completion-flag) | `command -- --completion [shell]` | Generate a completion script for the CLI. -| [Trace](using-cli.md#trace-flag) | `command -- --trace` | Gets a Fire trace for the command. -| [Verbose](using-cli.md#verbose-flag) | `command -- --verbose` | Include private members in the output. - -_Note that flags are separated from the Fire command by an isolated `--` arg. -Help is an exception; the isolated `--` is optional for getting help._ - - -##### Arguments for Calling fire.Fire() - -| Argument | Usage | Notes | -| --------- | ------------------------- | ------------------------------------ | -| component | `fire.Fire(component)` | If omitted, defaults to a dict of all locals and globals. | -| command | `fire.Fire(command='hello --name=5')` | Either a string or a list of arguments. If a string is provided, it is split to determine the arguments. If a list or tuple is provided, they are the arguments. If `command` is omitted, then `sys.argv[1:]` (the arguments from the command line) are used by default. | -| name | `fire.Fire(name='tool')` | The name of the CLI, ideally the name users will enter to run the CLI. This name will be used in the CLI's help screens. If the argument is omitted, it will be inferred automatically.| -| serialize | `fire.Fire(serialize=custom_serializer)` | If omitted, simple types are serialized via their builtin str method, and any objects that define a custom `__str__` method are serialized with that. If specified, all objects are serialized to text via the provided method. | - - -### Disclaimer - -Python Fire is not an official Google product. diff --git a/docs/index.md b/docs/index.md deleted file mode 100644 index 8dcc5db6..00000000 --- a/docs/index.md +++ /dev/null @@ -1,119 +0,0 @@ -# Python Fire [![PyPI](https://img.shields.io/pypi/pyversions/fire.svg?style=plastic)](https://github.com/google/python-fire) - -_Python Fire is a library for automatically generating command line interfaces -(CLIs) from absolutely any Python object._ - -- Python Fire is a simple way to create a CLI in Python. - [[1]](benefits.md#simple-cli) -- Python Fire is a helpful tool for developing and debugging Python code. - [[2]](benefits.md#debugging) -- Python Fire helps with exploring existing code or turning other people's - code into a CLI. [[3]](benefits.md#exploring) -- Python Fire makes transitioning between Bash and Python easier. - [[4]](benefits.md#bash) -- Python Fire makes using a Python REPL easier by setting up the REPL with the - modules and variables you'll need already imported and created. - [[5]](benefits.md#repl) - -## Installation - -To install Python Fire with pip, run: `pip install fire` - -To install Python Fire with conda, run: `conda install fire -c conda-forge` - -To install Python Fire from source, first clone the repository and then run: -`python setup.py install` - -## Basic Usage - -You can call `Fire` on any Python object:
-functions, classes, modules, objects, dictionaries, lists, tuples, etc. -They all work! - -Here's an example of calling Fire on a function. - -```python -import fire - -def hello(name="World"): - return "Hello %s!" % name - -if __name__ == '__main__': - fire.Fire(hello) -``` - -Then, from the command line, you can run: - -```bash -python hello.py # Hello World! -python hello.py --name=David # Hello David! -python hello.py --help # Shows usage information. -``` - -Here's an example of calling Fire on a class. - -```python -import fire - -class Calculator(object): - """A simple calculator class.""" - - def double(self, number): - return 2 * number - -if __name__ == '__main__': - fire.Fire(Calculator) -``` - -Then, from the command line, you can run: - -```bash -python calculator.py double 10 # 20 -python calculator.py double --number=15 # 30 -``` - -To learn how Fire behaves on functions, objects, dicts, lists, etc, and to learn -about Fire's other features, see the [Using a Fire CLI page](using-cli.md). - -For additional examples, see [The Python Fire Guide](guide.md). - -## Why is it called Fire? - -When you call `Fire`, it fires off (executes) your command. - -## Where can I learn more? - -Please see [The Python Fire Guide](guide.md). - -## Reference - -| Setup | Command | Notes -| :------ | :------------------ | :--------- -| install | `pip install fire` | - -| Creating a CLI | Command | Notes -| :--------------| :--------------------- | :--------- -| import | `import fire` | -| Call | `fire.Fire()` | Turns the current module into a Fire CLI. -| Call | `fire.Fire(component)` | Turns `component` into a Fire CLI. - -| Using a CLI | Command | Notes -| :---------------------------------------------- | :-------------------------------------- | :---- -| [Help](using-cli.md#help-flag) | `command --help` or `command -- --help` | -| [REPL](using-cli.md#interactive-flag) | `command -- --interactive` | Enters interactive mode. -| [Separator](using-cli.md#separator-flag) | `command -- --separator=X` | Sets the separator to `X`. The default separator is `-`. -| [Completion](using-cli.md#completion-flag) | `command -- --completion [shell]` | Generates a completion script for the CLI. -| [Trace](using-cli.md#trace-flag) | `command -- --trace` | Gets a Fire trace for the command. -| [Verbose](using-cli.md#verbose-flag) | `command -- --verbose` | - -_Note that flags are separated from the Fire command by an isolated `--` arg. -Help is an exception; the isolated `--` is optional for getting help._ - -## License - -Licensed under the -[Apache 2.0](https://github.com/google/python-fire/blob/master/LICENSE) License. - -## Disclaimer - -This is not an official Google product. diff --git a/docs/installation.md b/docs/installation.md deleted file mode 100644 index 7e4cccb8..00000000 --- a/docs/installation.md +++ /dev/null @@ -1,8 +0,0 @@ -# Installation - -To install Python Fire with pip, run: `pip install fire` - -To install Python Fire with conda, run: `conda install fire -c conda-forge` - -To install Python Fire from source, first clone the repository and then run -`python setup.py install`. To install from source for development, instead run `python setup.py develop`. diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md deleted file mode 100644 index 3ef6b548..00000000 --- a/docs/troubleshooting.md +++ /dev/null @@ -1,13 +0,0 @@ -# Troubleshooting - -This page describes known issues that users of Python Fire have run into. If you -have an issue not resolved here, consider opening a -[GitHub Issue](https://github.com/google/python-fire/issues). - -### Issue [#19](https://github.com/google/python-fire/issues/19): Don't name your module "cmd" - -If you have a module name that conflicts with the name of a builtin module, then -when Fire goes to import the builtin module, it will import your module instead. -This will result in an error, possibly an `AttributeError`. Specifically, do not -name your module any of the following: -sys, linecache, cmd, bdb, repr, os, re, pprint, traceback diff --git a/docs/using-cli.md b/docs/using-cli.md deleted file mode 100644 index bdfcb7db..00000000 --- a/docs/using-cli.md +++ /dev/null @@ -1,232 +0,0 @@ -# Using a Fire CLI - -## Basic usage - -Every Fire command corresponds to a Python component. - -The simplest Fire command consists of running your program with no additional -arguments. This command corresponds to the Python component you called the -`Fire` function on. If you did not supply an object in the call to `Fire`, then -the context in which `Fire` was called will be used as the Python component. - -You can append `--help` or `-h` to a command to see what Python component it -corresponds to, as well as the various ways in which you can extend the command. - -Flags to Fire should be separated from the Fire command by an isolated `--` in -order to distinguish between flags and named arguments. So, for example, to -enter interactive mode append `-- -i` or `-- --interactive` to any command. To -use Fire in verbose mode, append `-- --verbose`. - -Given a Fire command that corresponds to a Python object, you can extend that -command to access a member of that object, call it with arguments if it is a -function, instantiate it if it is a class, or index into it if it is a list. - -Read on to learn about how you can write a Fire command corresponding to -whatever Python component you're looking for. - - -### Accessing members of an object - -If your command corresponds to an object, you can extend your command by adding -the name of a member of that object as a new argument to the command. The -resulting command will correspond to that member. - -For example, if the object your command corresponds to has a method defined on -it named 'whack', then you can add the argument 'whack' to your command, and the -resulting new command corresponds to the whack method. - -As another example, if the object your command corresponds to has a property -named high_score, then you can add the argument 'high-score' to your command, -and the resulting new command corresponds to the value of the high_score -property. - - -### Accessing members of a dict - -If your command corresponds to a dict, you can extend your command by adding -the name of one of the dict's keys as an argument. - -For example, `widget function-that-returns-dict key` will correspond to the -value of the item with key `key` in the dict returned by -`function_that_returns_dict`. - - -### Accessing members of a list or tuple - -If your command corresponds to a list or tuple, you can extend your command by -adding the index of an element of the component to your command as an argument. - -For example, `widget function-that-returns-list 2` will correspond to item 2 of -the result of `function_that_returns_list`. - - -### Calling a function - -If your command corresponds to a function, you can extend your command by adding -the arguments of this function. Arguments can be specified positionally, or by -name. To specify an argument by name, use flag syntax. - -For example, suppose your `command` corresponds to the function `double`: - -```python -def double(value=0): - return 2 * value -``` - -Then you can extend your command using named arguments as `command --value 5`, -or using positional arguments as `command 5`. In both cases, the new command -corresponds to the result of the function, in this case the number 10. - -You can force a function that takes a variable number of arguments to be -evaluated by adding a separator (the default separator is the hyphen, "-"). This -will prevent arguments to the right of the separator from being consumed for -calling the function. This is useful if the function has arguments with default -values, or if the function accepts \*varargs, or if the function accepts -\*\*kwargs. - -See also the section on [Changing the Separator](#separator-flag). - - -### Instantiating a class - -If your command corresponds to a class, you can extend your command by adding -the arguments of the class's `__init__` function. Arguments must be specified -by name, using the flags syntax. See the section on -[calling a function](#calling-a-function) for more details. - -Similarly, when passing arguments to a callable object (an object with a custom -`__call__` function), those arguments must be passed using flags syntax. - -## Using Flags with Fire CLIs - -Command line arguments to a Fire CLI are normally consumed by Fire, as described -in the [Basic Usage](#basic-usage) section. In order to set Flags, put the flags -after the final standalone `--` argument. (If there is no `--` argument, then no -arguments are used for flags.) - -For example, to set the alsologtostderr flag, you could run the command: -`widget bang --noise=boom -- --alsologtostderr`. The `--noise` argument is -consumed by Fire, but the `--alsologtostderr` argument is treated as a normal -Flag. - -All CLIs built with Python Fire share some flags, as described in the next -sections. - - -## Python Fire's Flags - -As described in the [Using Flags](#using-flags) section, you must add an -isolated `--` argument in order to have arguments treated as Flags rather than -be consumed by Python Fire. All arguments to a Fire CLI after the final -standalone `--` argument are treated as Flags. - -The following flags are accepted by all Fire CLIs: -[`--interactive`/`-i`](#interactive-flag), -[`--help`/`-h`](#help-flag), -[`--separator`](#separator-flag), -[`--completion`](#completion-flag), -[`--trace`](#trace-flag), -and [`--verbose`/`-v`](#verbose-flag), -as described in the following sections. - -### `--interactive`: Interactive mode - -Call `widget -- --interactive` or `widget -- -i` to enter interactive mode. This -will put you in an IPython REPL, with the variable `widget` already defined. - -You can then explore the Python object that `widget` corresponds to -interactively using Python. - -Note: if you want fire to start the IPython REPL instead of the regular Python one, -the `ipython` package needs to be installed in your environment. - - -### `--completion`: Generating a completion script - -Call `widget -- --completion` to generate a completion script for the Fire CLI -`widget`. To save the completion script to your home directory, you could e.g. -run `widget -- --completion > ~/.widget-completion`. You should then source this -file; to get permanent completion, source this file from your `.bashrc` file. - -Call `widget -- --completion fish` to generate a completion script for the Fish -shell. Source this file from your fish.config. - -If the commands available in the Fire CLI change, you'll have to regenerate the -completion script and source it again. - - -### `--help`: Getting help - -Let say you have a command line tool named `widget` that was made with Fire. How -do you use this Fire CLI? - -The simplest way to get started is to run `widget -- --help`. This will give you -usage information for your CLI. You can always append `-- --help` to any Fire -command in order to get usage information for that command and any subcommands. - -Additionally, help will be displayed if you hit an error using Fire. For -example, if you try to pass too many or too few arguments to a function, then -help will be displayed. Similarly, if you try to access a member that does not -exist, or if you index into a list with too high an index, then help will be -displayed. - -The displayed help shows information about which Python component your command -corresponds to, as well as usage information for how to extend that command. - - -### `--trace`: Getting a Fire trace - -In order to understand what is happening when you call Python Fire, it can be -useful to request a trace. This is done via the `--trace` flag, e.g. -`widget whack 5 -- --trace`. - -A trace provides step by step information about how the Fire command was -executed. In includes which actions were taken, starting with the initial -component, leading to the final component represented by the command. - -A trace is also shown alongside the help if your Fire command reaches an error. - - -### `--separator`: Changing the separator - -As described in [Calling a Function](#calling-a-function), you can use a -separator argument when writing a command that corresponds to calling a -function. The separator will cause the function to be evaluated or the class to -be instantiated using only the arguments left of the separator. Arguments right -of the separator will then be applied to the result of the function call or to -the instantiated object. - -The default separator is `-`. - -If you want to supply the string "-" as an argument, then you will have to -change the separator. You can choose a new separator by supplying the -`--separator` flag to Fire. - -Here's an example to demonstrate separator usage. Let's say you have a function -that takes a variable number of args, and you want to call that function, and -then upper case the result. Here's how to do it: - -```python -# Here's the Python function -def display(arg1, arg2='!'): - return arg1 + arg2 -``` - -```bash -# Here's what you can do from Bash (Note: the default separator is the hyphen -) -display hello # hello! -display hello upper # helloupper -display hello - upper # HELLO! -display - SEP upper -- --separator SEP # -! -``` -Notice how in the third and fourth lines, the separator caused the display -function to be called with the default value for arg2. In the fourth example, -we change the separator to the string "SEP" so that we can pass '-' as an -argument. - -### `--verbose`: Verbose usage - -Adding the `-v` or `--verbose` flag turns on verbose mode. This will eg -reveal private members in the usage string. Often these members will not -actually be usable from the command line tool. As such, verbose mode should be -considered a debugging tool, but not fully supported yet. diff --git a/examples/cipher/__init__.py b/examples/cipher/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/cipher/cipher.py b/examples/cipher/cipher.py deleted file mode 100644 index 83610a5d..00000000 --- a/examples/cipher/cipher.py +++ /dev/null @@ -1,59 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""The Caesar Shift Cipher example Fire CLI. - -This module demonstrates the use of Fire without specifying a target component. -Notice how the call to Fire() in the main method doesn't indicate a component. -So, all local and global variables (including all functions defined in the -module) are made available as part of the Fire CLI. - -Example usage: -cipher rot13 'Hello world!' # Uryyb jbeyq! -cipher rot13 'Uryyb jbeyq!' # Hello world! -cipher caesar-encode 1 'Hello world!' # Ifmmp xpsme! -cipher caesar-decode 1 'Ifmmp xpsme!' # Hello world! -""" - -import fire - - -def caesar_encode(n=0, text=''): - return ''.join( - _caesar_shift_char(n, char) - for char in text - ) - - -def caesar_decode(n=0, text=''): - return caesar_encode(-n, text) - - -def rot13(text): - return caesar_encode(13, text) - - -def _caesar_shift_char(n=0, char=' '): - if not char.isalpha(): - return char - if char.isupper(): - return chr((ord(char) - ord('A') + n) % 26 + ord('A')) - return chr((ord(char) - ord('a') + n) % 26 + ord('a')) - - -def main(): - fire.Fire(name='cipher') - -if __name__ == '__main__': - main() diff --git a/examples/cipher/cipher_test.py b/examples/cipher/cipher_test.py deleted file mode 100644 index d2fb5c5f..00000000 --- a/examples/cipher/cipher_test.py +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for the cipher module.""" - -from fire import testutils - -from examples.cipher import cipher - - -class CipherTest(testutils.BaseTestCase): - - def testCipher(self): - self.assertEqual(cipher.rot13('Hello world!'), 'Uryyb jbeyq!') - self.assertEqual(cipher.caesar_encode(13, 'Hello world!'), 'Uryyb jbeyq!') - self.assertEqual(cipher.caesar_decode(13, 'Uryyb jbeyq!'), 'Hello world!') - - self.assertEqual(cipher.caesar_encode(1, 'Hello world!'), 'Ifmmp xpsme!') - self.assertEqual(cipher.caesar_decode(1, 'Ifmmp xpsme!'), 'Hello world!') - - -if __name__ == '__main__': - testutils.main() diff --git a/examples/diff/__init__.py b/examples/diff/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/examples/diff/diff.py b/examples/diff/diff.py deleted file mode 100644 index f99e525e..00000000 --- a/examples/diff/diff.py +++ /dev/null @@ -1,105 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -r"""A command line tool for diffing files. - -The Python 2.7 documentation demonstrates how to make a command line interface -for the difflib library using optparse: -https://docs.python.org/2/library/difflib.html#a-command-line-interface-to-difflib - -This file demonstrates how to create a command line interface providing the same -functionality using Python Fire. - -Usage: - -diff FROMFILE TOFILE COMMAND [LINES] - -Arguments can be passed positionally or via the Flag syntax. -Using positional arguments, the usage is: - -diff FROMFILE TOFILE -diff FROMFILE TOFILE context-diff [LINES] -diff FROMFILE TOFILE unified-diff [LINES] -diff FROMFILE TOFILE ndiff -diff FROMFILE TOFILE make-file [CONTEXT] [LINES] - -Using the Flag syntax, the usage is: - -diff --fromfile=FROMFILE --tofile=TOFILE -diff --fromfile=FROMFILE --tofile=TOFILE context-diff [--lines=LINES] -diff --fromfile=FROMFILE --tofile=TOFILE unified-diff [--lines=LINES] -diff --fromfile=FROMFILE --tofile=TOFILE ndiff -diff --fromfile=FROMFILE --tofile=TOFILE make-file \ - [--context=CONTEXT] [--lines LINES] - -As with any Fire CLI, you can append '--' followed by any Flags to any command. - -The Flags available for all Fire CLIs are: - --help - --interactive - --trace - --separator=SEPARATOR - --completion - --verbose -""" - -import difflib -import os -import time - -import fire - - -class DiffLibWrapper(object): - """Provides a simple interface to the difflib module. - - The purpose of this simple interface is to offer a limited subset of the - difflib functionality as a command line interface. - """ - - def __init__(self, fromfile, tofile): - self._fromfile = fromfile - self._tofile = tofile - - self.fromdate = time.ctime(os.stat(fromfile).st_mtime) - self.todate = time.ctime(os.stat(tofile).st_mtime) - with open(fromfile) as f: - self.fromlines = f.readlines() - with open(tofile) as f: - self.tolines = f.readlines() - - def unified_diff(self, lines=3): - return difflib.unified_diff( - self.fromlines, self.tolines, self._fromfile, - self._tofile, self.fromdate, self.todate, n=lines) - - def ndiff(self): - return difflib.ndiff(self.fromlines, self.tolines) - - def make_file(self, context=False, lines=3): - return difflib.HtmlDiff().make_file( - self.fromlines, self.tolines, self._fromfile, self._tofile, - context=context, numlines=lines) - - def context_diff(self, lines=3): - return difflib.context_diff( - self.fromlines, self.tolines, self._fromfile, - self._tofile, self.fromdate, self.todate, n=lines) - - -def main(): - fire.Fire(DiffLibWrapper, name='diff') - -if __name__ == '__main__': - main() diff --git a/examples/diff/diff_test.py b/examples/diff/diff_test.py deleted file mode 100644 index 81a513c3..00000000 --- a/examples/diff/diff_test.py +++ /dev/null @@ -1,95 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for the diff and difffull modules.""" - -import tempfile - -from fire import testutils - -from examples.diff import diff -from examples.diff import difffull - - -class DiffTest(testutils.BaseTestCase): - """The purpose of these tests is to ensure the difflib wrappers works. - - It is not the goal of these tests to exhaustively test difflib functionality. - """ - - def setUp(self): - self.file1 = file1 = tempfile.NamedTemporaryFile() - self.file2 = file2 = tempfile.NamedTemporaryFile() - - file1.write(b'test\ntest1\n') - file2.write(b'test\ntest2\nextraline\n') - - file1.flush() - file2.flush() - - self.diff = diff.DiffLibWrapper(file1.name, file2.name) - - def testSetUp(self): - self.assertEqual(self.diff.fromlines, ['test\n', 'test1\n']) - self.assertEqual(self.diff.tolines, ['test\n', 'test2\n', 'extraline\n']) - - def testUnifiedDiff(self): - results = list(self.diff.unified_diff()) - self.assertTrue(results[0].startswith('--- ' + self.file1.name)) - self.assertTrue(results[1].startswith('+++ ' + self.file2.name)) - self.assertEqual( - results[2:], - [ - '@@ -1,2 +1,3 @@\n', - ' test\n', - '-test1\n', - '+test2\n', - '+extraline\n', - ] - ) - - def testContextDiff(self): - expected_lines = [ - '***************\n', - '*** 1,2 ****\n', - ' test\n', - '! test1\n', - '--- 1,3 ----\n', - ' test\n', - '! test2\n', - '! extraline\n'] - results = list(self.diff.context_diff()) - self.assertEqual(results[2:], expected_lines) - - def testNDiff(self): - expected_lines = [ - ' test\n', - '- test1\n', - '? ^\n', - '+ test2\n', - '? ^\n', - '+ extraline\n'] - results = list(self.diff.ndiff()) - self.assertEqual(results, expected_lines) - - def testMakeDiff(self): - self.assertTrue(''.join(self.diff.make_file()).startswith('\n encoding => unicode works. - - Args: - buf: The console output string to convert. - - Returns: - The console output string buf converted to unicode. - """ - if isinstance(buf, str): - buf = buf.encode(self._encoding) - return str(buf, self._encoding, 'replace') - - def GetBoxLineCharacters(self): - """Returns the box/line drawing characters object. - - The element names are from ISO 8879:1986//ENTITIES Box and Line Drawing//EN: - http://www.w3.org/2003/entities/iso8879doc/isobox.html - - Returns: - A BoxLineCharacters object for the console output device. - """ - return self._box_line_characters - - def GetBullets(self): - """Returns the bullet characters list. - - Use the list elements in order for best appearance in nested bullet lists, - wrapping back to the first element for deep nesting. The list size depends - on the console implementation. - - Returns: - A tuple of bullet characters. - """ - return self._bullets - - def GetProgressTrackerSymbols(self): - """Returns the progress tracker characters object. - - Returns: - A ProgressTrackerSymbols object for the console output device. - """ - return self._progress_tracker_symbols - - def GetControlSequenceIndicator(self): - """Returns the control sequence indicator string. - - Returns: - The control sequence indicator string or None if control sequences are not - supported. - """ - return self._csi - - def GetControlSequenceLen(self, buf): - """Returns the control sequence length at the beginning of buf. - - Used in display width computations. Control sequences have display width 0. - - Args: - buf: The string to check for a control sequence. - - Returns: - The control sequence length at the beginning of buf or 0 if buf does not - start with a control sequence. - """ - if not self._csi or not buf.startswith(self._csi): - return 0 - n = 0 - for c in buf: - n += 1 - if c.isalpha(): - break - return n - - def GetEncoding(self): - """Returns the current encoding.""" - return self._encoding - - def GetFontCode(self, bold=False, italic=False): - """Returns a font code string for 0 or more embellishments. - - GetFontCode() with no args returns the default font code string. - - Args: - bold: True for bold embellishment. - italic: True for italic embellishment. - - Returns: - The font code string for the requested embellishments. Write this string - to the console output to control the font settings. - """ - if not self._csi: - return '' - codes = [] - if bold: - codes.append(self._font_bold) - if italic: - codes.append(self._font_italic) - return '{csi}{codes}m'.format(csi=self._csi, codes=';'.join(codes)) - - def GetRawKey(self): - """Reads one key press from stdin with no echo. - - Returns: - The key name, None for EOF, for function keys, otherwise a - character. - """ - return self._get_raw_key[0]() - - def GetTermIdentifier(self): - """Returns the TERM environment variable for the console. - - Returns: - str: A str that describes the console's text capabilities - """ - return self._term - - def GetTermSize(self): - """Returns the terminal (x, y) dimensions in characters. - - Returns: - (x, y): A tuple of the terminal x and y dimensions. - """ - return self._term_size - - def DisplayWidth(self, buf): - """Returns the display width of buf, handling unicode and ANSI controls. - - Args: - buf: The string to count from. - - Returns: - The display width of buf, handling unicode and ANSI controls. - """ - if not isinstance(buf, str): - # Handle non-string objects like Colorizer(). - return len(buf) - - cached = self._display_width_cache.get(buf, None) - if cached is not None: - return cached - - width = 0 - max_width = 0 - i = 0 - while i < len(buf): - if self._csi and buf[i:].startswith(self._csi): - i += self.GetControlSequenceLen(buf[i:]) - elif buf[i] == '\n': - # A newline incidates the start of a new line. - # Newline characters have 0 width. - max_width = max(width, max_width) - width = 0 - i += 1 - else: - width += GetCharacterDisplayWidth(buf[i]) - i += 1 - max_width = max(width, max_width) - - self._display_width_cache[buf] = max_width - return max_width - - def SplitIntoNormalAndControl(self, buf): - """Returns a list of (normal_string, control_sequence) tuples from buf. - - Args: - buf: The input string containing one or more control sequences - interspersed with normal strings. - - Returns: - A list of (normal_string, control_sequence) tuples. - """ - if not self._csi or not buf: - return [(buf, '')] - seq = [] - i = 0 - while i < len(buf): - c = buf.find(self._csi, i) - if c < 0: - seq.append((buf[i:], '')) - break - normal = buf[i:c] - i = c + self.GetControlSequenceLen(buf[c:]) - seq.append((normal, buf[c:i])) - return seq - - def SplitLine(self, line, width): - """Splits line into width length chunks. - - Args: - line: The line to split. - width: The width of each chunk except the last which could be smaller than - width. - - Returns: - A list of chunks, all but the last with display width == width. - """ - lines = [] - chunk = '' - w = 0 - keep = False - for normal, control in self.SplitIntoNormalAndControl(line): - keep = True - while True: - n = width - w - w += len(normal) - if w <= width: - break - lines.append(chunk + normal[:n]) - chunk = '' - keep = False - w = 0 - normal = normal[n:] - chunk += normal + control - if chunk or keep: - lines.append(chunk) - return lines - - def SupportsAnsi(self): - return (self._encoding != 'ascii' and - ('screen' in self._term or 'xterm' in self._term)) - - -class Colorizer(object): - """Resource string colorizer. - - Attributes: - _con: ConsoleAttr object. - _color: Color name. - _string: The string to colorize. - _justify: The justification function, no justification if None. For example, - justify=lambda s: s.center(10) - """ - - def __init__(self, string, color, justify=None): - """Constructor. - - Args: - string: The string to colorize. - color: Color name used to index ConsoleAttr._ANSI_COLOR. - justify: The justification function, no justification if None. For - example, justify=lambda s: s.center(10) - """ - self._con = GetConsoleAttr() - self._color = color - self._string = string - self._justify = justify - - def __eq__(self, other): - return self._string == str(other) - - def __ne__(self, other): - return not self == other - - def __gt__(self, other): - return self._string > str(other) - - def __lt__(self, other): - return self._string < str(other) - - def __ge__(self, other): - return not self < other - - def __le__(self, other): - return not self > other - - def __len__(self): - return self._con.DisplayWidth(self._string) - - def __str__(self): - return self._string - - def Render(self, stream, justify=None): - """Renders the string as self._color on the console. - - Args: - stream: The stream to render the string to. The stream given here *must* - have the same encoding as sys.stdout for this to work properly. - justify: The justification function, self._justify if None. - """ - stream.write( - self._con.Colorize(self._string, self._color, justify or self._justify)) - - -def GetConsoleAttr(encoding=None, reset=False): - """Gets the console attribute state. - - If this is the first call or reset is True or encoding is not None and does - not match the current encoding or out is not None and does not match the - current out then the state is (re)initialized. Otherwise the current state - is returned. - - This call associates the out file stream with the console. All console related - output should go to the same stream. - - Args: - encoding: Encoding override. - ascii -- ASCII. This is the default. - utf8 -- UTF-8 unicode. - win -- Windows code page 437. - reset: Force re-initialization if True. - - Returns: - The global ConsoleAttr state object. - """ - attr = ConsoleAttr._CONSOLE_ATTR_STATE # pylint: disable=protected-access - if not reset: - if not attr: - reset = True - elif encoding and encoding != attr.GetEncoding(): - reset = True - if reset: - attr = ConsoleAttr(encoding=encoding) - ConsoleAttr._CONSOLE_ATTR_STATE = attr # pylint: disable=protected-access - return attr - - -def ResetConsoleAttr(encoding=None): - """Resets the console attribute state to the console default. - - Args: - encoding: Reset to this encoding instead of the default. - ascii -- ASCII. This is the default. - utf8 -- UTF-8 unicode. - win -- Windows code page 437. - - Returns: - The global ConsoleAttr state object. - """ - return GetConsoleAttr(encoding=encoding, reset=True) - - -def GetCharacterDisplayWidth(char): - """Returns the monospaced terminal display width of char. - - Assumptions: - - monospaced display - - ambiguous or unknown chars default to width 1 - - ASCII control char width is 1 => don't use this for control chars - - Args: - char: The character to determine the display width of. - - Returns: - The monospaced terminal display width of char: either 0, 1, or 2. - """ - if not isinstance(char, str): - # Non-unicode chars have width 1. Don't use this function on control chars. - return 1 - - # Normalize to avoid special cases. - char = unicodedata.normalize('NFC', char) - - if unicodedata.combining(char) != 0: - # Modifies the previous character and does not move the cursor. - return 0 - elif unicodedata.category(char) == 'Cf': - # Unprintable formatting char. - return 0 - elif unicodedata.east_asian_width(char) in 'FW': - # Fullwidth or Wide chars take 2 character positions. - return 2 - else: - # Don't use this function on control chars. - return 1 - - -def SafeText(data, encoding=None, escape=True): - br"""Converts the data to a text string compatible with the given encoding. - - This works the same way as Decode() below except it guarantees that any - characters in the resulting text string can be re-encoded using the given - encoding (or GetConsoleAttr().GetEncoding() if None is given). This means - that the string will be safe to print to sys.stdout (for example) without - getting codec exceptions if the user's terminal doesn't support the encoding - used by the source of the text. - - Args: - data: Any bytes, string, or object that has str() or unicode() methods. - encoding: The encoding name to ensure compatibility with. Defaults to - GetConsoleAttr().GetEncoding(). - escape: Replace unencodable characters with a \uXXXX or \xXX equivalent if - True. Otherwise replace unencodable characters with an appropriate unknown - character, '?' for ASCII, and the unicode unknown replacement character - \uFFFE for unicode. - - Returns: - A text string representation of the data, but modified to remove any - characters that would result in an encoding exception with the target - encoding. In the worst case, with escape=False, it will contain only ? - characters. - """ - if data is None: - return 'None' - encoding = encoding or GetConsoleAttr().GetEncoding() - string = encoding_util.Decode(data, encoding=encoding) - - try: - # No change needed if the string encodes to the output encoding. - string.encode(encoding) - return string - except UnicodeError: - # The string does not encode to the output encoding. Encode it with error - # handling then convert it back into a text string (which will be - # guaranteed to only contain characters that can be encoded later. - return (string - .encode(encoding, 'backslashreplace' if escape else 'replace') - .decode(encoding)) - - -def EncodeToBytes(data): - r"""Encode data to bytes. - - The primary use case is for base64/mime style 7-bit ascii encoding where the - encoder input must be bytes. "safe" means that the conversion always returns - bytes and will not raise codec exceptions. - - If data is text then an 8-bit ascii encoding is attempted, then the console - encoding, and finally utf-8. - - Args: - data: Any bytes, string, or object that has str() or unicode() methods. - - Returns: - A bytes string representation of the data. - """ - if data is None: - return b'' - if isinstance(data, bytes): - # Already bytes - our work is done. - return data - - # Coerce to text that will be converted to bytes. - s = str(data) - - try: - # Assume the text can be directly converted to bytes (8-bit ascii). - return s.encode('iso-8859-1') - except UnicodeEncodeError: - pass - - try: - # Try the output encoding. - return s.encode(GetConsoleAttr().GetEncoding()) - except UnicodeEncodeError: - pass - - # Punt to utf-8. - return s.encode('utf-8') - - -def Decode(data, encoding=None): - """Converts the given string, bytes, or object to a text string. - - Args: - data: Any bytes, string, or object that has str() or unicode() methods. - encoding: A suggesting encoding used to decode. If this encoding doesn't - work, other defaults are tried. Defaults to - GetConsoleAttr().GetEncoding(). - - Returns: - A text string representation of the data. - """ - encoding = encoding or GetConsoleAttr().GetEncoding() - return encoding_util.Decode(data, encoding=encoding) diff --git a/fire/console/console_attr_os.py b/fire/console/console_attr_os.py deleted file mode 100644 index a7f38d4f..00000000 --- a/fire/console/console_attr_os.py +++ /dev/null @@ -1,257 +0,0 @@ -# -*- coding: utf-8 -*- # -# Copyright 2015 Google LLC. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""OS specific console_attr helper functions.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import unicode_literals - -import os -import sys - -from fire.console import encoding - - -def GetTermSize(): - """Gets the terminal x and y dimensions in characters. - - _GetTermSize*() helper functions taken from: - http://stackoverflow.com/questions/263890/ - - Returns: - (columns, lines): A tuple containing the terminal x and y dimensions. - """ - xy = None - # Believe the first helper that doesn't bail. - for get_terminal_size in (_GetTermSizePosix, - _GetTermSizeWindows, - _GetTermSizeEnvironment, - _GetTermSizeTput): - try: - xy = get_terminal_size() - if xy: - break - except: # pylint: disable=bare-except - pass - return xy or (80, 24) - - -def _GetTermSizePosix(): - """Returns the Posix terminal x and y dimensions.""" - # pylint: disable=g-import-not-at-top - import fcntl - # pylint: disable=g-import-not-at-top - import struct - # pylint: disable=g-import-not-at-top - import termios - - def _GetXY(fd): - """Returns the terminal (x,y) size for fd. - - Args: - fd: The terminal file descriptor. - - Returns: - The terminal (x,y) size for fd or None on error. - """ - try: - # This magic incantation converts a struct from ioctl(2) containing two - # binary shorts to a (rows, columns) int tuple. - rc = struct.unpack(b'hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, b'junk')) - return (rc[1], rc[0]) if rc else None - except: # pylint: disable=bare-except - return None - - xy = _GetXY(0) or _GetXY(1) or _GetXY(2) - if not xy: - fd = None - try: - fd = os.open(os.ctermid(), os.O_RDONLY) - xy = _GetXY(fd) - except: # pylint: disable=bare-except - xy = None - finally: - if fd is not None: - os.close(fd) - return xy - - -def _GetTermSizeWindows(): - """Returns the Windows terminal x and y dimensions.""" - # pylint:disable=g-import-not-at-top - import struct - # pylint: disable=g-import-not-at-top - from ctypes import create_string_buffer - # pylint:disable=g-import-not-at-top - from ctypes import windll - - # stdin handle is -10 - # stdout handle is -11 - # stderr handle is -12 - - h = windll.kernel32.GetStdHandle(-12) - csbi = create_string_buffer(22) - if not windll.kernel32.GetConsoleScreenBufferInfo(h, csbi): - return None - (unused_bufx, unused_bufy, unused_curx, unused_cury, unused_wattr, - left, top, right, bottom, - unused_maxx, unused_maxy) = struct.unpack(b'hhhhHhhhhhh', csbi.raw) - x = right - left + 1 - y = bottom - top + 1 - return (x, y) - - -def _GetTermSizeEnvironment(): - """Returns the terminal x and y dimensions from the environment.""" - return (int(os.environ['COLUMNS']), int(os.environ['LINES'])) - - -def _GetTermSizeTput(): - """Returns the terminal x and y dimensions from tput(1).""" - import subprocess # pylint: disable=g-import-not-at-top - output = encoding.Decode(subprocess.check_output(['tput', 'cols'], - stderr=subprocess.STDOUT)) - cols = int(output) - output = encoding.Decode(subprocess.check_output(['tput', 'lines'], - stderr=subprocess.STDOUT)) - rows = int(output) - return (cols, rows) - - -_ANSI_CSI = '\x1b' # ANSI control sequence indicator (ESC) -_CONTROL_D = '\x04' # unix EOF (^D) -_CONTROL_Z = '\x1a' # Windows EOF (^Z) -_WINDOWS_CSI_1 = '\x00' # Windows control sequence indicator #1 -_WINDOWS_CSI_2 = '\xe0' # Windows control sequence indicator #2 - - -def GetRawKeyFunction(): - """Returns a function that reads one keypress from stdin with no echo. - - Returns: - A function that reads one keypress from stdin with no echo or a function - that always returns None if stdin does not support it. - """ - # Believe the first helper that doesn't bail. - for get_raw_key_function in (_GetRawKeyFunctionPosix, - _GetRawKeyFunctionWindows): - try: - return get_raw_key_function() - except: # pylint: disable=bare-except - pass - return lambda: None - - -def _GetRawKeyFunctionPosix(): - """_GetRawKeyFunction helper using Posix APIs.""" - # pylint: disable=g-import-not-at-top - import tty - # pylint: disable=g-import-not-at-top - import termios - - def _GetRawKeyPosix(): - """Reads and returns one keypress from stdin, no echo, using Posix APIs. - - Returns: - The key name, None for EOF, <*> for function keys, otherwise a - character. - """ - ansi_to_key = { - 'A': '', - 'B': '', - 'D': '', - 'C': '', - '5': '', - '6': '', - 'H': '', - 'F': '', - 'M': '', - 'S': '', - 'T': '', - } - - # Flush pending output. sys.stdin.read() would do this, but it's explicitly - # bypassed in _GetKeyChar(). - sys.stdout.flush() - - fd = sys.stdin.fileno() - - def _GetKeyChar(): - return encoding.Decode(os.read(fd, 1)) - - old_settings = termios.tcgetattr(fd) - try: - tty.setraw(fd) - c = _GetKeyChar() - if c == _ANSI_CSI: - c = _GetKeyChar() - while True: - if c == _ANSI_CSI: - return c - if c.isalpha(): - break - prev_c = c - c = _GetKeyChar() - if c == '~': - c = prev_c - break - return ansi_to_key.get(c, '') - except: # pylint:disable=bare-except - c = None - finally: - termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) - return None if c in (_CONTROL_D, _CONTROL_Z) else c - - return _GetRawKeyPosix - - -def _GetRawKeyFunctionWindows(): - """_GetRawKeyFunction helper using Windows APIs.""" - # pylint: disable=g-import-not-at-top - import msvcrt - - def _GetRawKeyWindows(): - """Reads and returns one keypress from stdin, no echo, using Windows APIs. - - Returns: - The key name, None for EOF, <*> for function keys, otherwise a - character. - """ - windows_to_key = { - 'H': '', - 'P': '', - 'K': '', - 'M': '', - 'I': '', - 'Q': '', - 'G': '', - 'O': '', - } - - # Flush pending output. sys.stdin.read() would do this it's explicitly - # bypassed in _GetKeyChar(). - sys.stdout.flush() - - def _GetKeyChar(): - return encoding.Decode(msvcrt.getch()) - - c = _GetKeyChar() - # Special function key is a two character sequence; return the second char. - if c in (_WINDOWS_CSI_1, _WINDOWS_CSI_2): - return windows_to_key.get(_GetKeyChar(), '') - return None if c in (_CONTROL_D, _CONTROL_Z) else c - - return _GetRawKeyWindows diff --git a/fire/console/console_io.py b/fire/console/console_io.py deleted file mode 100644 index ec0858d9..00000000 --- a/fire/console/console_io.py +++ /dev/null @@ -1,110 +0,0 @@ -# -*- coding: utf-8 -*- # -# Copyright 2013 Google LLC. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""General console printing utilities used by the Cloud SDK.""" - -import os -import signal -import subprocess -import sys - -from fire.console import console_attr -from fire.console import console_pager -from fire.console import encoding -from fire.console import files - - -def IsInteractive(output=False, error=False, heuristic=False): - """Determines if the current terminal session is interactive. - - sys.stdin must be a terminal input stream. - - Args: - output: If True then sys.stdout must also be a terminal output stream. - error: If True then sys.stderr must also be a terminal output stream. - heuristic: If True then we also do some additional heuristics to check if - we are in an interactive context. Checking home path for example. - - Returns: - True if the current terminal session is interactive. - """ - if not sys.stdin.isatty(): - return False - if output and not sys.stdout.isatty(): - return False - if error and not sys.stderr.isatty(): - return False - - if heuristic: - # Check the home path. Most startup scripts for example are executed by - # users that don't have a home path set. Home is OS dependent though, so - # check everything. - # *NIX OS usually sets the HOME env variable. It is usually '/home/user', - # but can also be '/root'. If it's just '/' we are most likely in an init - # script. - # Windows usually sets HOMEDRIVE and HOMEPATH. If they don't exist we are - # probably being run from a task scheduler context. HOMEPATH can be '\' - # when a user has a network mapped home directory. - # Cygwin has it all! Both Windows and Linux. Checking both is perfect. - home = os.getenv('HOME') - homepath = os.getenv('HOMEPATH') - if not homepath and (not home or home == '/'): - return False - return True - - -def More(contents, out, prompt=None, check_pager=True): - """Run a user specified pager or fall back to the internal pager. - - Args: - contents: The entire contents of the text lines to page. - out: The output stream. - prompt: The page break prompt. - check_pager: Checks the PAGER env var and uses it if True. - """ - if not IsInteractive(output=True): - out.write(contents) - return - if check_pager: - pager = encoding.GetEncodedValue(os.environ, 'PAGER', None) - if pager == '-': - # Use the fallback Pager. - pager = None - elif not pager: - # Search for a pager that handles ANSI escapes. - for command in ('less', 'pager'): - if files.FindExecutableOnPath(command): - pager = command - break - if pager: - # If the pager is less(1) then instruct it to display raw ANSI escape - # sequences to enable colors and font embellishments. - less_orig = encoding.GetEncodedValue(os.environ, 'LESS', None) - less = '-R' + (less_orig or '') - encoding.SetEncodedValue(os.environ, 'LESS', less) - # Ignore SIGINT while the pager is running. - # We don't want to terminate the parent while the child is still alive. - signal.signal(signal.SIGINT, signal.SIG_IGN) - p = subprocess.Popen(pager, stdin=subprocess.PIPE, shell=True) - enc = console_attr.GetConsoleAttr().GetEncoding() - p.communicate(input=contents.encode(enc)) - p.wait() - # Start using default signal handling for SIGINT again. - signal.signal(signal.SIGINT, signal.SIG_DFL) - if less_orig is None: - encoding.SetEncodedValue(os.environ, 'LESS', None) - return - # Fall back to the internal pager. - console_pager.Pager(contents, out, prompt).Run() diff --git a/fire/console/console_pager.py b/fire/console/console_pager.py deleted file mode 100644 index 565c7e1e..00000000 --- a/fire/console/console_pager.py +++ /dev/null @@ -1,299 +0,0 @@ -# -*- coding: utf-8 -*- # -# Copyright 2015 Google LLC. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Simple console pager.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import unicode_literals - -import re -import sys - -from fire.console import console_attr - - -class Pager(object): - """A simple console text pager. - - This pager requires the entire contents to be available. The contents are - written one page of lines at a time. The prompt is written after each page of - lines. A one character response is expected. See HELP_TEXT below for more - info. - - The contents are written as is. For example, ANSI control codes will be in - effect. This is different from pagers like more(1) which is ANSI control code - agnostic and miscalculates line lengths, and less(1) which displays control - character names by default. - - Attributes: - _attr: The current ConsoleAttr handle. - _clear: A string that clears the prompt when written to _out. - _contents: The entire contents of the text lines to page. - _height: The terminal height in characters. - _out: The output stream, log.out (effectively) if None. - _prompt: The page break prompt. - _search_direction: The search direction command, n:forward, N:reverse. - _search_pattern: The current forward/reverse search compiled RE. - _width: The termonal width in characters. - """ - - HELP_TEXT = """ - Simple pager commands: - - b, ^B, , - Back one page. - f, ^F, , , - Forward one page. Does not quit if there are no more lines. - g, - Back to the first page. - g - Go to lines from the top. - G, - Forward to the last page. - G - Go to lines from the bottom. - h - Print pager command help. - j, +, - Forward one line. - k, -, - Back one line. - /pattern - Forward search for pattern. - ?pattern - Backward search for pattern. - n - Repeat current search. - N - Repeat current search in the opposite direction. - q, Q, ^C, ^D, ^Z - Quit return to the caller. - any other character - Prompt again. - - Hit any key to continue:""" - - PREV_POS_NXT_REPRINT = -1, -1 - - def __init__(self, contents, out=None, prompt=None): - """Constructor. - - Args: - contents: The entire contents of the text lines to page. - out: The output stream, log.out (effectively) if None. - prompt: The page break prompt, a default prompt is used if None.. - """ - self._contents = contents - self._out = out or sys.stdout - self._search_pattern = None - self._search_direction = None - - # prev_pos, prev_next values to force reprint - self.prev_pos, self.prev_nxt = self.PREV_POS_NXT_REPRINT - # Initialize the console attributes. - self._attr = console_attr.GetConsoleAttr() - self._width, self._height = self._attr.GetTermSize() - - # Initialize the prompt and the prompt clear string. - if not prompt: - prompt = '{bold}--({{percent}}%)--{normal}'.format( - bold=self._attr.GetFontCode(bold=True), - normal=self._attr.GetFontCode()) - self._clear = '\r{0}\r'.format(' ' * (self._attr.DisplayWidth(prompt) - 6)) - self._prompt = prompt - - # Initialize a list of lines with long lines split into separate display - # lines. - self._lines = [] - for line in contents.splitlines(): - self._lines += self._attr.SplitLine(line, self._width) - - def _Write(self, s): - """Mockable helper that writes s to self._out.""" - self._out.write(s) - - def _GetSearchCommand(self, c): - """Consumes a search command and returns the equivalent pager command. - - The search pattern is an RE that is pre-compiled and cached for subsequent - /, ?, n, or N commands. - - Args: - c: The search command char. - - Returns: - The pager command char. - """ - self._Write(c) - buf = '' - while True: - p = self._attr.GetRawKey() - if p in (None, '\n', '\r') or len(p) != 1: - break - self._Write(p) - buf += p - self._Write('\r' + ' ' * len(buf) + '\r') - if buf: - try: - self._search_pattern = re.compile(buf) - except re.error: - # Silently ignore pattern errors. - self._search_pattern = None - return '' - self._search_direction = 'n' if c == '/' else 'N' - return 'n' - - def _Help(self): - """Print command help and wait for any character to continue.""" - clear = self._height - (len(self.HELP_TEXT) - - len(self.HELP_TEXT.replace('\n', ''))) - if clear > 0: - self._Write('\n' * clear) - self._Write(self.HELP_TEXT) - self._attr.GetRawKey() - self._Write('\n') - - def Run(self): - """Run the pager.""" - # No paging if the contents are small enough. - if len(self._lines) <= self._height: - self._Write(self._contents) - return - - # We will not always reset previous values. - reset_prev_values = True - # Save room for the prompt at the bottom of the page. - self._height -= 1 - - # Loop over all the pages. - pos = 0 - while pos < len(self._lines): - # Write a page of lines. - nxt = pos + self._height - if nxt > len(self._lines): - nxt = len(self._lines) - pos = nxt - self._height - # Checks if the starting position is in between the current printed lines - # so we don't need to reprint all the lines. - if self.prev_pos < pos < self.prev_nxt: - # we start where the previous page ended. - self._Write('\n'.join(self._lines[self.prev_nxt:nxt]) + '\n') - elif pos != self.prev_pos and nxt != self.prev_nxt: - self._Write('\n'.join(self._lines[pos:nxt]) + '\n') - - # Handle the prompt response. - percent = self._prompt.format(percent=100 * nxt // len(self._lines)) - digits = '' - while True: - # We want to reset prev values if we just exited out of the while loop - if reset_prev_values: - self.prev_pos, self.prev_nxt = pos, nxt - reset_prev_values = False - self._Write(percent) - c = self._attr.GetRawKey() - self._Write(self._clear) - - # Parse the command. - if c in (None, # EOF. - 'q', # Quit. - 'Q', # Quit. - '\x03', # ^C (unix & windows terminal interrupt) - '\x1b', # ESC. - ): - # Quit. - return - elif c in ('/', '?'): - c = self._GetSearchCommand(c) - elif c.isdigit(): - # Collect digits for operation count. - digits += c - continue - - # Set the optional command count. - if digits: - count = int(digits) - digits = '' - else: - count = 0 - - # Finally commit to command c. - if c in ('', '', 'b', '\x02'): - # Previous page. - nxt = pos - self._height - if nxt < 0: - nxt = 0 - elif c in ('', '', 'f', '\x06', ' '): - # Next page. - if nxt >= len(self._lines): - continue - nxt = pos + self._height - if nxt >= len(self._lines): - nxt = pos - elif c in ('', 'g'): - # First page. - nxt = count - 1 - if nxt > len(self._lines) - self._height: - nxt = len(self._lines) - self._height - if nxt < 0: - nxt = 0 - elif c in ('', 'G'): - # Last page. - nxt = len(self._lines) - count - if nxt > len(self._lines) - self._height: - nxt = len(self._lines) - self._height - if nxt < 0: - nxt = 0 - elif c == 'h': - self._Help() - # Special case when we want to reprint the previous display. - self.prev_pos, self.prev_nxt = self.PREV_POS_NXT_REPRINT - nxt = pos - break - elif c in ('', 'j', '+', '\n', '\r'): - # Next line. - if nxt >= len(self._lines): - continue - nxt = pos + 1 - if nxt >= len(self._lines): - nxt = pos - elif c in ('', 'k', '-'): - # Previous line. - nxt = pos - 1 - if nxt < 0: - nxt = 0 - elif c in ('n', 'N'): - # Next pattern match search. - if not self._search_pattern: - continue - nxt = pos - i = pos - direction = 1 if c == self._search_direction else -1 - while True: - i += direction - if i < 0 or i >= len(self._lines): - break - if self._search_pattern.search(self._lines[i]): - nxt = i - break - else: - # Silently ignore everything else. - continue - if nxt != pos: - # We will exit the while loop because position changed so we can reset - # prev values. - reset_prev_values = True - break - pos = nxt diff --git a/fire/console/encoding.py b/fire/console/encoding.py deleted file mode 100644 index 662342c6..00000000 --- a/fire/console/encoding.py +++ /dev/null @@ -1,189 +0,0 @@ -# -*- coding: utf-8 -*- # - -# Copyright 2015 Google LLC. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""A module for dealing with unknown string and environment encodings.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import unicode_literals - -import sys - - -def Encode(string, encoding=None): - """Encode the text string to a byte string. - - Args: - string: str, The text string to encode. - encoding: The suggested encoding if known. - - Returns: - str, The binary string. - """ - del encoding # Unused. - return string - - -def Decode(data, encoding=None): - """Returns string with non-ascii characters decoded to UNICODE. - - UTF-8, the suggested encoding, and the usual suspects will be attempted in - order. - - Args: - data: A string or object that has str() and unicode() methods that may - contain an encoding incompatible with the standard output encoding. - encoding: The suggested encoding if known. - - Returns: - A text string representing the decoded byte string. - """ - if data is None: - return None - - # First we are going to get the data object to be a text string. - if isinstance(data, str) or isinstance(data, bytes): - string = data - else: - # Some non-string type of object. - string = str(data) - - if isinstance(string, str): - # Our work is done here. - return string - - try: - # Just return the string if its pure ASCII. - return string.decode('ascii') - except UnicodeError: - # The string is not ASCII encoded. - pass - - # Try the suggested encoding if specified. - if encoding: - try: - return string.decode(encoding) - except UnicodeError: - # Bad suggestion. - pass - - # Try UTF-8 because the other encodings could be extended ASCII. It would - # be exceptional if a valid extended ascii encoding with extended chars - # were also a valid UITF-8 encoding. - try: - return string.decode('utf8') - except UnicodeError: - # Not a UTF-8 encoding. - pass - - # Try the filesystem encoding. - try: - return string.decode(sys.getfilesystemencoding()) - except UnicodeError: - # string is not encoded for filesystem paths. - pass - - # Try the system default encoding. - try: - return string.decode(sys.getdefaultencoding()) - except UnicodeError: - # string is not encoded using the default encoding. - pass - - # We don't know the string encoding. - # This works around a Python str.encode() "feature" that throws - # an ASCII *decode* exception on str strings that contain 8th bit set - # bytes. For example, this sequence throws an exception: - # string = '\xdc' # iso-8859-1 'Ü' - # string = string.encode('ascii', 'backslashreplace') - # even though 'backslashreplace' is documented to handle encoding - # errors. We work around the problem by first decoding the str string - # from an 8-bit encoding to unicode, selecting any 8-bit encoding that - # uses all 256 bytes (such as ISO-8559-1): - # string = string.decode('iso-8859-1') - # Using this produces a sequence that works: - # string = '\xdc' - # string = string.decode('iso-8859-1') - # string = string.encode('ascii', 'backslashreplace') - return string.decode('iso-8859-1') - - -def GetEncodedValue(env, name, default=None): - """Returns the decoded value of the env var name. - - Args: - env: {str: str}, The env dict. - name: str, The env var name. - default: The value to return if name is not in env. - - Returns: - The decoded value of the env var name. - """ - name = Encode(name) - value = env.get(name) - if value is None: - return default - # In Python 3, the environment sets and gets accept and return text strings - # only, and it handles the encoding itself so this is not necessary. - return Decode(value) - - -def SetEncodedValue(env, name, value, encoding=None): - """Sets the value of name in env to an encoded value. - - Args: - env: {str: str}, The env dict. - name: str, The env var name. - value: str or unicode, The value for name. If None then name is removed from - env. - encoding: str, The encoding to use or None to try to infer it. - """ - # Python 2 *and* 3 unicode support falls apart at filesystem/argv/environment - # boundaries. The encoding used for filesystem paths and environment variable - # names/values is under user control on most systems. With one of those values - # in hand there is no way to tell exactly how the value was encoded. We get - # some reasonable hints from sys.getfilesystemencoding() or - # sys.getdefaultencoding() and use them to encode values that the receiving - # process will have a chance at decoding. Leaving the values as unicode - # strings will cause os module Unicode exceptions. What good is a language - # unicode model when the module support could care less? - name = Encode(name, encoding=encoding) - if value is None: - env.pop(name, None) - return - env[name] = Encode(value, encoding=encoding) - - -def EncodeEnv(env, encoding=None): - """Encodes all the key value pairs in env in preparation for subprocess. - - Args: - env: {str: str}, The environment you are going to pass to subprocess. - encoding: str, The encoding to use or None to use the default. - - Returns: - {bytes: bytes}, The environment to pass to subprocess. - """ - encoding = encoding or _GetEncoding() - return { - Encode(k, encoding=encoding): Encode(v, encoding=encoding) - for k, v in env.items() - } - - -def _GetEncoding(): - """Gets the default encoding to use.""" - return sys.getfilesystemencoding() or sys.getdefaultencoding() diff --git a/fire/console/files.py b/fire/console/files.py deleted file mode 100644 index 97222c3d..00000000 --- a/fire/console/files.py +++ /dev/null @@ -1,114 +0,0 @@ -# -*- coding: utf-8 -*- # -# Copyright 2013 Google LLC. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Some general file utilities used that can be used by the Cloud SDK.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import unicode_literals - -import os - -from fire.console import encoding as encoding_util -from fire.console import platforms - - -def _GetSystemPath(): - """Returns properly encoded system PATH variable string.""" - return encoding_util.GetEncodedValue(os.environ, 'PATH') - - -def _FindExecutableOnPath(executable, path, pathext): - """Internal function to a find an executable. - - Args: - executable: The name of the executable to find. - path: A list of directories to search separated by 'os.pathsep'. - pathext: An iterable of file name extensions to use. - - Returns: - str, the path to a file on `path` with name `executable` + `p` for - `p` in `pathext`. - - Raises: - ValueError: invalid input. - """ - - if isinstance(pathext, str): - raise ValueError('_FindExecutableOnPath(..., pathext=\'{0}\') failed ' - 'because pathext must be an iterable of strings, but got ' - 'a string.'.format(pathext)) - - # Prioritize preferred extension over earlier in path. - for ext in pathext: - for directory in path.split(os.pathsep): - # Windows can have paths quoted. - directory = directory.strip('"') - full = os.path.normpath(os.path.join(directory, executable) + ext) - # On Windows os.access(full, os.X_OK) is always True. - if os.path.isfile(full) and os.access(full, os.X_OK): - return full - return None - - -def _PlatformExecutableExtensions(platform): - if platform == platforms.OperatingSystem.WINDOWS: - return ('.exe', '.cmd', '.bat', '.com', '.ps1') - else: - return ('', '.sh') - - -def FindExecutableOnPath(executable, path=None, pathext=None, - allow_extensions=False): - """Searches for `executable` in the directories listed in `path` or $PATH. - - Executable must not contain a directory or an extension. - - Args: - executable: The name of the executable to find. - path: A list of directories to search separated by 'os.pathsep'. If None - then the system PATH is used. - pathext: An iterable of file name extensions to use. If None then - platform specific extensions are used. - allow_extensions: A boolean flag indicating whether extensions in the - executable are allowed. - - Returns: - The path of 'executable' (possibly with a platform-specific extension) if - found and executable, None if not found. - - Raises: - ValueError: if executable has a path or an extension, and extensions are - not allowed, or if there's an internal error. - """ - - if not allow_extensions and os.path.splitext(executable)[1]: - raise ValueError('FindExecutableOnPath({0},...) failed because first ' - 'argument must not have an extension.'.format(executable)) - - if os.path.dirname(executable): - raise ValueError('FindExecutableOnPath({0},...) failed because first ' - 'argument must not have a path.'.format(executable)) - - if path is None: - effective_path = _GetSystemPath() - else: - effective_path = path - effective_pathext = (pathext if pathext is not None - else _PlatformExecutableExtensions( - platforms.OperatingSystem.Current())) - - return _FindExecutableOnPath(executable, effective_path, - effective_pathext) diff --git a/fire/console/platforms.py b/fire/console/platforms.py deleted file mode 100644 index 13fd8204..00000000 --- a/fire/console/platforms.py +++ /dev/null @@ -1,483 +0,0 @@ -# -*- coding: utf-8 -*- # -# Copyright 2013 Google LLC. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Utilities for determining the current platform and architecture.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import unicode_literals - -import os -import platform -import subprocess -import sys - - -class Error(Exception): - """Base class for exceptions in the platforms module.""" - pass - - -class InvalidEnumValue(Error): # pylint: disable=g-bad-exception-name - """Exception for when a string could not be parsed to a valid enum value.""" - - def __init__(self, given, enum_type, options): - """Constructs a new exception. - - Args: - given: str, The given string that could not be parsed. - enum_type: str, The human readable name of the enum you were trying to - parse. - options: list(str), The valid values for this enum. - """ - super(InvalidEnumValue, self).__init__( - 'Could not parse [{0}] into a valid {1}. Valid values are [{2}]' - .format(given, enum_type, ', '.join(options))) - - -class OperatingSystem(object): - """An enum representing the operating system you are running on.""" - - class _OS(object): - """A single operating system.""" - - # pylint: disable=redefined-builtin - def __init__(self, id, name, file_name): - self.id = id - self.name = name - self.file_name = file_name - - def __str__(self): - return self.id - - def __eq__(self, other): - return (isinstance(other, type(self)) and - self.id == other.id and - self.name == other.name and - self.file_name == other.file_name) - - def __hash__(self): - return hash(self.id) + hash(self.name) + hash(self.file_name) - - def __ne__(self, other): - return not self == other - - @classmethod - def _CmpHelper(cls, x, y): - """Just a helper equivalent to the cmp() function in Python 2.""" - return (x > y) - (x < y) - - def __lt__(self, other): - return self._CmpHelper( - (self.id, self.name, self.file_name), - (other.id, other.name, other.file_name)) < 0 - - def __gt__(self, other): - return self._CmpHelper( - (self.id, self.name, self.file_name), - (other.id, other.name, other.file_name)) > 0 - - def __le__(self, other): - return not self.__gt__(other) - - def __ge__(self, other): - return not self.__lt__(other) - - WINDOWS = _OS('WINDOWS', 'Windows', 'windows') - MACOSX = _OS('MACOSX', 'Mac OS X', 'darwin') - LINUX = _OS('LINUX', 'Linux', 'linux') - CYGWIN = _OS('CYGWIN', 'Cygwin', 'cygwin') - MSYS = _OS('MSYS', 'Msys', 'msys') - _ALL = [WINDOWS, MACOSX, LINUX, CYGWIN, MSYS] - - @staticmethod - def AllValues(): - """Gets all possible enum values. - - Returns: - list, All the enum values. - """ - return list(OperatingSystem._ALL) - - @staticmethod - def FromId(os_id, error_on_unknown=True): - """Gets the enum corresponding to the given operating system id. - - Args: - os_id: str, The operating system id to parse - error_on_unknown: bool, True to raise an exception if the id is unknown, - False to just return None. - - Raises: - InvalidEnumValue: If the given value cannot be parsed. - - Returns: - OperatingSystemTuple, One of the OperatingSystem constants or None if the - input is None. - """ - if not os_id: - return None - for operating_system in OperatingSystem._ALL: - if operating_system.id == os_id: - return operating_system - if error_on_unknown: - raise InvalidEnumValue(os_id, 'Operating System', - [value.id for value in OperatingSystem._ALL]) - return None - - @staticmethod - def Current(): - """Determines the current operating system. - - Returns: - OperatingSystemTuple, One of the OperatingSystem constants or None if it - cannot be determined. - """ - if os.name == 'nt': - return OperatingSystem.WINDOWS - elif 'linux' in sys.platform: - return OperatingSystem.LINUX - elif 'darwin' in sys.platform: - return OperatingSystem.MACOSX - elif 'cygwin' in sys.platform: - return OperatingSystem.CYGWIN - elif 'msys' in sys.platform: - return OperatingSystem.MSYS - return None - - @staticmethod - def IsWindows(): - """Returns True if the current operating system is Windows.""" - return OperatingSystem.Current() is OperatingSystem.WINDOWS - - -class Architecture(object): - """An enum representing the system architecture you are running on.""" - - class _ARCH(object): - """A single architecture.""" - - # pylint: disable=redefined-builtin - def __init__(self, id, name, file_name): - self.id = id - self.name = name - self.file_name = file_name - - def __str__(self): - return self.id - - def __eq__(self, other): - return (isinstance(other, type(self)) and - self.id == other.id and - self.name == other.name and - self.file_name == other.file_name) - - def __hash__(self): - return hash(self.id) + hash(self.name) + hash(self.file_name) - - def __ne__(self, other): - return not self == other - - @classmethod - def _CmpHelper(cls, x, y): - """Just a helper equivalent to the cmp() function in Python 2.""" - return (x > y) - (x < y) - - def __lt__(self, other): - return self._CmpHelper( - (self.id, self.name, self.file_name), - (other.id, other.name, other.file_name)) < 0 - - def __gt__(self, other): - return self._CmpHelper( - (self.id, self.name, self.file_name), - (other.id, other.name, other.file_name)) > 0 - - def __le__(self, other): - return not self.__gt__(other) - - def __ge__(self, other): - return not self.__lt__(other) - - x86 = _ARCH('x86', 'x86', 'x86') - x86_64 = _ARCH('x86_64', 'x86_64', 'x86_64') - ppc = _ARCH('PPC', 'PPC', 'ppc') - arm = _ARCH('arm', 'arm', 'arm') - _ALL = [x86, x86_64, ppc, arm] - - # Possible values for `uname -m` and what arch they map to. - # Examples of possible values: https://en.wikipedia.org/wiki/Uname - _MACHINE_TO_ARCHITECTURE = { - 'amd64': x86_64, 'x86_64': x86_64, 'i686-64': x86_64, - 'i386': x86, 'i686': x86, 'x86': x86, - 'ia64': x86, # Itanium is different x64 arch, treat it as the common x86. - 'powerpc': ppc, 'power macintosh': ppc, 'ppc64': ppc, - 'armv6': arm, 'armv6l': arm, 'arm64': arm, 'armv7': arm, 'armv7l': arm} - - @staticmethod - def AllValues(): - """Gets all possible enum values. - - Returns: - list, All the enum values. - """ - return list(Architecture._ALL) - - @staticmethod - def FromId(architecture_id, error_on_unknown=True): - """Gets the enum corresponding to the given architecture id. - - Args: - architecture_id: str, The architecture id to parse - error_on_unknown: bool, True to raise an exception if the id is unknown, - False to just return None. - - Raises: - InvalidEnumValue: If the given value cannot be parsed. - - Returns: - ArchitectureTuple, One of the Architecture constants or None if the input - is None. - """ - if not architecture_id: - return None - for arch in Architecture._ALL: - if arch.id == architecture_id: - return arch - if error_on_unknown: - raise InvalidEnumValue(architecture_id, 'Architecture', - [value.id for value in Architecture._ALL]) - return None - - @staticmethod - def Current(): - """Determines the current system architecture. - - Returns: - ArchitectureTuple, One of the Architecture constants or None if it cannot - be determined. - """ - return Architecture._MACHINE_TO_ARCHITECTURE.get(platform.machine().lower()) - - -class Platform(object): - """Holds an operating system and architecture.""" - - def __init__(self, operating_system, architecture): - """Constructs a new platform. - - Args: - operating_system: OperatingSystem, The OS - architecture: Architecture, The machine architecture. - """ - self.operating_system = operating_system - self.architecture = architecture - - def __str__(self): - return '{}-{}'.format(self.operating_system, self.architecture) - - @staticmethod - def Current(os_override=None, arch_override=None): - """Determines the current platform you are running on. - - Args: - os_override: OperatingSystem, A value to use instead of the current. - arch_override: Architecture, A value to use instead of the current. - - Returns: - Platform, The platform tuple of operating system and architecture. Either - can be None if it could not be determined. - """ - return Platform( - os_override if os_override else OperatingSystem.Current(), - arch_override if arch_override else Architecture.Current()) - - def UserAgentFragment(self): - """Generates the fragment of the User-Agent that represents the OS. - - Examples: - (Linux 3.2.5-gg1236) - (Windows NT 6.1.7601) - (Macintosh; PPC Mac OS X 12.4.0) - (Macintosh; Intel Mac OS X 12.4.0) - - Returns: - str, The fragment of the User-Agent string. - """ - # Below, there are examples of the value of platform.uname() per platform. - # platform.release() is uname[2], platform.version() is uname[3]. - if self.operating_system == OperatingSystem.LINUX: - # ('Linux', '', '3.2.5-gg1236', - # '#1 SMP Tue May 21 02:35:06 PDT 2013', 'x86_64', 'x86_64') - return '({name} {version})'.format( - name=self.operating_system.name, version=platform.release()) - elif self.operating_system == OperatingSystem.WINDOWS: - # ('Windows', '', '7', '6.1.7601', 'AMD64', - # 'Intel64 Family 6 Model 45 Stepping 7, GenuineIntel') - return '({name} NT {version})'.format( - name=self.operating_system.name, version=platform.version()) - elif self.operating_system == OperatingSystem.MACOSX: - # ('Darwin', '', '12.4.0', - # 'Darwin Kernel Version 12.4.0: Wed May 1 17:57:12 PDT 2013; - # root:xnu-2050.24.15~1/RELEASE_X86_64', 'x86_64', 'i386') - format_string = '(Macintosh; {name} Mac OS X {version})' - arch_string = (self.architecture.name - if self.architecture == Architecture.ppc else 'Intel') - return format_string.format( - name=arch_string, version=platform.release()) - else: - return '()' - - def AsyncPopenArgs(self): - """Returns the args for spawning an async process using Popen on this OS. - - Make sure the main process does not wait for the new process. On windows - this means setting the 0x8 creation flag to detach the process. - - Killing a group leader kills the whole group. Setting creation flag 0x200 on - Windows or running setsid on *nix makes sure the new process is in a new - session with the new process the group leader. This means it can't be killed - if the parent is killed. - - Finally, all file descriptors (FD) need to be closed so that waiting for the - output of the main process does not inadvertently wait for the output of the - new process, which means waiting for the termination of the new process. - If the new process wants to write to a file, it can open new FDs. - - Returns: - {str:}, The args for spawning an async process using Popen on this OS. - """ - args = {} - if self.operating_system == OperatingSystem.WINDOWS: - args['close_fds'] = True # This is enough to close _all_ FDs on windows. - detached_process = 0x00000008 - create_new_process_group = 0x00000200 - # 0x008 | 0x200 == 0x208 - args['creationflags'] = detached_process | create_new_process_group - else: - # Killing a group leader kills the whole group. - # Create a new session with the new process the group leader. - args['preexec_fn'] = os.setsid - args['close_fds'] = True # This closes all FDs _except_ 0, 1, 2 on *nix. - args['stdin'] = subprocess.PIPE - args['stdout'] = subprocess.PIPE - args['stderr'] = subprocess.PIPE - return args - - -class PythonVersion(object): - """Class to validate the Python version we are using. - - The Cloud SDK officially supports Python 2.7. - - However, many commands do work with Python 2.6, so we don't error out when - users are using this (we consider it sometimes "compatible" but not - "supported"). - """ - - # See class docstring for descriptions of what these mean - MIN_REQUIRED_PY2_VERSION = (2, 6) - MIN_SUPPORTED_PY2_VERSION = (2, 7) - MIN_SUPPORTED_PY3_VERSION = (3, 4) - ENV_VAR_MESSAGE = """\ - -If you have a compatible Python interpreter installed, you can use it by setting -the CLOUDSDK_PYTHON environment variable to point to it. - -""" - - def __init__(self, version=None): - if version: - self.version = version - elif hasattr(sys, 'version_info'): - self.version = sys.version_info[:2] - else: - self.version = None - - def SupportedVersionMessage(self, allow_py3): - if allow_py3: - return 'Please use Python version {0}.{1}.x or {2}.{3} and up.'.format( - PythonVersion.MIN_SUPPORTED_PY2_VERSION[0], - PythonVersion.MIN_SUPPORTED_PY2_VERSION[1], - PythonVersion.MIN_SUPPORTED_PY3_VERSION[0], - PythonVersion.MIN_SUPPORTED_PY3_VERSION[1]) - else: - return 'Please use Python version {0}.{1}.x.'.format( - PythonVersion.MIN_SUPPORTED_PY2_VERSION[0], - PythonVersion.MIN_SUPPORTED_PY2_VERSION[1]) - - def IsCompatible(self, allow_py3=False, raise_exception=False): - """Ensure that the Python version we are using is compatible. - - This will print an error message if not compatible. - - Compatible versions are 2.6 and 2.7 and > 3.4 if allow_py3 is True. - We don't guarantee support for 2.6 so we want to warn about it. - - Args: - allow_py3: bool, True if we should allow a Python 3 interpreter to run - gcloud. If False, this returns an error for Python 3. - raise_exception: bool, True to raise an exception rather than printing - the error and exiting. - - Raises: - Error: If not compatible and raise_exception is True. - - Returns: - bool, True if the version is valid, False otherwise. - """ - error = None - if not self.version: - # We don't know the version, not a good sign. - error = ('ERROR: Your current version of Python is not compatible with ' - 'the Google Cloud SDK. {0}\n' - .format(self.SupportedVersionMessage(allow_py3))) - else: - if self.version[0] < 3: - # Python 2 Mode - if self.version < PythonVersion.MIN_REQUIRED_PY2_VERSION: - error = ('ERROR: Python {0}.{1} is not compatible with the Google ' - 'Cloud SDK. {2}\n' - .format(self.version[0], self.version[1], - self.SupportedVersionMessage(allow_py3))) - else: - # Python 3 Mode - if not allow_py3: - error = ('ERROR: Python 3 and later is not compatible with the ' - 'Google Cloud SDK. {0}\n' - .format(self.SupportedVersionMessage(allow_py3))) - elif self.version < PythonVersion.MIN_SUPPORTED_PY3_VERSION: - error = ('ERROR: Python {0}.{1} is not compatible with the Google ' - 'Cloud SDK. {2}\n' - .format(self.version[0], self.version[1], - self.SupportedVersionMessage(allow_py3))) - - if error: - if raise_exception: - raise Error(error) - sys.stderr.write(error) - sys.stderr.write(PythonVersion.ENV_VAR_MESSAGE) - return False - - # Warn that 2.6 might not work. - if (self.version >= self.MIN_REQUIRED_PY2_VERSION and - self.version < self.MIN_SUPPORTED_PY2_VERSION): - sys.stderr.write("""\ -WARNING: Python 2.6.x is no longer officially supported by the Google Cloud SDK -and may not function correctly. {0} -{1}""".format(self.SupportedVersionMessage(allow_py3), - PythonVersion.ENV_VAR_MESSAGE)) - - return True diff --git a/fire/console/text.py b/fire/console/text.py deleted file mode 100644 index 73e68488..00000000 --- a/fire/console/text.py +++ /dev/null @@ -1,103 +0,0 @@ -# -*- coding: utf-8 -*- # -# Copyright 2018 Google LLC. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Semantic text objects that are used for styled outputting.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import unicode_literals - -import enum - - -class TextAttributes(object): - """Attributes to use to style text with.""" - - def __init__(self, format_str=None, color=None, attrs=None): - """Defines a set of attributes for a piece of text. - - Args: - format_str: (str), string that will be used to format the text - with. For example '[{}]', to enclose text in brackets. - color: (Colors), the color the text should be formatted with. - attrs: (Attrs), the attributes to apply to text. - """ - self._format_str = format_str - self._color = color - self._attrs = attrs or [] - - @property - def format_str(self): - return self._format_str - - @property - def color(self): - return self._color - - @property - def attrs(self): - return self._attrs - - -class TypedText(object): - """Text with a semantic type that will be used for styling.""" - - def __init__(self, texts, text_type=None): - """String of text and a corresponding type to use to style that text. - - Args: - texts: (list[str]), list of strs or TypedText objects - that should be styled using text_type. - text_type: (TextTypes), the semantic type of the text that - will be used to style text. - """ - self.texts = texts - self.text_type = text_type - - def __len__(self): - length = 0 - for text in self.texts: - length += len(text) - return length - - def __add__(self, other): - texts = [self, other] - return TypedText(texts) - - def __radd__(self, other): - texts = [other, self] - return TypedText(texts) - - -class _TextTypes(enum.Enum): - """Text types base class that defines base functionality.""" - - def __call__(self, *args): - """Returns a TypedText object using this style.""" - return TypedText(list(args), self) - - -# TODO: Add more types. -class TextTypes(_TextTypes): - """Defines text types that can be used for styling text.""" - RESOURCE_NAME = 1 - URL = 2 - USER_INPUT = 3 - COMMAND = 4 - INFO = 5 - URI = 6 - OUTPUT = 7 - PT_SUCCESS = 8 - PT_FAILURE = 9 - diff --git a/fire/core.py b/fire/core.py deleted file mode 100644 index 8e23e76b..00000000 --- a/fire/core.py +++ /dev/null @@ -1,994 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Python Fire is a library for creating CLIs from absolutely any Python object. - -You can call Fire on any Python object: -functions, classes, modules, objects, dictionaries, lists, tuples, etc. -They all work! - -Python Fire turns any Python object into a command line interface. -Simply call the Fire function as your main method to create a CLI. - -When using Fire to build a CLI, your main method includes a call to Fire. Eg: - -def main(argv): - fire.Fire(Component) - -A Fire CLI command is run by consuming the arguments in the command in order to -access a member of current component, call the current component (if it's a -function), or instantiate the current component (if it's a class). The target -component begins as Component, and at each operation the component becomes the -result of the preceding operation. - -For example "command fn arg1 arg2" might access the "fn" property of the initial -target component, and then call that function with arguments 'arg1' and 'arg2'. -Additional examples are available in the examples directory. - -Fire Flags, common to all Fire CLIs, must go after a separating "--". For -example, to get help for a command you might run: `command -- --help`. - -The available flags for all Fire CLIs are: - -v --verbose: Include private members in help and usage information. - -h --help: Provide help and usage information for the command. - -i --interactive: Drop into a Python REPL after running the command. - --completion: Write the Bash completion script for the tool to stdout. - --completion fish: Write the Fish completion script for the tool to stdout. - --separator SEPARATOR: Use SEPARATOR in place of the default separator, '-'. - --trace: Get the Fire Trace for the command. -""" - -import asyncio -import inspect -import json -import os -import re -import shlex -import sys -import types - -from fire import completion -from fire import decorators -from fire import formatting -from fire import helptext -from fire import inspectutils -from fire import interact -from fire import parser -from fire import trace -from fire import value_types -from fire.console import console_io - - -def Fire(component=None, command=None, name=None, serialize=None): - """This function, Fire, is the main entrypoint for Python Fire. - - Executes a command either from the `command` argument or from sys.argv by - recursively traversing the target object `component`'s members consuming - arguments, evaluating functions, and instantiating classes as it goes. - - When building a CLI with Fire, your main method should call this function. - - Args: - component: The initial target component. - command: Optional. If supplied, this is the command executed. If not - supplied, then the command is taken from sys.argv instead. This can be - a string or a list of strings; a list of strings is preferred. - name: Optional. The name of the command as entered at the command line. - Used in interactive mode and for generating the completion script. - serialize: Optional. If supplied, all objects are serialized to text via - the provided callable. - Returns: - The result of executing the Fire command. Execution begins with the initial - target component. The component is updated by using the command arguments - to either access a member of the current component, call the current - component (if it's a function), or instantiate the current component (if - it's a class). When all arguments are consumed and there's no function left - to call or class left to instantiate, the resulting current component is - the final result. - Raises: - ValueError: If the command argument is supplied, but not a string or a - sequence of arguments. - FireExit: When Fire encounters a FireError, Fire will raise a FireExit with - code 2. When used with the help or trace flags, Fire will raise a - FireExit with code 0 if successful. - """ - name = name or os.path.basename(sys.argv[0]) - - # Get args as a list. - if isinstance(command, str): - args = shlex.split(command) - elif isinstance(command, (list, tuple)): - args = command - elif command is None: - # Use the command line args by default if no command is specified. - args = sys.argv[1:] - else: - raise ValueError('The command argument must be a string or a sequence of ' - 'arguments.') - - args, flag_args = parser.SeparateFlagArgs(args) - - argparser = parser.CreateParser() - parsed_flag_args, unused_args = argparser.parse_known_args(flag_args) - - context = {} - if parsed_flag_args.interactive or component is None: - # Determine the calling context. - caller = inspect.stack()[1] - caller_frame = caller[0] - caller_globals = caller_frame.f_globals - caller_locals = caller_frame.f_locals - context.update(caller_globals) - context.update(caller_locals) - - component_trace = _Fire(component, args, parsed_flag_args, context, name) - - if component_trace.HasError(): - _DisplayError(component_trace) - raise FireExit(2, component_trace) - if component_trace.show_trace and component_trace.show_help: - output = [f'Fire trace:\n{component_trace}\n'] - result = component_trace.GetResult() - help_text = helptext.HelpText( - result, trace=component_trace, verbose=component_trace.verbose) - output.append(help_text) - Display(output, out=sys.stderr) - raise FireExit(0, component_trace) - if component_trace.show_trace: - output = [f'Fire trace:\n{component_trace}'] - Display(output, out=sys.stderr) - raise FireExit(0, component_trace) - if component_trace.show_help: - result = component_trace.GetResult() - help_text = helptext.HelpText( - result, trace=component_trace, verbose=component_trace.verbose) - output = [help_text] - Display(output, out=sys.stderr) - raise FireExit(0, component_trace) - - # The command succeeded normally; print the result. - _PrintResult( - component_trace, verbose=component_trace.verbose, serialize=serialize) - result = component_trace.GetResult() - return result - - -def Display(lines, out): - text = '\n'.join(lines) + '\n' - console_io.More(text, out=out) - - -def CompletionScript(name, component, shell): - """Returns the text of the completion script for a Fire CLI.""" - return completion.Script(name, component, shell=shell) - - -class FireError(Exception): - """Exception used by Fire when a Fire command cannot be executed. - - These exceptions are not raised by the Fire function, but rather are caught - and added to the FireTrace. - """ - - -class FireExit(SystemExit): # pylint: disable=g-bad-exception-name - """An exception raised by Fire to the client in the case of a FireError. - - The trace of the Fire program is available on the `trace` property. - - This exception inherits from SystemExit, so clients may explicitly catch it - with `except SystemExit` or `except FireExit`. If not caught, this exception - will cause the client program to exit without a stacktrace. - """ - - def __init__(self, code, component_trace): - """Constructs a FireExit exception. - - Args: - code: (int) Exit code for the Fire CLI. - component_trace: (FireTrace) The trace for the Fire command. - """ - super().__init__(code) - self.trace = component_trace - - -def _IsHelpShortcut(component_trace, remaining_args): - """Determines if the user is trying to access help without '--' separator. - - For example, mycmd.py --help instead of mycmd.py -- --help. - - Args: - component_trace: (FireTrace) The trace for the Fire command. - remaining_args: List of remaining args that haven't been consumed yet. - Returns: - True if help is requested, False otherwise. - """ - show_help = False - if remaining_args: - target = remaining_args[0] - if target in ('-h', '--help'): - # Check if --help would be consumed as a keyword argument, or is a member. - component = component_trace.GetResult() - if inspect.isclass(component) or inspect.isroutine(component): - fn_spec = inspectutils.GetFullArgSpec(component) - _, remaining_kwargs, _ = _ParseKeywordArgs(remaining_args, fn_spec) - show_help = target in remaining_kwargs - else: - members = dict(inspect.getmembers(component)) - show_help = target not in members - - if show_help: - component_trace.show_help = True - command = f'{component_trace.GetCommand()} -- --help' - print(f'INFO: Showing help with the command {shlex.quote(command)}.\n', - file=sys.stderr) - return show_help - - -def _PrintResult(component_trace, verbose=False, serialize=None): - """Prints the result of the Fire call to stdout in a human readable way.""" - # TODO(dbieber): Design human readable deserializable serialization method - # and move serialization to its own module. - result = component_trace.GetResult() - - # Allow users to modify the return value of the component and provide - # custom formatting. - if serialize: - if not callable(serialize): - raise FireError( - 'The argument `serialize` must be empty or callable:', serialize) - result = serialize(result) - - if value_types.HasCustomStr(result): - # If the object has a custom __str__ method, rather than one inherited from - # object, then we use that to serialize the object. - print(str(result)) - return - - if isinstance(result, (list, set, frozenset, types.GeneratorType)): - for i in result: - print(_OneLineResult(i)) - elif inspect.isgeneratorfunction(result): - raise NotImplementedError - elif isinstance(result, dict) and value_types.IsSimpleGroup(result): - print(_DictAsString(result, verbose)) - elif isinstance(result, tuple): - print(_OneLineResult(result)) - elif isinstance(result, value_types.VALUE_TYPES): - if result is not None: - print(result) - else: - help_text = helptext.HelpText( - result, trace=component_trace, verbose=verbose) - output = [help_text] - Display(output, out=sys.stdout) - - -def _DisplayError(component_trace): - """Prints the Fire trace and the error to stdout.""" - result = component_trace.GetResult() - - output = [] - show_help = False - for help_flag in ('-h', '--help'): - if help_flag in component_trace.elements[-1].args: - show_help = True - - if show_help: - command = f'{component_trace.GetCommand()} -- --help' - print(f'INFO: Showing help with the command {shlex.quote(command)}.\n', - file=sys.stderr) - help_text = helptext.HelpText(result, trace=component_trace, - verbose=component_trace.verbose) - output.append(help_text) - Display(output, out=sys.stderr) - else: - print(formatting.Error('ERROR: ') - + component_trace.elements[-1].ErrorAsStr(), - file=sys.stderr) - error_text = helptext.UsageText(result, trace=component_trace, - verbose=component_trace.verbose) - print(error_text, file=sys.stderr) - - -def _DictAsString(result, verbose=False): - """Returns a dict as a string. - - Args: - result: The dict to convert to a string - verbose: Whether to include 'hidden' members, those keys starting with _. - Returns: - A string representing the dict - """ - - # We need to do 2 iterations over the items in the result dict - # 1) Getting visible items and the longest key for output formatting - # 2) Actually construct the output lines - class_attrs = inspectutils.GetClassAttrsDict(result) - result_visible = { - key: value for key, value in result.items() - if completion.MemberVisible(result, key, value, - class_attrs=class_attrs, verbose=verbose) - } - - if not result_visible: - return '{}' - - longest_key = max(len(str(key)) for key in result_visible.keys()) - format_string = f'{{key:{longest_key + 1}s}} {{value}}' - - lines = [] - for key, value in result.items(): - if completion.MemberVisible(result, key, value, class_attrs=class_attrs, - verbose=verbose): - line = format_string.format(key=f'{key}:', value=_OneLineResult(value)) - lines.append(line) - return '\n'.join(lines) - - -def _OneLineResult(result): - """Returns result serialized to a single line string.""" - # TODO(dbieber): Ensure line is fewer than eg 120 characters. - if isinstance(result, str): - return str(result).replace('\n', ' ') - - # TODO(dbieber): Show a small amount of usage information about the function - # or module if it fits cleanly on the line. - if inspect.isfunction(result): - return f'' - - if inspect.ismodule(result): - return f'' - - try: - # Don't force conversion to ascii. - return json.dumps(result, ensure_ascii=False) - except (TypeError, ValueError): - return str(result).replace('\n', ' ') - - -def _Fire(component, args, parsed_flag_args, context, name=None): - """Execute a Fire command on a target component using the args supplied. - - Arguments that come after a final isolated '--' are treated as Flags, eg for - interactive mode or completion script generation. - - Other arguments are consumed by the execution of the Fire command, eg in the - traversal of the members of the component, or in calling a function or - instantiating a class found during the traversal. - - The steps performed by this method are: - - 1. Parse any Flag args (the args after the final --) - - 2. Start with component as the current component. - 2a. If the current component is a class, instantiate it using args from args. - 2b. If the component is a routine, call it using args from args. - 2c. If the component is a sequence, index into it using an arg from - args. - 2d. If possible, access a member from the component using an arg from args. - 2e. If the component is a callable object, call it using args from args. - 2f. Repeat 2a-2e until no args remain. - Note: Only the first applicable rule from 2a-2e is applied in each iteration. - After each iteration of step 2a-2e, the current component is updated to be the - result of the applied rule. - - 3a. Embed into ipython REPL if interactive mode is selected. - 3b. Generate a completion script if that flag is provided. - - In step 2, arguments will only ever be consumed up to a separator; a single - step will never consume arguments from both sides of a separator. - The separator defaults to a hyphen (-), and can be overwritten with the - --separator Fire argument. - - Args: - component: The target component for Fire. - args: A list of args to consume in Firing on the component, usually from - the command line. - parsed_flag_args: The values of the flag args (e.g. --verbose, --separator) - that are part of every Fire CLI. - context: A dict with the local and global variables available at the call - to Fire. - name: Optional. The name of the command. Used in interactive mode and in - the tab completion script. - Returns: - FireTrace of components starting with component, tracing Fire's execution - path as it consumes args. - Raises: - ValueError: If there are arguments that cannot be consumed. - ValueError: If --completion is specified but no name available. - """ - verbose = parsed_flag_args.verbose - interactive = parsed_flag_args.interactive - separator = parsed_flag_args.separator - show_completion = parsed_flag_args.completion - show_help = parsed_flag_args.help - show_trace = parsed_flag_args.trace - - # component can be a module, class, routine, object, etc. - if component is None: - component = context - - initial_component = component - component_trace = trace.FireTrace( - initial_component=initial_component, name=name, separator=separator, - verbose=verbose, show_help=show_help, show_trace=show_trace) - - instance = None - remaining_args = args - while True: - last_component = component - initial_args = remaining_args - - if not remaining_args and (show_help or interactive or show_trace - or show_completion is not None): - # Don't initialize the final class or call the final function unless - # there's a separator after it, and instead process the current component. - break - - if _IsHelpShortcut(component_trace, remaining_args): - remaining_args = [] - break - - saved_args = [] - used_separator = False - if separator in remaining_args: - # For the current component, only use arguments up to the separator. - separator_index = remaining_args.index(separator) - saved_args = remaining_args[separator_index + 1:] - remaining_args = remaining_args[:separator_index] - used_separator = True - assert separator not in remaining_args - - handled = False - candidate_errors = [] - - is_callable = inspect.isclass(component) or inspect.isroutine(component) - is_callable_object = callable(component) and not is_callable - is_sequence = isinstance(component, (list, tuple)) - is_map = isinstance(component, dict) or inspectutils.IsNamedTuple(component) - - if not handled and is_callable: - # The component is a class or a routine; we'll try to initialize it or - # call it. - is_class = inspect.isclass(component) - - try: - component, remaining_args = _CallAndUpdateTrace( - component, - remaining_args, - component_trace, - treatment='class' if is_class else 'routine', - target=component.__name__) - handled = True - except FireError as error: - candidate_errors.append((error, initial_args)) - - if handled and last_component is initial_component: - # If the initial component is a class, keep an instance for use with -i. - instance = component - - if not handled and is_sequence and remaining_args: - # The component is a tuple or list; we'll try to access a member. - arg = remaining_args[0] - try: - index = int(arg) - component = component[index] - handled = True - except (ValueError, IndexError): - error = FireError( - 'Unable to index into component with argument:', arg) - candidate_errors.append((error, initial_args)) - - if handled: - remaining_args = remaining_args[1:] - filename = None - lineno = None - component_trace.AddAccessedProperty( - component, index, [arg], filename, lineno) - - if not handled and is_map and remaining_args: - # The component is a dict or other key-value map; try to access a member. - target = remaining_args[0] - - # Treat namedtuples as dicts when handling them as a map. - if inspectutils.IsNamedTuple(component): - component_dict = component._asdict() - else: - component_dict = component - - if target in component_dict: - component = component_dict[target] - handled = True - elif target.replace('-', '_') in component_dict: - component = component_dict[target.replace('-', '_')] - handled = True - else: - # The target isn't present in the dict as a string key, but maybe it is - # a key as another type. - # TODO(dbieber): Consider alternatives for accessing non-string keys. - for key, value in ( - component_dict.items()): - if target == str(key): - component = value - handled = True - break - - if handled: - remaining_args = remaining_args[1:] - filename = None - lineno = None - component_trace.AddAccessedProperty( - component, target, [target], filename, lineno) - else: - error = FireError('Cannot find key:', target) - candidate_errors.append((error, initial_args)) - - if not handled and remaining_args: - # Object handler. We'll try to access a member of the component. - try: - target = remaining_args[0] - - component, consumed_args, remaining_args = _GetMember( - component, remaining_args) - handled = True - - filename, lineno = inspectutils.GetFileAndLine(component) - - component_trace.AddAccessedProperty( - component, target, consumed_args, filename, lineno) - - except FireError as error: - # Couldn't access member. - candidate_errors.append((error, initial_args)) - - if not handled and is_callable_object: - # The component is a callable object; we'll try to call it. - try: - component, remaining_args = _CallAndUpdateTrace( - component, - remaining_args, - component_trace, - treatment='callable') - handled = True - except FireError as error: - candidate_errors.append((error, initial_args)) - - if not handled and candidate_errors: - error, initial_args = candidate_errors[0] - component_trace.AddError(error, initial_args) - return component_trace - - if used_separator: - # Add back in the arguments from after the separator. - if remaining_args: - remaining_args = remaining_args + [separator] + saved_args - elif (inspect.isclass(last_component) - or inspect.isroutine(last_component)): - remaining_args = saved_args - component_trace.AddSeparator() - elif component is not last_component: - remaining_args = [separator] + saved_args - else: - # It was an unnecessary separator. - remaining_args = saved_args - - if component is last_component and remaining_args == initial_args: - # We're making no progress. - break - - if remaining_args: - component_trace.AddError( - FireError('Could not consume arguments:', remaining_args), - initial_args) - return component_trace - - if show_completion is not None: - if name is None: - raise ValueError('Cannot make completion script without command name') - script = CompletionScript(name, initial_component, shell=show_completion) - component_trace.AddCompletionScript(script) - - if interactive: - variables = context.copy() - - if name is not None: - variables[name] = initial_component - variables['component'] = initial_component - variables['result'] = component - variables['trace'] = component_trace - - if instance is not None: - variables['self'] = instance - - interact.Embed(variables, verbose) - - component_trace.AddInteractiveMode() - - return component_trace - - -def _GetMember(component, args): - """Returns a subcomponent of component by consuming an arg from args. - - Given a starting component and args, this function gets a member from that - component, consuming one arg in the process. - - Args: - component: The component from which to get a member. - args: Args from which to consume in the search for the next component. - Returns: - component: The component that was found by consuming an arg. - consumed_args: The args that were consumed by getting this member. - remaining_args: The remaining args that haven't been consumed yet. - Raises: - FireError: If we cannot consume an argument to get a member. - """ - members = dir(component) - arg = args[0] - arg_names = [ - arg, - arg.replace('-', '_'), # treat '-' as '_'. - ] - - for arg_name in arg_names: - if arg_name in members: - return getattr(component, arg_name), [arg], args[1:] - - raise FireError('Could not consume arg:', arg) - - -def _CallAndUpdateTrace(component, args, component_trace, treatment='class', - target=None): - """Call the component by consuming args from args, and update the FireTrace. - - The component could be a class, a routine, or a callable object. This function - calls the component and adds the appropriate action to component_trace. - - Args: - component: The component to call - args: Args for calling the component - component_trace: FireTrace object that contains action trace - treatment: Type of treatment used. Indicating whether we treat the component - as a class, a routine, or a callable. - target: Target in FireTrace element, default is None. If the value is None, - the component itself will be used as target. - Returns: - component: The object that is the result of the callable call. - remaining_args: The remaining args that haven't been consumed yet. - """ - if not target: - target = component - filename, lineno = inspectutils.GetFileAndLine(component) - metadata = decorators.GetMetadata(component) - fn = component.__call__ if treatment == 'callable' else component - parse = _MakeParseFn(fn, metadata) - (varargs, kwargs), consumed_args, remaining_args, capacity = parse(args) - - # Call the function. - if inspectutils.IsCoroutineFunction(fn): - try: - loop = asyncio.get_running_loop() - except RuntimeError: - # No event loop running, create a new one - component = asyncio.run(fn(*varargs, **kwargs)) - else: - # Event loop is already running - component = loop.run_until_complete(fn(*varargs, **kwargs)) - else: - component = fn(*varargs, **kwargs) - - if treatment == 'class': - action = trace.INSTANTIATED_CLASS - elif treatment == 'routine': - action = trace.CALLED_ROUTINE - else: - action = trace.CALLED_CALLABLE - component_trace.AddCalledComponent( - component, target, consumed_args, filename, lineno, capacity, - action=action) - - return component, remaining_args - - -def _MakeParseFn(fn, metadata): - """Creates a parse function for fn. - - Args: - fn: The function or class to create the parse function for. - metadata: Additional metadata about the component the parse function is for. - Returns: - A parse function for fn. The parse function accepts a list of arguments - and returns (varargs, kwargs), remaining_args. The original function fn - can then be called with fn(*varargs, **kwargs). The remaining_args are - the leftover args from the arguments to the parse function. - """ - fn_spec = inspectutils.GetFullArgSpec(fn) - - # Note: num_required_args is the number of positional arguments without - # default values. All of these arguments are required. - num_required_args = len(fn_spec.args) - len(fn_spec.defaults) - required_kwonly = set(fn_spec.kwonlyargs) - set(fn_spec.kwonlydefaults) - - def _ParseFn(args): - """Parses the list of `args` into (varargs, kwargs), remaining_args.""" - kwargs, remaining_kwargs, remaining_args = _ParseKeywordArgs(args, fn_spec) - - # Note: _ParseArgs modifies kwargs. - parsed_args, kwargs, remaining_args, capacity = _ParseArgs( - fn_spec.args, fn_spec.defaults, num_required_args, kwargs, - remaining_args, metadata) - - if fn_spec.varargs or fn_spec.varkw: - # If we're allowed *varargs or **kwargs, there's always capacity. - capacity = True - - extra_kw = set(kwargs) - set(fn_spec.kwonlyargs) - if fn_spec.varkw is None and extra_kw: - raise FireError('Unexpected kwargs present:', extra_kw) - - missing_kwonly = set(required_kwonly) - set(kwargs) - if missing_kwonly: - raise FireError('Missing required flags:', missing_kwonly) - - # If we accept *varargs, then use all remaining arguments for *varargs. - if fn_spec.varargs is not None: - varargs, remaining_args = remaining_args, [] - else: - varargs = [] - - for index, value in enumerate(varargs): - varargs[index] = _ParseValue(value, None, None, metadata) - - varargs = parsed_args + varargs - remaining_args += remaining_kwargs - - consumed_args = args[:len(args) - len(remaining_args)] - return (varargs, kwargs), consumed_args, remaining_args, capacity - - return _ParseFn - - -def _ParseArgs(fn_args, fn_defaults, num_required_args, kwargs, - remaining_args, metadata): - """Parses the positional and named arguments from the available supplied args. - - Modifies kwargs, removing args as they are used. - - Args: - fn_args: A list of argument names that the target function accepts, - including positional and named arguments, but not the varargs or kwargs - names. - fn_defaults: A list of the default values in the function argspec. - num_required_args: The number of required arguments from the function's - argspec. This is the number of arguments without a default value. - kwargs: Dict with named command line arguments and their values. - remaining_args: The remaining command line arguments, which may still be - used as positional arguments. - metadata: Metadata about the function, typically from Fire decorators. - Returns: - parsed_args: A list of values to be used as positional arguments for calling - the target function. - kwargs: The input dict kwargs modified with the used kwargs removed. - remaining_args: A list of the supplied args that have not been used yet. - capacity: Whether the call could have taken args in place of defaults. - Raises: - FireError: If additional positional arguments are expected, but none are - available. - """ - accepts_positional_args = metadata.get(decorators.ACCEPTS_POSITIONAL_ARGS) - capacity = False # If we see a default get used, we'll set capacity to True - - # Select unnamed args. - parsed_args = [] - for index, arg in enumerate(fn_args): - value = kwargs.pop(arg, None) - if value is not None: # A value is specified at the command line. - value = _ParseValue(value, index, arg, metadata) - parsed_args.append(value) - else: # No value has been explicitly specified. - if remaining_args and accepts_positional_args: - # Use a positional arg. - value = remaining_args.pop(0) - value = _ParseValue(value, index, arg, metadata) - parsed_args.append(value) - elif index < num_required_args: - raise FireError( - 'The function received no value for the required argument:', arg) - else: - # We're past the args for which there's no default value. - # There's a default value for this arg. - capacity = True - default_index = index - num_required_args # index into the defaults. - parsed_args.append(fn_defaults[default_index]) - - for key, value in kwargs.items(): - kwargs[key] = _ParseValue(value, None, key, metadata) - - return parsed_args, kwargs, remaining_args, capacity - - -def _ParseKeywordArgs(args, fn_spec): - """Parses the supplied arguments for keyword arguments. - - Given a list of arguments, finds occurrences of --name value, and uses 'name' - as the keyword and 'value' as the value. Constructs and returns a dictionary - of these keyword arguments, and returns a list of the remaining arguments. - - Only if fn_keywords is None, this only finds argument names used by the - function, specified through fn_args. - - This returns the values of the args as strings. They are later processed by - _ParseArgs, which converts them to the appropriate type. - - Args: - args: A list of arguments. - fn_spec: The inspectutils.FullArgSpec describing the given callable. - Returns: - kwargs: A dictionary mapping keywords to values. - remaining_kwargs: A list of the unused kwargs from the original args. - remaining_args: A list of the unused arguments from the original args. - Raises: - FireError: If a single-character flag is passed that could refer to multiple - possible args. - """ - kwargs = {} - remaining_kwargs = [] - remaining_args = [] - fn_keywords = fn_spec.varkw - fn_args = fn_spec.args + fn_spec.kwonlyargs - - if not args: - return kwargs, remaining_kwargs, remaining_args - - skip_argument = False - - for index, argument in enumerate(args): - if skip_argument: - skip_argument = False - continue - - if _IsFlag(argument): - # This is a named argument. We get its value from this arg or the next. - - # Terminology: - # argument: A full token from the command line, e.g. '--alpha=10' - # stripped_argument: An argument without leading hyphens. - # key: The contents of the stripped argument up to the first equal sign. - # "shortcut flag": refers to an argument where the key is just the first - # letter of a longer keyword. - # keyword: The Python function argument being set by this argument. - # value: The unparsed value for that Python function argument. - contains_equals = '=' in argument - stripped_argument = argument.lstrip('-') - if contains_equals: - key, value = stripped_argument.split('=', 1) - else: - key = stripped_argument - value = None # value will be set later on. - - key = key.replace('-', '_') - is_bool_syntax = (not contains_equals and - (index + 1 == len(args) or _IsFlag(args[index + 1]))) - - # Determine the keyword. - keyword = '' # Indicates no valid keyword has been found yet. - if (key in fn_args - or (is_bool_syntax and key.startswith('no') and key[2:] in fn_args) - or fn_keywords): - keyword = key - elif len(key) == 1: - # This may be a shortcut flag. - matching_fn_args = [arg for arg in fn_args if arg[0] == key] - if len(matching_fn_args) == 1: - keyword = matching_fn_args[0] - elif len(matching_fn_args) > 1: - raise FireError( - f"The argument '{argument}' is ambiguous as it could " - f"refer to any of the following arguments: {matching_fn_args}" - ) - - # Determine the value. - if not keyword: - got_argument = False - elif contains_equals: - # Already got the value above. - got_argument = True - elif is_bool_syntax: - # There's no next arg or the next arg is a Flag, so we consider this - # flag to be a boolean. - got_argument = True - if keyword in fn_args: - value = 'True' - elif keyword.startswith('no'): - keyword = keyword[2:] - value = 'False' - else: - value = 'True' - else: - # The assert should pass. Otherwise either contains_equals or - # is_bool_syntax would have been True. - assert index + 1 < len(args) - value = args[index + 1] - got_argument = True - - # In order for us to consume the argument as a keyword arg, we either: - # Need to be explicitly expecting the keyword, or we need to be - # accepting **kwargs. - skip_argument = not contains_equals and not is_bool_syntax - if got_argument: - kwargs[keyword] = value - else: - remaining_kwargs.append(argument) - if skip_argument: - remaining_kwargs.append(args[index + 1]) - else: # not _IsFlag(argument) - remaining_args.append(argument) - - return kwargs, remaining_kwargs, remaining_args - - -def _IsFlag(argument): - """Determines if the argument is a flag argument. - - If it starts with a hyphen and isn't a negative number, it's a flag. - - Args: - argument: A command line argument that may or may not be a flag. - Returns: - A boolean indicating whether the argument is a flag. - """ - return _IsSingleCharFlag(argument) or _IsMultiCharFlag(argument) - - -def _IsSingleCharFlag(argument): - """Determines if the argument is a single char flag (e.g. '-a').""" - return re.match('^-[a-zA-Z]$', argument) or re.match('^-[a-zA-Z]=', argument) - - -def _IsMultiCharFlag(argument): - """Determines if the argument is a multi char flag (e.g. '--alpha').""" - return argument.startswith('--') or re.match('^-[a-zA-Z]', argument) - - -def _ParseValue(value, index, arg, metadata): - """Parses value, a string, into the appropriate type. - - The function used to parse value is determined by the remaining arguments. - - Args: - value: The string value to be parsed, typically a command line argument. - index: The index of the value in the function's argspec. - arg: The name of the argument the value is being parsed for. - metadata: Metadata about the function, typically from Fire decorators. - Returns: - value, parsed into the appropriate type for calling a function. - """ - parse_fn = parser.DefaultParseValue - - # We check to see if any parse function from the fn metadata applies here. - parse_fns = metadata.get(decorators.FIRE_PARSE_FNS) - if parse_fns: - default = parse_fns['default'] - positional = parse_fns['positional'] - named = parse_fns['named'] - - if index is not None and 0 <= index < len(positional): - parse_fn = positional[index] - elif arg in named: - parse_fn = named[arg] - elif default is not None: - parse_fn = default - - return parse_fn(value) diff --git a/fire/core_test.py b/fire/core_test.py deleted file mode 100644 index f48d6e2d..00000000 --- a/fire/core_test.py +++ /dev/null @@ -1,228 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for the core module.""" - -from unittest import mock - -from fire import core -from fire import test_components as tc -from fire import testutils -from fire import trace - - -class CoreTest(testutils.BaseTestCase): - - def testOneLineResult(self): - self.assertEqual(core._OneLineResult(1), '1') # pylint: disable=protected-access - self.assertEqual(core._OneLineResult('hello'), 'hello') # pylint: disable=protected-access - self.assertEqual(core._OneLineResult({}), '{}') # pylint: disable=protected-access - self.assertEqual(core._OneLineResult({'x': 'y'}), '{"x": "y"}') # pylint: disable=protected-access - - def testOneLineResultCircularRef(self): - circular_reference = tc.CircularReference() - self.assertEqual(core._OneLineResult(circular_reference.create()), # pylint: disable=protected-access - "{'y': {...}}") - - @mock.patch('fire.interact.Embed') - def testInteractiveMode(self, mock_embed): - core.Fire(tc.TypedProperties, command=['alpha']) - self.assertFalse(mock_embed.called) - core.Fire(tc.TypedProperties, command=['alpha', '--', '-i']) - self.assertTrue(mock_embed.called) - - @mock.patch('fire.interact.Embed') - def testInteractiveModeFullArgument(self, mock_embed): - core.Fire(tc.TypedProperties, command=['alpha', '--', '--interactive']) - self.assertTrue(mock_embed.called) - - @mock.patch('fire.interact.Embed') - def testInteractiveModeVariables(self, mock_embed): - core.Fire(tc.WithDefaults, command=['double', '2', '--', '-i']) - self.assertTrue(mock_embed.called) - (variables, verbose), unused_kwargs = mock_embed.call_args - self.assertFalse(verbose) - self.assertEqual(variables['result'], 4) - self.assertIsInstance(variables['self'], tc.WithDefaults) - self.assertIsInstance(variables['trace'], trace.FireTrace) - - @mock.patch('fire.interact.Embed') - def testInteractiveModeVariablesWithName(self, mock_embed): - core.Fire(tc.WithDefaults, - command=['double', '2', '--', '-i', '-v'], name='D') - self.assertTrue(mock_embed.called) - (variables, verbose), unused_kwargs = mock_embed.call_args - self.assertTrue(verbose) - self.assertEqual(variables['result'], 4) - self.assertIsInstance(variables['self'], tc.WithDefaults) - self.assertEqual(variables['D'], tc.WithDefaults) - self.assertIsInstance(variables['trace'], trace.FireTrace) - - # TODO(dbieber): Use parameterized tests to break up repetitive tests. - def testHelpWithClass(self): - with self.assertRaisesFireExit(0, 'SYNOPSIS.*ARG1'): - core.Fire(tc.InstanceVars, command=['--', '--help']) - with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*ARG1'): - core.Fire(tc.InstanceVars, command=['--help']) - with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*ARG1'): - core.Fire(tc.InstanceVars, command=['-h']) - - def testHelpWithMember(self): - with self.assertRaisesFireExit(0, 'SYNOPSIS.*capitalize'): - core.Fire(tc.TypedProperties, command=['gamma', '--', '--help']) - with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*capitalize'): - core.Fire(tc.TypedProperties, command=['gamma', '--help']) - with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*capitalize'): - core.Fire(tc.TypedProperties, command=['gamma', '-h']) - with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*delta'): - core.Fire(tc.TypedProperties, command=['delta', '--help']) - with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*echo'): - core.Fire(tc.TypedProperties, command=['echo', '--help']) - - def testHelpOnErrorInConstructor(self): - with self.assertRaisesFireExit(0, 'SYNOPSIS.*VALUE'): - core.Fire(tc.ErrorInConstructor, command=['--', '--help']) - with self.assertRaisesFireExit(0, 'INFO:.*SYNOPSIS.*VALUE'): - core.Fire(tc.ErrorInConstructor, command=['--help']) - - def testHelpWithNamespaceCollision(self): - # Tests cases when calling the help shortcut should not show help. - with self.assertOutputMatches(stdout='DESCRIPTION.*', stderr=None): - core.Fire(tc.WithHelpArg, command=['--help', 'False']) - with self.assertOutputMatches(stdout='help in a dict', stderr=None): - core.Fire(tc.WithHelpArg, command=['dictionary', '__help']) - with self.assertOutputMatches(stdout='{}', stderr=None): - core.Fire(tc.WithHelpArg, command=['dictionary', '--help']) - with self.assertOutputMatches(stdout='False', stderr=None): - core.Fire(tc.function_with_help, command=['False']) - - def testInvalidParameterRaisesFireExit(self): - with self.assertRaisesFireExit(2, 'runmisspelled'): - core.Fire(tc.Kwargs, command=['props', '--a=1', '--b=2', 'runmisspelled']) - - def testErrorRaising(self): - # Errors in user code should not be caught; they should surface as normal. - # This will lead to exit status code 1 for the client program. - with self.assertRaises(ValueError): - core.Fire(tc.ErrorRaiser, command=['fail']) - - def testFireError(self): - error = core.FireError('Example error') - self.assertIsNotNone(error) - - def testFireErrorMultipleValues(self): - error = core.FireError('Example error', 'value') - self.assertIsNotNone(error) - - def testPrintEmptyDict(self): - with self.assertOutputMatches(stdout='{}', stderr=None): - core.Fire(tc.EmptyDictOutput, command=['totally_empty']) - with self.assertOutputMatches(stdout='{}', stderr=None): - core.Fire(tc.EmptyDictOutput, command=['nothing_printable']) - - def testPrintOrderedDict(self): - with self.assertOutputMatches(stdout=r'A:\s+A\s+2:\s+2\s+', stderr=None): - core.Fire(tc.OrderedDictionary, command=['non_empty']) - with self.assertOutputMatches(stdout='{}'): - core.Fire(tc.OrderedDictionary, command=['empty']) - - def testPrintNamedTupleField(self): - with self.assertOutputMatches(stdout='11', stderr=None): - core.Fire(tc.NamedTuple, command=['point', 'x']) - - def testPrintNamedTupleFieldNameEqualsValue(self): - with self.assertOutputMatches(stdout='x', stderr=None): - core.Fire(tc.NamedTuple, command=['matching_names', 'x']) - - def testPrintNamedTupleIndex(self): - with self.assertOutputMatches(stdout='22', stderr=None): - core.Fire(tc.NamedTuple, command=['point', '1']) - - def testPrintSet(self): - with self.assertOutputMatches(stdout='.*three.*', stderr=None): - core.Fire(tc.simple_set(), command=[]) - - def testPrintFrozenSet(self): - with self.assertOutputMatches(stdout='.*three.*', stderr=None): - core.Fire(tc.simple_frozenset(), command=[]) - - def testPrintNamedTupleNegativeIndex(self): - with self.assertOutputMatches(stdout='11', stderr=None): - core.Fire(tc.NamedTuple, command=['point', '-2']) - - def testCallable(self): - with self.assertOutputMatches(stdout=r'foo:\s+foo\s+', stderr=None): - core.Fire(tc.CallableWithKeywordArgument(), command=['--foo=foo']) - with self.assertOutputMatches(stdout=r'foo\s+', stderr=None): - core.Fire(tc.CallableWithKeywordArgument(), command=['print_msg', 'foo']) - with self.assertOutputMatches(stdout=r'', stderr=None): - core.Fire(tc.CallableWithKeywordArgument(), command=[]) - - def testCallableWithPositionalArgs(self): - with self.assertRaisesFireExit(2, ''): - # This does not give 7 since positional args are disallowed for callable - # objects. - core.Fire(tc.CallableWithPositionalArgs(), command=['3', '4']) - - def testStaticMethod(self): - self.assertEqual( - core.Fire(tc.HasStaticAndClassMethods, - command=['static_fn', 'alpha']), - 'alpha', - ) - - def testClassMethod(self): - self.assertEqual( - core.Fire(tc.HasStaticAndClassMethods, - command=['class_fn', '6']), - 7, - ) - - def testCustomSerialize(self): - def serialize(x): - if isinstance(x, list): - return ', '.join(str(xi) for xi in x) - if isinstance(x, dict): - return ', '.join('{}={!r}'.format(k, v) for k, v in sorted(x.items())) - if x == 'special': - return ['SURPRISE!!', "I'm a list!"] - return x - - ident = lambda x: x - - with self.assertOutputMatches(stdout='a, b', stderr=None): - _ = core.Fire(ident, command=['[a,b]'], serialize=serialize) - with self.assertOutputMatches(stdout='a=5, b=6', stderr=None): - _ = core.Fire(ident, command=['{a:5,b:6}'], serialize=serialize) - with self.assertOutputMatches(stdout='asdf', stderr=None): - _ = core.Fire(ident, command=['asdf'], serialize=serialize) - with self.assertOutputMatches( - stdout="SURPRISE!!\nI'm a list!\n", stderr=None): - _ = core.Fire(ident, command=['special'], serialize=serialize) - with self.assertRaises(core.FireError): - core.Fire(ident, command=['asdf'], serialize=55) - - def testLruCacheDecoratorBoundArg(self): - self.assertEqual( - core.Fire(tc.py3.LruCacheDecoratedMethod, - command=['lru_cache_in_class', 'foo']), 'foo') - - def testLruCacheDecorator(self): - self.assertEqual( - core.Fire(tc.py3.lru_cache_decorated, - command=['foo']), 'foo') - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/custom_descriptions.py b/fire/custom_descriptions.py deleted file mode 100644 index ef1130a3..00000000 --- a/fire/custom_descriptions.py +++ /dev/null @@ -1,144 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Custom descriptions and summaries for the builtin types. - -The docstrings for objects of primitive types reflect the type of the object, -rather than the object itself. For example, the docstring for any dict is this: - -> print({'key': 'value'}.__doc__) -dict() -> new empty dictionary -dict(mapping) -> new dictionary initialized from a mapping object's - (key, value) pairs -dict(iterable) -> new dictionary initialized as if via: - d = {} - for k, v in iterable: - d[k] = v -dict(**kwargs) -> new dictionary initialized with the name=value pairs - in the keyword argument list. For example: dict(one=1, two=2) - -As you can see, this docstring is more pertinent to the function `dict` and -would be suitable as the result of `dict.__doc__`, but is wholely unsuitable -as a description for the dict `{'key': 'value'}`. - -This modules aims to resolve that problem, providing custom summaries and -descriptions for primitive typed values. -""" - -from fire import formatting - -TWO_DOUBLE_QUOTES = '""' -STRING_DESC_PREFIX = 'The string ' - - -def NeedsCustomDescription(component): - """Whether the component should use a custom description and summary. - - Components of primitive type, such as ints, floats, dicts, lists, and others - have messy builtin docstrings. These are inappropriate for display as - descriptions and summaries in a CLI. This function determines whether the - provided component has one of these docstrings. - - Note that an object such as `int` has the same docstring as an int like `3`. - The docstring is OK for `int`, but is inappropriate as a docstring for `3`. - - Args: - component: The component of interest. - Returns: - Whether the component should use a custom description and summary. - """ - type_ = type(component) - if ( - type_ in (str, int, bytes) - or type_ in (float, complex, bool) - or type_ in (dict, tuple, list, set, frozenset) - ): - return True - return False - - -def GetStringTypeSummary(obj, available_space, line_length): - """Returns a custom summary for string type objects. - - This function constructs a summary for string type objects by double quoting - the string value. The double quoted string value will be potentially truncated - with ellipsis depending on whether it has enough space available to show the - full string value. - - Args: - obj: The object to generate summary for. - available_space: Number of character spaces available. - line_length: The full width of the terminal, default is 80. - - Returns: - A summary for the input object. - """ - if len(obj) + len(TWO_DOUBLE_QUOTES) <= available_space: - content = obj - else: - additional_len_needed = len(TWO_DOUBLE_QUOTES) + len(formatting.ELLIPSIS) - if available_space < additional_len_needed: - available_space = line_length - content = formatting.EllipsisTruncate( - obj, available_space - len(TWO_DOUBLE_QUOTES), line_length) - return formatting.DoubleQuote(content) - - -def GetStringTypeDescription(obj, available_space, line_length): - """Returns the predefined description for string obj. - - This function constructs a description for string type objects in the format - of 'The string ""'. could be potentially - truncated depending on whether it has enough space available to show the full - string value. - - Args: - obj: The object to generate description for. - available_space: Number of character spaces available. - line_length: The full width of the terminal, default if 80. - - Returns: - A description for input object. - """ - additional_len_needed = len(STRING_DESC_PREFIX) + len( - TWO_DOUBLE_QUOTES) + len(formatting.ELLIPSIS) - if available_space < additional_len_needed: - available_space = line_length - - return STRING_DESC_PREFIX + formatting.DoubleQuote( - formatting.EllipsisTruncate( - obj, available_space - len(STRING_DESC_PREFIX) - - len(TWO_DOUBLE_QUOTES), line_length)) - - -CUSTOM_DESC_SUM_FN_DICT = { - 'str': (GetStringTypeSummary, GetStringTypeDescription), - 'unicode': (GetStringTypeSummary, GetStringTypeDescription), -} - - -def GetSummary(obj, available_space, line_length): - obj_type_name = type(obj).__name__ - if obj_type_name in CUSTOM_DESC_SUM_FN_DICT: - return CUSTOM_DESC_SUM_FN_DICT[obj_type_name][0](obj, available_space, - line_length) - return None - - -def GetDescription(obj, available_space, line_length): - obj_type_name = type(obj).__name__ - if obj_type_name in CUSTOM_DESC_SUM_FN_DICT: - return CUSTOM_DESC_SUM_FN_DICT[obj_type_name][1](obj, available_space, - line_length) - return None diff --git a/fire/custom_descriptions_test.py b/fire/custom_descriptions_test.py deleted file mode 100644 index 6cff2d5d..00000000 --- a/fire/custom_descriptions_test.py +++ /dev/null @@ -1,69 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for custom description module.""" - -from fire import custom_descriptions -from fire import testutils - -LINE_LENGTH = 80 - - -class CustomDescriptionTest(testutils.BaseTestCase): - - def test_string_type_summary_enough_space(self): - component = 'Test' - summary = custom_descriptions.GetSummary( - obj=component, available_space=80, line_length=LINE_LENGTH) - self.assertEqual(summary, '"Test"') - - def test_string_type_summary_not_enough_space_truncated(self): - component = 'Test' - summary = custom_descriptions.GetSummary( - obj=component, available_space=5, line_length=LINE_LENGTH) - self.assertEqual(summary, '"..."') - - def test_string_type_summary_not_enough_space_new_line(self): - component = 'Test' - summary = custom_descriptions.GetSummary( - obj=component, available_space=4, line_length=LINE_LENGTH) - self.assertEqual(summary, '"Test"') - - def test_string_type_summary_not_enough_space_long_truncated(self): - component = 'Lorem ipsum dolor sit amet' - summary = custom_descriptions.GetSummary( - obj=component, available_space=10, line_length=LINE_LENGTH) - self.assertEqual(summary, '"Lorem..."') - - def test_string_type_description_enough_space(self): - component = 'Test' - description = custom_descriptions.GetDescription( - obj=component, available_space=80, line_length=LINE_LENGTH) - self.assertEqual(description, 'The string "Test"') - - def test_string_type_description_not_enough_space_truncated(self): - component = 'Lorem ipsum dolor sit amet' - description = custom_descriptions.GetDescription( - obj=component, available_space=20, line_length=LINE_LENGTH) - self.assertEqual(description, 'The string "Lore..."') - - def test_string_type_description_not_enough_space_new_line(self): - component = 'Lorem ipsum dolor sit amet' - description = custom_descriptions.GetDescription( - obj=component, available_space=10, line_length=LINE_LENGTH) - self.assertEqual(description, 'The string "Lorem ipsum dolor sit amet"') - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/decorators.py b/fire/decorators.py deleted file mode 100644 index 547153c6..00000000 --- a/fire/decorators.py +++ /dev/null @@ -1,110 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""These decorators provide function metadata to Python Fire. - -SetParseFn and SetParseFns allow you to set the functions Fire uses for parsing -command line arguments to client code. -""" - -from typing import Any, Dict -import inspect - -FIRE_METADATA = 'FIRE_METADATA' -FIRE_PARSE_FNS = 'FIRE_PARSE_FNS' -ACCEPTS_POSITIONAL_ARGS = 'ACCEPTS_POSITIONAL_ARGS' - - -def SetParseFn(fn, *arguments): - """Sets the fn for Fire to use to parse args when calling the decorated fn. - - Args: - fn: The function to be used for parsing arguments. - *arguments: The arguments for which to use the parse fn. If none are listed, - then this will set the default parse function. - Returns: - The decorated function, which now has metadata telling Fire how to perform. - """ - def _Decorator(func): - parse_fns = GetParseFns(func) - if not arguments: - parse_fns['default'] = fn - else: - for argument in arguments: - parse_fns['named'][argument] = fn - _SetMetadata(func, FIRE_PARSE_FNS, parse_fns) - return func - - return _Decorator - - -def SetParseFns(*positional, **named): - """Set the fns for Fire to use to parse args when calling the decorated fn. - - Returns a decorator, which when applied to a function adds metadata to the - function telling Fire how to turn string command line arguments into proper - Python arguments with which to call the function. - - A parse function should accept a single string argument and return a value to - be used in its place when calling the decorated function. - - Args: - *positional: The functions to be used for parsing positional arguments. - **named: The functions to be used for parsing named arguments. - Returns: - The decorated function, which now has metadata telling Fire how to perform. - """ - def _Decorator(fn): - parse_fns = GetParseFns(fn) - parse_fns['positional'] = positional - parse_fns['named'].update(named) - _SetMetadata(fn, FIRE_PARSE_FNS, parse_fns) - return fn - - return _Decorator - - -def _SetMetadata(fn, attribute, value): - metadata = GetMetadata(fn) - metadata[attribute] = value - setattr(fn, FIRE_METADATA, metadata) - - -def GetMetadata(fn) -> Dict[str, Any]: - """Gets metadata attached to the function `fn` as an attribute. - - Args: - fn: The function from which to retrieve the function metadata. - Returns: - A dictionary mapping property strings to their value. - """ - # Class __init__ functions and object __call__ functions require flag style - # arguments. Other methods and functions may accept positional args. - default = { - ACCEPTS_POSITIONAL_ARGS: inspect.isroutine(fn), - } - try: - metadata = getattr(fn, FIRE_METADATA, default) - if ACCEPTS_POSITIONAL_ARGS in metadata: - return metadata - else: - return default - except: # pylint: disable=bare-except - return default - - -def GetParseFns(fn) -> Dict[str, Any]: - metadata = GetMetadata(fn) - default = {'default': None, 'positional': [], 'named': {}} - return metadata.get(FIRE_PARSE_FNS, default) diff --git a/fire/decorators_test.py b/fire/decorators_test.py deleted file mode 100644 index 9988743c..00000000 --- a/fire/decorators_test.py +++ /dev/null @@ -1,170 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for the decorators module.""" - -from fire import core -from fire import decorators -from fire import testutils - - -class NoDefaults: - """A class for testing decorated functions without default values.""" - - @decorators.SetParseFns(count=int) - def double(self, count): - return 2 * count - - @decorators.SetParseFns(count=float) - def triple(self, count): - return 3 * count - - @decorators.SetParseFns(int) - def quadruple(self, count): - return 4 * count - - -@decorators.SetParseFns(int) -def double(count): - return 2 * count - - -class WithDefaults: - - @decorators.SetParseFns(float) - def example1(self, arg1=10): - return arg1, type(arg1) - - @decorators.SetParseFns(arg1=float) - def example2(self, arg1=10): - return arg1, type(arg1) - - -class MixedArguments: - - @decorators.SetParseFns(float, arg2=str) - def example3(self, arg1, arg2): - return arg1, arg2 - - -class PartialParseFn: - - @decorators.SetParseFns(arg1=str) - def example4(self, arg1, arg2): - return arg1, arg2 - - @decorators.SetParseFns(arg2=str) - def example5(self, arg1, arg2): - return arg1, arg2 - - -class WithKwargs: - - @decorators.SetParseFns(mode=str, count=int) - def example6(self, **kwargs): - return ( - kwargs.get('mode', 'default'), - kwargs.get('count', 0), - ) - - -class WithVarArgs: - - @decorators.SetParseFn(str) - def example7(self, arg1, arg2=None, *varargs, **kwargs): # pylint: disable=keyword-arg-before-vararg - return arg1, arg2, varargs, kwargs - - -class FireDecoratorsTest(testutils.BaseTestCase): - - def testSetParseFnsNamedArgs(self): - self.assertEqual(core.Fire(NoDefaults, command=['double', '2']), 4) - self.assertEqual(core.Fire(NoDefaults, command=['triple', '4']), 12.0) - - def testSetParseFnsPositionalArgs(self): - self.assertEqual(core.Fire(NoDefaults, command=['quadruple', '5']), 20) - - def testSetParseFnsFnWithPositionalArgs(self): - self.assertEqual(core.Fire(double, command=['5']), 10) - - def testSetParseFnsDefaultsFromPython(self): - # When called from Python, function should behave normally. - self.assertTupleEqual(WithDefaults().example1(), (10, int)) - self.assertEqual(WithDefaults().example1(5), (5, int)) - self.assertEqual(WithDefaults().example1(12.0), (12, float)) - - def testSetParseFnsDefaultsFromFire(self): - # Fire should use the decorator to know how to parse string arguments. - self.assertEqual(core.Fire(WithDefaults, command=['example1']), (10, int)) - self.assertEqual(core.Fire(WithDefaults, command=['example1', '10']), - (10, float)) - self.assertEqual(core.Fire(WithDefaults, command=['example1', '13']), - (13, float)) - self.assertEqual(core.Fire(WithDefaults, command=['example1', '14.0']), - (14, float)) - - def testSetParseFnsNamedDefaultsFromPython(self): - # When called from Python, function should behave normally. - self.assertTupleEqual(WithDefaults().example2(), (10, int)) - self.assertEqual(WithDefaults().example2(5), (5, int)) - self.assertEqual(WithDefaults().example2(12.0), (12, float)) - - def testSetParseFnsNamedDefaultsFromFire(self): - # Fire should use the decorator to know how to parse string arguments. - self.assertEqual(core.Fire(WithDefaults, command=['example2']), (10, int)) - self.assertEqual(core.Fire(WithDefaults, command=['example2', '10']), - (10, float)) - self.assertEqual(core.Fire(WithDefaults, command=['example2', '13']), - (13, float)) - self.assertEqual(core.Fire(WithDefaults, command=['example2', '14.0']), - (14, float)) - - def testSetParseFnsPositionalAndNamed(self): - self.assertEqual(core.Fire(MixedArguments, ['example3', '10', '10']), - (10, '10')) - - def testSetParseFnsOnlySomeTypes(self): - self.assertEqual( - core.Fire(PartialParseFn, command=['example4', '10', '10']), ('10', 10)) - self.assertEqual( - core.Fire(PartialParseFn, command=['example5', '10', '10']), (10, '10')) - - def testSetParseFnsForKeywordArgs(self): - self.assertEqual( - core.Fire(WithKwargs, command=['example6']), ('default', 0)) - self.assertEqual( - core.Fire(WithKwargs, command=['example6', '--herring', '"red"']), - ('default', 0)) - self.assertEqual( - core.Fire(WithKwargs, command=['example6', '--mode', 'train']), - ('train', 0)) - self.assertEqual(core.Fire(WithKwargs, command=['example6', '--mode', '3']), - ('3', 0)) - self.assertEqual( - core.Fire(WithKwargs, - command=['example6', '--mode', '-1', '--count', '10']), - ('-1', 10)) - self.assertEqual( - core.Fire(WithKwargs, command=['example6', '--count', '-2']), - ('default', -2)) - - def testSetParseFn(self): - self.assertEqual( - core.Fire(WithVarArgs, - command=['example7', '1', '--arg2=2', '3', '4', '--kwarg=5']), - ('1', '2', ('3', '4'), {'kwarg': '5'})) - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/docstrings.py b/fire/docstrings.py deleted file mode 100644 index 2adfe5ec..00000000 --- a/fire/docstrings.py +++ /dev/null @@ -1,774 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Docstring parsing module for Python Fire. - -The following features of docstrings are not supported. -TODO(dbieber): Support these features. -- numpy docstrings may begin with the function signature. -- whitespace may be important for proper structuring of a docstring -- I've seen `argname` (with single backticks) as a style of documenting - arguments. The `argname` appears on one line, and the description on the next. -- .. Sphinx directives such as .. note:: are not understood. -- After a section ends, future contents may be included in the section. E.g. - :returns: This is what is returned. - Example: An example goes here. -- @param is sometimes used. E.g. - @param argname (type) Description - @return (type) Description -- The true signature of a function is not used by the docstring parser. It could - be useful for determining whether something is a section header or an argument - for example. -- This example confuses types as part of the docstrings. - Parameters - argname : argtype - Arg description -- If there's no blank line after the summary, the description will be slurped - up into the summary. -- "Examples" should be its own section type. aka "Usage". -- "Notes" should be a section type. -- Some people put parenthesis around their types in RST format, e.g. - :param (type) paramname: -- :rtype: directive (return type) -- Also ":rtype str" with no closing ":" has come up. -- Return types are not supported. -- "# Returns" as a section title style -- ":raises ExceptionType: Description" ignores the ExceptionType currently. -- "Defaults to X" occurs sometimes. -- "True | False" indicates bool type. -""" - -import collections -import enum -import re -import textwrap - - -class DocstringInfo( - collections.namedtuple( - 'DocstringInfo', - ('summary', 'description', 'args', 'returns', 'yields', 'raises'))): - pass -DocstringInfo.__new__.__defaults__ = (None,) * len(DocstringInfo._fields) - - -class ArgInfo( - collections.namedtuple( - 'ArgInfo', - ('name', 'type', 'description'))): - pass -ArgInfo.__new__.__defaults__ = (None,) * len(ArgInfo._fields) - - -class KwargInfo(ArgInfo): - pass -KwargInfo.__new__.__defaults__ = (None,) * len(KwargInfo._fields) - - -class Namespace(dict): - """A dict with attribute (dot-notation) access enabled.""" - - def __getattr__(self, key): - if key not in self: - self[key] = Namespace() - return self[key] - - def __setattr__(self, key, value): - self[key] = value - - def __delattr__(self, key): - if key in self: - del self[key] - - -class Sections(enum.Enum): - ARGS = 0 - RETURNS = 1 - YIELDS = 2 - RAISES = 3 - TYPE = 4 - - -class Formats(enum.Enum): - GOOGLE = 0 - NUMPY = 1 - RST = 2 - - -SECTION_TITLES = { - Sections.ARGS: ('argument', 'arg', 'parameter', 'param', 'key'), - Sections.RETURNS: ('return',), - Sections.YIELDS: ('yield',), - Sections.RAISES: ('raise', 'except', 'exception', 'throw', 'error', 'warn'), - Sections.TYPE: ('type',), # rst-only -} - - -def parse(docstring): - """Returns DocstringInfo about the given docstring. - - This parser aims to parse Google, numpy, and rst formatted docstrings. These - are the three most common docstring styles at the time of this writing. - - This parser aims to be permissive, working even when the docstring deviates - from the strict recommendations of these styles. - - This parser does not aim to fully extract all structured information from a - docstring, since there are simply too many ways to structure information in a - docstring. Sometimes content will remain as unstructured text and simply gets - included in the description. - - The Google docstring style guide is available at: - https://github.com/google/styleguide/blob/gh-pages/pyguide.md - - The numpy docstring style guide is available at: - https://numpydoc.readthedocs.io/en/latest/format.html - - Information about the rST docstring format is available at: - https://www.python.org/dev/peps/pep-0287/ - The full set of directives such as param and type for rST docstrings are at: - http://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html - - Note: This function does not claim to handle all docstrings well. A list of - limitations is available at the top of the file. It does aim to run without - crashing in O(n) time on all strings on length n. If you find a string that - causes this to crash or run unacceptably slowly, please consider submitting - a pull request. - - Args: - docstring: The docstring to parse. - - Returns: - A DocstringInfo containing information about the docstring. - """ - if docstring is None: - return DocstringInfo() - - lines = docstring.strip().split('\n') - lines_len = len(lines) - state = Namespace() # TODO(dbieber): Switch to an explicit class. - - # Variables in state include: - state.section.title = None - state.section.indentation = None - state.section.line1_indentation = None - state.section.format = None - state.summary.permitted = True - state.summary.lines = [] - state.description.lines = [] - state.args = [] - state.kwargs = [] - state.current_arg = None - state.returns.lines = [] - state.yields.lines = [] - state.raises.lines = [] - - for index, line in enumerate(lines): - has_next = index + 1 < lines_len - previous_line = lines[index - 1] if index > 0 else None - next_line = lines[index + 1] if has_next else None - line_info = _create_line_info(line, next_line, previous_line) - _consume_line(line_info, state) - - summary = ' '.join(state.summary.lines) if state.summary.lines else None - state.description.lines = _strip_blank_lines(state.description.lines) - description = textwrap.dedent('\n'.join(state.description.lines)) - if not description: - description = None - returns = _join_lines(state.returns.lines) - yields = _join_lines(state.yields.lines) - raises = _join_lines(state.raises.lines) - - args = [ArgInfo( - name=arg.name, type=_cast_to_known_type(_join_lines(arg.type.lines)), - description=_join_lines(arg.description.lines)) for arg in state.args] - - args.extend([KwargInfo( - name=arg.name, type=_cast_to_known_type(_join_lines(arg.type.lines)), - description=_join_lines(arg.description.lines)) for arg in state.kwargs]) - - return DocstringInfo( - summary=summary, - description=description, - args=args or None, - returns=returns, - raises=raises, - yields=yields, - ) - - -def _strip_blank_lines(lines): - """Removes lines containing only blank characters before and after the text. - - Args: - lines: A list of lines. - Returns: - A list of lines without trailing or leading blank lines. - """ - # Find the first non-blank line. - start = 0 - num_lines = len(lines) - while lines and start < num_lines and _is_blank(lines[start]): - start += 1 - - lines = lines[start:] - - # Remove trailing blank lines. - while lines and _is_blank(lines[-1]): - lines.pop() - - return lines - - -def _is_blank(line): - return not line or line.isspace() - - -def _join_lines(lines): - """Joins lines with the appropriate connective whitespace. - - This puts a single space between consecutive lines, unless there's a blank - line, in which case a full blank line is included. - - Args: - lines: A list of lines to join. - Returns: - A string, the lines joined together. - """ - # TODO(dbieber): Add parameters for variations in whitespace handling. - if not lines: - return None - - started = False - group_texts = [] # Full text of each section. - group_lines = [] # Lines within the current section. - for line in lines: - stripped_line = line.strip() - if stripped_line: - started = True - group_lines.append(stripped_line) - else: - if started: - group_text = ' '.join(group_lines) - group_texts.append(group_text) - group_lines = [] - - if group_lines: # Process the final group. - group_text = ' '.join(group_lines) - group_texts.append(group_text) - - return '\n\n'.join(group_texts) - - -def _get_or_create_arg_by_name(state, name, is_kwarg=False): - """Gets or creates a new Arg. - - These Arg objects (Namespaces) are turned into the ArgInfo namedtuples - returned by parse. Each Arg object is used to collect the name, type, and - description of a single argument to the docstring's function. - - Args: - state: The state of the parser. - name: The name of the arg to create. - is_kwarg: A boolean representing whether the argument is a keyword arg. - Returns: - The new Arg. - """ - for arg in state.args + state.kwargs: - if arg.name == name: - return arg - arg = Namespace() # TODO(dbieber): Switch to an explicit class. - arg.name = name - arg.type.lines = [] - arg.description.lines = [] - if is_kwarg: - state.kwargs.append(arg) - else: - state.args.append(arg) - return arg - - -def _is_arg_name(name): - """Returns whether name is a valid arg name. - - This is used to prevent multiple words (plaintext) from being misinterpreted - as an argument name. Any line that doesn't match the pattern for a valid - argument is treated as not being an argument. - - Args: - name: The name of the potential arg. - Returns: - True if name looks like an arg name, False otherwise. - """ - name = name.strip() - # arg_pattern is a letter or underscore followed by - # zero or more letters, numbers, or underscores. - arg_pattern = r'^[a-zA-Z_]\w*$' - re.match(arg_pattern, name) - return re.match(arg_pattern, name) is not None - - -def _as_arg_name_and_type(text): - """Returns text as a name and type, if text looks like an arg name and type. - - Example: - _as_arg_name_and_type("foo (int)") == "foo", "int" - - Args: - text: The text, which may or may not be an arg name and type. - Returns: - The arg name and type, if text looks like an arg name and type. - None otherwise. - """ - tokens = text.split() - if len(tokens) < 2: - return None - if _is_arg_name(tokens[0]): - type_token = ' '.join(tokens[1:]) - type_token = type_token.lstrip('{([').rstrip('])}') - return tokens[0], type_token - else: - return None - - -def _as_arg_names(names_str): - """Converts names_str to a list of arg names. - - Example: - _as_arg_names("a, b, c") == ["a", "b", "c"] - - Args: - names_str: A string with multiple space or comma separated arg names. - Returns: - A list of arg names, or None if names_str doesn't look like a list of arg - names. - """ - names = re.split(',| ', names_str) - names = [name.strip() for name in names if name.strip()] - for name in names: - if not _is_arg_name(name): - return None - if not names: - return None - return names - - -def _cast_to_known_type(name): - """Canonicalizes a string representing a type if possible. - - # TODO(dbieber): Support additional canonicalization, such as string/str, and - # boolean/bool. - - Example: - _cast_to_known_type("str.") == "str" - - Args: - name: A string representing a type, or None. - Returns: - A canonicalized version of the type string. - """ - if name is None: - return None - return name.rstrip('.') - - -def _consume_google_args_line(line_info, state): - """Consume a single line from a Google args section.""" - split_line = line_info.remaining.split(':', 1) - if len(split_line) > 1: - first, second = split_line # first is either the "arg" or "arg (type)" - if _is_arg_name(first.strip()): - arg = _get_or_create_arg_by_name(state, first.strip()) - arg.description.lines.append(second.strip()) - state.current_arg = arg - else: - arg_name_and_type = _as_arg_name_and_type(first) - if arg_name_and_type: - arg_name, type_str = arg_name_and_type - arg = _get_or_create_arg_by_name(state, arg_name) - arg.type.lines.append(type_str) - arg.description.lines.append(second.strip()) - state.current_arg = arg - else: - if state.current_arg: - state.current_arg.description.lines.append(split_line[0]) - else: - if state.current_arg: - state.current_arg.description.lines.append(split_line[0]) - - -def _consume_line(line_info, state): - """Consumes one line of text, updating the state accordingly. - - When _consume_line is called, part of the line may already have been processed - for header information. - - Args: - line_info: Information about the current and next line of the docstring. - state: The state of the docstring parser. - """ - _update_section_state(line_info, state) - - if state.section.title is None: - if state.summary.permitted: - if line_info.remaining: - state.summary.lines.append(line_info.remaining) - elif state.summary.lines: - state.summary.permitted = False - else: - # We're past the end of the summary. - # Additions now contribute to the description. - state.description.lines.append(line_info.remaining_raw) - else: - state.summary.permitted = False - - if state.section.new and state.section.format == Formats.RST: - # The current line starts with an RST directive, e.g. ":param arg:". - directive = _get_directive(line_info) - directive_tokens = directive.split() - if state.section.title == Sections.ARGS: - name = directive_tokens[-1] - arg = _get_or_create_arg_by_name( - state, - name, - is_kwarg=directive_tokens[0] == 'key' - ) - if len(directive_tokens) == 3: - # A param directive of the form ":param type arg:". - arg.type.lines.append(directive_tokens[1]) - state.current_arg = arg - elif state.section.title == Sections.TYPE: - name = directive_tokens[-1] - arg = _get_or_create_arg_by_name(state, name) - state.current_arg = arg - - if (state.section.format == Formats.NUMPY and - _line_is_hyphens(line_info.remaining)): - # Skip this all-hyphens line, which is part of the numpy section header. - return - - if state.section.title == Sections.ARGS: - if state.section.format == Formats.GOOGLE: - _consume_google_args_line(line_info, state) - elif state.section.format == Formats.RST: - state.current_arg.description.lines.append(line_info.remaining.strip()) - elif state.section.format == Formats.NUMPY: - line_stripped = line_info.remaining.strip() - if _is_arg_name(line_stripped): - # Token on its own line can either be the last word of the description - # of the previous arg, or a new arg. TODO: Whitespace can distinguish. - arg = _get_or_create_arg_by_name(state, line_stripped) - state.current_arg = arg - elif _line_is_numpy_parameter_type(line_info): - possible_args, type_data = line_stripped.split(':', 1) - arg_names = _as_arg_names(possible_args) # re.split(' |,', s) - if arg_names: - for arg_name in arg_names: - arg = _get_or_create_arg_by_name(state, arg_name) - arg.type.lines.append(type_data) - state.current_arg = arg # TODO(dbieber): Multiple current args. - else: # Just an ordinary line. - if state.current_arg: - state.current_arg.description.lines.append( - line_info.remaining.strip()) - else: - # TODO(dbieber): If not a blank line, add it to the description. - pass - else: # Just an ordinary line. - if state.current_arg: - state.current_arg.description.lines.append( - line_info.remaining.strip()) - else: - # TODO(dbieber): If not a blank line, add it to the description. - pass - - elif state.section.title == Sections.RETURNS: - state.returns.lines.append(line_info.remaining.strip()) - elif state.section.title == Sections.YIELDS: - state.yields.lines.append(line_info.remaining.strip()) - elif state.section.title == Sections.RAISES: - state.raises.lines.append(line_info.remaining.strip()) - elif state.section.title == Sections.TYPE: - if state.section.format == Formats.RST: - assert state.current_arg is not None - state.current_arg.type.lines.append(line_info.remaining.strip()) - else: - pass - - -def _create_line_info(line, next_line, previous_line): - """Returns information about the current line and surrounding lines.""" - line_info = Namespace() # TODO(dbieber): Switch to an explicit class. - line_info.line = line - line_info.stripped = line.strip() - line_info.remaining_raw = line_info.line - line_info.remaining = line_info.stripped - line_info.indentation = len(line) - len(line.lstrip()) - # TODO(dbieber): If next_line is blank, use the next non-blank line. - line_info.next.line = next_line - next_line_exists = next_line is not None - line_info.next.stripped = next_line.strip() if next_line_exists else None - line_info.next.indentation = ( - len(next_line) - len(next_line.lstrip()) if next_line_exists else None) - line_info.previous.line = previous_line - previous_line_exists = previous_line is not None - line_info.previous.indentation = ( - len(previous_line) - - len(previous_line.lstrip()) if previous_line_exists else None) - # Note: This counts all whitespace equally. - return line_info - - -def _update_section_state(line_info, state): - """Uses line_info to determine the current section of the docstring. - - Updates state and line_info.remaining. - - Args: - line_info: Information about the current line. - state: The state of the parser. - """ - section_updated = False - - google_section_permitted = _google_section_permitted(line_info, state) - google_section = google_section_permitted and _google_section(line_info) - if google_section: - state.section.format = Formats.GOOGLE - state.section.title = google_section - line_info.remaining = _get_after_google_header(line_info) - line_info.remaining_raw = line_info.remaining - section_updated = True - - rst_section = _rst_section(line_info) - if rst_section: - state.section.format = Formats.RST - state.section.title = rst_section - line_info.remaining = _get_after_directive(line_info) - line_info.remaining_raw = line_info.remaining - section_updated = True - - numpy_section = _numpy_section(line_info) - if numpy_section: - state.section.format = Formats.NUMPY - state.section.title = numpy_section - line_info.remaining = '' - line_info.remaining_raw = line_info.remaining - section_updated = True - - if section_updated: - state.section.new = True - state.section.indentation = line_info.indentation - state.section.line1_indentation = line_info.next.indentation - else: - state.section.new = False - - -def _google_section_permitted(line_info, state): - """Returns whether a new google section is permitted to start here. - - Q: Why might a new Google section not be allowed? - A: If we're in the middle of a Google "Args" section, then lines that start - "param:" will usually be a new arg, rather than a new section. - We use whitespace to determine when the Args section has actually ended. - - A Google section ends when either: - - A new google section begins at either - - indentation less than indentation of line 1 of the previous section - - or <= indentation of the previous section - - Or the docstring terminates. - - Args: - line_info: Information about the current line. - state: The state of the parser. - Returns: - True or False, indicating whether a new Google section is permitted at the - current line. - """ - if state.section.indentation is None: # We're not in a section yet. - return True - return (line_info.indentation <= state.section.indentation - or line_info.indentation < state.section.line1_indentation) - - -def _matches_section_title(title, section_title): - """Returns whether title is a match for a specific section_title. - - Example: - _matches_section_title('Yields', 'yield') == True - - Args: - title: The title to check for matching. - section_title: A specific known section title to check against. - """ - title = title.lower() - section_title = section_title.lower() - return section_title in (title, title[:-1]) # Supports plurals / some typos. - - -def _matches_section(title, section): - """Returns whether title is a match any known title for a specific section. - - Example: - _matches_section_title('Yields', Sections.YIELDS) == True - _matches_section_title('param', Sections.Args) == True - - Args: - title: The title to check for matching. - section: A specific section to check all possible titles for. - Returns: - True or False, indicating whether title is a match for the specified - section. - """ - for section_title in SECTION_TITLES[section]: - if _matches_section_title(title, section_title): - return True - return False - - -def _section_from_possible_title(possible_title): - """Returns a section matched by the possible title, or None if none match. - - Args: - possible_title: A string that may be the title of a new section. - Returns: - A Section type if one matches, or None if no section type matches. - """ - for section in SECTION_TITLES: - if _matches_section(possible_title, section): - return section - return None - - -def _google_section(line_info): - """Checks whether the current line is the start of a new Google-style section. - - This docstring is a Google-style docstring. Google-style sections look like - this: - - Section Name: - section body goes here - - Args: - line_info: Information about the current line. - Returns: - A Section type if one matches, or None if no section type matches. - """ - colon_index = line_info.remaining.find(':') - possible_title = line_info.remaining[:colon_index] - return _section_from_possible_title(possible_title) - - -def _get_after_google_header(line_info): - """Gets the remainder of the line, after a Google header.""" - colon_index = line_info.remaining.find(':') - return line_info.remaining[colon_index + 1:] - - -def _get_directive(line_info): - """Gets a directive from the start of the line. - - If the line is ":param str foo: Description of foo", then - _get_directive(line_info) returns "param str foo". - - Args: - line_info: Information about the current line. - Returns: - The contents of a directive, or None if the line doesn't start with a - directive. - """ - if line_info.stripped.startswith(':'): - return line_info.stripped.split(':', 2)[1] - else: - return None - - -def _get_after_directive(line_info): - """Gets the remainder of the line, after a directive.""" - sections = line_info.stripped.split(':', 2) - if len(sections) > 2: - return sections[-1] - else: - return '' - - -def _rst_section(line_info): - """Checks whether the current line is the start of a new RST-style section. - - RST uses directives to specify information. An RST directive, which we refer - to as a section here, are surrounded with colons. For example, :param name:. - - Args: - line_info: Information about the current line. - Returns: - A Section type if one matches, or None if no section type matches. - """ - directive = _get_directive(line_info) - if directive: - possible_title = directive.split()[0] - return _section_from_possible_title(possible_title) - else: - return None - - -def _line_is_hyphens(line): - """Returns whether the line is entirely hyphens (and not blank).""" - return line and not line.strip('-') - - -def _numpy_section(line_info): - """Checks whether the current line is the start of a new numpy-style section. - - Numpy style sections are followed by a full line of hyphens, for example: - - Section Name - ------------ - Section body goes here. - - Args: - line_info: Information about the current line. - Returns: - A Section type if one matches, or None if no section type matches. - """ - next_line_is_hyphens = _line_is_hyphens(line_info.next.stripped) - if next_line_is_hyphens: - possible_title = line_info.remaining - return _section_from_possible_title(possible_title) - else: - return None - - -def _line_is_numpy_parameter_type(line_info): - """Returns whether the line contains a numpy style parameter type definition. - - We look for a line of the form: - x : type - - And we have to exclude false positives on argument descriptions containing a - colon by checking the indentation of the line above. - - Args: - line_info: Information about the current line. - Returns: - True if the line is a numpy parameter type definition, False otherwise. - """ - line_stripped = line_info.remaining.strip() - if ':' in line_stripped: - previous_indent = line_info.previous.indentation - current_indent = line_info.indentation - if ':' in line_info.previous.line and current_indent > previous_indent: - # The parameter type was the previous line; this is the description. - return False - else: - return True - return False diff --git a/fire/docstrings_fuzz_test.py b/fire/docstrings_fuzz_test.py deleted file mode 100644 index 66be8006..00000000 --- a/fire/docstrings_fuzz_test.py +++ /dev/null @@ -1,36 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Fuzz tests for the docstring parser module.""" - -from fire import docstrings -from fire import testutils - -from hypothesis import example -from hypothesis import given -from hypothesis import settings -from hypothesis import strategies as st - - -class DocstringsFuzzTest(testutils.BaseTestCase): - - @settings(max_examples=1000, deadline=1000) - @given(st.text(min_size=1)) - @example('This is a one-line docstring.') - def test_fuzz_parse(self, value): - docstrings.parse(value) - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/docstrings_test.py b/fire/docstrings_test.py deleted file mode 100644 index ce516944..00000000 --- a/fire/docstrings_test.py +++ /dev/null @@ -1,361 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for fire docstrings module.""" - -from fire import docstrings -from fire import testutils - -# pylint: disable=invalid-name -DocstringInfo = docstrings.DocstringInfo -ArgInfo = docstrings.ArgInfo -KwargInfo = docstrings.KwargInfo -# pylint: enable=invalid-name - - -class DocstringsTest(testutils.BaseTestCase): - - def test_one_line_simple(self): - docstring = """A simple one line docstring.""" - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='A simple one line docstring.', - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_one_line_simple_whitespace(self): - docstring = """ - A simple one line docstring. - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='A simple one line docstring.', - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_one_line_too_long(self): - # pylint: disable=line-too-long - docstring = """A one line docstring that is both a little too verbose and a little too long so it keeps going well beyond a reasonable length for a one-liner. - """ - # pylint: enable=line-too-long - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='A one line docstring that is both a little too verbose and ' - 'a little too long so it keeps going well beyond a reasonable length ' - 'for a one-liner.', - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_one_line_runs_over(self): - # pylint: disable=line-too-long - docstring = """A one line docstring that is both a little too verbose and a little too long - so it runs onto a second line. - """ - # pylint: enable=line-too-long - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='A one line docstring that is both a little too verbose and ' - 'a little too long so it runs onto a second line.', - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_one_line_runs_over_whitespace(self): - docstring = """ - A one line docstring that is both a little too verbose and a little too long - so it runs onto a second line. - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='A one line docstring that is both a little too verbose and ' - 'a little too long so it runs onto a second line.', - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_google_format_args_only(self): - docstring = """One line description. - - Args: - arg1: arg1_description - arg2: arg2_description - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='One line description.', - args=[ - ArgInfo(name='arg1', description='arg1_description'), - ArgInfo(name='arg2', description='arg2_description'), - ] - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_google_format_arg_named_args(self): - docstring = """ - Args: - args: arg_description - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - args=[ - ArgInfo(name='args', description='arg_description'), - ] - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_google_format_typed_args_and_returns(self): - docstring = """Docstring summary. - - This is a longer description of the docstring. It spans multiple lines, as - is allowed. - - Args: - param1 (int): The first parameter. - param2 (str): The second parameter. - - Returns: - bool: The return value. True for success, False otherwise. - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='Docstring summary.', - description='This is a longer description of the docstring. It spans ' - 'multiple lines, as\nis allowed.', - args=[ - ArgInfo(name='param1', type='int', - description='The first parameter.'), - ArgInfo(name='param2', type='str', - description='The second parameter.'), - ], - returns='bool: The return value. True for success, False otherwise.' - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_google_format_multiline_arg_description(self): - docstring = """Docstring summary. - - This is a longer description of the docstring. It spans multiple lines, as - is allowed. - - Args: - param1 (int): The first parameter. - param2 (str): The second parameter. This has a lot of text, enough to - cover two lines. - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='Docstring summary.', - description='This is a longer description of the docstring. It spans ' - 'multiple lines, as\nis allowed.', - args=[ - ArgInfo(name='param1', type='int', - description='The first parameter.'), - ArgInfo(name='param2', type='str', - description='The second parameter. This has a lot of text, ' - 'enough to cover two lines.'), - ], - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_rst_format_typed_args_and_returns(self): - docstring = """Docstring summary. - - This is a longer description of the docstring. It spans across multiple - lines. - - :param arg1: Description of arg1. - :type arg1: str. - :param arg2: Description of arg2. - :type arg2: bool. - :returns: int -- description of the return value. - :raises: AttributeError, KeyError - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='Docstring summary.', - description='This is a longer description of the docstring. It spans ' - 'across multiple\nlines.', - args=[ - ArgInfo(name='arg1', type='str', - description='Description of arg1.'), - ArgInfo(name='arg2', type='bool', - description='Description of arg2.'), - ], - returns='int -- description of the return value.', - raises='AttributeError, KeyError', - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_numpy_format_typed_args_and_returns(self): - docstring = """Docstring summary. - - This is a longer description of the docstring. It spans across multiple - lines. - - Parameters - ---------- - param1 : int - The first parameter. - param2 : str - The second parameter. - - Returns - ------- - bool - True if successful, False otherwise. - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='Docstring summary.', - description='This is a longer description of the docstring. It spans ' - 'across multiple\nlines.', - args=[ - ArgInfo(name='param1', type='int', - description='The first parameter.'), - ArgInfo(name='param2', type='str', - description='The second parameter.'), - ], - # TODO(dbieber): Support return type. - returns='bool True if successful, False otherwise.', - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_numpy_format_multiline_arg_description(self): - docstring = """Docstring summary. - - This is a longer description of the docstring. It spans across multiple - lines. - - Parameters - ---------- - param1 : int - The first parameter. - param2 : str - The second parameter. This has a lot of text, enough to cover two - lines. - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='Docstring summary.', - description='This is a longer description of the docstring. It spans ' - 'across multiple\nlines.', - args=[ - ArgInfo(name='param1', type='int', - description='The first parameter.'), - ArgInfo(name='param2', type='str', - description='The second parameter. This has a lot of text, ' - 'enough to cover two lines.'), - ], - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_multisection_docstring(self): - docstring = """Docstring summary. - - This is the first section of a docstring description. - - This is the second section of a docstring description. This docstring - description has just two sections. - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='Docstring summary.', - description='This is the first section of a docstring description.' - '\n\n' - 'This is the second section of a docstring description. This docstring' - '\n' - 'description has just two sections.', - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_google_section_with_blank_first_line(self): - docstring = """Inspired by requests HTTPAdapter docstring. - - :param x: Simple param. - - Usage: - - >>> import requests - """ - docstring_info = docstrings.parse(docstring) - self.assertEqual('Inspired by requests HTTPAdapter docstring.', - docstring_info.summary) - - def test_ill_formed_docstring(self): - docstring = """Docstring summary. - - args: raises :: - : - pathological docstrings should not fail, and ideally should behave - reasonably. - """ - docstrings.parse(docstring) - - def test_strip_blank_lines(self): - lines = [' ', ' foo ', ' '] - expected_output = [' foo '] - - self.assertEqual(expected_output, docstrings._strip_blank_lines(lines)) # pylint: disable=protected-access - - def test_numpy_colon_in_description(self): - docstring = """ - Greets name. - - Arguments - --------- - name : str - name, default : World - arg2 : int - arg2, default:None - arg3 : bool - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='Greets name.', - description=None, - args=[ - ArgInfo(name='name', type='str', - description='name, default : World'), - ArgInfo(name='arg2', type='int', - description='arg2, default:None'), - ArgInfo(name='arg3', type='bool', description=None), - ] - ) - self.assertEqual(expected_docstring_info, docstring_info) - - def test_rst_format_typed_args_and_kwargs(self): - docstring = """Docstring summary. - - :param arg1: Description of arg1. - :type arg1: str. - :key arg2: Description of arg2. - :type arg2: bool. - :key arg3: Description of arg3. - :type arg3: str. - """ - docstring_info = docstrings.parse(docstring) - expected_docstring_info = DocstringInfo( - summary='Docstring summary.', - args=[ - ArgInfo(name='arg1', type='str', - description='Description of arg1.'), - KwargInfo(name='arg2', type='bool', - description='Description of arg2.'), - KwargInfo(name='arg3', type='str', - description='Description of arg3.'), - ], - ) - self.assertEqual(expected_docstring_info, docstring_info) - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/fire_import_test.py b/fire/fire_import_test.py deleted file mode 100644 index a6b4acc3..00000000 --- a/fire/fire_import_test.py +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests importing the fire module.""" - -import sys -from unittest import mock - -import fire -from fire import testutils - - -class FireImportTest(testutils.BaseTestCase): - """Tests importing Fire.""" - - def testFire(self): - with mock.patch.object(sys, 'argv', ['commandname']): - fire.Fire() - - def testFireMethods(self): - self.assertIsNotNone(fire.Fire) - - def testNoPrivateMethods(self): - self.assertTrue(hasattr(fire, 'Fire')) - self.assertFalse(hasattr(fire, '_Fire')) - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/fire_test.py b/fire/fire_test.py deleted file mode 100644 index 99b4a7c6..00000000 --- a/fire/fire_test.py +++ /dev/null @@ -1,721 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for the fire module.""" - -import os -import sys -from unittest import mock - -import fire -from fire import test_components as tc -from fire import testutils - - -class FireTest(testutils.BaseTestCase): - - def testFire(self): - with mock.patch.object(sys, 'argv', ['progname']): - fire.Fire(tc.Empty) - fire.Fire(tc.OldStyleEmpty) - fire.Fire(tc.WithInit) - # Test both passing command as a sequence and as a string. - self.assertEqual(fire.Fire(tc.NoDefaults, command='triple 4'), 12) - self.assertEqual(fire.Fire(tc.WithDefaults, command=('double', '2')), 4) - self.assertEqual(fire.Fire(tc.WithDefaults, command=['triple', '4']), 12) - self.assertEqual(fire.Fire(tc.OldStyleWithDefaults, - command=['double', '2']), 4) - self.assertEqual(fire.Fire(tc.OldStyleWithDefaults, - command=['triple', '4']), 12) - - def testFirePositionalCommand(self): - # Test passing command as a positional argument. - self.assertEqual(fire.Fire(tc.NoDefaults, 'double 2'), 4) - self.assertEqual(fire.Fire(tc.NoDefaults, ['double', '2']), 4) - - def testFireInvalidCommandArg(self): - with self.assertRaises(ValueError): - # This is not a valid command. - fire.Fire(tc.WithDefaults, command=10) - - def testFireDefaultName(self): - with mock.patch.object(sys, 'argv', - [os.path.join('python-fire', 'fire', - 'base_filename.py')]): - with self.assertOutputMatches(stdout='SYNOPSIS.*base_filename.py', - stderr=None): - fire.Fire(tc.Empty) - - def testFireNoArgs(self): - self.assertEqual(fire.Fire(tc.MixedDefaults, command=['ten']), 10) - - def testFireExceptions(self): - # Exceptions of Fire are printed to stderr and a FireExit is raised. - with self.assertRaisesFireExit(2): - fire.Fire(tc.Empty, command=['nomethod']) # Member doesn't exist. - with self.assertRaisesFireExit(2): - fire.Fire(tc.NoDefaults, command=['double']) # Missing argument. - with self.assertRaisesFireExit(2): - fire.Fire(tc.TypedProperties, command=['delta', 'x']) # Missing key. - - # Exceptions of the target components are still raised. - with self.assertRaises(ZeroDivisionError): - fire.Fire(tc.NumberDefaults, command=['reciprocal', '0.0']) - - def testFireNamedArgs(self): - self.assertEqual(fire.Fire(tc.WithDefaults, - command=['double', '--count', '5']), 10) - self.assertEqual(fire.Fire(tc.WithDefaults, - command=['triple', '--count', '5']), 15) - self.assertEqual( - fire.Fire(tc.OldStyleWithDefaults, command=['double', '--count', '5']), - 10) - self.assertEqual( - fire.Fire(tc.OldStyleWithDefaults, command=['triple', '--count', '5']), - 15) - - def testFireNamedArgsSingleHyphen(self): - self.assertEqual(fire.Fire(tc.WithDefaults, - command=['double', '-count', '5']), 10) - self.assertEqual(fire.Fire(tc.WithDefaults, - command=['triple', '-count', '5']), 15) - self.assertEqual( - fire.Fire(tc.OldStyleWithDefaults, command=['double', '-count', '5']), - 10) - self.assertEqual( - fire.Fire(tc.OldStyleWithDefaults, command=['triple', '-count', '5']), - 15) - - def testFireNamedArgsWithEquals(self): - self.assertEqual(fire.Fire(tc.WithDefaults, - command=['double', '--count=5']), 10) - self.assertEqual(fire.Fire(tc.WithDefaults, - command=['triple', '--count=5']), 15) - - def testFireNamedArgsWithEqualsSingleHyphen(self): - self.assertEqual(fire.Fire(tc.WithDefaults, - command=['double', '-count=5']), 10) - self.assertEqual(fire.Fire(tc.WithDefaults, - command=['triple', '-count=5']), 15) - - def testFireAllNamedArgs(self): - self.assertEqual(fire.Fire(tc.MixedDefaults, command=['sum', '1', '2']), 5) - self.assertEqual(fire.Fire(tc.MixedDefaults, - command=['sum', '--alpha', '1', '2']), 5) - self.assertEqual(fire.Fire(tc.MixedDefaults, - command=['sum', '--beta', '1', '2']), 4) - self.assertEqual(fire.Fire(tc.MixedDefaults, - command=['sum', '1', '--alpha', '2']), 4) - self.assertEqual(fire.Fire(tc.MixedDefaults, - command=['sum', '1', '--beta', '2']), 5) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['sum', '--alpha', '1', '--beta', '2']), 5) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['sum', '--beta', '1', '--alpha', '2']), 4) - - def testFireAllNamedArgsOneMissing(self): - self.assertEqual(fire.Fire(tc.MixedDefaults, command=['sum']), 0) - self.assertEqual(fire.Fire(tc.MixedDefaults, command=['sum', '1']), 1) - self.assertEqual(fire.Fire(tc.MixedDefaults, - command=['sum', '--alpha', '1']), 1) - self.assertEqual(fire.Fire(tc.MixedDefaults, - command=['sum', '--beta', '2']), 4) - - def testFirePartialNamedArgs(self): - self.assertEqual( - fire.Fire(tc.MixedDefaults, command=['identity', '1', '2']), (1, 2)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '--alpha', '1', '2']), (1, 2)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '--beta', '1', '2']), (2, 1)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '1', '--alpha', '2']), (2, 1)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '1', '--beta', '2']), (1, 2)) - self.assertEqual( - fire.Fire( - tc.MixedDefaults, - command=['identity', '--alpha', '1', '--beta', '2']), (1, 2)) - self.assertEqual( - fire.Fire( - tc.MixedDefaults, - command=['identity', '--beta', '1', '--alpha', '2']), (2, 1)) - - def testFirePartialNamedArgsOneMissing(self): - # Errors are written to standard out and a FireExit is raised. - with self.assertRaisesFireExit(2): - fire.Fire(tc.MixedDefaults, - command=['identity']) # Identity needs an arg. - - with self.assertRaisesFireExit(2): - # Identity needs a value for alpha. - fire.Fire(tc.MixedDefaults, command=['identity', '--beta', '2']) - - self.assertEqual( - fire.Fire(tc.MixedDefaults, command=['identity', '1']), (1, '0')) - self.assertEqual( - fire.Fire(tc.MixedDefaults, command=['identity', '--alpha', '1']), - (1, '0')) - - def testFireAnnotatedArgs(self): - self.assertEqual(fire.Fire(tc.Annotations, command=['double', '5']), 10) - self.assertEqual(fire.Fire(tc.Annotations, command=['triple', '5']), 15) - - def testFireKeywordOnlyArgs(self): - with self.assertRaisesFireExit(2): - # Keyword arguments must be passed with flag syntax. - fire.Fire(tc.py3.KeywordOnly, command=['double', '5']) - - self.assertEqual( - fire.Fire(tc.py3.KeywordOnly, command=['double', '--count', '5']), 10) - self.assertEqual( - fire.Fire(tc.py3.KeywordOnly, command=['triple', '--count', '5']), 15) - - def testFireProperties(self): - self.assertEqual(fire.Fire(tc.TypedProperties, command=['alpha']), True) - self.assertEqual(fire.Fire(tc.TypedProperties, command=['beta']), (1, 2, 3)) - - def testFireRecursion(self): - self.assertEqual( - fire.Fire(tc.TypedProperties, - command=['charlie', 'double', 'hello']), 'hellohello') - self.assertEqual(fire.Fire(tc.TypedProperties, - command=['charlie', 'triple', 'w']), 'www') - - def testFireVarArgs(self): - self.assertEqual( - fire.Fire(tc.VarArgs, - command=['cumsums', 'a', 'b', 'c', 'd']), - ['a', 'ab', 'abc', 'abcd']) - self.assertEqual( - fire.Fire(tc.VarArgs, command=['cumsums', '1', '2', '3', '4']), - [1, 3, 6, 10]) - - def testFireVarArgsWithNamedArgs(self): - self.assertEqual( - fire.Fire(tc.VarArgs, command=['varchars', '1', '2', 'c', 'd']), - (1, 2, 'cd')) - self.assertEqual( - fire.Fire(tc.VarArgs, command=['varchars', '3', '4', 'c', 'd', 'e']), - (3, 4, 'cde')) - - def testFireKeywordArgs(self): - self.assertEqual( - fire.Fire( - tc.Kwargs, - command=['props', '--name', 'David', '--age', '24']), - {'name': 'David', 'age': 24}) - # Run this test both with a list command and a string command. - self.assertEqual( - fire.Fire( - tc.Kwargs, - command=['props', '--message', - '"This is a message it has -- in it"']), # Quotes stripped - {'message': 'This is a message it has -- in it'}) - self.assertEqual( - fire.Fire( - tc.Kwargs, - command=['props', '--message', - 'This is a message it has -- in it']), - {'message': 'This is a message it has -- in it'}) - self.assertEqual( - fire.Fire( - tc.Kwargs, - command='props --message "This is a message it has -- in it"'), - {'message': 'This is a message it has -- in it'}) - self.assertEqual( - fire.Fire(tc.Kwargs, - command=['upper', '--alpha', 'A', '--beta', 'B']), - 'ALPHA BETA') - self.assertEqual( - fire.Fire( - tc.Kwargs, - command=['upper', '--alpha', 'A', '--beta', 'B', '-', 'lower']), - 'alpha beta') - - def testFireKeywordArgsWithMissingPositionalArgs(self): - self.assertEqual( - fire.Fire(tc.Kwargs, command=['run', 'Hello', 'World', '--cell', 'is']), - ('Hello', 'World', {'cell': 'is'})) - self.assertEqual( - fire.Fire(tc.Kwargs, command=['run', 'Hello', '--cell', 'ok']), - ('Hello', None, {'cell': 'ok'})) - - def testFireObject(self): - self.assertEqual( - fire.Fire(tc.WithDefaults(), command=['double', '--count', '5']), 10) - self.assertEqual( - fire.Fire(tc.WithDefaults(), command=['triple', '--count', '5']), 15) - - def testFireDict(self): - component = { - 'double': lambda x=0: 2 * x, - 'cheese': 'swiss', - } - self.assertEqual(fire.Fire(component, command=['double', '5']), 10) - self.assertEqual(fire.Fire(component, command=['cheese']), 'swiss') - - def testFireObjectWithDict(self): - self.assertEqual( - fire.Fire(tc.TypedProperties, command=['delta', 'echo']), 'E') - self.assertEqual( - fire.Fire(tc.TypedProperties, command=['delta', 'echo', 'lower']), 'e') - self.assertIsInstance( - fire.Fire(tc.TypedProperties, command=['delta', 'nest']), dict) - self.assertEqual( - fire.Fire(tc.TypedProperties, command=['delta', 'nest', '0']), 'a') - - def testFireSet(self): - component = tc.simple_set() - result = fire.Fire(component, command=[]) - self.assertEqual(len(result), 3) - - def testFireFrozenset(self): - component = tc.simple_frozenset() - result = fire.Fire(component, command=[]) - self.assertEqual(len(result), 3) - - def testFireList(self): - component = ['zero', 'one', 'two', 'three'] - self.assertEqual(fire.Fire(component, command=['2']), 'two') - self.assertEqual(fire.Fire(component, command=['3']), 'three') - self.assertEqual(fire.Fire(component, command=['-1']), 'three') - - def testFireObjectWithList(self): - self.assertEqual(fire.Fire(tc.TypedProperties, command=['echo', '0']), - 'alex') - self.assertEqual(fire.Fire(tc.TypedProperties, command=['echo', '1']), - 'bethany') - - def testFireObjectWithTuple(self): - self.assertEqual(fire.Fire(tc.TypedProperties, command=['fox', '0']), - 'carry') - self.assertEqual(fire.Fire(tc.TypedProperties, command=['fox', '1']), - 'divide') - - def testFireObjectWithListAsObject(self): - self.assertEqual( - fire.Fire(tc.TypedProperties, command=['echo', 'count', 'bethany']), - 1) - - def testFireObjectWithTupleAsObject(self): - self.assertEqual( - fire.Fire(tc.TypedProperties, command=['fox', 'count', 'divide']), - 1) - - def testFireNoComponent(self): - self.assertEqual(fire.Fire(command=['tc', 'WithDefaults', 'double', '10']), - 20) - last_char = lambda text: text[-1] # pylint: disable=unused-variable - self.assertEqual(fire.Fire(command=['last_char', '"Hello"']), 'o') - self.assertEqual(fire.Fire(command=['last-char', '"World"']), 'd') - rset = lambda count=0: set(range(count)) # pylint: disable=unused-variable - self.assertEqual(fire.Fire(command=['rset', '5']), {0, 1, 2, 3, 4}) - - def testFireUnderscores(self): - self.assertEqual( - fire.Fire(tc.Underscores, - command=['underscore-example']), 'fish fingers') - self.assertEqual( - fire.Fire(tc.Underscores, - command=['underscore_example']), 'fish fingers') - - def testFireUnderscoresInArg(self): - self.assertEqual( - fire.Fire(tc.Underscores, - command=['underscore-function', 'example']), 'example') - self.assertEqual( - fire.Fire(tc.Underscores, - command=['underscore_function', '--underscore-arg=score']), - 'score') - self.assertEqual( - fire.Fire(tc.Underscores, - command=['underscore_function', '--underscore_arg=score']), - 'score') - - def testBoolParsing(self): - self.assertEqual(fire.Fire(tc.BoolConverter, command=['as-bool', 'True']), - True) - self.assertEqual( - fire.Fire(tc.BoolConverter, command=['as-bool', 'False']), False) - self.assertEqual( - fire.Fire(tc.BoolConverter, command=['as-bool', '--arg=True']), True) - self.assertEqual( - fire.Fire(tc.BoolConverter, command=['as-bool', '--arg=False']), False) - self.assertEqual(fire.Fire(tc.BoolConverter, command=['as-bool', '--arg']), - True) - self.assertEqual( - fire.Fire(tc.BoolConverter, command=['as-bool', '--noarg']), False) - - def testBoolParsingContinued(self): - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', 'True', 'False']), (True, False)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '--alpha=False', '10']), (False, 10)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '--alpha', '--beta', '10']), (True, 10)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '--alpha', '--beta=10']), (True, 10)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '--noalpha', '--beta']), (False, True)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, command=['identity', '10', '--beta']), - (10, True)) - - def testBoolParsingSingleHyphen(self): - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-alpha=False', '10']), (False, 10)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-alpha', '-beta', '10']), (True, 10)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-alpha', '-beta=10']), (True, 10)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-noalpha', '-beta']), (False, True)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-alpha', '-10', '-beta']), (-10, True)) - - def testBoolParsingLessExpectedCases(self): - # Note: Does not return (True, 10). - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '--alpha', '10']), (10, '0')) - # To get (True, 10), use one of the following: - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '--alpha', '--beta=10']), - (True, 10)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', 'True', '10']), (True, 10)) - - # Note: Does not return (True, '--test') or ('--test', 0). - with self.assertRaisesFireExit(2): - fire.Fire(tc.MixedDefaults, command=['identity', '--alpha', '--test']) - - self.assertEqual( - fire.Fire( - tc.MixedDefaults, - command=['identity', '--alpha', 'True', '"--test"']), - (True, '--test')) - # To get ('--test', '0'), use one of the following: - self.assertEqual(fire.Fire(tc.MixedDefaults, - command=['identity', '--alpha=--test']), - ('--test', '0')) - self.assertEqual( - fire.Fire(tc.MixedDefaults, command=r'identity --alpha \"--test\"'), - ('--test', '0')) - - def testSingleCharFlagParsing(self): - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-a']), (True, '0')) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-a', '--beta=10']), (True, 10)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-a', '-b']), (True, True)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-a', '42', '-b']), (42, True)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-a', '42', '-b', '10']), (42, 10)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '--alpha', 'True', '-b', '10']), - (True, 10)) - with self.assertRaisesFireExit(2): - # This test attempts to use an ambiguous shortcut flag on a function with - # a naming conflict for the shortcut, triggering a FireError. - fire.Fire(tc.SimilarArgNames, command=['identity', '-b']) - - def testSingleCharFlagParsingEqualSign(self): - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-a=True']), (True, '0')) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-a=3', '--beta=10']), (3, 10)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-a=False', '-b=15']), (False, 15)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-a', '42', '-b=12']), (42, 12)) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-a=42', '-b', '10']), (42, 10)) - - def testSingleCharFlagParsingExactMatch(self): - self.assertEqual( - fire.Fire(tc.SimilarArgNames, - command=['identity2', '-a']), (True, None)) - self.assertEqual( - fire.Fire(tc.SimilarArgNames, - command=['identity2', '-a=10']), (10, None)) - self.assertEqual( - fire.Fire(tc.SimilarArgNames, - command=['identity2', '--a']), (True, None)) - self.assertEqual( - fire.Fire(tc.SimilarArgNames, - command=['identity2', '-alpha']), (None, True)) - self.assertEqual( - fire.Fire(tc.SimilarArgNames, - command=['identity2', '-a', '-alpha']), (True, True)) - - def testSingleCharFlagParsingCapitalLetter(self): - self.assertEqual( - fire.Fire(tc.CapitalizedArgNames, - command=['sum', '-D', '5', '-G', '10']), 15) - - def testBoolParsingWithNo(self): - # In these examples --nothing always refers to the nothing argument: - def fn1(thing, nothing): - return thing, nothing - - self.assertEqual(fire.Fire(fn1, command=['--thing', '--nothing']), - (True, True)) - self.assertEqual(fire.Fire(fn1, command=['--thing', '--nonothing']), - (True, False)) - - with self.assertRaisesFireExit(2): - # In this case nothing=False (since rightmost setting of a flag gets - # precedence), but it errors because thing has no value. - fire.Fire(fn1, command=['--nothing', '--nonothing']) - - # In these examples, --nothing sets thing=False: - def fn2(thing, **kwargs): - return thing, kwargs - self.assertEqual(fire.Fire(fn2, command=['--thing']), (True, {})) - self.assertEqual(fire.Fire(fn2, command=['--nothing']), (False, {})) - with self.assertRaisesFireExit(2): - # In this case, nothing=True, but it errors because thing has no value. - fire.Fire(fn2, command=['--nothing=True']) - self.assertEqual(fire.Fire(fn2, command=['--nothing', '--nothing=True']), - (False, {'nothing': True})) - - def fn3(arg, **kwargs): - return arg, kwargs - self.assertEqual(fire.Fire(fn3, command=['--arg=value', '--thing']), - ('value', {'thing': True})) - self.assertEqual(fire.Fire(fn3, command=['--arg=value', '--nothing']), - ('value', {'thing': False})) - self.assertEqual(fire.Fire(fn3, command=['--arg=value', '--nonothing']), - ('value', {'nothing': False})) - - def testTraceFlag(self): - with self.assertRaisesFireExit(0, 'Fire trace:\n'): - fire.Fire(tc.BoolConverter, command=['as-bool', 'True', '--', '--trace']) - with self.assertRaisesFireExit(0, 'Fire trace:\n'): - fire.Fire(tc.BoolConverter, command=['as-bool', 'True', '--', '-t']) - with self.assertRaisesFireExit(0, 'Fire trace:\n'): - fire.Fire(tc.BoolConverter, command=['--', '--trace']) - - def testHelpFlag(self): - with self.assertRaisesFireExit(0): - fire.Fire(tc.BoolConverter, command=['as-bool', 'True', '--', '--help']) - with self.assertRaisesFireExit(0): - fire.Fire(tc.BoolConverter, command=['as-bool', 'True', '--', '-h']) - with self.assertRaisesFireExit(0): - fire.Fire(tc.BoolConverter, command=['--', '--help']) - - def testHelpFlagAndTraceFlag(self): - with self.assertRaisesFireExit(0, 'Fire trace:\n.*SYNOPSIS'): - fire.Fire(tc.BoolConverter, - command=['as-bool', 'True', '--', '--help', '--trace']) - with self.assertRaisesFireExit(0, 'Fire trace:\n.*SYNOPSIS'): - fire.Fire(tc.BoolConverter, command=['as-bool', 'True', '--', '-h', '-t']) - with self.assertRaisesFireExit(0, 'Fire trace:\n.*SYNOPSIS'): - fire.Fire(tc.BoolConverter, command=['--', '-h', '--trace']) - - def testTabCompletionNoName(self): - completion_script = fire.Fire(tc.NoDefaults, command=['--', '--completion']) - self.assertIn('double', completion_script) - self.assertIn('triple', completion_script) - - def testTabCompletion(self): - completion_script = fire.Fire( - tc.NoDefaults, command=['--', '--completion'], name='c') - self.assertIn('double', completion_script) - self.assertIn('triple', completion_script) - - def testTabCompletionWithDict(self): - actions = {'multiply': lambda a, b: a * b} - completion_script = fire.Fire( - actions, command=['--', '--completion'], name='actCLI') - self.assertIn('actCLI', completion_script) - self.assertIn('multiply', completion_script) - - def testBasicSeparator(self): - # '-' is the default separator. - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '+', '_']), ('+', '_')) - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '_', '+', '-']), ('_', '+')) - - # If we change the separator we can use '-' as an argument. - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['identity', '-', '_', '--', '--separator', '&']), - ('-', '_')) - - # The separator triggers a function call, but there aren't enough arguments. - with self.assertRaisesFireExit(2): - fire.Fire(tc.MixedDefaults, command=['identity', '-', '_', '+']) - - def testNonComparable(self): - """Fire should work with classes that disallow comparisons.""" - # Make sure this test passes both with a string command or a list command. - self.assertIsInstance( - fire.Fire(tc.NonComparable, command=''), tc.NonComparable) - self.assertIsInstance( - fire.Fire(tc.NonComparable, command=[]), tc.NonComparable) - - # The first separator instantiates the NonComparable object. - # The second separator causes Fire to check if the separator was necessary. - self.assertIsInstance( - fire.Fire(tc.NonComparable, command=['-', '-']), tc.NonComparable) - - def testExtraSeparators(self): - self.assertEqual( - fire.Fire( - tc.ReturnsObj, - command=['get-obj', 'arg1', 'arg2', '-', '-', 'as-bool', 'True']), - True) - self.assertEqual( - fire.Fire( - tc.ReturnsObj, - command=['get-obj', 'arg1', 'arg2', '-', '-', '-', 'as-bool', - 'True']), - True) - - def testSeparatorForChaining(self): - # Without a separator all args are consumed by get_obj. - self.assertIsInstance( - fire.Fire(tc.ReturnsObj, - command=['get-obj', 'arg1', 'arg2', 'as-bool', 'True']), - tc.BoolConverter) - # With a separator only the preceding args are consumed by get_obj. - self.assertEqual( - fire.Fire( - tc.ReturnsObj, - command=['get-obj', 'arg1', 'arg2', '-', 'as-bool', 'True']), True) - self.assertEqual( - fire.Fire(tc.ReturnsObj, - command=['get-obj', 'arg1', 'arg2', '&', 'as-bool', 'True', - '--', '--separator', '&']), - True) - self.assertEqual( - fire.Fire(tc.ReturnsObj, - command=['get-obj', 'arg1', '$$', 'as-bool', 'True', '--', - '--separator', '$$']), - True) - - def testNegativeNumbers(self): - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['sum', '--alpha', '-3', '--beta', '-4']), -11) - - def testFloatForExpectedInt(self): - self.assertEqual( - fire.Fire(tc.MixedDefaults, - command=['sum', '--alpha', '2.2', '--beta', '3.0']), 8.2) - self.assertEqual( - fire.Fire( - tc.NumberDefaults, - command=['integer_reciprocal', '--divisor', '5.0']), 0.2) - self.assertEqual( - fire.Fire(tc.NumberDefaults, command=['integer_reciprocal', '4.0']), - 0.25) - - def testClassInstantiation(self): - self.assertIsInstance(fire.Fire(tc.InstanceVars, - command=['--arg1=a1', '--arg2=a2']), - tc.InstanceVars) - with self.assertRaisesFireExit(2): - # Cannot instantiate a class with positional args. - fire.Fire(tc.InstanceVars, command=['a1', 'a2']) - - def testTraceErrors(self): - # Class needs additional value but runs out of args. - with self.assertRaisesFireExit(2): - fire.Fire(tc.InstanceVars, command=['a1']) - with self.assertRaisesFireExit(2): - fire.Fire(tc.InstanceVars, command=['--arg1=a1']) - - # Routine needs additional value but runs out of args. - with self.assertRaisesFireExit(2): - fire.Fire(tc.InstanceVars, command=['a1', 'a2', '-', 'run', 'b1']) - with self.assertRaisesFireExit(2): - fire.Fire(tc.InstanceVars, - command=['--arg1=a1', '--arg2=a2', '-', 'run b1']) - - # Extra args cannot be consumed. - with self.assertRaisesFireExit(2): - fire.Fire(tc.InstanceVars, - command=['a1', 'a2', '-', 'run', 'b1', 'b2', 'b3']) - with self.assertRaisesFireExit(2): - fire.Fire( - tc.InstanceVars, - command=['--arg1=a1', '--arg2=a2', '-', 'run', 'b1', 'b2', 'b3']) - - # Cannot find member to access. - with self.assertRaisesFireExit(2): - fire.Fire(tc.InstanceVars, command=['a1', 'a2', '-', 'jog']) - with self.assertRaisesFireExit(2): - fire.Fire(tc.InstanceVars, command=['--arg1=a1', '--arg2=a2', '-', 'jog']) - - def testClassWithDefaultMethod(self): - self.assertEqual( - fire.Fire(tc.DefaultMethod, command=['double', '10']), 20 - ) - - def testClassWithInvalidProperty(self): - self.assertEqual( - fire.Fire(tc.InvalidProperty, command=['double', '10']), 20 - ) - - def testHelpKwargsDecorator(self): - # Issue #190, follow the wrapped method instead of crashing. - with self.assertRaisesFireExit(0): - fire.Fire(tc.decorated_method, command=['-h']) - with self.assertRaisesFireExit(0): - fire.Fire(tc.decorated_method, command=['--help']) - - def testFireAsyncio(self): - self.assertEqual(fire.Fire(tc.py3.WithAsyncio, - command=['double', '--count', '10']), 20) - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/formatting.py b/fire/formatting.py deleted file mode 100644 index 68484c27..00000000 --- a/fire/formatting.py +++ /dev/null @@ -1,93 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Formatting utilities for use in creating help text.""" - -from fire import formatting_windows # pylint: disable=unused-import -import termcolor - - -ELLIPSIS = '...' - - -def Indent(text, spaces=2): - lines = text.split('\n') - return '\n'.join( - ' ' * spaces + line if line else line - for line in lines) - - -def Bold(text): - return termcolor.colored(text, attrs=['bold']) - - -def Underline(text): - return termcolor.colored(text, attrs=['underline']) - - -def BoldUnderline(text): - return Bold(Underline(text)) - - -def WrappedJoin(items, separator=' | ', width=80): - """Joins the items by the separator, wrapping lines at the given width.""" - lines = [] - current_line = '' - for index, item in enumerate(items): - is_final_item = index == len(items) - 1 - if is_final_item: - if len(current_line) + len(item) <= width: - current_line += item - else: - lines.append(current_line.rstrip()) - current_line = item - else: - if len(current_line) + len(item) + len(separator) <= width: - current_line += item + separator - else: - lines.append(current_line.rstrip()) - current_line = item + separator - - lines.append(current_line) - return lines - - -def Error(text): - return termcolor.colored(text, color='red', attrs=['bold']) - - -def EllipsisTruncate(text, available_space, line_length): - """Truncate text from the end with ellipsis.""" - if available_space < len(ELLIPSIS): - available_space = line_length - # No need to truncate - if len(text) <= available_space: - return text - return text[:available_space - len(ELLIPSIS)] + ELLIPSIS - - -def EllipsisMiddleTruncate(text, available_space, line_length): - """Truncates text from the middle with ellipsis.""" - if available_space < len(ELLIPSIS): - available_space = line_length - if len(text) < available_space: - return text - available_string_len = available_space - len(ELLIPSIS) - first_half_len = int(available_string_len / 2) # start from middle - second_half_len = available_string_len - first_half_len - return text[:first_half_len] + ELLIPSIS + text[-second_half_len:] - - -def DoubleQuote(text): - return '"%s"' % text diff --git a/fire/formatting_test.py b/fire/formatting_test.py deleted file mode 100644 index e0f6699d..00000000 --- a/fire/formatting_test.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for formatting.py.""" - -from fire import formatting -from fire import testutils - -LINE_LENGTH = 80 - - -class FormattingTest(testutils.BaseTestCase): - - def test_bold(self): - text = formatting.Bold('hello') - self.assertIn(text, ['hello', '\x1b[1mhello\x1b[0m']) - - def test_underline(self): - text = formatting.Underline('hello') - self.assertIn(text, ['hello', '\x1b[4mhello\x1b[0m']) - - def test_indent(self): - text = formatting.Indent('hello', spaces=2) - self.assertEqual(' hello', text) - - def test_indent_multiple_lines(self): - text = formatting.Indent('hello\nworld', spaces=2) - self.assertEqual(' hello\n world', text) - - def test_wrap_one_item(self): - lines = formatting.WrappedJoin(['rice']) - self.assertEqual(['rice'], lines) - - def test_wrap_multiple_items(self): - lines = formatting.WrappedJoin(['rice', 'beans', 'chicken', 'cheese'], - width=15) - self.assertEqual(['rice | beans |', - 'chicken |', - 'cheese'], lines) - - def test_ellipsis_truncate(self): - text = 'This is a string' - truncated_text = formatting.EllipsisTruncate( - text=text, available_space=10, line_length=LINE_LENGTH) - self.assertEqual('This is...', truncated_text) - - def test_ellipsis_truncate_not_enough_space(self): - text = 'This is a string' - truncated_text = formatting.EllipsisTruncate( - text=text, available_space=2, line_length=LINE_LENGTH) - self.assertEqual('This is a string', truncated_text) - - def test_ellipsis_middle_truncate(self): - text = '1000000000L' - truncated_text = formatting.EllipsisMiddleTruncate( - text=text, available_space=7, line_length=LINE_LENGTH) - self.assertEqual('10...0L', truncated_text) - - def test_ellipsis_middle_truncate_not_enough_space(self): - text = '1000000000L' - truncated_text = formatting.EllipsisMiddleTruncate( - text=text, available_space=2, line_length=LINE_LENGTH) - self.assertEqual('1000000000L', truncated_text) - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/formatting_windows.py b/fire/formatting_windows.py deleted file mode 100644 index 749ab6d0..00000000 --- a/fire/formatting_windows.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""This module is used for enabling formatting on Windows.""" - -import ctypes -import os -import platform -import subprocess -import sys - -try: - import colorama # pylint: disable=g-import-not-at-top - HAS_COLORAMA = True -except ImportError: - HAS_COLORAMA = False - - -def initialize_or_disable(): - """Enables ANSI processing on Windows or disables it as needed.""" - if HAS_COLORAMA: - wrap = True - if (hasattr(sys.stdout, 'isatty') - and sys.stdout.isatty() - and platform.release() == '10'): - # Enables native ANSI sequences in console. - # Windows 10, 2016, and 2019 only. - - wrap = False - kernel32 = ctypes.windll.kernel32 - enable_virtual_terminal_processing = 0x04 - out_handle = kernel32.GetStdHandle(subprocess.STD_OUTPUT_HANDLE) # pylint: disable=line-too-long, - # GetConsoleMode fails if the terminal isn't native. - mode = ctypes.wintypes.DWORD() - if kernel32.GetConsoleMode(out_handle, ctypes.byref(mode)) == 0: - wrap = True - if not mode.value & enable_virtual_terminal_processing: - if kernel32.SetConsoleMode( - out_handle, mode.value | enable_virtual_terminal_processing) == 0: - # kernel32.SetConsoleMode to enable ANSI sequences failed - wrap = True - colorama.init(wrap=wrap) - else: - os.environ['ANSI_COLORS_DISABLED'] = '1' - -if sys.platform.startswith('win'): - initialize_or_disable() diff --git a/fire/helptext.py b/fire/helptext.py deleted file mode 100644 index 347278da..00000000 --- a/fire/helptext.py +++ /dev/null @@ -1,787 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Utilities for producing help strings for use in Fire CLIs. - -Can produce help strings suitable for display in Fire CLIs for any type of -Python object, module, class, or function. - -There are two types of informative strings: Usage and Help screens. - -Usage screens are shown when the user accesses a group or accesses a command -without calling it. A Usage screen shows information about how to use that group -or command. Usage screens are typically short and show the minimal information -necessary for the user to determine how to proceed. - -Help screens are shown when the user requests help with the help flag (--help). -Help screens are shown in a less-style console view, and contain detailed help -information. -""" - -from __future__ import annotations - -import collections -import itertools - -from fire import completion -from fire import custom_descriptions -from fire import decorators -from fire import docstrings -from fire import formatting -from fire import inspectutils -from fire import value_types - -LINE_LENGTH = 80 -SECTION_INDENTATION = 4 -SUBSECTION_INDENTATION = 4 - - -def HelpText(component, trace=None, verbose=False): - """Gets the help string for the current component, suitable for a help screen. - - Args: - component: The component to construct the help string for. - trace: The Fire trace of the command so far. The command executed so far - can be extracted from this trace. - verbose: Whether to include private members in the help screen. - - Returns: - The full help screen as a string. - """ - # Preprocessing needed to create the sections: - info = inspectutils.Info(component) - actions_grouped_by_kind = _GetActionsGroupedByKind(component, verbose=verbose) - spec = inspectutils.GetFullArgSpec(component) - metadata = decorators.GetMetadata(component) - - # Sections: - name_section = _NameSection(component, info, trace=trace, verbose=verbose) - synopsis_section = _SynopsisSection( - component, actions_grouped_by_kind, spec, metadata, trace=trace) - description_section = _DescriptionSection(component, info) - # TODO(dbieber): Add returns and raises sections for functions. - - if callable(component): - args_and_flags_sections, notes_sections = _ArgsAndFlagsSections( - info, spec, metadata) - else: - args_and_flags_sections = [] - notes_sections = [] - usage_details_sections = _UsageDetailsSections(component, - actions_grouped_by_kind) - - sections = ( - [name_section, synopsis_section, description_section] - + args_and_flags_sections - + usage_details_sections - + notes_sections - ) - valid_sections = [section for section in sections if section is not None] - return '\n\n'.join( - _CreateOutputSection(name, content) - for name, content in valid_sections - ) - - -def _NameSection(component, info, trace=None, verbose=False) -> tuple[str, str]: - """The "Name" section of the help string.""" - - # Only include separators in the name in verbose mode. - current_command = _GetCurrentCommand(trace, include_separators=verbose) - summary = _GetSummary(info) - - # If the docstring is one of the messy builtin docstrings, show custom one. - if custom_descriptions.NeedsCustomDescription(component): - available_space = LINE_LENGTH - SECTION_INDENTATION - len(current_command + - ' - ') - summary = custom_descriptions.GetSummary(component, available_space, - LINE_LENGTH) - - if summary: - text = f'{current_command} - {summary}' - else: - text = current_command - return ('NAME', text) - - -def _SynopsisSection(component, actions_grouped_by_kind, spec, metadata, - trace=None) -> tuple[str, str]: - """The "Synopsis" section of the help string.""" - current_command = _GetCurrentCommand(trace=trace, include_separators=True) - - possible_actions = _GetPossibleActions(actions_grouped_by_kind) - - continuations = [] - if possible_actions: - continuations.append(_GetPossibleActionsString(possible_actions)) - if callable(component): - callable_continuation = _GetArgsAndFlagsString(spec, metadata) - if callable_continuation: - continuations.append(callable_continuation) - elif trace: - # This continuation might be blank if no args are needed. - # In this case, show a separator. - continuations.append(trace.separator) - continuation = ' | '.join(continuations) - - text = f'{current_command} {continuation}' - return ('SYNOPSIS', text) - - -def _DescriptionSection(component, info) -> tuple[str, str] | None: - """The "Description" sections of the help string. - - Args: - component: The component to produce the description section for. - info: The info dict for the component of interest. - - Returns: - Returns the description if available. If not, returns the summary. - If neither are available, returns None. - """ - if custom_descriptions.NeedsCustomDescription(component): - available_space = LINE_LENGTH - SECTION_INDENTATION - description = custom_descriptions.GetDescription(component, available_space, - LINE_LENGTH) - summary = custom_descriptions.GetSummary(component, available_space, - LINE_LENGTH) - else: - description = _GetDescription(info) - summary = _GetSummary(info) - # Fall back to summary if description is not available. - text = description or summary or None - if text: - return ('DESCRIPTION', text) - else: - return None - - -def _CreateKeywordOnlyFlagItem(flag, docstring_info, spec, short_arg): - return _CreateFlagItem( - flag, docstring_info, spec, required=flag not in spec.kwonlydefaults, - short_arg=short_arg) - - -def _GetShortFlags(flags): - """Gets a list of single-character flags that uniquely identify a flag. - - Args: - flags: list of strings representing flags - - Returns: - List of single character short flags, - where the character occurred at the start of a flag once. - """ - short_flags = [f[0] for f in flags] - short_flag_counts = collections.Counter(short_flags) - return [v for v in short_flags if short_flag_counts[v] == 1] - - -def _ArgsAndFlagsSections(info, spec, metadata): - """The "Args and Flags" sections of the help string.""" - args_with_no_defaults = spec.args[:len(spec.args) - len(spec.defaults)] - args_with_defaults = spec.args[len(spec.args) - len(spec.defaults):] - - # Check if positional args are allowed. If not, require flag syntax for args. - accepts_positional_args = metadata.get(decorators.ACCEPTS_POSITIONAL_ARGS) - - args_and_flags_sections = [] - notes_sections = [] - - docstring_info = info['docstring_info'] - - arg_items = [ - _CreateArgItem(arg, docstring_info, spec) - for arg in args_with_no_defaults - ] - - if spec.varargs: - arg_items.append( - _CreateArgItem(spec.varargs, docstring_info, spec) - ) - - if arg_items: - title = 'POSITIONAL ARGUMENTS' if accepts_positional_args else 'ARGUMENTS' - arguments_section = (title, '\n'.join(arg_items).rstrip('\n')) - args_and_flags_sections.append(arguments_section) - if args_with_no_defaults and accepts_positional_args: - notes_sections.append( - ('NOTES', 'You can also use flags syntax for POSITIONAL ARGUMENTS') - ) - - unique_short_args = _GetShortFlags(args_with_defaults) - positional_flag_items = [ - _CreateFlagItem( - flag, docstring_info, spec, required=False, - short_arg=flag[0] in unique_short_args - ) - for flag in args_with_defaults - ] - - unique_short_kwonly_flags = _GetShortFlags(spec.kwonlyargs) - kwonly_flag_items = [ - _CreateKeywordOnlyFlagItem( - flag, docstring_info, spec, - short_arg=flag[0] in unique_short_kwonly_flags - ) - for flag in spec.kwonlyargs - ] - flag_items = positional_flag_items + kwonly_flag_items - - if spec.varkw: - # Include kwargs documented via :key param: - documented_kwargs = [] - - # add short flags if possible - flags = docstring_info.args or [] - flag_names = [f.name for f in flags] - unique_short_flags = _GetShortFlags(flag_names) - for flag in flags: - if isinstance(flag, docstrings.KwargInfo): - if flag.name[0] in unique_short_flags: - short_name = flag.name[0] - flag_string = f'-{short_name}, --{flag.name}' - else: - flag_string = f'--{flag.name}' - - flag_item = _CreateFlagItem( - flag.name, docstring_info, spec, - flag_string=flag_string) - documented_kwargs.append(flag_item) - if documented_kwargs: - # Separate documented kwargs from other flags using a message - if flag_items: - message = 'The following flags are also accepted.' - item = _CreateItem(message, None, indent=4) - flag_items.append(item) - flag_items.extend(documented_kwargs) - - description = _GetArgDescription(spec.varkw, docstring_info) - if documented_kwargs: - message = 'Additional undocumented flags may also be accepted.' - elif flag_items: - message = 'Additional flags are accepted.' - else: - message = 'Flags are accepted.' - item = _CreateItem(message, description, indent=4) - flag_items.append(item) - - if flag_items: - flags_section = ('FLAGS', '\n'.join(flag_items)) - args_and_flags_sections.append(flags_section) - - return args_and_flags_sections, notes_sections - - -def _UsageDetailsSections(component, actions_grouped_by_kind): - """The usage details sections of the help string.""" - groups, commands, values, indexes = actions_grouped_by_kind - - sections = [] - if groups.members: - sections.append(_MakeUsageDetailsSection(groups)) - if commands.members: - sections.append(_MakeUsageDetailsSection(commands)) - if values.members: - sections.append(_ValuesUsageDetailsSection(component, values)) - if indexes.members: - sections.append(('INDEXES', _NewChoicesSection('INDEX', indexes.names))) - - return sections - - -def _GetSummary(info): - docstring_info = info['docstring_info'] - return docstring_info.summary if docstring_info.summary else None - - -def _GetDescription(info): - docstring_info = info['docstring_info'] - return docstring_info.description if docstring_info.description else None - - -def _GetArgsAndFlagsString(spec, metadata): - """The args and flags string for showing how to call a function. - - If positional arguments are accepted, the args will be shown as positional. - E.g. "ARG1 ARG2 [--flag=FLAG]" - - If positional arguments are disallowed, the args will be shown with flags - syntax. - E.g. "--arg1=ARG1 [--flag=FLAG]" - - Args: - spec: The full arg spec for the component to construct the args and flags - string for. - metadata: Metadata for the component, including whether it accepts - positional arguments. - - Returns: - The constructed args and flags string. - """ - args_with_no_defaults = spec.args[:len(spec.args) - len(spec.defaults)] - args_with_defaults = spec.args[len(spec.args) - len(spec.defaults):] - - # Check if positional args are allowed. If not, require flag syntax for args. - accepts_positional_args = metadata.get(decorators.ACCEPTS_POSITIONAL_ARGS) - - arg_and_flag_strings = [] - if args_with_no_defaults: - if accepts_positional_args: - arg_strings = [formatting.Underline(arg.upper()) - for arg in args_with_no_defaults] - else: - arg_strings = [ - f'--{arg}={formatting.Underline(arg.upper())}' - for arg in args_with_no_defaults - ] - arg_and_flag_strings.extend(arg_strings) - - # If there are any arguments that are treated as flags: - if args_with_defaults or spec.kwonlyargs or spec.varkw: - arg_and_flag_strings.append('') - - if spec.varargs: - varargs_underlined = formatting.Underline(spec.varargs.upper()) - varargs_string = f'[{varargs_underlined}]...' - arg_and_flag_strings.append(varargs_string) - - return ' '.join(arg_and_flag_strings) - - -def _GetPossibleActions(actions_grouped_by_kind): - """The list of possible action kinds.""" - possible_actions = [] - for action_group in actions_grouped_by_kind: - if action_group.members: - possible_actions.append(action_group.name) - return possible_actions - - -def _GetPossibleActionsString(possible_actions): - """A help screen string listing the possible action kinds available.""" - return ' | '.join(formatting.Underline(action.upper()) - for action in possible_actions) - - -def _GetActionsGroupedByKind(component, verbose=False): - """Gets lists of available actions, grouped by action kind.""" - groups = ActionGroup(name='group', plural='groups') - commands = ActionGroup(name='command', plural='commands') - values = ActionGroup(name='value', plural='values') - indexes = ActionGroup(name='index', plural='indexes') - - members = completion.VisibleMembers(component, verbose=verbose) - for member_name, member in members: - member_name = str(member_name) - if value_types.IsGroup(member): - groups.Add(name=member_name, member=member) - if value_types.IsCommand(member): - commands.Add(name=member_name, member=member) - if value_types.IsValue(member): - values.Add(name=member_name, member=member) - - if isinstance(component, (list, tuple)) and component: - component_len = len(component) - if component_len < 10: - indexes.Add(name=', '.join(str(x) for x in range(component_len))) - else: - indexes.Add(name=f'0..{component_len-1}') - - return [groups, commands, values, indexes] - - -def _GetCurrentCommand(trace=None, include_separators=True): - """Returns current command for the purpose of generating help text.""" - if trace: - current_command = trace.GetCommand(include_separators=include_separators) - else: - current_command = '' - return current_command - - -def _CreateOutputSection(name: str, content: str) -> str: - return f"""{formatting.Bold(name)} -{formatting.Indent(content, SECTION_INDENTATION)}""" - - -def _CreateArgItem(arg, docstring_info, spec): - """Returns a string describing a positional argument. - - Args: - arg: The name of the positional argument. - docstring_info: A docstrings.DocstringInfo namedtuple with information about - the containing function's docstring. - spec: An instance of fire.inspectutils.FullArgSpec, containing type and - default information about the arguments to a callable. - - Returns: - A string to be used in constructing the help screen for the function. - """ - - # The help string is indented, so calculate the maximum permitted length - # before indentation to avoid exceeding the maximum line length. - max_str_length = LINE_LENGTH - SECTION_INDENTATION - SUBSECTION_INDENTATION - - description = _GetArgDescription(arg, docstring_info) - - arg_string = formatting.BoldUnderline(arg.upper()) - - arg_type = _GetArgType(arg, spec) - arg_type = f'Type: {arg_type}' if arg_type else '' - available_space = max_str_length - len(arg_type) - arg_type = ( - formatting.EllipsisTruncate(arg_type, available_space, max_str_length)) - - description = '\n'.join(part for part in (arg_type, description) if part) - - return _CreateItem(arg_string, description, indent=SUBSECTION_INDENTATION) - - -def _CreateFlagItem(flag, docstring_info, spec, required=False, - flag_string=None, short_arg=False): - """Returns a string describing a flag using docstring and FullArgSpec info. - - Args: - flag: The name of the flag. - docstring_info: A docstrings.DocstringInfo namedtuple with information about - the containing function's docstring. - spec: An instance of fire.inspectutils.FullArgSpec, containing type and - default information about the arguments to a callable. - required: Whether the flag is required. - flag_string: If provided, use this string for the flag, rather than - constructing one from the flag name. - short_arg: Whether the flag has a short variation or not. - Returns: - A string to be used in constructing the help screen for the function. - """ - # pylint: disable=g-bad-todo - # TODO(MichaelCG8): Get type and default information from docstrings if it is - # not available in FullArgSpec. This will require updating - # fire.docstrings.parser(). - - # The help string is indented, so calculate the maximum permitted length - # before indentation to avoid exceeding the maximum line length. - max_str_length = LINE_LENGTH - SECTION_INDENTATION - SUBSECTION_INDENTATION - - description = _GetArgDescription(flag, docstring_info) - - if not flag_string: - flag_name_upper = formatting.Underline(flag.upper()) - flag_string = f'--{flag}={flag_name_upper}' - if required: - flag_string += ' (required)' - if short_arg: - short_flag = flag[0] - flag_string = f'-{short_flag}, {flag_string}' - - arg_type = _GetArgType(flag, spec) - arg_default = _GetArgDefault(flag, spec) - - # We need to handle the case where there is a default of None, but otherwise - # the argument has another type. - if arg_default == 'None': - arg_type = f'Optional[{arg_type}]' - - arg_type = f'Type: {arg_type}' if arg_type else '' - available_space = max_str_length - len(arg_type) - arg_type = ( - formatting.EllipsisTruncate(arg_type, available_space, max_str_length)) - - arg_default = f'Default: {arg_default}' if arg_default else '' - available_space = max_str_length - len(arg_default) - arg_default = ( - formatting.EllipsisTruncate(arg_default, available_space, max_str_length)) - - description = '\n'.join( - part for part in (arg_type, arg_default, description) if part - ) - - return _CreateItem(flag_string, description, indent=SUBSECTION_INDENTATION) - - -def _GetArgType(arg, spec): - """Returns a string describing the type of an argument. - - Args: - arg: The name of the argument. - spec: An instance of fire.inspectutils.FullArgSpec, containing type and - default information about the arguments to a callable. - Returns: - A string to be used in constructing the help screen for the function, the - empty string if the argument type is not available. - """ - if arg in spec.annotations: - arg_type = spec.annotations[arg] - try: - return arg_type.__qualname__ - except AttributeError: - # Some typing objects, such as typing.Union do not have either a __name__ - # or __qualname__ attribute. - # repr(typing.Union[int, str]) will return ': typing.Union[int, str]' - return repr(arg_type) - return '' - - -def _GetArgDefault(flag, spec): - """Returns a string describing a flag's default value. - - Args: - flag: The name of the flag. - spec: An instance of fire.inspectutils.FullArgSpec, containing type and - default information about the arguments to a callable. - Returns: - A string to be used in constructing the help screen for the function, the - empty string if the flag does not have a default or the default is not - available. - """ - num_defaults = len(spec.defaults) - args_with_defaults = spec.args[-num_defaults:] - - for arg, default in zip(args_with_defaults, spec.defaults): - if arg == flag: - return repr(default) - if flag in spec.kwonlydefaults: - return repr(spec.kwonlydefaults[flag]) - return '' - - -def _CreateItem(name, description, indent=2): - if not description: - return name - description = formatting.Indent(description, indent) - return f"""{name} -{description}""" - - -def _GetArgDescription(name, docstring_info): - if docstring_info.args: - for arg_in_docstring in docstring_info.args: - if arg_in_docstring.name in (name, f'*{name}', f'**{name}'): - return arg_in_docstring.description - return None - - -def _MakeUsageDetailsSection(action_group): - """Creates a usage details section for the provided action group.""" - item_strings = [] - for name, member in action_group.GetItems(): - info = inspectutils.Info(member) - item = name - docstring_info = info.get('docstring_info') - if (docstring_info - and not custom_descriptions.NeedsCustomDescription(member)): - summary = docstring_info.summary - elif custom_descriptions.NeedsCustomDescription(member): - summary = custom_descriptions.GetSummary( - member, LINE_LENGTH - SECTION_INDENTATION, LINE_LENGTH) - else: - summary = None - item = _CreateItem(name, summary) - item_strings.append(item) - return (action_group.plural.upper(), - _NewChoicesSection(action_group.name.upper(), item_strings)) - - -def _ValuesUsageDetailsSection(component, values): - """Creates a section tuple for the values section of the usage details.""" - value_item_strings = [] - for value_name, value in values.GetItems(): - del value - init_info = inspectutils.Info(component.__class__.__init__) - value_item = None - if 'docstring_info' in init_info: - init_docstring_info = init_info['docstring_info'] - if init_docstring_info.args: - for arg_info in init_docstring_info.args: - if arg_info.name == value_name: - value_item = _CreateItem(value_name, arg_info.description) - if value_item is None: - value_item = str(value_name) - value_item_strings.append(value_item) - return ('VALUES', _NewChoicesSection('VALUE', value_item_strings)) - - -def _NewChoicesSection(name, choices): - name_formatted = formatting.Bold(formatting.Underline(name)) - return _CreateItem( - f'{name_formatted} is one of the following:', - '\n' + '\n\n'.join(choices), - indent=1) - - -def UsageText(component, trace=None, verbose=False): - """Returns usage text for the given component. - - Args: - component: The component to determine the usage text for. - trace: The Fire trace object containing all metadata of current execution. - verbose: Whether to display the usage text in verbose mode. - - Returns: - String suitable for display in an error screen. - """ - # Get the command so far: - if trace: - command = trace.GetCommand() - needs_separating_hyphen_hyphen = trace.NeedsSeparatingHyphenHyphen() - else: - command = None - needs_separating_hyphen_hyphen = False - - if not command: - command = '' - - # Build the continuations for the command: - continued_command = command - - spec = inspectutils.GetFullArgSpec(component) - metadata = decorators.GetMetadata(component) - - # Usage for objects. - actions_grouped_by_kind = _GetActionsGroupedByKind(component, verbose=verbose) - possible_actions = _GetPossibleActions(actions_grouped_by_kind) - - continuations = [] - if possible_actions: - continuations.append(_GetPossibleActionsUsageString(possible_actions)) - - availability_lines = _UsageAvailabilityLines(actions_grouped_by_kind) - - if callable(component): - callable_items = _GetCallableUsageItems(spec, metadata) - if callable_items: - continuations.append(' '.join(callable_items)) - elif trace: - continuations.append(trace.separator) - availability_lines.extend(_GetCallableAvailabilityLines(spec)) - - if continuations: - continued_command += ' ' + ' | '.join(continuations) - help_command = ( - command - + (' -- ' if needs_separating_hyphen_hyphen else ' ') - + '--help' - ) - - return f"""Usage: {continued_command} -{''.join(availability_lines)} -For detailed information on this command, run: - {help_command}""" - - -def _GetPossibleActionsUsageString(possible_actions): - if possible_actions: - actions_str = '|'.join(possible_actions) - return f'<{actions_str}>' - return None - - -def _UsageAvailabilityLines(actions_grouped_by_kind): - availability_lines = [] - for action_group in actions_grouped_by_kind: - if action_group.members: - availability_line = _CreateAvailabilityLine( - header=f'available {action_group.plural}:', - items=action_group.names - ) - availability_lines.append(availability_line) - return availability_lines - - -def _GetCallableUsageItems(spec, metadata): - """A list of elements that comprise the usage summary for a callable.""" - args_with_no_defaults = spec.args[:len(spec.args) - len(spec.defaults)] - args_with_defaults = spec.args[len(spec.args) - len(spec.defaults):] - - # Check if positional args are allowed. If not, show flag syntax for args. - accepts_positional_args = metadata.get(decorators.ACCEPTS_POSITIONAL_ARGS) - - if not accepts_positional_args: - items = [f'--{arg}={arg.upper()}' - for arg in args_with_no_defaults] - else: - items = [arg.upper() for arg in args_with_no_defaults] - - # If there are any arguments that are treated as flags: - if args_with_defaults or spec.kwonlyargs or spec.varkw: - items.append('') - - if spec.varargs: - items.append(f'[{spec.varargs.upper()}]...') - - return items - - -def _KeywordOnlyArguments(spec, required=True): - return (flag for flag in spec.kwonlyargs - if required != (flag in spec.kwonlydefaults)) - - -def _GetCallableAvailabilityLines(spec): - """The list of availability lines for a callable for use in a usage string.""" - args_with_defaults = spec.args[len(spec.args) - len(spec.defaults):] - - # TODO(dbieber): Handle args_with_no_defaults if not accepts_positional_args. - optional_flags = [f'--{flag}' for flag in itertools.chain( - args_with_defaults, _KeywordOnlyArguments(spec, required=False))] - required_flags = [ - f'--{flag}' for flag in _KeywordOnlyArguments(spec, required=True) - ] - - # Flags section: - availability_lines = [] - if optional_flags: - availability_lines.append( - _CreateAvailabilityLine(header='optional flags:', items=optional_flags, - header_indent=2)) - if required_flags: - availability_lines.append( - _CreateAvailabilityLine(header='required flags:', items=required_flags, - header_indent=2)) - if spec.varkw: - additional_flags = ('additional flags are accepted' - if optional_flags or required_flags else - 'flags are accepted') - availability_lines.append( - _CreateAvailabilityLine(header=additional_flags, items=[], - header_indent=2)) - return availability_lines - - -def _CreateAvailabilityLine(header, items, - header_indent=2, items_indent=25, - line_length=LINE_LENGTH): - items_width = line_length - items_indent - items_text = '\n'.join(formatting.WrappedJoin(items, width=items_width)) - indented_items_text = formatting.Indent(items_text, spaces=items_indent) - indented_header = formatting.Indent(header, spaces=header_indent) - return indented_header + indented_items_text[len(indented_header):] + '\n' - - -class ActionGroup: - """A group of actions of the same kind.""" - - def __init__(self, name, plural): - self.name = name - self.plural = plural - self.names = [] - self.members = [] - - def Add(self, name, member=None): - self.names.append(name) - self.members.append(member) - - def GetItems(self): - return zip(self.names, self.members) diff --git a/fire/helptext_test.py b/fire/helptext_test.py deleted file mode 100644 index c7098fc4..00000000 --- a/fire/helptext_test.py +++ /dev/null @@ -1,596 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for the helptext module.""" - -import os -import textwrap - -from fire import formatting -from fire import helptext -from fire import test_components as tc -from fire import testutils -from fire import trace - - -class HelpTest(testutils.BaseTestCase): - - def setUp(self): - super().setUp() - os.environ['ANSI_COLORS_DISABLED'] = '1' - - def testHelpTextNoDefaults(self): - component = tc.NoDefaults - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, name='NoDefaults')) - self.assertIn('NAME\n NoDefaults', help_screen) - self.assertIn('SYNOPSIS\n NoDefaults', help_screen) - self.assertNotIn('DESCRIPTION', help_screen) - self.assertNotIn('NOTES', help_screen) - - def testHelpTextNoDefaultsObject(self): - component = tc.NoDefaults() - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, name='NoDefaults')) - self.assertIn('NAME\n NoDefaults', help_screen) - self.assertIn('SYNOPSIS\n NoDefaults COMMAND', help_screen) - self.assertNotIn('DESCRIPTION', help_screen) - self.assertIn('COMMANDS\n COMMAND is one of the following:', - help_screen) - self.assertIn('double', help_screen) - self.assertIn('triple', help_screen) - self.assertNotIn('NOTES', help_screen) - - def testHelpTextFunction(self): - component = tc.NoDefaults().double - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, name='double')) - self.assertIn('NAME\n double', help_screen) - self.assertIn('SYNOPSIS\n double COUNT', help_screen) - self.assertNotIn('DESCRIPTION', help_screen) - self.assertIn('POSITIONAL ARGUMENTS\n COUNT', help_screen) - self.assertIn( - 'NOTES\n You can also use flags syntax for POSITIONAL ARGUMENTS', - help_screen) - - def testHelpTextFunctionWithDefaults(self): - component = tc.WithDefaults().triple - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, name='triple')) - self.assertIn('NAME\n triple', help_screen) - self.assertIn('SYNOPSIS\n triple ', help_screen) - self.assertNotIn('DESCRIPTION', help_screen) - self.assertIn( - 'FLAGS\n -c, --count=COUNT\n Default: 0', - help_screen) - self.assertNotIn('NOTES', help_screen) - - def testHelpTextFunctionWithLongDefaults(self): - component = tc.WithDefaults().text - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, name='text')) - self.assertIn('NAME\n text', help_screen) - self.assertIn('SYNOPSIS\n text ', help_screen) - self.assertNotIn('DESCRIPTION', help_screen) - self.assertIn( - 'FLAGS\n -s, --string=STRING\n' - ' Default: \'0001020304050607080910' - '1112131415161718192021222324252627282...', - help_screen) - self.assertNotIn('NOTES', help_screen) - - def testHelpTextFunctionWithKwargs(self): - component = tc.fn_with_kwarg - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, name='text')) - self.assertIn('NAME\n text', help_screen) - self.assertIn('SYNOPSIS\n text ARG1 ARG2 ', help_screen) - self.assertIn('DESCRIPTION\n Function with kwarg', help_screen) - self.assertIn( - 'FLAGS\n --arg3\n Description of arg3.\n ' - 'Additional undocumented flags may also be accepted.', - help_screen) - - def testHelpTextFunctionWithKwargsAndDefaults(self): - component = tc.fn_with_kwarg_and_defaults - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, name='text')) - self.assertIn('NAME\n text', help_screen) - self.assertIn('SYNOPSIS\n text ARG1 ARG2 ', help_screen) - self.assertIn('DESCRIPTION\n Function with kwarg', help_screen) - self.assertIn( - 'FLAGS\n -o, --opt=OPT\n Default: True\n' - ' The following flags are also accepted.' - '\n --arg3\n Description of arg3.\n ' - 'Additional undocumented flags may also be accepted.', - help_screen) - - def testHelpTextFunctionWithDefaultsAndTypes(self): - component = ( - tc.py3.WithDefaultsAndTypes().double) - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, name='double')) - self.assertIn('NAME\n double', help_screen) - self.assertIn('SYNOPSIS\n double ', help_screen) - self.assertIn('DESCRIPTION', help_screen) - self.assertIn( - 'FLAGS\n -c, --count=COUNT\n Type: float\n Default: 0', - help_screen) - self.assertNotIn('NOTES', help_screen) - - def testHelpTextFunctionWithTypesAndDefaultNone(self): - component = ( - tc.py3.WithDefaultsAndTypes().get_int) - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, name='get_int')) - self.assertIn('NAME\n get_int', help_screen) - self.assertIn('SYNOPSIS\n get_int ', help_screen) - self.assertNotIn('DESCRIPTION', help_screen) - self.assertIn( - 'FLAGS\n -v, --value=VALUE\n' - ' Type: Optional[int]\n Default: None', - help_screen) - self.assertNotIn('NOTES', help_screen) - - def testHelpTextFunctionWithTypes(self): - component = tc.py3.WithTypes().double - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, name='double')) - self.assertIn('NAME\n double', help_screen) - self.assertIn('SYNOPSIS\n double COUNT', help_screen) - self.assertIn('DESCRIPTION', help_screen) - self.assertIn( - 'POSITIONAL ARGUMENTS\n COUNT\n Type: float', - help_screen) - self.assertIn( - 'NOTES\n You can also use flags syntax for POSITIONAL ARGUMENTS', - help_screen) - - def testHelpTextFunctionWithLongTypes(self): - component = tc.py3.WithTypes().long_type - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, name='long_type')) - self.assertIn('NAME\n long_type', help_screen) - self.assertIn('SYNOPSIS\n long_type LONG_OBJ', help_screen) - self.assertNotIn('DESCRIPTION', help_screen) - # TODO(dbieber): Assert type is displayed correctly. Type displayed - # differently in Travis vs in Google. - # self.assertIn( - # 'POSITIONAL ARGUMENTS\n LONG_OBJ\n' - # ' Type: typing.Tuple[typing.Tuple[' - # 'typing.Tuple[typing.Tuple[typing.Tupl...', - # help_screen) - self.assertIn( - 'NOTES\n You can also use flags syntax for POSITIONAL ARGUMENTS', - help_screen) - - def testHelpTextFunctionWithBuiltin(self): - component = 'test'.upper - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, 'upper')) - self.assertIn('NAME\n upper', help_screen) - self.assertIn('SYNOPSIS\n upper', help_screen) - # We don't check description content here since the content is python - # version dependent. - self.assertIn('DESCRIPTION\n', help_screen) - self.assertNotIn('NOTES', help_screen) - - def testHelpTextFunctionIntType(self): - component = int - help_screen = helptext.HelpText( - component=component, trace=trace.FireTrace(component, 'int')) - self.assertIn('NAME\n int', help_screen) - self.assertIn('SYNOPSIS\n int', help_screen) - # We don't check description content here since the content is python - # version dependent. - self.assertIn('DESCRIPTION\n', help_screen) - - def testHelpTextEmptyList(self): - component = [] - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, 'list')) - self.assertIn('NAME\n list', help_screen) - self.assertIn('SYNOPSIS\n list COMMAND', help_screen) - # TODO(zuhaochen): Change assertion after custom description is - # implemented for list type. - self.assertNotIn('DESCRIPTION', help_screen) - # We don't check the listed commands either since the list API could - # potentially change between Python versions. - self.assertIn('COMMANDS\n COMMAND is one of the following:\n', - help_screen) - - def testHelpTextShortList(self): - component = [10] - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, 'list')) - self.assertIn('NAME\n list', help_screen) - self.assertIn('SYNOPSIS\n list COMMAND', help_screen) - # TODO(zuhaochen): Change assertion after custom description is - # implemented for list type. - self.assertNotIn('DESCRIPTION', help_screen) - - # We don't check the listed commands comprehensively since the list API - # could potentially change between Python versions. Check a few - # functions(command) that we're confident likely remain available. - self.assertIn('COMMANDS\n COMMAND is one of the following:\n', - help_screen) - self.assertIn(' append\n', help_screen) - - def testHelpTextInt(self): - component = 7 - help_screen = helptext.HelpText( - component=component, trace=trace.FireTrace(component, '7')) - self.assertIn('NAME\n 7', help_screen) - self.assertIn('SYNOPSIS\n 7 COMMAND | VALUE', help_screen) - # TODO(zuhaochen): Change assertion after implementing custom - # description for int. - self.assertNotIn('DESCRIPTION', help_screen) - self.assertIn('COMMANDS\n COMMAND is one of the following:\n', - help_screen) - self.assertIn('VALUES\n VALUE is one of the following:\n', help_screen) - - def testHelpTextNoInit(self): - component = tc.OldStyleEmpty - help_screen = helptext.HelpText( - component=component, - trace=trace.FireTrace(component, 'OldStyleEmpty')) - self.assertIn('NAME\n OldStyleEmpty', help_screen) - self.assertIn('SYNOPSIS\n OldStyleEmpty', help_screen) - - def testHelpTextKeywordOnlyArgumentsWithDefault(self): - component = tc.py3.KeywordOnly.with_default - output = helptext.HelpText( - component=component, trace=trace.FireTrace(component, 'with_default')) - self.assertIn('NAME\n with_default', output) - self.assertIn('FLAGS\n -x, --x=X', output) - - def testHelpTextKeywordOnlyArgumentsWithoutDefault(self): - component = tc.py3.KeywordOnly.double - output = helptext.HelpText( - component=component, trace=trace.FireTrace(component, 'double')) - self.assertIn('NAME\n double', output) - self.assertIn('FLAGS\n -c, --count=COUNT (required)', output) - - def testHelpTextFunctionMixedDefaults(self): - component = tc.py3.HelpTextComponent().identity - t = trace.FireTrace(component, name='FunctionMixedDefaults') - output = helptext.HelpText(component, trace=t) - self.assertIn('NAME\n FunctionMixedDefaults', output) - self.assertIn('FunctionMixedDefaults ', output) - self.assertIn('--alpha=ALPHA (required)', output) - self.assertIn('--beta=BETA\n Default: \'0\'', output) - - def testHelpScreen(self): - component = tc.ClassWithDocstring() - t = trace.FireTrace(component, name='ClassWithDocstring') - help_output = helptext.HelpText(component, t) - expected_output = """ -NAME - ClassWithDocstring - Test class for testing help text output. - -SYNOPSIS - ClassWithDocstring COMMAND | VALUE - -DESCRIPTION - This is some detail description of this test class. - -COMMANDS - COMMAND is one of the following: - - print_msg - Prints a message. - -VALUES - VALUE is one of the following: - - message - The default message to print.""" - self.assertEqual(textwrap.dedent(expected_output).strip(), - help_output.strip()) - - def testHelpScreenForFunctionDocstringWithLineBreak(self): - component = tc.ClassWithMultilineDocstring.example_generator - t = trace.FireTrace(component, name='example_generator') - help_output = helptext.HelpText(component, t) - expected_output = """ - NAME - example_generator - Generators have a ``Yields`` section instead of a ``Returns`` section. - - SYNOPSIS - example_generator N - - DESCRIPTION - Generators have a ``Yields`` section instead of a ``Returns`` section. - - POSITIONAL ARGUMENTS - N - The upper limit of the range to generate, from 0 to `n` - 1. - - NOTES - You can also use flags syntax for POSITIONAL ARGUMENTS""" - self.assertEqual(textwrap.dedent(expected_output).strip(), - help_output.strip()) - - def testHelpScreenForFunctionFunctionWithDefaultArgs(self): - component = tc.WithDefaults().double - t = trace.FireTrace(component, name='double') - help_output = helptext.HelpText(component, t) - expected_output = """ - NAME - double - Returns the input multiplied by 2. - - SYNOPSIS - double - - DESCRIPTION - Returns the input multiplied by 2. - - FLAGS - -c, --count=COUNT - Default: 0 - Input number that you want to double.""" - self.assertEqual(textwrap.dedent(expected_output).strip(), - help_output.strip()) - - def testHelpTextUnderlineFlag(self): - component = tc.WithDefaults().triple - t = trace.FireTrace(component, name='triple') - help_screen = helptext.HelpText(component, t) - self.assertIn(formatting.Bold('NAME') + '\n triple', help_screen) - self.assertIn( - formatting.Bold('SYNOPSIS') + '\n triple ', - help_screen) - self.assertIn( - formatting.Bold('FLAGS') + '\n -c, --' + - formatting.Underline('count'), - help_screen) - - def testHelpTextBoldCommandName(self): - component = tc.ClassWithDocstring() - t = trace.FireTrace(component, name='ClassWithDocstring') - help_screen = helptext.HelpText(component, t) - self.assertIn( - formatting.Bold('NAME') + '\n ClassWithDocstring', help_screen) - self.assertIn(formatting.Bold('COMMANDS') + '\n', help_screen) - self.assertIn( - formatting.BoldUnderline('COMMAND') + ' is one of the following:\n', - help_screen) - self.assertIn(formatting.Bold('print_msg') + '\n', help_screen) - - def testHelpTextObjectWithGroupAndValues(self): - component = tc.TypedProperties() - t = trace.FireTrace(component, name='TypedProperties') - help_screen = helptext.HelpText( - component=component, trace=t, verbose=True) - print(help_screen) - self.assertIn('GROUPS', help_screen) - self.assertIn('GROUP is one of the following:', help_screen) - self.assertIn( - 'charlie\n Class with functions that have default arguments.', - help_screen) - self.assertIn('VALUES', help_screen) - self.assertIn('VALUE is one of the following:', help_screen) - self.assertIn('alpha', help_screen) - - def testHelpTextNameSectionCommandWithSeparator(self): - component = 9 - t = trace.FireTrace(component, name='int', separator='-') - t.AddSeparator() - help_screen = helptext.HelpText(component=component, trace=t, verbose=False) - self.assertIn('int -', help_screen) - self.assertNotIn('int - -', help_screen) - - def testHelpTextNameSectionCommandWithSeparatorVerbose(self): - component = tc.WithDefaults().double - t = trace.FireTrace(component, name='double', separator='-') - t.AddSeparator() - help_screen = helptext.HelpText(component=component, trace=t, verbose=True) - self.assertIn('double -', help_screen) - self.assertIn('double - -', help_screen) - - def testHelpTextMultipleKeywoardArgumentsWithShortArgs(self): - component = tc.fn_with_multiple_defaults - t = trace.FireTrace(component, name='shortargs') - help_screen = helptext.HelpText(component, t) - self.assertIn(formatting.Bold('NAME') + '\n shortargs', help_screen) - self.assertIn( - formatting.Bold('SYNOPSIS') + '\n shortargs ', - help_screen) - self.assertIn( - formatting.Bold('FLAGS') + '\n -f, --first', - help_screen) - self.assertIn('\n --last', help_screen) - self.assertIn('\n --late', help_screen) - - -class UsageTest(testutils.BaseTestCase): - - def testUsageOutput(self): - component = tc.NoDefaults() - t = trace.FireTrace(component, name='NoDefaults') - usage_output = helptext.UsageText(component, trace=t, verbose=False) - expected_output = """ - Usage: NoDefaults - available commands: double | triple - - For detailed information on this command, run: - NoDefaults --help""" - - self.assertEqual( - usage_output, - textwrap.dedent(expected_output).lstrip('\n')) - - def testUsageOutputVerbose(self): - component = tc.NoDefaults() - t = trace.FireTrace(component, name='NoDefaults') - usage_output = helptext.UsageText(component, trace=t, verbose=True) - expected_output = """ - Usage: NoDefaults - available commands: double | triple - - For detailed information on this command, run: - NoDefaults --help""" - self.assertEqual( - usage_output, - textwrap.dedent(expected_output).lstrip('\n')) - - def testUsageOutputMethod(self): - component = tc.NoDefaults().double - t = trace.FireTrace(component, name='NoDefaults') - t.AddAccessedProperty(component, 'double', ['double'], None, None) - usage_output = helptext.UsageText(component, trace=t, verbose=False) - expected_output = """ - Usage: NoDefaults double COUNT - - For detailed information on this command, run: - NoDefaults double --help""" - self.assertEqual( - usage_output, - textwrap.dedent(expected_output).lstrip('\n')) - - def testUsageOutputFunctionWithHelp(self): - component = tc.function_with_help - t = trace.FireTrace(component, name='function_with_help') - usage_output = helptext.UsageText(component, trace=t, verbose=False) - expected_output = """ - Usage: function_with_help - optional flags: --help - - For detailed information on this command, run: - function_with_help -- --help""" - self.assertEqual( - usage_output, - textwrap.dedent(expected_output).lstrip('\n')) - - def testUsageOutputFunctionWithDocstring(self): - component = tc.multiplier_with_docstring - t = trace.FireTrace(component, name='multiplier_with_docstring') - usage_output = helptext.UsageText(component, trace=t, verbose=False) - expected_output = """ - Usage: multiplier_with_docstring NUM - optional flags: --rate - - For detailed information on this command, run: - multiplier_with_docstring --help""" - self.assertEqual( - textwrap.dedent(expected_output).lstrip('\n'), - usage_output) - - def testUsageOutputFunctionMixedDefaults(self): - component = tc.py3.HelpTextComponent().identity - t = trace.FireTrace(component, name='FunctionMixedDefaults') - usage_output = helptext.UsageText(component, trace=t, verbose=False) - expected_output = """ - Usage: FunctionMixedDefaults - optional flags: --beta - required flags: --alpha - - For detailed information on this command, run: - FunctionMixedDefaults --help""" - expected_output = textwrap.dedent(expected_output).lstrip('\n') - self.assertEqual(expected_output, usage_output) - - def testUsageOutputCallable(self): - # This is both a group and a command. - component = tc.CallableWithKeywordArgument() - t = trace.FireTrace(component, name='CallableWithKeywordArgument', - separator='@') - usage_output = helptext.UsageText(component, trace=t, verbose=False) - expected_output = """ - Usage: CallableWithKeywordArgument | - available commands: print_msg - flags are accepted - - For detailed information on this command, run: - CallableWithKeywordArgument -- --help""" - self.assertEqual( - textwrap.dedent(expected_output).lstrip('\n'), - usage_output) - - def testUsageOutputConstructorWithParameter(self): - component = tc.InstanceVars - t = trace.FireTrace(component, name='InstanceVars') - usage_output = helptext.UsageText(component, trace=t, verbose=False) - expected_output = """ - Usage: InstanceVars --arg1=ARG1 --arg2=ARG2 - - For detailed information on this command, run: - InstanceVars --help""" - self.assertEqual( - textwrap.dedent(expected_output).lstrip('\n'), - usage_output) - - def testUsageOutputConstructorWithParameterVerbose(self): - component = tc.InstanceVars - t = trace.FireTrace(component, name='InstanceVars') - usage_output = helptext.UsageText(component, trace=t, verbose=True) - expected_output = """ - Usage: InstanceVars | --arg1=ARG1 --arg2=ARG2 - available commands: run - - For detailed information on this command, run: - InstanceVars --help""" - self.assertEqual( - textwrap.dedent(expected_output).lstrip('\n'), - usage_output) - - def testUsageOutputEmptyDict(self): - component = {} - t = trace.FireTrace(component, name='EmptyDict') - usage_output = helptext.UsageText(component, trace=t, verbose=True) - expected_output = """ - Usage: EmptyDict - - For detailed information on this command, run: - EmptyDict --help""" - self.assertEqual( - textwrap.dedent(expected_output).lstrip('\n'), - usage_output) - - def testUsageOutputNone(self): - component = None - t = trace.FireTrace(component, name='None') - usage_output = helptext.UsageText(component, trace=t, verbose=True) - expected_output = """ - Usage: None - - For detailed information on this command, run: - None --help""" - self.assertEqual( - textwrap.dedent(expected_output).lstrip('\n'), - usage_output) - - def testInitRequiresFlagSyntaxSubclassNamedTuple(self): - component = tc.SubPoint - t = trace.FireTrace(component, name='SubPoint') - usage_output = helptext.UsageText(component, trace=t, verbose=False) - expected_output = 'Usage: SubPoint --x=X --y=Y' - self.assertIn(expected_output, usage_output) - -if __name__ == '__main__': - testutils.main() diff --git a/fire/inspectutils.py b/fire/inspectutils.py deleted file mode 100644 index 17508e30..00000000 --- a/fire/inspectutils.py +++ /dev/null @@ -1,349 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Inspection utility functions for Python Fire.""" - -import inspect -import sys -import types - -from fire import docstrings - - -class FullArgSpec: - """The arguments of a function, as in Python 3's inspect.FullArgSpec.""" - - def __init__(self, args=None, varargs=None, varkw=None, defaults=None, - kwonlyargs=None, kwonlydefaults=None, annotations=None): - """Constructs a FullArgSpec with each provided attribute, or the default. - - Args: - args: A list of the argument names accepted by the function. - varargs: The name of the *varargs argument or None if there isn't one. - varkw: The name of the **kwargs argument or None if there isn't one. - defaults: A tuple of the defaults for the arguments that accept defaults. - kwonlyargs: A list of argument names that must be passed with a keyword. - kwonlydefaults: A dictionary of keyword only arguments and their defaults. - annotations: A dictionary of arguments and their annotated types. - """ - self.args = args or [] - self.varargs = varargs - self.varkw = varkw - self.defaults = defaults or () - self.kwonlyargs = kwonlyargs or [] - self.kwonlydefaults = kwonlydefaults or {} - self.annotations = annotations or {} - - -def _GetArgSpecInfo(fn): - """Gives information pertaining to computing the ArgSpec of fn. - - Determines if the first arg is supplied automatically when fn is called. - This arg will be supplied automatically if fn is a bound method or a class - with an __init__ method. - - Also returns the function who's ArgSpec should be used for determining the - calling parameters for fn. This may be different from fn itself if fn is a - class with an __init__ method. - - Args: - fn: The function or class of interest. - Returns: - A tuple with the following two items: - fn: The function to use for determining the arg spec of this function. - skip_arg: Whether the first argument will be supplied automatically, and - hence should be skipped when supplying args from a Fire command. - """ - skip_arg = False - if inspect.isclass(fn): - # If the function is a class, we try to use its init method. - skip_arg = True - elif inspect.ismethod(fn): - # If the function is a bound method, we skip the `self` argument. - skip_arg = fn.__self__ is not None - elif inspect.isbuiltin(fn): - # If the function is a bound builtin, we skip the `self` argument, unless - # the function is from a standard library module in which case its __self__ - # attribute is that module. - if not isinstance(fn.__self__, types.ModuleType): - skip_arg = True - elif not inspect.isfunction(fn): - # The purpose of this else clause is to set skip_arg for callable objects. - skip_arg = True - return fn, skip_arg - - -def Py3GetFullArgSpec(fn): - """A alternative to the builtin getfullargspec. - - The builtin inspect.getfullargspec uses: - `skip_bound_args=False, follow_wrapped_chains=False` - in order to be backwards compatible. - - This function instead skips bound args (self) and follows wrapped chains. - - Args: - fn: The function or class of interest. - Returns: - An inspect.FullArgSpec namedtuple with the full arg spec of the function. - """ - # pylint: disable=no-member - - try: - sig = inspect._signature_from_callable( # pylint: disable=protected-access # type: ignore - fn, - skip_bound_arg=True, - follow_wrapper_chains=True, - sigcls=inspect.Signature) - except Exception: - # 'signature' can raise ValueError (most common), AttributeError, and - # possibly others. We catch all exceptions here, and reraise a TypeError. - raise TypeError('Unsupported callable.') - - args = [] - varargs = None - varkw = None - kwonlyargs = [] - defaults = () - annotations = {} - defaults = () - kwdefaults = {} - - if sig.return_annotation is not sig.empty: - annotations['return'] = sig.return_annotation - - for param in sig.parameters.values(): - kind = param.kind - name = param.name - - # pylint: disable=protected-access - if kind is inspect._POSITIONAL_ONLY: # type: ignore - args.append(name) - elif kind is inspect._POSITIONAL_OR_KEYWORD: # type: ignore - args.append(name) - if param.default is not param.empty: - defaults += (param.default,) - elif kind is inspect._VAR_POSITIONAL: # type: ignore - varargs = name - elif kind is inspect._KEYWORD_ONLY: # type: ignore - kwonlyargs.append(name) - if param.default is not param.empty: - kwdefaults[name] = param.default - elif kind is inspect._VAR_KEYWORD: # type: ignore - varkw = name - if param.annotation is not param.empty: - annotations[name] = param.annotation - # pylint: enable=protected-access - - if not kwdefaults: - # compatibility with 'func.__kwdefaults__' - kwdefaults = None - - if not defaults: - # compatibility with 'func.__defaults__' - defaults = None - return inspect.FullArgSpec(args, varargs, varkw, defaults, - kwonlyargs, kwdefaults, annotations) - # pylint: enable=no-member - - -def GetFullArgSpec(fn): - """Returns a FullArgSpec describing the given callable.""" - original_fn = fn - fn, skip_arg = _GetArgSpecInfo(fn) - - try: - if sys.version_info[0:2] >= (3, 5): - (args, varargs, varkw, defaults, - kwonlyargs, kwonlydefaults, annotations) = Py3GetFullArgSpec(fn) - else: # Specifically Python 3.4. - (args, varargs, varkw, defaults, - kwonlyargs, kwonlydefaults, annotations) = inspect.getfullargspec(fn) # pylint: disable=deprecated-method,no-member - - except TypeError: - # If we can't get the argspec, how do we know if the fn should take args? - # 1. If it's a builtin, it can take args. - # 2. If it's an implicit __init__ function (a 'slot wrapper'), that comes - # from a namedtuple, use _fields to determine the args. - # 3. If it's another slot wrapper (that comes from not subclassing object in - # Python 2), then there are no args. - # Are there other cases? We just don't know. - - # Case 1: Builtins accept args. - if inspect.isbuiltin(fn): - # TODO(dbieber): Try parsing the docstring, if available. - # TODO(dbieber): Use known argspecs, like set.add and namedtuple.count. - return FullArgSpec(varargs='vars', varkw='kwargs') - - # Case 2: namedtuples store their args in their _fields attribute. - # TODO(dbieber): Determine if there's a way to detect false positives. - # In Python 2, a class that does not subclass anything, does not define - # __init__, and has an attribute named _fields will cause Fire to think it - # expects args for its constructor when in fact it does not. - fields = getattr(original_fn, '_fields', None) - if fields is not None: - return FullArgSpec(args=list(fields)) - - # Case 3: Other known slot wrappers do not accept args. - return FullArgSpec() - - # In Python 3.5+ Py3GetFullArgSpec uses skip_bound_arg=True already. - skip_arg_required = sys.version_info[0:2] == (3, 4) - if skip_arg_required and skip_arg and args: - args.pop(0) # Remove 'self' or 'cls' from the list of arguments. - return FullArgSpec(args, varargs, varkw, defaults, - kwonlyargs, kwonlydefaults, annotations) - - -def GetFileAndLine(component): - """Returns the filename and line number of component. - - Args: - component: A component to find the source information for, usually a class - or routine. - Returns: - filename: The name of the file where component is defined. - lineno: The line number where component is defined. - """ - if inspect.isbuiltin(component): - return None, None - - try: - filename = inspect.getsourcefile(component) - except TypeError: - return None, None - - try: - unused_code, lineindex = inspect.findsource(component) - lineno = lineindex + 1 - except (OSError, IndexError): - lineno = None - - return filename, lineno - - -def Info(component): - """Returns a dict with information about the given component. - - The dict will have at least some of the following fields. - type_name: The type of `component`. - string_form: A string representation of `component`. - file: The file in which `component` is defined. - line: The line number at which `component` is defined. - docstring: The docstring of `component`. - init_docstring: The init docstring of `component`. - class_docstring: The class docstring of `component`. - call_docstring: The call docstring of `component`. - length: The length of `component`. - - Args: - component: The component to analyze. - Returns: - A dict with information about the component. - """ - try: - from IPython.core import oinspect # pylint: disable=import-outside-toplevel,g-import-not-at-top - try: - inspector = oinspect.Inspector(theme_name="neutral") - except TypeError: # Only recent versions of IPython support theme_name. - inspector = oinspect.Inspector() # type: ignore - info = inspector.info(component) - - # IPython's oinspect.Inspector.info may return '' - if info['docstring'] == '': - info['docstring'] = None - except ImportError: - info = _InfoBackup(component) - - try: - unused_code, lineindex = inspect.findsource(component) - info['line'] = lineindex + 1 # type: ignore - except (TypeError, OSError): - info['line'] = None # type: ignore - - if 'docstring' in info: - info['docstring_info'] = docstrings.parse(info['docstring']) # type: ignore - - return info - - -def _InfoBackup(component): - """Returns a dict with information about the given component. - - This function is to be called only in the case that IPython's - oinspect module is not available. The info dict it produces may - contain less information that contained in the info dict produced - by oinspect. - - Args: - component: The component to analyze. - Returns: - A dict with information about the component. - """ - info = {} - - info['type_name'] = type(component).__name__ - info['string_form'] = str(component) - - filename, lineno = GetFileAndLine(component) - info['file'] = filename - info['line'] = lineno - info['docstring'] = inspect.getdoc(component) - - try: - info['length'] = str(len(component)) - except (TypeError, AttributeError): - pass - - return info - - -def IsNamedTuple(component): - """Return true if the component is a namedtuple. - - Unfortunately, Python offers no native way to check for a namedtuple type. - Instead, we need to use a simple hack which should suffice for our case. - namedtuples are internally implemented as tuples, therefore we need to: - 1. Check if the component is an instance of tuple. - 2. Check if the component has a _fields attribute which regular tuples do - not have. - - Args: - component: The component to analyze. - Returns: - True if the component is a namedtuple or False otherwise. - """ - if not isinstance(component, tuple): - return False - - has_fields = bool(getattr(component, '_fields', None)) - return has_fields - - -def GetClassAttrsDict(component): - """Gets the attributes of the component class, as a dict with name keys.""" - if not inspect.isclass(component): - return None - class_attrs_list = inspect.classify_class_attrs(component) - return { - class_attr.name: class_attr - for class_attr in class_attrs_list - } - - -def IsCoroutineFunction(fn): - try: - return inspect.iscoroutinefunction(fn) - except: # pylint: disable=bare-except - return False diff --git a/fire/inspectutils_test.py b/fire/inspectutils_test.py deleted file mode 100644 index 47de7e72..00000000 --- a/fire/inspectutils_test.py +++ /dev/null @@ -1,130 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for the inspectutils module.""" - -import os - -from fire import inspectutils -from fire import test_components as tc -from fire import testutils - - -class InspectUtilsTest(testutils.BaseTestCase): - - def testGetFullArgSpec(self): - spec = inspectutils.GetFullArgSpec(tc.identity) - self.assertEqual(spec.args, ['arg1', 'arg2', 'arg3', 'arg4']) - self.assertEqual(spec.defaults, (10, 20)) - self.assertEqual(spec.varargs, 'arg5') - self.assertEqual(spec.varkw, 'arg6') - self.assertEqual(spec.kwonlyargs, []) - self.assertEqual(spec.kwonlydefaults, {}) - self.assertEqual(spec.annotations, {'arg2': int, 'arg4': int}) - - def testGetFullArgSpecPy3(self): - spec = inspectutils.GetFullArgSpec(tc.py3.identity) - self.assertEqual(spec.args, ['arg1', 'arg2', 'arg3', 'arg4']) - self.assertEqual(spec.defaults, (10, 20)) - self.assertEqual(spec.varargs, 'arg5') - self.assertEqual(spec.varkw, 'arg10') - self.assertEqual(spec.kwonlyargs, ['arg6', 'arg7', 'arg8', 'arg9']) - self.assertEqual(spec.kwonlydefaults, {'arg8': 30, 'arg9': 40}) - self.assertEqual(spec.annotations, - {'arg2': int, 'arg4': int, 'arg7': int, 'arg9': int}) - - def testGetFullArgSpecFromBuiltin(self): - spec = inspectutils.GetFullArgSpec('test'.upper) - self.assertEqual(spec.args, []) - self.assertEqual(spec.defaults, ()) - self.assertEqual(spec.kwonlyargs, []) - self.assertEqual(spec.kwonlydefaults, {}) - self.assertEqual(spec.annotations, {}) - - def testGetFullArgSpecFromSlotWrapper(self): - spec = inspectutils.GetFullArgSpec(tc.NoDefaults) - self.assertEqual(spec.args, []) - self.assertEqual(spec.defaults, ()) - self.assertEqual(spec.varargs, None) - self.assertEqual(spec.varkw, None) - self.assertEqual(spec.kwonlyargs, []) - self.assertEqual(spec.kwonlydefaults, {}) - self.assertEqual(spec.annotations, {}) - - def testGetFullArgSpecFromNamedTuple(self): - spec = inspectutils.GetFullArgSpec(tc.NamedTuplePoint) - self.assertEqual(spec.args, ['x', 'y']) - self.assertEqual(spec.defaults, ()) - self.assertEqual(spec.varargs, None) - self.assertEqual(spec.varkw, None) - self.assertEqual(spec.kwonlyargs, []) - self.assertEqual(spec.kwonlydefaults, {}) - self.assertEqual(spec.annotations, {}) - - def testGetFullArgSpecFromNamedTupleSubclass(self): - spec = inspectutils.GetFullArgSpec(tc.SubPoint) - self.assertEqual(spec.args, ['x', 'y']) - self.assertEqual(spec.defaults, ()) - self.assertEqual(spec.varargs, None) - self.assertEqual(spec.varkw, None) - self.assertEqual(spec.kwonlyargs, []) - self.assertEqual(spec.kwonlydefaults, {}) - self.assertEqual(spec.annotations, {}) - - def testGetFullArgSpecFromClassNoInit(self): - spec = inspectutils.GetFullArgSpec(tc.OldStyleEmpty) - self.assertEqual(spec.args, []) - self.assertEqual(spec.defaults, ()) - self.assertEqual(spec.varargs, None) - self.assertEqual(spec.varkw, None) - self.assertEqual(spec.kwonlyargs, []) - self.assertEqual(spec.kwonlydefaults, {}) - self.assertEqual(spec.annotations, {}) - - def testGetFullArgSpecFromMethod(self): - spec = inspectutils.GetFullArgSpec(tc.NoDefaults().double) - self.assertEqual(spec.args, ['count']) - self.assertEqual(spec.defaults, ()) - self.assertEqual(spec.varargs, None) - self.assertEqual(spec.varkw, None) - self.assertEqual(spec.kwonlyargs, []) - self.assertEqual(spec.kwonlydefaults, {}) - self.assertEqual(spec.annotations, {}) - - def testInfoOne(self): - info = inspectutils.Info(1) - self.assertEqual(info.get('type_name'), 'int') - self.assertEqual(info.get('file'), None) - self.assertEqual(info.get('line'), None) - self.assertEqual(info.get('string_form'), '1') - - def testInfoClass(self): - info = inspectutils.Info(tc.NoDefaults) - self.assertEqual(info.get('type_name'), 'type') - self.assertIn(os.path.join('fire', 'test_components.py'), info.get('file')) - self.assertGreater(info.get('line'), 0) - - def testInfoClassNoInit(self): - info = inspectutils.Info(tc.OldStyleEmpty) - self.assertEqual(info.get('type_name'), 'type') - self.assertIn(os.path.join('fire', 'test_components.py'), info.get('file')) - self.assertGreater(info.get('line'), 0) - - def testInfoNoDocstring(self): - info = inspectutils.Info(tc.NoDefaults) - self.assertEqual(info['docstring'], None, 'Docstring should be None') - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/interact.py b/fire/interact.py deleted file mode 100644 index eccd3990..00000000 --- a/fire/interact.py +++ /dev/null @@ -1,96 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""This module enables interactive mode in Python Fire. - -It uses IPython as an optional dependency. When IPython is installed, the -interactive flag will use IPython's REPL. When IPython is not installed, the -interactive flag will start a Python REPL with the builtin `code` module's -InteractiveConsole class. -""" - -import inspect - - -def Embed(variables, verbose=False): - """Drops into a Python REPL with variables available as local variables. - - Args: - variables: A dict of variables to make available. Keys are variable names. - Values are variable values. - verbose: Whether to include 'hidden' members, those keys starting with _. - """ - print(_AvailableString(variables, verbose)) - - try: - _EmbedIPython(variables) - except ImportError: - _EmbedCode(variables) - - -def _AvailableString(variables, verbose=False): - """Returns a string describing what objects are available in the Python REPL. - - Args: - variables: A dict of the object to be available in the REPL. - verbose: Whether to include 'hidden' members, those keys starting with _. - Returns: - A string fit for printing at the start of the REPL, indicating what objects - are available for the user to use. - """ - modules = [] - other = [] - for name, value in variables.items(): - if not verbose and name.startswith('_'): - continue - if '-' in name or '/' in name: - continue - - if inspect.ismodule(value): - modules.append(name) - else: - other.append(name) - - lists = [ - ('Modules', modules), - ('Objects', other)] - list_strs = [] - for name, varlist in lists: - if varlist: - items_str = ', '.join(sorted(varlist)) - list_strs.append(f'{name}: {items_str}') - - lists_str = '\n'.join(list_strs) - return ( - 'Fire is starting a Python REPL with the following objects:\n' - f'{lists_str}\n' - ) - - -def _EmbedIPython(variables, argv=None): - """Drops into an IPython REPL with variables available for use. - - Args: - variables: A dict of variables to make available. Keys are variable names. - Values are variable values. - argv: The argv to use for starting ipython. Defaults to an empty list. - """ - import IPython # pylint: disable=import-outside-toplevel,g-import-not-at-top - argv = argv or [] - IPython.start_ipython(argv=argv, user_ns=variables) - - -def _EmbedCode(variables): - import code # pylint: disable=import-outside-toplevel,g-import-not-at-top - code.InteractiveConsole(variables).interact() diff --git a/fire/interact_test.py b/fire/interact_test.py deleted file mode 100644 index 2f286824..00000000 --- a/fire/interact_test.py +++ /dev/null @@ -1,48 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for the interact module.""" - -from unittest import mock - -from fire import interact -from fire import testutils - - -try: - import IPython # pylint: disable=unused-import, g-import-not-at-top - INTERACT_METHOD = 'IPython.start_ipython' -except ImportError: - INTERACT_METHOD = 'code.InteractiveConsole' - - -class InteractTest(testutils.BaseTestCase): - - @mock.patch(INTERACT_METHOD) - def testInteract(self, mock_interact_method): - self.assertFalse(mock_interact_method.called) - interact.Embed({}) - self.assertTrue(mock_interact_method.called) - - @mock.patch(INTERACT_METHOD) - def testInteractVariables(self, mock_interact_method): - self.assertFalse(mock_interact_method.called) - interact.Embed({ - 'count': 10, - 'mock': mock, - }) - self.assertTrue(mock_interact_method.called) - -if __name__ == '__main__': - testutils.main() diff --git a/fire/main_test.py b/fire/main_test.py deleted file mode 100644 index 9e1c382b..00000000 --- a/fire/main_test.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Test using Fire via `python -m fire`.""" - -import os -import tempfile - -from fire import __main__ -from fire import testutils - - -class MainModuleTest(testutils.BaseTestCase): - """Tests to verify the behavior of __main__ (python -m fire).""" - - def testNameSetting(self): - # Confirm one of the usage lines has the gettempdir member. - with self.assertOutputMatches('gettempdir'): - __main__.main(['__main__.py', 'tempfile']) - - def testArgPassing(self): - expected = os.path.join('part1', 'part2', 'part3') - with self.assertOutputMatches('%s\n' % expected): - __main__.main( - ['__main__.py', 'os.path', 'join', 'part1', 'part2', 'part3']) - with self.assertOutputMatches('%s\n' % expected): - __main__.main( - ['__main__.py', 'os', 'path', '-', 'join', 'part1', 'part2', 'part3']) - - -class MainModuleFileTest(testutils.BaseTestCase): - """Tests to verify correct import behavior for file executables.""" - - def setUp(self): - super().setUp() - self.file = tempfile.NamedTemporaryFile(suffix='.py') # pylint: disable=consider-using-with - self.file.write(b'class Foo:\n def double(self, n):\n return 2 * n\n') - self.file.flush() - - self.file2 = tempfile.NamedTemporaryFile() # pylint: disable=consider-using-with - - def testFileNameFire(self): - # Confirm that the file is correctly imported and doubles the number. - with self.assertOutputMatches('4'): - __main__.main( - ['__main__.py', self.file.name, 'Foo', 'double', '--n', '2']) - - def testFileNameFailure(self): - # Confirm that an existing file without a .py suffix raises a ValueError. - with self.assertRaises(ValueError): - __main__.main( - ['__main__.py', self.file2.name, 'Foo', 'double', '--n', '2']) - - def testFileNameModuleDuplication(self): - # Confirm that a file that masks a module still loads the module. - with self.assertOutputMatches('gettempdir'): - dirname = os.path.dirname(self.file.name) - with testutils.ChangeDirectory(dirname): - with open('tempfile', 'w'): - __main__.main([ - '__main__.py', - 'tempfile', - ]) - - os.remove('tempfile') - - def testFileNameModuleFileFailure(self): - # Confirm that an invalid file that masks a non-existent module fails. - with self.assertRaisesRegex(ValueError, - r'Fire can only be called on \.py files\.'): # pylint: disable=line-too-long, - dirname = os.path.dirname(self.file.name) - with testutils.ChangeDirectory(dirname): - with open('foobar', 'w'): - __main__.main([ - '__main__.py', - 'foobar', - ]) - - os.remove('foobar') - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/parser.py b/fire/parser.py deleted file mode 100644 index a335cc2c..00000000 --- a/fire/parser.py +++ /dev/null @@ -1,132 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Provides parsing functionality used by Python Fire.""" - -import argparse -import ast -import sys - -if sys.version_info[0:2] < (3, 8): - _StrNode = ast.Str # type: ignore # pylint: disable=no-member # deprecated but needed for Python < 3.8 -else: - _StrNode = ast.Constant - - -def CreateParser(): - parser = argparse.ArgumentParser(add_help=False) - parser.add_argument('--verbose', '-v', action='store_true') - parser.add_argument('--interactive', '-i', action='store_true') - parser.add_argument('--separator', default='-') - parser.add_argument('--completion', nargs='?', const='bash', type=str) - parser.add_argument('--help', '-h', action='store_true') - parser.add_argument('--trace', '-t', action='store_true') - # TODO(dbieber): Consider allowing name to be passed as an argument. - return parser - - -def SeparateFlagArgs(args): - """Splits a list of args into those for Flags and those for Fire. - - If an isolated '--' arg is not present in the arg list, then all of the args - are for Fire. If there is an isolated '--', then the args after the final '--' - are flag args, and the rest of the args are fire args. - - Args: - args: The list of arguments received by the Fire command. - Returns: - A tuple with the Fire args (a list), followed by the Flag args (a list). - """ - if '--' in args: - separator_index = len(args) - 1 - args[::-1].index('--') # index of last -- - flag_args = args[separator_index + 1:] - args = args[:separator_index] - return args, flag_args - return args, [] - - -def DefaultParseValue(value): - """The default argument parsing function used by Fire CLIs. - - If the value is made of only Python literals and containers, then the value - is parsed as it's Python value. Otherwise, provided the value contains no - quote, escape, or parenthetical characters, the value is treated as a string. - - Args: - value: A string from the command line to be parsed for use in a Fire CLI. - Returns: - The parsed value, of the type determined most appropriate. - """ - # Note: _LiteralEval will treat '#' as the start of a comment. - try: - return _LiteralEval(value) - except (SyntaxError, ValueError): - # If _LiteralEval can't parse the value, treat it as a string. - return value - - -def _LiteralEval(value): - """Parse value as a Python literal, or container of containers and literals. - - First the AST of the value is updated so that bare-words are turned into - strings. Then the resulting AST is evaluated as a literal or container of - only containers and literals. - - This allows for the YAML-like syntax {a: b} to represent the dict {'a': 'b'} - - Args: - value: A string to be parsed as a literal or container of containers and - literals. - Returns: - The Python value representing the value arg. - Raises: - ValueError: If the value is not an expression with only containers and - literals. - SyntaxError: If the value string has a syntax error. - """ - root = ast.parse(value, mode='eval') - if isinstance(root.body, ast.BinOp): - raise ValueError(value) - - for node in ast.walk(root): - for field, child in ast.iter_fields(node): - if isinstance(child, list): - for index, subchild in enumerate(child): - if isinstance(subchild, ast.Name): - child[index] = _Replacement(subchild) - - elif isinstance(child, ast.Name): - replacement = _Replacement(child) - setattr(node, field, replacement) - - # ast.literal_eval supports the following types: - # strings, bytes, numbers, tuples, lists, dicts, sets, booleans, and None - # (bytes and set literals only starting with Python 3.2) - return ast.literal_eval(root) - - -def _Replacement(node): - """Returns a node to use in place of the supplied node in the AST. - - Args: - node: A node of type Name. Could be a variable, or builtin constant. - Returns: - A node to use in place of the supplied Node. Either the same node, or a - String node whose value matches the Name node's id. - """ - value = node.id - # These are the only builtin constants supported by literal_eval. - if value in ('True', 'False', 'None'): - return node - return _StrNode(value) diff --git a/fire/parser_fuzz_test.py b/fire/parser_fuzz_test.py deleted file mode 100644 index 10f497cf..00000000 --- a/fire/parser_fuzz_test.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Fuzz tests for the parser module.""" - -from fire import parser -from fire import testutils -from hypothesis import example -from hypothesis import given -from hypothesis import settings -from hypothesis import strategies as st -import Levenshtein - - -class ParserFuzzTest(testutils.BaseTestCase): - - @settings(max_examples=10000) - @given(st.text(min_size=1)) - @example('True') - @example(r'"test\t\t\a\\a"') - @example(r' "test\t\t\a\\a" ') - @example('"(1, 2)"') - @example('(1, 2)') - @example('(1, 2)') - @example('(1, 2) ') - @example('a,b,c,d') - @example('(a,b,c,d)') - @example('[a,b,c,d]') - @example('{a,b,c,d}') - @example('test:(a,b,c,d)') - @example('{test:(a,b,c,d)}') - @example('{test:a,b,c,d}') - @example('{test:a,b:(c,d)}') # Note: Edit distance may be high for dicts. - @example('0,') - @example('#') - @example('A#00000') # Note: '#'' is treated as a comment. - @example('\x80') # Note: Causes UnicodeDecodeError. - @example(100 * '[' + '0') # Note: Causes MemoryError. - @example('\r\r\r\r1\r\r') - def testDefaultParseValueFuzz(self, value): - try: - result = parser.DefaultParseValue(value) - except TypeError: - # It's OK to get a TypeError if the string has the null character. - if '\x00' in value: - return - raise - except MemoryError: - if len(value) > 100: - # This is not what we're testing. - return - raise - - try: - uvalue = str(value) - uresult = str(result) - except UnicodeDecodeError: - # This is not what we're testing. - return - - # Check that the parsed value doesn't differ too much from the input. - distance = Levenshtein.distance(uresult, uvalue) - max_distance = ( - 2 + # Quotes or parenthesis can be implicit. - sum(c.isspace() for c in value) + - value.count('"') + value.count("'") + - 3 * (value.count(',') + 1) + # 'a,' can expand to "'a', " - 3 * (value.count(':')) + # 'a:' can expand to "'a': " - 2 * value.count('\\')) - if '#' in value: - max_distance += len(value) - value.index('#') - - if not isinstance(result, str): - max_distance += value.count('0') # Leading 0s are stripped. - - # Note: We don't check distance for dicts since item order can be changed. - if '{' not in value: - self.assertLessEqual(distance, max_distance, - (distance, max_distance, uvalue, uresult)) - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/parser_test.py b/fire/parser_test.py deleted file mode 100644 index a404eea2..00000000 --- a/fire/parser_test.py +++ /dev/null @@ -1,140 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for the parser module.""" - -from fire import parser -from fire import testutils - - -class ParserTest(testutils.BaseTestCase): - - def testCreateParser(self): - self.assertIsNotNone(parser.CreateParser()) - - def testSeparateFlagArgs(self): - self.assertEqual(parser.SeparateFlagArgs([]), ([], [])) - self.assertEqual(parser.SeparateFlagArgs(['a', 'b']), (['a', 'b'], [])) - self.assertEqual(parser.SeparateFlagArgs(['a', 'b', '--']), - (['a', 'b'], [])) - self.assertEqual(parser.SeparateFlagArgs(['a', 'b', '--', 'c']), - (['a', 'b'], ['c'])) - self.assertEqual(parser.SeparateFlagArgs(['--']), - ([], [])) - self.assertEqual(parser.SeparateFlagArgs(['--', 'c', 'd']), - ([], ['c', 'd'])) - self.assertEqual(parser.SeparateFlagArgs(['a', 'b', '--', 'c', 'd']), - (['a', 'b'], ['c', 'd'])) - self.assertEqual(parser.SeparateFlagArgs(['a', 'b', '--', 'c', 'd', '--']), - (['a', 'b', '--', 'c', 'd'], [])) - self.assertEqual(parser.SeparateFlagArgs(['a', 'b', '--', 'c', '--', 'd']), - (['a', 'b', '--', 'c'], ['d'])) - - def testDefaultParseValueStrings(self): - self.assertEqual(parser.DefaultParseValue('hello'), 'hello') - self.assertEqual(parser.DefaultParseValue('path/file.jpg'), 'path/file.jpg') - self.assertEqual(parser.DefaultParseValue('hello world'), 'hello world') - self.assertEqual(parser.DefaultParseValue('--flag'), '--flag') - - def testDefaultParseValueQuotedStrings(self): - self.assertEqual(parser.DefaultParseValue("'hello'"), 'hello') - self.assertEqual(parser.DefaultParseValue("'hello world'"), 'hello world') - self.assertEqual(parser.DefaultParseValue("'--flag'"), '--flag') - self.assertEqual(parser.DefaultParseValue('"hello"'), 'hello') - self.assertEqual(parser.DefaultParseValue('"hello world"'), 'hello world') - self.assertEqual(parser.DefaultParseValue('"--flag"'), '--flag') - - def testDefaultParseValueSpecialStrings(self): - self.assertEqual(parser.DefaultParseValue('-'), '-') - self.assertEqual(parser.DefaultParseValue('--'), '--') - self.assertEqual(parser.DefaultParseValue('---'), '---') - self.assertEqual(parser.DefaultParseValue('----'), '----') - self.assertEqual(parser.DefaultParseValue('None'), None) - self.assertEqual(parser.DefaultParseValue("'None'"), 'None') - - def testDefaultParseValueNumbers(self): - self.assertEqual(parser.DefaultParseValue('23'), 23) - self.assertEqual(parser.DefaultParseValue('-23'), -23) - self.assertEqual(parser.DefaultParseValue('23.0'), 23.0) - self.assertIsInstance(parser.DefaultParseValue('23'), int) - self.assertIsInstance(parser.DefaultParseValue('23.0'), float) - self.assertEqual(parser.DefaultParseValue('23.5'), 23.5) - self.assertEqual(parser.DefaultParseValue('-23.5'), -23.5) - - def testDefaultParseValueStringNumbers(self): - self.assertEqual(parser.DefaultParseValue("'23'"), '23') - self.assertEqual(parser.DefaultParseValue("'23.0'"), '23.0') - self.assertEqual(parser.DefaultParseValue("'23.5'"), '23.5') - self.assertEqual(parser.DefaultParseValue('"23"'), '23') - self.assertEqual(parser.DefaultParseValue('"23.0"'), '23.0') - self.assertEqual(parser.DefaultParseValue('"23.5"'), '23.5') - - def testDefaultParseValueQuotedStringNumbers(self): - self.assertEqual(parser.DefaultParseValue('"\'123\'"'), "'123'") - - def testDefaultParseValueOtherNumbers(self): - self.assertEqual(parser.DefaultParseValue('1e5'), 100000.0) - - def testDefaultParseValueLists(self): - self.assertEqual(parser.DefaultParseValue('[1, 2, 3]'), [1, 2, 3]) - self.assertEqual(parser.DefaultParseValue('[1, "2", 3]'), [1, '2', 3]) - self.assertEqual(parser.DefaultParseValue('[1, \'"2"\', 3]'), [1, '"2"', 3]) - self.assertEqual(parser.DefaultParseValue( - '[1, "hello", 3]'), [1, 'hello', 3]) - - def testDefaultParseValueBareWordsLists(self): - self.assertEqual(parser.DefaultParseValue('[one, 2, "3"]'), ['one', 2, '3']) - - def testDefaultParseValueDict(self): - self.assertEqual( - parser.DefaultParseValue('{"abc": 5, "123": 1}'), {'abc': 5, '123': 1}) - - def testDefaultParseValueNone(self): - self.assertEqual(parser.DefaultParseValue('None'), None) - - def testDefaultParseValueBool(self): - self.assertEqual(parser.DefaultParseValue('True'), True) - self.assertEqual(parser.DefaultParseValue('False'), False) - - def testDefaultParseValueBareWordsTuple(self): - self.assertEqual(parser.DefaultParseValue('(one, 2, "3")'), ('one', 2, '3')) - self.assertEqual(parser.DefaultParseValue('one, "2", 3'), ('one', '2', 3)) - - def testDefaultParseValueNestedContainers(self): - self.assertEqual( - parser.DefaultParseValue( - '[(A, 2, "3"), 5, {alpha: 10.2, beta: "cat"}]'), - [('A', 2, '3'), 5, {'alpha': 10.2, 'beta': 'cat'}]) - - def testDefaultParseValueComments(self): - self.assertEqual(parser.DefaultParseValue('"0#comments"'), '0#comments') - # Comments are stripped. This behavior may change in the future. - self.assertEqual(parser.DefaultParseValue('0#comments'), 0) - - def testDefaultParseValueBadLiteral(self): - # If it can't be parsed, we treat it as a string. This behavior may change. - self.assertEqual( - parser.DefaultParseValue('[(A, 2, "3"), 5'), '[(A, 2, "3"), 5') - self.assertEqual(parser.DefaultParseValue('x=10'), 'x=10') - - def testDefaultParseValueSyntaxError(self): - # If it can't be parsed, we treat it as a string. - self.assertEqual(parser.DefaultParseValue('"'), '"') - - def testDefaultParseValueIgnoreBinOp(self): - self.assertEqual(parser.DefaultParseValue('2017-10-10'), '2017-10-10') - self.assertEqual(parser.DefaultParseValue('1+1'), '1+1') - -if __name__ == '__main__': - testutils.main() diff --git a/fire/test_components.py b/fire/test_components.py deleted file mode 100644 index 887a0dc6..00000000 --- a/fire/test_components.py +++ /dev/null @@ -1,568 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""This module has components that are used for testing Python Fire.""" - -import collections -import enum -import functools - -from fire import test_components_py3 as py3 # pylint: disable=unused-import,no-name-in-module,g-import-not-at-top - - -def identity(arg1, arg2, arg3=10, arg4=20, *arg5, **arg6): # pylint: disable=keyword-arg-before-vararg - return arg1, arg2, arg3, arg4, arg5, arg6 - -identity.__annotations__ = {'arg2': int, 'arg4': int} - - -def multiplier_with_docstring(num, rate=2): - """Multiplies num by rate. - - Args: - num (int): the num you want to multiply - rate (int): the rate for multiplication - Returns: - Multiplication of num by rate - """ - return num * rate - - -def function_with_help(help=True): # pylint: disable=redefined-builtin - return help - - -class Empty: - pass - - -class OldStyleEmpty: # pylint: disable=old-style-class,no-init - pass - - -class WithInit: - - def __init__(self): - pass - - -class ErrorInConstructor: - - def __init__(self, value='value'): - self.value = value - raise ValueError('Error in constructor') - - -class WithHelpArg: - """Test class for testing when class has a help= arg.""" - - def __init__(self, help=True): # pylint: disable=redefined-builtin - self.has_help = help - self.dictionary = {'__help': 'help in a dict'} - - -class NoDefaults: - - def double(self, count): - return 2 * count - - def triple(self, count): - return 3 * count - - -class WithDefaults: - """Class with functions that have default arguments.""" - - def double(self, count=0): - """Returns the input multiplied by 2. - - Args: - count: Input number that you want to double. - - Returns: - A number that is the double of count. - """ - return 2 * count - - def triple(self, count=0): - return 3 * count - - def text( - self, - string=('0001020304050607080910111213141516171819' - '2021222324252627282930313233343536373839') - ): - return string - - -class OldStyleWithDefaults: # pylint: disable=old-style-class,no-init - - def double(self, count=0): - return 2 * count - - def triple(self, count=0): - return 3 * count - - -class MixedDefaults: - - def ten(self): - return 10 - - def sum(self, alpha=0, beta=0): - return alpha + 2 * beta - - def identity(self, alpha, beta='0'): - return alpha, beta - - -class SimilarArgNames: - - def identity(self, bool_one=False, bool_two=False): - return bool_one, bool_two - - def identity2(self, a=None, alpha=None): - return a, alpha - - -class CapitalizedArgNames: - - def sum(self, Delta=1.0, Gamma=2.0): # pylint: disable=invalid-name - return Delta + Gamma - - -class Annotations: - - def double(self, count=0): - return 2 * count - - def triple(self, count=0): - return 3 * count - - double.__annotations__ = {'count': float} - triple.__annotations__ = {'count': float} - - -class TypedProperties: - """Test class for testing Python Fire with properties of various types.""" - - def __init__(self): - self.alpha = True - self.beta = (1, 2, 3) - self.charlie = WithDefaults() - self.delta = { - 'echo': 'E', - 'nest': { - 0: 'a', - 1: 'b', - }, - } - self.echo = ['alex', 'bethany'] - self.fox = ('carry', 'divide') - self.gamma = 'myexcitingstring' - - -class VarArgs: - """Test class for testing Python Fire with a property with varargs.""" - - def cumsums(self, *items): - total = None - sums = [] - for item in items: - if total is None: - total = item - else: - total += item - sums.append(total) - return sums - - def varchars(self, alpha=0, beta=0, *chars): # pylint: disable=keyword-arg-before-vararg - return alpha, beta, ''.join(chars) - - -class Underscores: - - def __init__(self): - self.underscore_example = 'fish fingers' - - def underscore_function(self, underscore_arg): - return underscore_arg - - -class BoolConverter: - - def as_bool(self, arg=False): - return bool(arg) - - -class ReturnsObj: - - def get_obj(self, *items): - del items # Unused - return BoolConverter() - - -class NumberDefaults: - - def reciprocal(self, divisor=10.0): - return 1.0 / divisor - - def integer_reciprocal(self, divisor=10): - return 1.0 / divisor - - -class InstanceVars: - - def __init__(self, arg1, arg2): - self.arg1 = arg1 - self.arg2 = arg2 - - def run(self, arg1, arg2): - return (self.arg1, self.arg2, arg1, arg2) - - -class Kwargs: - - def props(self, **kwargs): - return kwargs - - def upper(self, **kwargs): - return ' '.join(sorted(kwargs.keys())).upper() - - def run(self, positional, named=None, **kwargs): - return (positional, named, kwargs) - - -class ErrorRaiser: - - def fail(self): - raise ValueError('This error is part of a test.') - - -class NonComparable: - - def __eq__(self, other): - raise ValueError('Instances of this class cannot be compared.') - - def __ne__(self, other): - raise ValueError('Instances of this class cannot be compared.') - - -class EmptyDictOutput: - - def totally_empty(self): - return {} - - def nothing_printable(self): - return {'__do_not_print_me': 1} - - -class CircularReference: - - def create(self): - x = {} - x['y'] = x - return x - - -class OrderedDictionary: - - def empty(self): - return collections.OrderedDict() - - def non_empty(self): - ordered_dict = collections.OrderedDict() - ordered_dict['A'] = 'A' - ordered_dict[2] = 2 - return ordered_dict - - -class NamedTuple: - """Functions returning named tuples used for testing.""" - - def point(self): - """Point example straight from Python docs.""" - # pylint: disable=invalid-name - Point = collections.namedtuple('Point', ['x', 'y']) - return Point(11, y=22) - - def matching_names(self): - """Field name equals value.""" - # pylint: disable=invalid-name - Point = collections.namedtuple('Point', ['x', 'y']) - return Point(x='x', y='y') - - -class CallableWithPositionalArgs: - """Test class for supporting callable.""" - - TEST = 1 - - def __call__(self, x, y): - return x + y - - def fn(self, x): - return x + 1 - - -NamedTuplePoint = collections.namedtuple('NamedTuplePoint', ['x', 'y']) - - -class SubPoint(NamedTuplePoint): - """Used for verifying subclasses of namedtuples behave as intended.""" - - def coordinate_sum(self): - return self.x + self.y - - -class CallableWithKeywordArgument: - """Test class for supporting callable.""" - - def __call__(self, **kwargs): - for key, value in kwargs.items(): - print('{}: {}'.format(key, value)) - - def print_msg(self, msg): - print(msg) - - -CALLABLE_WITH_KEYWORD_ARGUMENT = CallableWithKeywordArgument() - - -class ClassWithDocstring: - """Test class for testing help text output. - - This is some detail description of this test class. - """ - - def __init__(self, message='Hello!'): - """Constructor of the test class. - - Constructs a new ClassWithDocstring object. - - Args: - message: The default message to print. - """ - self.message = message - - def print_msg(self, msg=None): - """Prints a message.""" - if msg is None: - msg = self.message - print(msg) - - -class ClassWithMultilineDocstring: - """Test class for testing help text output with multiline docstring. - - This is a test class that has a long docstring description that spans across - multiple lines for testing line breaking in help text. - """ - - @staticmethod - def example_generator(n): - """Generators have a ``Yields`` section instead of a ``Returns`` section. - - Args: - n (int): The upper limit of the range to generate, from 0 to `n` - 1. - - Yields: - int: The next number in the range of 0 to `n` - 1. - - Examples: - Examples should be written in doctest format, and should illustrate how - to use the function. - - >>> print([i for i in example_generator(4)]) - [0, 1, 2, 3] - - """ - yield from range(n) - - -def simple_set(): - return {1, 2, 'three'} - - -def simple_frozenset(): - return frozenset({1, 2, 'three'}) - - -class Subdict(dict): - """A subclass of dict, for testing purposes.""" - - -# An example subdict. -SUBDICT = Subdict({1: 2, 'red': 'blue'}) - - -class Color(enum.Enum): - RED = 1 - GREEN = 2 - BLUE = 3 - - -class HasStaticAndClassMethods: - """A class with a static method and a class method.""" - - CLASS_STATE = 1 - - def __init__(self, instance_state): - self.instance_state = instance_state - - @staticmethod - def static_fn(args): - return args - - @classmethod - def class_fn(cls, args): - return args + cls.CLASS_STATE - - -def function_with_varargs(arg1, arg2, arg3=1, *varargs): # pylint: disable=keyword-arg-before-vararg - """Function with varargs. - - Args: - arg1: Position arg docstring. - arg2: Position arg docstring. - arg3: Flags docstring. - *varargs: Accepts unlimited positional args. - Returns: - The unlimited positional args. - """ - del arg1, arg2, arg3 # Unused. - return varargs - - -def function_with_keyword_arguments(arg1, arg2=3, **kwargs): - del arg2 # Unused. - return arg1, kwargs - - -def fn_with_code_in_docstring(): - """This has code in the docstring. - - - - Example: - x = fn_with_code_in_docstring() - indentation_matters = True - - - - Returns: - True. - """ - return True - - -class BinaryCanvas: - """A canvas with which to make binary art, one bit at a time.""" - - def __init__(self, size=10): - self.pixels = [[0] * size for _ in range(size)] - self._size = size - self._row = 0 # The row of the cursor. - self._col = 0 # The column of the cursor. - - def __str__(self): - return '\n'.join( - ' '.join(str(pixel) for pixel in row) for row in self.pixels) - - def show(self): - print(self) - return self - - def move(self, row, col): - self._row = row % self._size - self._col = col % self._size - return self - - def on(self): - return self.set(1) - - def off(self): - return self.set(0) - - def set(self, value): - self.pixels[self._row][self._col] = value - return self - - -class DefaultMethod: - - def double(self, number): - return 2 * number - - def __getattr__(self, name): - def _missing(): - return 'Undefined function' - return _missing - - -class InvalidProperty: - - def double(self, number): - return 2 * number - - @property - def prop(self): - raise ValueError('test') - - -def simple_decorator(f): - @functools.wraps(f) - def wrapper(*args, **kwargs): - return f(*args, **kwargs) - return wrapper - - -@simple_decorator -def decorated_method(name='World'): - return 'Hello %s' % name - - -# pylint: disable=g-doc-args,g-doc-return-or-yield -def fn_with_kwarg(arg1, arg2, **kwargs): - """Function with kwarg. - - :param arg1: Description of arg1. - :param arg2: Description of arg2. - :key arg3: Description of arg3. - """ - del arg1, arg2 - return kwargs.get('arg3') - - -def fn_with_kwarg_and_defaults(arg1, arg2, opt=True, **kwargs): - """Function with kwarg and defaults. - - :param arg1: Description of arg1. - :param arg2: Description of arg2. - :key arg3: Description of arg3. - """ - del arg1, arg2, opt - return kwargs.get('arg3') - - -def fn_with_multiple_defaults(first='first', last='last', late='late'): - """Function with kwarg and defaults. - - :key first: Description of first. - :key last: Description of last. - :key late: Description of late. - """ - del last, late - return first -# pylint: enable=g-doc-args,g-doc-return-or-yield diff --git a/fire/test_components_bin.py b/fire/test_components_bin.py deleted file mode 100644 index 62afdf11..00000000 --- a/fire/test_components_bin.py +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Python Fire test components Fire CLI. - -This file is useful for replicating test results manually. -""" - -import fire -from fire import test_components - - -def main(): - fire.Fire(test_components) - -if __name__ == '__main__': - main() diff --git a/fire/test_components_py3.py b/fire/test_components_py3.py deleted file mode 100644 index 192302d3..00000000 --- a/fire/test_components_py3.py +++ /dev/null @@ -1,101 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""This module has components that use Python 3 specific syntax.""" - -import asyncio -import functools -from typing import Tuple - - -# pylint: disable=keyword-arg-before-vararg -def identity(arg1, arg2: int, arg3=10, arg4: int = 20, *arg5, - arg6, arg7: int, arg8=30, arg9: int = 40, **arg10): - return arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10 - - -class HelpTextComponent: - - def identity(self, *, alpha, beta='0'): - return alpha, beta - - -class KeywordOnly: - - def double(self, *, count): - return count * 2 - - def triple(self, *, count): - return count * 3 - - def with_default(self, *, x="x"): - print("x: " + x) - - -class LruCacheDecoratedMethod: - - @functools.lru_cache() - def lru_cache_in_class(self, arg1): - return arg1 - - -@functools.lru_cache() -def lru_cache_decorated(arg1): - return arg1 - - -class WithAsyncio: - - async def double(self, count=0): - return 2 * count - - -class WithTypes: - """Class with functions that have default arguments and types.""" - - def double(self, count: float) -> float: - """Returns the input multiplied by 2. - - Args: - count: Input number that you want to double. - - Returns: - A number that is the double of count. - """ - return 2 * count - - def long_type( - self, - long_obj: (Tuple[Tuple[Tuple[Tuple[Tuple[Tuple[Tuple[ - Tuple[Tuple[Tuple[Tuple[Tuple[int]]]]]]]]]]]]) - ): - return long_obj - - -class WithDefaultsAndTypes: - """Class with functions that have default arguments and types.""" - - def double(self, count: float = 0) -> float: - """Returns the input multiplied by 2. - - Args: - count: Input number that you want to double. - - Returns: - A number that is the double of count. - """ - return 2 * count - - def get_int(self, value: int = None): - return 0 if value is None else value diff --git a/fire/test_components_test.py b/fire/test_components_test.py deleted file mode 100644 index 531f882c..00000000 --- a/fire/test_components_test.py +++ /dev/null @@ -1,36 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for the test_components module.""" - -from fire import test_components as tc -from fire import testutils - - -class TestComponentsTest(testutils.BaseTestCase): - """Tests to verify that the test components are importable and okay.""" - - def testTestComponents(self): - self.assertIsNotNone(tc.Empty) - self.assertIsNotNone(tc.OldStyleEmpty) - - def testNonComparable(self): - with self.assertRaises(ValueError): - tc.NonComparable() != 2 # pylint: disable=expression-not-assigned - with self.assertRaises(ValueError): - tc.NonComparable() == 2 # pylint: disable=expression-not-assigned - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/testutils.py b/fire/testutils.py deleted file mode 100644 index eca37f43..00000000 --- a/fire/testutils.py +++ /dev/null @@ -1,112 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Utilities for Python Fire's tests.""" - -import contextlib -import io -import os -import re -import sys -import unittest -from unittest import mock - -from fire import core -from fire import trace - - -class BaseTestCase(unittest.TestCase): - """Shared test case for Python Fire tests.""" - - @contextlib.contextmanager - def assertOutputMatches(self, stdout='.*', stderr='.*', capture=True): - """Asserts that the context generates stdout and stderr matching regexps. - - Note: If wrapped code raises an exception, stdout and stderr will not be - checked. - - Args: - stdout: (str) regexp to match against stdout (None will check no stdout) - stderr: (str) regexp to match against stderr (None will check no stderr) - capture: (bool, default True) do not bubble up stdout or stderr - - Yields: - Yields to the wrapped context. - """ - stdout_fp = io.StringIO() - stderr_fp = io.StringIO() - try: - with mock.patch.object(sys, 'stdout', stdout_fp): - with mock.patch.object(sys, 'stderr', stderr_fp): - yield - finally: - if not capture: - sys.stdout.write(stdout_fp.getvalue()) - sys.stderr.write(stderr_fp.getvalue()) - - for name, regexp, fp in [('stdout', stdout, stdout_fp), - ('stderr', stderr, stderr_fp)]: - value = fp.getvalue() - if regexp is None: - if value: - raise AssertionError('%s: Expected no output. Got: %r' % - (name, value)) - else: - if not re.search(regexp, value, re.DOTALL | re.MULTILINE): - raise AssertionError('%s: Expected %r to match %r' % - (name, value, regexp)) - - @contextlib.contextmanager - def assertRaisesFireExit(self, code, regexp='.*'): - """Asserts that a FireExit error is raised in the context. - - Allows tests to check that Fire's wrapper around SystemExit is raised - and that a regexp is matched in the output. - - Args: - code: The status code that the FireExit should contain. - regexp: stdout must match this regex. - - Yields: - Yields to the wrapped context. - """ - with self.assertOutputMatches(stderr=regexp): - with self.assertRaises(core.FireExit): - try: - yield - except core.FireExit as exc: - if exc.code != code: - raise AssertionError('Incorrect exit code: %r != %r' % - (exc.code, code)) - self.assertIsInstance(exc.trace, trace.FireTrace) - raise - - -@contextlib.contextmanager -def ChangeDirectory(directory): - """Context manager to mock a directory change and revert on exit.""" - cwdir = os.getcwd() - os.chdir(directory) - - try: - yield directory - finally: - os.chdir(cwdir) - - -# pylint: disable=invalid-name -main = unittest.main -skip = unittest.skip -skipIf = unittest.skipIf -# pylint: enable=invalid-name diff --git a/fire/testutils_test.py b/fire/testutils_test.py deleted file mode 100644 index 4cfc0937..00000000 --- a/fire/testutils_test.py +++ /dev/null @@ -1,53 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Test the test utilities for Fire's tests.""" - -import sys - -from fire import testutils - - -class TestTestUtils(testutils.BaseTestCase): - """Let's get meta.""" - - def testNoCheckOnException(self): - with self.assertRaises(ValueError): - with self.assertOutputMatches(stdout='blah'): - raise ValueError() - - def testCheckStdoutOrStderrNone(self): - with self.assertRaisesRegex(AssertionError, 'stdout:'): - with self.assertOutputMatches(stdout=None): - print('blah') - - with self.assertRaisesRegex(AssertionError, 'stderr:'): - with self.assertOutputMatches(stderr=None): - print('blah', file=sys.stderr) - - with self.assertRaisesRegex(AssertionError, 'stderr:'): - with self.assertOutputMatches(stdout='apple', stderr=None): - print('apple') - print('blah', file=sys.stderr) - - def testCorrectOrderingOfAssertRaises(self): - # Check to make sure FireExit tests are correct. - with self.assertOutputMatches(stdout='Yep.*first.*second'): - with self.assertRaises(ValueError): - print('Yep, this is the first line.\nThis is the second.') - raise ValueError() - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/trace.py b/fire/trace.py deleted file mode 100644 index 601026fd..00000000 --- a/fire/trace.py +++ /dev/null @@ -1,306 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""This module has classes for tracing the execution of a Fire execution. - -A FireTrace consists of a sequence of FireTraceElement objects. Each element -represents an action taken by Fire during a single Fire execution. An action may -be instantiating a class, calling a routine, or accessing a property. - -Each action consumes args and results in a new component. The final component -is serialized to stdout by Fire as well as returned by the Fire method. If -a Fire usage error occurs, such as insufficient arguments being provided to call -a function, then that error will be captured in the trace and the final -component will be None. -""" - -import shlex - -from fire import inspectutils - -INITIAL_COMPONENT = 'Initial component' -INSTANTIATED_CLASS = 'Instantiated class' -CALLED_ROUTINE = 'Called routine' -CALLED_CALLABLE = 'Called callable' -ACCESSED_PROPERTY = 'Accessed property' -COMPLETION_SCRIPT = 'Generated completion script' -INTERACTIVE_MODE = 'Entered interactive mode' - - -class FireTrace: - """A FireTrace represents the steps taken during a single Fire execution. - - A FireTrace consists of a sequence of FireTraceElement objects. Each element - represents an action taken by Fire during a single Fire execution. An action - may be instantiating a class, calling a routine, or accessing a property. - """ - - def __init__(self, initial_component, name=None, separator='-', verbose=False, - show_help=False, show_trace=False): - initial_trace_element = FireTraceElement( - component=initial_component, - action=INITIAL_COMPONENT, - ) - - self.name = name - self.separator = separator - self.elements = [initial_trace_element] - self.verbose = verbose - self.show_help = show_help - self.show_trace = show_trace - - def GetResult(self): - """Returns the component from the last element of the trace.""" - return self.GetLastHealthyElement().component - - def GetLastHealthyElement(self): - """Returns the last element of the trace that is not an error. - - This element will contain the final component indicated by the trace. - - Returns: - The last element of the trace that is not an error. - """ - for element in reversed(self.elements): - if not element.HasError(): - return element - return self.elements[0] # The initial element is always healthy. - - def HasError(self): - """Returns whether the Fire execution encountered a Fire usage error.""" - return self.elements[-1].HasError() - - def AddAccessedProperty(self, component, target, args, filename, lineno): - element = FireTraceElement( - component=component, - action=ACCESSED_PROPERTY, - target=target, - args=args, - filename=filename, - lineno=lineno, - ) - self.elements.append(element) - - def AddCalledComponent(self, component, target, args, filename, lineno, - capacity, action=CALLED_CALLABLE): - """Adds an element to the trace indicating that a component was called. - - Also applies to instantiating a class. - - Args: - component: The result of calling the callable. - target: The name of the callable. - args: The args consumed in order to call this callable. - filename: The file in which the callable is defined, or None if N/A. - lineno: The line number on which the callable is defined, or None if N/A. - capacity: (bool) Whether the callable could have accepted additional args. - action: The value to include as the action in the FireTraceElement. - """ - element = FireTraceElement( - component=component, - action=action, - target=target, - args=args, - filename=filename, - lineno=lineno, - capacity=capacity, - ) - self.elements.append(element) - - def AddCompletionScript(self, script): - element = FireTraceElement( - component=script, - action=COMPLETION_SCRIPT, - ) - self.elements.append(element) - - def AddInteractiveMode(self): - element = FireTraceElement(action=INTERACTIVE_MODE) - self.elements.append(element) - - def AddError(self, error, args): - element = FireTraceElement(error=error, args=args) - self.elements.append(element) - - def AddSeparator(self): - """Marks that the most recent element of the trace used a separator. - - A separator is an argument you can pass to a Fire CLI to separate args left - of the separator from args right of the separator. - - Here's an example to demonstrate the separator. Let's say you have a - function that takes a variable number of args, and you want to call that - function, and then upper case the result. Here's how to do it: - - # in Python - def display(arg1, arg2='!'): - return arg1 + arg2 - - # from Bash (the default separator is the hyphen -) - display hello # hello! - display hello upper # helloupper - display hello - upper # HELLO! - - Note how the separator caused the display function to be called with the - default value for arg2. - """ - self.elements[-1].AddSeparator() - - def _Quote(self, arg): - if arg.startswith('--') and '=' in arg: - prefix, value = arg.split('=', 1) - return shlex.quote(prefix) + '=' + shlex.quote(value) - return shlex.quote(arg) - - def GetCommand(self, include_separators=True): - """Returns the command representing the trace up to this point. - - Args: - include_separators: Whether or not to include separators in the command. - - Returns: - A string representing a Fire CLI command that would produce this trace. - """ - args = [] - if self.name: - args.append(self.name) - - for element in self.elements: - if element.HasError(): - continue - if element.args: - args.extend(element.args) - if element.HasSeparator() and include_separators: - args.append(self.separator) - - if self.NeedsSeparator() and include_separators: - args.append(self.separator) - - return ' '.join(self._Quote(arg) for arg in args) - - def NeedsSeparator(self): - """Returns whether a separator should be added to the command. - - If the command is a function call, then adding an additional argument to the - command sometimes would add an extra arg to the function call, and sometimes - would add an arg acting on the result of the function call. - - This function tells us whether we should add a separator to the command - before adding additional arguments in order to make sure the arg is applied - to the result of the function call, and not the function call itself. - - Returns: - Whether a separator should be added to the command if order to keep the - component referred to by the command the same when adding additional args. - """ - element = self.GetLastHealthyElement() - return element.HasCapacity() and not element.HasSeparator() - - def __str__(self): - lines = [] - for index, element in enumerate(self.elements): - line = f'{index + 1}. {element}' - lines.append(line) - return '\n'.join(lines) - - def NeedsSeparatingHyphenHyphen(self, flag='help'): - """Returns whether a the trace need '--' before '--help'. - - '--' is needed when the component takes keyword arguments, when the value of - flag matches one of the argument of the component, or the component takes in - keyword-only arguments(e.g. argument with default value). - - Args: - flag: the flag available for the trace - - Returns: - True for needed '--', False otherwise. - - """ - element = self.GetLastHealthyElement() - component = element.component - spec = inspectutils.GetFullArgSpec(component) - return (spec.varkw is not None - or flag in spec.args - or flag in spec.kwonlyargs) - - -class FireTraceElement: - """A FireTraceElement represents a single step taken by a Fire execution. - - Examples of a FireTraceElement are the instantiation of a class or the - accessing of an object member. - """ - - def __init__(self, - component=None, - action=None, - target=None, - args=None, - filename=None, - lineno=None, - error=None, - capacity=None): - """Instantiates a FireTraceElement. - - Args: - component: The result of this element of the trace. - action: The type of action (e.g. instantiating a class) taking place. - target: (string) The name of the component being acted upon. - args: The args consumed by the represented action. - filename: The file in which the action is defined, or None if N/A. - lineno: The line number on which the action is defined, or None if N/A. - error: The error represented by the action, or None if N/A. - capacity: (bool) Whether the action could have accepted additional args. - """ - self.component = component - self._action = action - self._target = target - self.args = args - self._filename = filename - self._lineno = lineno - self._error = error - self._separator = False - self._capacity = capacity - - def HasError(self): - return self._error is not None - - def HasCapacity(self): - return self._capacity - - def HasSeparator(self): - return self._separator - - def AddSeparator(self): - self._separator = True - - def ErrorAsStr(self): - return ' '.join(str(arg) for arg in self._error.args) - - def __str__(self): - if self.HasError(): - return self.ErrorAsStr() - else: - # Format is: {action} "{target}" ({filename}:{lineno}) - string = self._action - if self._target is not None: - string += f' "{self._target}"' - if self._filename is not None: - path = self._filename - if self._lineno is not None: - path += f':{self._lineno}' - - string += f' ({path})' - return string diff --git a/fire/trace_test.py b/fire/trace_test.py deleted file mode 100644 index 1f858f5e..00000000 --- a/fire/trace_test.py +++ /dev/null @@ -1,150 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Tests for the trace module.""" - -from fire import testutils -from fire import trace - - -class FireTraceTest(testutils.BaseTestCase): - - def testFireTraceInitialization(self): - t = trace.FireTrace(10) - self.assertIsNotNone(t) - self.assertIsNotNone(t.elements) - - def testFireTraceGetResult(self): - t = trace.FireTrace('start') - self.assertEqual(t.GetResult(), 'start') - t.AddAccessedProperty('t', 'final', None, 'example.py', 10) - self.assertEqual(t.GetResult(), 't') - - def testFireTraceHasError(self): - t = trace.FireTrace('start') - self.assertFalse(t.HasError()) - t.AddAccessedProperty('t', 'final', None, 'example.py', 10) - self.assertFalse(t.HasError()) - t.AddError(ValueError('example error'), ['arg']) - self.assertTrue(t.HasError()) - - def testAddAccessedProperty(self): - t = trace.FireTrace('initial object') - args = ('example', 'args') - t.AddAccessedProperty('new component', 'prop', args, 'sample.py', 12) - self.assertEqual( - str(t), - '1. Initial component\n2. Accessed property "prop" (sample.py:12)') - - def testAddCalledCallable(self): - t = trace.FireTrace('initial object') - args = ('example', 'args') - t.AddCalledComponent('result', 'cell', args, 'sample.py', 10, False, - action=trace.CALLED_CALLABLE) - self.assertEqual( - str(t), - '1. Initial component\n2. Called callable "cell" (sample.py:10)') - - def testAddCalledRoutine(self): - t = trace.FireTrace('initial object') - args = ('example', 'args') - t.AddCalledComponent('result', 'run', args, 'sample.py', 12, False, - action=trace.CALLED_ROUTINE) - self.assertEqual( - str(t), - '1. Initial component\n2. Called routine "run" (sample.py:12)') - - def testAddInstantiatedClass(self): - t = trace.FireTrace('initial object') - args = ('example', 'args') - t.AddCalledComponent( - 'Classname', 'classname', args, 'sample.py', 12, False, - action=trace.INSTANTIATED_CLASS) - target = """1. Initial component -2. Instantiated class "classname" (sample.py:12)""" - self.assertEqual(str(t), target) - - def testAddCompletionScript(self): - t = trace.FireTrace('initial object') - t.AddCompletionScript('This is the completion script string.') - self.assertEqual( - str(t), - '1. Initial component\n2. Generated completion script') - - def testAddInteractiveMode(self): - t = trace.FireTrace('initial object') - t.AddInteractiveMode() - self.assertEqual( - str(t), - '1. Initial component\n2. Entered interactive mode') - - def testGetCommand(self): - t = trace.FireTrace('initial object') - args = ('example', 'args') - t.AddCalledComponent('result', 'run', args, 'sample.py', 12, False, - action=trace.CALLED_ROUTINE) - self.assertEqual(t.GetCommand(), 'example args') - - def testGetCommandWithQuotes(self): - t = trace.FireTrace('initial object') - args = ('example', 'spaced arg') - t.AddCalledComponent('result', 'run', args, 'sample.py', 12, False, - action=trace.CALLED_ROUTINE) - self.assertEqual(t.GetCommand(), "example 'spaced arg'") - - def testGetCommandWithFlagQuotes(self): - t = trace.FireTrace('initial object') - args = ('--example=spaced arg',) - t.AddCalledComponent('result', 'run', args, 'sample.py', 12, False, - action=trace.CALLED_ROUTINE) - self.assertEqual(t.GetCommand(), "--example='spaced arg'") - - -class FireTraceElementTest(testutils.BaseTestCase): - - def testFireTraceElementHasError(self): - el = trace.FireTraceElement() - self.assertFalse(el.HasError()) - - el = trace.FireTraceElement(error=ValueError('example error')) - self.assertTrue(el.HasError()) - - def testFireTraceElementAsStringNoMetadata(self): - el = trace.FireTraceElement( - component='Example', - action='Fake action', - ) - self.assertEqual(str(el), 'Fake action') - - def testFireTraceElementAsStringWithTarget(self): - el = trace.FireTraceElement( - component='Example', - action='Created toy', - target='Beaker', - ) - self.assertEqual(str(el), 'Created toy "Beaker"') - - def testFireTraceElementAsStringWithTargetAndLineNo(self): - el = trace.FireTraceElement( - component='Example', - action='Created toy', - target='Beaker', - filename='beaker.py', - lineno=10, - ) - self.assertEqual(str(el), 'Created toy "Beaker" (beaker.py:10)') - - -if __name__ == '__main__': - testutils.main() diff --git a/fire/value_types.py b/fire/value_types.py deleted file mode 100644 index 81308973..00000000 --- a/fire/value_types.py +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright (C) 2018 Google Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Types of values.""" - -import inspect - -from fire import inspectutils - - -VALUE_TYPES = (bool, str, bytes, int, float, complex, - type(Ellipsis), type(None), type(NotImplemented)) - - -def IsGroup(component): - # TODO(dbieber): Check if there are any subcomponents. - return not IsCommand(component) and not IsValue(component) - - -def IsCommand(component): - return inspect.isroutine(component) or inspect.isclass(component) - - -def IsValue(component): - return isinstance(component, VALUE_TYPES) or HasCustomStr(component) - - -def IsSimpleGroup(component): - """If a group is simple enough, then we treat it as a value in PrintResult. - - Only if a group contains all value types do we consider it simple enough to - print as a value. - - Args: - component: The group to check for value-group status. - Returns: - A boolean indicating if the group should be treated as a value for printing - purposes. - """ - assert isinstance(component, dict) - for unused_key, value in component.items(): - if not IsValue(value) and not isinstance(value, (list, dict)): - return False - return True - - -def HasCustomStr(component): - """Determines if a component has a custom __str__ method. - - Uses inspect.classify_class_attrs to determine the origin of the object's - __str__ method, if one is present. If it defined by `object` itself, then - it is not considered custom. Otherwise it is. This means that the __str__ - methods of primitives like ints and floats are considered custom. - - Objects with custom __str__ methods are treated as values and can be - serialized in places where more complex objects would have their help screen - shown instead. - - Args: - component: The object to check for a custom __str__ method. - Returns: - Whether `component` has a custom __str__ method. - """ - if hasattr(component, '__str__'): - class_attrs = inspectutils.GetClassAttrsDict(type(component)) or {} - str_attr = class_attrs.get('__str__') - if str_attr and str_attr.defining_class is not object: - return True - return False diff --git a/guide/index.html b/guide/index.html new file mode 100644 index 00000000..c8bc8626 --- /dev/null +++ b/guide/index.html @@ -0,0 +1,876 @@ + + + + + + + + The Python Fire Guide - Python Fire + + + + + + + + + + + + + +
+ + +
+ +
+
+
    +
  • + +
  • +
  • +
+
+
+
+
+ +

The Python Fire Guide

+

Introduction

+

Welcome to the Python Fire guide! Python Fire is a Python library that will turn +any Python component into a command line interface with just a single call to +Fire.

+

Let's get started!

+

Installation

+

To install Python Fire from pypi, run:

+

pip install fire

+

Alternatively, to install Python Fire from source, clone the source and run:

+

python setup.py install

+

Hello World

+
Version 1: fire.Fire()
+

The easiest way to use Fire is to take any Python program, and then simply call +fire.Fire() at the end of the program. This will expose the full contents of +the program to the command line.

+
import fire
+
+def hello(name):
+  return f'Hello {name}!'
+
+if __name__ == '__main__':
+  fire.Fire()
+
+

Here's how we can run our program from the command line:

+
$ python example.py hello World
+Hello World!
+
+
Version 2: fire.Fire(<fn>)
+

Let's modify our program slightly to only expose the hello function to the +command line.

+
import fire
+
+def hello(name):
+  return f'Hello {name}!'
+
+if __name__ == '__main__':
+  fire.Fire(hello)
+
+

Here's how we can run this from the command line:

+
$ python example.py World
+Hello World!
+
+

Notice we no longer have to specify to run the hello function, because we +called fire.Fire(hello).

+
Version 3: Using a main
+

We can alternatively write this program like this:

+
import fire
+
+def hello(name):
+  return f'Hello {name}!'
+
+def main():
+  fire.Fire(hello)
+
+if __name__ == '__main__':
+  main()
+
+

Or if we're using +entry points, +then simply this:

+
import fire
+
+def hello(name):
+  return f'Hello {name}!'
+
+def main():
+  fire.Fire(hello)
+
+
Version 4: Fire Without Code Changes
+

If you have a file example.py that doesn't even import fire:

+
def hello(name):
+  return f'Hello {name}!'
+
+

Then you can use it with Fire like this:

+
$ python -m fire example hello --name=World
+Hello World!
+
+

You can also specify the filepath of example.py rather than its module path, +like so:

+
$ python -m fire example.py hello --name=World
+Hello World!
+
+

Exposing Multiple Commands

+

In the previous example, we exposed a single function to the command line. Now +we'll look at ways of exposing multiple functions to the command line.

+
Version 1: fire.Fire()
+

The simplest way to expose multiple commands is to write multiple functions, and +then call Fire.

+
import fire
+
+def add(x, y):
+  return x + y
+
+def multiply(x, y):
+  return x * y
+
+if __name__ == '__main__':
+  fire.Fire()
+
+

We can use this like so:

+
$ python example.py add 10 20
+30
+$ python example.py multiply 10 20
+200
+
+

You'll notice that Fire correctly parsed 10 and 20 as numbers, rather than +as strings. Read more about argument parsing here.

+
Version 2: fire.Fire(<dict>)
+

In version 1 we exposed all the program's functionality to the command line. By +using a dict, we can selectively expose functions to the command line.

+
import fire
+
+def add(x, y):
+  return x + y
+
+def multiply(x, y):
+  return x * y
+
+if __name__ == '__main__':
+  fire.Fire({
+      'add': add,
+      'multiply': multiply,
+  })
+
+

We can use this in the same way as before:

+
$ python example.py add 10 20
+30
+$ python example.py multiply 10 20
+200
+
+
Version 3: fire.Fire(<object>)
+

Fire also works on objects, as in this variant. This is a good way to expose +multiple commands.

+
import fire
+
+class Calculator(object):
+
+  def add(self, x, y):
+    return x + y
+
+  def multiply(self, x, y):
+    return x * y
+
+if __name__ == '__main__':
+  calculator = Calculator()
+  fire.Fire(calculator)
+
+

We can use this in the same way as before:

+
$ python example.py add 10 20
+30
+$ python example.py multiply 10 20
+200
+
+
Version 4: fire.Fire(<class>)
+

Fire also works on classes. This is another good way to expose multiple +commands.

+
import fire
+
+class Calculator(object):
+
+  def add(self, x, y):
+    return x + y
+
+  def multiply(self, x, y):
+    return x * y
+
+if __name__ == '__main__':
+  fire.Fire(Calculator)
+
+

We can use this in the same way as before:

+
$ python example.py add 10 20
+30
+$ python example.py multiply 10 20
+200
+
+

Why might you prefer a class over an object? One reason is that you can pass +arguments for constructing the class too, as in this broken calculator example.

+
import fire
+
+class BrokenCalculator(object):
+
+  def __init__(self, offset=1):
+      self._offset = offset
+
+  def add(self, x, y):
+    return x + y + self._offset
+
+  def multiply(self, x, y):
+    return x * y + self._offset
+
+if __name__ == '__main__':
+  fire.Fire(BrokenCalculator)
+
+

When you use a broken calculator, you get wrong answers:

+
$ python example.py add 10 20
+31
+$ python example.py multiply 10 20
+201
+
+

But you can always fix it:

+
$ python example.py add 10 20 --offset=0
+30
+$ python example.py multiply 10 20 --offset=0
+200
+
+

Unlike calling ordinary functions, which can be done both with positional +arguments and named arguments (--flag syntax), arguments to __init__ +functions must be passed with the --flag syntax. See the section on +calling functions for more.

+

Grouping Commands

+

Here's an example of how you might make a command line interface with grouped +commands.

+
class IngestionStage(object):
+
+  def run(self):
+    return 'Ingesting! Nom nom nom...'
+
+class DigestionStage(object):
+
+  def run(self, volume=1):
+    return ' '.join(['Burp!'] * volume)
+
+  def status(self):
+    return 'Satiated.'
+
+class Pipeline(object):
+
+  def __init__(self):
+    self.ingestion = IngestionStage()
+    self.digestion = DigestionStage()
+
+  def run(self):
+    ingestion_output = self.ingestion.run()
+    digestion_output = self.digestion.run()
+    return [ingestion_output, digestion_output]
+
+if __name__ == '__main__':
+  fire.Fire(Pipeline)
+
+

Here's how this looks at the command line:

+
$ python example.py run
+Ingesting! Nom nom nom...
+Burp!
+$ python example.py ingestion run
+Ingesting! Nom nom nom...
+$ python example.py digestion run
+Burp!
+$ python example.py digestion status
+Satiated.
+
+

You can nest your commands in arbitrarily complex ways, if you're feeling grumpy +or adventurous.

+

Accessing Properties

+

In the examples we've looked at so far, our invocations of python example.py +have all run some function from the example program. In this example, we simply +access a property.

+
from airports import airports
+
+import fire
+
+class Airport(object):
+
+  def __init__(self, code):
+    self.code = code
+    self.name = dict(airports).get(self.code)
+    self.city = self.name.split(',')[0] if self.name else None
+
+if __name__ == '__main__':
+  fire.Fire(Airport)
+
+

Now we can use this program to learn about airport codes!

+
$ python example.py --code=JFK code
+JFK
+$ python example.py --code=SJC name
+San Jose-Sunnyvale-Santa Clara, CA - Norman Y. Mineta San Jose International (SJC)
+$ python example.py --code=ALB city
+Albany-Schenectady-Troy
+
+

By the way, you can find this +airports module here.

+

Chaining Function Calls

+

When you run a Fire CLI, you can take all the same actions on the result of +the call to Fire that you can take on the original object passed in.

+

For example, we can use our Airport CLI from the previous example like this:

+
$ python example.py --code=ALB city upper
+ALBANY-SCHENECTADY-TROY
+
+

This works since upper is a method on all strings.

+

So, if you want to set up your functions to chain nicely, all you have to do is +have a class whose methods return self. Here's an example.

+
import fire
+
+class BinaryCanvas(object):
+  """A canvas with which to make binary art, one bit at a time."""
+
+  def __init__(self, size=10):
+    self.pixels = [[0] * size for _ in range(size)]
+    self._size = size
+    self._row = 0  # The row of the cursor.
+    self._col = 0  # The column of the cursor.
+
+  def __str__(self):
+    return '\n'.join(' '.join(str(pixel) for pixel in row) for row in self.pixels)
+
+  def show(self):
+    print(self)
+    return self
+
+  def move(self, row, col):
+    self._row = row % self._size
+    self._col = col % self._size
+    return self
+
+  def on(self):
+    return self.set(1)
+
+  def off(self):
+    return self.set(0)
+
+  def set(self, value):
+    self.pixels[self._row][self._col] = value
+    return self
+
+if __name__ == '__main__':
+  fire.Fire(BinaryCanvas)
+
+

Now we can draw stuff :).

+
$ python example.py move 3 3 on move 3 6 on move 6 3 on move 6 6 on move 7 4 on move 7 5 on
+0 0 0 0 0 0 0 0 0 0
+0 0 0 0 0 0 0 0 0 0
+0 0 0 0 0 0 0 0 0 0
+0 0 0 1 0 0 1 0 0 0
+0 0 0 0 0 0 0 0 0 0
+0 0 0 0 0 0 0 0 0 0
+0 0 0 1 0 0 1 0 0 0
+0 0 0 0 1 1 0 0 0 0
+0 0 0 0 0 0 0 0 0 0
+0 0 0 0 0 0 0 0 0 0
+
+

It's supposed to be a smiley face.

+

Custom Serialization

+

You'll notice in the BinaryCanvas example, the canvas with the smiley face was +printed to the screen. You can determine how a component will be serialized by +defining its __str__ method.

+

If a custom __str__ method is present on the final component, the object is +serialized and printed. If there's no custom __str__ method, then the help +screen for the object is shown instead.

+

Can we make an even simpler example than Hello World?

+

Yes, this program is even simpler than our original Hello World example.

+
import fire
+english = 'Hello World'
+spanish = 'Hola Mundo'
+fire.Fire()
+
+

You can use it like this:

+
$ python example.py english
+Hello World
+$ python example.py spanish
+Hola Mundo
+
+

Calling Functions

+

Arguments to a constructor are passed by name using flag syntax --name=value.

+

For example, consider this simple class:

+
import fire
+
+class Building(object):
+
+  def __init__(self, name, stories=1):
+    self.name = name
+    self.stories = stories
+
+  def climb_stairs(self, stairs_per_story=10):
+    for story in range(self.stories):
+      for stair in range(1, stairs_per_story):
+        yield stair
+      yield 'Phew!'
+    yield 'Done!'
+
+if __name__ == '__main__':
+  fire.Fire(Building)
+
+

We can instantiate it as follows: python example.py --name="Sherrerd Hall"

+

Arguments to other functions may be passed positionally or by name using flag +syntax.

+

To instantiate a Building and then run the climb_stairs function, the +following commands are all valid:

+
$ python example.py --name="Sherrerd Hall" --stories=3 climb_stairs 10
+$ python example.py --name="Sherrerd Hall" climb_stairs --stairs_per_story=10
+$ python example.py --name="Sherrerd Hall" climb_stairs --stairs-per-story 10
+$ python example.py climb-stairs --stairs-per-story 10 --name="Sherrerd Hall"
+
+

You'll notice that hyphens and underscores (- and _) are interchangeable in +member names and flag names.

+

You'll also notice that the constructor's arguments can come after the +function's arguments or before the function.

+

You'll also notice that the equal sign between the flag name and its value is +optional.

+
Functions with *varargs and **kwargs
+

Fire supports functions that take *varargs or **kwargs. Here's an example:

+
import fire
+
+def order_by_length(*items):
+  """Orders items by length, breaking ties alphabetically."""
+  sorted_items = sorted(items, key=lambda item: (len(str(item)), str(item)))
+  return ' '.join(sorted_items)
+
+if __name__ == '__main__':
+  fire.Fire(order_by_length)
+
+

To use it, we run:

+
$ python example.py dog cat elephant
+cat dog elephant
+
+

You can use a separator to indicate that you're done providing arguments to a +function. All arguments after the separator will be used to process the result +of the function, rather than being passed to the function itself. The default +separator is the hyphen -.

+

Here's an example where we use a separator.

+
$ python example.py dog cat elephant - upper
+CAT DOG ELEPHANT
+
+

Without the separator, upper would have been treated as another argument.

+
$ python example.py dog cat elephant upper
+cat dog upper elephant
+
+

You can change the separator with the --separator flag. Flags are always +separated from your Fire command by an isolated --. Here's an example where we +change the separator.

+
$ python example.py dog cat elephant X upper -- --separator=X
+CAT DOG ELEPHANT
+
+

Separators can be useful when a function accepts *varargs, **kwargs, or +default values that you don't want to specify. It is also important to remember +to change the separator if you want to pass - as an argument.

+
Async Functions
+

Fire supports calling async functions too. Here's a simple example.

+
import asyncio
+
+async def count_to_ten():
+  for i in range(1, 11):
+    await asyncio.sleep(1)
+    print(i)
+
+if __name__ == '__main__':
+  fire.Fire(count_to_ten)
+
+

Whenever fire encounters a coroutine function, it runs it, blocking until it completes.

+

Argument Parsing

+

The types of the arguments are determined by their values, rather than by the +function signature where they're used. You can pass any Python literal from the +command line: numbers, strings, tuples, lists, dictionaries, (sets are only +supported in some versions of Python). You can also nest the collections +arbitrarily as long as they only contain literals.

+

To demonstrate this, we'll make a small example program that tells us the type +of any argument we give it:

+
import fire
+fire.Fire(lambda obj: type(obj).__name__)
+
+

And we'll use it like so:

+
$ python example.py 10
+int
+$ python example.py 10.0
+float
+$ python example.py hello
+str
+$ python example.py '(1,2)'
+tuple
+$ python example.py [1,2]
+list
+$ python example.py True
+bool
+$ python example.py {name:David}
+dict
+
+

You'll notice in that last example that bare-words are automatically replaced +with strings.

+

Be careful with your quotes! If you want to pass the string "10", rather than +the int 10, you'll need to either escape or quote your quotes. Otherwise Bash +will eat your quotes and pass an unquoted 10 to your Python program, where +Fire will interpret it as a number.

+
$ python example.py 10
+int
+$ python example.py "10"
+int
+$ python example.py '"10"'
+str
+$ python example.py "'10'"
+str
+$ python example.py \"10\"
+str
+
+

Be careful with your quotes! Remember that Bash processes your arguments first, +and then Fire parses the result of that. +If you wanted to pass the dict {"name": "David Bieber"} to your program, you +might try this:

+
$ python example.py '{"name": "David Bieber"}'  # Good! Do this.
+dict
+$ python example.py {"name":'"David Bieber"'}  # Okay.
+dict
+$ python example.py {"name":"David Bieber"}  # Wrong. This is parsed as a string.
+str
+$ python example.py {"name": "David Bieber"}  # Wrong. This isn't even treated as a single argument.
+<error>
+$ python example.py '{"name": "Justin Bieber"}'  # Wrong. This is not the Bieber you're looking for. (The syntax is fine though :))
+dict
+
+
Boolean Arguments
+

The tokens True and False are parsed as boolean values.

+

You may also specify booleans via flag syntax --name and --noname, which set +name to True and False respectively.

+

Continuing the previous example, we could run any of the following:

+
$ python example.py --obj=True
+bool
+$ python example.py --obj=False
+bool
+$ python example.py --obj
+bool
+$ python example.py --noobj
+bool
+
+

Be careful with boolean flags! If a token other than another flag immediately +follows a flag that's supposed to be a boolean, the flag will take on the value +of the token rather than the boolean value. You can resolve this: by putting a +separator after your last flag, by explicitly stating the value of the boolean +flag (as in --obj=True), or by making sure there's another flag after any +boolean flag argument.

+

Using Fire Flags

+

Fire CLIs all come with a number of flags. These flags should be separated from +the Fire command by an isolated --. If there is at least one isolated -- +argument, then arguments after the final isolated -- are treated as flags, +whereas all arguments before the final isolated -- are considered part of the +Fire command.

+

One useful flag is the --interactive flag. Use the --interactive flag on any +CLI to enter a Python REPL with all the modules and variables used in the +context where Fire was called already available to you for use. Other useful +variables, such as the result of the Fire command will also be available. Use +this feature like this: python example.py -- --interactive.

+

You can add the help flag to any command to see help and usage information. Fire +incorporates your docstrings into the help and usage information that it +generates. Fire will try to provide help even if you omit the isolated -- +separating the flags from the Fire command, but may not always be able to, since +help is a valid argument name. Use this feature like this: python +example.py -- --help or python example.py --help (or even python example.py +-h).

+

The complete set of flags available is shown below, in the reference section.

+

Reference

+ + + + + + + + + + + + + + + +
SetupCommandNotes
installpip install fire
+
Creating a CLI
+ + + + + + + + + + + + + + + + + + + + + + + + + +
Creating a CLICommandNotes
importimport fire
Callfire.Fire()Turns the current module into a Fire CLI.
Callfire.Fire(component)Turns component into a Fire CLI.
+
Flags
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Using a CLICommandNotes
Helpcommand -- --helpShow help and usage information for the command.
REPLcommand -- --interactiveEnter interactive mode.
Separatorcommand -- --separator=XThis sets the separator to X. The default separator is -.
Completioncommand -- --completion [shell]Generate a completion script for the CLI.
Tracecommand -- --traceGets a Fire trace for the command.
Verbosecommand -- --verboseInclude private members in the output.
+

Note that flags are separated from the Fire command by an isolated -- arg. +Help is an exception; the isolated -- is optional for getting help.

+
Arguments for Calling fire.Fire()
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ArgumentUsageNotes
componentfire.Fire(component)If omitted, defaults to a dict of all locals and globals.
commandfire.Fire(command='hello --name=5')Either a string or a list of arguments. If a string is provided, it is split to determine the arguments. If a list or tuple is provided, they are the arguments. If command is omitted, then sys.argv[1:] (the arguments from the command line) are used by default.
namefire.Fire(name='tool')The name of the CLI, ideally the name users will enter to run the CLI. This name will be used in the CLI's help screens. If the argument is omitted, it will be inferred automatically.
serializefire.Fire(serialize=custom_serializer)If omitted, simple types are serialized via their builtin str method, and any objects that define a custom __str__ method are serialized with that. If specified, all objects are serialized to text via the provided method.
+

Disclaimer

+

Python Fire is not an official Google product.

+ +
+
+ +
+
+ +
+ +
+ +
+ + + + « Previous + + + Next » + + +
+ + + + + + + + + diff --git a/img/favicon.ico b/img/favicon.ico new file mode 100644 index 00000000..e85006a3 Binary files /dev/null and b/img/favicon.ico differ diff --git a/index.html b/index.html new file mode 100644 index 00000000..dee351b6 --- /dev/null +++ b/index.html @@ -0,0 +1,311 @@ + + + + + + + + Python Fire + + + + + + + + + + + + + +
+ + +
+ +
+
+
    +
  • + +
  • +
  • +
+
+
+
+
+ +

Python Fire PyPI

+

Python Fire is a library for automatically generating command line interfaces +(CLIs) from absolutely any Python object.

+
    +
  • Python Fire is a simple way to create a CLI in Python. + [1]
  • +
  • Python Fire is a helpful tool for developing and debugging Python code. + [2]
  • +
  • Python Fire helps with exploring existing code or turning other people's + code into a CLI. [3]
  • +
  • Python Fire makes transitioning between Bash and Python easier. + [4]
  • +
  • Python Fire makes using a Python REPL easier by setting up the REPL with the + modules and variables you'll need already imported and created. + [5]
  • +
+

Installation

+

To install Python Fire with pip, run: pip install fire

+

To install Python Fire with conda, run: conda install fire -c conda-forge

+

To install Python Fire from source, first clone the repository and then run: +python setup.py install

+

Basic Usage

+

You can call Fire on any Python object:
+functions, classes, modules, objects, dictionaries, lists, tuples, etc. +They all work!

+

Here's an example of calling Fire on a function.

+
import fire
+
+def hello(name="World"):
+  return "Hello %s!" % name
+
+if __name__ == '__main__':
+  fire.Fire(hello)
+
+

Then, from the command line, you can run:

+
python hello.py  # Hello World!
+python hello.py --name=David  # Hello David!
+python hello.py --help  # Shows usage information.
+
+

Here's an example of calling Fire on a class.

+
import fire
+
+class Calculator(object):
+  """A simple calculator class."""
+
+  def double(self, number):
+    return 2 * number
+
+if __name__ == '__main__':
+  fire.Fire(Calculator)
+
+

Then, from the command line, you can run:

+
python calculator.py double 10  # 20
+python calculator.py double --number=15  # 30
+
+

To learn how Fire behaves on functions, objects, dicts, lists, etc, and to learn +about Fire's other features, see the Using a Fire CLI page.

+

For additional examples, see The Python Fire Guide.

+

Why is it called Fire?

+

When you call Fire, it fires off (executes) your command.

+

Where can I learn more?

+

Please see The Python Fire Guide.

+

Reference

+ + + + + + + + + + + + + + + +
SetupCommandNotes
installpip install fire
+ + + + + + + + + + + + + + + + + + + + + + + + + +
Creating a CLICommandNotes
importimport fire
Callfire.Fire()Turns the current module into a Fire CLI.
Callfire.Fire(component)Turns component into a Fire CLI.
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Using a CLICommandNotes
Helpcommand --help or command -- --help
REPLcommand -- --interactiveEnters interactive mode.
Separatorcommand -- --separator=XSets the separator to X. The default separator is -.
Completioncommand -- --completion [shell]Generates a completion script for the CLI.
Tracecommand -- --traceGets a Fire trace for the command.
Verbosecommand -- --verbose
+

Note that flags are separated from the Fire command by an isolated -- arg. +Help is an exception; the isolated -- is optional for getting help.

+

License

+

Licensed under the +Apache 2.0 License.

+

Disclaimer

+

This is not an official Google product.

+ +
+
+ +
+
+ +
+ +
+ +
+ + + + + Next » + + +
+ + + + + + + + + + + diff --git a/installation/index.html b/installation/index.html new file mode 100644 index 00000000..0cd0dc28 --- /dev/null +++ b/installation/index.html @@ -0,0 +1,147 @@ + + + + + + + + Installation - Python Fire + + + + + + + + + + + + + +
+ + +
+ +
+
+
    +
  • + +
  • +
  • +
+
+
+
+
+ +

Installation

+

To install Python Fire with pip, run: pip install fire

+

To install Python Fire with conda, run: conda install fire -c conda-forge

+

To install Python Fire from source, first clone the repository and then run +python setup.py install. To install from source for development, instead run python setup.py develop.

+ +
+
+ +
+
+ +
+ +
+ +
+ + + + « Previous + + + Next » + + +
+ + + + + + + + + diff --git a/js/html5shiv.min.js b/js/html5shiv.min.js new file mode 100644 index 00000000..1a01c94b --- /dev/null +++ b/js/html5shiv.min.js @@ -0,0 +1,4 @@ +/** +* @preserve HTML5 Shiv 3.7.3 | @afarkas @jdalton @jon_neal @rem | MIT/GPL2 Licensed +*/ +!function(a,b){function c(a,b){var c=a.createElement("p"),d=a.getElementsByTagName("head")[0]||a.documentElement;return c.innerHTML="x",d.insertBefore(c.lastChild,d.firstChild)}function d(){var a=t.elements;return"string"==typeof a?a.split(" "):a}function e(a,b){var c=t.elements;"string"!=typeof c&&(c=c.join(" ")),"string"!=typeof a&&(a=a.join(" ")),t.elements=c+" "+a,j(b)}function f(a){var b=s[a[q]];return b||(b={},r++,a[q]=r,s[r]=b),b}function g(a,c,d){if(c||(c=b),l)return c.createElement(a);d||(d=f(c));var e;return e=d.cache[a]?d.cache[a].cloneNode():p.test(a)?(d.cache[a]=d.createElem(a)).cloneNode():d.createElem(a),!e.canHaveChildren||o.test(a)||e.tagUrn?e:d.frag.appendChild(e)}function h(a,c){if(a||(a=b),l)return a.createDocumentFragment();c=c||f(a);for(var e=c.frag.cloneNode(),g=0,h=d(),i=h.length;i>g;g++)e.createElement(h[g]);return e}function i(a,b){b.cache||(b.cache={},b.createElem=a.createElement,b.createFrag=a.createDocumentFragment,b.frag=b.createFrag()),a.createElement=function(c){return t.shivMethods?g(c,a,b):b.createElem(c)},a.createDocumentFragment=Function("h,f","return function(){var n=f.cloneNode(),c=n.createElement;h.shivMethods&&("+d().join().replace(/[\w\-:]+/g,function(a){return b.createElem(a),b.frag.createElement(a),'c("'+a+'")'})+");return n}")(t,b.frag)}function j(a){a||(a=b);var d=f(a);return!t.shivCSS||k||d.hasCSS||(d.hasCSS=!!c(a,"article,aside,dialog,figcaption,figure,footer,header,hgroup,main,nav,section{display:block}mark{background:#FF0;color:#000}template{display:none}")),l||i(a,d),a}var k,l,m="3.7.3",n=a.html5||{},o=/^<|^(?:button|map|select|textarea|object|iframe|option|optgroup)$/i,p=/^(?:a|b|code|div|fieldset|h1|h2|h3|h4|h5|h6|i|label|li|ol|p|q|span|strong|style|table|tbody|td|th|tr|ul)$/i,q="_html5shiv",r=0,s={};!function(){try{var a=b.createElement("a");a.innerHTML="",k="hidden"in a,l=1==a.childNodes.length||function(){b.createElement("a");var a=b.createDocumentFragment();return"undefined"==typeof a.cloneNode||"undefined"==typeof a.createDocumentFragment||"undefined"==typeof a.createElement}()}catch(c){k=!0,l=!0}}();var t={elements:n.elements||"abbr article aside audio bdi canvas data datalist details dialog figcaption figure footer header hgroup main mark meter nav output picture progress section summary template time video",version:m,shivCSS:n.shivCSS!==!1,supportsUnknownElements:l,shivMethods:n.shivMethods!==!1,type:"default",shivDocument:j,createElement:g,createDocumentFragment:h,addElements:e};a.html5=t,j(b),"object"==typeof module&&module.exports&&(module.exports=t)}("undefined"!=typeof window?window:this,document); diff --git a/js/jquery-3.6.0.min.js b/js/jquery-3.6.0.min.js new file mode 100644 index 00000000..c4c6022f --- /dev/null +++ b/js/jquery-3.6.0.min.js @@ -0,0 +1,2 @@ +/*! jQuery v3.6.0 | (c) OpenJS Foundation and other contributors | jquery.org/license */ +!function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=e.document?t(e,!0):function(e){if(!e.document)throw new Error("jQuery requires a window with a document");return t(e)}:t(e)}("undefined"!=typeof window?window:this,function(C,e){"use strict";var t=[],r=Object.getPrototypeOf,s=t.slice,g=t.flat?function(e){return t.flat.call(e)}:function(e){return t.concat.apply([],e)},u=t.push,i=t.indexOf,n={},o=n.toString,v=n.hasOwnProperty,a=v.toString,l=a.call(Object),y={},m=function(e){return"function"==typeof e&&"number"!=typeof e.nodeType&&"function"!=typeof e.item},x=function(e){return null!=e&&e===e.window},E=C.document,c={type:!0,src:!0,nonce:!0,noModule:!0};function b(e,t,n){var r,i,o=(n=n||E).createElement("script");if(o.text=e,t)for(r in c)(i=t[r]||t.getAttribute&&t.getAttribute(r))&&o.setAttribute(r,i);n.head.appendChild(o).parentNode.removeChild(o)}function w(e){return null==e?e+"":"object"==typeof e||"function"==typeof e?n[o.call(e)]||"object":typeof e}var f="3.6.0",S=function(e,t){return new S.fn.init(e,t)};function p(e){var t=!!e&&"length"in e&&e.length,n=w(e);return!m(e)&&!x(e)&&("array"===n||0===t||"number"==typeof t&&0+~]|"+M+")"+M+"*"),U=new RegExp(M+"|>"),X=new RegExp(F),V=new RegExp("^"+I+"$"),G={ID:new RegExp("^#("+I+")"),CLASS:new RegExp("^\\.("+I+")"),TAG:new RegExp("^("+I+"|[*])"),ATTR:new RegExp("^"+W),PSEUDO:new RegExp("^"+F),CHILD:new RegExp("^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\("+M+"*(even|odd|(([+-]|)(\\d*)n|)"+M+"*(?:([+-]|)"+M+"*(\\d+)|))"+M+"*\\)|)","i"),bool:new RegExp("^(?:"+R+")$","i"),needsContext:new RegExp("^"+M+"*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\("+M+"*((?:-\\d)?\\d*)"+M+"*\\)|)(?=[^-]|$)","i")},Y=/HTML$/i,Q=/^(?:input|select|textarea|button)$/i,J=/^h\d$/i,K=/^[^{]+\{\s*\[native \w/,Z=/^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,ee=/[+~]/,te=new RegExp("\\\\[\\da-fA-F]{1,6}"+M+"?|\\\\([^\\r\\n\\f])","g"),ne=function(e,t){var n="0x"+e.slice(1)-65536;return t||(n<0?String.fromCharCode(n+65536):String.fromCharCode(n>>10|55296,1023&n|56320))},re=/([\0-\x1f\x7f]|^-?\d)|^-$|[^\0-\x1f\x7f-\uFFFF\w-]/g,ie=function(e,t){return t?"\0"===e?"\ufffd":e.slice(0,-1)+"\\"+e.charCodeAt(e.length-1).toString(16)+" ":"\\"+e},oe=function(){T()},ae=be(function(e){return!0===e.disabled&&"fieldset"===e.nodeName.toLowerCase()},{dir:"parentNode",next:"legend"});try{H.apply(t=O.call(p.childNodes),p.childNodes),t[p.childNodes.length].nodeType}catch(e){H={apply:t.length?function(e,t){L.apply(e,O.call(t))}:function(e,t){var n=e.length,r=0;while(e[n++]=t[r++]);e.length=n-1}}}function se(t,e,n,r){var i,o,a,s,u,l,c,f=e&&e.ownerDocument,p=e?e.nodeType:9;if(n=n||[],"string"!=typeof t||!t||1!==p&&9!==p&&11!==p)return n;if(!r&&(T(e),e=e||C,E)){if(11!==p&&(u=Z.exec(t)))if(i=u[1]){if(9===p){if(!(a=e.getElementById(i)))return n;if(a.id===i)return n.push(a),n}else if(f&&(a=f.getElementById(i))&&y(e,a)&&a.id===i)return n.push(a),n}else{if(u[2])return H.apply(n,e.getElementsByTagName(t)),n;if((i=u[3])&&d.getElementsByClassName&&e.getElementsByClassName)return H.apply(n,e.getElementsByClassName(i)),n}if(d.qsa&&!N[t+" "]&&(!v||!v.test(t))&&(1!==p||"object"!==e.nodeName.toLowerCase())){if(c=t,f=e,1===p&&(U.test(t)||z.test(t))){(f=ee.test(t)&&ye(e.parentNode)||e)===e&&d.scope||((s=e.getAttribute("id"))?s=s.replace(re,ie):e.setAttribute("id",s=S)),o=(l=h(t)).length;while(o--)l[o]=(s?"#"+s:":scope")+" "+xe(l[o]);c=l.join(",")}try{return H.apply(n,f.querySelectorAll(c)),n}catch(e){N(t,!0)}finally{s===S&&e.removeAttribute("id")}}}return g(t.replace($,"$1"),e,n,r)}function ue(){var r=[];return function e(t,n){return r.push(t+" ")>b.cacheLength&&delete e[r.shift()],e[t+" "]=n}}function le(e){return e[S]=!0,e}function ce(e){var t=C.createElement("fieldset");try{return!!e(t)}catch(e){return!1}finally{t.parentNode&&t.parentNode.removeChild(t),t=null}}function fe(e,t){var n=e.split("|"),r=n.length;while(r--)b.attrHandle[n[r]]=t}function pe(e,t){var n=t&&e,r=n&&1===e.nodeType&&1===t.nodeType&&e.sourceIndex-t.sourceIndex;if(r)return r;if(n)while(n=n.nextSibling)if(n===t)return-1;return e?1:-1}function de(t){return function(e){return"input"===e.nodeName.toLowerCase()&&e.type===t}}function he(n){return function(e){var t=e.nodeName.toLowerCase();return("input"===t||"button"===t)&&e.type===n}}function ge(t){return function(e){return"form"in e?e.parentNode&&!1===e.disabled?"label"in e?"label"in e.parentNode?e.parentNode.disabled===t:e.disabled===t:e.isDisabled===t||e.isDisabled!==!t&&ae(e)===t:e.disabled===t:"label"in e&&e.disabled===t}}function ve(a){return le(function(o){return o=+o,le(function(e,t){var n,r=a([],e.length,o),i=r.length;while(i--)e[n=r[i]]&&(e[n]=!(t[n]=e[n]))})})}function ye(e){return e&&"undefined"!=typeof e.getElementsByTagName&&e}for(e in d=se.support={},i=se.isXML=function(e){var t=e&&e.namespaceURI,n=e&&(e.ownerDocument||e).documentElement;return!Y.test(t||n&&n.nodeName||"HTML")},T=se.setDocument=function(e){var t,n,r=e?e.ownerDocument||e:p;return r!=C&&9===r.nodeType&&r.documentElement&&(a=(C=r).documentElement,E=!i(C),p!=C&&(n=C.defaultView)&&n.top!==n&&(n.addEventListener?n.addEventListener("unload",oe,!1):n.attachEvent&&n.attachEvent("onunload",oe)),d.scope=ce(function(e){return a.appendChild(e).appendChild(C.createElement("div")),"undefined"!=typeof e.querySelectorAll&&!e.querySelectorAll(":scope fieldset div").length}),d.attributes=ce(function(e){return e.className="i",!e.getAttribute("className")}),d.getElementsByTagName=ce(function(e){return e.appendChild(C.createComment("")),!e.getElementsByTagName("*").length}),d.getElementsByClassName=K.test(C.getElementsByClassName),d.getById=ce(function(e){return a.appendChild(e).id=S,!C.getElementsByName||!C.getElementsByName(S).length}),d.getById?(b.filter.ID=function(e){var t=e.replace(te,ne);return function(e){return e.getAttribute("id")===t}},b.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&E){var n=t.getElementById(e);return n?[n]:[]}}):(b.filter.ID=function(e){var n=e.replace(te,ne);return function(e){var t="undefined"!=typeof e.getAttributeNode&&e.getAttributeNode("id");return t&&t.value===n}},b.find.ID=function(e,t){if("undefined"!=typeof t.getElementById&&E){var n,r,i,o=t.getElementById(e);if(o){if((n=o.getAttributeNode("id"))&&n.value===e)return[o];i=t.getElementsByName(e),r=0;while(o=i[r++])if((n=o.getAttributeNode("id"))&&n.value===e)return[o]}return[]}}),b.find.TAG=d.getElementsByTagName?function(e,t){return"undefined"!=typeof t.getElementsByTagName?t.getElementsByTagName(e):d.qsa?t.querySelectorAll(e):void 0}:function(e,t){var n,r=[],i=0,o=t.getElementsByTagName(e);if("*"===e){while(n=o[i++])1===n.nodeType&&r.push(n);return r}return o},b.find.CLASS=d.getElementsByClassName&&function(e,t){if("undefined"!=typeof t.getElementsByClassName&&E)return t.getElementsByClassName(e)},s=[],v=[],(d.qsa=K.test(C.querySelectorAll))&&(ce(function(e){var t;a.appendChild(e).innerHTML="",e.querySelectorAll("[msallowcapture^='']").length&&v.push("[*^$]="+M+"*(?:''|\"\")"),e.querySelectorAll("[selected]").length||v.push("\\["+M+"*(?:value|"+R+")"),e.querySelectorAll("[id~="+S+"-]").length||v.push("~="),(t=C.createElement("input")).setAttribute("name",""),e.appendChild(t),e.querySelectorAll("[name='']").length||v.push("\\["+M+"*name"+M+"*="+M+"*(?:''|\"\")"),e.querySelectorAll(":checked").length||v.push(":checked"),e.querySelectorAll("a#"+S+"+*").length||v.push(".#.+[+~]"),e.querySelectorAll("\\\f"),v.push("[\\r\\n\\f]")}),ce(function(e){e.innerHTML="";var t=C.createElement("input");t.setAttribute("type","hidden"),e.appendChild(t).setAttribute("name","D"),e.querySelectorAll("[name=d]").length&&v.push("name"+M+"*[*^$|!~]?="),2!==e.querySelectorAll(":enabled").length&&v.push(":enabled",":disabled"),a.appendChild(e).disabled=!0,2!==e.querySelectorAll(":disabled").length&&v.push(":enabled",":disabled"),e.querySelectorAll("*,:x"),v.push(",.*:")})),(d.matchesSelector=K.test(c=a.matches||a.webkitMatchesSelector||a.mozMatchesSelector||a.oMatchesSelector||a.msMatchesSelector))&&ce(function(e){d.disconnectedMatch=c.call(e,"*"),c.call(e,"[s!='']:x"),s.push("!=",F)}),v=v.length&&new RegExp(v.join("|")),s=s.length&&new RegExp(s.join("|")),t=K.test(a.compareDocumentPosition),y=t||K.test(a.contains)?function(e,t){var n=9===e.nodeType?e.documentElement:e,r=t&&t.parentNode;return e===r||!(!r||1!==r.nodeType||!(n.contains?n.contains(r):e.compareDocumentPosition&&16&e.compareDocumentPosition(r)))}:function(e,t){if(t)while(t=t.parentNode)if(t===e)return!0;return!1},j=t?function(e,t){if(e===t)return l=!0,0;var n=!e.compareDocumentPosition-!t.compareDocumentPosition;return n||(1&(n=(e.ownerDocument||e)==(t.ownerDocument||t)?e.compareDocumentPosition(t):1)||!d.sortDetached&&t.compareDocumentPosition(e)===n?e==C||e.ownerDocument==p&&y(p,e)?-1:t==C||t.ownerDocument==p&&y(p,t)?1:u?P(u,e)-P(u,t):0:4&n?-1:1)}:function(e,t){if(e===t)return l=!0,0;var n,r=0,i=e.parentNode,o=t.parentNode,a=[e],s=[t];if(!i||!o)return e==C?-1:t==C?1:i?-1:o?1:u?P(u,e)-P(u,t):0;if(i===o)return pe(e,t);n=e;while(n=n.parentNode)a.unshift(n);n=t;while(n=n.parentNode)s.unshift(n);while(a[r]===s[r])r++;return r?pe(a[r],s[r]):a[r]==p?-1:s[r]==p?1:0}),C},se.matches=function(e,t){return se(e,null,null,t)},se.matchesSelector=function(e,t){if(T(e),d.matchesSelector&&E&&!N[t+" "]&&(!s||!s.test(t))&&(!v||!v.test(t)))try{var n=c.call(e,t);if(n||d.disconnectedMatch||e.document&&11!==e.document.nodeType)return n}catch(e){N(t,!0)}return 0":{dir:"parentNode",first:!0}," ":{dir:"parentNode"},"+":{dir:"previousSibling",first:!0},"~":{dir:"previousSibling"}},preFilter:{ATTR:function(e){return e[1]=e[1].replace(te,ne),e[3]=(e[3]||e[4]||e[5]||"").replace(te,ne),"~="===e[2]&&(e[3]=" "+e[3]+" "),e.slice(0,4)},CHILD:function(e){return e[1]=e[1].toLowerCase(),"nth"===e[1].slice(0,3)?(e[3]||se.error(e[0]),e[4]=+(e[4]?e[5]+(e[6]||1):2*("even"===e[3]||"odd"===e[3])),e[5]=+(e[7]+e[8]||"odd"===e[3])):e[3]&&se.error(e[0]),e},PSEUDO:function(e){var t,n=!e[6]&&e[2];return G.CHILD.test(e[0])?null:(e[3]?e[2]=e[4]||e[5]||"":n&&X.test(n)&&(t=h(n,!0))&&(t=n.indexOf(")",n.length-t)-n.length)&&(e[0]=e[0].slice(0,t),e[2]=n.slice(0,t)),e.slice(0,3))}},filter:{TAG:function(e){var t=e.replace(te,ne).toLowerCase();return"*"===e?function(){return!0}:function(e){return e.nodeName&&e.nodeName.toLowerCase()===t}},CLASS:function(e){var t=m[e+" "];return t||(t=new RegExp("(^|"+M+")"+e+"("+M+"|$)"))&&m(e,function(e){return t.test("string"==typeof e.className&&e.className||"undefined"!=typeof e.getAttribute&&e.getAttribute("class")||"")})},ATTR:function(n,r,i){return function(e){var t=se.attr(e,n);return null==t?"!="===r:!r||(t+="","="===r?t===i:"!="===r?t!==i:"^="===r?i&&0===t.indexOf(i):"*="===r?i&&-1:\x20\t\r\n\f]*)[\x20\t\r\n\f]*\/?>(?:<\/\1>|)$/i;function j(e,n,r){return m(n)?S.grep(e,function(e,t){return!!n.call(e,t,e)!==r}):n.nodeType?S.grep(e,function(e){return e===n!==r}):"string"!=typeof n?S.grep(e,function(e){return-1)[^>]*|#([\w-]+))$/;(S.fn.init=function(e,t,n){var r,i;if(!e)return this;if(n=n||D,"string"==typeof e){if(!(r="<"===e[0]&&">"===e[e.length-1]&&3<=e.length?[null,e,null]:q.exec(e))||!r[1]&&t)return!t||t.jquery?(t||n).find(e):this.constructor(t).find(e);if(r[1]){if(t=t instanceof S?t[0]:t,S.merge(this,S.parseHTML(r[1],t&&t.nodeType?t.ownerDocument||t:E,!0)),N.test(r[1])&&S.isPlainObject(t))for(r in t)m(this[r])?this[r](t[r]):this.attr(r,t[r]);return this}return(i=E.getElementById(r[2]))&&(this[0]=i,this.length=1),this}return e.nodeType?(this[0]=e,this.length=1,this):m(e)?void 0!==n.ready?n.ready(e):e(S):S.makeArray(e,this)}).prototype=S.fn,D=S(E);var L=/^(?:parents|prev(?:Until|All))/,H={children:!0,contents:!0,next:!0,prev:!0};function O(e,t){while((e=e[t])&&1!==e.nodeType);return e}S.fn.extend({has:function(e){var t=S(e,this),n=t.length;return this.filter(function(){for(var e=0;e\x20\t\r\n\f]*)/i,he=/^$|^module$|\/(?:java|ecma)script/i;ce=E.createDocumentFragment().appendChild(E.createElement("div")),(fe=E.createElement("input")).setAttribute("type","radio"),fe.setAttribute("checked","checked"),fe.setAttribute("name","t"),ce.appendChild(fe),y.checkClone=ce.cloneNode(!0).cloneNode(!0).lastChild.checked,ce.innerHTML="",y.noCloneChecked=!!ce.cloneNode(!0).lastChild.defaultValue,ce.innerHTML="",y.option=!!ce.lastChild;var ge={thead:[1,"","
"],col:[2,"","
"],tr:[2,"","
"],td:[3,"","
"],_default:[0,"",""]};function ve(e,t){var n;return n="undefined"!=typeof e.getElementsByTagName?e.getElementsByTagName(t||"*"):"undefined"!=typeof e.querySelectorAll?e.querySelectorAll(t||"*"):[],void 0===t||t&&A(e,t)?S.merge([e],n):n}function ye(e,t){for(var n=0,r=e.length;n",""]);var me=/<|&#?\w+;/;function xe(e,t,n,r,i){for(var o,a,s,u,l,c,f=t.createDocumentFragment(),p=[],d=0,h=e.length;d\s*$/g;function je(e,t){return A(e,"table")&&A(11!==t.nodeType?t:t.firstChild,"tr")&&S(e).children("tbody")[0]||e}function De(e){return e.type=(null!==e.getAttribute("type"))+"/"+e.type,e}function qe(e){return"true/"===(e.type||"").slice(0,5)?e.type=e.type.slice(5):e.removeAttribute("type"),e}function Le(e,t){var n,r,i,o,a,s;if(1===t.nodeType){if(Y.hasData(e)&&(s=Y.get(e).events))for(i in Y.remove(t,"handle events"),s)for(n=0,r=s[i].length;n").attr(n.scriptAttrs||{}).prop({charset:n.scriptCharset,src:n.url}).on("load error",i=function(e){r.remove(),i=null,e&&t("error"===e.type?404:200,e.type)}),E.head.appendChild(r[0])},abort:function(){i&&i()}}});var _t,zt=[],Ut=/(=)\?(?=&|$)|\?\?/;S.ajaxSetup({jsonp:"callback",jsonpCallback:function(){var e=zt.pop()||S.expando+"_"+wt.guid++;return this[e]=!0,e}}),S.ajaxPrefilter("json jsonp",function(e,t,n){var r,i,o,a=!1!==e.jsonp&&(Ut.test(e.url)?"url":"string"==typeof e.data&&0===(e.contentType||"").indexOf("application/x-www-form-urlencoded")&&Ut.test(e.data)&&"data");if(a||"jsonp"===e.dataTypes[0])return r=e.jsonpCallback=m(e.jsonpCallback)?e.jsonpCallback():e.jsonpCallback,a?e[a]=e[a].replace(Ut,"$1"+r):!1!==e.jsonp&&(e.url+=(Tt.test(e.url)?"&":"?")+e.jsonp+"="+r),e.converters["script json"]=function(){return o||S.error(r+" was not called"),o[0]},e.dataTypes[0]="json",i=C[r],C[r]=function(){o=arguments},n.always(function(){void 0===i?S(C).removeProp(r):C[r]=i,e[r]&&(e.jsonpCallback=t.jsonpCallback,zt.push(r)),o&&m(i)&&i(o[0]),o=i=void 0}),"script"}),y.createHTMLDocument=((_t=E.implementation.createHTMLDocument("").body).innerHTML="
",2===_t.childNodes.length),S.parseHTML=function(e,t,n){return"string"!=typeof e?[]:("boolean"==typeof t&&(n=t,t=!1),t||(y.createHTMLDocument?((r=(t=E.implementation.createHTMLDocument("")).createElement("base")).href=E.location.href,t.head.appendChild(r)):t=E),o=!n&&[],(i=N.exec(e))?[t.createElement(i[1])]:(i=xe([e],t,o),o&&o.length&&S(o).remove(),S.merge([],i.childNodes)));var r,i,o},S.fn.load=function(e,t,n){var r,i,o,a=this,s=e.indexOf(" ");return-1").append(S.parseHTML(e)).find(r):e)}).always(n&&function(e,t){a.each(function(){n.apply(this,o||[e.responseText,t,e])})}),this},S.expr.pseudos.animated=function(t){return S.grep(S.timers,function(e){return t===e.elem}).length},S.offset={setOffset:function(e,t,n){var r,i,o,a,s,u,l=S.css(e,"position"),c=S(e),f={};"static"===l&&(e.style.position="relative"),s=c.offset(),o=S.css(e,"top"),u=S.css(e,"left"),("absolute"===l||"fixed"===l)&&-1<(o+u).indexOf("auto")?(a=(r=c.position()).top,i=r.left):(a=parseFloat(o)||0,i=parseFloat(u)||0),m(t)&&(t=t.call(e,n,S.extend({},s))),null!=t.top&&(f.top=t.top-s.top+a),null!=t.left&&(f.left=t.left-s.left+i),"using"in t?t.using.call(e,f):c.css(f)}},S.fn.extend({offset:function(t){if(arguments.length)return void 0===t?this:this.each(function(e){S.offset.setOffset(this,t,e)});var e,n,r=this[0];return r?r.getClientRects().length?(e=r.getBoundingClientRect(),n=r.ownerDocument.defaultView,{top:e.top+n.pageYOffset,left:e.left+n.pageXOffset}):{top:0,left:0}:void 0},position:function(){if(this[0]){var e,t,n,r=this[0],i={top:0,left:0};if("fixed"===S.css(r,"position"))t=r.getBoundingClientRect();else{t=this.offset(),n=r.ownerDocument,e=r.offsetParent||n.documentElement;while(e&&(e===n.body||e===n.documentElement)&&"static"===S.css(e,"position"))e=e.parentNode;e&&e!==r&&1===e.nodeType&&((i=S(e).offset()).top+=S.css(e,"borderTopWidth",!0),i.left+=S.css(e,"borderLeftWidth",!0))}return{top:t.top-i.top-S.css(r,"marginTop",!0),left:t.left-i.left-S.css(r,"marginLeft",!0)}}},offsetParent:function(){return this.map(function(){var e=this.offsetParent;while(e&&"static"===S.css(e,"position"))e=e.offsetParent;return e||re})}}),S.each({scrollLeft:"pageXOffset",scrollTop:"pageYOffset"},function(t,i){var o="pageYOffset"===i;S.fn[t]=function(e){return $(this,function(e,t,n){var r;if(x(e)?r=e:9===e.nodeType&&(r=e.defaultView),void 0===n)return r?r[i]:e[t];r?r.scrollTo(o?r.pageXOffset:n,o?n:r.pageYOffset):e[t]=n},t,e,arguments.length)}}),S.each(["top","left"],function(e,n){S.cssHooks[n]=Fe(y.pixelPosition,function(e,t){if(t)return t=We(e,n),Pe.test(t)?S(e).position()[n]+"px":t})}),S.each({Height:"height",Width:"width"},function(a,s){S.each({padding:"inner"+a,content:s,"":"outer"+a},function(r,o){S.fn[o]=function(e,t){var n=arguments.length&&(r||"boolean"!=typeof e),i=r||(!0===e||!0===t?"margin":"border");return $(this,function(e,t,n){var r;return x(e)?0===o.indexOf("outer")?e["inner"+a]:e.document.documentElement["client"+a]:9===e.nodeType?(r=e.documentElement,Math.max(e.body["scroll"+a],r["scroll"+a],e.body["offset"+a],r["offset"+a],r["client"+a])):void 0===n?S.css(e,t,i):S.style(e,t,n,i)},s,n?e:void 0,n)}})}),S.each(["ajaxStart","ajaxStop","ajaxComplete","ajaxError","ajaxSuccess","ajaxSend"],function(e,t){S.fn[t]=function(e){return this.on(t,e)}}),S.fn.extend({bind:function(e,t,n){return this.on(e,null,t,n)},unbind:function(e,t){return this.off(e,null,t)},delegate:function(e,t,n,r){return this.on(t,e,n,r)},undelegate:function(e,t,n){return 1===arguments.length?this.off(e,"**"):this.off(t,e||"**",n)},hover:function(e,t){return this.mouseenter(e).mouseleave(t||e)}}),S.each("blur focus focusin focusout resize scroll click dblclick mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave change select submit keydown keypress keyup contextmenu".split(" "),function(e,n){S.fn[n]=function(e,t){return 0"),n("table.docutils.footnote").wrap("
"),n("table.docutils.citation").wrap("
"),n(".wy-menu-vertical ul").not(".simple").siblings("a").each((function(){var t=n(this);expand=n(''),expand.on("click",(function(n){return e.toggleCurrent(t),n.stopPropagation(),!1})),t.prepend(expand)}))},reset:function(){var n=encodeURI(window.location.hash)||"#";try{var e=$(".wy-menu-vertical"),t=e.find('[href="'+n+'"]');if(0===t.length){var i=$('.document [id="'+n.substring(1)+'"]').closest("div.section");0===(t=e.find('[href="#'+i.attr("id")+'"]')).length&&(t=e.find('[href="#"]'))}if(t.length>0){$(".wy-menu-vertical .current").removeClass("current").attr("aria-expanded","false"),t.addClass("current").attr("aria-expanded","true"),t.closest("li.toctree-l1").parent().addClass("current").attr("aria-expanded","true");for(let n=1;n<=10;n++)t.closest("li.toctree-l"+n).addClass("current").attr("aria-expanded","true");t[0].scrollIntoView()}}catch(n){console.log("Error expanding nav for anchor",n)}},onScroll:function(){this.winScroll=!1;var n=this.win.scrollTop(),e=n+this.winHeight,t=this.navBar.scrollTop()+(n-this.winPosition);n<0||e>this.docHeight||(this.navBar.scrollTop(t),this.winPosition=n)},onResize:function(){this.winResize=!1,this.winHeight=this.win.height(),this.docHeight=$(document).height()},hashChange:function(){this.linkScroll=!0,this.win.one("hashchange",(function(){this.linkScroll=!1}))},toggleCurrent:function(n){var e=n.closest("li");e.siblings("li.current").removeClass("current").attr("aria-expanded","false"),e.siblings().find("li.current").removeClass("current").attr("aria-expanded","false");var t=e.find("> ul li");t.length&&(t.removeClass("current").attr("aria-expanded","false"),e.toggleClass("current").attr("aria-expanded",(function(n,e){return"true"==e?"false":"true"})))}},"undefined"!=typeof window&&(window.SphinxRtdTheme={Navigation:n.exports.ThemeNav,StickyNav:n.exports.ThemeNav}),function(){for(var n=0,e=["ms","moz","webkit","o"],t=0;t to the black list. It should be a base name, not a -# path. You may set this option multiple times. -ignore= - -# Pickle collected data for later comparisons. -persistent=yes - -# List of plugins (as comma separated values of python modules names) to load, -# usually to register additional checkers. -load-plugins= - - -[MESSAGES CONTROL] - -# Enable the message, report, category or checker with the given id(s). You can -# either give multiple identifier separated by comma (,) or put this option -# multiple time. -enable=indexing-exception,old-raise-syntax - -# Disable the message, report, category or checker with the given id(s). You -# can either give multiple identifier separated by comma (,) or put this option -# multiple time. -disable=design,similarities,no-self-use,attribute-defined-outside-init,locally-disabled,star-args,pointless-except,bad-option-value,global-statement,fixme,suppressed-message,useless-suppression,locally-enabled,file-ignored,wrong-import-order,useless-object-inheritance,no-else-return,super-with-arguments,raise-missing-from,consider-using-f-string,unspecified-encoding,unnecessary-lambda-assignment,wrong-import-position,ungrouped-imports,deprecated-module - - -[REPORTS] - -# Set the output format. Available formats are text, parseable, colorized, msvs -# (visual studio) and html -output-format=text - -# Tells whether to display a full report or only the messages -reports=yes - -# Python expression which should return a note less than 10 (10 is the highest -# note). You have access to the variables errors warning, statement which -# respectively contain the number of errors / warnings messages and the total -# number of statements analyzed. This is used by the global evaluation report -# (R0004). -evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) - - -[VARIABLES] - -# Tells whether we should check for unused import in __init__ files. -init-import=no - -# A regular expression matching names used for dummy variables (i.e. not used). -dummy-variables-rgx=\*{0,2}(_$|unused_|dummy_) - -# List of additional names supposed to be defined in builtins. Remember that -# you should avoid to define new builtins when possible. -additional-builtins= - - -[BASIC] - -# Regular expression which should only match correct module names -module-rgx=(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$ - -# Regular expression which should only match correct module level names -const-rgx=(([A-Z_][A-Z0-9_]*)|(__.*__))$ - -# Regular expression which should only match correct class names -class-rgx=[A-Z_][a-zA-Z0-9]+$ - -# Regular expression which should only match correct function names -function-rgx=^(?:(?P_?[A-Z][a-zA-Z0-9]*)|(?P_?[a-z][a-z0-9_]*))$ - -# Regular expression which should only match correct method names -method-rgx=^(?:(?P__[a-z0-9_]+__|next)|(?P_{0,2}(?:test|assert)?[A-Z][a-zA-Z0-9]*)|(?:_{0,2}[a-z][a-z0-9_]*))$ - -# Regular expression which should only match correct instance attribute names -attr-rgx=^_{0,2}[a-z][a-z0-9_]*$ - -# Regular expression which should only match correct argument names -argument-rgx=^[a-z][a-z0-9_]*$ - -# Regular expression which should only match correct variable names -variable-rgx=^[a-z][a-z0-9_]*$ - -# Regular expression which should only match correct list comprehension / -# generator expression variable names -inlinevar-rgx=^[a-z][a-z0-9_]*$ - -# Good variable names which should always be accepted, separated by a comma -good-names=i,j,k,ex,main,Run,_ - -# Bad variable names which should always be refused, separated by a comma -bad-names=map,filter,apply,input,reduce,foo,bar,baz,toto,tutu,tata - -# Regular expression which should only match functions or classes name which do -# not require a docstring -no-docstring-rgx=(__.*__|main|test.*|.*Test) - -# Minimum length for a docstring -docstring-min-length=10 - - -[MISCELLANEOUS] - -# List of note tags to take in consideration, separated by a comma. -notes=FIXME,XXX,TODO - - -[FORMAT] - -# Maximum number of characters on a single line. -max-line-length=80 - -# Maximum number of lines in a module -max-module-lines=99999 - -# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 -# tab). -indent-string=' ' - - -[SIMILARITIES] - -# Minimum lines number of a similarity. -min-similarity-lines=4 - -# Ignore comments when computing similarities. -ignore-comments=yes - -# Ignore docstrings when computing similarities. -ignore-docstrings=yes - - -[TYPECHECK] - -# Tells whether missing members accessed in mixin class should be ignored. A -# mixin class is detected if its name ends with "mixin" (case insensitive). -ignore-mixin-members=yes - -# List of classes names for which member attributes should not be checked -# (useful for classes with attributes dynamically set). -ignored-classes= - -# List of members which are set dynamically and missed by pylint inference -# system, and so shouldn't trigger E0201 when accessed. -generated-members= - - -[DESIGN] - -# Maximum number of arguments for function / method -max-args=5 - -# Argument names that match this expression will be ignored. Default to name -# with leading underscore -ignored-argument-names=_.* - -# Maximum number of locals for function / method body -max-locals=15 - -# Maximum number of return / yield for function / method body -max-returns=6 - -# Maximum number of branch for function / method body -max-branches=12 - -# Maximum number of statements in function / method body -max-statements=50 - -# Maximum number of parents for a class (see R0901). -max-parents=7 - -# Maximum number of attributes for a class (see R0902). -max-attributes=7 - -# Minimum number of public methods for a class (see R0903). -min-public-methods=2 - -# Maximum number of public methods for a class (see R0904). -max-public-methods=20 - - -[IMPORTS] - -# Deprecated modules which should not be used, separated by a comma -deprecated-modules=regsub,string,TERMIOS,Bastion,rexec - -# Create a graph of every (i.e. internal and external) dependencies in the -# given file (report RP0402 must not be disabled) -import-graph= - -# Create a graph of external dependencies in the given file (report RP0402 must -# not be disabled) -ext-import-graph= - -# Create a graph of internal dependencies in the given file (report RP0402 must -# not be disabled) -int-import-graph= - - -[CLASSES] - -# List of method names used to declare (i.e. assign) instance attributes. -defining-attr-methods=__init__,__new__,setUp diff --git a/pyproject.toml b/pyproject.toml deleted file mode 100644 index 912c08aa..00000000 --- a/pyproject.toml +++ /dev/null @@ -1,66 +0,0 @@ -[build-system] -requires = ["setuptools>=45", "wheel"] -build-backend = "setuptools.build_meta" - -[project] -name = "fire" -version = "0.7.1" -description = "A library for automatically generating command line interfaces." -readme = "README.md" -license = {text = "Apache-2.0"} -authors = [ - {name = "David Bieber", email = "david810+fire@gmail.com"} -] -classifiers = [ - "Development Status :: 4 - Beta", - "Intended Audience :: Developers", - "Topic :: Software Development :: Libraries :: Python Modules", - "Programming Language :: Python", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", - "Programming Language :: Python :: 3.12", - "Programming Language :: Python :: 3.13", - "Programming Language :: Python :: 3.14", - "Operating System :: OS Independent", - "Operating System :: POSIX", - "Operating System :: MacOS", - "Operating System :: Unix", -] -keywords = ["command", "line", "interface", "cli", "python", "fire", "interactive", "bash", "tool"] -requires-python = ">=3.7" -dependencies = [ - "termcolor", -] - -[project.urls] -Homepage = "https://github.com/google/python-fire" -Repository = "https://github.com/google/python-fire" - -[project.optional-dependencies] -test = [ - "setuptools<=80.9.0", - "pip", - "pylint<3.3.8", - "pytest<=8.4.1", - "pytest-pylint<=1.1.2", - "pytest-runner<7.0.0", - "termcolor<3.2.0", - "hypothesis<6.137.0", - "levenshtein<=0.27.1", -] - -[tool.setuptools.packages.find] -include = ["fire*"] - -[tool.setuptools.package-data] -fire = ["console/*"] - -[tool.pytest.ini_options] -addopts = [ - "--ignore=fire/test_components_py3.py", - "--ignore=fire/parser_fuzz_test.py" -] diff --git a/search.html b/search.html new file mode 100644 index 00000000..8c84b999 --- /dev/null +++ b/search.html @@ -0,0 +1,136 @@ + + + + + + + + Python Fire + + + + + + + + + + + +
+ + +
+ +
+
+
    +
  • +
  • +
  • +
+
+
+
+
+ + +

Search Results

+ + + +
+ Searching... +
+ + +
+
+ +
+
+ +
+ +
+ +
+ + + + + +
+ + + + + + + + + diff --git a/search/lunr.js b/search/lunr.js new file mode 100644 index 00000000..aca0a167 --- /dev/null +++ b/search/lunr.js @@ -0,0 +1,3475 @@ +/** + * lunr - http://lunrjs.com - A bit like Solr, but much smaller and not as bright - 2.3.9 + * Copyright (C) 2020 Oliver Nightingale + * @license MIT + */ + +;(function(){ + +/** + * A convenience function for configuring and constructing + * a new lunr Index. + * + * A lunr.Builder instance is created and the pipeline setup + * with a trimmer, stop word filter and stemmer. + * + * This builder object is yielded to the configuration function + * that is passed as a parameter, allowing the list of fields + * and other builder parameters to be customised. + * + * All documents _must_ be added within the passed config function. + * + * @example + * var idx = lunr(function () { + * this.field('title') + * this.field('body') + * this.ref('id') + * + * documents.forEach(function (doc) { + * this.add(doc) + * }, this) + * }) + * + * @see {@link lunr.Builder} + * @see {@link lunr.Pipeline} + * @see {@link lunr.trimmer} + * @see {@link lunr.stopWordFilter} + * @see {@link lunr.stemmer} + * @namespace {function} lunr + */ +var lunr = function (config) { + var builder = new lunr.Builder + + builder.pipeline.add( + lunr.trimmer, + lunr.stopWordFilter, + lunr.stemmer + ) + + builder.searchPipeline.add( + lunr.stemmer + ) + + config.call(builder, builder) + return builder.build() +} + +lunr.version = "2.3.9" +/*! + * lunr.utils + * Copyright (C) 2020 Oliver Nightingale + */ + +/** + * A namespace containing utils for the rest of the lunr library + * @namespace lunr.utils + */ +lunr.utils = {} + +/** + * Print a warning message to the console. + * + * @param {String} message The message to be printed. + * @memberOf lunr.utils + * @function + */ +lunr.utils.warn = (function (global) { + /* eslint-disable no-console */ + return function (message) { + if (global.console && console.warn) { + console.warn(message) + } + } + /* eslint-enable no-console */ +})(this) + +/** + * Convert an object to a string. + * + * In the case of `null` and `undefined` the function returns + * the empty string, in all other cases the result of calling + * `toString` on the passed object is returned. + * + * @param {Any} obj The object to convert to a string. + * @return {String} string representation of the passed object. + * @memberOf lunr.utils + */ +lunr.utils.asString = function (obj) { + if (obj === void 0 || obj === null) { + return "" + } else { + return obj.toString() + } +} + +/** + * Clones an object. + * + * Will create a copy of an existing object such that any mutations + * on the copy cannot affect the original. + * + * Only shallow objects are supported, passing a nested object to this + * function will cause a TypeError. + * + * Objects with primitives, and arrays of primitives are supported. + * + * @param {Object} obj The object to clone. + * @return {Object} a clone of the passed object. + * @throws {TypeError} when a nested object is passed. + * @memberOf Utils + */ +lunr.utils.clone = function (obj) { + if (obj === null || obj === undefined) { + return obj + } + + var clone = Object.create(null), + keys = Object.keys(obj) + + for (var i = 0; i < keys.length; i++) { + var key = keys[i], + val = obj[key] + + if (Array.isArray(val)) { + clone[key] = val.slice() + continue + } + + if (typeof val === 'string' || + typeof val === 'number' || + typeof val === 'boolean') { + clone[key] = val + continue + } + + throw new TypeError("clone is not deep and does not support nested objects") + } + + return clone +} +lunr.FieldRef = function (docRef, fieldName, stringValue) { + this.docRef = docRef + this.fieldName = fieldName + this._stringValue = stringValue +} + +lunr.FieldRef.joiner = "/" + +lunr.FieldRef.fromString = function (s) { + var n = s.indexOf(lunr.FieldRef.joiner) + + if (n === -1) { + throw "malformed field ref string" + } + + var fieldRef = s.slice(0, n), + docRef = s.slice(n + 1) + + return new lunr.FieldRef (docRef, fieldRef, s) +} + +lunr.FieldRef.prototype.toString = function () { + if (this._stringValue == undefined) { + this._stringValue = this.fieldName + lunr.FieldRef.joiner + this.docRef + } + + return this._stringValue +} +/*! + * lunr.Set + * Copyright (C) 2020 Oliver Nightingale + */ + +/** + * A lunr set. + * + * @constructor + */ +lunr.Set = function (elements) { + this.elements = Object.create(null) + + if (elements) { + this.length = elements.length + + for (var i = 0; i < this.length; i++) { + this.elements[elements[i]] = true + } + } else { + this.length = 0 + } +} + +/** + * A complete set that contains all elements. + * + * @static + * @readonly + * @type {lunr.Set} + */ +lunr.Set.complete = { + intersect: function (other) { + return other + }, + + union: function () { + return this + }, + + contains: function () { + return true + } +} + +/** + * An empty set that contains no elements. + * + * @static + * @readonly + * @type {lunr.Set} + */ +lunr.Set.empty = { + intersect: function () { + return this + }, + + union: function (other) { + return other + }, + + contains: function () { + return false + } +} + +/** + * Returns true if this set contains the specified object. + * + * @param {object} object - Object whose presence in this set is to be tested. + * @returns {boolean} - True if this set contains the specified object. + */ +lunr.Set.prototype.contains = function (object) { + return !!this.elements[object] +} + +/** + * Returns a new set containing only the elements that are present in both + * this set and the specified set. + * + * @param {lunr.Set} other - set to intersect with this set. + * @returns {lunr.Set} a new set that is the intersection of this and the specified set. + */ + +lunr.Set.prototype.intersect = function (other) { + var a, b, elements, intersection = [] + + if (other === lunr.Set.complete) { + return this + } + + if (other === lunr.Set.empty) { + return other + } + + if (this.length < other.length) { + a = this + b = other + } else { + a = other + b = this + } + + elements = Object.keys(a.elements) + + for (var i = 0; i < elements.length; i++) { + var element = elements[i] + if (element in b.elements) { + intersection.push(element) + } + } + + return new lunr.Set (intersection) +} + +/** + * Returns a new set combining the elements of this and the specified set. + * + * @param {lunr.Set} other - set to union with this set. + * @return {lunr.Set} a new set that is the union of this and the specified set. + */ + +lunr.Set.prototype.union = function (other) { + if (other === lunr.Set.complete) { + return lunr.Set.complete + } + + if (other === lunr.Set.empty) { + return this + } + + return new lunr.Set(Object.keys(this.elements).concat(Object.keys(other.elements))) +} +/** + * A function to calculate the inverse document frequency for + * a posting. This is shared between the builder and the index + * + * @private + * @param {object} posting - The posting for a given term + * @param {number} documentCount - The total number of documents. + */ +lunr.idf = function (posting, documentCount) { + var documentsWithTerm = 0 + + for (var fieldName in posting) { + if (fieldName == '_index') continue // Ignore the term index, its not a field + documentsWithTerm += Object.keys(posting[fieldName]).length + } + + var x = (documentCount - documentsWithTerm + 0.5) / (documentsWithTerm + 0.5) + + return Math.log(1 + Math.abs(x)) +} + +/** + * A token wraps a string representation of a token + * as it is passed through the text processing pipeline. + * + * @constructor + * @param {string} [str=''] - The string token being wrapped. + * @param {object} [metadata={}] - Metadata associated with this token. + */ +lunr.Token = function (str, metadata) { + this.str = str || "" + this.metadata = metadata || {} +} + +/** + * Returns the token string that is being wrapped by this object. + * + * @returns {string} + */ +lunr.Token.prototype.toString = function () { + return this.str +} + +/** + * A token update function is used when updating or optionally + * when cloning a token. + * + * @callback lunr.Token~updateFunction + * @param {string} str - The string representation of the token. + * @param {Object} metadata - All metadata associated with this token. + */ + +/** + * Applies the given function to the wrapped string token. + * + * @example + * token.update(function (str, metadata) { + * return str.toUpperCase() + * }) + * + * @param {lunr.Token~updateFunction} fn - A function to apply to the token string. + * @returns {lunr.Token} + */ +lunr.Token.prototype.update = function (fn) { + this.str = fn(this.str, this.metadata) + return this +} + +/** + * Creates a clone of this token. Optionally a function can be + * applied to the cloned token. + * + * @param {lunr.Token~updateFunction} [fn] - An optional function to apply to the cloned token. + * @returns {lunr.Token} + */ +lunr.Token.prototype.clone = function (fn) { + fn = fn || function (s) { return s } + return new lunr.Token (fn(this.str, this.metadata), this.metadata) +} +/*! + * lunr.tokenizer + * Copyright (C) 2020 Oliver Nightingale + */ + +/** + * A function for splitting a string into tokens ready to be inserted into + * the search index. Uses `lunr.tokenizer.separator` to split strings, change + * the value of this property to change how strings are split into tokens. + * + * This tokenizer will convert its parameter to a string by calling `toString` and + * then will split this string on the character in `lunr.tokenizer.separator`. + * Arrays will have their elements converted to strings and wrapped in a lunr.Token. + * + * Optional metadata can be passed to the tokenizer, this metadata will be cloned and + * added as metadata to every token that is created from the object to be tokenized. + * + * @static + * @param {?(string|object|object[])} obj - The object to convert into tokens + * @param {?object} metadata - Optional metadata to associate with every token + * @returns {lunr.Token[]} + * @see {@link lunr.Pipeline} + */ +lunr.tokenizer = function (obj, metadata) { + if (obj == null || obj == undefined) { + return [] + } + + if (Array.isArray(obj)) { + return obj.map(function (t) { + return new lunr.Token( + lunr.utils.asString(t).toLowerCase(), + lunr.utils.clone(metadata) + ) + }) + } + + var str = obj.toString().toLowerCase(), + len = str.length, + tokens = [] + + for (var sliceEnd = 0, sliceStart = 0; sliceEnd <= len; sliceEnd++) { + var char = str.charAt(sliceEnd), + sliceLength = sliceEnd - sliceStart + + if ((char.match(lunr.tokenizer.separator) || sliceEnd == len)) { + + if (sliceLength > 0) { + var tokenMetadata = lunr.utils.clone(metadata) || {} + tokenMetadata["position"] = [sliceStart, sliceLength] + tokenMetadata["index"] = tokens.length + + tokens.push( + new lunr.Token ( + str.slice(sliceStart, sliceEnd), + tokenMetadata + ) + ) + } + + sliceStart = sliceEnd + 1 + } + + } + + return tokens +} + +/** + * The separator used to split a string into tokens. Override this property to change the behaviour of + * `lunr.tokenizer` behaviour when tokenizing strings. By default this splits on whitespace and hyphens. + * + * @static + * @see lunr.tokenizer + */ +lunr.tokenizer.separator = /[\s\-]+/ +/*! + * lunr.Pipeline + * Copyright (C) 2020 Oliver Nightingale + */ + +/** + * lunr.Pipelines maintain an ordered list of functions to be applied to all + * tokens in documents entering the search index and queries being ran against + * the index. + * + * An instance of lunr.Index created with the lunr shortcut will contain a + * pipeline with a stop word filter and an English language stemmer. Extra + * functions can be added before or after either of these functions or these + * default functions can be removed. + * + * When run the pipeline will call each function in turn, passing a token, the + * index of that token in the original list of all tokens and finally a list of + * all the original tokens. + * + * The output of functions in the pipeline will be passed to the next function + * in the pipeline. To exclude a token from entering the index the function + * should return undefined, the rest of the pipeline will not be called with + * this token. + * + * For serialisation of pipelines to work, all functions used in an instance of + * a pipeline should be registered with lunr.Pipeline. Registered functions can + * then be loaded. If trying to load a serialised pipeline that uses functions + * that are not registered an error will be thrown. + * + * If not planning on serialising the pipeline then registering pipeline functions + * is not necessary. + * + * @constructor + */ +lunr.Pipeline = function () { + this._stack = [] +} + +lunr.Pipeline.registeredFunctions = Object.create(null) + +/** + * A pipeline function maps lunr.Token to lunr.Token. A lunr.Token contains the token + * string as well as all known metadata. A pipeline function can mutate the token string + * or mutate (or add) metadata for a given token. + * + * A pipeline function can indicate that the passed token should be discarded by returning + * null, undefined or an empty string. This token will not be passed to any downstream pipeline + * functions and will not be added to the index. + * + * Multiple tokens can be returned by returning an array of tokens. Each token will be passed + * to any downstream pipeline functions and all will returned tokens will be added to the index. + * + * Any number of pipeline functions may be chained together using a lunr.Pipeline. + * + * @interface lunr.PipelineFunction + * @param {lunr.Token} token - A token from the document being processed. + * @param {number} i - The index of this token in the complete list of tokens for this document/field. + * @param {lunr.Token[]} tokens - All tokens for this document/field. + * @returns {(?lunr.Token|lunr.Token[])} + */ + +/** + * Register a function with the pipeline. + * + * Functions that are used in the pipeline should be registered if the pipeline + * needs to be serialised, or a serialised pipeline needs to be loaded. + * + * Registering a function does not add it to a pipeline, functions must still be + * added to instances of the pipeline for them to be used when running a pipeline. + * + * @param {lunr.PipelineFunction} fn - The function to check for. + * @param {String} label - The label to register this function with + */ +lunr.Pipeline.registerFunction = function (fn, label) { + if (label in this.registeredFunctions) { + lunr.utils.warn('Overwriting existing registered function: ' + label) + } + + fn.label = label + lunr.Pipeline.registeredFunctions[fn.label] = fn +} + +/** + * Warns if the function is not registered as a Pipeline function. + * + * @param {lunr.PipelineFunction} fn - The function to check for. + * @private + */ +lunr.Pipeline.warnIfFunctionNotRegistered = function (fn) { + var isRegistered = fn.label && (fn.label in this.registeredFunctions) + + if (!isRegistered) { + lunr.utils.warn('Function is not registered with pipeline. This may cause problems when serialising the index.\n', fn) + } +} + +/** + * Loads a previously serialised pipeline. + * + * All functions to be loaded must already be registered with lunr.Pipeline. + * If any function from the serialised data has not been registered then an + * error will be thrown. + * + * @param {Object} serialised - The serialised pipeline to load. + * @returns {lunr.Pipeline} + */ +lunr.Pipeline.load = function (serialised) { + var pipeline = new lunr.Pipeline + + serialised.forEach(function (fnName) { + var fn = lunr.Pipeline.registeredFunctions[fnName] + + if (fn) { + pipeline.add(fn) + } else { + throw new Error('Cannot load unregistered function: ' + fnName) + } + }) + + return pipeline +} + +/** + * Adds new functions to the end of the pipeline. + * + * Logs a warning if the function has not been registered. + * + * @param {lunr.PipelineFunction[]} functions - Any number of functions to add to the pipeline. + */ +lunr.Pipeline.prototype.add = function () { + var fns = Array.prototype.slice.call(arguments) + + fns.forEach(function (fn) { + lunr.Pipeline.warnIfFunctionNotRegistered(fn) + this._stack.push(fn) + }, this) +} + +/** + * Adds a single function after a function that already exists in the + * pipeline. + * + * Logs a warning if the function has not been registered. + * + * @param {lunr.PipelineFunction} existingFn - A function that already exists in the pipeline. + * @param {lunr.PipelineFunction} newFn - The new function to add to the pipeline. + */ +lunr.Pipeline.prototype.after = function (existingFn, newFn) { + lunr.Pipeline.warnIfFunctionNotRegistered(newFn) + + var pos = this._stack.indexOf(existingFn) + if (pos == -1) { + throw new Error('Cannot find existingFn') + } + + pos = pos + 1 + this._stack.splice(pos, 0, newFn) +} + +/** + * Adds a single function before a function that already exists in the + * pipeline. + * + * Logs a warning if the function has not been registered. + * + * @param {lunr.PipelineFunction} existingFn - A function that already exists in the pipeline. + * @param {lunr.PipelineFunction} newFn - The new function to add to the pipeline. + */ +lunr.Pipeline.prototype.before = function (existingFn, newFn) { + lunr.Pipeline.warnIfFunctionNotRegistered(newFn) + + var pos = this._stack.indexOf(existingFn) + if (pos == -1) { + throw new Error('Cannot find existingFn') + } + + this._stack.splice(pos, 0, newFn) +} + +/** + * Removes a function from the pipeline. + * + * @param {lunr.PipelineFunction} fn The function to remove from the pipeline. + */ +lunr.Pipeline.prototype.remove = function (fn) { + var pos = this._stack.indexOf(fn) + if (pos == -1) { + return + } + + this._stack.splice(pos, 1) +} + +/** + * Runs the current list of functions that make up the pipeline against the + * passed tokens. + * + * @param {Array} tokens The tokens to run through the pipeline. + * @returns {Array} + */ +lunr.Pipeline.prototype.run = function (tokens) { + var stackLength = this._stack.length + + for (var i = 0; i < stackLength; i++) { + var fn = this._stack[i] + var memo = [] + + for (var j = 0; j < tokens.length; j++) { + var result = fn(tokens[j], j, tokens) + + if (result === null || result === void 0 || result === '') continue + + if (Array.isArray(result)) { + for (var k = 0; k < result.length; k++) { + memo.push(result[k]) + } + } else { + memo.push(result) + } + } + + tokens = memo + } + + return tokens +} + +/** + * Convenience method for passing a string through a pipeline and getting + * strings out. This method takes care of wrapping the passed string in a + * token and mapping the resulting tokens back to strings. + * + * @param {string} str - The string to pass through the pipeline. + * @param {?object} metadata - Optional metadata to associate with the token + * passed to the pipeline. + * @returns {string[]} + */ +lunr.Pipeline.prototype.runString = function (str, metadata) { + var token = new lunr.Token (str, metadata) + + return this.run([token]).map(function (t) { + return t.toString() + }) +} + +/** + * Resets the pipeline by removing any existing processors. + * + */ +lunr.Pipeline.prototype.reset = function () { + this._stack = [] +} + +/** + * Returns a representation of the pipeline ready for serialisation. + * + * Logs a warning if the function has not been registered. + * + * @returns {Array} + */ +lunr.Pipeline.prototype.toJSON = function () { + return this._stack.map(function (fn) { + lunr.Pipeline.warnIfFunctionNotRegistered(fn) + + return fn.label + }) +} +/*! + * lunr.Vector + * Copyright (C) 2020 Oliver Nightingale + */ + +/** + * A vector is used to construct the vector space of documents and queries. These + * vectors support operations to determine the similarity between two documents or + * a document and a query. + * + * Normally no parameters are required for initializing a vector, but in the case of + * loading a previously dumped vector the raw elements can be provided to the constructor. + * + * For performance reasons vectors are implemented with a flat array, where an elements + * index is immediately followed by its value. E.g. [index, value, index, value]. This + * allows the underlying array to be as sparse as possible and still offer decent + * performance when being used for vector calculations. + * + * @constructor + * @param {Number[]} [elements] - The flat list of element index and element value pairs. + */ +lunr.Vector = function (elements) { + this._magnitude = 0 + this.elements = elements || [] +} + + +/** + * Calculates the position within the vector to insert a given index. + * + * This is used internally by insert and upsert. If there are duplicate indexes then + * the position is returned as if the value for that index were to be updated, but it + * is the callers responsibility to check whether there is a duplicate at that index + * + * @param {Number} insertIdx - The index at which the element should be inserted. + * @returns {Number} + */ +lunr.Vector.prototype.positionForIndex = function (index) { + // For an empty vector the tuple can be inserted at the beginning + if (this.elements.length == 0) { + return 0 + } + + var start = 0, + end = this.elements.length / 2, + sliceLength = end - start, + pivotPoint = Math.floor(sliceLength / 2), + pivotIndex = this.elements[pivotPoint * 2] + + while (sliceLength > 1) { + if (pivotIndex < index) { + start = pivotPoint + } + + if (pivotIndex > index) { + end = pivotPoint + } + + if (pivotIndex == index) { + break + } + + sliceLength = end - start + pivotPoint = start + Math.floor(sliceLength / 2) + pivotIndex = this.elements[pivotPoint * 2] + } + + if (pivotIndex == index) { + return pivotPoint * 2 + } + + if (pivotIndex > index) { + return pivotPoint * 2 + } + + if (pivotIndex < index) { + return (pivotPoint + 1) * 2 + } +} + +/** + * Inserts an element at an index within the vector. + * + * Does not allow duplicates, will throw an error if there is already an entry + * for this index. + * + * @param {Number} insertIdx - The index at which the element should be inserted. + * @param {Number} val - The value to be inserted into the vector. + */ +lunr.Vector.prototype.insert = function (insertIdx, val) { + this.upsert(insertIdx, val, function () { + throw "duplicate index" + }) +} + +/** + * Inserts or updates an existing index within the vector. + * + * @param {Number} insertIdx - The index at which the element should be inserted. + * @param {Number} val - The value to be inserted into the vector. + * @param {function} fn - A function that is called for updates, the existing value and the + * requested value are passed as arguments + */ +lunr.Vector.prototype.upsert = function (insertIdx, val, fn) { + this._magnitude = 0 + var position = this.positionForIndex(insertIdx) + + if (this.elements[position] == insertIdx) { + this.elements[position + 1] = fn(this.elements[position + 1], val) + } else { + this.elements.splice(position, 0, insertIdx, val) + } +} + +/** + * Calculates the magnitude of this vector. + * + * @returns {Number} + */ +lunr.Vector.prototype.magnitude = function () { + if (this._magnitude) return this._magnitude + + var sumOfSquares = 0, + elementsLength = this.elements.length + + for (var i = 1; i < elementsLength; i += 2) { + var val = this.elements[i] + sumOfSquares += val * val + } + + return this._magnitude = Math.sqrt(sumOfSquares) +} + +/** + * Calculates the dot product of this vector and another vector. + * + * @param {lunr.Vector} otherVector - The vector to compute the dot product with. + * @returns {Number} + */ +lunr.Vector.prototype.dot = function (otherVector) { + var dotProduct = 0, + a = this.elements, b = otherVector.elements, + aLen = a.length, bLen = b.length, + aVal = 0, bVal = 0, + i = 0, j = 0 + + while (i < aLen && j < bLen) { + aVal = a[i], bVal = b[j] + if (aVal < bVal) { + i += 2 + } else if (aVal > bVal) { + j += 2 + } else if (aVal == bVal) { + dotProduct += a[i + 1] * b[j + 1] + i += 2 + j += 2 + } + } + + return dotProduct +} + +/** + * Calculates the similarity between this vector and another vector. + * + * @param {lunr.Vector} otherVector - The other vector to calculate the + * similarity with. + * @returns {Number} + */ +lunr.Vector.prototype.similarity = function (otherVector) { + return this.dot(otherVector) / this.magnitude() || 0 +} + +/** + * Converts the vector to an array of the elements within the vector. + * + * @returns {Number[]} + */ +lunr.Vector.prototype.toArray = function () { + var output = new Array (this.elements.length / 2) + + for (var i = 1, j = 0; i < this.elements.length; i += 2, j++) { + output[j] = this.elements[i] + } + + return output +} + +/** + * A JSON serializable representation of the vector. + * + * @returns {Number[]} + */ +lunr.Vector.prototype.toJSON = function () { + return this.elements +} +/* eslint-disable */ +/*! + * lunr.stemmer + * Copyright (C) 2020 Oliver Nightingale + * Includes code from - http://tartarus.org/~martin/PorterStemmer/js.txt + */ + +/** + * lunr.stemmer is an english language stemmer, this is a JavaScript + * implementation of the PorterStemmer taken from http://tartarus.org/~martin + * + * @static + * @implements {lunr.PipelineFunction} + * @param {lunr.Token} token - The string to stem + * @returns {lunr.Token} + * @see {@link lunr.Pipeline} + * @function + */ +lunr.stemmer = (function(){ + var step2list = { + "ational" : "ate", + "tional" : "tion", + "enci" : "ence", + "anci" : "ance", + "izer" : "ize", + "bli" : "ble", + "alli" : "al", + "entli" : "ent", + "eli" : "e", + "ousli" : "ous", + "ization" : "ize", + "ation" : "ate", + "ator" : "ate", + "alism" : "al", + "iveness" : "ive", + "fulness" : "ful", + "ousness" : "ous", + "aliti" : "al", + "iviti" : "ive", + "biliti" : "ble", + "logi" : "log" + }, + + step3list = { + "icate" : "ic", + "ative" : "", + "alize" : "al", + "iciti" : "ic", + "ical" : "ic", + "ful" : "", + "ness" : "" + }, + + c = "[^aeiou]", // consonant + v = "[aeiouy]", // vowel + C = c + "[^aeiouy]*", // consonant sequence + V = v + "[aeiou]*", // vowel sequence + + mgr0 = "^(" + C + ")?" + V + C, // [C]VC... is m>0 + meq1 = "^(" + C + ")?" + V + C + "(" + V + ")?$", // [C]VC[V] is m=1 + mgr1 = "^(" + C + ")?" + V + C + V + C, // [C]VCVC... is m>1 + s_v = "^(" + C + ")?" + v; // vowel in stem + + var re_mgr0 = new RegExp(mgr0); + var re_mgr1 = new RegExp(mgr1); + var re_meq1 = new RegExp(meq1); + var re_s_v = new RegExp(s_v); + + var re_1a = /^(.+?)(ss|i)es$/; + var re2_1a = /^(.+?)([^s])s$/; + var re_1b = /^(.+?)eed$/; + var re2_1b = /^(.+?)(ed|ing)$/; + var re_1b_2 = /.$/; + var re2_1b_2 = /(at|bl|iz)$/; + var re3_1b_2 = new RegExp("([^aeiouylsz])\\1$"); + var re4_1b_2 = new RegExp("^" + C + v + "[^aeiouwxy]$"); + + var re_1c = /^(.+?[^aeiou])y$/; + var re_2 = /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/; + + var re_3 = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/; + + var re_4 = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/; + var re2_4 = /^(.+?)(s|t)(ion)$/; + + var re_5 = /^(.+?)e$/; + var re_5_1 = /ll$/; + var re3_5 = new RegExp("^" + C + v + "[^aeiouwxy]$"); + + var porterStemmer = function porterStemmer(w) { + var stem, + suffix, + firstch, + re, + re2, + re3, + re4; + + if (w.length < 3) { return w; } + + firstch = w.substr(0,1); + if (firstch == "y") { + w = firstch.toUpperCase() + w.substr(1); + } + + // Step 1a + re = re_1a + re2 = re2_1a; + + if (re.test(w)) { w = w.replace(re,"$1$2"); } + else if (re2.test(w)) { w = w.replace(re2,"$1$2"); } + + // Step 1b + re = re_1b; + re2 = re2_1b; + if (re.test(w)) { + var fp = re.exec(w); + re = re_mgr0; + if (re.test(fp[1])) { + re = re_1b_2; + w = w.replace(re,""); + } + } else if (re2.test(w)) { + var fp = re2.exec(w); + stem = fp[1]; + re2 = re_s_v; + if (re2.test(stem)) { + w = stem; + re2 = re2_1b_2; + re3 = re3_1b_2; + re4 = re4_1b_2; + if (re2.test(w)) { w = w + "e"; } + else if (re3.test(w)) { re = re_1b_2; w = w.replace(re,""); } + else if (re4.test(w)) { w = w + "e"; } + } + } + + // Step 1c - replace suffix y or Y by i if preceded by a non-vowel which is not the first letter of the word (so cry -> cri, by -> by, say -> say) + re = re_1c; + if (re.test(w)) { + var fp = re.exec(w); + stem = fp[1]; + w = stem + "i"; + } + + // Step 2 + re = re_2; + if (re.test(w)) { + var fp = re.exec(w); + stem = fp[1]; + suffix = fp[2]; + re = re_mgr0; + if (re.test(stem)) { + w = stem + step2list[suffix]; + } + } + + // Step 3 + re = re_3; + if (re.test(w)) { + var fp = re.exec(w); + stem = fp[1]; + suffix = fp[2]; + re = re_mgr0; + if (re.test(stem)) { + w = stem + step3list[suffix]; + } + } + + // Step 4 + re = re_4; + re2 = re2_4; + if (re.test(w)) { + var fp = re.exec(w); + stem = fp[1]; + re = re_mgr1; + if (re.test(stem)) { + w = stem; + } + } else if (re2.test(w)) { + var fp = re2.exec(w); + stem = fp[1] + fp[2]; + re2 = re_mgr1; + if (re2.test(stem)) { + w = stem; + } + } + + // Step 5 + re = re_5; + if (re.test(w)) { + var fp = re.exec(w); + stem = fp[1]; + re = re_mgr1; + re2 = re_meq1; + re3 = re3_5; + if (re.test(stem) || (re2.test(stem) && !(re3.test(stem)))) { + w = stem; + } + } + + re = re_5_1; + re2 = re_mgr1; + if (re.test(w) && re2.test(w)) { + re = re_1b_2; + w = w.replace(re,""); + } + + // and turn initial Y back to y + + if (firstch == "y") { + w = firstch.toLowerCase() + w.substr(1); + } + + return w; + }; + + return function (token) { + return token.update(porterStemmer); + } +})(); + +lunr.Pipeline.registerFunction(lunr.stemmer, 'stemmer') +/*! + * lunr.stopWordFilter + * Copyright (C) 2020 Oliver Nightingale + */ + +/** + * lunr.generateStopWordFilter builds a stopWordFilter function from the provided + * list of stop words. + * + * The built in lunr.stopWordFilter is built using this generator and can be used + * to generate custom stopWordFilters for applications or non English languages. + * + * @function + * @param {Array} token The token to pass through the filter + * @returns {lunr.PipelineFunction} + * @see lunr.Pipeline + * @see lunr.stopWordFilter + */ +lunr.generateStopWordFilter = function (stopWords) { + var words = stopWords.reduce(function (memo, stopWord) { + memo[stopWord] = stopWord + return memo + }, {}) + + return function (token) { + if (token && words[token.toString()] !== token.toString()) return token + } +} + +/** + * lunr.stopWordFilter is an English language stop word list filter, any words + * contained in the list will not be passed through the filter. + * + * This is intended to be used in the Pipeline. If the token does not pass the + * filter then undefined will be returned. + * + * @function + * @implements {lunr.PipelineFunction} + * @params {lunr.Token} token - A token to check for being a stop word. + * @returns {lunr.Token} + * @see {@link lunr.Pipeline} + */ +lunr.stopWordFilter = lunr.generateStopWordFilter([ + 'a', + 'able', + 'about', + 'across', + 'after', + 'all', + 'almost', + 'also', + 'am', + 'among', + 'an', + 'and', + 'any', + 'are', + 'as', + 'at', + 'be', + 'because', + 'been', + 'but', + 'by', + 'can', + 'cannot', + 'could', + 'dear', + 'did', + 'do', + 'does', + 'either', + 'else', + 'ever', + 'every', + 'for', + 'from', + 'get', + 'got', + 'had', + 'has', + 'have', + 'he', + 'her', + 'hers', + 'him', + 'his', + 'how', + 'however', + 'i', + 'if', + 'in', + 'into', + 'is', + 'it', + 'its', + 'just', + 'least', + 'let', + 'like', + 'likely', + 'may', + 'me', + 'might', + 'most', + 'must', + 'my', + 'neither', + 'no', + 'nor', + 'not', + 'of', + 'off', + 'often', + 'on', + 'only', + 'or', + 'other', + 'our', + 'own', + 'rather', + 'said', + 'say', + 'says', + 'she', + 'should', + 'since', + 'so', + 'some', + 'than', + 'that', + 'the', + 'their', + 'them', + 'then', + 'there', + 'these', + 'they', + 'this', + 'tis', + 'to', + 'too', + 'twas', + 'us', + 'wants', + 'was', + 'we', + 'were', + 'what', + 'when', + 'where', + 'which', + 'while', + 'who', + 'whom', + 'why', + 'will', + 'with', + 'would', + 'yet', + 'you', + 'your' +]) + +lunr.Pipeline.registerFunction(lunr.stopWordFilter, 'stopWordFilter') +/*! + * lunr.trimmer + * Copyright (C) 2020 Oliver Nightingale + */ + +/** + * lunr.trimmer is a pipeline function for trimming non word + * characters from the beginning and end of tokens before they + * enter the index. + * + * This implementation may not work correctly for non latin + * characters and should either be removed or adapted for use + * with languages with non-latin characters. + * + * @static + * @implements {lunr.PipelineFunction} + * @param {lunr.Token} token The token to pass through the filter + * @returns {lunr.Token} + * @see lunr.Pipeline + */ +lunr.trimmer = function (token) { + return token.update(function (s) { + return s.replace(/^\W+/, '').replace(/\W+$/, '') + }) +} + +lunr.Pipeline.registerFunction(lunr.trimmer, 'trimmer') +/*! + * lunr.TokenSet + * Copyright (C) 2020 Oliver Nightingale + */ + +/** + * A token set is used to store the unique list of all tokens + * within an index. Token sets are also used to represent an + * incoming query to the index, this query token set and index + * token set are then intersected to find which tokens to look + * up in the inverted index. + * + * A token set can hold multiple tokens, as in the case of the + * index token set, or it can hold a single token as in the + * case of a simple query token set. + * + * Additionally token sets are used to perform wildcard matching. + * Leading, contained and trailing wildcards are supported, and + * from this edit distance matching can also be provided. + * + * Token sets are implemented as a minimal finite state automata, + * where both common prefixes and suffixes are shared between tokens. + * This helps to reduce the space used for storing the token set. + * + * @constructor + */ +lunr.TokenSet = function () { + this.final = false + this.edges = {} + this.id = lunr.TokenSet._nextId + lunr.TokenSet._nextId += 1 +} + +/** + * Keeps track of the next, auto increment, identifier to assign + * to a new tokenSet. + * + * TokenSets require a unique identifier to be correctly minimised. + * + * @private + */ +lunr.TokenSet._nextId = 1 + +/** + * Creates a TokenSet instance from the given sorted array of words. + * + * @param {String[]} arr - A sorted array of strings to create the set from. + * @returns {lunr.TokenSet} + * @throws Will throw an error if the input array is not sorted. + */ +lunr.TokenSet.fromArray = function (arr) { + var builder = new lunr.TokenSet.Builder + + for (var i = 0, len = arr.length; i < len; i++) { + builder.insert(arr[i]) + } + + builder.finish() + return builder.root +} + +/** + * Creates a token set from a query clause. + * + * @private + * @param {Object} clause - A single clause from lunr.Query. + * @param {string} clause.term - The query clause term. + * @param {number} [clause.editDistance] - The optional edit distance for the term. + * @returns {lunr.TokenSet} + */ +lunr.TokenSet.fromClause = function (clause) { + if ('editDistance' in clause) { + return lunr.TokenSet.fromFuzzyString(clause.term, clause.editDistance) + } else { + return lunr.TokenSet.fromString(clause.term) + } +} + +/** + * Creates a token set representing a single string with a specified + * edit distance. + * + * Insertions, deletions, substitutions and transpositions are each + * treated as an edit distance of 1. + * + * Increasing the allowed edit distance will have a dramatic impact + * on the performance of both creating and intersecting these TokenSets. + * It is advised to keep the edit distance less than 3. + * + * @param {string} str - The string to create the token set from. + * @param {number} editDistance - The allowed edit distance to match. + * @returns {lunr.Vector} + */ +lunr.TokenSet.fromFuzzyString = function (str, editDistance) { + var root = new lunr.TokenSet + + var stack = [{ + node: root, + editsRemaining: editDistance, + str: str + }] + + while (stack.length) { + var frame = stack.pop() + + // no edit + if (frame.str.length > 0) { + var char = frame.str.charAt(0), + noEditNode + + if (char in frame.node.edges) { + noEditNode = frame.node.edges[char] + } else { + noEditNode = new lunr.TokenSet + frame.node.edges[char] = noEditNode + } + + if (frame.str.length == 1) { + noEditNode.final = true + } + + stack.push({ + node: noEditNode, + editsRemaining: frame.editsRemaining, + str: frame.str.slice(1) + }) + } + + if (frame.editsRemaining == 0) { + continue + } + + // insertion + if ("*" in frame.node.edges) { + var insertionNode = frame.node.edges["*"] + } else { + var insertionNode = new lunr.TokenSet + frame.node.edges["*"] = insertionNode + } + + if (frame.str.length == 0) { + insertionNode.final = true + } + + stack.push({ + node: insertionNode, + editsRemaining: frame.editsRemaining - 1, + str: frame.str + }) + + // deletion + // can only do a deletion if we have enough edits remaining + // and if there are characters left to delete in the string + if (frame.str.length > 1) { + stack.push({ + node: frame.node, + editsRemaining: frame.editsRemaining - 1, + str: frame.str.slice(1) + }) + } + + // deletion + // just removing the last character from the str + if (frame.str.length == 1) { + frame.node.final = true + } + + // substitution + // can only do a substitution if we have enough edits remaining + // and if there are characters left to substitute + if (frame.str.length >= 1) { + if ("*" in frame.node.edges) { + var substitutionNode = frame.node.edges["*"] + } else { + var substitutionNode = new lunr.TokenSet + frame.node.edges["*"] = substitutionNode + } + + if (frame.str.length == 1) { + substitutionNode.final = true + } + + stack.push({ + node: substitutionNode, + editsRemaining: frame.editsRemaining - 1, + str: frame.str.slice(1) + }) + } + + // transposition + // can only do a transposition if there are edits remaining + // and there are enough characters to transpose + if (frame.str.length > 1) { + var charA = frame.str.charAt(0), + charB = frame.str.charAt(1), + transposeNode + + if (charB in frame.node.edges) { + transposeNode = frame.node.edges[charB] + } else { + transposeNode = new lunr.TokenSet + frame.node.edges[charB] = transposeNode + } + + if (frame.str.length == 1) { + transposeNode.final = true + } + + stack.push({ + node: transposeNode, + editsRemaining: frame.editsRemaining - 1, + str: charA + frame.str.slice(2) + }) + } + } + + return root +} + +/** + * Creates a TokenSet from a string. + * + * The string may contain one or more wildcard characters (*) + * that will allow wildcard matching when intersecting with + * another TokenSet. + * + * @param {string} str - The string to create a TokenSet from. + * @returns {lunr.TokenSet} + */ +lunr.TokenSet.fromString = function (str) { + var node = new lunr.TokenSet, + root = node + + /* + * Iterates through all characters within the passed string + * appending a node for each character. + * + * When a wildcard character is found then a self + * referencing edge is introduced to continually match + * any number of any characters. + */ + for (var i = 0, len = str.length; i < len; i++) { + var char = str[i], + final = (i == len - 1) + + if (char == "*") { + node.edges[char] = node + node.final = final + + } else { + var next = new lunr.TokenSet + next.final = final + + node.edges[char] = next + node = next + } + } + + return root +} + +/** + * Converts this TokenSet into an array of strings + * contained within the TokenSet. + * + * This is not intended to be used on a TokenSet that + * contains wildcards, in these cases the results are + * undefined and are likely to cause an infinite loop. + * + * @returns {string[]} + */ +lunr.TokenSet.prototype.toArray = function () { + var words = [] + + var stack = [{ + prefix: "", + node: this + }] + + while (stack.length) { + var frame = stack.pop(), + edges = Object.keys(frame.node.edges), + len = edges.length + + if (frame.node.final) { + /* In Safari, at this point the prefix is sometimes corrupted, see: + * https://github.com/olivernn/lunr.js/issues/279 Calling any + * String.prototype method forces Safari to "cast" this string to what + * it's supposed to be, fixing the bug. */ + frame.prefix.charAt(0) + words.push(frame.prefix) + } + + for (var i = 0; i < len; i++) { + var edge = edges[i] + + stack.push({ + prefix: frame.prefix.concat(edge), + node: frame.node.edges[edge] + }) + } + } + + return words +} + +/** + * Generates a string representation of a TokenSet. + * + * This is intended to allow TokenSets to be used as keys + * in objects, largely to aid the construction and minimisation + * of a TokenSet. As such it is not designed to be a human + * friendly representation of the TokenSet. + * + * @returns {string} + */ +lunr.TokenSet.prototype.toString = function () { + // NOTE: Using Object.keys here as this.edges is very likely + // to enter 'hash-mode' with many keys being added + // + // avoiding a for-in loop here as it leads to the function + // being de-optimised (at least in V8). From some simple + // benchmarks the performance is comparable, but allowing + // V8 to optimize may mean easy performance wins in the future. + + if (this._str) { + return this._str + } + + var str = this.final ? '1' : '0', + labels = Object.keys(this.edges).sort(), + len = labels.length + + for (var i = 0; i < len; i++) { + var label = labels[i], + node = this.edges[label] + + str = str + label + node.id + } + + return str +} + +/** + * Returns a new TokenSet that is the intersection of + * this TokenSet and the passed TokenSet. + * + * This intersection will take into account any wildcards + * contained within the TokenSet. + * + * @param {lunr.TokenSet} b - An other TokenSet to intersect with. + * @returns {lunr.TokenSet} + */ +lunr.TokenSet.prototype.intersect = function (b) { + var output = new lunr.TokenSet, + frame = undefined + + var stack = [{ + qNode: b, + output: output, + node: this + }] + + while (stack.length) { + frame = stack.pop() + + // NOTE: As with the #toString method, we are using + // Object.keys and a for loop instead of a for-in loop + // as both of these objects enter 'hash' mode, causing + // the function to be de-optimised in V8 + var qEdges = Object.keys(frame.qNode.edges), + qLen = qEdges.length, + nEdges = Object.keys(frame.node.edges), + nLen = nEdges.length + + for (var q = 0; q < qLen; q++) { + var qEdge = qEdges[q] + + for (var n = 0; n < nLen; n++) { + var nEdge = nEdges[n] + + if (nEdge == qEdge || qEdge == '*') { + var node = frame.node.edges[nEdge], + qNode = frame.qNode.edges[qEdge], + final = node.final && qNode.final, + next = undefined + + if (nEdge in frame.output.edges) { + // an edge already exists for this character + // no need to create a new node, just set the finality + // bit unless this node is already final + next = frame.output.edges[nEdge] + next.final = next.final || final + + } else { + // no edge exists yet, must create one + // set the finality bit and insert it + // into the output + next = new lunr.TokenSet + next.final = final + frame.output.edges[nEdge] = next + } + + stack.push({ + qNode: qNode, + output: next, + node: node + }) + } + } + } + } + + return output +} +lunr.TokenSet.Builder = function () { + this.previousWord = "" + this.root = new lunr.TokenSet + this.uncheckedNodes = [] + this.minimizedNodes = {} +} + +lunr.TokenSet.Builder.prototype.insert = function (word) { + var node, + commonPrefix = 0 + + if (word < this.previousWord) { + throw new Error ("Out of order word insertion") + } + + for (var i = 0; i < word.length && i < this.previousWord.length; i++) { + if (word[i] != this.previousWord[i]) break + commonPrefix++ + } + + this.minimize(commonPrefix) + + if (this.uncheckedNodes.length == 0) { + node = this.root + } else { + node = this.uncheckedNodes[this.uncheckedNodes.length - 1].child + } + + for (var i = commonPrefix; i < word.length; i++) { + var nextNode = new lunr.TokenSet, + char = word[i] + + node.edges[char] = nextNode + + this.uncheckedNodes.push({ + parent: node, + char: char, + child: nextNode + }) + + node = nextNode + } + + node.final = true + this.previousWord = word +} + +lunr.TokenSet.Builder.prototype.finish = function () { + this.minimize(0) +} + +lunr.TokenSet.Builder.prototype.minimize = function (downTo) { + for (var i = this.uncheckedNodes.length - 1; i >= downTo; i--) { + var node = this.uncheckedNodes[i], + childKey = node.child.toString() + + if (childKey in this.minimizedNodes) { + node.parent.edges[node.char] = this.minimizedNodes[childKey] + } else { + // Cache the key for this node since + // we know it can't change anymore + node.child._str = childKey + + this.minimizedNodes[childKey] = node.child + } + + this.uncheckedNodes.pop() + } +} +/*! + * lunr.Index + * Copyright (C) 2020 Oliver Nightingale + */ + +/** + * An index contains the built index of all documents and provides a query interface + * to the index. + * + * Usually instances of lunr.Index will not be created using this constructor, instead + * lunr.Builder should be used to construct new indexes, or lunr.Index.load should be + * used to load previously built and serialized indexes. + * + * @constructor + * @param {Object} attrs - The attributes of the built search index. + * @param {Object} attrs.invertedIndex - An index of term/field to document reference. + * @param {Object} attrs.fieldVectors - Field vectors + * @param {lunr.TokenSet} attrs.tokenSet - An set of all corpus tokens. + * @param {string[]} attrs.fields - The names of indexed document fields. + * @param {lunr.Pipeline} attrs.pipeline - The pipeline to use for search terms. + */ +lunr.Index = function (attrs) { + this.invertedIndex = attrs.invertedIndex + this.fieldVectors = attrs.fieldVectors + this.tokenSet = attrs.tokenSet + this.fields = attrs.fields + this.pipeline = attrs.pipeline +} + +/** + * A result contains details of a document matching a search query. + * @typedef {Object} lunr.Index~Result + * @property {string} ref - The reference of the document this result represents. + * @property {number} score - A number between 0 and 1 representing how similar this document is to the query. + * @property {lunr.MatchData} matchData - Contains metadata about this match including which term(s) caused the match. + */ + +/** + * Although lunr provides the ability to create queries using lunr.Query, it also provides a simple + * query language which itself is parsed into an instance of lunr.Query. + * + * For programmatically building queries it is advised to directly use lunr.Query, the query language + * is best used for human entered text rather than program generated text. + * + * At its simplest queries can just be a single term, e.g. `hello`, multiple terms are also supported + * and will be combined with OR, e.g `hello world` will match documents that contain either 'hello' + * or 'world', though those that contain both will rank higher in the results. + * + * Wildcards can be included in terms to match one or more unspecified characters, these wildcards can + * be inserted anywhere within the term, and more than one wildcard can exist in a single term. Adding + * wildcards will increase the number of documents that will be found but can also have a negative + * impact on query performance, especially with wildcards at the beginning of a term. + * + * Terms can be restricted to specific fields, e.g. `title:hello`, only documents with the term + * hello in the title field will match this query. Using a field not present in the index will lead + * to an error being thrown. + * + * Modifiers can also be added to terms, lunr supports edit distance and boost modifiers on terms. A term + * boost will make documents matching that term score higher, e.g. `foo^5`. Edit distance is also supported + * to provide fuzzy matching, e.g. 'hello~2' will match documents with hello with an edit distance of 2. + * Avoid large values for edit distance to improve query performance. + * + * Each term also supports a presence modifier. By default a term's presence in document is optional, however + * this can be changed to either required or prohibited. For a term's presence to be required in a document the + * term should be prefixed with a '+', e.g. `+foo bar` is a search for documents that must contain 'foo' and + * optionally contain 'bar'. Conversely a leading '-' sets the terms presence to prohibited, i.e. it must not + * appear in a document, e.g. `-foo bar` is a search for documents that do not contain 'foo' but may contain 'bar'. + * + * To escape special characters the backslash character '\' can be used, this allows searches to include + * characters that would normally be considered modifiers, e.g. `foo\~2` will search for a term "foo~2" instead + * of attempting to apply a boost of 2 to the search term "foo". + * + * @typedef {string} lunr.Index~QueryString + * @example Simple single term query + * hello + * @example Multiple term query + * hello world + * @example term scoped to a field + * title:hello + * @example term with a boost of 10 + * hello^10 + * @example term with an edit distance of 2 + * hello~2 + * @example terms with presence modifiers + * -foo +bar baz + */ + +/** + * Performs a search against the index using lunr query syntax. + * + * Results will be returned sorted by their score, the most relevant results + * will be returned first. For details on how the score is calculated, please see + * the {@link https://lunrjs.com/guides/searching.html#scoring|guide}. + * + * For more programmatic querying use lunr.Index#query. + * + * @param {lunr.Index~QueryString} queryString - A string containing a lunr query. + * @throws {lunr.QueryParseError} If the passed query string cannot be parsed. + * @returns {lunr.Index~Result[]} + */ +lunr.Index.prototype.search = function (queryString) { + return this.query(function (query) { + var parser = new lunr.QueryParser(queryString, query) + parser.parse() + }) +} + +/** + * A query builder callback provides a query object to be used to express + * the query to perform on the index. + * + * @callback lunr.Index~queryBuilder + * @param {lunr.Query} query - The query object to build up. + * @this lunr.Query + */ + +/** + * Performs a query against the index using the yielded lunr.Query object. + * + * If performing programmatic queries against the index, this method is preferred + * over lunr.Index#search so as to avoid the additional query parsing overhead. + * + * A query object is yielded to the supplied function which should be used to + * express the query to be run against the index. + * + * Note that although this function takes a callback parameter it is _not_ an + * asynchronous operation, the callback is just yielded a query object to be + * customized. + * + * @param {lunr.Index~queryBuilder} fn - A function that is used to build the query. + * @returns {lunr.Index~Result[]} + */ +lunr.Index.prototype.query = function (fn) { + // for each query clause + // * process terms + // * expand terms from token set + // * find matching documents and metadata + // * get document vectors + // * score documents + + var query = new lunr.Query(this.fields), + matchingFields = Object.create(null), + queryVectors = Object.create(null), + termFieldCache = Object.create(null), + requiredMatches = Object.create(null), + prohibitedMatches = Object.create(null) + + /* + * To support field level boosts a query vector is created per + * field. An empty vector is eagerly created to support negated + * queries. + */ + for (var i = 0; i < this.fields.length; i++) { + queryVectors[this.fields[i]] = new lunr.Vector + } + + fn.call(query, query) + + for (var i = 0; i < query.clauses.length; i++) { + /* + * Unless the pipeline has been disabled for this term, which is + * the case for terms with wildcards, we need to pass the clause + * term through the search pipeline. A pipeline returns an array + * of processed terms. Pipeline functions may expand the passed + * term, which means we may end up performing multiple index lookups + * for a single query term. + */ + var clause = query.clauses[i], + terms = null, + clauseMatches = lunr.Set.empty + + if (clause.usePipeline) { + terms = this.pipeline.runString(clause.term, { + fields: clause.fields + }) + } else { + terms = [clause.term] + } + + for (var m = 0; m < terms.length; m++) { + var term = terms[m] + + /* + * Each term returned from the pipeline needs to use the same query + * clause object, e.g. the same boost and or edit distance. The + * simplest way to do this is to re-use the clause object but mutate + * its term property. + */ + clause.term = term + + /* + * From the term in the clause we create a token set which will then + * be used to intersect the indexes token set to get a list of terms + * to lookup in the inverted index + */ + var termTokenSet = lunr.TokenSet.fromClause(clause), + expandedTerms = this.tokenSet.intersect(termTokenSet).toArray() + + /* + * If a term marked as required does not exist in the tokenSet it is + * impossible for the search to return any matches. We set all the field + * scoped required matches set to empty and stop examining any further + * clauses. + */ + if (expandedTerms.length === 0 && clause.presence === lunr.Query.presence.REQUIRED) { + for (var k = 0; k < clause.fields.length; k++) { + var field = clause.fields[k] + requiredMatches[field] = lunr.Set.empty + } + + break + } + + for (var j = 0; j < expandedTerms.length; j++) { + /* + * For each term get the posting and termIndex, this is required for + * building the query vector. + */ + var expandedTerm = expandedTerms[j], + posting = this.invertedIndex[expandedTerm], + termIndex = posting._index + + for (var k = 0; k < clause.fields.length; k++) { + /* + * For each field that this query term is scoped by (by default + * all fields are in scope) we need to get all the document refs + * that have this term in that field. + * + * The posting is the entry in the invertedIndex for the matching + * term from above. + */ + var field = clause.fields[k], + fieldPosting = posting[field], + matchingDocumentRefs = Object.keys(fieldPosting), + termField = expandedTerm + "/" + field, + matchingDocumentsSet = new lunr.Set(matchingDocumentRefs) + + /* + * if the presence of this term is required ensure that the matching + * documents are added to the set of required matches for this clause. + * + */ + if (clause.presence == lunr.Query.presence.REQUIRED) { + clauseMatches = clauseMatches.union(matchingDocumentsSet) + + if (requiredMatches[field] === undefined) { + requiredMatches[field] = lunr.Set.complete + } + } + + /* + * if the presence of this term is prohibited ensure that the matching + * documents are added to the set of prohibited matches for this field, + * creating that set if it does not yet exist. + */ + if (clause.presence == lunr.Query.presence.PROHIBITED) { + if (prohibitedMatches[field] === undefined) { + prohibitedMatches[field] = lunr.Set.empty + } + + prohibitedMatches[field] = prohibitedMatches[field].union(matchingDocumentsSet) + + /* + * Prohibited matches should not be part of the query vector used for + * similarity scoring and no metadata should be extracted so we continue + * to the next field + */ + continue + } + + /* + * The query field vector is populated using the termIndex found for + * the term and a unit value with the appropriate boost applied. + * Using upsert because there could already be an entry in the vector + * for the term we are working with. In that case we just add the scores + * together. + */ + queryVectors[field].upsert(termIndex, clause.boost, function (a, b) { return a + b }) + + /** + * If we've already seen this term, field combo then we've already collected + * the matching documents and metadata, no need to go through all that again + */ + if (termFieldCache[termField]) { + continue + } + + for (var l = 0; l < matchingDocumentRefs.length; l++) { + /* + * All metadata for this term/field/document triple + * are then extracted and collected into an instance + * of lunr.MatchData ready to be returned in the query + * results + */ + var matchingDocumentRef = matchingDocumentRefs[l], + matchingFieldRef = new lunr.FieldRef (matchingDocumentRef, field), + metadata = fieldPosting[matchingDocumentRef], + fieldMatch + + if ((fieldMatch = matchingFields[matchingFieldRef]) === undefined) { + matchingFields[matchingFieldRef] = new lunr.MatchData (expandedTerm, field, metadata) + } else { + fieldMatch.add(expandedTerm, field, metadata) + } + + } + + termFieldCache[termField] = true + } + } + } + + /** + * If the presence was required we need to update the requiredMatches field sets. + * We do this after all fields for the term have collected their matches because + * the clause terms presence is required in _any_ of the fields not _all_ of the + * fields. + */ + if (clause.presence === lunr.Query.presence.REQUIRED) { + for (var k = 0; k < clause.fields.length; k++) { + var field = clause.fields[k] + requiredMatches[field] = requiredMatches[field].intersect(clauseMatches) + } + } + } + + /** + * Need to combine the field scoped required and prohibited + * matching documents into a global set of required and prohibited + * matches + */ + var allRequiredMatches = lunr.Set.complete, + allProhibitedMatches = lunr.Set.empty + + for (var i = 0; i < this.fields.length; i++) { + var field = this.fields[i] + + if (requiredMatches[field]) { + allRequiredMatches = allRequiredMatches.intersect(requiredMatches[field]) + } + + if (prohibitedMatches[field]) { + allProhibitedMatches = allProhibitedMatches.union(prohibitedMatches[field]) + } + } + + var matchingFieldRefs = Object.keys(matchingFields), + results = [], + matches = Object.create(null) + + /* + * If the query is negated (contains only prohibited terms) + * we need to get _all_ fieldRefs currently existing in the + * index. This is only done when we know that the query is + * entirely prohibited terms to avoid any cost of getting all + * fieldRefs unnecessarily. + * + * Additionally, blank MatchData must be created to correctly + * populate the results. + */ + if (query.isNegated()) { + matchingFieldRefs = Object.keys(this.fieldVectors) + + for (var i = 0; i < matchingFieldRefs.length; i++) { + var matchingFieldRef = matchingFieldRefs[i] + var fieldRef = lunr.FieldRef.fromString(matchingFieldRef) + matchingFields[matchingFieldRef] = new lunr.MatchData + } + } + + for (var i = 0; i < matchingFieldRefs.length; i++) { + /* + * Currently we have document fields that match the query, but we + * need to return documents. The matchData and scores are combined + * from multiple fields belonging to the same document. + * + * Scores are calculated by field, using the query vectors created + * above, and combined into a final document score using addition. + */ + var fieldRef = lunr.FieldRef.fromString(matchingFieldRefs[i]), + docRef = fieldRef.docRef + + if (!allRequiredMatches.contains(docRef)) { + continue + } + + if (allProhibitedMatches.contains(docRef)) { + continue + } + + var fieldVector = this.fieldVectors[fieldRef], + score = queryVectors[fieldRef.fieldName].similarity(fieldVector), + docMatch + + if ((docMatch = matches[docRef]) !== undefined) { + docMatch.score += score + docMatch.matchData.combine(matchingFields[fieldRef]) + } else { + var match = { + ref: docRef, + score: score, + matchData: matchingFields[fieldRef] + } + matches[docRef] = match + results.push(match) + } + } + + /* + * Sort the results objects by score, highest first. + */ + return results.sort(function (a, b) { + return b.score - a.score + }) +} + +/** + * Prepares the index for JSON serialization. + * + * The schema for this JSON blob will be described in a + * separate JSON schema file. + * + * @returns {Object} + */ +lunr.Index.prototype.toJSON = function () { + var invertedIndex = Object.keys(this.invertedIndex) + .sort() + .map(function (term) { + return [term, this.invertedIndex[term]] + }, this) + + var fieldVectors = Object.keys(this.fieldVectors) + .map(function (ref) { + return [ref, this.fieldVectors[ref].toJSON()] + }, this) + + return { + version: lunr.version, + fields: this.fields, + fieldVectors: fieldVectors, + invertedIndex: invertedIndex, + pipeline: this.pipeline.toJSON() + } +} + +/** + * Loads a previously serialized lunr.Index + * + * @param {Object} serializedIndex - A previously serialized lunr.Index + * @returns {lunr.Index} + */ +lunr.Index.load = function (serializedIndex) { + var attrs = {}, + fieldVectors = {}, + serializedVectors = serializedIndex.fieldVectors, + invertedIndex = Object.create(null), + serializedInvertedIndex = serializedIndex.invertedIndex, + tokenSetBuilder = new lunr.TokenSet.Builder, + pipeline = lunr.Pipeline.load(serializedIndex.pipeline) + + if (serializedIndex.version != lunr.version) { + lunr.utils.warn("Version mismatch when loading serialised index. Current version of lunr '" + lunr.version + "' does not match serialized index '" + serializedIndex.version + "'") + } + + for (var i = 0; i < serializedVectors.length; i++) { + var tuple = serializedVectors[i], + ref = tuple[0], + elements = tuple[1] + + fieldVectors[ref] = new lunr.Vector(elements) + } + + for (var i = 0; i < serializedInvertedIndex.length; i++) { + var tuple = serializedInvertedIndex[i], + term = tuple[0], + posting = tuple[1] + + tokenSetBuilder.insert(term) + invertedIndex[term] = posting + } + + tokenSetBuilder.finish() + + attrs.fields = serializedIndex.fields + + attrs.fieldVectors = fieldVectors + attrs.invertedIndex = invertedIndex + attrs.tokenSet = tokenSetBuilder.root + attrs.pipeline = pipeline + + return new lunr.Index(attrs) +} +/*! + * lunr.Builder + * Copyright (C) 2020 Oliver Nightingale + */ + +/** + * lunr.Builder performs indexing on a set of documents and + * returns instances of lunr.Index ready for querying. + * + * All configuration of the index is done via the builder, the + * fields to index, the document reference, the text processing + * pipeline and document scoring parameters are all set on the + * builder before indexing. + * + * @constructor + * @property {string} _ref - Internal reference to the document reference field. + * @property {string[]} _fields - Internal reference to the document fields to index. + * @property {object} invertedIndex - The inverted index maps terms to document fields. + * @property {object} documentTermFrequencies - Keeps track of document term frequencies. + * @property {object} documentLengths - Keeps track of the length of documents added to the index. + * @property {lunr.tokenizer} tokenizer - Function for splitting strings into tokens for indexing. + * @property {lunr.Pipeline} pipeline - The pipeline performs text processing on tokens before indexing. + * @property {lunr.Pipeline} searchPipeline - A pipeline for processing search terms before querying the index. + * @property {number} documentCount - Keeps track of the total number of documents indexed. + * @property {number} _b - A parameter to control field length normalization, setting this to 0 disabled normalization, 1 fully normalizes field lengths, the default value is 0.75. + * @property {number} _k1 - A parameter to control how quickly an increase in term frequency results in term frequency saturation, the default value is 1.2. + * @property {number} termIndex - A counter incremented for each unique term, used to identify a terms position in the vector space. + * @property {array} metadataWhitelist - A list of metadata keys that have been whitelisted for entry in the index. + */ +lunr.Builder = function () { + this._ref = "id" + this._fields = Object.create(null) + this._documents = Object.create(null) + this.invertedIndex = Object.create(null) + this.fieldTermFrequencies = {} + this.fieldLengths = {} + this.tokenizer = lunr.tokenizer + this.pipeline = new lunr.Pipeline + this.searchPipeline = new lunr.Pipeline + this.documentCount = 0 + this._b = 0.75 + this._k1 = 1.2 + this.termIndex = 0 + this.metadataWhitelist = [] +} + +/** + * Sets the document field used as the document reference. Every document must have this field. + * The type of this field in the document should be a string, if it is not a string it will be + * coerced into a string by calling toString. + * + * The default ref is 'id'. + * + * The ref should _not_ be changed during indexing, it should be set before any documents are + * added to the index. Changing it during indexing can lead to inconsistent results. + * + * @param {string} ref - The name of the reference field in the document. + */ +lunr.Builder.prototype.ref = function (ref) { + this._ref = ref +} + +/** + * A function that is used to extract a field from a document. + * + * Lunr expects a field to be at the top level of a document, if however the field + * is deeply nested within a document an extractor function can be used to extract + * the right field for indexing. + * + * @callback fieldExtractor + * @param {object} doc - The document being added to the index. + * @returns {?(string|object|object[])} obj - The object that will be indexed for this field. + * @example Extracting a nested field + * function (doc) { return doc.nested.field } + */ + +/** + * Adds a field to the list of document fields that will be indexed. Every document being + * indexed should have this field. Null values for this field in indexed documents will + * not cause errors but will limit the chance of that document being retrieved by searches. + * + * All fields should be added before adding documents to the index. Adding fields after + * a document has been indexed will have no effect on already indexed documents. + * + * Fields can be boosted at build time. This allows terms within that field to have more + * importance when ranking search results. Use a field boost to specify that matches within + * one field are more important than other fields. + * + * @param {string} fieldName - The name of a field to index in all documents. + * @param {object} attributes - Optional attributes associated with this field. + * @param {number} [attributes.boost=1] - Boost applied to all terms within this field. + * @param {fieldExtractor} [attributes.extractor] - Function to extract a field from a document. + * @throws {RangeError} fieldName cannot contain unsupported characters '/' + */ +lunr.Builder.prototype.field = function (fieldName, attributes) { + if (/\//.test(fieldName)) { + throw new RangeError ("Field '" + fieldName + "' contains illegal character '/'") + } + + this._fields[fieldName] = attributes || {} +} + +/** + * A parameter to tune the amount of field length normalisation that is applied when + * calculating relevance scores. A value of 0 will completely disable any normalisation + * and a value of 1 will fully normalise field lengths. The default is 0.75. Values of b + * will be clamped to the range 0 - 1. + * + * @param {number} number - The value to set for this tuning parameter. + */ +lunr.Builder.prototype.b = function (number) { + if (number < 0) { + this._b = 0 + } else if (number > 1) { + this._b = 1 + } else { + this._b = number + } +} + +/** + * A parameter that controls the speed at which a rise in term frequency results in term + * frequency saturation. The default value is 1.2. Setting this to a higher value will give + * slower saturation levels, a lower value will result in quicker saturation. + * + * @param {number} number - The value to set for this tuning parameter. + */ +lunr.Builder.prototype.k1 = function (number) { + this._k1 = number +} + +/** + * Adds a document to the index. + * + * Before adding fields to the index the index should have been fully setup, with the document + * ref and all fields to index already having been specified. + * + * The document must have a field name as specified by the ref (by default this is 'id') and + * it should have all fields defined for indexing, though null or undefined values will not + * cause errors. + * + * Entire documents can be boosted at build time. Applying a boost to a document indicates that + * this document should rank higher in search results than other documents. + * + * @param {object} doc - The document to add to the index. + * @param {object} attributes - Optional attributes associated with this document. + * @param {number} [attributes.boost=1] - Boost applied to all terms within this document. + */ +lunr.Builder.prototype.add = function (doc, attributes) { + var docRef = doc[this._ref], + fields = Object.keys(this._fields) + + this._documents[docRef] = attributes || {} + this.documentCount += 1 + + for (var i = 0; i < fields.length; i++) { + var fieldName = fields[i], + extractor = this._fields[fieldName].extractor, + field = extractor ? extractor(doc) : doc[fieldName], + tokens = this.tokenizer(field, { + fields: [fieldName] + }), + terms = this.pipeline.run(tokens), + fieldRef = new lunr.FieldRef (docRef, fieldName), + fieldTerms = Object.create(null) + + this.fieldTermFrequencies[fieldRef] = fieldTerms + this.fieldLengths[fieldRef] = 0 + + // store the length of this field for this document + this.fieldLengths[fieldRef] += terms.length + + // calculate term frequencies for this field + for (var j = 0; j < terms.length; j++) { + var term = terms[j] + + if (fieldTerms[term] == undefined) { + fieldTerms[term] = 0 + } + + fieldTerms[term] += 1 + + // add to inverted index + // create an initial posting if one doesn't exist + if (this.invertedIndex[term] == undefined) { + var posting = Object.create(null) + posting["_index"] = this.termIndex + this.termIndex += 1 + + for (var k = 0; k < fields.length; k++) { + posting[fields[k]] = Object.create(null) + } + + this.invertedIndex[term] = posting + } + + // add an entry for this term/fieldName/docRef to the invertedIndex + if (this.invertedIndex[term][fieldName][docRef] == undefined) { + this.invertedIndex[term][fieldName][docRef] = Object.create(null) + } + + // store all whitelisted metadata about this token in the + // inverted index + for (var l = 0; l < this.metadataWhitelist.length; l++) { + var metadataKey = this.metadataWhitelist[l], + metadata = term.metadata[metadataKey] + + if (this.invertedIndex[term][fieldName][docRef][metadataKey] == undefined) { + this.invertedIndex[term][fieldName][docRef][metadataKey] = [] + } + + this.invertedIndex[term][fieldName][docRef][metadataKey].push(metadata) + } + } + + } +} + +/** + * Calculates the average document length for this index + * + * @private + */ +lunr.Builder.prototype.calculateAverageFieldLengths = function () { + + var fieldRefs = Object.keys(this.fieldLengths), + numberOfFields = fieldRefs.length, + accumulator = {}, + documentsWithField = {} + + for (var i = 0; i < numberOfFields; i++) { + var fieldRef = lunr.FieldRef.fromString(fieldRefs[i]), + field = fieldRef.fieldName + + documentsWithField[field] || (documentsWithField[field] = 0) + documentsWithField[field] += 1 + + accumulator[field] || (accumulator[field] = 0) + accumulator[field] += this.fieldLengths[fieldRef] + } + + var fields = Object.keys(this._fields) + + for (var i = 0; i < fields.length; i++) { + var fieldName = fields[i] + accumulator[fieldName] = accumulator[fieldName] / documentsWithField[fieldName] + } + + this.averageFieldLength = accumulator +} + +/** + * Builds a vector space model of every document using lunr.Vector + * + * @private + */ +lunr.Builder.prototype.createFieldVectors = function () { + var fieldVectors = {}, + fieldRefs = Object.keys(this.fieldTermFrequencies), + fieldRefsLength = fieldRefs.length, + termIdfCache = Object.create(null) + + for (var i = 0; i < fieldRefsLength; i++) { + var fieldRef = lunr.FieldRef.fromString(fieldRefs[i]), + fieldName = fieldRef.fieldName, + fieldLength = this.fieldLengths[fieldRef], + fieldVector = new lunr.Vector, + termFrequencies = this.fieldTermFrequencies[fieldRef], + terms = Object.keys(termFrequencies), + termsLength = terms.length + + + var fieldBoost = this._fields[fieldName].boost || 1, + docBoost = this._documents[fieldRef.docRef].boost || 1 + + for (var j = 0; j < termsLength; j++) { + var term = terms[j], + tf = termFrequencies[term], + termIndex = this.invertedIndex[term]._index, + idf, score, scoreWithPrecision + + if (termIdfCache[term] === undefined) { + idf = lunr.idf(this.invertedIndex[term], this.documentCount) + termIdfCache[term] = idf + } else { + idf = termIdfCache[term] + } + + score = idf * ((this._k1 + 1) * tf) / (this._k1 * (1 - this._b + this._b * (fieldLength / this.averageFieldLength[fieldName])) + tf) + score *= fieldBoost + score *= docBoost + scoreWithPrecision = Math.round(score * 1000) / 1000 + // Converts 1.23456789 to 1.234. + // Reducing the precision so that the vectors take up less + // space when serialised. Doing it now so that they behave + // the same before and after serialisation. Also, this is + // the fastest approach to reducing a number's precision in + // JavaScript. + + fieldVector.insert(termIndex, scoreWithPrecision) + } + + fieldVectors[fieldRef] = fieldVector + } + + this.fieldVectors = fieldVectors +} + +/** + * Creates a token set of all tokens in the index using lunr.TokenSet + * + * @private + */ +lunr.Builder.prototype.createTokenSet = function () { + this.tokenSet = lunr.TokenSet.fromArray( + Object.keys(this.invertedIndex).sort() + ) +} + +/** + * Builds the index, creating an instance of lunr.Index. + * + * This completes the indexing process and should only be called + * once all documents have been added to the index. + * + * @returns {lunr.Index} + */ +lunr.Builder.prototype.build = function () { + this.calculateAverageFieldLengths() + this.createFieldVectors() + this.createTokenSet() + + return new lunr.Index({ + invertedIndex: this.invertedIndex, + fieldVectors: this.fieldVectors, + tokenSet: this.tokenSet, + fields: Object.keys(this._fields), + pipeline: this.searchPipeline + }) +} + +/** + * Applies a plugin to the index builder. + * + * A plugin is a function that is called with the index builder as its context. + * Plugins can be used to customise or extend the behaviour of the index + * in some way. A plugin is just a function, that encapsulated the custom + * behaviour that should be applied when building the index. + * + * The plugin function will be called with the index builder as its argument, additional + * arguments can also be passed when calling use. The function will be called + * with the index builder as its context. + * + * @param {Function} plugin The plugin to apply. + */ +lunr.Builder.prototype.use = function (fn) { + var args = Array.prototype.slice.call(arguments, 1) + args.unshift(this) + fn.apply(this, args) +} +/** + * Contains and collects metadata about a matching document. + * A single instance of lunr.MatchData is returned as part of every + * lunr.Index~Result. + * + * @constructor + * @param {string} term - The term this match data is associated with + * @param {string} field - The field in which the term was found + * @param {object} metadata - The metadata recorded about this term in this field + * @property {object} metadata - A cloned collection of metadata associated with this document. + * @see {@link lunr.Index~Result} + */ +lunr.MatchData = function (term, field, metadata) { + var clonedMetadata = Object.create(null), + metadataKeys = Object.keys(metadata || {}) + + // Cloning the metadata to prevent the original + // being mutated during match data combination. + // Metadata is kept in an array within the inverted + // index so cloning the data can be done with + // Array#slice + for (var i = 0; i < metadataKeys.length; i++) { + var key = metadataKeys[i] + clonedMetadata[key] = metadata[key].slice() + } + + this.metadata = Object.create(null) + + if (term !== undefined) { + this.metadata[term] = Object.create(null) + this.metadata[term][field] = clonedMetadata + } +} + +/** + * An instance of lunr.MatchData will be created for every term that matches a + * document. However only one instance is required in a lunr.Index~Result. This + * method combines metadata from another instance of lunr.MatchData with this + * objects metadata. + * + * @param {lunr.MatchData} otherMatchData - Another instance of match data to merge with this one. + * @see {@link lunr.Index~Result} + */ +lunr.MatchData.prototype.combine = function (otherMatchData) { + var terms = Object.keys(otherMatchData.metadata) + + for (var i = 0; i < terms.length; i++) { + var term = terms[i], + fields = Object.keys(otherMatchData.metadata[term]) + + if (this.metadata[term] == undefined) { + this.metadata[term] = Object.create(null) + } + + for (var j = 0; j < fields.length; j++) { + var field = fields[j], + keys = Object.keys(otherMatchData.metadata[term][field]) + + if (this.metadata[term][field] == undefined) { + this.metadata[term][field] = Object.create(null) + } + + for (var k = 0; k < keys.length; k++) { + var key = keys[k] + + if (this.metadata[term][field][key] == undefined) { + this.metadata[term][field][key] = otherMatchData.metadata[term][field][key] + } else { + this.metadata[term][field][key] = this.metadata[term][field][key].concat(otherMatchData.metadata[term][field][key]) + } + + } + } + } +} + +/** + * Add metadata for a term/field pair to this instance of match data. + * + * @param {string} term - The term this match data is associated with + * @param {string} field - The field in which the term was found + * @param {object} metadata - The metadata recorded about this term in this field + */ +lunr.MatchData.prototype.add = function (term, field, metadata) { + if (!(term in this.metadata)) { + this.metadata[term] = Object.create(null) + this.metadata[term][field] = metadata + return + } + + if (!(field in this.metadata[term])) { + this.metadata[term][field] = metadata + return + } + + var metadataKeys = Object.keys(metadata) + + for (var i = 0; i < metadataKeys.length; i++) { + var key = metadataKeys[i] + + if (key in this.metadata[term][field]) { + this.metadata[term][field][key] = this.metadata[term][field][key].concat(metadata[key]) + } else { + this.metadata[term][field][key] = metadata[key] + } + } +} +/** + * A lunr.Query provides a programmatic way of defining queries to be performed + * against a {@link lunr.Index}. + * + * Prefer constructing a lunr.Query using the {@link lunr.Index#query} method + * so the query object is pre-initialized with the right index fields. + * + * @constructor + * @property {lunr.Query~Clause[]} clauses - An array of query clauses. + * @property {string[]} allFields - An array of all available fields in a lunr.Index. + */ +lunr.Query = function (allFields) { + this.clauses = [] + this.allFields = allFields +} + +/** + * Constants for indicating what kind of automatic wildcard insertion will be used when constructing a query clause. + * + * This allows wildcards to be added to the beginning and end of a term without having to manually do any string + * concatenation. + * + * The wildcard constants can be bitwise combined to select both leading and trailing wildcards. + * + * @constant + * @default + * @property {number} wildcard.NONE - The term will have no wildcards inserted, this is the default behaviour + * @property {number} wildcard.LEADING - Prepend the term with a wildcard, unless a leading wildcard already exists + * @property {number} wildcard.TRAILING - Append a wildcard to the term, unless a trailing wildcard already exists + * @see lunr.Query~Clause + * @see lunr.Query#clause + * @see lunr.Query#term + * @example query term with trailing wildcard + * query.term('foo', { wildcard: lunr.Query.wildcard.TRAILING }) + * @example query term with leading and trailing wildcard + * query.term('foo', { + * wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING + * }) + */ + +lunr.Query.wildcard = new String ("*") +lunr.Query.wildcard.NONE = 0 +lunr.Query.wildcard.LEADING = 1 +lunr.Query.wildcard.TRAILING = 2 + +/** + * Constants for indicating what kind of presence a term must have in matching documents. + * + * @constant + * @enum {number} + * @see lunr.Query~Clause + * @see lunr.Query#clause + * @see lunr.Query#term + * @example query term with required presence + * query.term('foo', { presence: lunr.Query.presence.REQUIRED }) + */ +lunr.Query.presence = { + /** + * Term's presence in a document is optional, this is the default value. + */ + OPTIONAL: 1, + + /** + * Term's presence in a document is required, documents that do not contain + * this term will not be returned. + */ + REQUIRED: 2, + + /** + * Term's presence in a document is prohibited, documents that do contain + * this term will not be returned. + */ + PROHIBITED: 3 +} + +/** + * A single clause in a {@link lunr.Query} contains a term and details on how to + * match that term against a {@link lunr.Index}. + * + * @typedef {Object} lunr.Query~Clause + * @property {string[]} fields - The fields in an index this clause should be matched against. + * @property {number} [boost=1] - Any boost that should be applied when matching this clause. + * @property {number} [editDistance] - Whether the term should have fuzzy matching applied, and how fuzzy the match should be. + * @property {boolean} [usePipeline] - Whether the term should be passed through the search pipeline. + * @property {number} [wildcard=lunr.Query.wildcard.NONE] - Whether the term should have wildcards appended or prepended. + * @property {number} [presence=lunr.Query.presence.OPTIONAL] - The terms presence in any matching documents. + */ + +/** + * Adds a {@link lunr.Query~Clause} to this query. + * + * Unless the clause contains the fields to be matched all fields will be matched. In addition + * a default boost of 1 is applied to the clause. + * + * @param {lunr.Query~Clause} clause - The clause to add to this query. + * @see lunr.Query~Clause + * @returns {lunr.Query} + */ +lunr.Query.prototype.clause = function (clause) { + if (!('fields' in clause)) { + clause.fields = this.allFields + } + + if (!('boost' in clause)) { + clause.boost = 1 + } + + if (!('usePipeline' in clause)) { + clause.usePipeline = true + } + + if (!('wildcard' in clause)) { + clause.wildcard = lunr.Query.wildcard.NONE + } + + if ((clause.wildcard & lunr.Query.wildcard.LEADING) && (clause.term.charAt(0) != lunr.Query.wildcard)) { + clause.term = "*" + clause.term + } + + if ((clause.wildcard & lunr.Query.wildcard.TRAILING) && (clause.term.slice(-1) != lunr.Query.wildcard)) { + clause.term = "" + clause.term + "*" + } + + if (!('presence' in clause)) { + clause.presence = lunr.Query.presence.OPTIONAL + } + + this.clauses.push(clause) + + return this +} + +/** + * A negated query is one in which every clause has a presence of + * prohibited. These queries require some special processing to return + * the expected results. + * + * @returns boolean + */ +lunr.Query.prototype.isNegated = function () { + for (var i = 0; i < this.clauses.length; i++) { + if (this.clauses[i].presence != lunr.Query.presence.PROHIBITED) { + return false + } + } + + return true +} + +/** + * Adds a term to the current query, under the covers this will create a {@link lunr.Query~Clause} + * to the list of clauses that make up this query. + * + * The term is used as is, i.e. no tokenization will be performed by this method. Instead conversion + * to a token or token-like string should be done before calling this method. + * + * The term will be converted to a string by calling `toString`. Multiple terms can be passed as an + * array, each term in the array will share the same options. + * + * @param {object|object[]} term - The term(s) to add to the query. + * @param {object} [options] - Any additional properties to add to the query clause. + * @returns {lunr.Query} + * @see lunr.Query#clause + * @see lunr.Query~Clause + * @example adding a single term to a query + * query.term("foo") + * @example adding a single term to a query and specifying search fields, term boost and automatic trailing wildcard + * query.term("foo", { + * fields: ["title"], + * boost: 10, + * wildcard: lunr.Query.wildcard.TRAILING + * }) + * @example using lunr.tokenizer to convert a string to tokens before using them as terms + * query.term(lunr.tokenizer("foo bar")) + */ +lunr.Query.prototype.term = function (term, options) { + if (Array.isArray(term)) { + term.forEach(function (t) { this.term(t, lunr.utils.clone(options)) }, this) + return this + } + + var clause = options || {} + clause.term = term.toString() + + this.clause(clause) + + return this +} +lunr.QueryParseError = function (message, start, end) { + this.name = "QueryParseError" + this.message = message + this.start = start + this.end = end +} + +lunr.QueryParseError.prototype = new Error +lunr.QueryLexer = function (str) { + this.lexemes = [] + this.str = str + this.length = str.length + this.pos = 0 + this.start = 0 + this.escapeCharPositions = [] +} + +lunr.QueryLexer.prototype.run = function () { + var state = lunr.QueryLexer.lexText + + while (state) { + state = state(this) + } +} + +lunr.QueryLexer.prototype.sliceString = function () { + var subSlices = [], + sliceStart = this.start, + sliceEnd = this.pos + + for (var i = 0; i < this.escapeCharPositions.length; i++) { + sliceEnd = this.escapeCharPositions[i] + subSlices.push(this.str.slice(sliceStart, sliceEnd)) + sliceStart = sliceEnd + 1 + } + + subSlices.push(this.str.slice(sliceStart, this.pos)) + this.escapeCharPositions.length = 0 + + return subSlices.join('') +} + +lunr.QueryLexer.prototype.emit = function (type) { + this.lexemes.push({ + type: type, + str: this.sliceString(), + start: this.start, + end: this.pos + }) + + this.start = this.pos +} + +lunr.QueryLexer.prototype.escapeCharacter = function () { + this.escapeCharPositions.push(this.pos - 1) + this.pos += 1 +} + +lunr.QueryLexer.prototype.next = function () { + if (this.pos >= this.length) { + return lunr.QueryLexer.EOS + } + + var char = this.str.charAt(this.pos) + this.pos += 1 + return char +} + +lunr.QueryLexer.prototype.width = function () { + return this.pos - this.start +} + +lunr.QueryLexer.prototype.ignore = function () { + if (this.start == this.pos) { + this.pos += 1 + } + + this.start = this.pos +} + +lunr.QueryLexer.prototype.backup = function () { + this.pos -= 1 +} + +lunr.QueryLexer.prototype.acceptDigitRun = function () { + var char, charCode + + do { + char = this.next() + charCode = char.charCodeAt(0) + } while (charCode > 47 && charCode < 58) + + if (char != lunr.QueryLexer.EOS) { + this.backup() + } +} + +lunr.QueryLexer.prototype.more = function () { + return this.pos < this.length +} + +lunr.QueryLexer.EOS = 'EOS' +lunr.QueryLexer.FIELD = 'FIELD' +lunr.QueryLexer.TERM = 'TERM' +lunr.QueryLexer.EDIT_DISTANCE = 'EDIT_DISTANCE' +lunr.QueryLexer.BOOST = 'BOOST' +lunr.QueryLexer.PRESENCE = 'PRESENCE' + +lunr.QueryLexer.lexField = function (lexer) { + lexer.backup() + lexer.emit(lunr.QueryLexer.FIELD) + lexer.ignore() + return lunr.QueryLexer.lexText +} + +lunr.QueryLexer.lexTerm = function (lexer) { + if (lexer.width() > 1) { + lexer.backup() + lexer.emit(lunr.QueryLexer.TERM) + } + + lexer.ignore() + + if (lexer.more()) { + return lunr.QueryLexer.lexText + } +} + +lunr.QueryLexer.lexEditDistance = function (lexer) { + lexer.ignore() + lexer.acceptDigitRun() + lexer.emit(lunr.QueryLexer.EDIT_DISTANCE) + return lunr.QueryLexer.lexText +} + +lunr.QueryLexer.lexBoost = function (lexer) { + lexer.ignore() + lexer.acceptDigitRun() + lexer.emit(lunr.QueryLexer.BOOST) + return lunr.QueryLexer.lexText +} + +lunr.QueryLexer.lexEOS = function (lexer) { + if (lexer.width() > 0) { + lexer.emit(lunr.QueryLexer.TERM) + } +} + +// This matches the separator used when tokenising fields +// within a document. These should match otherwise it is +// not possible to search for some tokens within a document. +// +// It is possible for the user to change the separator on the +// tokenizer so it _might_ clash with any other of the special +// characters already used within the search string, e.g. :. +// +// This means that it is possible to change the separator in +// such a way that makes some words unsearchable using a search +// string. +lunr.QueryLexer.termSeparator = lunr.tokenizer.separator + +lunr.QueryLexer.lexText = function (lexer) { + while (true) { + var char = lexer.next() + + if (char == lunr.QueryLexer.EOS) { + return lunr.QueryLexer.lexEOS + } + + // Escape character is '\' + if (char.charCodeAt(0) == 92) { + lexer.escapeCharacter() + continue + } + + if (char == ":") { + return lunr.QueryLexer.lexField + } + + if (char == "~") { + lexer.backup() + if (lexer.width() > 0) { + lexer.emit(lunr.QueryLexer.TERM) + } + return lunr.QueryLexer.lexEditDistance + } + + if (char == "^") { + lexer.backup() + if (lexer.width() > 0) { + lexer.emit(lunr.QueryLexer.TERM) + } + return lunr.QueryLexer.lexBoost + } + + // "+" indicates term presence is required + // checking for length to ensure that only + // leading "+" are considered + if (char == "+" && lexer.width() === 1) { + lexer.emit(lunr.QueryLexer.PRESENCE) + return lunr.QueryLexer.lexText + } + + // "-" indicates term presence is prohibited + // checking for length to ensure that only + // leading "-" are considered + if (char == "-" && lexer.width() === 1) { + lexer.emit(lunr.QueryLexer.PRESENCE) + return lunr.QueryLexer.lexText + } + + if (char.match(lunr.QueryLexer.termSeparator)) { + return lunr.QueryLexer.lexTerm + } + } +} + +lunr.QueryParser = function (str, query) { + this.lexer = new lunr.QueryLexer (str) + this.query = query + this.currentClause = {} + this.lexemeIdx = 0 +} + +lunr.QueryParser.prototype.parse = function () { + this.lexer.run() + this.lexemes = this.lexer.lexemes + + var state = lunr.QueryParser.parseClause + + while (state) { + state = state(this) + } + + return this.query +} + +lunr.QueryParser.prototype.peekLexeme = function () { + return this.lexemes[this.lexemeIdx] +} + +lunr.QueryParser.prototype.consumeLexeme = function () { + var lexeme = this.peekLexeme() + this.lexemeIdx += 1 + return lexeme +} + +lunr.QueryParser.prototype.nextClause = function () { + var completedClause = this.currentClause + this.query.clause(completedClause) + this.currentClause = {} +} + +lunr.QueryParser.parseClause = function (parser) { + var lexeme = parser.peekLexeme() + + if (lexeme == undefined) { + return + } + + switch (lexeme.type) { + case lunr.QueryLexer.PRESENCE: + return lunr.QueryParser.parsePresence + case lunr.QueryLexer.FIELD: + return lunr.QueryParser.parseField + case lunr.QueryLexer.TERM: + return lunr.QueryParser.parseTerm + default: + var errorMessage = "expected either a field or a term, found " + lexeme.type + + if (lexeme.str.length >= 1) { + errorMessage += " with value '" + lexeme.str + "'" + } + + throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end) + } +} + +lunr.QueryParser.parsePresence = function (parser) { + var lexeme = parser.consumeLexeme() + + if (lexeme == undefined) { + return + } + + switch (lexeme.str) { + case "-": + parser.currentClause.presence = lunr.Query.presence.PROHIBITED + break + case "+": + parser.currentClause.presence = lunr.Query.presence.REQUIRED + break + default: + var errorMessage = "unrecognised presence operator'" + lexeme.str + "'" + throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end) + } + + var nextLexeme = parser.peekLexeme() + + if (nextLexeme == undefined) { + var errorMessage = "expecting term or field, found nothing" + throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end) + } + + switch (nextLexeme.type) { + case lunr.QueryLexer.FIELD: + return lunr.QueryParser.parseField + case lunr.QueryLexer.TERM: + return lunr.QueryParser.parseTerm + default: + var errorMessage = "expecting term or field, found '" + nextLexeme.type + "'" + throw new lunr.QueryParseError (errorMessage, nextLexeme.start, nextLexeme.end) + } +} + +lunr.QueryParser.parseField = function (parser) { + var lexeme = parser.consumeLexeme() + + if (lexeme == undefined) { + return + } + + if (parser.query.allFields.indexOf(lexeme.str) == -1) { + var possibleFields = parser.query.allFields.map(function (f) { return "'" + f + "'" }).join(', '), + errorMessage = "unrecognised field '" + lexeme.str + "', possible fields: " + possibleFields + + throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end) + } + + parser.currentClause.fields = [lexeme.str] + + var nextLexeme = parser.peekLexeme() + + if (nextLexeme == undefined) { + var errorMessage = "expecting term, found nothing" + throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end) + } + + switch (nextLexeme.type) { + case lunr.QueryLexer.TERM: + return lunr.QueryParser.parseTerm + default: + var errorMessage = "expecting term, found '" + nextLexeme.type + "'" + throw new lunr.QueryParseError (errorMessage, nextLexeme.start, nextLexeme.end) + } +} + +lunr.QueryParser.parseTerm = function (parser) { + var lexeme = parser.consumeLexeme() + + if (lexeme == undefined) { + return + } + + parser.currentClause.term = lexeme.str.toLowerCase() + + if (lexeme.str.indexOf("*") != -1) { + parser.currentClause.usePipeline = false + } + + var nextLexeme = parser.peekLexeme() + + if (nextLexeme == undefined) { + parser.nextClause() + return + } + + switch (nextLexeme.type) { + case lunr.QueryLexer.TERM: + parser.nextClause() + return lunr.QueryParser.parseTerm + case lunr.QueryLexer.FIELD: + parser.nextClause() + return lunr.QueryParser.parseField + case lunr.QueryLexer.EDIT_DISTANCE: + return lunr.QueryParser.parseEditDistance + case lunr.QueryLexer.BOOST: + return lunr.QueryParser.parseBoost + case lunr.QueryLexer.PRESENCE: + parser.nextClause() + return lunr.QueryParser.parsePresence + default: + var errorMessage = "Unexpected lexeme type '" + nextLexeme.type + "'" + throw new lunr.QueryParseError (errorMessage, nextLexeme.start, nextLexeme.end) + } +} + +lunr.QueryParser.parseEditDistance = function (parser) { + var lexeme = parser.consumeLexeme() + + if (lexeme == undefined) { + return + } + + var editDistance = parseInt(lexeme.str, 10) + + if (isNaN(editDistance)) { + var errorMessage = "edit distance must be numeric" + throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end) + } + + parser.currentClause.editDistance = editDistance + + var nextLexeme = parser.peekLexeme() + + if (nextLexeme == undefined) { + parser.nextClause() + return + } + + switch (nextLexeme.type) { + case lunr.QueryLexer.TERM: + parser.nextClause() + return lunr.QueryParser.parseTerm + case lunr.QueryLexer.FIELD: + parser.nextClause() + return lunr.QueryParser.parseField + case lunr.QueryLexer.EDIT_DISTANCE: + return lunr.QueryParser.parseEditDistance + case lunr.QueryLexer.BOOST: + return lunr.QueryParser.parseBoost + case lunr.QueryLexer.PRESENCE: + parser.nextClause() + return lunr.QueryParser.parsePresence + default: + var errorMessage = "Unexpected lexeme type '" + nextLexeme.type + "'" + throw new lunr.QueryParseError (errorMessage, nextLexeme.start, nextLexeme.end) + } +} + +lunr.QueryParser.parseBoost = function (parser) { + var lexeme = parser.consumeLexeme() + + if (lexeme == undefined) { + return + } + + var boost = parseInt(lexeme.str, 10) + + if (isNaN(boost)) { + var errorMessage = "boost must be numeric" + throw new lunr.QueryParseError (errorMessage, lexeme.start, lexeme.end) + } + + parser.currentClause.boost = boost + + var nextLexeme = parser.peekLexeme() + + if (nextLexeme == undefined) { + parser.nextClause() + return + } + + switch (nextLexeme.type) { + case lunr.QueryLexer.TERM: + parser.nextClause() + return lunr.QueryParser.parseTerm + case lunr.QueryLexer.FIELD: + parser.nextClause() + return lunr.QueryParser.parseField + case lunr.QueryLexer.EDIT_DISTANCE: + return lunr.QueryParser.parseEditDistance + case lunr.QueryLexer.BOOST: + return lunr.QueryParser.parseBoost + case lunr.QueryLexer.PRESENCE: + parser.nextClause() + return lunr.QueryParser.parsePresence + default: + var errorMessage = "Unexpected lexeme type '" + nextLexeme.type + "'" + throw new lunr.QueryParseError (errorMessage, nextLexeme.start, nextLexeme.end) + } +} + + /** + * export the module via AMD, CommonJS or as a browser global + * Export code from https://github.com/umdjs/umd/blob/master/returnExports.js + */ + ;(function (root, factory) { + if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define(factory) + } else if (typeof exports === 'object') { + /** + * Node. Does not work with strict CommonJS, but + * only CommonJS-like environments that support module.exports, + * like Node. + */ + module.exports = factory() + } else { + // Browser globals (root is window) + root.lunr = factory() + } + }(this, function () { + /** + * Just return a value to define the module export. + * This example returns an object, but the module + * can return a function as the exported value. + */ + return lunr + })) +})(); diff --git a/search/main.js b/search/main.js new file mode 100644 index 00000000..a5e469d7 --- /dev/null +++ b/search/main.js @@ -0,0 +1,109 @@ +function getSearchTermFromLocation() { + var sPageURL = window.location.search.substring(1); + var sURLVariables = sPageURL.split('&'); + for (var i = 0; i < sURLVariables.length; i++) { + var sParameterName = sURLVariables[i].split('='); + if (sParameterName[0] == 'q') { + return decodeURIComponent(sParameterName[1].replace(/\+/g, '%20')); + } + } +} + +function joinUrl (base, path) { + if (path.substring(0, 1) === "/") { + // path starts with `/`. Thus it is absolute. + return path; + } + if (base.substring(base.length-1) === "/") { + // base ends with `/` + return base + path; + } + return base + "/" + path; +} + +function escapeHtml (value) { + return value.replace(/&/g, '&') + .replace(/"/g, '"') + .replace(//g, '>'); +} + +function formatResult (location, title, summary) { + return ''; +} + +function displayResults (results) { + var search_results = document.getElementById("mkdocs-search-results"); + while (search_results.firstChild) { + search_results.removeChild(search_results.firstChild); + } + if (results.length > 0){ + for (var i=0; i < results.length; i++){ + var result = results[i]; + var html = formatResult(result.location, result.title, result.summary); + search_results.insertAdjacentHTML('beforeend', html); + } + } else { + var noResultsText = search_results.getAttribute('data-no-results-text'); + if (!noResultsText) { + noResultsText = "No results found"; + } + search_results.insertAdjacentHTML('beforeend', '

' + noResultsText + '

'); + } +} + +function doSearch () { + var query = document.getElementById('mkdocs-search-query').value; + if (query.length > min_search_length) { + if (!window.Worker) { + displayResults(search(query)); + } else { + searchWorker.postMessage({query: query}); + } + } else { + // Clear results for short queries + displayResults([]); + } +} + +function initSearch () { + var search_input = document.getElementById('mkdocs-search-query'); + if (search_input) { + search_input.addEventListener("keyup", doSearch); + } + var term = getSearchTermFromLocation(); + if (term) { + search_input.value = term; + doSearch(); + } +} + +function onWorkerMessage (e) { + if (e.data.allowSearch) { + initSearch(); + } else if (e.data.results) { + var results = e.data.results; + displayResults(results); + } else if (e.data.config) { + min_search_length = e.data.config.min_search_length-1; + } +} + +if (!window.Worker) { + console.log('Web Worker API not supported'); + // load index in main thread + $.getScript(joinUrl(base_url, "search/worker.js")).done(function () { + console.log('Loaded worker'); + init(); + window.postMessage = function (msg) { + onWorkerMessage({data: msg}); + }; + }).fail(function (jqxhr, settings, exception) { + console.error('Could not load worker.js'); + }); +} else { + // Wrap search in a web worker + var searchWorker = new Worker(joinUrl(base_url, "search/worker.js")); + searchWorker.postMessage({init: true}); + searchWorker.onmessage = onWorkerMessage; +} diff --git a/search/search_index.json b/search/search_index.json new file mode 100644 index 00000000..e01f6cdb --- /dev/null +++ b/search/search_index.json @@ -0,0 +1 @@ +{"config":{"indexing":"full","lang":["en"],"min_search_length":3,"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"Python Fire Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object. Python Fire is a simple way to create a CLI in Python. [1] Python Fire is a helpful tool for developing and debugging Python code. [2] Python Fire helps with exploring existing code or turning other people's code into a CLI. [3] Python Fire makes transitioning between Bash and Python easier. [4] Python Fire makes using a Python REPL easier by setting up the REPL with the modules and variables you'll need already imported and created. [5] Installation To install Python Fire with pip, run: pip install fire To install Python Fire with conda, run: conda install fire -c conda-forge To install Python Fire from source, first clone the repository and then run: python setup.py install Basic Usage You can call Fire on any Python object: functions, classes, modules, objects, dictionaries, lists, tuples, etc. They all work! Here's an example of calling Fire on a function. import fire def hello(name=\"World\"): return \"Hello %s!\" % name if __name__ == '__main__': fire.Fire(hello) Then, from the command line, you can run: python hello.py # Hello World! python hello.py --name=David # Hello David! python hello.py --help # Shows usage information. Here's an example of calling Fire on a class. import fire class Calculator(object): \"\"\"A simple calculator class.\"\"\" def double(self, number): return 2 * number if __name__ == '__main__': fire.Fire(Calculator) Then, from the command line, you can run: python calculator.py double 10 # 20 python calculator.py double --number=15 # 30 To learn how Fire behaves on functions, objects, dicts, lists, etc, and to learn about Fire's other features, see the Using a Fire CLI page . For additional examples, see The Python Fire Guide . Why is it called Fire? When you call Fire , it fires off (executes) your command. Where can I learn more? Please see The Python Fire Guide . Reference Setup Command Notes install pip install fire Creating a CLI Command Notes import import fire Call fire.Fire() Turns the current module into a Fire CLI. Call fire.Fire(component) Turns component into a Fire CLI. Using a CLI Command Notes Help command --help or command -- --help REPL command -- --interactive Enters interactive mode. Separator command -- --separator=X Sets the separator to X . The default separator is - . Completion command -- --completion [shell] Generates a completion script for the CLI. Trace command -- --trace Gets a Fire trace for the command. Verbose command -- --verbose Note that flags are separated from the Fire command by an isolated -- arg. Help is an exception; the isolated -- is optional for getting help. License Licensed under the Apache 2.0 License. Disclaimer This is not an official Google product.","title":"Overview"},{"location":"#python-fire","text":"Python Fire is a library for automatically generating command line interfaces (CLIs) from absolutely any Python object. Python Fire is a simple way to create a CLI in Python. [1] Python Fire is a helpful tool for developing and debugging Python code. [2] Python Fire helps with exploring existing code or turning other people's code into a CLI. [3] Python Fire makes transitioning between Bash and Python easier. [4] Python Fire makes using a Python REPL easier by setting up the REPL with the modules and variables you'll need already imported and created. [5]","title":"Python Fire"},{"location":"#installation","text":"To install Python Fire with pip, run: pip install fire To install Python Fire with conda, run: conda install fire -c conda-forge To install Python Fire from source, first clone the repository and then run: python setup.py install","title":"Installation"},{"location":"#basic-usage","text":"You can call Fire on any Python object: functions, classes, modules, objects, dictionaries, lists, tuples, etc. They all work! Here's an example of calling Fire on a function. import fire def hello(name=\"World\"): return \"Hello %s!\" % name if __name__ == '__main__': fire.Fire(hello) Then, from the command line, you can run: python hello.py # Hello World! python hello.py --name=David # Hello David! python hello.py --help # Shows usage information. Here's an example of calling Fire on a class. import fire class Calculator(object): \"\"\"A simple calculator class.\"\"\" def double(self, number): return 2 * number if __name__ == '__main__': fire.Fire(Calculator) Then, from the command line, you can run: python calculator.py double 10 # 20 python calculator.py double --number=15 # 30 To learn how Fire behaves on functions, objects, dicts, lists, etc, and to learn about Fire's other features, see the Using a Fire CLI page . For additional examples, see The Python Fire Guide .","title":"Basic Usage"},{"location":"#why-is-it-called-fire","text":"When you call Fire , it fires off (executes) your command.","title":"Why is it called Fire?"},{"location":"#where-can-i-learn-more","text":"Please see The Python Fire Guide .","title":"Where can I learn more?"},{"location":"#reference","text":"Setup Command Notes install pip install fire Creating a CLI Command Notes import import fire Call fire.Fire() Turns the current module into a Fire CLI. Call fire.Fire(component) Turns component into a Fire CLI. Using a CLI Command Notes Help command --help or command -- --help REPL command -- --interactive Enters interactive mode. Separator command -- --separator=X Sets the separator to X . The default separator is - . Completion command -- --completion [shell] Generates a completion script for the CLI. Trace command -- --trace Gets a Fire trace for the command. Verbose command -- --verbose Note that flags are separated from the Fire command by an isolated -- arg. Help is an exception; the isolated -- is optional for getting help.","title":"Reference"},{"location":"#license","text":"Licensed under the Apache 2.0 License.","title":"License"},{"location":"#disclaimer","text":"This is not an official Google product.","title":"Disclaimer"},{"location":"api/","text":"Python Fire Quick Reference Setup Command Notes install pip install fire Installs fire from pypi Creating a CLI Command Notes import import fire Call fire.Fire() Turns the current module into a Fire CLI. Call fire.Fire(component) Turns component into a Fire CLI. Using a CLI Command Notes Help command --help Show the help screen. REPL command -- --interactive Enters interactive mode. Separator command -- --separator=X This sets the separator to X . The default separator is - . Completion command -- --completion [shell] Generate a completion script for the CLI. Trace command -- --trace Gets a Fire trace for the command. Verbose command -- --verbose Note that flags are separated from the Fire command by an isolated -- arg. Help is an exception; the isolated -- is optional for getting help. Arguments for Calling fire.Fire() Argument Usage Notes component fire.Fire(component) If omitted, defaults to a dict of all locals and globals. command fire.Fire(command='hello --name=5') Either a string or a list of arguments. If a string is provided, it is split to determine the arguments. If a list or tuple is provided, they are the arguments. If command is omitted, then sys.argv[1:] (the arguments from the command line) are used by default. name fire.Fire(name='tool') The name of the CLI, ideally the name users will enter to run the CLI. This name will be used in the CLI's help screens. If the argument is omitted, it will be inferred automatically. serialize fire.Fire(serialize=custom_serializer) If omitted, simple types are serialized via their builtin str method, and any objects that define a custom __str__ method are serialized with that. If specified, all objects are serialized to text via the provided method. Using a Fire CLI without modifying any code You can use Python Fire on a module without modifying the code of the module. The syntax for this is: python -m fire or python -m fire For example, python -m fire calendar -h will treat the built in calendar module as a CLI and provide its help.","title":"Reference"},{"location":"api/#python-fire-quick-reference","text":"Setup Command Notes install pip install fire Installs fire from pypi Creating a CLI Command Notes import import fire Call fire.Fire() Turns the current module into a Fire CLI. Call fire.Fire(component) Turns component into a Fire CLI. Using a CLI Command Notes Help command --help Show the help screen. REPL command -- --interactive Enters interactive mode. Separator command -- --separator=X This sets the separator to X . The default separator is - . Completion command -- --completion [shell] Generate a completion script for the CLI. Trace command -- --trace Gets a Fire trace for the command. Verbose command -- --verbose Note that flags are separated from the Fire command by an isolated -- arg. Help is an exception; the isolated -- is optional for getting help.","title":"Python Fire Quick Reference"},{"location":"api/#arguments-for-calling-firefire","text":"Argument Usage Notes component fire.Fire(component) If omitted, defaults to a dict of all locals and globals. command fire.Fire(command='hello --name=5') Either a string or a list of arguments. If a string is provided, it is split to determine the arguments. If a list or tuple is provided, they are the arguments. If command is omitted, then sys.argv[1:] (the arguments from the command line) are used by default. name fire.Fire(name='tool') The name of the CLI, ideally the name users will enter to run the CLI. This name will be used in the CLI's help screens. If the argument is omitted, it will be inferred automatically. serialize fire.Fire(serialize=custom_serializer) If omitted, simple types are serialized via their builtin str method, and any objects that define a custom __str__ method are serialized with that. If specified, all objects are serialized to text via the provided method.","title":"Arguments for Calling fire.Fire()"},{"location":"api/#using-a-fire-cli-without-modifying-any-code","text":"You can use Python Fire on a module without modifying the code of the module. The syntax for this is: python -m fire or python -m fire For example, python -m fire calendar -h will treat the built in calendar module as a CLI and provide its help.","title":"Using a Fire CLI without modifying any code"},{"location":"benefits/","text":"Benefits of Python Fire Create CLIs in Python It's dead simple. Simply write the functionality you want exposed at the command line as a function / module / class, and then call Fire. With this addition of a single-line call to Fire, your CLI is ready to go. Develop and debug Python code When you're writing a Python library, you probably want to try it out as you go. You could write a main method to check the functionality you're interested in, but then you have to change the main method with every new experiment you're interested in testing, and constantly updating the main method is a hassle. You could also open an IPython REPL and import your library there and test it, but then you have to deal with reloading your imports every time you change something. If you simply call Fire in your library, then you can run all of it's functionality from the command line without having to keep making changes to a main method. And if you use the --interactive flag to enter an IPython REPL then you don't need to load the imports or create your variables; they'll already be ready for use as soon as you start the REPL. Explore existing code; turn other people's code into a CLI You can take an existing module, maybe even one that you don't have access to the source code for, and call Fire on it. This lets you easily see what functionality this code exposes, without you having to read through all the code. This technique can be a very simple way to create very powerful CLIs. Call Fire on the difflib library and you get a powerful diffing tool. Call Fire on the Python Imaging Library (PIL) module and you get a powerful image manipulation command line tool, very similar in nature to ImageMagick. The auto-generated help strings that Fire provides when you run a Fire CLI allow you to see all the functionality these modules provide in a concise manner. Transition between Bash and Python Using Fire lets you call Python directly from Bash. So you can mix your Python functions with the unix tools you know and love, like grep , xargs , wc , etc. Additionally since writing CLIs in Python requires only a single call to Fire, it is now easy to write even one-off scripts that would previously have been in Bash, in Python. Explore code in a Python REPL When you use the --interactive flag to enter an IPython REPL, it starts with variables and modules already defined for you. You don't need to waste time importing the modules you care about or defining the variables you're going to use, since Fire has already done so for you.","title":"Benefits"},{"location":"benefits/#benefits-of-python-fire","text":"","title":"Benefits of Python Fire"},{"location":"benefits/#create-clis-in-python","text":"It's dead simple. Simply write the functionality you want exposed at the command line as a function / module / class, and then call Fire. With this addition of a single-line call to Fire, your CLI is ready to go.","title":"Create CLIs in Python"},{"location":"benefits/#develop-and-debug-python-code","text":"When you're writing a Python library, you probably want to try it out as you go. You could write a main method to check the functionality you're interested in, but then you have to change the main method with every new experiment you're interested in testing, and constantly updating the main method is a hassle. You could also open an IPython REPL and import your library there and test it, but then you have to deal with reloading your imports every time you change something. If you simply call Fire in your library, then you can run all of it's functionality from the command line without having to keep making changes to a main method. And if you use the --interactive flag to enter an IPython REPL then you don't need to load the imports or create your variables; they'll already be ready for use as soon as you start the REPL.","title":"Develop and debug Python code"},{"location":"benefits/#explore-existing-code-turn-other-peoples-code-into-a-cli","text":"You can take an existing module, maybe even one that you don't have access to the source code for, and call Fire on it. This lets you easily see what functionality this code exposes, without you having to read through all the code. This technique can be a very simple way to create very powerful CLIs. Call Fire on the difflib library and you get a powerful diffing tool. Call Fire on the Python Imaging Library (PIL) module and you get a powerful image manipulation command line tool, very similar in nature to ImageMagick. The auto-generated help strings that Fire provides when you run a Fire CLI allow you to see all the functionality these modules provide in a concise manner.","title":"Explore existing code; turn other people's code into a CLI"},{"location":"benefits/#transition-between-bash-and-python","text":"Using Fire lets you call Python directly from Bash. So you can mix your Python functions with the unix tools you know and love, like grep , xargs , wc , etc. Additionally since writing CLIs in Python requires only a single call to Fire, it is now easy to write even one-off scripts that would previously have been in Bash, in Python.","title":"Transition between Bash and Python"},{"location":"benefits/#explore-code-in-a-python-repl","text":"When you use the --interactive flag to enter an IPython REPL, it starts with variables and modules already defined for you. You don't need to waste time importing the modules you care about or defining the variables you're going to use, since Fire has already done so for you.","title":"Explore code in a Python REPL"},{"location":"guide/","text":"The Python Fire Guide Introduction Welcome to the Python Fire guide! Python Fire is a Python library that will turn any Python component into a command line interface with just a single call to Fire . Let's get started! Installation To install Python Fire from pypi, run: pip install fire Alternatively, to install Python Fire from source, clone the source and run: python setup.py install Hello World Version 1: fire.Fire() The easiest way to use Fire is to take any Python program, and then simply call fire.Fire() at the end of the program. This will expose the full contents of the program to the command line. import fire def hello(name): return f'Hello {name}!' if __name__ == '__main__': fire.Fire() Here's how we can run our program from the command line: $ python example.py hello World Hello World! Version 2: fire.Fire() Let's modify our program slightly to only expose the hello function to the command line. import fire def hello(name): return f'Hello {name}!' if __name__ == '__main__': fire.Fire(hello) Here's how we can run this from the command line: $ python example.py World Hello World! Notice we no longer have to specify to run the hello function, because we called fire.Fire(hello) . Version 3: Using a main We can alternatively write this program like this: import fire def hello(name): return f'Hello {name}!' def main(): fire.Fire(hello) if __name__ == '__main__': main() Or if we're using entry points , then simply this: import fire def hello(name): return f'Hello {name}!' def main(): fire.Fire(hello) Version 4: Fire Without Code Changes If you have a file example.py that doesn't even import fire: def hello(name): return f'Hello {name}!' Then you can use it with Fire like this: $ python -m fire example hello --name=World Hello World! You can also specify the filepath of example.py rather than its module path, like so: $ python -m fire example.py hello --name=World Hello World! Exposing Multiple Commands In the previous example, we exposed a single function to the command line. Now we'll look at ways of exposing multiple functions to the command line. Version 1: fire.Fire() The simplest way to expose multiple commands is to write multiple functions, and then call Fire. import fire def add(x, y): return x + y def multiply(x, y): return x * y if __name__ == '__main__': fire.Fire() We can use this like so: $ python example.py add 10 20 30 $ python example.py multiply 10 20 200 You'll notice that Fire correctly parsed 10 and 20 as numbers, rather than as strings. Read more about argument parsing here . Version 2: fire.Fire() In version 1 we exposed all the program's functionality to the command line. By using a dict, we can selectively expose functions to the command line. import fire def add(x, y): return x + y def multiply(x, y): return x * y if __name__ == '__main__': fire.Fire({ 'add': add, 'multiply': multiply, }) We can use this in the same way as before: $ python example.py add 10 20 30 $ python example.py multiply 10 20 200 Version 3: fire.Fire() Fire also works on objects, as in this variant. This is a good way to expose multiple commands. import fire class Calculator(object): def add(self, x, y): return x + y def multiply(self, x, y): return x * y if __name__ == '__main__': calculator = Calculator() fire.Fire(calculator) We can use this in the same way as before: $ python example.py add 10 20 30 $ python example.py multiply 10 20 200 Version 4: fire.Fire() Fire also works on classes. This is another good way to expose multiple commands. import fire class Calculator(object): def add(self, x, y): return x + y def multiply(self, x, y): return x * y if __name__ == '__main__': fire.Fire(Calculator) We can use this in the same way as before: $ python example.py add 10 20 30 $ python example.py multiply 10 20 200 Why might you prefer a class over an object? One reason is that you can pass arguments for constructing the class too, as in this broken calculator example. import fire class BrokenCalculator(object): def __init__(self, offset=1): self._offset = offset def add(self, x, y): return x + y + self._offset def multiply(self, x, y): return x * y + self._offset if __name__ == '__main__': fire.Fire(BrokenCalculator) When you use a broken calculator, you get wrong answers: $ python example.py add 10 20 31 $ python example.py multiply 10 20 201 But you can always fix it: $ python example.py add 10 20 --offset=0 30 $ python example.py multiply 10 20 --offset=0 200 Unlike calling ordinary functions, which can be done both with positional arguments and named arguments (--flag syntax), arguments to __init__ functions must be passed with the --flag syntax. See the section on calling functions for more. Grouping Commands Here's an example of how you might make a command line interface with grouped commands. class IngestionStage(object): def run(self): return 'Ingesting! Nom nom nom...' class DigestionStage(object): def run(self, volume=1): return ' '.join(['Burp!'] * volume) def status(self): return 'Satiated.' class Pipeline(object): def __init__(self): self.ingestion = IngestionStage() self.digestion = DigestionStage() def run(self): ingestion_output = self.ingestion.run() digestion_output = self.digestion.run() return [ingestion_output, digestion_output] if __name__ == '__main__': fire.Fire(Pipeline) Here's how this looks at the command line: $ python example.py run Ingesting! Nom nom nom... Burp! $ python example.py ingestion run Ingesting! Nom nom nom... $ python example.py digestion run Burp! $ python example.py digestion status Satiated. You can nest your commands in arbitrarily complex ways, if you're feeling grumpy or adventurous. Accessing Properties In the examples we've looked at so far, our invocations of python example.py have all run some function from the example program. In this example, we simply access a property. from airports import airports import fire class Airport(object): def __init__(self, code): self.code = code self.name = dict(airports).get(self.code) self.city = self.name.split(',')[0] if self.name else None if __name__ == '__main__': fire.Fire(Airport) Now we can use this program to learn about airport codes! $ python example.py --code=JFK code JFK $ python example.py --code=SJC name San Jose-Sunnyvale-Santa Clara, CA - Norman Y. Mineta San Jose International (SJC) $ python example.py --code=ALB city Albany-Schenectady-Troy By the way, you can find this airports module here . Chaining Function Calls When you run a Fire CLI, you can take all the same actions on the result of the call to Fire that you can take on the original object passed in. For example, we can use our Airport CLI from the previous example like this: $ python example.py --code=ALB city upper ALBANY-SCHENECTADY-TROY This works since upper is a method on all strings. So, if you want to set up your functions to chain nicely, all you have to do is have a class whose methods return self. Here's an example. import fire class BinaryCanvas(object): \"\"\"A canvas with which to make binary art, one bit at a time.\"\"\" def __init__(self, size=10): self.pixels = [[0] * size for _ in range(size)] self._size = size self._row = 0 # The row of the cursor. self._col = 0 # The column of the cursor. def __str__(self): return '\\n'.join(' '.join(str(pixel) for pixel in row) for row in self.pixels) def show(self): print(self) return self def move(self, row, col): self._row = row % self._size self._col = col % self._size return self def on(self): return self.set(1) def off(self): return self.set(0) def set(self, value): self.pixels[self._row][self._col] = value return self if __name__ == '__main__': fire.Fire(BinaryCanvas) Now we can draw stuff :). $ python example.py move 3 3 on move 3 6 on move 6 3 on move 6 6 on move 7 4 on move 7 5 on 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 It's supposed to be a smiley face. Custom Serialization You'll notice in the BinaryCanvas example, the canvas with the smiley face was printed to the screen. You can determine how a component will be serialized by defining its __str__ method. If a custom __str__ method is present on the final component, the object is serialized and printed. If there's no custom __str__ method, then the help screen for the object is shown instead. Can we make an even simpler example than Hello World? Yes, this program is even simpler than our original Hello World example. import fire english = 'Hello World' spanish = 'Hola Mundo' fire.Fire() You can use it like this: $ python example.py english Hello World $ python example.py spanish Hola Mundo Calling Functions Arguments to a constructor are passed by name using flag syntax --name=value . For example, consider this simple class: import fire class Building(object): def __init__(self, name, stories=1): self.name = name self.stories = stories def climb_stairs(self, stairs_per_story=10): for story in range(self.stories): for stair in range(1, stairs_per_story): yield stair yield 'Phew!' yield 'Done!' if __name__ == '__main__': fire.Fire(Building) We can instantiate it as follows: python example.py --name=\"Sherrerd Hall\" Arguments to other functions may be passed positionally or by name using flag syntax. To instantiate a Building and then run the climb_stairs function, the following commands are all valid: $ python example.py --name=\"Sherrerd Hall\" --stories=3 climb_stairs 10 $ python example.py --name=\"Sherrerd Hall\" climb_stairs --stairs_per_story=10 $ python example.py --name=\"Sherrerd Hall\" climb_stairs --stairs-per-story 10 $ python example.py climb-stairs --stairs-per-story 10 --name=\"Sherrerd Hall\" You'll notice that hyphens and underscores ( - and _ ) are interchangeable in member names and flag names. You'll also notice that the constructor's arguments can come after the function's arguments or before the function. You'll also notice that the equal sign between the flag name and its value is optional. Functions with *varargs and **kwargs Fire supports functions that take *varargs or **kwargs. Here's an example: import fire def order_by_length(*items): \"\"\"Orders items by length, breaking ties alphabetically.\"\"\" sorted_items = sorted(items, key=lambda item: (len(str(item)), str(item))) return ' '.join(sorted_items) if __name__ == '__main__': fire.Fire(order_by_length) To use it, we run: $ python example.py dog cat elephant cat dog elephant You can use a separator to indicate that you're done providing arguments to a function. All arguments after the separator will be used to process the result of the function, rather than being passed to the function itself. The default separator is the hyphen - . Here's an example where we use a separator. $ python example.py dog cat elephant - upper CAT DOG ELEPHANT Without the separator, upper would have been treated as another argument. $ python example.py dog cat elephant upper cat dog upper elephant You can change the separator with the --separator flag. Flags are always separated from your Fire command by an isolated -- . Here's an example where we change the separator. $ python example.py dog cat elephant X upper -- --separator=X CAT DOG ELEPHANT Separators can be useful when a function accepts *varargs, **kwargs, or default values that you don't want to specify. It is also important to remember to change the separator if you want to pass - as an argument. Async Functions Fire supports calling async functions too. Here's a simple example. import asyncio async def count_to_ten(): for i in range(1, 11): await asyncio.sleep(1) print(i) if __name__ == '__main__': fire.Fire(count_to_ten) Whenever fire encounters a coroutine function, it runs it, blocking until it completes. Argument Parsing The types of the arguments are determined by their values, rather than by the function signature where they're used. You can pass any Python literal from the command line: numbers, strings, tuples, lists, dictionaries, (sets are only supported in some versions of Python). You can also nest the collections arbitrarily as long as they only contain literals. To demonstrate this, we'll make a small example program that tells us the type of any argument we give it: import fire fire.Fire(lambda obj: type(obj).__name__) And we'll use it like so: $ python example.py 10 int $ python example.py 10.0 float $ python example.py hello str $ python example.py '(1,2)' tuple $ python example.py [1,2] list $ python example.py True bool $ python example.py {name:David} dict You'll notice in that last example that bare-words are automatically replaced with strings. Be careful with your quotes! If you want to pass the string \"10\" , rather than the int 10 , you'll need to either escape or quote your quotes. Otherwise Bash will eat your quotes and pass an unquoted 10 to your Python program, where Fire will interpret it as a number. $ python example.py 10 int $ python example.py \"10\" int $ python example.py '\"10\"' str $ python example.py \"'10'\" str $ python example.py \\\"10\\\" str Be careful with your quotes! Remember that Bash processes your arguments first, and then Fire parses the result of that. If you wanted to pass the dict {\"name\": \"David Bieber\"} to your program, you might try this: $ python example.py '{\"name\": \"David Bieber\"}' # Good! Do this. dict $ python example.py {\"name\":'\"David Bieber\"'} # Okay. dict $ python example.py {\"name\":\"David Bieber\"} # Wrong. This is parsed as a string. str $ python example.py {\"name\": \"David Bieber\"} # Wrong. This isn't even treated as a single argument. $ python example.py '{\"name\": \"Justin Bieber\"}' # Wrong. This is not the Bieber you're looking for. (The syntax is fine though :)) dict Boolean Arguments The tokens True and False are parsed as boolean values. You may also specify booleans via flag syntax --name and --noname , which set name to True and False respectively. Continuing the previous example, we could run any of the following: $ python example.py --obj=True bool $ python example.py --obj=False bool $ python example.py --obj bool $ python example.py --noobj bool Be careful with boolean flags! If a token other than another flag immediately follows a flag that's supposed to be a boolean, the flag will take on the value of the token rather than the boolean value. You can resolve this: by putting a separator after your last flag, by explicitly stating the value of the boolean flag (as in --obj=True ), or by making sure there's another flag after any boolean flag argument. Using Fire Flags Fire CLIs all come with a number of flags. These flags should be separated from the Fire command by an isolated -- . If there is at least one isolated -- argument, then arguments after the final isolated -- are treated as flags, whereas all arguments before the final isolated -- are considered part of the Fire command. One useful flag is the --interactive flag. Use the --interactive flag on any CLI to enter a Python REPL with all the modules and variables used in the context where Fire was called already available to you for use. Other useful variables, such as the result of the Fire command will also be available. Use this feature like this: python example.py -- --interactive . You can add the help flag to any command to see help and usage information. Fire incorporates your docstrings into the help and usage information that it generates. Fire will try to provide help even if you omit the isolated -- separating the flags from the Fire command, but may not always be able to, since help is a valid argument name. Use this feature like this: python example.py -- --help or python example.py --help (or even python example.py -h ). The complete set of flags available is shown below, in the reference section. Reference Setup Command Notes install pip install fire Creating a CLI Creating a CLI Command Notes import import fire Call fire.Fire() Turns the current module into a Fire CLI. Call fire.Fire(component) Turns component into a Fire CLI. Flags Using a CLI Command Notes Help command -- --help Show help and usage information for the command. REPL command -- --interactive Enter interactive mode. Separator command -- --separator=X This sets the separator to X . The default separator is - . Completion command -- --completion [shell] Generate a completion script for the CLI. Trace command -- --trace Gets a Fire trace for the command. Verbose command -- --verbose Include private members in the output. Note that flags are separated from the Fire command by an isolated -- arg. Help is an exception; the isolated -- is optional for getting help. Arguments for Calling fire.Fire() Argument Usage Notes component fire.Fire(component) If omitted, defaults to a dict of all locals and globals. command fire.Fire(command='hello --name=5') Either a string or a list of arguments. If a string is provided, it is split to determine the arguments. If a list or tuple is provided, they are the arguments. If command is omitted, then sys.argv[1:] (the arguments from the command line) are used by default. name fire.Fire(name='tool') The name of the CLI, ideally the name users will enter to run the CLI. This name will be used in the CLI's help screens. If the argument is omitted, it will be inferred automatically. serialize fire.Fire(serialize=custom_serializer) If omitted, simple types are serialized via their builtin str method, and any objects that define a custom __str__ method are serialized with that. If specified, all objects are serialized to text via the provided method. Disclaimer Python Fire is not an official Google product.","title":"The Python Fire Guide"},{"location":"guide/#the-python-fire-guide","text":"","title":"The Python Fire Guide"},{"location":"guide/#introduction","text":"Welcome to the Python Fire guide! Python Fire is a Python library that will turn any Python component into a command line interface with just a single call to Fire . Let's get started!","title":"Introduction"},{"location":"guide/#installation","text":"To install Python Fire from pypi, run: pip install fire Alternatively, to install Python Fire from source, clone the source and run: python setup.py install","title":"Installation"},{"location":"guide/#hello-world","text":"","title":"Hello World"},{"location":"guide/#version-1-firefire","text":"The easiest way to use Fire is to take any Python program, and then simply call fire.Fire() at the end of the program. This will expose the full contents of the program to the command line. import fire def hello(name): return f'Hello {name}!' if __name__ == '__main__': fire.Fire() Here's how we can run our program from the command line: $ python example.py hello World Hello World!","title":"Version 1: fire.Fire()"},{"location":"guide/#version-2-firefirefn","text":"Let's modify our program slightly to only expose the hello function to the command line. import fire def hello(name): return f'Hello {name}!' if __name__ == '__main__': fire.Fire(hello) Here's how we can run this from the command line: $ python example.py World Hello World! Notice we no longer have to specify to run the hello function, because we called fire.Fire(hello) .","title":"Version 2: fire.Fire(<fn>)"},{"location":"guide/#version-3-using-a-main","text":"We can alternatively write this program like this: import fire def hello(name): return f'Hello {name}!' def main(): fire.Fire(hello) if __name__ == '__main__': main() Or if we're using entry points , then simply this: import fire def hello(name): return f'Hello {name}!' def main(): fire.Fire(hello)","title":"Version 3: Using a main"},{"location":"guide/#version-4-fire-without-code-changes","text":"If you have a file example.py that doesn't even import fire: def hello(name): return f'Hello {name}!' Then you can use it with Fire like this: $ python -m fire example hello --name=World Hello World! You can also specify the filepath of example.py rather than its module path, like so: $ python -m fire example.py hello --name=World Hello World!","title":"Version 4: Fire Without Code Changes"},{"location":"guide/#exposing-multiple-commands","text":"In the previous example, we exposed a single function to the command line. Now we'll look at ways of exposing multiple functions to the command line.","title":"Exposing Multiple Commands"},{"location":"guide/#version-1-firefire_1","text":"The simplest way to expose multiple commands is to write multiple functions, and then call Fire. import fire def add(x, y): return x + y def multiply(x, y): return x * y if __name__ == '__main__': fire.Fire() We can use this like so: $ python example.py add 10 20 30 $ python example.py multiply 10 20 200 You'll notice that Fire correctly parsed 10 and 20 as numbers, rather than as strings. Read more about argument parsing here .","title":"Version 1: fire.Fire()"},{"location":"guide/#version-2-firefiredict","text":"In version 1 we exposed all the program's functionality to the command line. By using a dict, we can selectively expose functions to the command line. import fire def add(x, y): return x + y def multiply(x, y): return x * y if __name__ == '__main__': fire.Fire({ 'add': add, 'multiply': multiply, }) We can use this in the same way as before: $ python example.py add 10 20 30 $ python example.py multiply 10 20 200","title":"Version 2: fire.Fire(<dict>)"},{"location":"guide/#version-3-firefireobject","text":"Fire also works on objects, as in this variant. This is a good way to expose multiple commands. import fire class Calculator(object): def add(self, x, y): return x + y def multiply(self, x, y): return x * y if __name__ == '__main__': calculator = Calculator() fire.Fire(calculator) We can use this in the same way as before: $ python example.py add 10 20 30 $ python example.py multiply 10 20 200","title":"Version 3: fire.Fire(<object>)"},{"location":"guide/#version-4-firefireclass","text":"Fire also works on classes. This is another good way to expose multiple commands. import fire class Calculator(object): def add(self, x, y): return x + y def multiply(self, x, y): return x * y if __name__ == '__main__': fire.Fire(Calculator) We can use this in the same way as before: $ python example.py add 10 20 30 $ python example.py multiply 10 20 200 Why might you prefer a class over an object? One reason is that you can pass arguments for constructing the class too, as in this broken calculator example. import fire class BrokenCalculator(object): def __init__(self, offset=1): self._offset = offset def add(self, x, y): return x + y + self._offset def multiply(self, x, y): return x * y + self._offset if __name__ == '__main__': fire.Fire(BrokenCalculator) When you use a broken calculator, you get wrong answers: $ python example.py add 10 20 31 $ python example.py multiply 10 20 201 But you can always fix it: $ python example.py add 10 20 --offset=0 30 $ python example.py multiply 10 20 --offset=0 200 Unlike calling ordinary functions, which can be done both with positional arguments and named arguments (--flag syntax), arguments to __init__ functions must be passed with the --flag syntax. See the section on calling functions for more.","title":"Version 4: fire.Fire(<class>)"},{"location":"guide/#grouping-commands","text":"Here's an example of how you might make a command line interface with grouped commands. class IngestionStage(object): def run(self): return 'Ingesting! Nom nom nom...' class DigestionStage(object): def run(self, volume=1): return ' '.join(['Burp!'] * volume) def status(self): return 'Satiated.' class Pipeline(object): def __init__(self): self.ingestion = IngestionStage() self.digestion = DigestionStage() def run(self): ingestion_output = self.ingestion.run() digestion_output = self.digestion.run() return [ingestion_output, digestion_output] if __name__ == '__main__': fire.Fire(Pipeline) Here's how this looks at the command line: $ python example.py run Ingesting! Nom nom nom... Burp! $ python example.py ingestion run Ingesting! Nom nom nom... $ python example.py digestion run Burp! $ python example.py digestion status Satiated. You can nest your commands in arbitrarily complex ways, if you're feeling grumpy or adventurous.","title":"Grouping Commands"},{"location":"guide/#accessing-properties","text":"In the examples we've looked at so far, our invocations of python example.py have all run some function from the example program. In this example, we simply access a property. from airports import airports import fire class Airport(object): def __init__(self, code): self.code = code self.name = dict(airports).get(self.code) self.city = self.name.split(',')[0] if self.name else None if __name__ == '__main__': fire.Fire(Airport) Now we can use this program to learn about airport codes! $ python example.py --code=JFK code JFK $ python example.py --code=SJC name San Jose-Sunnyvale-Santa Clara, CA - Norman Y. Mineta San Jose International (SJC) $ python example.py --code=ALB city Albany-Schenectady-Troy By the way, you can find this airports module here .","title":"Accessing Properties"},{"location":"guide/#chaining-function-calls","text":"When you run a Fire CLI, you can take all the same actions on the result of the call to Fire that you can take on the original object passed in. For example, we can use our Airport CLI from the previous example like this: $ python example.py --code=ALB city upper ALBANY-SCHENECTADY-TROY This works since upper is a method on all strings. So, if you want to set up your functions to chain nicely, all you have to do is have a class whose methods return self. Here's an example. import fire class BinaryCanvas(object): \"\"\"A canvas with which to make binary art, one bit at a time.\"\"\" def __init__(self, size=10): self.pixels = [[0] * size for _ in range(size)] self._size = size self._row = 0 # The row of the cursor. self._col = 0 # The column of the cursor. def __str__(self): return '\\n'.join(' '.join(str(pixel) for pixel in row) for row in self.pixels) def show(self): print(self) return self def move(self, row, col): self._row = row % self._size self._col = col % self._size return self def on(self): return self.set(1) def off(self): return self.set(0) def set(self, value): self.pixels[self._row][self._col] = value return self if __name__ == '__main__': fire.Fire(BinaryCanvas) Now we can draw stuff :). $ python example.py move 3 3 on move 3 6 on move 6 3 on move 6 6 on move 7 4 on move 7 5 on 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 It's supposed to be a smiley face.","title":"Chaining Function Calls"},{"location":"guide/#custom-serialization","text":"You'll notice in the BinaryCanvas example, the canvas with the smiley face was printed to the screen. You can determine how a component will be serialized by defining its __str__ method. If a custom __str__ method is present on the final component, the object is serialized and printed. If there's no custom __str__ method, then the help screen for the object is shown instead.","title":"Custom Serialization"},{"location":"guide/#can-we-make-an-even-simpler-example-than-hello-world","text":"Yes, this program is even simpler than our original Hello World example. import fire english = 'Hello World' spanish = 'Hola Mundo' fire.Fire() You can use it like this: $ python example.py english Hello World $ python example.py spanish Hola Mundo","title":"Can we make an even simpler example than Hello World?"},{"location":"guide/#calling-functions","text":"Arguments to a constructor are passed by name using flag syntax --name=value . For example, consider this simple class: import fire class Building(object): def __init__(self, name, stories=1): self.name = name self.stories = stories def climb_stairs(self, stairs_per_story=10): for story in range(self.stories): for stair in range(1, stairs_per_story): yield stair yield 'Phew!' yield 'Done!' if __name__ == '__main__': fire.Fire(Building) We can instantiate it as follows: python example.py --name=\"Sherrerd Hall\" Arguments to other functions may be passed positionally or by name using flag syntax. To instantiate a Building and then run the climb_stairs function, the following commands are all valid: $ python example.py --name=\"Sherrerd Hall\" --stories=3 climb_stairs 10 $ python example.py --name=\"Sherrerd Hall\" climb_stairs --stairs_per_story=10 $ python example.py --name=\"Sherrerd Hall\" climb_stairs --stairs-per-story 10 $ python example.py climb-stairs --stairs-per-story 10 --name=\"Sherrerd Hall\" You'll notice that hyphens and underscores ( - and _ ) are interchangeable in member names and flag names. You'll also notice that the constructor's arguments can come after the function's arguments or before the function. You'll also notice that the equal sign between the flag name and its value is optional.","title":"Calling Functions"},{"location":"guide/#functions-with-varargs-and-kwargs","text":"Fire supports functions that take *varargs or **kwargs. Here's an example: import fire def order_by_length(*items): \"\"\"Orders items by length, breaking ties alphabetically.\"\"\" sorted_items = sorted(items, key=lambda item: (len(str(item)), str(item))) return ' '.join(sorted_items) if __name__ == '__main__': fire.Fire(order_by_length) To use it, we run: $ python example.py dog cat elephant cat dog elephant You can use a separator to indicate that you're done providing arguments to a function. All arguments after the separator will be used to process the result of the function, rather than being passed to the function itself. The default separator is the hyphen - . Here's an example where we use a separator. $ python example.py dog cat elephant - upper CAT DOG ELEPHANT Without the separator, upper would have been treated as another argument. $ python example.py dog cat elephant upper cat dog upper elephant You can change the separator with the --separator flag. Flags are always separated from your Fire command by an isolated -- . Here's an example where we change the separator. $ python example.py dog cat elephant X upper -- --separator=X CAT DOG ELEPHANT Separators can be useful when a function accepts *varargs, **kwargs, or default values that you don't want to specify. It is also important to remember to change the separator if you want to pass - as an argument.","title":"Functions with *varargs and **kwargs"},{"location":"guide/#async-functions","text":"Fire supports calling async functions too. Here's a simple example. import asyncio async def count_to_ten(): for i in range(1, 11): await asyncio.sleep(1) print(i) if __name__ == '__main__': fire.Fire(count_to_ten) Whenever fire encounters a coroutine function, it runs it, blocking until it completes.","title":"Async Functions"},{"location":"guide/#argument-parsing","text":"The types of the arguments are determined by their values, rather than by the function signature where they're used. You can pass any Python literal from the command line: numbers, strings, tuples, lists, dictionaries, (sets are only supported in some versions of Python). You can also nest the collections arbitrarily as long as they only contain literals. To demonstrate this, we'll make a small example program that tells us the type of any argument we give it: import fire fire.Fire(lambda obj: type(obj).__name__) And we'll use it like so: $ python example.py 10 int $ python example.py 10.0 float $ python example.py hello str $ python example.py '(1,2)' tuple $ python example.py [1,2] list $ python example.py True bool $ python example.py {name:David} dict You'll notice in that last example that bare-words are automatically replaced with strings. Be careful with your quotes! If you want to pass the string \"10\" , rather than the int 10 , you'll need to either escape or quote your quotes. Otherwise Bash will eat your quotes and pass an unquoted 10 to your Python program, where Fire will interpret it as a number. $ python example.py 10 int $ python example.py \"10\" int $ python example.py '\"10\"' str $ python example.py \"'10'\" str $ python example.py \\\"10\\\" str Be careful with your quotes! Remember that Bash processes your arguments first, and then Fire parses the result of that. If you wanted to pass the dict {\"name\": \"David Bieber\"} to your program, you might try this: $ python example.py '{\"name\": \"David Bieber\"}' # Good! Do this. dict $ python example.py {\"name\":'\"David Bieber\"'} # Okay. dict $ python example.py {\"name\":\"David Bieber\"} # Wrong. This is parsed as a string. str $ python example.py {\"name\": \"David Bieber\"} # Wrong. This isn't even treated as a single argument. $ python example.py '{\"name\": \"Justin Bieber\"}' # Wrong. This is not the Bieber you're looking for. (The syntax is fine though :)) dict","title":"Argument Parsing"},{"location":"guide/#boolean-arguments","text":"The tokens True and False are parsed as boolean values. You may also specify booleans via flag syntax --name and --noname , which set name to True and False respectively. Continuing the previous example, we could run any of the following: $ python example.py --obj=True bool $ python example.py --obj=False bool $ python example.py --obj bool $ python example.py --noobj bool Be careful with boolean flags! If a token other than another flag immediately follows a flag that's supposed to be a boolean, the flag will take on the value of the token rather than the boolean value. You can resolve this: by putting a separator after your last flag, by explicitly stating the value of the boolean flag (as in --obj=True ), or by making sure there's another flag after any boolean flag argument.","title":"Boolean Arguments"},{"location":"guide/#using-fire-flags","text":"Fire CLIs all come with a number of flags. These flags should be separated from the Fire command by an isolated -- . If there is at least one isolated -- argument, then arguments after the final isolated -- are treated as flags, whereas all arguments before the final isolated -- are considered part of the Fire command. One useful flag is the --interactive flag. Use the --interactive flag on any CLI to enter a Python REPL with all the modules and variables used in the context where Fire was called already available to you for use. Other useful variables, such as the result of the Fire command will also be available. Use this feature like this: python example.py -- --interactive . You can add the help flag to any command to see help and usage information. Fire incorporates your docstrings into the help and usage information that it generates. Fire will try to provide help even if you omit the isolated -- separating the flags from the Fire command, but may not always be able to, since help is a valid argument name. Use this feature like this: python example.py -- --help or python example.py --help (or even python example.py -h ). The complete set of flags available is shown below, in the reference section.","title":"Using Fire Flags"},{"location":"guide/#reference","text":"Setup Command Notes install pip install fire","title":"Reference"},{"location":"guide/#creating-a-cli","text":"Creating a CLI Command Notes import import fire Call fire.Fire() Turns the current module into a Fire CLI. Call fire.Fire(component) Turns component into a Fire CLI.","title":"Creating a CLI"},{"location":"guide/#flags","text":"Using a CLI Command Notes Help command -- --help Show help and usage information for the command. REPL command -- --interactive Enter interactive mode. Separator command -- --separator=X This sets the separator to X . The default separator is - . Completion command -- --completion [shell] Generate a completion script for the CLI. Trace command -- --trace Gets a Fire trace for the command. Verbose command -- --verbose Include private members in the output. Note that flags are separated from the Fire command by an isolated -- arg. Help is an exception; the isolated -- is optional for getting help.","title":"Flags"},{"location":"guide/#arguments-for-calling-firefire","text":"Argument Usage Notes component fire.Fire(component) If omitted, defaults to a dict of all locals and globals. command fire.Fire(command='hello --name=5') Either a string or a list of arguments. If a string is provided, it is split to determine the arguments. If a list or tuple is provided, they are the arguments. If command is omitted, then sys.argv[1:] (the arguments from the command line) are used by default. name fire.Fire(name='tool') The name of the CLI, ideally the name users will enter to run the CLI. This name will be used in the CLI's help screens. If the argument is omitted, it will be inferred automatically. serialize fire.Fire(serialize=custom_serializer) If omitted, simple types are serialized via their builtin str method, and any objects that define a custom __str__ method are serialized with that. If specified, all objects are serialized to text via the provided method.","title":"Arguments for Calling fire.Fire()"},{"location":"guide/#disclaimer","text":"Python Fire is not an official Google product.","title":"Disclaimer"},{"location":"installation/","text":"Installation To install Python Fire with pip, run: pip install fire To install Python Fire with conda, run: conda install fire -c conda-forge To install Python Fire from source, first clone the repository and then run python setup.py install . To install from source for development, instead run python setup.py develop .","title":"Installation"},{"location":"installation/#installation","text":"To install Python Fire with pip, run: pip install fire To install Python Fire with conda, run: conda install fire -c conda-forge To install Python Fire from source, first clone the repository and then run python setup.py install . To install from source for development, instead run python setup.py develop .","title":"Installation"},{"location":"troubleshooting/","text":"Troubleshooting This page describes known issues that users of Python Fire have run into. If you have an issue not resolved here, consider opening a GitHub Issue . Issue #19 : Don't name your module \"cmd\" If you have a module name that conflicts with the name of a builtin module, then when Fire goes to import the builtin module, it will import your module instead. This will result in an error, possibly an AttributeError . Specifically, do not name your module any of the following: sys, linecache, cmd, bdb, repr, os, re, pprint, traceback","title":"Troubleshooting"},{"location":"troubleshooting/#troubleshooting","text":"This page describes known issues that users of Python Fire have run into. If you have an issue not resolved here, consider opening a GitHub Issue .","title":"Troubleshooting"},{"location":"troubleshooting/#issue-19-dont-name-your-module-cmd","text":"If you have a module name that conflicts with the name of a builtin module, then when Fire goes to import the builtin module, it will import your module instead. This will result in an error, possibly an AttributeError . Specifically, do not name your module any of the following: sys, linecache, cmd, bdb, repr, os, re, pprint, traceback","title":"Issue #19: Don't name your module \"cmd\""},{"location":"using-cli/","text":"Using a Fire CLI Basic usage Every Fire command corresponds to a Python component. The simplest Fire command consists of running your program with no additional arguments. This command corresponds to the Python component you called the Fire function on. If you did not supply an object in the call to Fire , then the context in which Fire was called will be used as the Python component. You can append --help or -h to a command to see what Python component it corresponds to, as well as the various ways in which you can extend the command. Flags to Fire should be separated from the Fire command by an isolated -- in order to distinguish between flags and named arguments. So, for example, to enter interactive mode append -- -i or -- --interactive to any command. To use Fire in verbose mode, append -- --verbose . Given a Fire command that corresponds to a Python object, you can extend that command to access a member of that object, call it with arguments if it is a function, instantiate it if it is a class, or index into it if it is a list. Read on to learn about how you can write a Fire command corresponding to whatever Python component you're looking for. Accessing members of an object If your command corresponds to an object, you can extend your command by adding the name of a member of that object as a new argument to the command. The resulting command will correspond to that member. For example, if the object your command corresponds to has a method defined on it named 'whack', then you can add the argument 'whack' to your command, and the resulting new command corresponds to the whack method. As another example, if the object your command corresponds to has a property named high_score, then you can add the argument 'high-score' to your command, and the resulting new command corresponds to the value of the high_score property. Accessing members of a dict If your command corresponds to a dict, you can extend your command by adding the name of one of the dict's keys as an argument. For example, widget function-that-returns-dict key will correspond to the value of the item with key key in the dict returned by function_that_returns_dict . Accessing members of a list or tuple If your command corresponds to a list or tuple, you can extend your command by adding the index of an element of the component to your command as an argument. For example, widget function-that-returns-list 2 will correspond to item 2 of the result of function_that_returns_list . Calling a function If your command corresponds to a function, you can extend your command by adding the arguments of this function. Arguments can be specified positionally, or by name. To specify an argument by name, use flag syntax. For example, suppose your command corresponds to the function double : def double(value=0): return 2 * value Then you can extend your command using named arguments as command --value 5 , or using positional arguments as command 5 . In both cases, the new command corresponds to the result of the function, in this case the number 10. You can force a function that takes a variable number of arguments to be evaluated by adding a separator (the default separator is the hyphen, \"-\"). This will prevent arguments to the right of the separator from being consumed for calling the function. This is useful if the function has arguments with default values, or if the function accepts *varargs, or if the function accepts **kwargs. See also the section on Changing the Separator . Instantiating a class If your command corresponds to a class, you can extend your command by adding the arguments of the class's __init__ function. Arguments must be specified by name, using the flags syntax. See the section on calling a function for more details. Similarly, when passing arguments to a callable object (an object with a custom __call__ function), those arguments must be passed using flags syntax. Using Flags with Fire CLIs Command line arguments to a Fire CLI are normally consumed by Fire, as described in the Basic Usage section. In order to set Flags, put the flags after the final standalone -- argument. (If there is no -- argument, then no arguments are used for flags.) For example, to set the alsologtostderr flag, you could run the command: widget bang --noise=boom -- --alsologtostderr . The --noise argument is consumed by Fire, but the --alsologtostderr argument is treated as a normal Flag. All CLIs built with Python Fire share some flags, as described in the next sections. Python Fire's Flags As described in the Using Flags section, you must add an isolated -- argument in order to have arguments treated as Flags rather than be consumed by Python Fire. All arguments to a Fire CLI after the final standalone -- argument are treated as Flags. The following flags are accepted by all Fire CLIs: --interactive / -i , --help / -h , --separator , --completion , --trace , and --verbose / -v , as described in the following sections. --interactive : Interactive mode Call widget -- --interactive or widget -- -i to enter interactive mode. This will put you in an IPython REPL, with the variable widget already defined. You can then explore the Python object that widget corresponds to interactively using Python. Note: if you want fire to start the IPython REPL instead of the regular Python one, the ipython package needs to be installed in your environment. --completion : Generating a completion script Call widget -- --completion to generate a completion script for the Fire CLI widget . To save the completion script to your home directory, you could e.g. run widget -- --completion > ~/.widget-completion . You should then source this file; to get permanent completion, source this file from your .bashrc file. Call widget -- --completion fish to generate a completion script for the Fish shell. Source this file from your fish.config. If the commands available in the Fire CLI change, you'll have to regenerate the completion script and source it again. --help : Getting help Let say you have a command line tool named widget that was made with Fire. How do you use this Fire CLI? The simplest way to get started is to run widget -- --help . This will give you usage information for your CLI. You can always append -- --help to any Fire command in order to get usage information for that command and any subcommands. Additionally, help will be displayed if you hit an error using Fire. For example, if you try to pass too many or too few arguments to a function, then help will be displayed. Similarly, if you try to access a member that does not exist, or if you index into a list with too high an index, then help will be displayed. The displayed help shows information about which Python component your command corresponds to, as well as usage information for how to extend that command. --trace : Getting a Fire trace In order to understand what is happening when you call Python Fire, it can be useful to request a trace. This is done via the --trace flag, e.g. widget whack 5 -- --trace . A trace provides step by step information about how the Fire command was executed. In includes which actions were taken, starting with the initial component, leading to the final component represented by the command. A trace is also shown alongside the help if your Fire command reaches an error. --separator : Changing the separator As described in Calling a Function , you can use a separator argument when writing a command that corresponds to calling a function. The separator will cause the function to be evaluated or the class to be instantiated using only the arguments left of the separator. Arguments right of the separator will then be applied to the result of the function call or to the instantiated object. The default separator is - . If you want to supply the string \"-\" as an argument, then you will have to change the separator. You can choose a new separator by supplying the --separator flag to Fire. Here's an example to demonstrate separator usage. Let's say you have a function that takes a variable number of args, and you want to call that function, and then upper case the result. Here's how to do it: # Here's the Python function def display(arg1, arg2='!'): return arg1 + arg2 # Here's what you can do from Bash (Note: the default separator is the hyphen -) display hello # hello! display hello upper # helloupper display hello - upper # HELLO! display - SEP upper -- --separator SEP # -! Notice how in the third and fourth lines, the separator caused the display function to be called with the default value for arg2. In the fourth example, we change the separator to the string \"SEP\" so that we can pass '-' as an argument. --verbose : Verbose usage Adding the -v or --verbose flag turns on verbose mode. This will eg reveal private members in the usage string. Often these members will not actually be usable from the command line tool. As such, verbose mode should be considered a debugging tool, but not fully supported yet.","title":"Using a CLI"},{"location":"using-cli/#using-a-fire-cli","text":"","title":"Using a Fire CLI"},{"location":"using-cli/#basic-usage","text":"Every Fire command corresponds to a Python component. The simplest Fire command consists of running your program with no additional arguments. This command corresponds to the Python component you called the Fire function on. If you did not supply an object in the call to Fire , then the context in which Fire was called will be used as the Python component. You can append --help or -h to a command to see what Python component it corresponds to, as well as the various ways in which you can extend the command. Flags to Fire should be separated from the Fire command by an isolated -- in order to distinguish between flags and named arguments. So, for example, to enter interactive mode append -- -i or -- --interactive to any command. To use Fire in verbose mode, append -- --verbose . Given a Fire command that corresponds to a Python object, you can extend that command to access a member of that object, call it with arguments if it is a function, instantiate it if it is a class, or index into it if it is a list. Read on to learn about how you can write a Fire command corresponding to whatever Python component you're looking for.","title":"Basic usage"},{"location":"using-cli/#accessing-members-of-an-object","text":"If your command corresponds to an object, you can extend your command by adding the name of a member of that object as a new argument to the command. The resulting command will correspond to that member. For example, if the object your command corresponds to has a method defined on it named 'whack', then you can add the argument 'whack' to your command, and the resulting new command corresponds to the whack method. As another example, if the object your command corresponds to has a property named high_score, then you can add the argument 'high-score' to your command, and the resulting new command corresponds to the value of the high_score property.","title":"Accessing members of an object"},{"location":"using-cli/#accessing-members-of-a-dict","text":"If your command corresponds to a dict, you can extend your command by adding the name of one of the dict's keys as an argument. For example, widget function-that-returns-dict key will correspond to the value of the item with key key in the dict returned by function_that_returns_dict .","title":"Accessing members of a dict"},{"location":"using-cli/#accessing-members-of-a-list-or-tuple","text":"If your command corresponds to a list or tuple, you can extend your command by adding the index of an element of the component to your command as an argument. For example, widget function-that-returns-list 2 will correspond to item 2 of the result of function_that_returns_list .","title":"Accessing members of a list or tuple"},{"location":"using-cli/#calling-a-function","text":"If your command corresponds to a function, you can extend your command by adding the arguments of this function. Arguments can be specified positionally, or by name. To specify an argument by name, use flag syntax. For example, suppose your command corresponds to the function double : def double(value=0): return 2 * value Then you can extend your command using named arguments as command --value 5 , or using positional arguments as command 5 . In both cases, the new command corresponds to the result of the function, in this case the number 10. You can force a function that takes a variable number of arguments to be evaluated by adding a separator (the default separator is the hyphen, \"-\"). This will prevent arguments to the right of the separator from being consumed for calling the function. This is useful if the function has arguments with default values, or if the function accepts *varargs, or if the function accepts **kwargs. See also the section on Changing the Separator .","title":"Calling a function"},{"location":"using-cli/#instantiating-a-class","text":"If your command corresponds to a class, you can extend your command by adding the arguments of the class's __init__ function. Arguments must be specified by name, using the flags syntax. See the section on calling a function for more details. Similarly, when passing arguments to a callable object (an object with a custom __call__ function), those arguments must be passed using flags syntax.","title":"Instantiating a class"},{"location":"using-cli/#using-flags-with-fire-clis","text":"Command line arguments to a Fire CLI are normally consumed by Fire, as described in the Basic Usage section. In order to set Flags, put the flags after the final standalone -- argument. (If there is no -- argument, then no arguments are used for flags.) For example, to set the alsologtostderr flag, you could run the command: widget bang --noise=boom -- --alsologtostderr . The --noise argument is consumed by Fire, but the --alsologtostderr argument is treated as a normal Flag. All CLIs built with Python Fire share some flags, as described in the next sections.","title":"Using Flags with Fire CLIs"},{"location":"using-cli/#python-fires-flags","text":"As described in the Using Flags section, you must add an isolated -- argument in order to have arguments treated as Flags rather than be consumed by Python Fire. All arguments to a Fire CLI after the final standalone -- argument are treated as Flags. The following flags are accepted by all Fire CLIs: --interactive / -i , --help / -h , --separator , --completion , --trace , and --verbose / -v , as described in the following sections.","title":"Python Fire's Flags"},{"location":"using-cli/#-interactive-interactive-mode","text":"Call widget -- --interactive or widget -- -i to enter interactive mode. This will put you in an IPython REPL, with the variable widget already defined. You can then explore the Python object that widget corresponds to interactively using Python. Note: if you want fire to start the IPython REPL instead of the regular Python one, the ipython package needs to be installed in your environment.","title":"--interactive: Interactive mode"},{"location":"using-cli/#-completion-generating-a-completion-script","text":"Call widget -- --completion to generate a completion script for the Fire CLI widget . To save the completion script to your home directory, you could e.g. run widget -- --completion > ~/.widget-completion . You should then source this file; to get permanent completion, source this file from your .bashrc file. Call widget -- --completion fish to generate a completion script for the Fish shell. Source this file from your fish.config. If the commands available in the Fire CLI change, you'll have to regenerate the completion script and source it again.","title":"--completion: Generating a completion script"},{"location":"using-cli/#-help-getting-help","text":"Let say you have a command line tool named widget that was made with Fire. How do you use this Fire CLI? The simplest way to get started is to run widget -- --help . This will give you usage information for your CLI. You can always append -- --help to any Fire command in order to get usage information for that command and any subcommands. Additionally, help will be displayed if you hit an error using Fire. For example, if you try to pass too many or too few arguments to a function, then help will be displayed. Similarly, if you try to access a member that does not exist, or if you index into a list with too high an index, then help will be displayed. The displayed help shows information about which Python component your command corresponds to, as well as usage information for how to extend that command.","title":"--help: Getting help"},{"location":"using-cli/#-trace-getting-a-fire-trace","text":"In order to understand what is happening when you call Python Fire, it can be useful to request a trace. This is done via the --trace flag, e.g. widget whack 5 -- --trace . A trace provides step by step information about how the Fire command was executed. In includes which actions were taken, starting with the initial component, leading to the final component represented by the command. A trace is also shown alongside the help if your Fire command reaches an error.","title":"--trace: Getting a Fire trace"},{"location":"using-cli/#-separator-changing-the-separator","text":"As described in Calling a Function , you can use a separator argument when writing a command that corresponds to calling a function. The separator will cause the function to be evaluated or the class to be instantiated using only the arguments left of the separator. Arguments right of the separator will then be applied to the result of the function call or to the instantiated object. The default separator is - . If you want to supply the string \"-\" as an argument, then you will have to change the separator. You can choose a new separator by supplying the --separator flag to Fire. Here's an example to demonstrate separator usage. Let's say you have a function that takes a variable number of args, and you want to call that function, and then upper case the result. Here's how to do it: # Here's the Python function def display(arg1, arg2='!'): return arg1 + arg2 # Here's what you can do from Bash (Note: the default separator is the hyphen -) display hello # hello! display hello upper # helloupper display hello - upper # HELLO! display - SEP upper -- --separator SEP # -! Notice how in the third and fourth lines, the separator caused the display function to be called with the default value for arg2. In the fourth example, we change the separator to the string \"SEP\" so that we can pass '-' as an argument.","title":"--separator: Changing the separator"},{"location":"using-cli/#-verbose-verbose-usage","text":"Adding the -v or --verbose flag turns on verbose mode. This will eg reveal private members in the usage string. Often these members will not actually be usable from the command line tool. As such, verbose mode should be considered a debugging tool, but not fully supported yet.","title":"--verbose: Verbose usage"}]} \ No newline at end of file diff --git a/search/worker.js b/search/worker.js new file mode 100644 index 00000000..8628dbce --- /dev/null +++ b/search/worker.js @@ -0,0 +1,133 @@ +var base_path = 'function' === typeof importScripts ? '.' : '/search/'; +var allowSearch = false; +var index; +var documents = {}; +var lang = ['en']; +var data; + +function getScript(script, callback) { + console.log('Loading script: ' + script); + $.getScript(base_path + script).done(function () { + callback(); + }).fail(function (jqxhr, settings, exception) { + console.log('Error: ' + exception); + }); +} + +function getScriptsInOrder(scripts, callback) { + if (scripts.length === 0) { + callback(); + return; + } + getScript(scripts[0], function() { + getScriptsInOrder(scripts.slice(1), callback); + }); +} + +function loadScripts(urls, callback) { + if( 'function' === typeof importScripts ) { + importScripts.apply(null, urls); + callback(); + } else { + getScriptsInOrder(urls, callback); + } +} + +function onJSONLoaded () { + data = JSON.parse(this.responseText); + var scriptsToLoad = ['lunr.js']; + if (data.config && data.config.lang && data.config.lang.length) { + lang = data.config.lang; + } + if (lang.length > 1 || lang[0] !== "en") { + scriptsToLoad.push('lunr.stemmer.support.js'); + if (lang.length > 1) { + scriptsToLoad.push('lunr.multi.js'); + } + if (lang.includes("ja") || lang.includes("jp")) { + scriptsToLoad.push('tinyseg.js'); + } + for (var i=0; i < lang.length; i++) { + if (lang[i] != 'en') { + scriptsToLoad.push(['lunr', lang[i], 'js'].join('.')); + } + } + } + loadScripts(scriptsToLoad, onScriptsLoaded); +} + +function onScriptsLoaded () { + console.log('All search scripts loaded, building Lunr index...'); + if (data.config && data.config.separator && data.config.separator.length) { + lunr.tokenizer.separator = new RegExp(data.config.separator); + } + + if (data.index) { + index = lunr.Index.load(data.index); + data.docs.forEach(function (doc) { + documents[doc.location] = doc; + }); + console.log('Lunr pre-built index loaded, search ready'); + } else { + index = lunr(function () { + if (lang.length === 1 && lang[0] !== "en" && lunr[lang[0]]) { + this.use(lunr[lang[0]]); + } else if (lang.length > 1) { + this.use(lunr.multiLanguage.apply(null, lang)); // spread operator not supported in all browsers: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator#Browser_compatibility + } + this.field('title'); + this.field('text'); + this.ref('location'); + + for (var i=0; i < data.docs.length; i++) { + var doc = data.docs[i]; + this.add(doc); + documents[doc.location] = doc; + } + }); + console.log('Lunr index built, search ready'); + } + allowSearch = true; + postMessage({config: data.config}); + postMessage({allowSearch: allowSearch}); +} + +function init () { + var oReq = new XMLHttpRequest(); + oReq.addEventListener("load", onJSONLoaded); + var index_path = base_path + '/search_index.json'; + if( 'function' === typeof importScripts ){ + index_path = 'search_index.json'; + } + oReq.open("GET", index_path); + oReq.send(); +} + +function search (query) { + if (!allowSearch) { + console.error('Assets for search still loading'); + return; + } + + var resultDocuments = []; + var results = index.search(query); + for (var i=0; i < results.length; i++){ + var result = results[i]; + doc = documents[result.ref]; + doc.summary = doc.text.substring(0, 200); + resultDocuments.push(doc); + } + return resultDocuments; +} + +if( 'function' === typeof importScripts ) { + onmessage = function (e) { + if (e.data.init) { + init(); + } else if (e.data.query) { + postMessage({ results: search(e.data.query) }); + } else { + console.error("Worker - Unrecognized message: " + e); + } + }; +} diff --git a/sitemap.xml b/sitemap.xml new file mode 100644 index 00000000..0f8724ef --- /dev/null +++ b/sitemap.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/sitemap.xml.gz b/sitemap.xml.gz new file mode 100644 index 00000000..ad87d6f1 Binary files /dev/null and b/sitemap.xml.gz differ diff --git a/troubleshooting/index.html b/troubleshooting/index.html new file mode 100644 index 00000000..20ac2333 --- /dev/null +++ b/troubleshooting/index.html @@ -0,0 +1,154 @@ + + + + + + + + Troubleshooting - Python Fire + + + + + + + + + + + + + +
+ + +
+ +
+
+
    +
  • + +
  • +
  • +
+
+
+
+
+ +

Troubleshooting

+

This page describes known issues that users of Python Fire have run into. If you +have an issue not resolved here, consider opening a +GitHub Issue.

+

Issue #19: Don't name your module "cmd"

+

If you have a module name that conflicts with the name of a builtin module, then +when Fire goes to import the builtin module, it will import your module instead. +This will result in an error, possibly an AttributeError. Specifically, do not +name your module any of the following: +sys, linecache, cmd, bdb, repr, os, re, pprint, traceback

+ +
+
+ +
+
+ +
+ +
+ +
+ + + + « Previous + + + Next » + + +
+ + + + + + + + + diff --git a/using-cli/index.html b/using-cli/index.html new file mode 100644 index 00000000..b8ca27fd --- /dev/null +++ b/using-cli/index.html @@ -0,0 +1,333 @@ + + + + + + + + Using a CLI - Python Fire + + + + + + + + + + + + + +
+ + +
+ +
+
+
    +
  • + +
  • +
  • +
+
+
+
+
+ +

Using a Fire CLI

+

Basic usage

+

Every Fire command corresponds to a Python component.

+

The simplest Fire command consists of running your program with no additional +arguments. This command corresponds to the Python component you called the +Fire function on. If you did not supply an object in the call to Fire, then +the context in which Fire was called will be used as the Python component.

+

You can append --help or -h to a command to see what Python component it +corresponds to, as well as the various ways in which you can extend the command.

+

Flags to Fire should be separated from the Fire command by an isolated -- in +order to distinguish between flags and named arguments. So, for example, to +enter interactive mode append -- -i or -- --interactive to any command. To +use Fire in verbose mode, append -- --verbose.

+

Given a Fire command that corresponds to a Python object, you can extend that +command to access a member of that object, call it with arguments if it is a +function, instantiate it if it is a class, or index into it if it is a list.

+

Read on to learn about how you can write a Fire command corresponding to +whatever Python component you're looking for.

+

Accessing members of an object

+

If your command corresponds to an object, you can extend your command by adding +the name of a member of that object as a new argument to the command. The +resulting command will correspond to that member.

+

For example, if the object your command corresponds to has a method defined on +it named 'whack', then you can add the argument 'whack' to your command, and the +resulting new command corresponds to the whack method.

+

As another example, if the object your command corresponds to has a property +named high_score, then you can add the argument 'high-score' to your command, +and the resulting new command corresponds to the value of the high_score +property.

+

Accessing members of a dict

+

If your command corresponds to a dict, you can extend your command by adding +the name of one of the dict's keys as an argument.

+

For example, widget function-that-returns-dict key will correspond to the +value of the item with key key in the dict returned by +function_that_returns_dict.

+

Accessing members of a list or tuple

+

If your command corresponds to a list or tuple, you can extend your command by +adding the index of an element of the component to your command as an argument.

+

For example, widget function-that-returns-list 2 will correspond to item 2 of +the result of function_that_returns_list.

+

Calling a function

+

If your command corresponds to a function, you can extend your command by adding +the arguments of this function. Arguments can be specified positionally, or by +name. To specify an argument by name, use flag syntax.

+

For example, suppose your command corresponds to the function double:

+
def double(value=0):
+  return 2 * value
+
+

Then you can extend your command using named arguments as command --value 5, +or using positional arguments as command 5. In both cases, the new command +corresponds to the result of the function, in this case the number 10.

+

You can force a function that takes a variable number of arguments to be +evaluated by adding a separator (the default separator is the hyphen, "-"). This +will prevent arguments to the right of the separator from being consumed for +calling the function. This is useful if the function has arguments with default +values, or if the function accepts *varargs, or if the function accepts +**kwargs.

+

See also the section on Changing the Separator.

+

Instantiating a class

+

If your command corresponds to a class, you can extend your command by adding +the arguments of the class's __init__ function. Arguments must be specified +by name, using the flags syntax. See the section on +calling a function for more details.

+

Similarly, when passing arguments to a callable object (an object with a custom +__call__ function), those arguments must be passed using flags syntax.

+

Using Flags with Fire CLIs

+

Command line arguments to a Fire CLI are normally consumed by Fire, as described +in the Basic Usage section. In order to set Flags, put the flags +after the final standalone -- argument. (If there is no -- argument, then no +arguments are used for flags.)

+

For example, to set the alsologtostderr flag, you could run the command: +widget bang --noise=boom -- --alsologtostderr. The --noise argument is +consumed by Fire, but the --alsologtostderr argument is treated as a normal +Flag.

+

All CLIs built with Python Fire share some flags, as described in the next +sections.

+

Python Fire's Flags

+

As described in the Using Flags section, you must add an +isolated -- argument in order to have arguments treated as Flags rather than +be consumed by Python Fire. All arguments to a Fire CLI after the final +standalone -- argument are treated as Flags.

+

The following flags are accepted by all Fire CLIs: +--interactive/-i, +--help/-h, +--separator, +--completion, +--trace, +and --verbose/-v, +as described in the following sections.

+

--interactive: Interactive mode

+

Call widget -- --interactive or widget -- -i to enter interactive mode. This +will put you in an IPython REPL, with the variable widget already defined.

+

You can then explore the Python object that widget corresponds to +interactively using Python.

+

Note: if you want fire to start the IPython REPL instead of the regular Python one, +the ipython package needs to be installed in your environment.

+

--completion: Generating a completion script

+

Call widget -- --completion to generate a completion script for the Fire CLI +widget. To save the completion script to your home directory, you could e.g. +run widget -- --completion > ~/.widget-completion. You should then source this +file; to get permanent completion, source this file from your .bashrc file.

+

Call widget -- --completion fish to generate a completion script for the Fish +shell. Source this file from your fish.config.

+

If the commands available in the Fire CLI change, you'll have to regenerate the +completion script and source it again.

+

--help: Getting help

+

Let say you have a command line tool named widget that was made with Fire. How +do you use this Fire CLI?

+

The simplest way to get started is to run widget -- --help. This will give you +usage information for your CLI. You can always append -- --help to any Fire +command in order to get usage information for that command and any subcommands.

+

Additionally, help will be displayed if you hit an error using Fire. For +example, if you try to pass too many or too few arguments to a function, then +help will be displayed. Similarly, if you try to access a member that does not +exist, or if you index into a list with too high an index, then help will be +displayed.

+

The displayed help shows information about which Python component your command +corresponds to, as well as usage information for how to extend that command.

+

--trace: Getting a Fire trace

+

In order to understand what is happening when you call Python Fire, it can be +useful to request a trace. This is done via the --trace flag, e.g. +widget whack 5 -- --trace.

+

A trace provides step by step information about how the Fire command was +executed. In includes which actions were taken, starting with the initial +component, leading to the final component represented by the command.

+

A trace is also shown alongside the help if your Fire command reaches an error.

+

--separator: Changing the separator

+

As described in Calling a Function, you can use a +separator argument when writing a command that corresponds to calling a +function. The separator will cause the function to be evaluated or the class to +be instantiated using only the arguments left of the separator. Arguments right +of the separator will then be applied to the result of the function call or to +the instantiated object.

+

The default separator is -.

+

If you want to supply the string "-" as an argument, then you will have to +change the separator. You can choose a new separator by supplying the +--separator flag to Fire.

+

Here's an example to demonstrate separator usage. Let's say you have a function +that takes a variable number of args, and you want to call that function, and +then upper case the result. Here's how to do it:

+
# Here's the Python function
+def display(arg1, arg2='!'):
+  return arg1 + arg2
+
+
# Here's what you can do from Bash (Note: the default separator is the hyphen -)
+display hello                         # hello!
+display hello upper                   # helloupper
+display hello - upper                 # HELLO!
+display - SEP upper -- --separator SEP    # -!
+
+

Notice how in the third and fourth lines, the separator caused the display +function to be called with the default value for arg2. In the fourth example, +we change the separator to the string "SEP" so that we can pass '-' as an +argument.

+

--verbose: Verbose usage

+

Adding the -v or --verbose flag turns on verbose mode. This will eg +reveal private members in the usage string. Often these members will not +actually be usable from the command line tool. As such, verbose mode should be +considered a debugging tool, but not fully supported yet.

+ +
+
+ +
+
+ +
+ +
+ +
+ + + + « Previous + + + Next » + + +
+ + + + + + + + +