# -*- coding: utf-8 -*-
from setuptools import setup

packages = \
['clima', 'clima.fire']

package_data = \
{'': ['*']}

install_requires = \
['bleach>=4.1.0,<5.0.0',
 'cryptography>=35.0.0,<36.0.0',
 'python-dotenv==0.15.0',
 'setuptools>=58.2.0,<59.0.0',
 'six',
 'tabulate>=0.8.7,<0.9.0',
 'wheel']

setup_kwargs = {
    'name': 'clima',
    'version': '0.7.13',
    'description': 'Simple boilerplate for cli scripts',
    'long_description': '<img src="https://raw.githubusercontent.com/d3rp/clima/master/clima.png" align="left" /> Create a command line interface with minimal setup.\n\n\n[![PyPI](https://img.shields.io/pypi/v/clima)](https://pypi.org/project/clima/)\n[![Python versions](https://img.shields.io/pypi/pyversions/clima)]()\n[![PyPI license](https://img.shields.io/pypi/l/clima)]() \n[![Build status](https://app.travis-ci.com/d3rp/clima.svg?branch=master)](https://app.travis-ci.com/github/d3rp/clima)\n[![Dependencies](https://badges.hiptest.com/librariesio/release/github/d3rp/clima)](https://libraries.io/pypi/clima) \n\n # clima - command line interface with a schema \n\n##### Table of contents\n\n  * [Briefly](#briefly)\n     * [Features](#features)\n     * [Cli definition](#cli-definition)\n     * [Configuration object in a spiffy](#configuration-object-in-a-spiffy)\n  * [Installing](#installing)\n  * [Usage](#usage)\n  * [Examples and platforms](#examples-and-platforms)\n     * [Testing the examples](#testing-the-examples)\n  * [Version printing](#version-printing)\n  * [Autocompletion](#autocompletion)\n     * [..in IDEs (wip)](#in-ides-wip)\n     * [..in bash](#in-bash)\n  * [Post init hook](#post-init-hook)\n     * [Cli.post_init()](#clipost_init)\n     * [Schema.post_init()](#schemapost_init)\n  * [Configuration options](#configuration-options)\n     * [Configuration file and environment variables](#configuration-file-and-environment-variables)\n     * [Type casting with configuration definition](#type-casting-with-configuration-definition)\n     * [Configuration file in the home directory](#configuration-file-in-the-home-directory)\n     * [.env file](#env-file)\n     * [Password unwrapping/decryption with pass](#password-unwrappingdecryption-with-pass)\n  * [Additional features via Fire](#additional-features-via-fire)\n  * [Truncated error printing](#truncated-error-printing)\n  * [Ways to run the script for the uninitiated](#ways-to-run-the-script-for-the-uninitiated)\n     * [Linking executable script to ~/.local/bin](#linking-executable-script-to-localbin)\n     * [Packaging a module (pip ready)](#packaging-a-module-pip-ready)\n  * [Building/Installing from source](#buildinginstalling-from-source)\n  * [Long description and background](#long-description-and-background)\n     * [Why another cli framework?](#why-another-cli-framework)\n  * [Dependencies](#dependencies)\n   \n## Briefly\n\n### Features\n\nClima handles loading and parsing command\nline arguments with some off-the-shelf features, including:\n\n- a global configuration object\n    - a quick definition of defaults\n    - defining defaults doubles as a description for help on the command line\n    - type handling with annotations\n- definitions with configuration files\n- env variables\n    - loading .env files\n- secrets stored with [pass](https://www.passwordstore.org/)\n- post_init hook\n\n### Cli definition\n\nCreating a command-line interface for your program:\n\n1. Import all necessary parts from the package clima\n1. (optional) Define configuration i.e., Schema\n1. Define the command line commands i.e., CLI-class:\n\n![example ascii](https://raw.githubusercontent.com/d3rp/clima/master/example.svg)\n\nExample: to set up a configuration and a command-line interface ready to go.\n\n    from clima import c\n    \n    @c\n    class Cli:\n        def say_hi(self):\n            print(\'oh hi - whatever this is..\')\n            \n            \nThe command line usage form could be as simple as:\n  \n     my_tool say_hi\n  \n### Configuration object in a spiffy\n\n    from clima import c\n    \n    # Defining the settings (configuration object)\n    class S(Schema):\n        place = \'world\'\n        \n    @c\n    class Cli:\n        def say_hi(self):\n            # using configuration object \'c\'\n            print(f\'oh hi - {c.place}\')\n            \n            \nAgain, the command line usage form could be as simple as:\n  \n     my_tool say_hi\n     my_tool say_hi --place \'other world\'\n \nSee the `examples` folder and other sections for more examples. For example, the folder includes [something that resembles\nthe example above](examples/readme_example.py).\n  \n\n## Installing\n\n    pip install --user clima\n\n[toc](#table-of-contents)\n\n   \n\n## Usage\n\nSee the example file in [`examples/script_example.py`](examples/script_example.py). Here\'s a rundown of the individual\nparts in such a script (adapted from another example at [module example](examples/module_example)).\n\nFirst, import the required components:\n\n    from clima import c, Schema\n    \nIn your code, define the `Schema` subclass:\n\n    class Configuration(Schema):\n        a: str = \'A\'  # a description\n        x: int = 1  # x description\n\nHere "Configuration" is an arbitrary name, no magic there. The inherited `Schema` class\ndefines the attributes (i.e. `a` and `x` in this example). \n\nNote the specific formatting of the `Schema` subclass:\n\n        # attribute[: type] = default value  [# Description for the --help]\n        a: str = \'A\'  # a description\n       \n`a` is the attribute that can be called in the code later with `c.a`. In this example, it has a type of \'str\' and a default\nvalue of \'A\'. The values in square brackets `[]` are\noptional.\n\nClima parses the comment after the definition for the command-line help printout. In other words, Clima parses all of these parts to be displayed when the program is called using the argument \'--help\'. For example like this:\n\n    ./script.py foo -h\n    \nWill now produce:\n\n     Usage:       script.py foo [ARGS]\n     \n     Description: Args:\n         --a (str): a description (Default is \'A\')\n         --x (int): x description (Default is 1)    \n\nThe example shown in this readme is at `examples/readme_example.py`.\n\nClima parses the methods as subcommands with their respective doc-strings, when the class is wrapped with the decorator `@c`, and will show these in the subcommand\'s help printout. \n\nThe subcommands should be defined somewhat as follows:\n\n    @c\n    class Cli:\n        def subcommand_foo(self):\n            """This will be shown in --help for subcommand-foo"""\n            print(\'foo\')\n            print(c.a)\n            print(c.x)\n\n        def subcommand_bar(self):\n            """This will be shown in --help for subcommand-bar"""\n            print(\'bar\')\n\nNote the double usage of the `c` - first as a decorator and later as the parsed configuration\ninside the method:\n\n    ...\n        ...\n        print(c.a)\n        print(c.x)\n\nAs a decorator, `@c` defines the class to be parsed as the subcommands. As an object `c` it is used to access all the arguments.\n\n[toc](#table-of-contents)\n\n## Examples and platforms\n\nTried and used on Linux, macOS, and windows. However, packaging and dependency management in python is sometimes hairy and\nyour mileage may vary.\n\nMore examples in the [examples directory](examples) with printouts of the defined subcommands and helps.\n\n\n### Testing the examples\n\nThe [examples](examples) can be tried out by cloning the repo and running from repo directory root (on linux and the like):\n\n    git clone https://github.com/d3rp/clima.git \n    cd clima\n    PYTHONPATH=$PWD python ./examples/readme_example.py foo -h\n\nRunning the examples that wrap a module:\n\n    PYTHONPATH=$PWD python ./examples/module_example/__main__.py -h\n    PYTHONPATH=$PWD python ./examples/module_example/__main__.py subcommand-foo -h\n    PYTHONPATH=$PWD python ./examples/module_example/__main__.py subcommand-bar\n    ...\n\nThe output should resemble this (fire v0.1.3 prints out Args, fire v0.2.1 does not (though it looks much nicer))\n\n```\n$ tester subcommand-foo -- -h\n\nType:        method\nString form: <bound method Cli.subcommand_foo of <__main__.Cli object at 0x000002995AD74BE0>>\nFile:        C:\\Users\\foobar\\code\\py\\clima\\tester\\__main__.py\nLine:        18\nDocstring:   This will be shown in --help for subcommand-foo\nArgs:\n    --a (str): a description (Default is \'A\')\n    --x (int): x description (Default is 1)\n\nUsage:       __main__.py subcommand-foo [--X ...]\n```\n\nAll of the example scripts can be run by installing [poetry](https://python-poetry.org) and running the `run_examples.bash`\nscript:\n\n    pip install --user poetry\n    ./run_examples.bash\n   \n[toc](#table-of-contents)\n\n## Version printing\n\nVersion printing works via the `version` subcommand and provides a version check functionality for (poetry) packaged scripts. Thus with bumping the version with poetry, Clima will handle parsing the current version of the packaged tool thus providing a subcommand such as:\n\n    my_tool version\n   \nClima parses the `version` attribute into the `c` object, so if you want control over it, you can overwrite it with the `post_init` or by handling the `c.version` otherwise.\n\n## Autocompletion\n \n### ..in IDEs (wip)\n\nAlso, to enable autocompletion in IDEs, this hack suffices:\n\n    c: Configuration = c\n\nPut it in the "global namespace" e.g. just after defining the template. See the [`examples/script_example.py`](examples/script_example.py) for a specific example.\n\nWhen all is complete, the imported `c` variable should have all the bits and pieces for the configuration. It can be\nused inside the Cli class as well as imported around the codebase thus encapsulating all the configurations into one\ncontainer with quick access to the attributes (`c.a`, `c.x`, ...).\n\n### ..in bash\n\nRunning the script with `-- --completion` arguments should print an autocompletion declaration to include in a bash completions file:\n\n    my_tool -- --completion\n  \nTBD: zsh etc. completions\n\n## Post init hook\n\nThere are two ways to define a `post_init` hook depending on the `Schema` subclass or the `Cli` definition.\n\n### Cli.post_init()\n\nOn some occasions, it is helpful to deduce specific defaults from the given arguments, \nfor example, in a cross-platform build allowing\nonly minimal CLI arguments. For those cases, there is a `post_init` hook\nWhen defining the `post_init()` in the `Cli` class, for example:\n\n    @c\n    class Cli:\n    \n        @staticmethod\n        def post_init(s):\n            if s.platform = \'win\':\n                self.bin_path = \'c:/Users/foo/bar\'\n            else:\n                s.bin_path = \'/Users/mac/sth\'\n            \n        def subcommand(self):\n            pass\n               \nThe method will have access to the cli args, but can not introduce new variables to the schema.\n\nThis is arguably the more useful of the two variations of post_inits.\n\nNote: The signature of the `post_init()` differs in these two options. For the time being, it is a `@staticmethod`\n\n### Schema.post_init()\n\nThis alternative provides post_init-like features that are overrideable with the CLI arguments.\n\n    class SoAdvanced(Schema):\n    \n        platform: str = \'win\'  # a description\n        bin_path: pathlib.Path = \'\'  # x description\n        \n        def post_init(self, *args):\n            if self.platform = \'win\':\n                self.win_specific_field = \'All your files are locked by us..\'\n            \n                \nNote: This post_init() does not have access to the CLI arguments, but the `Schema`\'s post_init can introduce new\nattributes/properties/fields/arguments to the configuration, which the `Cli`-class post-init can\'t.\nThe `Schema`\'s `post_init()` hook is run after the schema object\'s initialisation, but BEFORE the command-line object\'s initialisation. \n\n\n[toc](#table-of-contents)\n\n## Configuration options\n\nIt is tedious to write a long list of parameters on the command line when most use cases follow a similar pattern. There are several options to choose from to facilitate the use of configurations.\n\nThe `c` decorator/configuration chains multiple configuration options together in order of priority (lower numbers refer to higher priority):\n\n1. command line arguments\n1. Environment variables\n1. .env file\n1. configuration file definitions\n1. decrypted passwords from `~/.password-store` if gnugpg is installed\n1. defaults in the subclass inheriting `Schema`\n\n### Configuration file and environment variables\n\n\nThe configuration file should be named with either the postfix `.cfg` or `.conf`, for example `foo.conf`, and have an ini type formatting including\nan explicit \'Clima\' section:\n\n    # foo.conf\n    [Clima]\n    x = 2\n\nThe keys are the same as in the schema class\' declaration. Default values can be defined for all, some or none of the attributes. The same applies to the env variables.\n    \n    # linux example\n    X=2 tester subcommand-foo\n    \nA configuration file defined this way can be located in the current working directory or - if your `Schema` defines a\n `cwd` field - there. Clima\nwill try to use the first configuration file it finds, so that might produce some caveats.\n\n    class Conf(Schema):\n        cwd = \'\'\n\n    # Running ./script.py --cwd <folder> would automatically load the first *.conf file in <folder>\n\n### Type casting with configuration definition\n \nThe `Schema` definition can have type annotations, which are used to cast the given arguments. For example\n\n    class C(Schema):\n        p: Path = \'\'  # Path to something\n\nResults in `c.p`\'s type cast as `Path`.   \n\n### Configuration file in the home directory\n\nYou can also define the config file in the configuration class (one inheriting `Schema`) by defining the\nmagic field `CFG`.\n\nFor example, lets say the command `my_tool` (packaged etc) has a user configuration file at `~/.my_tool.conf`. This\ncan now be handled with just adding `CFG = Path.home() / \'.my_tool.conf` to the Schema:\n\n    from pathlib import Path\n    \n    class S(Schema):\n        bing = \'bang\'\n        CFG = Path.home() / \'.my_tool.conf\'\n    \nThen, for example, the configuration file would be written as:\n\n    #~/.my_tool.conf\n    [Clima]\n    bing = diudiu\n\nRunning the command `my_tool` would produce the value in the configuration file, though the arguments can still be overriden. \n\n    my_tool run \n    # diudiu\n    \n    my_tool run --bing bam\n    # bam\n    \n### .env file\n \n This is handled by [dotenv](https://github.com/theskumar/python-dotenv). In short, all the defaults defined in the\n `Schema` subclass can be overridden either by:\n \n    <field> = <value>\n\nor\n\n    export <field> = <value>\n    \n### Password unwrapping/decryption with pass\n \n Note: Currently this works only for gpg-keys without password. It\'s not ideal, but it\'s better than plain text `.env`\n files ;)\n \n Note 2: Leading and trailing whitespace (including `\\n` linefeeds) are stripped, when decrypted.\n \n [pass](https://passwordstore.org) can be used to store passwords as gpg encrypted files under the home directory. Clima\n uses the default path of ~/.password-store and the files found within. It will then match the arguments with the \n stored passwords, for example:\n \n     tree -A ~/.password-store                                                                                                                                                                                                                                                                            \ue0b2 ✔ | 41s | anaconda3 \ue73c\n     /Users/me/.password-store\n     ├── work\n     │   ├── ci\n     │   │   ├── sign_id.gpg\n     │   │   ├── sign_pw.gpg\n     ... ... ...\n \n And an according `Schema` definition:\n \n     class Conf(Schema):\n         sign_id: str = \'\'  # signing id for the CI\n         sign_pw: str = \'\'  # signing pw for the CI\n \n Would accept those arguments as cli arguments, or if omitted, would traverse through the `.password-store` and decrypt the\n found `sign_id.gpg` and `sign_pw.gpg` placing the values found in the configuration object `c`.\n     \n [toc](#table-of-contents)      \n \n## Additional features via Fire\n\nSee the [Python Fire\'s Flags](https://github.com/google/python-fire/blob/master/docs/using-cli.md#python-fires-flags)\ndocumentation for nice additional features such as:\n\n    # e.g. tester.py is our cli program\n    tester.py subcommand-foo -- --trace\n    tester.py -- --interactive\n    tester.py -- --completion\n    \n\n## Truncated error printing\n\nThis feature rose as an opinionated option, and I admit, it should be something the user could bypass. Even though I have used python for a few years professionally, I am still not satisfied with its error printing. When raising exceptions, Clima truncates the error lists and tries to provide a more readable version of the "first" point of failure. The whole traceback is written into a logfile `exception_traceback.log` to examine if the truncated output provides insufficient information.\n\nNote: When running the examples, the `exception_traceback.log` file will be written inside the `examples` directory\n\n\n## Ways to run the script for the uninitiated\n\nHere are some suggestions on how to wrap scripts using Clima.\n\n### Linking executable script to ~/.local/bin\n\nLet\'s say those lines were written in a file named `script.py`. Command line usage in a terminal would then be e.g.:\n\n    python script.py foo\n    python script.py foo --a 42\n\nAdding this line in `script.py`\n\n    #!/usr/bin/env python\n    \nand changing its execution permissions (mac, linux):\n\n    chmod +x script.py\n   \nAllows for a shorter means of execution:\n\n    ./script.py foo\n\nNow this could be linked as an adhoc command for example:\n\n    ln -s $PWD/script.py ~/.local/bin/my_command\n\n### Packaging a module (pip ready)\n\nFor a pip-installable package, one could [package this as a runnable command](https://github.com/d3rp/my_tool) -\npublish in the public or one\'s private pypi etc - and then approach the convenience factor shown at first.\n\n    pip install my_tool\n    my_command foo -h\n\nTo publish with poetry is quite straight forward. First create an account in pypi.org and then:\n\n    cd <project directory>\n    poetry build\n    poetry publish\n \nYou can use `version` to bump up versions:\n    \n    poetry version patch\n\n## Building/Installing from source\n\nThis repo is based on [poetry](https://poetry.eustace.io).\n\n    git clone https://github.com/d3rp/clima.git \n    cd clima\n    poetry install --no-dev\n\nThe `--no-dev` is for to install the running environment without development tooling.\n\n[toc](#table-of-contents)\n\n## Long description and background\n\nYou can write your subcommands as a class encapsulating the "business logic". Clima encapsulates the command-line arguments as a container, mapping its attributes as you declare them in a simple schema class.\n\nIn other words, you can use this to wrap your scripts as command line commands without resorting to Bash or maintaining argument parsing in python. It removes the need to duplicate comments to have `--help` remember what the arguments were and what they did. Sprinkling some decorator magic offers a typical user experience of a CLI program (e.g. argument parsing and validation, --help, subcommands, ...).\n\nThe premise behind Clima is that a simple script usually uses a script wide global configuration throughout the user code, or in other words, a context for the program accessed in different parts of the code. Clima populates that context, or configuration, with given arguments falling back on defaults in the code and some further complimentary options. Those are then made accessible via a global `c` variable, or container, that can be tossed around the code base with minimal additional effort. \nWith almost no additional effort, Clima can provide autocompletion in IDEs (as attributes). This feature helps when extending your code as the autocomplete kicks in after typing `c.` offering those fields in your "schema" as attributes.\n \n### Why another cli framework?\n\nClima does not try to cater as a feature-complete CLI framework like the ones listed below. It is a package to help with boilerplate to get quick but reusable tools for your workflow.\n\nOther options for a full-featured CLI experience:\n\n* [docopt](https://docopt.org)\n* [fire](https://github.com/google/python-fire)\n* [cleo](https://github.com/sdispater/cleo)\n* [click](https://click.palletsprojects.com)\n* [typer](https://github.com/tiangolo/typer)\n\n\n## Dependencies\n\n* [dotenv](https://github.com/theskumar/python-dotenv)\n* gnugpg - this is pass through though. If it\'s not installed, the feature is not in use.\n\n* fire - [python-fire](https://github.com/google/python-fire) from google does the cli wrapping / forked and included \ninto the repo - I wanted to have the version 0.1.x formatting and help output with few hacks of my own\n\n\n[toc](#table-of-contents)\n',
    'author': 'Joni Turunen',
    'author_email': 'rojun.itu@gmail.com',
    'maintainer': None,
    'maintainer_email': None,
    'url': 'https://github.com/d3rp/clima.git',
    'packages': packages,
    'package_data': package_data,
    'install_requires': install_requires,
    'python_requires': '>=3.8.0,<4.0.0',
}


setup(**setup_kwargs)
