scriptconfig
Advanced tools
+201
| Apache License | ||
| Version 2.0, January 2004 | ||
| http://www.apache.org/licenses/ | ||
| TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | ||
| 1. Definitions. | ||
| "License" shall mean the terms and conditions for use, reproduction, | ||
| and distribution as defined by Sections 1 through 9 of this document. | ||
| "Licensor" shall mean the copyright owner or entity authorized by | ||
| the copyright owner that is granting the License. | ||
| "Legal Entity" shall mean the union of the acting entity and all | ||
| other entities that control, are controlled by, or are under common | ||
| control with that entity. For the purposes of this definition, | ||
| "control" means (i) the power, direct or indirect, to cause the | ||
| direction or management of such entity, whether by contract or | ||
| otherwise, or (ii) ownership of fifty percent (50%) or more of the | ||
| outstanding shares, or (iii) beneficial ownership of such entity. | ||
| "You" (or "Your") shall mean an individual or Legal Entity | ||
| exercising permissions granted by this License. | ||
| "Source" form shall mean the preferred form for making modifications, | ||
| including but not limited to software source code, documentation | ||
| source, and configuration files. | ||
| "Object" form shall mean any form resulting from mechanical | ||
| transformation or translation of a Source form, including but | ||
| not limited to compiled object code, generated documentation, | ||
| and conversions to other media types. | ||
| "Work" shall mean the work of authorship, whether in Source or | ||
| Object form, made available under the License, as indicated by a | ||
| copyright notice that is included in or attached to the work | ||
| (an example is provided in the Appendix below). | ||
| "Derivative Works" shall mean any work, whether in Source or Object | ||
| form, that is based on (or derived from) the Work and for which the | ||
| editorial revisions, annotations, elaborations, or other modifications | ||
| represent, as a whole, an original work of authorship. For the purposes | ||
| of this License, Derivative Works shall not include works that remain | ||
| separable from, or merely link (or bind by name) to the interfaces of, | ||
| the Work and Derivative Works thereof. | ||
| "Contribution" shall mean any work of authorship, including | ||
| the original version of the Work and any modifications or additions | ||
| to that Work or Derivative Works thereof, that is intentionally | ||
| submitted to Licensor for inclusion in the Work by the copyright owner | ||
| or by an individual or Legal Entity authorized to submit on behalf of | ||
| the copyright owner. For the purposes of this definition, "submitted" | ||
| means any form of electronic, verbal, or written communication sent | ||
| to the Licensor or its representatives, including but not limited to | ||
| communication on electronic mailing lists, source code control systems, | ||
| and issue tracking systems that are managed by, or on behalf of, the | ||
| Licensor for the purpose of discussing and improving the Work, but | ||
| excluding communication that is conspicuously marked or otherwise | ||
| designated in writing by the copyright owner as "Not a Contribution." | ||
| "Contributor" shall mean Licensor and any individual or Legal Entity | ||
| on behalf of whom a Contribution has been received by Licensor and | ||
| subsequently incorporated within the Work. | ||
| 2. Grant of Copyright License. Subject to the terms and conditions of | ||
| this License, each Contributor hereby grants to You a perpetual, | ||
| worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
| copyright license to reproduce, prepare Derivative Works of, | ||
| publicly display, publicly perform, sublicense, and distribute the | ||
| Work and such Derivative Works in Source or Object form. | ||
| 3. Grant of Patent License. Subject to the terms and conditions of | ||
| this License, each Contributor hereby grants to You a perpetual, | ||
| worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
| (except as stated in this section) patent license to make, have made, | ||
| use, offer to sell, sell, import, and otherwise transfer the Work, | ||
| where such license applies only to those patent claims licensable | ||
| by such Contributor that are necessarily infringed by their | ||
| Contribution(s) alone or by combination of their Contribution(s) | ||
| with the Work to which such Contribution(s) was submitted. If You | ||
| institute patent litigation against any entity (including a | ||
| cross-claim or counterclaim in a lawsuit) alleging that the Work | ||
| or a Contribution incorporated within the Work constitutes direct | ||
| or contributory patent infringement, then any patent licenses | ||
| granted to You under this License for that Work shall terminate | ||
| as of the date such litigation is filed. | ||
| 4. Redistribution. You may reproduce and distribute copies of the | ||
| Work or Derivative Works thereof in any medium, with or without | ||
| modifications, and in Source or Object form, provided that You | ||
| meet the following conditions: | ||
| (a) You must give any other recipients of the Work or | ||
| Derivative Works a copy of this License; and | ||
| (b) You must cause any modified files to carry prominent notices | ||
| stating that You changed the files; and | ||
| (c) You must retain, in the Source form of any Derivative Works | ||
| that You distribute, all copyright, patent, trademark, and | ||
| attribution notices from the Source form of the Work, | ||
| excluding those notices that do not pertain to any part of | ||
| the Derivative Works; and | ||
| (d) If the Work includes a "NOTICE" text file as part of its | ||
| distribution, then any Derivative Works that You distribute must | ||
| include a readable copy of the attribution notices contained | ||
| within such NOTICE file, excluding those notices that do not | ||
| pertain to any part of the Derivative Works, in at least one | ||
| of the following places: within a NOTICE text file distributed | ||
| as part of the Derivative Works; within the Source form or | ||
| documentation, if provided along with the Derivative Works; or, | ||
| within a display generated by the Derivative Works, if and | ||
| wherever such third-party notices normally appear. The contents | ||
| of the NOTICE file are for informational purposes only and | ||
| do not modify the License. You may add Your own attribution | ||
| notices within Derivative Works that You distribute, alongside | ||
| or as an addendum to the NOTICE text from the Work, provided | ||
| that such additional attribution notices cannot be construed | ||
| as modifying the License. | ||
| You may add Your own copyright statement to Your modifications and | ||
| may provide additional or different license terms and conditions | ||
| for use, reproduction, or distribution of Your modifications, or | ||
| for any such Derivative Works as a whole, provided Your use, | ||
| reproduction, and distribution of the Work otherwise complies with | ||
| the conditions stated in this License. | ||
| 5. Submission of Contributions. Unless You explicitly state otherwise, | ||
| any Contribution intentionally submitted for inclusion in the Work | ||
| by You to the Licensor shall be under the terms and conditions of | ||
| this License, without any additional terms or conditions. | ||
| Notwithstanding the above, nothing herein shall supersede or modify | ||
| the terms of any separate license agreement you may have executed | ||
| with Licensor regarding such Contributions. | ||
| 6. Trademarks. This License does not grant permission to use the trade | ||
| names, trademarks, service marks, or product names of the Licensor, | ||
| except as required for reasonable and customary use in describing the | ||
| origin of the Work and reproducing the content of the NOTICE file. | ||
| 7. Disclaimer of Warranty. Unless required by applicable law or | ||
| agreed to in writing, Licensor provides the Work (and each | ||
| Contributor provides its Contributions) on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| implied, including, without limitation, any warranties or conditions | ||
| of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A | ||
| PARTICULAR PURPOSE. You are solely responsible for determining the | ||
| appropriateness of using or redistributing the Work and assume any | ||
| risks associated with Your exercise of permissions under this License. | ||
| 8. Limitation of Liability. In no event and under no legal theory, | ||
| whether in tort (including negligence), contract, or otherwise, | ||
| unless required by applicable law (such as deliberate and grossly | ||
| negligent acts) or agreed to in writing, shall any Contributor be | ||
| liable to You for damages, including any direct, indirect, special, | ||
| incidental, or consequential damages of any character arising as a | ||
| result of this License or out of the use or inability to use the | ||
| Work (including but not limited to damages for loss of goodwill, | ||
| work stoppage, computer failure or malfunction, or any and all | ||
| other commercial damages or losses), even if such Contributor | ||
| has been advised of the possibility of such damages. | ||
| 9. Accepting Warranty or Additional Liability. While redistributing | ||
| the Work or Derivative Works thereof, You may choose to offer, | ||
| and charge a fee for, acceptance of support, warranty, indemnity, | ||
| or other liability obligations and/or rights consistent with this | ||
| License. However, in accepting such obligations, You may act only | ||
| on Your own behalf and on Your sole responsibility, not on behalf | ||
| of any other Contributor, and only if You agree to indemnify, | ||
| defend, and hold each Contributor harmless for any liability | ||
| incurred by, or claims asserted against, such Contributor by reason | ||
| of your accepting any such warranty or additional liability. | ||
| END OF TERMS AND CONDITIONS | ||
| APPENDIX: How to apply the Apache License to your work. | ||
| To apply the Apache License to your work, attach the following | ||
| boilerplate notice, with the fields enclosed by brackets "{}" | ||
| replaced with your own identifying information. (Don't include | ||
| the brackets!) The text should be enclosed in the appropriate | ||
| comment syntax for the file format. We also recommend that a | ||
| file or class name and description of purpose be included on the | ||
| same "printed page" as the copyright notice for easier | ||
| identification within third-party archives. | ||
| Copyright 2022 "Kitware 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. |
| include requirements/*.txt |
+865
| Metadata-Version: 2.1 | ||
| Name: scriptconfig | ||
| Version: 0.8.0 | ||
| Summary: Easy dict-based script configuration with CLI support | ||
| Home-page: https://gitlab.kitware.com/utils/scriptconfig/ | ||
| Author: Kitware Inc., Jon Crall | ||
| Author-email: kitware@kitware.com, jon.crall@kitware.com | ||
| License: Apache 2 | ||
| Classifier: Development Status :: 4 - Beta | ||
| Classifier: Intended Audience :: Developers | ||
| Classifier: Topic :: Software Development :: Libraries :: Python Modules | ||
| Classifier: Topic :: Utilities | ||
| Classifier: License :: OSI Approved :: Apache Software License | ||
| Classifier: Programming Language :: Python :: 3.6 | ||
| Classifier: Programming Language :: Python :: 3.7 | ||
| Classifier: Programming Language :: Python :: 3.8 | ||
| Classifier: Programming Language :: Python :: 3.9 | ||
| Classifier: Programming Language :: Python :: 3.10 | ||
| Classifier: Programming Language :: Python :: 3.11 | ||
| Requires-Python: >=3.6 | ||
| Description-Content-Type: text/x-rst | ||
| License-File: LICENSE | ||
| Requires-Dist: ubelt>=1.3.6 | ||
| Requires-Dist: PyYAML>=6.0.1; python_version < "4.0" and python_version >= "3.12" | ||
| Requires-Dist: PyYAML>=6.0; python_version < "3.12" and python_version >= "3.11" | ||
| Requires-Dist: PyYAML>=6.0; python_version < "3.11" and python_version >= "3.10" | ||
| Requires-Dist: PyYAML>=5.4.1; python_version < "3.10" and python_version >= "3.9" | ||
| Requires-Dist: PyYAML>=5.4.1; python_version < "3.9" and python_version >= "3.8" | ||
| Requires-Dist: PyYAML>=5.4.1; python_version < "3.8" and python_version >= "3.7" | ||
| Requires-Dist: PyYAML>=5.4.1; python_version < "3.7" and python_version >= "3.6" | ||
| Provides-Extra: all | ||
| Requires-Dist: ubelt>=1.3.6; extra == "all" | ||
| Requires-Dist: PyYAML>=6.0.1; (python_version < "4.0" and python_version >= "3.12") and extra == "all" | ||
| Requires-Dist: PyYAML>=6.0; (python_version < "3.12" and python_version >= "3.11") and extra == "all" | ||
| Requires-Dist: PyYAML>=6.0; (python_version < "3.11" and python_version >= "3.10") and extra == "all" | ||
| Requires-Dist: PyYAML>=5.4.1; (python_version < "3.10" and python_version >= "3.9") and extra == "all" | ||
| Requires-Dist: PyYAML>=5.4.1; (python_version < "3.9" and python_version >= "3.8") and extra == "all" | ||
| Requires-Dist: PyYAML>=5.4.1; (python_version < "3.8" and python_version >= "3.7") and extra == "all" | ||
| Requires-Dist: PyYAML>=5.4.1; (python_version < "3.7" and python_version >= "3.6") and extra == "all" | ||
| Requires-Dist: pytest>=6.2.5; python_version >= "3.10.0" and extra == "all" | ||
| Requires-Dist: pytest>=4.6.0; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == "all" | ||
| Requires-Dist: pytest>=4.6.0; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == "all" | ||
| Requires-Dist: pytest<=6.1.2,>=4.6.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "all" | ||
| Requires-Dist: pytest<=4.6.11,>=4.6.0; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "all" | ||
| Requires-Dist: pytest<=4.6.11,>=4.6.0; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "all" | ||
| Requires-Dist: coverage>=6.1.1; python_version >= "3.10" and extra == "all" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.10" and python_version >= "3.9") and extra == "all" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.9" and python_version >= "3.8") and extra == "all" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.8" and python_version >= "3.7") and extra == "all" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.7" and python_version >= "3.6") and extra == "all" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.6" and python_version >= "3.5") and extra == "all" | ||
| Requires-Dist: coverage>=4.3.4; (python_version < "3.5" and python_version >= "3.4") and extra == "all" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.4" and python_version >= "2.7") and extra == "all" | ||
| Requires-Dist: coverage>=4.5; (python_version < "2.7" and python_version >= "2.6") and extra == "all" | ||
| Requires-Dist: pytest-cov>=3.0.0; python_version >= "3.6.0" and extra == "all" | ||
| Requires-Dist: pytest-cov>=2.9.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "all" | ||
| Requires-Dist: pytest-cov>=2.8.1; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "all" | ||
| Requires-Dist: pytest-cov>=2.8.1; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "all" | ||
| Requires-Dist: xdoctest>=1.1.5; extra == "all" | ||
| Requires-Dist: numpy>=1.26.0; (python_version < "4.0" and python_version >= "3.12") and extra == "all" | ||
| Requires-Dist: numpy>=1.23.2; (python_version < "3.12" and python_version >= "3.11") and extra == "all" | ||
| Requires-Dist: numpy>=1.21.6; (python_version < "3.11" and python_version >= "3.10") and extra == "all" | ||
| Requires-Dist: numpy>=1.19.3; (python_version < "3.10" and python_version >= "3.9") and extra == "all" | ||
| Requires-Dist: numpy>=1.19.2; (python_version < "3.9" and python_version >= "3.8") and extra == "all" | ||
| Requires-Dist: numpy>=1.14.5; (python_version < "3.8" and python_version >= "3.7") and extra == "all" | ||
| Requires-Dist: numpy>=1.12.0; (python_version < "3.7" and python_version >= "3.6") and extra == "all" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.6" and python_version >= "3.5") and extra == "all" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.5" and python_version >= "3.4") and extra == "all" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.4" and python_version >= "2.7") and extra == "all" | ||
| Requires-Dist: omegaconf>=2.2.2; python_version >= "3.6" and extra == "all" | ||
| Requires-Dist: rich_argparse>=1.1.0; python_version >= "3.7" and extra == "all" | ||
| Requires-Dist: argcomplete>=3.0.5; extra == "all" | ||
| Provides-Extra: tests | ||
| Requires-Dist: pytest>=6.2.5; python_version >= "3.10.0" and extra == "tests" | ||
| Requires-Dist: pytest>=4.6.0; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == "tests" | ||
| Requires-Dist: pytest>=4.6.0; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == "tests" | ||
| Requires-Dist: pytest<=6.1.2,>=4.6.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "tests" | ||
| Requires-Dist: pytest<=4.6.11,>=4.6.0; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "tests" | ||
| Requires-Dist: pytest<=4.6.11,>=4.6.0; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "tests" | ||
| Requires-Dist: coverage>=6.1.1; python_version >= "3.10" and extra == "tests" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.10" and python_version >= "3.9") and extra == "tests" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.9" and python_version >= "3.8") and extra == "tests" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.8" and python_version >= "3.7") and extra == "tests" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.7" and python_version >= "3.6") and extra == "tests" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.6" and python_version >= "3.5") and extra == "tests" | ||
| Requires-Dist: coverage>=4.3.4; (python_version < "3.5" and python_version >= "3.4") and extra == "tests" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.4" and python_version >= "2.7") and extra == "tests" | ||
| Requires-Dist: coverage>=4.5; (python_version < "2.7" and python_version >= "2.6") and extra == "tests" | ||
| Requires-Dist: pytest-cov>=3.0.0; python_version >= "3.6.0" and extra == "tests" | ||
| Requires-Dist: pytest-cov>=2.9.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "tests" | ||
| Requires-Dist: pytest-cov>=2.8.1; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "tests" | ||
| Requires-Dist: pytest-cov>=2.8.1; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "tests" | ||
| Requires-Dist: xdoctest>=1.1.5; extra == "tests" | ||
| Provides-Extra: optional | ||
| Requires-Dist: numpy>=1.26.0; (python_version < "4.0" and python_version >= "3.12") and extra == "optional" | ||
| Requires-Dist: numpy>=1.23.2; (python_version < "3.12" and python_version >= "3.11") and extra == "optional" | ||
| Requires-Dist: numpy>=1.21.6; (python_version < "3.11" and python_version >= "3.10") and extra == "optional" | ||
| Requires-Dist: numpy>=1.19.3; (python_version < "3.10" and python_version >= "3.9") and extra == "optional" | ||
| Requires-Dist: numpy>=1.19.2; (python_version < "3.9" and python_version >= "3.8") and extra == "optional" | ||
| Requires-Dist: numpy>=1.14.5; (python_version < "3.8" and python_version >= "3.7") and extra == "optional" | ||
| Requires-Dist: numpy>=1.12.0; (python_version < "3.7" and python_version >= "3.6") and extra == "optional" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.6" and python_version >= "3.5") and extra == "optional" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.5" and python_version >= "3.4") and extra == "optional" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.4" and python_version >= "2.7") and extra == "optional" | ||
| Requires-Dist: omegaconf>=2.2.2; python_version >= "3.6" and extra == "optional" | ||
| Requires-Dist: rich_argparse>=1.1.0; python_version >= "3.7" and extra == "optional" | ||
| Requires-Dist: argcomplete>=3.0.5; extra == "optional" | ||
| Provides-Extra: all-strict | ||
| Requires-Dist: ubelt==1.3.6; extra == "all-strict" | ||
| Requires-Dist: PyYAML==6.0.1; (python_version < "4.0" and python_version >= "3.12") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==6.0; (python_version < "3.12" and python_version >= "3.11") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==6.0; (python_version < "3.11" and python_version >= "3.10") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.10" and python_version >= "3.9") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.9" and python_version >= "3.8") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.8" and python_version >= "3.7") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.7" and python_version >= "3.6") and extra == "all-strict" | ||
| Requires-Dist: pytest==6.2.5; python_version >= "3.10.0" and extra == "all-strict" | ||
| Requires-Dist: pytest==4.6.0; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == "all-strict" | ||
| Requires-Dist: pytest==4.6.0; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == "all-strict" | ||
| Requires-Dist: pytest<=6.1.2,==4.6.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "all-strict" | ||
| Requires-Dist: pytest<=4.6.11,==4.6.0; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "all-strict" | ||
| Requires-Dist: pytest<=4.6.11,==4.6.0; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "all-strict" | ||
| Requires-Dist: coverage==6.1.1; python_version >= "3.10" and extra == "all-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.10" and python_version >= "3.9") and extra == "all-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.9" and python_version >= "3.8") and extra == "all-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.8" and python_version >= "3.7") and extra == "all-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.7" and python_version >= "3.6") and extra == "all-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.6" and python_version >= "3.5") and extra == "all-strict" | ||
| Requires-Dist: coverage==4.3.4; (python_version < "3.5" and python_version >= "3.4") and extra == "all-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.4" and python_version >= "2.7") and extra == "all-strict" | ||
| Requires-Dist: coverage==4.5; (python_version < "2.7" and python_version >= "2.6") and extra == "all-strict" | ||
| Requires-Dist: pytest-cov==3.0.0; python_version >= "3.6.0" and extra == "all-strict" | ||
| Requires-Dist: pytest-cov==2.9.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "all-strict" | ||
| Requires-Dist: pytest-cov==2.8.1; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "all-strict" | ||
| Requires-Dist: pytest-cov==2.8.1; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "all-strict" | ||
| Requires-Dist: xdoctest==1.1.5; extra == "all-strict" | ||
| Requires-Dist: numpy==1.26.0; (python_version < "4.0" and python_version >= "3.12") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.23.2; (python_version < "3.12" and python_version >= "3.11") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.21.6; (python_version < "3.11" and python_version >= "3.10") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.19.3; (python_version < "3.10" and python_version >= "3.9") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.19.2; (python_version < "3.9" and python_version >= "3.8") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.14.5; (python_version < "3.8" and python_version >= "3.7") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.12.0; (python_version < "3.7" and python_version >= "3.6") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.6" and python_version >= "3.5") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.5" and python_version >= "3.4") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.4" and python_version >= "2.7") and extra == "all-strict" | ||
| Requires-Dist: omegaconf==2.2.2; python_version >= "3.6" and extra == "all-strict" | ||
| Requires-Dist: rich_argparse==1.1.0; python_version >= "3.7" and extra == "all-strict" | ||
| Requires-Dist: argcomplete==3.0.5; extra == "all-strict" | ||
| Provides-Extra: runtime-strict | ||
| Requires-Dist: ubelt==1.3.6; extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==6.0.1; (python_version < "4.0" and python_version >= "3.12") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==6.0; (python_version < "3.12" and python_version >= "3.11") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==6.0; (python_version < "3.11" and python_version >= "3.10") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.10" and python_version >= "3.9") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.9" and python_version >= "3.8") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.8" and python_version >= "3.7") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.7" and python_version >= "3.6") and extra == "runtime-strict" | ||
| Provides-Extra: tests-strict | ||
| Requires-Dist: pytest==6.2.5; python_version >= "3.10.0" and extra == "tests-strict" | ||
| Requires-Dist: pytest==4.6.0; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest==4.6.0; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest<=6.1.2,==4.6.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest<=4.6.11,==4.6.0; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest<=4.6.11,==4.6.0; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "tests-strict" | ||
| Requires-Dist: coverage==6.1.1; python_version >= "3.10" and extra == "tests-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.10" and python_version >= "3.9") and extra == "tests-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.9" and python_version >= "3.8") and extra == "tests-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.8" and python_version >= "3.7") and extra == "tests-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.7" and python_version >= "3.6") and extra == "tests-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.6" and python_version >= "3.5") and extra == "tests-strict" | ||
| Requires-Dist: coverage==4.3.4; (python_version < "3.5" and python_version >= "3.4") and extra == "tests-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.4" and python_version >= "2.7") and extra == "tests-strict" | ||
| Requires-Dist: coverage==4.5; (python_version < "2.7" and python_version >= "2.6") and extra == "tests-strict" | ||
| Requires-Dist: pytest-cov==3.0.0; python_version >= "3.6.0" and extra == "tests-strict" | ||
| Requires-Dist: pytest-cov==2.9.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest-cov==2.8.1; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest-cov==2.8.1; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "tests-strict" | ||
| Requires-Dist: xdoctest==1.1.5; extra == "tests-strict" | ||
| Provides-Extra: optional-strict | ||
| Requires-Dist: numpy==1.26.0; (python_version < "4.0" and python_version >= "3.12") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.23.2; (python_version < "3.12" and python_version >= "3.11") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.21.6; (python_version < "3.11" and python_version >= "3.10") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.19.3; (python_version < "3.10" and python_version >= "3.9") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.19.2; (python_version < "3.9" and python_version >= "3.8") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.14.5; (python_version < "3.8" and python_version >= "3.7") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.12.0; (python_version < "3.7" and python_version >= "3.6") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.6" and python_version >= "3.5") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.5" and python_version >= "3.4") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.4" and python_version >= "2.7") and extra == "optional-strict" | ||
| Requires-Dist: omegaconf==2.2.2; python_version >= "3.6" and extra == "optional-strict" | ||
| Requires-Dist: rich_argparse==1.1.0; python_version >= "3.7" and extra == "optional-strict" | ||
| Requires-Dist: argcomplete==3.0.5; extra == "optional-strict" | ||
| ScriptConfig | ||
| ============ | ||
| .. # TODO Get CI services running on gitlab | ||
| .. #|CircleCI| |Travis| |Codecov| |ReadTheDocs| | ||
| |GitlabCIPipeline| |GitlabCICoverage| |Appveyor| |Pypi| |PypiDownloads| | ||
| +------------------+--------------------------------------------------+ | ||
| | Read the docs | https://scriptconfig.readthedocs.io | | ||
| +------------------+--------------------------------------------------+ | ||
| | Gitlab (main) | https://gitlab.kitware.com/utils/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| | Github (mirror) | https://github.com/Kitware/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| | Pypi | https://pypi.org/project/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| The goal of ``scriptconfig`` is to make it easy to be able to define a default | ||
| configuration by **simply defining a dictionary**, and then allow that | ||
| configuration to be modified by either: | ||
| 1. Updating it with another Python dictionary (e.g. ``kwargs``) | ||
| 2. Reading a YAML/JSON configuration file, or | ||
| 3. Inspecting values on ``sys.argv``, in which case we provide a powerful | ||
| command line interface (CLI). | ||
| The simplest way to create a script config is to create a class that inherits | ||
| from ``scriptconfig.DataConfig``. Then, use class variables to define the | ||
| expected keys and default values. | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| class ExampleConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| # Wrap defaults with `Value` to provide metadata | ||
| option1 = scfg.Value('default1', help='option1 help') | ||
| option2 = scfg.Value('default2', help='option2 help') | ||
| option3 = scfg.Value('default3', help='option3 help') | ||
| # Wrapping a default with `Value` is optional | ||
| option4 = 'default4' | ||
| An instance of a config object will work similarly to a dataclass, but it also | ||
| implements methods to duck-type a dictionary. Thus a scriptconfig object can be | ||
| dropped into code that uses an existing dictionary configuration or an existing | ||
| argparse namespace configuration. | ||
| .. code-block:: python | ||
| # Use as a dictionary with defaults | ||
| config = ExampleConfig(option1=123) | ||
| print(config) | ||
| # Can access items like a dictionary | ||
| print(config['option1']) | ||
| # OR Can access items like a namespace | ||
| print(config.option1) | ||
| Use the ``.cli`` classmethod to create an extended argparse command line | ||
| interface. Options to the ``cli`` method are similar to | ||
| ``argparse.ArgumentParser.parse_args``. | ||
| .. code-block:: python | ||
| # Use as a argparse CLI | ||
| config = ExampleConfig.cli(argv=['--option2=overruled']) | ||
| print(config) | ||
| After all that, if you still aren't loving scriptconfig, or you can't use it as | ||
| a dependency in production, you can ask it to convert itself to pure-argparse | ||
| via ``print(ExampleConfig().port_to_argparse())``, and it will print out: | ||
| .. code-block:: python | ||
| import argparse | ||
| parser = argparse.ArgumentParser( | ||
| prog='ExampleConfig', | ||
| description='The docstring will be the description in the CLI help', | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ) | ||
| parser.add_argument('--option1', help='option1 help', default='default1', dest='option1', required=False) | ||
| parser.add_argument('--option2', help='option2 help', default='default2', dest='option2', required=False) | ||
| parser.add_argument('--option3', help='option3 help', default='default3', dest='option3', required=False) | ||
| parser.add_argument('--option4', help='', default='default4', dest='option4', required=False) | ||
| Of course, the above also removes extra features of scriptconfig - so its not | ||
| exactly 1-to-1, but it's close. It's also a good tool for transferring any | ||
| existing intuition about ``argparse`` to ``scriptconfig``. | ||
| Similarly there is a method which can take an existing ArgumentParser as input, | ||
| and produce a scriptconfig definition. Given the above ``parser`` object, | ||
| ``print(scfg.Config.port_from_argparse(parser, style))`` will print out: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| option1 = scfg.Value('default1', help='option1 help') | ||
| option2 = scfg.Value('default2', help='option2 help') | ||
| option3 = scfg.Value('default3', help='option3 help') | ||
| option4 = scfg.Value('default4', help='') | ||
| Goal | ||
| ---- | ||
| The idea is we want to be able to start writing a simple program with a simple | ||
| configuration and allow it to evolve with minimal refactoring. In the early | ||
| stages we will insist that there be little-to-no boilerplate, but as a program | ||
| evolves we will add boilerplate to enhance the featurefull-ness of our program. | ||
| When we start coding we should aim for something like this: | ||
| .. code-block:: python | ||
| def my_function(): | ||
| config = { | ||
| 'simple_option1': 1, | ||
| 'simple_option2': 2, | ||
| } | ||
| # Early algorithmic and debugging logic | ||
| ... | ||
| As we evolve our code, we can plug scriptconfig in like this: | ||
| .. code-block:: python | ||
| def my_function(): | ||
| default_config = { | ||
| 'simple_option1': 1, | ||
| 'simple_option2': 2, | ||
| } | ||
| import scriptconfig | ||
| class MyConfig(scriptconfig.DataConfig): | ||
| __default__ = default_config | ||
| config = MyConfig() | ||
| # Transition algorithmic and debugging logic | ||
| ... | ||
| It's not pretty, but it gives us the ability to a fairly advanced CLI right | ||
| away (i.e by calling the ``.cli`` classmethod) without any major sacrifice to | ||
| code simplicity. However, as a project evolves we may eventually want to | ||
| refactor our CLI to gain full control over the metadata in our configuration an | ||
| CLI. Scriptconfig has a tool to help with this too. Given this janky definition, | ||
| we can port to a more ellegant style. We can run | ||
| ``print(config.port_to_dataconf())`` which prints: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| argparse CLI generated by scriptconfig 0.7.12 | ||
| """ | ||
| simple_option1 = scfg.Value(1, help=None) | ||
| simple_option2 = scfg.Value(2, help=None) | ||
| And then use that to make the refactor much easier. | ||
| The final state of a scriptconfig program might look something like this: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| This is my CLI description | ||
| """ | ||
| simple_option1 = scfg.Value(1, help=ub.paragraph( | ||
| ''' | ||
| A reasonably detailed but concise description of an argument. | ||
| About one paragraph is reasonable. | ||
| ''') | ||
| simple_option2 = scfg.Value(2, help='more help is better') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| config = cls.cli(cmdline=cmdline, data=kwargs) | ||
| my_function(config) | ||
| def my_function(config): | ||
| # Continued algorithmic and debugging logic | ||
| ... | ||
| Note that the fundamental impact on the ``...`` -- i.e. the intereting part of | ||
| the function -- remain completely unchanged! From it's point of view, you never | ||
| did anything to the original ``config`` dictionary, because scriptconfig | ||
| duck-typed it at every stage. | ||
| Installation | ||
| ------------ | ||
| The `scriptconfig <https://pypi.org/project/scriptconfig/>`_ package can be installed via pip: | ||
| .. code-block:: bash | ||
| pip install scriptconfig | ||
| To install with argcomplete and rich-argparse support, either install these | ||
| packages separately or use: | ||
| .. code-block:: bash | ||
| pip install scriptconfig[optional] | ||
| Features | ||
| -------- | ||
| - Serializes to JSON | ||
| - Dict-like interface. By default a ``Config`` object operates independent of config files or the command line. | ||
| - Can create command line interfaces | ||
| - Can directly create an independent argparse object | ||
| - Can use special command line loading using ``self.load(cmdline=True)``. This extends the basic argparse interface with: | ||
| - Can specify options as either ``--option value`` or ``--option=value`` | ||
| - Default config options allow for "smartcasting" values like lists and paths | ||
| - Automatically add ``--config``, ``--dumps``, and ``--dump`` CLI options | ||
| when reading cmdline via ``load``. | ||
| - Fuzzy hyphen matching: e.g. ``--foo-bar=2`` and ``--foo_bar=2`` are treated the same for argparse options (note: modal commands do not have this option yet) | ||
| - Inheritance unions configs. | ||
| - Modal configs (see scriptconfig.modal) | ||
| - Integration with `argcomplete <https://pypi.org/project/argcomplete/>`_ for shell autocomplete. | ||
| - Integration with `rich_argparse <https://pypi.org/project/rich_argparse/>`_ for colorful CLI help pages. | ||
| Example Script | ||
| -------------- | ||
| Scriptconfig is used to define a flat configuration dictionary with values that | ||
| can be specified via Python keyword arguments, command line parameters, or a | ||
| YAML config file. Consider the following script that prints its config, opens a | ||
| file, computes its hash, and then prints it to stdout. | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| import hashlib | ||
| class FileHashConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| fpath = scfg.Value(None, position=1, help='a path to a file to hash') | ||
| hasher = scfg.Value('sha1', choices=['sha1', 'sha512'], help='a name of a hashlib hasher') | ||
| def main(**kwargs): | ||
| config = FileHashConfig.cli(data=kwargs) | ||
| print('config = {!r}'.format(config)) | ||
| fpath = config['fpath'] | ||
| hasher = getattr(hashlib, config['hasher'])() | ||
| with open(fpath, 'rb') as file: | ||
| hasher.update(file.read()) | ||
| hashstr = hasher.hexdigest() | ||
| print('The {hasher} hash of {fpath} is {hashstr}'.format( | ||
| hashstr=hashstr, **config)) | ||
| if __name__ == '__main__': | ||
| main() | ||
| If this script is in a module ``hash_demo.py`` (e.g. in the examples folder of | ||
| this repo), it can be invoked in these following ways. | ||
| Purely from the command line: | ||
| .. code-block:: bash | ||
| # Get help | ||
| python hash_demo.py --help | ||
| # Using key-val pairs | ||
| python hash_demo.py --fpath=$HOME/.bashrc --hasher=sha1 | ||
| # Using a positional arguments and other defaults | ||
| python hash_demo.py $HOME/.bashrc | ||
| From the command line using a YAML config: | ||
| .. code-block:: bash | ||
| # Write out a config file | ||
| echo '{"fpath": "hashconfig.json", "hasher": "sha512"}' > hashconfig.json | ||
| # Use the special `--config` cli arg provided by scriptconfig | ||
| python hash_demo.py --config=hashconfig.json | ||
| # You can also mix and match, this overrides the hasher in the config with sha1 | ||
| python hash_demo.py --config=hashconfig.json --hasher=sha1 | ||
| Lastly you can call it from good ol' Python. | ||
| .. code-block:: python | ||
| import hash_demo | ||
| hash_demo.main(fpath=hash_demo.__file__, hasher='sha512') | ||
| Modal CLIs | ||
| ---------- | ||
| A ModalCLI defines a way to group several smaller scriptconfig CLIs into a | ||
| single parent CLI that chooses between them "modally". E.g. if we define two | ||
| configs: do_foo and do_bar, we use ModalCLI to define a parent program that can | ||
| run one or the other. Let's make this more concrete. | ||
| Consider the code in ``examples/demo_modal.py``: | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| class DoFooCLI(scfg.DataConfig): | ||
| __command__ = 'do_foo' | ||
| option1 = scfg.Value(None, help='option1') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| self = cls.cli(cmdline=cmdline, data=kwargs) | ||
| print('Called Foo with: ' + str(self)) | ||
| class DoBarCLI(scfg.DataConfig): | ||
| __command__ = 'do_bar' | ||
| option1 = scfg.Value(None, help='option1') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| self = cls.cli(cmdline=cmdline, data=kwargs) | ||
| print('Called Bar with: ' + str(self)) | ||
| class MyModalCLI(scfg.ModalCLI): | ||
| __version__ = '1.2.3' | ||
| foo = DoFooCLI | ||
| bar = DoBarCLI | ||
| if __name__ == '__main__': | ||
| MyModalCLI().main() | ||
| Running: ``python examples/demo_modal.py --help``, results in: | ||
| .. code-block:: | ||
| usage: demo_modal.py [-h] [--version] {do_foo,do_bar} ... | ||
| options: | ||
| -h, --help show this help message and exit | ||
| --version show version number and exit (default: False) | ||
| commands: | ||
| {do_foo,do_bar} specify a command to run | ||
| do_foo argparse CLI generated by scriptconfig 0.7.12 | ||
| do_bar argparse CLI generated by scriptconfig 0.7.12 | ||
| And if you specify a command, ``python examples/demo_modal.py do_bar --help``, you get the help for that subcommand: | ||
| .. code-block:: | ||
| usage: DoBarCLI [-h] [--option1 OPTION1] | ||
| argparse CLI generated by scriptconfig 0.7.12 | ||
| options: | ||
| -h, --help show this help message and exit | ||
| --option1 OPTION1 option1 (default: None) | ||
| Autocomplete | ||
| ------------ | ||
| If you installed the optional `argcomplete <https://pypi.org/project/argcomplete/>`_ package you will find that pressing | ||
| tab will autocomplete registered arguments for scriptconfig CLIs. See project instructions for details, but on standard Linux | ||
| distributions you can enable global completion via: | ||
| .. code:: bash | ||
| pip install argcomplete | ||
| mkdir -p ~/.bash_completion.d | ||
| activate-global-python-argcomplete --dest ~/.bash_completion.d | ||
| source ~/.bash_completion.d/python-argcomplete | ||
| And then add these lines to your ``.bashrc``: | ||
| .. code:: bash | ||
| if [ -f "$HOME/.bash_completion.d/python-argcomplete" ]; then | ||
| source ~/.bash_completion.d/python-argcomplete | ||
| fi | ||
| Lastly, ensure your Python script has the following two comments at the top: | ||
| .. code:: python | ||
| #!/usr/bin/env python | ||
| # PYTHON_ARGCOMPLETE_OK | ||
| Project Design Goals | ||
| -------------------- | ||
| * Write Python programs that can be invoked either through the commandline | ||
| or via Python itself. | ||
| * Drop in replacement for any dictionary-based configuration system. | ||
| * Intuitive parsing (currently working on this), ideally improve on | ||
| argparse if possible. This means being able to easily specify simple | ||
| lists, numbers, strings, and paths. | ||
| To get started lets consider some example usage: | ||
| .. code-block:: python | ||
| >>> import scriptconfig as scfg | ||
| >>> # In its simplest incarnation, the config class specifies default values. | ||
| >>> # For each configuration parameter. | ||
| >>> class ExampleConfig(scfg.DataConfig): | ||
| >>> num = 1 | ||
| >>> mode = 'bar' | ||
| >>> ignore = ['baz', 'biz'] | ||
| >>> # Creating an instance, starts using the defaults | ||
| >>> config = ExampleConfig() | ||
| >>> assert config['num'] == 1 | ||
| >>> # Or pass in known data. (load as shown in the original example still works) | ||
| >>> kwargs = {'num': 2} | ||
| >>> config = ExampleConfig.cli(default=kwargs, cmdline=False) | ||
| >>> assert config['num'] == 2 | ||
| >>> # The `load` method can also be passed a JSON/YAML file/path. | ||
| >>> config_fpath = '/tmp/foo' | ||
| >>> open(config_fpath, 'w').write('{"mode": "foo"}') | ||
| >>> config.load(config_fpath, cmdline=False) | ||
| >>> assert config['num'] == 2 | ||
| >>> assert config['mode'] == "foo" | ||
| >>> # It is possbile to load only from CLI by setting cmdline=True | ||
| >>> # or by setting it to a custom sys.argv | ||
| >>> config = ExampleConfig.cli(argv=['--num=4']) | ||
| >>> assert config['num'] == 4 | ||
| >>> # Note that using `config.load(cmdline=True)` will just use the | ||
| >>> # contents of sys.argv | ||
| Notice in the above example the keys in your default dictionary are command | ||
| line arguments and values are their defaults. You can augment default values | ||
| by wrapping them in ``scriptconfig.Value`` objects to encapsulate information | ||
| like help documentation or type information. | ||
| .. code-block:: python | ||
| >>> import scriptconfig as scfg | ||
| >>> class ExampleConfig(scfg.Config): | ||
| >>> __default__ = { | ||
| >>> 'num': scfg.Value(1, help='a number'), | ||
| >>> 'mode': scfg.Value('bar', help='mode1 help'), | ||
| >>> 'mode2': scfg.Value('bar', type=str, help='mode2 help'), | ||
| >>> 'ignore': scfg.Value(['baz', 'biz'], help='list of ignore vals'), | ||
| >>> } | ||
| >>> config = ExampleConfig() | ||
| >>> # smartcast can handle lists as long as there are no spaces | ||
| >>> config.load(cmdline=['--ignore=spam,eggs']) | ||
| >>> assert config['ignore'] == ['spam', 'eggs'] | ||
| >>> # Note that the Value type can influence how data is parsed | ||
| >>> config.load(cmdline=['--mode=spam,eggs', '--mode2=spam,eggs']) | ||
| (Note the above example uses the older ``Config`` usage pattern where | ||
| attributes are members of a ``__default__`` dictionary. The ``DataConfig`` | ||
| class should be favored moving forward past version 0.6.2. However, | ||
| the ``__default__`` attribute is always available if you have an existing | ||
| dictionary you want to wrap with scriptconfig. | ||
| Gotchas | ||
| ------- | ||
| **CLI Values with commas:** | ||
| When using ``scriptconfig`` to generate a command line interface, it uses a | ||
| function called ``smartcast`` to try to determine input type when it is not | ||
| explicitly given. If you've ever used a program that tries to be "smart" you'll | ||
| know this can end up with some weird behavior. The case where that happens here | ||
| is when you pass a value that contains commas on the command line. If you don't | ||
| specify the default value as a ``scriptconfig.Value`` with a specified | ||
| ``type``, if will interpret your input as a list of values. In the future we | ||
| may change the behavior of ``smartcast``, or prevent it from being used as a | ||
| default. | ||
| **Boolean flags and positional arguments:** | ||
| ``scriptconfig`` always provides a key/value way to express arguments. However, it also | ||
| recognizes that sometimes you want to just type ``--flag`` and not ``--flag=1``. | ||
| We allow for this for ``Values`` with ``isflag=1``, but this causes a | ||
| corner-case ambituity with positional arguments. For the following example: | ||
| .. code:: python | ||
| class MyConfig(scfg.DataConfig): | ||
| arg1 = scfg.Value(None, position=1) | ||
| flag1 = scfg.Value(False, isflag=True, position=1) | ||
| For ``--flag 1`` We cannot determine if you wanted | ||
| ``{'arg1': 1, 'flag1': False}`` or ``{'arg1': None, 'flag1': True}``. | ||
| This is fixable by either using strict key/value arguments, expressing all | ||
| positional arguments before using flag arguments, or using the `` -- `` | ||
| construct and putting all positional arguments at the end. In the future we may | ||
| raise an AmbiguityError when specifying arguments like this, but for now we | ||
| leave the behavior undefined. | ||
| FAQ | ||
| --- | ||
| Question: How do I override the default values for a scriptconfig object using JSON file? | ||
| Answer: This depends if you want to pass the path to that JSON file via the command line or if you have that file in memory already. There are ways to do either. In the first case you can pass ``--config=<path-to-your-file>`` (assuming you have set the ``cmdline=True`` keyword arg when creating your config object e.g.: ``config = MyConfig(cmdline=True)``. In the second case when you create an instance of the scriptconfig object pass the ``default=<your dict>`` when creating the object: e.g. ``config = MyConfig(default=json.load(open(fpath, 'r')))``. But the special ``--config`` ``--dump`` and ``--dumps`` CLI arg is baked into script config to make this easier. | ||
| Related Software | ||
| ---------------- | ||
| I've never been completely happy with existing config / argument parser | ||
| software. I prefer to not use decorators, so click and to some extend hydra are | ||
| no-gos. Fire is nice when you want a really quick CLI, but is not so nice if | ||
| you ever go to deploy the program in the real world. | ||
| The builtin argparse in Python is pretty good, but I with it was easier to do | ||
| things like allowing arguments to be flags or key/value pairs. This library | ||
| uses argparse under the hood because of its stable and standard backend, but | ||
| that does mean we inherit some of its quirks. | ||
| The configargparse library - like this one - augments argparse with the ability | ||
| to read defaults from config files, but it has some major usage limitations due | ||
| to its implementation and there are better options (like jsonargparse). It also | ||
| does not support the use case of calling the CLI as a Python function very | ||
| well. | ||
| The jsonargparse library is newer than this one, and looks very compelling. I | ||
| feel like the definition of CLIs in this library are complementary and I'm | ||
| considering adding support in this library for jsonargparse because it solves | ||
| the problem of nested configurations and I would like to inherit from that. | ||
| Keep an eye out for this feature in future work. | ||
| Hydra - https://hydra.cc/docs/intro/ | ||
| OmegaConf - https://omegaconf.readthedocs.io/en/latest/index.html | ||
| Argparse - https://docs.python.org/3/library/argparse.html | ||
| JsonArgparse - https://jsonargparse.readthedocs.io/en/stable/index.html | ||
| Fire - https://pypi.org/project/fire/ | ||
| Click - https://pypi.org/project/click/ | ||
| ConfigArgparse - https://pypi.org/project/ConfigArgParse/ | ||
| TODO | ||
| ---- | ||
| - [ ] Nested Modal CLI's | ||
| - [ ] Fuzzy hyphens in ModelCLIs | ||
| - [X] Policy on nested heirachies (currently disallowed) - jsonargparse will be the solution here. | ||
| - [ ] How to best integrate with jsonargparse | ||
| - [ ] Policy on smartcast (currently enabled) | ||
| - [ ] Find a way to gracefully way to make smartcast do less. (e.g. no list parsing, but int is ok, we may think about accepting YAML) | ||
| - [X] Policy on positional arguments (currently experimental) - we have implemented them permissively with one undefined corner case. | ||
| - [X] Fixed length - nope | ||
| - [X] Variable length | ||
| - [X] Can argparse be modified to always allow for them to appear at the beginning or end? - Probably not. | ||
| - [x] Can we get argparse to allow a positional arg change the value of a prefixed arg and still have a sane help menu? | ||
| - [x] Policy on boolean flags - See the ``isflag`` argument of ``scriptconfig.Value`` | ||
| - [x] Improve over argparse's default autogenerated help docs (needs exploration on what is possible with argparse and where extensions are feasible) | ||
| .. |GitlabCIPipeline| image:: https://gitlab.kitware.com/utils/scriptconfig/badges/main/pipeline.svg | ||
| :target: https://gitlab.kitware.com/utils/scriptconfig/-/jobs | ||
| .. |GitlabCICoverage| image:: https://gitlab.kitware.com/utils/scriptconfig/badges/main/coverage.svg | ||
| :target: https://gitlab.kitware.com/utils/scriptconfig/commits/main | ||
| .. # See: https://ci.appveyor.com/project/jon.crall/scriptconfig/settings/badges | ||
| .. |Appveyor| image:: https://ci.appveyor.com/api/projects/status/br3p8lkuvol2vas4/branch/main?svg=true | ||
| :target: https://ci.appveyor.com/project/jon.crall/scriptconfig/branch/main | ||
| .. |Codecov| image:: https://codecov.io/github/Erotemic/scriptconfig/badge.svg?branch=main&service=github | ||
| :target: https://codecov.io/github/Erotemic/scriptconfig?branch=main | ||
| .. |Pypi| image:: https://img.shields.io/pypi/v/scriptconfig.svg | ||
| :target: https://pypi.python.org/pypi/scriptconfig | ||
| .. |PypiDownloads| image:: https://img.shields.io/pypi/dm/scriptconfig.svg | ||
| :target: https://pypistats.org/packages/scriptconfig | ||
| .. |ReadTheDocs| image:: https://readthedocs.org/projects/scriptconfig/badge/?version=latest | ||
| :target: http://scriptconfig.readthedocs.io/en/latest/ |
| [build-system] | ||
| requires = [ "setuptools>=41.0.1",] | ||
| build-backend = "setuptools.build_meta" | ||
| [tool.mypy] | ||
| ignore_missing_imports = true | ||
| [tool.xcookie] | ||
| tags = [ "gitlab", "kitware", "purepy",] | ||
| mod_name = "scriptconfig" | ||
| repo_name = "scriptconfig" | ||
| rel_mod_parent_dpath = "." | ||
| os = [ "osx", "linux", "win", "all",] | ||
| url = "https://gitlab.kitware.com/utils/scriptconfig" | ||
| min_python = 3.8 | ||
| max_python = 3.12 | ||
| version = "{mod_dpath}/__init__.py::__version__" | ||
| author = "Kitware Inc., Jon Crall" | ||
| author_email = "kitware@kitware.com, jon.crall@kitware.com" | ||
| description = "Easy dict-based script configuration with CLI support" | ||
| license = "Apache 2" | ||
| dev_status = "beta" | ||
| enable_gpg = true | ||
| typed = "partial" | ||
| pkg_name = "scriptconfig" | ||
| remote_host = "https://gitlab.kitware.com" | ||
| remote_group = "utils" | ||
| [tool.pytest.ini_options] | ||
| addopts = "-p no:doctest --xdoctest --xdoctest-style=google --ignore-glob=setup.py --ignore-glob=dev --ignore-glob=docs" | ||
| norecursedirs = ".git ignore build __pycache__ dev _skbuild docs" | ||
| filterwarnings = [ "default", "ignore:.*No cfgstr given in Cacher constructor or call.*:Warning", "ignore:.*Define the __nice__ method for.*:Warning", "ignore:.*private pytest class or function.*:Warning",] | ||
| [tool.coverage.run] | ||
| branch = true | ||
| [tool.coverage.report] | ||
| exclude_lines = [ "pragma: no cover", ".* # pragma: no cover", ".* # nocover", "def __repr__", "raise AssertionError", "raise NotImplementedError", "if 0:", "if trace is not None", "verbose = .*", "^ *raise", "^ *pass *$", "if _debug:", "if __name__ == .__main__.:", ".*if six.PY2:",] | ||
| omit = [ "scriptconfig/__main__.py", "*/setup.py",] |
+671
| ScriptConfig | ||
| ============ | ||
| .. # TODO Get CI services running on gitlab | ||
| .. #|CircleCI| |Travis| |Codecov| |ReadTheDocs| | ||
| |GitlabCIPipeline| |GitlabCICoverage| |Appveyor| |Pypi| |PypiDownloads| | ||
| +------------------+--------------------------------------------------+ | ||
| | Read the docs | https://scriptconfig.readthedocs.io | | ||
| +------------------+--------------------------------------------------+ | ||
| | Gitlab (main) | https://gitlab.kitware.com/utils/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| | Github (mirror) | https://github.com/Kitware/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| | Pypi | https://pypi.org/project/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| The goal of ``scriptconfig`` is to make it easy to be able to define a default | ||
| configuration by **simply defining a dictionary**, and then allow that | ||
| configuration to be modified by either: | ||
| 1. Updating it with another Python dictionary (e.g. ``kwargs``) | ||
| 2. Reading a YAML/JSON configuration file, or | ||
| 3. Inspecting values on ``sys.argv``, in which case we provide a powerful | ||
| command line interface (CLI). | ||
| The simplest way to create a script config is to create a class that inherits | ||
| from ``scriptconfig.DataConfig``. Then, use class variables to define the | ||
| expected keys and default values. | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| class ExampleConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| # Wrap defaults with `Value` to provide metadata | ||
| option1 = scfg.Value('default1', help='option1 help') | ||
| option2 = scfg.Value('default2', help='option2 help') | ||
| option3 = scfg.Value('default3', help='option3 help') | ||
| # Wrapping a default with `Value` is optional | ||
| option4 = 'default4' | ||
| An instance of a config object will work similarly to a dataclass, but it also | ||
| implements methods to duck-type a dictionary. Thus a scriptconfig object can be | ||
| dropped into code that uses an existing dictionary configuration or an existing | ||
| argparse namespace configuration. | ||
| .. code-block:: python | ||
| # Use as a dictionary with defaults | ||
| config = ExampleConfig(option1=123) | ||
| print(config) | ||
| # Can access items like a dictionary | ||
| print(config['option1']) | ||
| # OR Can access items like a namespace | ||
| print(config.option1) | ||
| Use the ``.cli`` classmethod to create an extended argparse command line | ||
| interface. Options to the ``cli`` method are similar to | ||
| ``argparse.ArgumentParser.parse_args``. | ||
| .. code-block:: python | ||
| # Use as a argparse CLI | ||
| config = ExampleConfig.cli(argv=['--option2=overruled']) | ||
| print(config) | ||
| After all that, if you still aren't loving scriptconfig, or you can't use it as | ||
| a dependency in production, you can ask it to convert itself to pure-argparse | ||
| via ``print(ExampleConfig().port_to_argparse())``, and it will print out: | ||
| .. code-block:: python | ||
| import argparse | ||
| parser = argparse.ArgumentParser( | ||
| prog='ExampleConfig', | ||
| description='The docstring will be the description in the CLI help', | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ) | ||
| parser.add_argument('--option1', help='option1 help', default='default1', dest='option1', required=False) | ||
| parser.add_argument('--option2', help='option2 help', default='default2', dest='option2', required=False) | ||
| parser.add_argument('--option3', help='option3 help', default='default3', dest='option3', required=False) | ||
| parser.add_argument('--option4', help='', default='default4', dest='option4', required=False) | ||
| Of course, the above also removes extra features of scriptconfig - so its not | ||
| exactly 1-to-1, but it's close. It's also a good tool for transferring any | ||
| existing intuition about ``argparse`` to ``scriptconfig``. | ||
| Similarly there is a method which can take an existing ArgumentParser as input, | ||
| and produce a scriptconfig definition. Given the above ``parser`` object, | ||
| ``print(scfg.Config.port_from_argparse(parser, style))`` will print out: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| option1 = scfg.Value('default1', help='option1 help') | ||
| option2 = scfg.Value('default2', help='option2 help') | ||
| option3 = scfg.Value('default3', help='option3 help') | ||
| option4 = scfg.Value('default4', help='') | ||
| Goal | ||
| ---- | ||
| The idea is we want to be able to start writing a simple program with a simple | ||
| configuration and allow it to evolve with minimal refactoring. In the early | ||
| stages we will insist that there be little-to-no boilerplate, but as a program | ||
| evolves we will add boilerplate to enhance the featurefull-ness of our program. | ||
| When we start coding we should aim for something like this: | ||
| .. code-block:: python | ||
| def my_function(): | ||
| config = { | ||
| 'simple_option1': 1, | ||
| 'simple_option2': 2, | ||
| } | ||
| # Early algorithmic and debugging logic | ||
| ... | ||
| As we evolve our code, we can plug scriptconfig in like this: | ||
| .. code-block:: python | ||
| def my_function(): | ||
| default_config = { | ||
| 'simple_option1': 1, | ||
| 'simple_option2': 2, | ||
| } | ||
| import scriptconfig | ||
| class MyConfig(scriptconfig.DataConfig): | ||
| __default__ = default_config | ||
| config = MyConfig() | ||
| # Transition algorithmic and debugging logic | ||
| ... | ||
| It's not pretty, but it gives us the ability to a fairly advanced CLI right | ||
| away (i.e by calling the ``.cli`` classmethod) without any major sacrifice to | ||
| code simplicity. However, as a project evolves we may eventually want to | ||
| refactor our CLI to gain full control over the metadata in our configuration an | ||
| CLI. Scriptconfig has a tool to help with this too. Given this janky definition, | ||
| we can port to a more ellegant style. We can run | ||
| ``print(config.port_to_dataconf())`` which prints: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| argparse CLI generated by scriptconfig 0.7.12 | ||
| """ | ||
| simple_option1 = scfg.Value(1, help=None) | ||
| simple_option2 = scfg.Value(2, help=None) | ||
| And then use that to make the refactor much easier. | ||
| The final state of a scriptconfig program might look something like this: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| This is my CLI description | ||
| """ | ||
| simple_option1 = scfg.Value(1, help=ub.paragraph( | ||
| ''' | ||
| A reasonably detailed but concise description of an argument. | ||
| About one paragraph is reasonable. | ||
| ''') | ||
| simple_option2 = scfg.Value(2, help='more help is better') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| config = cls.cli(cmdline=cmdline, data=kwargs) | ||
| my_function(config) | ||
| def my_function(config): | ||
| # Continued algorithmic and debugging logic | ||
| ... | ||
| Note that the fundamental impact on the ``...`` -- i.e. the intereting part of | ||
| the function -- remain completely unchanged! From it's point of view, you never | ||
| did anything to the original ``config`` dictionary, because scriptconfig | ||
| duck-typed it at every stage. | ||
| Installation | ||
| ------------ | ||
| The `scriptconfig <https://pypi.org/project/scriptconfig/>`_ package can be installed via pip: | ||
| .. code-block:: bash | ||
| pip install scriptconfig | ||
| To install with argcomplete and rich-argparse support, either install these | ||
| packages separately or use: | ||
| .. code-block:: bash | ||
| pip install scriptconfig[optional] | ||
| Features | ||
| -------- | ||
| - Serializes to JSON | ||
| - Dict-like interface. By default a ``Config`` object operates independent of config files or the command line. | ||
| - Can create command line interfaces | ||
| - Can directly create an independent argparse object | ||
| - Can use special command line loading using ``self.load(cmdline=True)``. This extends the basic argparse interface with: | ||
| - Can specify options as either ``--option value`` or ``--option=value`` | ||
| - Default config options allow for "smartcasting" values like lists and paths | ||
| - Automatically add ``--config``, ``--dumps``, and ``--dump`` CLI options | ||
| when reading cmdline via ``load``. | ||
| - Fuzzy hyphen matching: e.g. ``--foo-bar=2`` and ``--foo_bar=2`` are treated the same for argparse options (note: modal commands do not have this option yet) | ||
| - Inheritance unions configs. | ||
| - Modal configs (see scriptconfig.modal) | ||
| - Integration with `argcomplete <https://pypi.org/project/argcomplete/>`_ for shell autocomplete. | ||
| - Integration with `rich_argparse <https://pypi.org/project/rich_argparse/>`_ for colorful CLI help pages. | ||
| Example Script | ||
| -------------- | ||
| Scriptconfig is used to define a flat configuration dictionary with values that | ||
| can be specified via Python keyword arguments, command line parameters, or a | ||
| YAML config file. Consider the following script that prints its config, opens a | ||
| file, computes its hash, and then prints it to stdout. | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| import hashlib | ||
| class FileHashConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| fpath = scfg.Value(None, position=1, help='a path to a file to hash') | ||
| hasher = scfg.Value('sha1', choices=['sha1', 'sha512'], help='a name of a hashlib hasher') | ||
| def main(**kwargs): | ||
| config = FileHashConfig.cli(data=kwargs) | ||
| print('config = {!r}'.format(config)) | ||
| fpath = config['fpath'] | ||
| hasher = getattr(hashlib, config['hasher'])() | ||
| with open(fpath, 'rb') as file: | ||
| hasher.update(file.read()) | ||
| hashstr = hasher.hexdigest() | ||
| print('The {hasher} hash of {fpath} is {hashstr}'.format( | ||
| hashstr=hashstr, **config)) | ||
| if __name__ == '__main__': | ||
| main() | ||
| If this script is in a module ``hash_demo.py`` (e.g. in the examples folder of | ||
| this repo), it can be invoked in these following ways. | ||
| Purely from the command line: | ||
| .. code-block:: bash | ||
| # Get help | ||
| python hash_demo.py --help | ||
| # Using key-val pairs | ||
| python hash_demo.py --fpath=$HOME/.bashrc --hasher=sha1 | ||
| # Using a positional arguments and other defaults | ||
| python hash_demo.py $HOME/.bashrc | ||
| From the command line using a YAML config: | ||
| .. code-block:: bash | ||
| # Write out a config file | ||
| echo '{"fpath": "hashconfig.json", "hasher": "sha512"}' > hashconfig.json | ||
| # Use the special `--config` cli arg provided by scriptconfig | ||
| python hash_demo.py --config=hashconfig.json | ||
| # You can also mix and match, this overrides the hasher in the config with sha1 | ||
| python hash_demo.py --config=hashconfig.json --hasher=sha1 | ||
| Lastly you can call it from good ol' Python. | ||
| .. code-block:: python | ||
| import hash_demo | ||
| hash_demo.main(fpath=hash_demo.__file__, hasher='sha512') | ||
| Modal CLIs | ||
| ---------- | ||
| A ModalCLI defines a way to group several smaller scriptconfig CLIs into a | ||
| single parent CLI that chooses between them "modally". E.g. if we define two | ||
| configs: do_foo and do_bar, we use ModalCLI to define a parent program that can | ||
| run one or the other. Let's make this more concrete. | ||
| Consider the code in ``examples/demo_modal.py``: | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| class DoFooCLI(scfg.DataConfig): | ||
| __command__ = 'do_foo' | ||
| option1 = scfg.Value(None, help='option1') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| self = cls.cli(cmdline=cmdline, data=kwargs) | ||
| print('Called Foo with: ' + str(self)) | ||
| class DoBarCLI(scfg.DataConfig): | ||
| __command__ = 'do_bar' | ||
| option1 = scfg.Value(None, help='option1') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| self = cls.cli(cmdline=cmdline, data=kwargs) | ||
| print('Called Bar with: ' + str(self)) | ||
| class MyModalCLI(scfg.ModalCLI): | ||
| __version__ = '1.2.3' | ||
| foo = DoFooCLI | ||
| bar = DoBarCLI | ||
| if __name__ == '__main__': | ||
| MyModalCLI().main() | ||
| Running: ``python examples/demo_modal.py --help``, results in: | ||
| .. code-block:: | ||
| usage: demo_modal.py [-h] [--version] {do_foo,do_bar} ... | ||
| options: | ||
| -h, --help show this help message and exit | ||
| --version show version number and exit (default: False) | ||
| commands: | ||
| {do_foo,do_bar} specify a command to run | ||
| do_foo argparse CLI generated by scriptconfig 0.7.12 | ||
| do_bar argparse CLI generated by scriptconfig 0.7.12 | ||
| And if you specify a command, ``python examples/demo_modal.py do_bar --help``, you get the help for that subcommand: | ||
| .. code-block:: | ||
| usage: DoBarCLI [-h] [--option1 OPTION1] | ||
| argparse CLI generated by scriptconfig 0.7.12 | ||
| options: | ||
| -h, --help show this help message and exit | ||
| --option1 OPTION1 option1 (default: None) | ||
| Autocomplete | ||
| ------------ | ||
| If you installed the optional `argcomplete <https://pypi.org/project/argcomplete/>`_ package you will find that pressing | ||
| tab will autocomplete registered arguments for scriptconfig CLIs. See project instructions for details, but on standard Linux | ||
| distributions you can enable global completion via: | ||
| .. code:: bash | ||
| pip install argcomplete | ||
| mkdir -p ~/.bash_completion.d | ||
| activate-global-python-argcomplete --dest ~/.bash_completion.d | ||
| source ~/.bash_completion.d/python-argcomplete | ||
| And then add these lines to your ``.bashrc``: | ||
| .. code:: bash | ||
| if [ -f "$HOME/.bash_completion.d/python-argcomplete" ]; then | ||
| source ~/.bash_completion.d/python-argcomplete | ||
| fi | ||
| Lastly, ensure your Python script has the following two comments at the top: | ||
| .. code:: python | ||
| #!/usr/bin/env python | ||
| # PYTHON_ARGCOMPLETE_OK | ||
| Project Design Goals | ||
| -------------------- | ||
| * Write Python programs that can be invoked either through the commandline | ||
| or via Python itself. | ||
| * Drop in replacement for any dictionary-based configuration system. | ||
| * Intuitive parsing (currently working on this), ideally improve on | ||
| argparse if possible. This means being able to easily specify simple | ||
| lists, numbers, strings, and paths. | ||
| To get started lets consider some example usage: | ||
| .. code-block:: python | ||
| >>> import scriptconfig as scfg | ||
| >>> # In its simplest incarnation, the config class specifies default values. | ||
| >>> # For each configuration parameter. | ||
| >>> class ExampleConfig(scfg.DataConfig): | ||
| >>> num = 1 | ||
| >>> mode = 'bar' | ||
| >>> ignore = ['baz', 'biz'] | ||
| >>> # Creating an instance, starts using the defaults | ||
| >>> config = ExampleConfig() | ||
| >>> assert config['num'] == 1 | ||
| >>> # Or pass in known data. (load as shown in the original example still works) | ||
| >>> kwargs = {'num': 2} | ||
| >>> config = ExampleConfig.cli(default=kwargs, cmdline=False) | ||
| >>> assert config['num'] == 2 | ||
| >>> # The `load` method can also be passed a JSON/YAML file/path. | ||
| >>> config_fpath = '/tmp/foo' | ||
| >>> open(config_fpath, 'w').write('{"mode": "foo"}') | ||
| >>> config.load(config_fpath, cmdline=False) | ||
| >>> assert config['num'] == 2 | ||
| >>> assert config['mode'] == "foo" | ||
| >>> # It is possbile to load only from CLI by setting cmdline=True | ||
| >>> # or by setting it to a custom sys.argv | ||
| >>> config = ExampleConfig.cli(argv=['--num=4']) | ||
| >>> assert config['num'] == 4 | ||
| >>> # Note that using `config.load(cmdline=True)` will just use the | ||
| >>> # contents of sys.argv | ||
| Notice in the above example the keys in your default dictionary are command | ||
| line arguments and values are their defaults. You can augment default values | ||
| by wrapping them in ``scriptconfig.Value`` objects to encapsulate information | ||
| like help documentation or type information. | ||
| .. code-block:: python | ||
| >>> import scriptconfig as scfg | ||
| >>> class ExampleConfig(scfg.Config): | ||
| >>> __default__ = { | ||
| >>> 'num': scfg.Value(1, help='a number'), | ||
| >>> 'mode': scfg.Value('bar', help='mode1 help'), | ||
| >>> 'mode2': scfg.Value('bar', type=str, help='mode2 help'), | ||
| >>> 'ignore': scfg.Value(['baz', 'biz'], help='list of ignore vals'), | ||
| >>> } | ||
| >>> config = ExampleConfig() | ||
| >>> # smartcast can handle lists as long as there are no spaces | ||
| >>> config.load(cmdline=['--ignore=spam,eggs']) | ||
| >>> assert config['ignore'] == ['spam', 'eggs'] | ||
| >>> # Note that the Value type can influence how data is parsed | ||
| >>> config.load(cmdline=['--mode=spam,eggs', '--mode2=spam,eggs']) | ||
| (Note the above example uses the older ``Config`` usage pattern where | ||
| attributes are members of a ``__default__`` dictionary. The ``DataConfig`` | ||
| class should be favored moving forward past version 0.6.2. However, | ||
| the ``__default__`` attribute is always available if you have an existing | ||
| dictionary you want to wrap with scriptconfig. | ||
| Gotchas | ||
| ------- | ||
| **CLI Values with commas:** | ||
| When using ``scriptconfig`` to generate a command line interface, it uses a | ||
| function called ``smartcast`` to try to determine input type when it is not | ||
| explicitly given. If you've ever used a program that tries to be "smart" you'll | ||
| know this can end up with some weird behavior. The case where that happens here | ||
| is when you pass a value that contains commas on the command line. If you don't | ||
| specify the default value as a ``scriptconfig.Value`` with a specified | ||
| ``type``, if will interpret your input as a list of values. In the future we | ||
| may change the behavior of ``smartcast``, or prevent it from being used as a | ||
| default. | ||
| **Boolean flags and positional arguments:** | ||
| ``scriptconfig`` always provides a key/value way to express arguments. However, it also | ||
| recognizes that sometimes you want to just type ``--flag`` and not ``--flag=1``. | ||
| We allow for this for ``Values`` with ``isflag=1``, but this causes a | ||
| corner-case ambituity with positional arguments. For the following example: | ||
| .. code:: python | ||
| class MyConfig(scfg.DataConfig): | ||
| arg1 = scfg.Value(None, position=1) | ||
| flag1 = scfg.Value(False, isflag=True, position=1) | ||
| For ``--flag 1`` We cannot determine if you wanted | ||
| ``{'arg1': 1, 'flag1': False}`` or ``{'arg1': None, 'flag1': True}``. | ||
| This is fixable by either using strict key/value arguments, expressing all | ||
| positional arguments before using flag arguments, or using the `` -- `` | ||
| construct and putting all positional arguments at the end. In the future we may | ||
| raise an AmbiguityError when specifying arguments like this, but for now we | ||
| leave the behavior undefined. | ||
| FAQ | ||
| --- | ||
| Question: How do I override the default values for a scriptconfig object using JSON file? | ||
| Answer: This depends if you want to pass the path to that JSON file via the command line or if you have that file in memory already. There are ways to do either. In the first case you can pass ``--config=<path-to-your-file>`` (assuming you have set the ``cmdline=True`` keyword arg when creating your config object e.g.: ``config = MyConfig(cmdline=True)``. In the second case when you create an instance of the scriptconfig object pass the ``default=<your dict>`` when creating the object: e.g. ``config = MyConfig(default=json.load(open(fpath, 'r')))``. But the special ``--config`` ``--dump`` and ``--dumps`` CLI arg is baked into script config to make this easier. | ||
| Related Software | ||
| ---------------- | ||
| I've never been completely happy with existing config / argument parser | ||
| software. I prefer to not use decorators, so click and to some extend hydra are | ||
| no-gos. Fire is nice when you want a really quick CLI, but is not so nice if | ||
| you ever go to deploy the program in the real world. | ||
| The builtin argparse in Python is pretty good, but I with it was easier to do | ||
| things like allowing arguments to be flags or key/value pairs. This library | ||
| uses argparse under the hood because of its stable and standard backend, but | ||
| that does mean we inherit some of its quirks. | ||
| The configargparse library - like this one - augments argparse with the ability | ||
| to read defaults from config files, but it has some major usage limitations due | ||
| to its implementation and there are better options (like jsonargparse). It also | ||
| does not support the use case of calling the CLI as a Python function very | ||
| well. | ||
| The jsonargparse library is newer than this one, and looks very compelling. I | ||
| feel like the definition of CLIs in this library are complementary and I'm | ||
| considering adding support in this library for jsonargparse because it solves | ||
| the problem of nested configurations and I would like to inherit from that. | ||
| Keep an eye out for this feature in future work. | ||
| Hydra - https://hydra.cc/docs/intro/ | ||
| OmegaConf - https://omegaconf.readthedocs.io/en/latest/index.html | ||
| Argparse - https://docs.python.org/3/library/argparse.html | ||
| JsonArgparse - https://jsonargparse.readthedocs.io/en/stable/index.html | ||
| Fire - https://pypi.org/project/fire/ | ||
| Click - https://pypi.org/project/click/ | ||
| ConfigArgparse - https://pypi.org/project/ConfigArgParse/ | ||
| TODO | ||
| ---- | ||
| - [ ] Nested Modal CLI's | ||
| - [ ] Fuzzy hyphens in ModelCLIs | ||
| - [X] Policy on nested heirachies (currently disallowed) - jsonargparse will be the solution here. | ||
| - [ ] How to best integrate with jsonargparse | ||
| - [ ] Policy on smartcast (currently enabled) | ||
| - [ ] Find a way to gracefully way to make smartcast do less. (e.g. no list parsing, but int is ok, we may think about accepting YAML) | ||
| - [X] Policy on positional arguments (currently experimental) - we have implemented them permissively with one undefined corner case. | ||
| - [X] Fixed length - nope | ||
| - [X] Variable length | ||
| - [X] Can argparse be modified to always allow for them to appear at the beginning or end? - Probably not. | ||
| - [x] Can we get argparse to allow a positional arg change the value of a prefixed arg and still have a sane help menu? | ||
| - [x] Policy on boolean flags - See the ``isflag`` argument of ``scriptconfig.Value`` | ||
| - [x] Improve over argparse's default autogenerated help docs (needs exploration on what is possible with argparse and where extensions are feasible) | ||
| .. |GitlabCIPipeline| image:: https://gitlab.kitware.com/utils/scriptconfig/badges/main/pipeline.svg | ||
| :target: https://gitlab.kitware.com/utils/scriptconfig/-/jobs | ||
| .. |GitlabCICoverage| image:: https://gitlab.kitware.com/utils/scriptconfig/badges/main/coverage.svg | ||
| :target: https://gitlab.kitware.com/utils/scriptconfig/commits/main | ||
| .. # See: https://ci.appveyor.com/project/jon.crall/scriptconfig/settings/badges | ||
| .. |Appveyor| image:: https://ci.appveyor.com/api/projects/status/br3p8lkuvol2vas4/branch/main?svg=true | ||
| :target: https://ci.appveyor.com/project/jon.crall/scriptconfig/branch/main | ||
| .. |Codecov| image:: https://codecov.io/github/Erotemic/scriptconfig/badge.svg?branch=main&service=github | ||
| :target: https://codecov.io/github/Erotemic/scriptconfig?branch=main | ||
| .. |Pypi| image:: https://img.shields.io/pypi/v/scriptconfig.svg | ||
| :target: https://pypi.python.org/pypi/scriptconfig | ||
| .. |PypiDownloads| image:: https://img.shields.io/pypi/dm/scriptconfig.svg | ||
| :target: https://pypistats.org/packages/scriptconfig | ||
| .. |ReadTheDocs| image:: https://readthedocs.org/projects/scriptconfig/badge/?version=latest | ||
| :target: http://scriptconfig.readthedocs.io/en/latest/ |
| sphinx >= 5.0.1 | ||
| sphinx-autobuild >= 2021.3.14 | ||
| sphinx_rtd_theme >= 1.0.0 | ||
| sphinxcontrib-napoleon >= 0.7 | ||
| sphinx-autoapi >= 1.8.4 | ||
| Pygments >= 2.9.0 | ||
| myst_parser >= 0.18.0 | ||
| sphinx-reredirects >= 0.0.1 |
| flake8>=5.0.0 |
| # xdev availpkg numpy | ||
| # 1.19.2 is the tensorflow minimum | ||
| numpy>=1.26.0 ; python_version < '4.0' and python_version >= '3.12' # Python 3.12+ | ||
| numpy>=1.23.2 ; python_version < '3.12' and python_version >= '3.11' # Python 3.11 | ||
| numpy>=1.21.6 ; python_version < '3.11' and python_version >= '3.10' # Python 3.10 | ||
| numpy>=1.19.3 ; python_version < '3.10' and python_version >= '3.9' # Python 3.9 | ||
| numpy>=1.19.2 ; python_version < '3.9' and python_version >= '3.8' # Python 3.8 | ||
| numpy>=1.14.5 ; python_version < '3.8' and python_version >= '3.7' # Python 3.7 | ||
| numpy>=1.12.0 ; python_version < '3.7' and python_version >= '3.6' # Python 3.6 | ||
| numpy>=1.11.1 ; python_version < '3.6' and python_version >= '3.5' # Python 3.5 | ||
| numpy>=1.11.1 ; python_version < '3.5' and python_version >= '3.4' # Python 3.4 | ||
| numpy>=1.11.1 ; python_version < '3.4' and python_version >= '2.7' # Python 2.7 | ||
| omegaconf>=2.2.2 ; python_version >= '3.6' # Python 3.6+ | ||
| rich_argparse>=1.1.0; python_version >= '3.7' | ||
| argcomplete>=3.0.5 |
| ubelt>=1.3.6 | ||
| PyYAML>=6.0.1 ; python_version < '4.0' and python_version >= '3.12' # Python 3.12+ | ||
| PyYAML>=6.0 ; python_version < '3.12' and python_version >= '3.11' # Python 3.11+ | ||
| PyYAML>=6.0 ; python_version < '3.11' and python_version >= '3.10' # Python 3.10 | ||
| PyYAML>=5.4.1 ; python_version < '3.10' and python_version >= '3.9' # Python 3.9 | ||
| PyYAML>=5.4.1 ; python_version < '3.9' and python_version >= '3.8' # Python 3.8 | ||
| PyYAML>=5.4.1 ; python_version < '3.8' and python_version >= '3.7' # Python 3.7 | ||
| PyYAML>=5.4.1 ; python_version < '3.7' and python_version >= '3.6' # Python 3.6 |
| pytest>=6.2.5 ; python_version >= '3.10.0' # Python 3.10+ | ||
| pytest>=4.6.0 ; python_version < '3.10.0' and python_version >= '3.7.0' # Python 3.7-3.9 | ||
| pytest>=4.6.0 ; python_version < '3.7.0' and python_version >= '3.6.0' # Python 3.6 | ||
| pytest>=4.6.0, <= 6.1.2 ; python_version < '3.6.0' and python_version >= '3.5.0' # Python 3.5 | ||
| pytest>=4.6.0, <= 4.6.11 ; python_version < '3.5.0' and python_version >= '3.4.0' # Python 3.4 | ||
| pytest>=4.6.0, <= 4.6.11 ; python_version < '2.8.0' and python_version >= '2.7.0' # Python 2.7 | ||
| coverage>=6.1.1 ; python_version >= '3.10' # Python 3.10+ | ||
| coverage>=5.3.1 ; python_version < '3.10' and python_version >= '3.9' # Python 3.9 | ||
| coverage>=6.1.1 ; python_version < '3.9' and python_version >= '3.8' # Python 3.8 | ||
| coverage>=6.1.1 ; python_version < '3.8' and python_version >= '3.7' # Python 3.7 | ||
| coverage>=6.1.1 ; python_version < '3.7' and python_version >= '3.6' # Python 3.6 | ||
| coverage>=5.3.1 ; python_version < '3.6' and python_version >= '3.5' # Python 3.5 | ||
| coverage>=4.3.4 ; python_version < '3.5' and python_version >= '3.4' # Python 3.4 | ||
| coverage>=5.3.1 ; python_version < '3.4' and python_version >= '2.7' # Python 2.7 | ||
| coverage>=4.5 ; python_version < '2.7' and python_version >= '2.6' # Python 2.6 | ||
| pytest-cov>=3.0.0 ; python_version >= '3.6.0' # Python 3.6+ | ||
| pytest-cov>=2.9.0 ; python_version < '3.6.0' and python_version >= '3.5.0' # Python 3.5 | ||
| pytest-cov>=2.8.1 ; python_version < '3.5.0' and python_version >= '3.4.0' # Python 3.4 | ||
| pytest-cov>=2.8.1 ; python_version < '2.8.0' and python_version >= '2.7.0' # Python 2.7 | ||
| xdoctest >= 1.1.5 |
| Metadata-Version: 2.1 | ||
| Name: scriptconfig | ||
| Version: 0.8.0 | ||
| Summary: Easy dict-based script configuration with CLI support | ||
| Home-page: https://gitlab.kitware.com/utils/scriptconfig/ | ||
| Author: Kitware Inc., Jon Crall | ||
| Author-email: kitware@kitware.com, jon.crall@kitware.com | ||
| License: Apache 2 | ||
| Classifier: Development Status :: 4 - Beta | ||
| Classifier: Intended Audience :: Developers | ||
| Classifier: Topic :: Software Development :: Libraries :: Python Modules | ||
| Classifier: Topic :: Utilities | ||
| Classifier: License :: OSI Approved :: Apache Software License | ||
| Classifier: Programming Language :: Python :: 3.6 | ||
| Classifier: Programming Language :: Python :: 3.7 | ||
| Classifier: Programming Language :: Python :: 3.8 | ||
| Classifier: Programming Language :: Python :: 3.9 | ||
| Classifier: Programming Language :: Python :: 3.10 | ||
| Classifier: Programming Language :: Python :: 3.11 | ||
| Requires-Python: >=3.6 | ||
| Description-Content-Type: text/x-rst | ||
| License-File: LICENSE | ||
| Requires-Dist: ubelt>=1.3.6 | ||
| Requires-Dist: PyYAML>=6.0.1; python_version < "4.0" and python_version >= "3.12" | ||
| Requires-Dist: PyYAML>=6.0; python_version < "3.12" and python_version >= "3.11" | ||
| Requires-Dist: PyYAML>=6.0; python_version < "3.11" and python_version >= "3.10" | ||
| Requires-Dist: PyYAML>=5.4.1; python_version < "3.10" and python_version >= "3.9" | ||
| Requires-Dist: PyYAML>=5.4.1; python_version < "3.9" and python_version >= "3.8" | ||
| Requires-Dist: PyYAML>=5.4.1; python_version < "3.8" and python_version >= "3.7" | ||
| Requires-Dist: PyYAML>=5.4.1; python_version < "3.7" and python_version >= "3.6" | ||
| Provides-Extra: all | ||
| Requires-Dist: ubelt>=1.3.6; extra == "all" | ||
| Requires-Dist: PyYAML>=6.0.1; (python_version < "4.0" and python_version >= "3.12") and extra == "all" | ||
| Requires-Dist: PyYAML>=6.0; (python_version < "3.12" and python_version >= "3.11") and extra == "all" | ||
| Requires-Dist: PyYAML>=6.0; (python_version < "3.11" and python_version >= "3.10") and extra == "all" | ||
| Requires-Dist: PyYAML>=5.4.1; (python_version < "3.10" and python_version >= "3.9") and extra == "all" | ||
| Requires-Dist: PyYAML>=5.4.1; (python_version < "3.9" and python_version >= "3.8") and extra == "all" | ||
| Requires-Dist: PyYAML>=5.4.1; (python_version < "3.8" and python_version >= "3.7") and extra == "all" | ||
| Requires-Dist: PyYAML>=5.4.1; (python_version < "3.7" and python_version >= "3.6") and extra == "all" | ||
| Requires-Dist: pytest>=6.2.5; python_version >= "3.10.0" and extra == "all" | ||
| Requires-Dist: pytest>=4.6.0; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == "all" | ||
| Requires-Dist: pytest>=4.6.0; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == "all" | ||
| Requires-Dist: pytest<=6.1.2,>=4.6.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "all" | ||
| Requires-Dist: pytest<=4.6.11,>=4.6.0; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "all" | ||
| Requires-Dist: pytest<=4.6.11,>=4.6.0; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "all" | ||
| Requires-Dist: coverage>=6.1.1; python_version >= "3.10" and extra == "all" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.10" and python_version >= "3.9") and extra == "all" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.9" and python_version >= "3.8") and extra == "all" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.8" and python_version >= "3.7") and extra == "all" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.7" and python_version >= "3.6") and extra == "all" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.6" and python_version >= "3.5") and extra == "all" | ||
| Requires-Dist: coverage>=4.3.4; (python_version < "3.5" and python_version >= "3.4") and extra == "all" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.4" and python_version >= "2.7") and extra == "all" | ||
| Requires-Dist: coverage>=4.5; (python_version < "2.7" and python_version >= "2.6") and extra == "all" | ||
| Requires-Dist: pytest-cov>=3.0.0; python_version >= "3.6.0" and extra == "all" | ||
| Requires-Dist: pytest-cov>=2.9.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "all" | ||
| Requires-Dist: pytest-cov>=2.8.1; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "all" | ||
| Requires-Dist: pytest-cov>=2.8.1; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "all" | ||
| Requires-Dist: xdoctest>=1.1.5; extra == "all" | ||
| Requires-Dist: numpy>=1.26.0; (python_version < "4.0" and python_version >= "3.12") and extra == "all" | ||
| Requires-Dist: numpy>=1.23.2; (python_version < "3.12" and python_version >= "3.11") and extra == "all" | ||
| Requires-Dist: numpy>=1.21.6; (python_version < "3.11" and python_version >= "3.10") and extra == "all" | ||
| Requires-Dist: numpy>=1.19.3; (python_version < "3.10" and python_version >= "3.9") and extra == "all" | ||
| Requires-Dist: numpy>=1.19.2; (python_version < "3.9" and python_version >= "3.8") and extra == "all" | ||
| Requires-Dist: numpy>=1.14.5; (python_version < "3.8" and python_version >= "3.7") and extra == "all" | ||
| Requires-Dist: numpy>=1.12.0; (python_version < "3.7" and python_version >= "3.6") and extra == "all" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.6" and python_version >= "3.5") and extra == "all" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.5" and python_version >= "3.4") and extra == "all" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.4" and python_version >= "2.7") and extra == "all" | ||
| Requires-Dist: omegaconf>=2.2.2; python_version >= "3.6" and extra == "all" | ||
| Requires-Dist: rich_argparse>=1.1.0; python_version >= "3.7" and extra == "all" | ||
| Requires-Dist: argcomplete>=3.0.5; extra == "all" | ||
| Provides-Extra: tests | ||
| Requires-Dist: pytest>=6.2.5; python_version >= "3.10.0" and extra == "tests" | ||
| Requires-Dist: pytest>=4.6.0; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == "tests" | ||
| Requires-Dist: pytest>=4.6.0; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == "tests" | ||
| Requires-Dist: pytest<=6.1.2,>=4.6.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "tests" | ||
| Requires-Dist: pytest<=4.6.11,>=4.6.0; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "tests" | ||
| Requires-Dist: pytest<=4.6.11,>=4.6.0; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "tests" | ||
| Requires-Dist: coverage>=6.1.1; python_version >= "3.10" and extra == "tests" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.10" and python_version >= "3.9") and extra == "tests" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.9" and python_version >= "3.8") and extra == "tests" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.8" and python_version >= "3.7") and extra == "tests" | ||
| Requires-Dist: coverage>=6.1.1; (python_version < "3.7" and python_version >= "3.6") and extra == "tests" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.6" and python_version >= "3.5") and extra == "tests" | ||
| Requires-Dist: coverage>=4.3.4; (python_version < "3.5" and python_version >= "3.4") and extra == "tests" | ||
| Requires-Dist: coverage>=5.3.1; (python_version < "3.4" and python_version >= "2.7") and extra == "tests" | ||
| Requires-Dist: coverage>=4.5; (python_version < "2.7" and python_version >= "2.6") and extra == "tests" | ||
| Requires-Dist: pytest-cov>=3.0.0; python_version >= "3.6.0" and extra == "tests" | ||
| Requires-Dist: pytest-cov>=2.9.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "tests" | ||
| Requires-Dist: pytest-cov>=2.8.1; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "tests" | ||
| Requires-Dist: pytest-cov>=2.8.1; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "tests" | ||
| Requires-Dist: xdoctest>=1.1.5; extra == "tests" | ||
| Provides-Extra: optional | ||
| Requires-Dist: numpy>=1.26.0; (python_version < "4.0" and python_version >= "3.12") and extra == "optional" | ||
| Requires-Dist: numpy>=1.23.2; (python_version < "3.12" and python_version >= "3.11") and extra == "optional" | ||
| Requires-Dist: numpy>=1.21.6; (python_version < "3.11" and python_version >= "3.10") and extra == "optional" | ||
| Requires-Dist: numpy>=1.19.3; (python_version < "3.10" and python_version >= "3.9") and extra == "optional" | ||
| Requires-Dist: numpy>=1.19.2; (python_version < "3.9" and python_version >= "3.8") and extra == "optional" | ||
| Requires-Dist: numpy>=1.14.5; (python_version < "3.8" and python_version >= "3.7") and extra == "optional" | ||
| Requires-Dist: numpy>=1.12.0; (python_version < "3.7" and python_version >= "3.6") and extra == "optional" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.6" and python_version >= "3.5") and extra == "optional" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.5" and python_version >= "3.4") and extra == "optional" | ||
| Requires-Dist: numpy>=1.11.1; (python_version < "3.4" and python_version >= "2.7") and extra == "optional" | ||
| Requires-Dist: omegaconf>=2.2.2; python_version >= "3.6" and extra == "optional" | ||
| Requires-Dist: rich_argparse>=1.1.0; python_version >= "3.7" and extra == "optional" | ||
| Requires-Dist: argcomplete>=3.0.5; extra == "optional" | ||
| Provides-Extra: all-strict | ||
| Requires-Dist: ubelt==1.3.6; extra == "all-strict" | ||
| Requires-Dist: PyYAML==6.0.1; (python_version < "4.0" and python_version >= "3.12") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==6.0; (python_version < "3.12" and python_version >= "3.11") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==6.0; (python_version < "3.11" and python_version >= "3.10") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.10" and python_version >= "3.9") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.9" and python_version >= "3.8") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.8" and python_version >= "3.7") and extra == "all-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.7" and python_version >= "3.6") and extra == "all-strict" | ||
| Requires-Dist: pytest==6.2.5; python_version >= "3.10.0" and extra == "all-strict" | ||
| Requires-Dist: pytest==4.6.0; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == "all-strict" | ||
| Requires-Dist: pytest==4.6.0; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == "all-strict" | ||
| Requires-Dist: pytest<=6.1.2,==4.6.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "all-strict" | ||
| Requires-Dist: pytest<=4.6.11,==4.6.0; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "all-strict" | ||
| Requires-Dist: pytest<=4.6.11,==4.6.0; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "all-strict" | ||
| Requires-Dist: coverage==6.1.1; python_version >= "3.10" and extra == "all-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.10" and python_version >= "3.9") and extra == "all-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.9" and python_version >= "3.8") and extra == "all-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.8" and python_version >= "3.7") and extra == "all-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.7" and python_version >= "3.6") and extra == "all-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.6" and python_version >= "3.5") and extra == "all-strict" | ||
| Requires-Dist: coverage==4.3.4; (python_version < "3.5" and python_version >= "3.4") and extra == "all-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.4" and python_version >= "2.7") and extra == "all-strict" | ||
| Requires-Dist: coverage==4.5; (python_version < "2.7" and python_version >= "2.6") and extra == "all-strict" | ||
| Requires-Dist: pytest-cov==3.0.0; python_version >= "3.6.0" and extra == "all-strict" | ||
| Requires-Dist: pytest-cov==2.9.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "all-strict" | ||
| Requires-Dist: pytest-cov==2.8.1; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "all-strict" | ||
| Requires-Dist: pytest-cov==2.8.1; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "all-strict" | ||
| Requires-Dist: xdoctest==1.1.5; extra == "all-strict" | ||
| Requires-Dist: numpy==1.26.0; (python_version < "4.0" and python_version >= "3.12") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.23.2; (python_version < "3.12" and python_version >= "3.11") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.21.6; (python_version < "3.11" and python_version >= "3.10") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.19.3; (python_version < "3.10" and python_version >= "3.9") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.19.2; (python_version < "3.9" and python_version >= "3.8") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.14.5; (python_version < "3.8" and python_version >= "3.7") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.12.0; (python_version < "3.7" and python_version >= "3.6") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.6" and python_version >= "3.5") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.5" and python_version >= "3.4") and extra == "all-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.4" and python_version >= "2.7") and extra == "all-strict" | ||
| Requires-Dist: omegaconf==2.2.2; python_version >= "3.6" and extra == "all-strict" | ||
| Requires-Dist: rich_argparse==1.1.0; python_version >= "3.7" and extra == "all-strict" | ||
| Requires-Dist: argcomplete==3.0.5; extra == "all-strict" | ||
| Provides-Extra: runtime-strict | ||
| Requires-Dist: ubelt==1.3.6; extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==6.0.1; (python_version < "4.0" and python_version >= "3.12") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==6.0; (python_version < "3.12" and python_version >= "3.11") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==6.0; (python_version < "3.11" and python_version >= "3.10") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.10" and python_version >= "3.9") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.9" and python_version >= "3.8") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.8" and python_version >= "3.7") and extra == "runtime-strict" | ||
| Requires-Dist: PyYAML==5.4.1; (python_version < "3.7" and python_version >= "3.6") and extra == "runtime-strict" | ||
| Provides-Extra: tests-strict | ||
| Requires-Dist: pytest==6.2.5; python_version >= "3.10.0" and extra == "tests-strict" | ||
| Requires-Dist: pytest==4.6.0; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest==4.6.0; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest<=6.1.2,==4.6.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest<=4.6.11,==4.6.0; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest<=4.6.11,==4.6.0; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "tests-strict" | ||
| Requires-Dist: coverage==6.1.1; python_version >= "3.10" and extra == "tests-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.10" and python_version >= "3.9") and extra == "tests-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.9" and python_version >= "3.8") and extra == "tests-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.8" and python_version >= "3.7") and extra == "tests-strict" | ||
| Requires-Dist: coverage==6.1.1; (python_version < "3.7" and python_version >= "3.6") and extra == "tests-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.6" and python_version >= "3.5") and extra == "tests-strict" | ||
| Requires-Dist: coverage==4.3.4; (python_version < "3.5" and python_version >= "3.4") and extra == "tests-strict" | ||
| Requires-Dist: coverage==5.3.1; (python_version < "3.4" and python_version >= "2.7") and extra == "tests-strict" | ||
| Requires-Dist: coverage==4.5; (python_version < "2.7" and python_version >= "2.6") and extra == "tests-strict" | ||
| Requires-Dist: pytest-cov==3.0.0; python_version >= "3.6.0" and extra == "tests-strict" | ||
| Requires-Dist: pytest-cov==2.9.0; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest-cov==2.8.1; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == "tests-strict" | ||
| Requires-Dist: pytest-cov==2.8.1; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == "tests-strict" | ||
| Requires-Dist: xdoctest==1.1.5; extra == "tests-strict" | ||
| Provides-Extra: optional-strict | ||
| Requires-Dist: numpy==1.26.0; (python_version < "4.0" and python_version >= "3.12") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.23.2; (python_version < "3.12" and python_version >= "3.11") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.21.6; (python_version < "3.11" and python_version >= "3.10") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.19.3; (python_version < "3.10" and python_version >= "3.9") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.19.2; (python_version < "3.9" and python_version >= "3.8") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.14.5; (python_version < "3.8" and python_version >= "3.7") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.12.0; (python_version < "3.7" and python_version >= "3.6") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.6" and python_version >= "3.5") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.5" and python_version >= "3.4") and extra == "optional-strict" | ||
| Requires-Dist: numpy==1.11.1; (python_version < "3.4" and python_version >= "2.7") and extra == "optional-strict" | ||
| Requires-Dist: omegaconf==2.2.2; python_version >= "3.6" and extra == "optional-strict" | ||
| Requires-Dist: rich_argparse==1.1.0; python_version >= "3.7" and extra == "optional-strict" | ||
| Requires-Dist: argcomplete==3.0.5; extra == "optional-strict" | ||
| ScriptConfig | ||
| ============ | ||
| .. # TODO Get CI services running on gitlab | ||
| .. #|CircleCI| |Travis| |Codecov| |ReadTheDocs| | ||
| |GitlabCIPipeline| |GitlabCICoverage| |Appveyor| |Pypi| |PypiDownloads| | ||
| +------------------+--------------------------------------------------+ | ||
| | Read the docs | https://scriptconfig.readthedocs.io | | ||
| +------------------+--------------------------------------------------+ | ||
| | Gitlab (main) | https://gitlab.kitware.com/utils/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| | Github (mirror) | https://github.com/Kitware/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| | Pypi | https://pypi.org/project/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| The goal of ``scriptconfig`` is to make it easy to be able to define a default | ||
| configuration by **simply defining a dictionary**, and then allow that | ||
| configuration to be modified by either: | ||
| 1. Updating it with another Python dictionary (e.g. ``kwargs``) | ||
| 2. Reading a YAML/JSON configuration file, or | ||
| 3. Inspecting values on ``sys.argv``, in which case we provide a powerful | ||
| command line interface (CLI). | ||
| The simplest way to create a script config is to create a class that inherits | ||
| from ``scriptconfig.DataConfig``. Then, use class variables to define the | ||
| expected keys and default values. | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| class ExampleConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| # Wrap defaults with `Value` to provide metadata | ||
| option1 = scfg.Value('default1', help='option1 help') | ||
| option2 = scfg.Value('default2', help='option2 help') | ||
| option3 = scfg.Value('default3', help='option3 help') | ||
| # Wrapping a default with `Value` is optional | ||
| option4 = 'default4' | ||
| An instance of a config object will work similarly to a dataclass, but it also | ||
| implements methods to duck-type a dictionary. Thus a scriptconfig object can be | ||
| dropped into code that uses an existing dictionary configuration or an existing | ||
| argparse namespace configuration. | ||
| .. code-block:: python | ||
| # Use as a dictionary with defaults | ||
| config = ExampleConfig(option1=123) | ||
| print(config) | ||
| # Can access items like a dictionary | ||
| print(config['option1']) | ||
| # OR Can access items like a namespace | ||
| print(config.option1) | ||
| Use the ``.cli`` classmethod to create an extended argparse command line | ||
| interface. Options to the ``cli`` method are similar to | ||
| ``argparse.ArgumentParser.parse_args``. | ||
| .. code-block:: python | ||
| # Use as a argparse CLI | ||
| config = ExampleConfig.cli(argv=['--option2=overruled']) | ||
| print(config) | ||
| After all that, if you still aren't loving scriptconfig, or you can't use it as | ||
| a dependency in production, you can ask it to convert itself to pure-argparse | ||
| via ``print(ExampleConfig().port_to_argparse())``, and it will print out: | ||
| .. code-block:: python | ||
| import argparse | ||
| parser = argparse.ArgumentParser( | ||
| prog='ExampleConfig', | ||
| description='The docstring will be the description in the CLI help', | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ) | ||
| parser.add_argument('--option1', help='option1 help', default='default1', dest='option1', required=False) | ||
| parser.add_argument('--option2', help='option2 help', default='default2', dest='option2', required=False) | ||
| parser.add_argument('--option3', help='option3 help', default='default3', dest='option3', required=False) | ||
| parser.add_argument('--option4', help='', default='default4', dest='option4', required=False) | ||
| Of course, the above also removes extra features of scriptconfig - so its not | ||
| exactly 1-to-1, but it's close. It's also a good tool for transferring any | ||
| existing intuition about ``argparse`` to ``scriptconfig``. | ||
| Similarly there is a method which can take an existing ArgumentParser as input, | ||
| and produce a scriptconfig definition. Given the above ``parser`` object, | ||
| ``print(scfg.Config.port_from_argparse(parser, style))`` will print out: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| option1 = scfg.Value('default1', help='option1 help') | ||
| option2 = scfg.Value('default2', help='option2 help') | ||
| option3 = scfg.Value('default3', help='option3 help') | ||
| option4 = scfg.Value('default4', help='') | ||
| Goal | ||
| ---- | ||
| The idea is we want to be able to start writing a simple program with a simple | ||
| configuration and allow it to evolve with minimal refactoring. In the early | ||
| stages we will insist that there be little-to-no boilerplate, but as a program | ||
| evolves we will add boilerplate to enhance the featurefull-ness of our program. | ||
| When we start coding we should aim for something like this: | ||
| .. code-block:: python | ||
| def my_function(): | ||
| config = { | ||
| 'simple_option1': 1, | ||
| 'simple_option2': 2, | ||
| } | ||
| # Early algorithmic and debugging logic | ||
| ... | ||
| As we evolve our code, we can plug scriptconfig in like this: | ||
| .. code-block:: python | ||
| def my_function(): | ||
| default_config = { | ||
| 'simple_option1': 1, | ||
| 'simple_option2': 2, | ||
| } | ||
| import scriptconfig | ||
| class MyConfig(scriptconfig.DataConfig): | ||
| __default__ = default_config | ||
| config = MyConfig() | ||
| # Transition algorithmic and debugging logic | ||
| ... | ||
| It's not pretty, but it gives us the ability to a fairly advanced CLI right | ||
| away (i.e by calling the ``.cli`` classmethod) without any major sacrifice to | ||
| code simplicity. However, as a project evolves we may eventually want to | ||
| refactor our CLI to gain full control over the metadata in our configuration an | ||
| CLI. Scriptconfig has a tool to help with this too. Given this janky definition, | ||
| we can port to a more ellegant style. We can run | ||
| ``print(config.port_to_dataconf())`` which prints: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| argparse CLI generated by scriptconfig 0.7.12 | ||
| """ | ||
| simple_option1 = scfg.Value(1, help=None) | ||
| simple_option2 = scfg.Value(2, help=None) | ||
| And then use that to make the refactor much easier. | ||
| The final state of a scriptconfig program might look something like this: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| This is my CLI description | ||
| """ | ||
| simple_option1 = scfg.Value(1, help=ub.paragraph( | ||
| ''' | ||
| A reasonably detailed but concise description of an argument. | ||
| About one paragraph is reasonable. | ||
| ''') | ||
| simple_option2 = scfg.Value(2, help='more help is better') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| config = cls.cli(cmdline=cmdline, data=kwargs) | ||
| my_function(config) | ||
| def my_function(config): | ||
| # Continued algorithmic and debugging logic | ||
| ... | ||
| Note that the fundamental impact on the ``...`` -- i.e. the intereting part of | ||
| the function -- remain completely unchanged! From it's point of view, you never | ||
| did anything to the original ``config`` dictionary, because scriptconfig | ||
| duck-typed it at every stage. | ||
| Installation | ||
| ------------ | ||
| The `scriptconfig <https://pypi.org/project/scriptconfig/>`_ package can be installed via pip: | ||
| .. code-block:: bash | ||
| pip install scriptconfig | ||
| To install with argcomplete and rich-argparse support, either install these | ||
| packages separately or use: | ||
| .. code-block:: bash | ||
| pip install scriptconfig[optional] | ||
| Features | ||
| -------- | ||
| - Serializes to JSON | ||
| - Dict-like interface. By default a ``Config`` object operates independent of config files or the command line. | ||
| - Can create command line interfaces | ||
| - Can directly create an independent argparse object | ||
| - Can use special command line loading using ``self.load(cmdline=True)``. This extends the basic argparse interface with: | ||
| - Can specify options as either ``--option value`` or ``--option=value`` | ||
| - Default config options allow for "smartcasting" values like lists and paths | ||
| - Automatically add ``--config``, ``--dumps``, and ``--dump`` CLI options | ||
| when reading cmdline via ``load``. | ||
| - Fuzzy hyphen matching: e.g. ``--foo-bar=2`` and ``--foo_bar=2`` are treated the same for argparse options (note: modal commands do not have this option yet) | ||
| - Inheritance unions configs. | ||
| - Modal configs (see scriptconfig.modal) | ||
| - Integration with `argcomplete <https://pypi.org/project/argcomplete/>`_ for shell autocomplete. | ||
| - Integration with `rich_argparse <https://pypi.org/project/rich_argparse/>`_ for colorful CLI help pages. | ||
| Example Script | ||
| -------------- | ||
| Scriptconfig is used to define a flat configuration dictionary with values that | ||
| can be specified via Python keyword arguments, command line parameters, or a | ||
| YAML config file. Consider the following script that prints its config, opens a | ||
| file, computes its hash, and then prints it to stdout. | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| import hashlib | ||
| class FileHashConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| fpath = scfg.Value(None, position=1, help='a path to a file to hash') | ||
| hasher = scfg.Value('sha1', choices=['sha1', 'sha512'], help='a name of a hashlib hasher') | ||
| def main(**kwargs): | ||
| config = FileHashConfig.cli(data=kwargs) | ||
| print('config = {!r}'.format(config)) | ||
| fpath = config['fpath'] | ||
| hasher = getattr(hashlib, config['hasher'])() | ||
| with open(fpath, 'rb') as file: | ||
| hasher.update(file.read()) | ||
| hashstr = hasher.hexdigest() | ||
| print('The {hasher} hash of {fpath} is {hashstr}'.format( | ||
| hashstr=hashstr, **config)) | ||
| if __name__ == '__main__': | ||
| main() | ||
| If this script is in a module ``hash_demo.py`` (e.g. in the examples folder of | ||
| this repo), it can be invoked in these following ways. | ||
| Purely from the command line: | ||
| .. code-block:: bash | ||
| # Get help | ||
| python hash_demo.py --help | ||
| # Using key-val pairs | ||
| python hash_demo.py --fpath=$HOME/.bashrc --hasher=sha1 | ||
| # Using a positional arguments and other defaults | ||
| python hash_demo.py $HOME/.bashrc | ||
| From the command line using a YAML config: | ||
| .. code-block:: bash | ||
| # Write out a config file | ||
| echo '{"fpath": "hashconfig.json", "hasher": "sha512"}' > hashconfig.json | ||
| # Use the special `--config` cli arg provided by scriptconfig | ||
| python hash_demo.py --config=hashconfig.json | ||
| # You can also mix and match, this overrides the hasher in the config with sha1 | ||
| python hash_demo.py --config=hashconfig.json --hasher=sha1 | ||
| Lastly you can call it from good ol' Python. | ||
| .. code-block:: python | ||
| import hash_demo | ||
| hash_demo.main(fpath=hash_demo.__file__, hasher='sha512') | ||
| Modal CLIs | ||
| ---------- | ||
| A ModalCLI defines a way to group several smaller scriptconfig CLIs into a | ||
| single parent CLI that chooses between them "modally". E.g. if we define two | ||
| configs: do_foo and do_bar, we use ModalCLI to define a parent program that can | ||
| run one or the other. Let's make this more concrete. | ||
| Consider the code in ``examples/demo_modal.py``: | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| class DoFooCLI(scfg.DataConfig): | ||
| __command__ = 'do_foo' | ||
| option1 = scfg.Value(None, help='option1') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| self = cls.cli(cmdline=cmdline, data=kwargs) | ||
| print('Called Foo with: ' + str(self)) | ||
| class DoBarCLI(scfg.DataConfig): | ||
| __command__ = 'do_bar' | ||
| option1 = scfg.Value(None, help='option1') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| self = cls.cli(cmdline=cmdline, data=kwargs) | ||
| print('Called Bar with: ' + str(self)) | ||
| class MyModalCLI(scfg.ModalCLI): | ||
| __version__ = '1.2.3' | ||
| foo = DoFooCLI | ||
| bar = DoBarCLI | ||
| if __name__ == '__main__': | ||
| MyModalCLI().main() | ||
| Running: ``python examples/demo_modal.py --help``, results in: | ||
| .. code-block:: | ||
| usage: demo_modal.py [-h] [--version] {do_foo,do_bar} ... | ||
| options: | ||
| -h, --help show this help message and exit | ||
| --version show version number and exit (default: False) | ||
| commands: | ||
| {do_foo,do_bar} specify a command to run | ||
| do_foo argparse CLI generated by scriptconfig 0.7.12 | ||
| do_bar argparse CLI generated by scriptconfig 0.7.12 | ||
| And if you specify a command, ``python examples/demo_modal.py do_bar --help``, you get the help for that subcommand: | ||
| .. code-block:: | ||
| usage: DoBarCLI [-h] [--option1 OPTION1] | ||
| argparse CLI generated by scriptconfig 0.7.12 | ||
| options: | ||
| -h, --help show this help message and exit | ||
| --option1 OPTION1 option1 (default: None) | ||
| Autocomplete | ||
| ------------ | ||
| If you installed the optional `argcomplete <https://pypi.org/project/argcomplete/>`_ package you will find that pressing | ||
| tab will autocomplete registered arguments for scriptconfig CLIs. See project instructions for details, but on standard Linux | ||
| distributions you can enable global completion via: | ||
| .. code:: bash | ||
| pip install argcomplete | ||
| mkdir -p ~/.bash_completion.d | ||
| activate-global-python-argcomplete --dest ~/.bash_completion.d | ||
| source ~/.bash_completion.d/python-argcomplete | ||
| And then add these lines to your ``.bashrc``: | ||
| .. code:: bash | ||
| if [ -f "$HOME/.bash_completion.d/python-argcomplete" ]; then | ||
| source ~/.bash_completion.d/python-argcomplete | ||
| fi | ||
| Lastly, ensure your Python script has the following two comments at the top: | ||
| .. code:: python | ||
| #!/usr/bin/env python | ||
| # PYTHON_ARGCOMPLETE_OK | ||
| Project Design Goals | ||
| -------------------- | ||
| * Write Python programs that can be invoked either through the commandline | ||
| or via Python itself. | ||
| * Drop in replacement for any dictionary-based configuration system. | ||
| * Intuitive parsing (currently working on this), ideally improve on | ||
| argparse if possible. This means being able to easily specify simple | ||
| lists, numbers, strings, and paths. | ||
| To get started lets consider some example usage: | ||
| .. code-block:: python | ||
| >>> import scriptconfig as scfg | ||
| >>> # In its simplest incarnation, the config class specifies default values. | ||
| >>> # For each configuration parameter. | ||
| >>> class ExampleConfig(scfg.DataConfig): | ||
| >>> num = 1 | ||
| >>> mode = 'bar' | ||
| >>> ignore = ['baz', 'biz'] | ||
| >>> # Creating an instance, starts using the defaults | ||
| >>> config = ExampleConfig() | ||
| >>> assert config['num'] == 1 | ||
| >>> # Or pass in known data. (load as shown in the original example still works) | ||
| >>> kwargs = {'num': 2} | ||
| >>> config = ExampleConfig.cli(default=kwargs, cmdline=False) | ||
| >>> assert config['num'] == 2 | ||
| >>> # The `load` method can also be passed a JSON/YAML file/path. | ||
| >>> config_fpath = '/tmp/foo' | ||
| >>> open(config_fpath, 'w').write('{"mode": "foo"}') | ||
| >>> config.load(config_fpath, cmdline=False) | ||
| >>> assert config['num'] == 2 | ||
| >>> assert config['mode'] == "foo" | ||
| >>> # It is possbile to load only from CLI by setting cmdline=True | ||
| >>> # or by setting it to a custom sys.argv | ||
| >>> config = ExampleConfig.cli(argv=['--num=4']) | ||
| >>> assert config['num'] == 4 | ||
| >>> # Note that using `config.load(cmdline=True)` will just use the | ||
| >>> # contents of sys.argv | ||
| Notice in the above example the keys in your default dictionary are command | ||
| line arguments and values are their defaults. You can augment default values | ||
| by wrapping them in ``scriptconfig.Value`` objects to encapsulate information | ||
| like help documentation or type information. | ||
| .. code-block:: python | ||
| >>> import scriptconfig as scfg | ||
| >>> class ExampleConfig(scfg.Config): | ||
| >>> __default__ = { | ||
| >>> 'num': scfg.Value(1, help='a number'), | ||
| >>> 'mode': scfg.Value('bar', help='mode1 help'), | ||
| >>> 'mode2': scfg.Value('bar', type=str, help='mode2 help'), | ||
| >>> 'ignore': scfg.Value(['baz', 'biz'], help='list of ignore vals'), | ||
| >>> } | ||
| >>> config = ExampleConfig() | ||
| >>> # smartcast can handle lists as long as there are no spaces | ||
| >>> config.load(cmdline=['--ignore=spam,eggs']) | ||
| >>> assert config['ignore'] == ['spam', 'eggs'] | ||
| >>> # Note that the Value type can influence how data is parsed | ||
| >>> config.load(cmdline=['--mode=spam,eggs', '--mode2=spam,eggs']) | ||
| (Note the above example uses the older ``Config`` usage pattern where | ||
| attributes are members of a ``__default__`` dictionary. The ``DataConfig`` | ||
| class should be favored moving forward past version 0.6.2. However, | ||
| the ``__default__`` attribute is always available if you have an existing | ||
| dictionary you want to wrap with scriptconfig. | ||
| Gotchas | ||
| ------- | ||
| **CLI Values with commas:** | ||
| When using ``scriptconfig`` to generate a command line interface, it uses a | ||
| function called ``smartcast`` to try to determine input type when it is not | ||
| explicitly given. If you've ever used a program that tries to be "smart" you'll | ||
| know this can end up with some weird behavior. The case where that happens here | ||
| is when you pass a value that contains commas on the command line. If you don't | ||
| specify the default value as a ``scriptconfig.Value`` with a specified | ||
| ``type``, if will interpret your input as a list of values. In the future we | ||
| may change the behavior of ``smartcast``, or prevent it from being used as a | ||
| default. | ||
| **Boolean flags and positional arguments:** | ||
| ``scriptconfig`` always provides a key/value way to express arguments. However, it also | ||
| recognizes that sometimes you want to just type ``--flag`` and not ``--flag=1``. | ||
| We allow for this for ``Values`` with ``isflag=1``, but this causes a | ||
| corner-case ambituity with positional arguments. For the following example: | ||
| .. code:: python | ||
| class MyConfig(scfg.DataConfig): | ||
| arg1 = scfg.Value(None, position=1) | ||
| flag1 = scfg.Value(False, isflag=True, position=1) | ||
| For ``--flag 1`` We cannot determine if you wanted | ||
| ``{'arg1': 1, 'flag1': False}`` or ``{'arg1': None, 'flag1': True}``. | ||
| This is fixable by either using strict key/value arguments, expressing all | ||
| positional arguments before using flag arguments, or using the `` -- `` | ||
| construct and putting all positional arguments at the end. In the future we may | ||
| raise an AmbiguityError when specifying arguments like this, but for now we | ||
| leave the behavior undefined. | ||
| FAQ | ||
| --- | ||
| Question: How do I override the default values for a scriptconfig object using JSON file? | ||
| Answer: This depends if you want to pass the path to that JSON file via the command line or if you have that file in memory already. There are ways to do either. In the first case you can pass ``--config=<path-to-your-file>`` (assuming you have set the ``cmdline=True`` keyword arg when creating your config object e.g.: ``config = MyConfig(cmdline=True)``. In the second case when you create an instance of the scriptconfig object pass the ``default=<your dict>`` when creating the object: e.g. ``config = MyConfig(default=json.load(open(fpath, 'r')))``. But the special ``--config`` ``--dump`` and ``--dumps`` CLI arg is baked into script config to make this easier. | ||
| Related Software | ||
| ---------------- | ||
| I've never been completely happy with existing config / argument parser | ||
| software. I prefer to not use decorators, so click and to some extend hydra are | ||
| no-gos. Fire is nice when you want a really quick CLI, but is not so nice if | ||
| you ever go to deploy the program in the real world. | ||
| The builtin argparse in Python is pretty good, but I with it was easier to do | ||
| things like allowing arguments to be flags or key/value pairs. This library | ||
| uses argparse under the hood because of its stable and standard backend, but | ||
| that does mean we inherit some of its quirks. | ||
| The configargparse library - like this one - augments argparse with the ability | ||
| to read defaults from config files, but it has some major usage limitations due | ||
| to its implementation and there are better options (like jsonargparse). It also | ||
| does not support the use case of calling the CLI as a Python function very | ||
| well. | ||
| The jsonargparse library is newer than this one, and looks very compelling. I | ||
| feel like the definition of CLIs in this library are complementary and I'm | ||
| considering adding support in this library for jsonargparse because it solves | ||
| the problem of nested configurations and I would like to inherit from that. | ||
| Keep an eye out for this feature in future work. | ||
| Hydra - https://hydra.cc/docs/intro/ | ||
| OmegaConf - https://omegaconf.readthedocs.io/en/latest/index.html | ||
| Argparse - https://docs.python.org/3/library/argparse.html | ||
| JsonArgparse - https://jsonargparse.readthedocs.io/en/stable/index.html | ||
| Fire - https://pypi.org/project/fire/ | ||
| Click - https://pypi.org/project/click/ | ||
| ConfigArgparse - https://pypi.org/project/ConfigArgParse/ | ||
| TODO | ||
| ---- | ||
| - [ ] Nested Modal CLI's | ||
| - [ ] Fuzzy hyphens in ModelCLIs | ||
| - [X] Policy on nested heirachies (currently disallowed) - jsonargparse will be the solution here. | ||
| - [ ] How to best integrate with jsonargparse | ||
| - [ ] Policy on smartcast (currently enabled) | ||
| - [ ] Find a way to gracefully way to make smartcast do less. (e.g. no list parsing, but int is ok, we may think about accepting YAML) | ||
| - [X] Policy on positional arguments (currently experimental) - we have implemented them permissively with one undefined corner case. | ||
| - [X] Fixed length - nope | ||
| - [X] Variable length | ||
| - [X] Can argparse be modified to always allow for them to appear at the beginning or end? - Probably not. | ||
| - [x] Can we get argparse to allow a positional arg change the value of a prefixed arg and still have a sane help menu? | ||
| - [x] Policy on boolean flags - See the ``isflag`` argument of ``scriptconfig.Value`` | ||
| - [x] Improve over argparse's default autogenerated help docs (needs exploration on what is possible with argparse and where extensions are feasible) | ||
| .. |GitlabCIPipeline| image:: https://gitlab.kitware.com/utils/scriptconfig/badges/main/pipeline.svg | ||
| :target: https://gitlab.kitware.com/utils/scriptconfig/-/jobs | ||
| .. |GitlabCICoverage| image:: https://gitlab.kitware.com/utils/scriptconfig/badges/main/coverage.svg | ||
| :target: https://gitlab.kitware.com/utils/scriptconfig/commits/main | ||
| .. # See: https://ci.appveyor.com/project/jon.crall/scriptconfig/settings/badges | ||
| .. |Appveyor| image:: https://ci.appveyor.com/api/projects/status/br3p8lkuvol2vas4/branch/main?svg=true | ||
| :target: https://ci.appveyor.com/project/jon.crall/scriptconfig/branch/main | ||
| .. |Codecov| image:: https://codecov.io/github/Erotemic/scriptconfig/badge.svg?branch=main&service=github | ||
| :target: https://codecov.io/github/Erotemic/scriptconfig?branch=main | ||
| .. |Pypi| image:: https://img.shields.io/pypi/v/scriptconfig.svg | ||
| :target: https://pypi.python.org/pypi/scriptconfig | ||
| .. |PypiDownloads| image:: https://img.shields.io/pypi/dm/scriptconfig.svg | ||
| :target: https://pypistats.org/packages/scriptconfig | ||
| .. |ReadTheDocs| image:: https://readthedocs.org/projects/scriptconfig/badge/?version=latest | ||
| :target: http://scriptconfig.readthedocs.io/en/latest/ |
| ubelt>=1.3.6 | ||
| [:python_version < "3.10" and python_version >= "3.9"] | ||
| PyYAML>=5.4.1 | ||
| [:python_version < "3.11" and python_version >= "3.10"] | ||
| PyYAML>=6.0 | ||
| [:python_version < "3.12" and python_version >= "3.11"] | ||
| PyYAML>=6.0 | ||
| [:python_version < "3.7" and python_version >= "3.6"] | ||
| PyYAML>=5.4.1 | ||
| [:python_version < "3.8" and python_version >= "3.7"] | ||
| PyYAML>=5.4.1 | ||
| [:python_version < "3.9" and python_version >= "3.8"] | ||
| PyYAML>=5.4.1 | ||
| [:python_version < "4.0" and python_version >= "3.12"] | ||
| PyYAML>=6.0.1 | ||
| [all] | ||
| ubelt>=1.3.6 | ||
| xdoctest>=1.1.5 | ||
| argcomplete>=3.0.5 | ||
| [all-strict] | ||
| ubelt==1.3.6 | ||
| xdoctest==1.1.5 | ||
| argcomplete==3.0.5 | ||
| [all-strict:python_version < "2.7" and python_version >= "2.6"] | ||
| coverage==4.5 | ||
| [all-strict:python_version < "2.8.0" and python_version >= "2.7.0"] | ||
| pytest<=4.6.11,==4.6.0 | ||
| pytest-cov==2.8.1 | ||
| [all-strict:python_version < "3.10" and python_version >= "3.9"] | ||
| PyYAML==5.4.1 | ||
| coverage==5.3.1 | ||
| numpy==1.19.3 | ||
| [all-strict:python_version < "3.10.0" and python_version >= "3.7.0"] | ||
| pytest==4.6.0 | ||
| [all-strict:python_version < "3.11" and python_version >= "3.10"] | ||
| PyYAML==6.0 | ||
| numpy==1.21.6 | ||
| [all-strict:python_version < "3.12" and python_version >= "3.11"] | ||
| PyYAML==6.0 | ||
| numpy==1.23.2 | ||
| [all-strict:python_version < "3.4" and python_version >= "2.7"] | ||
| coverage==5.3.1 | ||
| numpy==1.11.1 | ||
| [all-strict:python_version < "3.5" and python_version >= "3.4"] | ||
| coverage==4.3.4 | ||
| numpy==1.11.1 | ||
| [all-strict:python_version < "3.5.0" and python_version >= "3.4.0"] | ||
| pytest<=4.6.11,==4.6.0 | ||
| pytest-cov==2.8.1 | ||
| [all-strict:python_version < "3.6" and python_version >= "3.5"] | ||
| coverage==5.3.1 | ||
| numpy==1.11.1 | ||
| [all-strict:python_version < "3.6.0" and python_version >= "3.5.0"] | ||
| pytest<=6.1.2,==4.6.0 | ||
| pytest-cov==2.9.0 | ||
| [all-strict:python_version < "3.7" and python_version >= "3.6"] | ||
| PyYAML==5.4.1 | ||
| coverage==6.1.1 | ||
| numpy==1.12.0 | ||
| [all-strict:python_version < "3.7.0" and python_version >= "3.6.0"] | ||
| pytest==4.6.0 | ||
| [all-strict:python_version < "3.8" and python_version >= "3.7"] | ||
| PyYAML==5.4.1 | ||
| coverage==6.1.1 | ||
| numpy==1.14.5 | ||
| [all-strict:python_version < "3.9" and python_version >= "3.8"] | ||
| PyYAML==5.4.1 | ||
| coverage==6.1.1 | ||
| numpy==1.19.2 | ||
| [all-strict:python_version < "4.0" and python_version >= "3.12"] | ||
| PyYAML==6.0.1 | ||
| numpy==1.26.0 | ||
| [all-strict:python_version >= "3.10"] | ||
| coverage==6.1.1 | ||
| [all-strict:python_version >= "3.10.0"] | ||
| pytest==6.2.5 | ||
| [all-strict:python_version >= "3.6"] | ||
| omegaconf==2.2.2 | ||
| [all-strict:python_version >= "3.6.0"] | ||
| pytest-cov==3.0.0 | ||
| [all-strict:python_version >= "3.7"] | ||
| rich_argparse==1.1.0 | ||
| [all:python_version < "2.7" and python_version >= "2.6"] | ||
| coverage>=4.5 | ||
| [all:python_version < "2.8.0" and python_version >= "2.7.0"] | ||
| pytest<=4.6.11,>=4.6.0 | ||
| pytest-cov>=2.8.1 | ||
| [all:python_version < "3.10" and python_version >= "3.9"] | ||
| PyYAML>=5.4.1 | ||
| coverage>=5.3.1 | ||
| numpy>=1.19.3 | ||
| [all:python_version < "3.10.0" and python_version >= "3.7.0"] | ||
| pytest>=4.6.0 | ||
| [all:python_version < "3.11" and python_version >= "3.10"] | ||
| PyYAML>=6.0 | ||
| numpy>=1.21.6 | ||
| [all:python_version < "3.12" and python_version >= "3.11"] | ||
| PyYAML>=6.0 | ||
| numpy>=1.23.2 | ||
| [all:python_version < "3.4" and python_version >= "2.7"] | ||
| coverage>=5.3.1 | ||
| numpy>=1.11.1 | ||
| [all:python_version < "3.5" and python_version >= "3.4"] | ||
| coverage>=4.3.4 | ||
| numpy>=1.11.1 | ||
| [all:python_version < "3.5.0" and python_version >= "3.4.0"] | ||
| pytest<=4.6.11,>=4.6.0 | ||
| pytest-cov>=2.8.1 | ||
| [all:python_version < "3.6" and python_version >= "3.5"] | ||
| coverage>=5.3.1 | ||
| numpy>=1.11.1 | ||
| [all:python_version < "3.6.0" and python_version >= "3.5.0"] | ||
| pytest<=6.1.2,>=4.6.0 | ||
| pytest-cov>=2.9.0 | ||
| [all:python_version < "3.7" and python_version >= "3.6"] | ||
| PyYAML>=5.4.1 | ||
| coverage>=6.1.1 | ||
| numpy>=1.12.0 | ||
| [all:python_version < "3.7.0" and python_version >= "3.6.0"] | ||
| pytest>=4.6.0 | ||
| [all:python_version < "3.8" and python_version >= "3.7"] | ||
| PyYAML>=5.4.1 | ||
| coverage>=6.1.1 | ||
| numpy>=1.14.5 | ||
| [all:python_version < "3.9" and python_version >= "3.8"] | ||
| PyYAML>=5.4.1 | ||
| coverage>=6.1.1 | ||
| numpy>=1.19.2 | ||
| [all:python_version < "4.0" and python_version >= "3.12"] | ||
| PyYAML>=6.0.1 | ||
| numpy>=1.26.0 | ||
| [all:python_version >= "3.10"] | ||
| coverage>=6.1.1 | ||
| [all:python_version >= "3.10.0"] | ||
| pytest>=6.2.5 | ||
| [all:python_version >= "3.6"] | ||
| omegaconf>=2.2.2 | ||
| [all:python_version >= "3.6.0"] | ||
| pytest-cov>=3.0.0 | ||
| [all:python_version >= "3.7"] | ||
| rich_argparse>=1.1.0 | ||
| [optional] | ||
| argcomplete>=3.0.5 | ||
| [optional-strict] | ||
| argcomplete==3.0.5 | ||
| [optional-strict:python_version < "3.10" and python_version >= "3.9"] | ||
| numpy==1.19.3 | ||
| [optional-strict:python_version < "3.11" and python_version >= "3.10"] | ||
| numpy==1.21.6 | ||
| [optional-strict:python_version < "3.12" and python_version >= "3.11"] | ||
| numpy==1.23.2 | ||
| [optional-strict:python_version < "3.4" and python_version >= "2.7"] | ||
| numpy==1.11.1 | ||
| [optional-strict:python_version < "3.5" and python_version >= "3.4"] | ||
| numpy==1.11.1 | ||
| [optional-strict:python_version < "3.6" and python_version >= "3.5"] | ||
| numpy==1.11.1 | ||
| [optional-strict:python_version < "3.7" and python_version >= "3.6"] | ||
| numpy==1.12.0 | ||
| [optional-strict:python_version < "3.8" and python_version >= "3.7"] | ||
| numpy==1.14.5 | ||
| [optional-strict:python_version < "3.9" and python_version >= "3.8"] | ||
| numpy==1.19.2 | ||
| [optional-strict:python_version < "4.0" and python_version >= "3.12"] | ||
| numpy==1.26.0 | ||
| [optional-strict:python_version >= "3.6"] | ||
| omegaconf==2.2.2 | ||
| [optional-strict:python_version >= "3.7"] | ||
| rich_argparse==1.1.0 | ||
| [optional:python_version < "3.10" and python_version >= "3.9"] | ||
| numpy>=1.19.3 | ||
| [optional:python_version < "3.11" and python_version >= "3.10"] | ||
| numpy>=1.21.6 | ||
| [optional:python_version < "3.12" and python_version >= "3.11"] | ||
| numpy>=1.23.2 | ||
| [optional:python_version < "3.4" and python_version >= "2.7"] | ||
| numpy>=1.11.1 | ||
| [optional:python_version < "3.5" and python_version >= "3.4"] | ||
| numpy>=1.11.1 | ||
| [optional:python_version < "3.6" and python_version >= "3.5"] | ||
| numpy>=1.11.1 | ||
| [optional:python_version < "3.7" and python_version >= "3.6"] | ||
| numpy>=1.12.0 | ||
| [optional:python_version < "3.8" and python_version >= "3.7"] | ||
| numpy>=1.14.5 | ||
| [optional:python_version < "3.9" and python_version >= "3.8"] | ||
| numpy>=1.19.2 | ||
| [optional:python_version < "4.0" and python_version >= "3.12"] | ||
| numpy>=1.26.0 | ||
| [optional:python_version >= "3.6"] | ||
| omegaconf>=2.2.2 | ||
| [optional:python_version >= "3.7"] | ||
| rich_argparse>=1.1.0 | ||
| [runtime-strict] | ||
| ubelt==1.3.6 | ||
| [runtime-strict:python_version < "3.10" and python_version >= "3.9"] | ||
| PyYAML==5.4.1 | ||
| [runtime-strict:python_version < "3.11" and python_version >= "3.10"] | ||
| PyYAML==6.0 | ||
| [runtime-strict:python_version < "3.12" and python_version >= "3.11"] | ||
| PyYAML==6.0 | ||
| [runtime-strict:python_version < "3.7" and python_version >= "3.6"] | ||
| PyYAML==5.4.1 | ||
| [runtime-strict:python_version < "3.8" and python_version >= "3.7"] | ||
| PyYAML==5.4.1 | ||
| [runtime-strict:python_version < "3.9" and python_version >= "3.8"] | ||
| PyYAML==5.4.1 | ||
| [runtime-strict:python_version < "4.0" and python_version >= "3.12"] | ||
| PyYAML==6.0.1 | ||
| [tests] | ||
| xdoctest>=1.1.5 | ||
| [tests-strict] | ||
| xdoctest==1.1.5 | ||
| [tests-strict:python_version < "2.7" and python_version >= "2.6"] | ||
| coverage==4.5 | ||
| [tests-strict:python_version < "2.8.0" and python_version >= "2.7.0"] | ||
| pytest<=4.6.11,==4.6.0 | ||
| pytest-cov==2.8.1 | ||
| [tests-strict:python_version < "3.10" and python_version >= "3.9"] | ||
| coverage==5.3.1 | ||
| [tests-strict:python_version < "3.10.0" and python_version >= "3.7.0"] | ||
| pytest==4.6.0 | ||
| [tests-strict:python_version < "3.4" and python_version >= "2.7"] | ||
| coverage==5.3.1 | ||
| [tests-strict:python_version < "3.5" and python_version >= "3.4"] | ||
| coverage==4.3.4 | ||
| [tests-strict:python_version < "3.5.0" and python_version >= "3.4.0"] | ||
| pytest<=4.6.11,==4.6.0 | ||
| pytest-cov==2.8.1 | ||
| [tests-strict:python_version < "3.6" and python_version >= "3.5"] | ||
| coverage==5.3.1 | ||
| [tests-strict:python_version < "3.6.0" and python_version >= "3.5.0"] | ||
| pytest<=6.1.2,==4.6.0 | ||
| pytest-cov==2.9.0 | ||
| [tests-strict:python_version < "3.7" and python_version >= "3.6"] | ||
| coverage==6.1.1 | ||
| [tests-strict:python_version < "3.7.0" and python_version >= "3.6.0"] | ||
| pytest==4.6.0 | ||
| [tests-strict:python_version < "3.8" and python_version >= "3.7"] | ||
| coverage==6.1.1 | ||
| [tests-strict:python_version < "3.9" and python_version >= "3.8"] | ||
| coverage==6.1.1 | ||
| [tests-strict:python_version >= "3.10"] | ||
| coverage==6.1.1 | ||
| [tests-strict:python_version >= "3.10.0"] | ||
| pytest==6.2.5 | ||
| [tests-strict:python_version >= "3.6.0"] | ||
| pytest-cov==3.0.0 | ||
| [tests:python_version < "2.7" and python_version >= "2.6"] | ||
| coverage>=4.5 | ||
| [tests:python_version < "2.8.0" and python_version >= "2.7.0"] | ||
| pytest<=4.6.11,>=4.6.0 | ||
| pytest-cov>=2.8.1 | ||
| [tests:python_version < "3.10" and python_version >= "3.9"] | ||
| coverage>=5.3.1 | ||
| [tests:python_version < "3.10.0" and python_version >= "3.7.0"] | ||
| pytest>=4.6.0 | ||
| [tests:python_version < "3.4" and python_version >= "2.7"] | ||
| coverage>=5.3.1 | ||
| [tests:python_version < "3.5" and python_version >= "3.4"] | ||
| coverage>=4.3.4 | ||
| [tests:python_version < "3.5.0" and python_version >= "3.4.0"] | ||
| pytest<=4.6.11,>=4.6.0 | ||
| pytest-cov>=2.8.1 | ||
| [tests:python_version < "3.6" and python_version >= "3.5"] | ||
| coverage>=5.3.1 | ||
| [tests:python_version < "3.6.0" and python_version >= "3.5.0"] | ||
| pytest<=6.1.2,>=4.6.0 | ||
| pytest-cov>=2.9.0 | ||
| [tests:python_version < "3.7" and python_version >= "3.6"] | ||
| coverage>=6.1.1 | ||
| [tests:python_version < "3.7.0" and python_version >= "3.6.0"] | ||
| pytest>=4.6.0 | ||
| [tests:python_version < "3.8" and python_version >= "3.7"] | ||
| coverage>=6.1.1 | ||
| [tests:python_version < "3.9" and python_version >= "3.8"] | ||
| coverage>=6.1.1 | ||
| [tests:python_version >= "3.10"] | ||
| coverage>=6.1.1 | ||
| [tests:python_version >= "3.10.0"] | ||
| pytest>=6.2.5 | ||
| [tests:python_version >= "3.6.0"] | ||
| pytest-cov>=3.0.0 |
| LICENSE | ||
| MANIFEST.in | ||
| README.rst | ||
| pyproject.toml | ||
| setup.py | ||
| requirements/docs.txt | ||
| requirements/linting.txt | ||
| requirements/optional.txt | ||
| requirements/runtime.txt | ||
| requirements/tests.txt | ||
| scriptconfig/__init__.py | ||
| scriptconfig/_ubelt_repr_extension.py | ||
| scriptconfig/_ubelt_repr_extension.pyi | ||
| scriptconfig/argparse_ext.py | ||
| scriptconfig/argparse_ext.pyi | ||
| scriptconfig/cli.py | ||
| scriptconfig/cli.pyi | ||
| scriptconfig/config.py | ||
| scriptconfig/config.pyi | ||
| scriptconfig/dataconfig.py | ||
| scriptconfig/dataconfig.pyi | ||
| scriptconfig/dict_like.py | ||
| scriptconfig/dict_like.pyi | ||
| scriptconfig/file_like.py | ||
| scriptconfig/file_like.pyi | ||
| scriptconfig/modal.py | ||
| scriptconfig/modal.pyi | ||
| scriptconfig/py.typed | ||
| scriptconfig/smartcast.py | ||
| scriptconfig/smartcast.pyi | ||
| scriptconfig/value.py | ||
| scriptconfig/value.pyi | ||
| scriptconfig.egg-info/PKG-INFO | ||
| scriptconfig.egg-info/SOURCES.txt | ||
| scriptconfig.egg-info/dependency_links.txt | ||
| scriptconfig.egg-info/requires.txt | ||
| scriptconfig.egg-info/top_level.txt | ||
| scriptconfig/util/__init__.py | ||
| scriptconfig/util/util_class.py | ||
| scriptconfig/util/util_class.pyi | ||
| scriptconfig/util/util_exception.py | ||
| tests/test_aliases.py | ||
| tests/test_argparse_roundtrip.py | ||
| tests/test_class_attrs.py | ||
| tests/test_cli.py | ||
| tests/test_counter_flags.py | ||
| tests/test_data_versus_default.py | ||
| tests/test_dataconfig.py | ||
| tests/test_dictlike.py | ||
| tests/test_import.py | ||
| tests/test_inheritence.py | ||
| tests/test_lists.py | ||
| tests/test_paths.py | ||
| tests/test_pickle_dataconf.py | ||
| tests/test_post_init.py | ||
| tests/test_special_options.py | ||
| tests/test_urepr_ext.py |
| scriptconfig |
| [egg_info] | ||
| tag_build = | ||
| tag_date = 0 | ||
+247
| #!/usr/bin/env python | ||
| # Generated by ~/code/xcookie/xcookie/builders/setup.py | ||
| # based on part ~/code/xcookie/xcookie/rc/setup.py.in | ||
| import sys | ||
| import re | ||
| from os.path import exists, dirname, join | ||
| from setuptools import find_packages | ||
| from setuptools import setup | ||
| def parse_version(fpath): | ||
| """ | ||
| Statically parse the version number from a python file | ||
| """ | ||
| value = static_parse("__version__", fpath) | ||
| return value | ||
| def static_parse(varname, fpath): | ||
| """ | ||
| Statically parse the a constant variable from a python file | ||
| """ | ||
| import ast | ||
| if not exists(fpath): | ||
| raise ValueError("fpath={!r} does not exist".format(fpath)) | ||
| with open(fpath, "r") as file_: | ||
| sourcecode = file_.read() | ||
| pt = ast.parse(sourcecode) | ||
| class StaticVisitor(ast.NodeVisitor): | ||
| def visit_Assign(self, node): | ||
| for target in node.targets: | ||
| if getattr(target, "id", None) == varname: | ||
| self.static_value = node.value.s | ||
| visitor = StaticVisitor() | ||
| visitor.visit(pt) | ||
| try: | ||
| value = visitor.static_value | ||
| except AttributeError: | ||
| import warnings | ||
| value = "Unknown {}".format(varname) | ||
| warnings.warn(value) | ||
| return value | ||
| def parse_description(): | ||
| """ | ||
| Parse the description in the README file | ||
| CommandLine: | ||
| pandoc --from=markdown --to=rst --output=README.rst README.md | ||
| python -c "import setup; print(setup.parse_description())" | ||
| """ | ||
| readme_fpath = join(dirname(__file__), "README.rst") | ||
| # This breaks on pip install, so check that it exists. | ||
| if exists(readme_fpath): | ||
| with open(readme_fpath, "r") as f: | ||
| text = f.read() | ||
| return text | ||
| return "" | ||
| def parse_requirements(fname="requirements.txt", versions=False): | ||
| """ | ||
| Parse the package dependencies listed in a requirements file but strips | ||
| specific versioning information. | ||
| Args: | ||
| fname (str): path to requirements file | ||
| versions (bool | str, default=False): | ||
| If true include version specs. | ||
| If strict, then pin to the minimum version. | ||
| Returns: | ||
| List[str]: list of requirements items | ||
| CommandLine: | ||
| python -c "import setup, ubelt; print(ubelt.urepr(setup.parse_requirements()))" | ||
| """ | ||
| require_fpath = fname | ||
| def parse_line(line, dpath=""): | ||
| """ | ||
| Parse information from a line in a requirements text file | ||
| line = 'git+https://a.com/somedep@sometag#egg=SomeDep' | ||
| line = '-e git+https://a.com/somedep@sometag#egg=SomeDep' | ||
| """ | ||
| # Remove inline comments | ||
| comment_pos = line.find(" #") | ||
| if comment_pos > -1: | ||
| line = line[:comment_pos] | ||
| if line.startswith("-r "): | ||
| # Allow specifying requirements in other files | ||
| target = join(dpath, line.split(" ")[1]) | ||
| for info in parse_require_file(target): | ||
| yield info | ||
| else: | ||
| # See: https://www.python.org/dev/peps/pep-0508/ | ||
| info = {"line": line} | ||
| if line.startswith("-e "): | ||
| info["package"] = line.split("#egg=")[1] | ||
| else: | ||
| if "--find-links" in line: | ||
| # setuptools doesnt seem to handle find links | ||
| line = line.split("--find-links")[0] | ||
| if ";" in line: | ||
| pkgpart, platpart = line.split(";") | ||
| # Handle platform specific dependencies | ||
| # setuptools.readthedocs.io/en/latest/setuptools.html | ||
| # #declaring-platform-specific-dependencies | ||
| plat_deps = platpart.strip() | ||
| info["platform_deps"] = plat_deps | ||
| else: | ||
| pkgpart = line | ||
| platpart = None | ||
| # Remove versioning from the package | ||
| pat = "(" + "|".join([">=", "==", ">"]) + ")" | ||
| parts = re.split(pat, pkgpart, maxsplit=1) | ||
| parts = [p.strip() for p in parts] | ||
| info["package"] = parts[0] | ||
| if len(parts) > 1: | ||
| op, rest = parts[1:] | ||
| version = rest # NOQA | ||
| info["version"] = (op, version) | ||
| yield info | ||
| def parse_require_file(fpath): | ||
| dpath = dirname(fpath) | ||
| with open(fpath, "r") as f: | ||
| for line in f.readlines(): | ||
| line = line.strip() | ||
| if line and not line.startswith("#"): | ||
| for info in parse_line(line, dpath=dpath): | ||
| yield info | ||
| def gen_packages_items(): | ||
| if exists(require_fpath): | ||
| for info in parse_require_file(require_fpath): | ||
| parts = [info["package"]] | ||
| if versions and "version" in info: | ||
| if versions == "strict": | ||
| # In strict mode, we pin to the minimum version | ||
| if info["version"]: | ||
| # Only replace the first >= instance | ||
| verstr = "".join(info["version"]).replace(">=", "==", 1) | ||
| parts.append(verstr) | ||
| else: | ||
| parts.extend(info["version"]) | ||
| if not sys.version.startswith("3.4"): | ||
| # apparently package_deps are broken in 3.4 | ||
| plat_deps = info.get("platform_deps") | ||
| if plat_deps is not None: | ||
| parts.append(";" + plat_deps) | ||
| item = "".join(parts) | ||
| yield item | ||
| packages = list(gen_packages_items()) | ||
| return packages | ||
| # # Maybe use in the future? But has private deps | ||
| # def parse_requirements_alt(fpath='requirements.txt', versions='loose'): | ||
| # """ | ||
| # Args: | ||
| # versions (str): can be | ||
| # False or "free" - remove all constraints | ||
| # True or "loose" - use the greater or equal (>=) in the req file | ||
| # strict - replace all greater equal with equals | ||
| # """ | ||
| # # Note: different versions of pip might have different internals. | ||
| # # This may need to be fixed. | ||
| # from pip._internal.req import parse_requirements | ||
| # from pip._internal.network.session import PipSession | ||
| # requirements = [] | ||
| # for req in parse_requirements(fpath, session=PipSession()): | ||
| # if not versions or versions == 'free': | ||
| # req_name = req.requirement.split(' ')[0] | ||
| # requirements.append(req_name) | ||
| # elif versions == 'loose' or versions is True: | ||
| # requirements.append(req.requirement) | ||
| # elif versions == 'strict': | ||
| # part1, *rest = req.requirement.split(';') | ||
| # strict_req = ';'.join([part1.replace('>=', '==')] + rest) | ||
| # requirements.append(strict_req) | ||
| # else: | ||
| # raise KeyError(versions) | ||
| # requirements = [r.replace(' ', '') for r in requirements] | ||
| # return requirements | ||
| NAME = "scriptconfig" | ||
| INIT_PATH = "scriptconfig/__init__.py" | ||
| VERSION = parse_version(INIT_PATH) | ||
| if __name__ == "__main__": | ||
| setupkw = {} | ||
| setupkw["install_requires"] = parse_requirements( | ||
| "requirements/runtime.txt", versions="loose" | ||
| ) | ||
| setupkw["extras_require"] = { | ||
| "all": parse_requirements("requirements.txt", versions="loose"), | ||
| "tests": parse_requirements("requirements/tests.txt", versions="loose"), | ||
| "optional": parse_requirements("requirements/optional.txt", versions="loose"), | ||
| "all-strict": parse_requirements("requirements.txt", versions="strict"), | ||
| "runtime-strict": parse_requirements( | ||
| "requirements/runtime.txt", versions="strict" | ||
| ), | ||
| "tests-strict": parse_requirements("requirements/tests.txt", versions="strict"), | ||
| "optional-strict": parse_requirements( | ||
| "requirements/optional.txt", versions="strict" | ||
| ), | ||
| } | ||
| setupkw["name"] = NAME | ||
| setupkw["version"] = VERSION | ||
| setupkw["author"] = "Kitware Inc., Jon Crall" | ||
| setupkw["author_email"] = "kitware@kitware.com, jon.crall@kitware.com" | ||
| setupkw["url"] = "https://gitlab.kitware.com/utils/scriptconfig/" | ||
| setupkw["description"] = "Easy dict-based script configuration with CLI support" | ||
| setupkw["long_description"] = parse_description() | ||
| setupkw["long_description_content_type"] = "text/x-rst" | ||
| setupkw["license"] = "Apache 2" | ||
| setupkw["packages"] = find_packages(".") | ||
| setupkw["python_requires"] = ">=3.6" | ||
| setupkw["classifiers"] = [ | ||
| "Development Status :: 4 - Beta", | ||
| "Intended Audience :: Developers", | ||
| "Topic :: Software Development :: Libraries :: Python Modules", | ||
| "Topic :: Utilities", | ||
| "License :: OSI Approved :: Apache Software License", | ||
| "Programming Language :: Python :: 3.6", | ||
| "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", | ||
| ] | ||
| setupkw["package_data"] = {"scriptconfig": ["py.typed", "*.pyi"]} | ||
| setup(**setupkw) |
| def test_config_aliases(): | ||
| import scriptconfig as scfg | ||
| import pytest | ||
| # import ubelt as ub | ||
| __common_default__ = { | ||
| 'opt1': scfg.Value(None, alias=['option1']), | ||
| 'opt2': scfg.Value(None, alias=['option2', 'old_name']), | ||
| } | ||
| class Config1(scfg.Config): | ||
| __default__ = __common_default__ | ||
| with pytest.warns(Warning): | ||
| class Config2(scfg.Config): | ||
| default = __common_default__ | ||
| class Config3(scfg.DataConfig): | ||
| __default__ = __common_default__ | ||
| config1 = Config1() | ||
| config2 = Config2() | ||
| config3 = Config3() | ||
| config_instances = [config1, config2, config3] | ||
| for config in config_instances: | ||
| assert config['opt1'] == config['option1'] and config['opt1'] is None | ||
| config['opt1'] = 2 | ||
| assert config['opt1'] == config['option1'] == 2 |
| """ | ||
| Test porting back and forth to / from argparse | ||
| """ | ||
| import scriptconfig as scfg | ||
| import ubelt as ub | ||
| import argparse | ||
| def port_scriptconfig_from_argparse(): | ||
| """ | ||
| xdoctest ~/code/scriptconfig/tests/test_argparse_roundtrip.py port_scriptconfig_from_argparse | ||
| """ | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument('--flag1', action='count') | ||
| parser.add_argument('--flag2', action='store_true') | ||
| parser.add_argument('--flag3', action='count', help='specified looooooooooooooooooooooonggg help ') | ||
| parser.add_argument('--flag4', action='store_true', help='specified help') | ||
| text = scfg.Config.port_argparse(parser) | ||
| print(text) | ||
| tq = '"""' | ||
| want = ub.codeblock( | ||
| """ | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ + tq + """ | ||
| $ | ||
| """ + tq + """ | ||
| flag1 = scfg.Value(None, isflag='counter', help=None) | ||
| flag2 = scfg.Value(False, isflag=True, help=None) | ||
| flag3 = scfg.Value(None, isflag='counter', help=ub.paragraph( | ||
| ''' | ||
| specified looooooooooooooooooooooonggg help | ||
| ''')) | ||
| flag4 = scfg.Value(False, isflag=True, help='specified help') | ||
| """).replace('$', '') | ||
| print(text) | ||
| print(want) | ||
| assert text == want | ||
| ns = {} | ||
| exec(text, ns, ns) | ||
| MyConfig = ns['MyConfig'] | ||
| # Note: we currently can't create argparse objects with the same flexible | ||
| # flag or key/value specification. Future work may fix this. | ||
| recon = MyConfig().port_to_argparse() | ||
| print(recon) | ||
| def port_argparse_from_scriptconfig(): | ||
| """ | ||
| xdoctest ~/code/scriptconfig/tests/test_argparse_roundtrip.py port_argparse_from_scriptconfig | ||
| """ | ||
| class MyConfig(scfg.DataConfig): | ||
| param1 = scfg.Value(None, type=str, help='help text') | ||
| argparse_text = MyConfig().port_to_argparse() | ||
| want = ub.codeblock( | ||
| """ | ||
| import argparse | ||
| parser = argparse.ArgumentParser( | ||
| description='argparse CLI generated by scriptconfig 0.7.11', | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ) | ||
| parser.add_argument('--param1', help='help text', type=str, dest='param1', required=False) | ||
| """).replace('$', '') | ||
| print(argparse_text) | ||
| print(want) | ||
| assert argparse_text == want | ||
| def port_argparse_from_scriptconfig_with_unwrapped_values(): | ||
| """ | ||
| xdoctest ~/code/scriptconfig/tests/test_argparse_roundtrip.py port_argparse_from_scriptconfig_with_unwrapped_values | ||
| """ | ||
| class MyConfig(scfg.DataConfig): | ||
| option1 = scfg.Value('default1', help='option1 help') | ||
| option2 = scfg.Value('default2', help='option2 help') | ||
| option3 = scfg.Value('default3', help='option3 help') | ||
| option4 = 'default4' | ||
| argparse_text = MyConfig().port_to_argparse() | ||
| print(argparse_text) | ||
| want = ub.codeblock( | ||
| """ | ||
| import argparse | ||
| parser = argparse.ArgumentParser( | ||
| prog='MyConfig', | ||
| description='argparse CLI generated by scriptconfig 0.7.12', | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ) | ||
| parser.add_argument('--option1', help='option1 help', default='default1', dest='option1', required=False) | ||
| parser.add_argument('--option2', help='option2 help', default='default2', dest='option2', required=False) | ||
| parser.add_argument('--option3', help='option3 help', default='default3', dest='option3', required=False) | ||
| parser.add_argument('--option4', help='', default='default4', dest='option4', required=False) | ||
| """).replace('$', '') | ||
| print(want) | ||
| assert argparse_text == want |
| """ | ||
| Test that class attributes are correctly initialized. | ||
| """ | ||
| def test_class_inst_default_attr(): | ||
| """ | ||
| There are a lot of ways to access the defaults. We have | ||
| default : class attribute | ||
| __default__ : class attribute | ||
| default : instance attribute | ||
| __default__ : instance attribute | ||
| _default : instance attribute | ||
| """ | ||
| import scriptconfig as scfg | ||
| import pytest | ||
| with pytest.warns(Warning): | ||
| class Config1(scfg.Config): | ||
| default = { | ||
| 'option1': scfg.Value((1, 2, 3), tuple, alias='a'), | ||
| 'option2': 'bar', | ||
| 'option3': None, | ||
| } | ||
| class Config2(scfg.Config): | ||
| __default__ = { | ||
| 'option1': scfg.Value((1, 2, 3), tuple, alias='a'), | ||
| 'option2': 'bar', | ||
| 'option3': None, | ||
| } | ||
| class DataConfig3(scfg.DataConfig): | ||
| __default__ = { | ||
| 'option1': scfg.Value((1, 2, 3), tuple, alias='a'), | ||
| 'option2': 'bar', | ||
| 'option3': None, | ||
| } | ||
| class DataConfig4(scfg.DataConfig): | ||
| # Note: in a data config you have to use "__default__", so we expect | ||
| # this to fail. | ||
| default = { | ||
| 'option1': scfg.Value((1, 2, 3), tuple, alias='a'), | ||
| 'option2': 'bar', | ||
| 'option3': None, | ||
| } | ||
| config1 = Config1() | ||
| config2 = Config2() | ||
| config3 = DataConfig3() | ||
| config4 = DataConfig4() | ||
| a = config1.to_dict() | ||
| b = config2.to_dict() | ||
| c = config3.to_dict() | ||
| d = config4.to_dict() | ||
| assert a == b == c | ||
| assert a != d | ||
| config_instances = [config1, config2, config3] | ||
| import ubelt as ub | ||
| for config in config_instances: | ||
| defaults = ub.udict({ | ||
| 'self._default': config._default, | ||
| 'self.__default__': config.__default__, | ||
| 'self.default': config.default, | ||
| 'cls.__default__': config.__class__.__default__, | ||
| 'cls.default': config.__class__.default, | ||
| }) | ||
| default_ids = defaults.map_values(id) | ||
| print('default_ids = {}'.format(ub.urepr(default_ids, nl=1, align=':'))) | ||
| assert ub.allsame((default_ids - {'self._default'}).values()), ( | ||
| 'All default containers except for _default should be the same' | ||
| ) | ||
| assert default_ids['self._default'] != default_ids['self.__default__'] | ||
| def test_class_inst_normalize_attr(): | ||
| """ | ||
| The normalize and __post_init__ methods should function equivalently | ||
| """ | ||
| import scriptconfig as scfg | ||
| import ubelt as ub | ||
| import pytest | ||
| test_state = ub.ddict(lambda: 0) | ||
| config_classes = [] | ||
| common_default = { | ||
| 'opt1': scfg.Value(None, alias=['option1']), | ||
| 'opt2': scfg.Value(None, alias=['option2', 'old_name']), | ||
| } | ||
| with pytest.warns(Warning): | ||
| @config_classes.append | ||
| class Config1A(scfg.Config): | ||
| __default__ = common_default | ||
| def normalize(self): | ||
| test_state[self.__class__.__name__ + '.normalize'] += 1 | ||
| self['opt1'] = 'normalized' | ||
| @config_classes.append | ||
| class Config1B(scfg.Config): | ||
| __default__ = common_default | ||
| def __post_init__(self): | ||
| test_state[self.__class__.__name__ + '.__post_init__'] += 1 | ||
| self['opt1'] = 'post-initialized' | ||
| @config_classes.append | ||
| class Config1C(scfg.Config): | ||
| __default__ = common_default | ||
| def __post_init__(self): | ||
| test_state[self.__class__.__name__ + '.__post_init__'] += 1 | ||
| self['opt1'] = 'post-initialized' | ||
| def normalize(self): | ||
| test_state[self.__class__.__name__ + '.normalize'] += 1 | ||
| self['opt1'] = 'normalized' | ||
| with pytest.warns(Warning): | ||
| @config_classes.append | ||
| class DataConfig2A(scfg.DataConfig): | ||
| __default__ = common_default | ||
| def normalize(self): | ||
| test_state[self.__class__.__name__ + '.normalize'] += 1 | ||
| self['opt1'] = 'normalized' | ||
| @config_classes.append | ||
| class DataConfig2B(scfg.DataConfig): | ||
| __default__ = common_default | ||
| def __post_init__(self): | ||
| test_state[self.__class__.__name__ + '.__post_init__'] += 1 | ||
| self['opt1'] = 'post-initialized' | ||
| @config_classes.append | ||
| class DataConfig2C(scfg.DataConfig): | ||
| __default__ = common_default | ||
| def __post_init__(self): | ||
| test_state[self.__class__.__name__ + '.__post_init__'] += 1 | ||
| self['opt1'] = 'post-initialized' | ||
| def normalize(self): | ||
| test_state[self.__class__.__name__ + '.normalize'] += 1 | ||
| self['opt1'] = 'normalized' | ||
| instances = {} | ||
| for cls in config_classes: | ||
| instances[cls.__name__] = cls() | ||
| assert len(instances) == len(test_state) == 6 | ||
| assert all(v == 1 for v in test_state.values()), ( | ||
| 'Only normalize or __post_init__ should be called, depending on ' | ||
| 'which one is defined.' | ||
| ) | ||
| # post-init should be used over normalize when available | ||
| assert instances['Config1A']['opt1'] == 'normalized' | ||
| assert instances['Config1B']['opt1'] == 'post-initialized' | ||
| assert instances['Config1C']['opt1'] == 'post-initialized' | ||
| assert instances['DataConfig2A']['opt1'] == 'normalized' | ||
| assert instances['DataConfig2B']['opt1'] == 'post-initialized' | ||
| assert instances['DataConfig2C']['opt1'] == 'post-initialized' |
| import scriptconfig as scfg | ||
| def test_cli_dataconfig(): | ||
| class ConfigCls(scfg.DataConfig): | ||
| x: int = 0 | ||
| y: str = 3 | ||
| _test_common_cli_classmethod(ConfigCls) | ||
| def test_cli_dataconfig_with_alias(): | ||
| class ConfigCls(scfg.DataConfig): | ||
| foo = scfg.Value(0, alias=['x'], type=int) | ||
| y: str = 3 | ||
| _test_common_cli_classmethod(ConfigCls) | ||
| def test_cli_config_with_alias(): | ||
| class ConfigCls(scfg.DataConfig): | ||
| __default__ = dict( | ||
| foo=scfg.Value(0, alias=['x'], type=int), | ||
| y=3, | ||
| ) | ||
| _test_common_cli_classmethod(ConfigCls) | ||
| def test_cli_config(): | ||
| class ConfigCls(scfg.Config): | ||
| __default__ = { | ||
| 'x': 0, | ||
| 'y': 3, | ||
| } | ||
| _test_common_cli_classmethod(ConfigCls) | ||
| def _test_common_cli_classmethod(ConfigCls): | ||
| config = ConfigCls.cli(argv=[]) | ||
| assert config['x'] == 0 | ||
| config = ConfigCls.cli(argv=['--x', '3']) | ||
| assert config['x'] == 3 | ||
| config = ConfigCls.cli(argv=['--z', '3'], strict=False) | ||
| assert config['x'] == 0 | ||
| import pytest | ||
| with pytest.raises(SystemExit): | ||
| config = ConfigCls.cli(argv=['--z', '3'], strict=True) | ||
| config = ConfigCls.cli(default={'x': 4}, argv=[]) | ||
| assert config['x'] == 4 | ||
| config = ConfigCls.cli(data={'x': 4}, argv=[]) | ||
| assert config['x'] == 4 | ||
| config = ConfigCls.cli(default={'x': 4}, argv=['--x=5']) | ||
| assert config['x'] == 5 | ||
| config = ConfigCls.cli(data={'x': 4}, argv=['--x=5']) | ||
| assert config['x'] == 5 |
| def test_counter_flags(): | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| flag0 = scfg.Value(False, short_alias=['e'], isflag=True) | ||
| flag1 = scfg.Value(0, short_alias=['f'], isflag='counter') | ||
| config = MyConfig.cli(argv=[]) | ||
| assert config.flag0 is False | ||
| assert config.flag1 == 0 | ||
| config = MyConfig.cli(argv=['--flag0']) | ||
| assert config.flag0 is True | ||
| config = MyConfig.cli(argv=['-e']) | ||
| assert config.flag0 is True | ||
| config = MyConfig.cli(argv=['-f']) | ||
| assert config.flag1 == 1 | ||
| # Double specifying normal flags does nothing | ||
| config = MyConfig.cli(argv=['-e', '-e']) | ||
| assert config.flag0 is True | ||
| # Double specifying counter flags increments | ||
| config = MyConfig.cli(argv=['-f', '-f']) | ||
| assert config.flag1 == 2 | ||
| # Double specifying counter flags increments | ||
| config = MyConfig.cli(argv=['-f', '-f', '--flag1']) | ||
| assert config.flag1 == 3 | ||
| # Hard specifications overwrite the value | ||
| config = MyConfig.cli(argv=['-f', '-f', '--flag1', '--flag1=231']) | ||
| assert config.flag1 == 231 | ||
| # Hard specifications can be incremented after the fact | ||
| config = MyConfig.cli(argv=['-f', '-f', '--flag1', '--flag1=231', '-f']) | ||
| assert config.flag1 == 232 | ||
| if 0: | ||
| # TODO: Can we fix the implementation to allow for this? | ||
| # Hard specifications can be incremented after the fact | ||
| config = MyConfig.cli(argv=['-fff']) | ||
| assert config.flag1 == 3 | ||
| def port_argparse_counter_to_scriptconfig(): | ||
| """ | ||
| xdoctest ~/code/scriptconfig/tests/test_counter_flags.py port_argparse_counter_to_scriptconfig | ||
| """ | ||
| import argparse | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument('--flag1', action='count') | ||
| parser.add_argument('--flag2', action='store_true') | ||
| parser.add_argument('--flag3', action='count', help='specified looooooooooooooooooooooonggg help ') | ||
| parser.add_argument('--flag4', action='store_true', help='specified help') | ||
| import scriptconfig as scfg | ||
| text = scfg.Config.port_argparse(parser) | ||
| print(text) | ||
| import ubelt as ub | ||
| tq = '"""' | ||
| want = ub.codeblock( | ||
| """ | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ + tq + """ | ||
| $ | ||
| """ + tq + """ | ||
| flag1 = scfg.Value(None, isflag='counter', help=None) | ||
| flag2 = scfg.Value(False, isflag=True, help=None) | ||
| flag3 = scfg.Value(None, isflag='counter', help=ub.paragraph( | ||
| ''' | ||
| specified looooooooooooooooooooooonggg help | ||
| ''')) | ||
| flag4 = scfg.Value(False, isflag=True, help='specified help') | ||
| """).replace('$', '') | ||
| print(text) | ||
| print(want) | ||
| assert text == want | ||
| ns = {} | ||
| exec(text, ns, ns) | ||
| MyConfig = ns['MyConfig'] | ||
| # Note: we currently can't create argparse objects with the same flexible | ||
| # flag or key/value specification. Future work may fix this. | ||
| recon = MyConfig().port_to_argparse() | ||
| print(recon) |
| """ | ||
| The difference between data and default is that default will update the | ||
| defaults and persist between multiple load operations whereas data will | ||
| only set the immediate values and not persist over multiple loads. | ||
| """ | ||
| import scriptconfig as scfg | ||
| import pytest | ||
| def generate_dataconfig_instance_variants(): | ||
| # In its simplest incarnation, the config class specifies default values. | ||
| # For each configuration parameter. | ||
| class ExampleConfig1(scfg.DataConfig): | ||
| num = 1 | ||
| mode = 'bar' | ||
| ignore = ['baz', 'biz'] | ||
| # Test with data configs | ||
| config = ExampleConfig1() | ||
| yield config, 'dataconfig' | ||
| class ExampleConfig2(scfg.Config): | ||
| __default__ = dict( | ||
| num=1, | ||
| mode='bar', | ||
| ignore=['baz', 'biz'], | ||
| ) | ||
| # Test with original configs | ||
| config = ExampleConfig2() | ||
| yield config, 'orig-dunder-default' | ||
| with pytest.warns(Warning): | ||
| class ExampleConfig3(scfg.Config): | ||
| default = dict( | ||
| num=1, | ||
| mode='bar', | ||
| ignore=['baz', 'biz'], | ||
| ) | ||
| config = ExampleConfig3() | ||
| yield config, 'orig-default' | ||
| @pytest.mark.parametrize('config, test_name', generate_dataconfig_instance_variants()) | ||
| def test_data_vs_default(config, test_name): | ||
| assert config['num'] == 1 | ||
| # Using load(data=kwargs) unions kwargs with the existing defaults | ||
| # but does not change the defaults | ||
| kwargs = {'num': 2} | ||
| config = config.load(data=kwargs) | ||
| assert config['num'] == 2 | ||
| assert config['mode'] == 'bar' | ||
| # Calling load again will reset any unspecified params to the defaults | ||
| kwargs = {'mode': 'foo'} | ||
| config = config.load(data=kwargs) | ||
| assert config['num'] == 1 | ||
| assert config['mode'] == 'foo' | ||
| # Using load(default=kwargs) changes the defaults and then does the same | ||
| # process. | ||
| kwargs = {'num': 2} | ||
| config = config.load(default=kwargs) | ||
| assert config['num'] == 2 | ||
| assert config['mode'] == 'bar' | ||
| # Calling again will show the defaults are changed | ||
| kwargs = {'mode': 'foo'} | ||
| config = config.load(default=kwargs) | ||
| assert config['num'] == 2 | ||
| assert config['mode'] == 'foo' | ||
| config = config.load() | ||
| assert config['num'] == 2 | ||
| assert config['mode'] == 'foo' | ||
| # Test that cmdline will overload a default | ||
| config = config.load(cmdline='--num=3', default={'num': 4}) | ||
| assert config['num'] == 3 | ||
| # But the new default should persist | ||
| config = config.load() | ||
| assert config['num'] == 4 | ||
| # Test that data will overload a default | ||
| config = config.load(data=dict(num=10), default={'num': 5}) | ||
| assert config['num'] == 10 | ||
| # But the new default should persist | ||
| config = config.load() | ||
| assert config['num'] == 5 |
| import scriptconfig as scfg | ||
| def test_dataconfig_setattr_simple(): | ||
| import pytest | ||
| class ExampleDataConfig(scfg.DataConfig): | ||
| x: int = 0 | ||
| y: str = 3 | ||
| self = ExampleDataConfig() | ||
| print(f'self.__dict__={self.__dict__}') | ||
| print(f'self.x={self.x}') | ||
| new_val = 432 | ||
| self['x'] = new_val | ||
| assert 'x' not in self.__dict__ | ||
| assert self['x'] == new_val | ||
| assert self.x == new_val | ||
| new_val = 433 | ||
| self.x = new_val | ||
| assert 'x' not in self.__dict__ | ||
| assert self['x'] == new_val | ||
| assert self.x == new_val | ||
| new_val = 434 | ||
| self['x'] = new_val | ||
| assert 'x' not in self.__dict__ | ||
| assert self['x'] == new_val | ||
| assert self.x == new_val | ||
| new_val = 435 | ||
| self.x = new_val | ||
| assert 'x' not in self.__dict__ | ||
| assert self['x'] == new_val | ||
| assert self.x == new_val | ||
| # self.notakey | ||
| with pytest.raises(AttributeError): | ||
| self.notakey | ||
| self.notakey = 100 | ||
| assert 'notakey' not in self | ||
| assert 'notakey' in self.__dict__ | ||
| with pytest.raises(KeyError): | ||
| self['notakey'] | ||
| assert self.notakey == 100 | ||
| def test_dataconfig_setattr_combos(): | ||
| class ExampleDataConfig(scfg.DataConfig): | ||
| x: int = 0 | ||
| y: str = 3 | ||
| self = ExampleDataConfig() | ||
| def setmethod_item(self, key, value): | ||
| # Test setting the value by using __setitem__ | ||
| self[key] = value | ||
| def setmethod_attr(self, key, value): | ||
| # Test setting the value by using __setattr__ | ||
| setattr(self, key, value) | ||
| def getmethod_item(self, key): | ||
| return self[key] | ||
| def getmethod_attr(self, key): | ||
| return getattr(self, key) | ||
| import ubelt as ub | ||
| import itertools as it | ||
| grid = list(ub.named_product({ | ||
| 'key': ['x'], | ||
| 'setmethod': [setmethod_item, setmethod_attr], | ||
| 'getmethod': [getmethod_item, getmethod_attr], | ||
| })) | ||
| tasks = list(ub.flatten(it.permutations(grid, len(grid)))) | ||
| for new_value, task in enumerate(tasks, start=101): | ||
| task['new_value'] = new_value | ||
| for task in tasks: | ||
| key = task['key'] | ||
| setmethod = task['setmethod'] | ||
| getmethod = task['getmethod'] | ||
| new_val = task['key'] | ||
| old_val = getmethod(self, key) | ||
| assert new_val != old_val | ||
| assert key in self | ||
| assert key not in self.__dict__ | ||
| setmethod(self, key, new_value) | ||
| assert getmethod(self, key) == new_value | ||
| assert key in self | ||
| assert key not in self.__dict__ | ||
| def test_dataconfig_warning(): | ||
| """ | ||
| Test that the user gets a warning if they make this common mistake | ||
| """ | ||
| import scriptconfig as scfg | ||
| import pytest | ||
| with pytest.warns(Warning): | ||
| class ExampleDataConfig(scfg.DataConfig): | ||
| x = scfg.Value(None), | ||
| def test_dataconfig_with_funcs(): | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| __default__ = { | ||
| 'a': 1, | ||
| 'b': 1, | ||
| } | ||
| def c(self): | ||
| ... | ||
| @staticmethod | ||
| def d(): | ||
| ... | ||
| @classmethod | ||
| def e(cls): | ||
| ... | ||
| f = lambda x: None # NOQA | ||
| assert callable(MyConfig.c) | ||
| assert callable(MyConfig.f) | ||
| assert callable(MyConfig.e) | ||
| assert callable(MyConfig.d) | ||
| assert not hasattr(MyConfig, 'a') | ||
| assert not hasattr(MyConfig, 'b') | ||
| assert 'e' not in MyConfig.__default__ | ||
| def test_dataconfig_docstring(): | ||
| import scriptconfig as scfg | ||
| class MyConfig1(scfg.DataConfig): | ||
| ... | ||
| class MyConfig2(scfg.DataConfig): | ||
| """ | ||
| Hello World | ||
| """ | ||
| ... | ||
| assert MyConfig1.__description__ is None | ||
| self1 = MyConfig1() | ||
| self2 = MyConfig2() | ||
| assert 'generated by scriptconfig' in self1._description | ||
| assert self2._description == 'Hello World' |
| import scriptconfig as scfg | ||
| class DemoConfig(scfg.Config): | ||
| __default__ = { | ||
| 'num': 1, | ||
| 'mode': 'bar', | ||
| 'mode2': scfg.Value('bar', str), | ||
| 'ignore': ['baz', 'biz'], | ||
| } | ||
| def test_cast_set(): | ||
| config = DemoConfig() | ||
| keys = set(config) | ||
| assert keys == set(config.keys()) |
| def test_import(): | ||
| import scriptconfig |
| def test_inheritence(): | ||
| from scriptconfig import DataConfig | ||
| import ubelt as ub | ||
| class Config1(DataConfig): | ||
| arg1 = 1 | ||
| arg2 = 2 | ||
| arg3 = 3 | ||
| class Config2(Config1): | ||
| arg4 = 4 | ||
| arg5 = 5 | ||
| arg6 = 6 | ||
| class Config3(Config2): | ||
| arg2 = 22 | ||
| arg3 = 33 | ||
| arg5 = 55 | ||
| c1 = Config1() | ||
| c2 = Config2() | ||
| c3 = Config3() | ||
| text1 = ('c1 = {}'.format(ub.urepr(c1, nl=1))) | ||
| text2 = ('c2 = {}'.format(ub.urepr(c2, nl=1))) | ||
| text3 = ('c3 = {}'.format(ub.urepr(c3, nl=1))) | ||
| print(text1) | ||
| print(text2) | ||
| print(text3) | ||
| assert text1 == ub.codeblock( | ||
| ''' | ||
| c1 = Config1(**{ | ||
| 'arg1': 1, | ||
| 'arg2': 2, | ||
| 'arg3': 3, | ||
| }) | ||
| ''') | ||
| assert text2 == ub.codeblock( | ||
| ''' | ||
| c2 = Config2(**{ | ||
| 'arg1': 1, | ||
| 'arg2': 2, | ||
| 'arg3': 3, | ||
| 'arg4': 4, | ||
| 'arg5': 5, | ||
| 'arg6': 6, | ||
| }) | ||
| ''') | ||
| assert text3 == ub.codeblock( | ||
| ''' | ||
| c3 = Config3(**{ | ||
| 'arg1': 1, | ||
| 'arg2': 22, | ||
| 'arg3': 33, | ||
| 'arg4': 4, | ||
| 'arg5': 55, | ||
| 'arg6': 6, | ||
| }) | ||
| ''') | ||
| def test_multiple_inheritence(): | ||
| from scriptconfig import DataConfig | ||
| class Fooable(DataConfig): | ||
| foo_arg1 = 1 | ||
| foo_arg2 = 2 | ||
| foobarg1 = 3 | ||
| foobarg2 = 4 | ||
| class Barable(DataConfig): | ||
| bar_arg1 = 'a' | ||
| bar_arg2 = 'b' | ||
| foobarg1 = 'c' | ||
| foobarg2 = 'd' | ||
| class Foobarable(Fooable, Barable): | ||
| foo_arg2 = ... | ||
| bar_arg2 = ... | ||
| foobarg2 = ... | ||
| config = Foobarable() | ||
| import ubelt as ub | ||
| text = ub.urepr(config, nl=1) | ||
| print(text) | ||
| assert text == ub.codeblock( | ||
| ''' | ||
| Foobarable(**{ | ||
| 'foo_arg1': 1, | ||
| 'foo_arg2': Ellipsis, | ||
| 'foobarg1': 'c', | ||
| 'foobarg2': Ellipsis, | ||
| 'bar_arg1': 'a', | ||
| 'bar_arg2': Ellipsis, | ||
| }) | ||
| ''') | ||
| def test_multiple_inheritence_diag(): | ||
| from scriptconfig import DataConfig | ||
| class Base(DataConfig): | ||
| base_arg1 = 'B1' | ||
| base_arg2 = 'B2' | ||
| base_arg3 = 'B3' | ||
| base_arg4 = 'B4' | ||
| class Left(Base): | ||
| left_arg1 = 'L1' | ||
| left_arg2 = 'L2' | ||
| base_arg2 = 'L_B2' | ||
| class Right(Base): | ||
| right_arg1 = 'R1' | ||
| right_arg2 = 'R2' | ||
| base_arg3 = 'R_B3' | ||
| class Joined(Left, Right): | ||
| left_arg2 = 'J1' | ||
| right_arg2 = 'J2' | ||
| base_arg4 = 'J3' | ||
| config = Joined() | ||
| import ubelt as ub | ||
| text = ub.urepr(config, nl=1) | ||
| print(text) | ||
| assert text == ub.codeblock( | ||
| ''' | ||
| Joined(**{ | ||
| 'base_arg1': 'B1', | ||
| 'base_arg2': 'B2', | ||
| 'base_arg3': 'R_B3', | ||
| 'base_arg4': 'J3', | ||
| 'left_arg1': 'L1', | ||
| 'left_arg2': 'J1', | ||
| 'right_arg1': 'R1', | ||
| 'right_arg2': 'J2', | ||
| }) | ||
| ''') |
| import ubelt as ub | ||
| def test_list_parsing(): | ||
| """ | ||
| References: | ||
| .. [1] https://stackoverflow.com/questions/15753701/how-can-i-pass-a-list-as-a-command-line-argument-with-argparse | ||
| .. [2] https://www.gnu.org/prep/standards/html_node/Command_002dLine-Interfaces.html | ||
| .. [3] http://www.catb.org/~esr/writings/taoup/html/ch10s05.html | ||
| .. [4] https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html | ||
| https://stackoverflow.com/questions/8957222/are-there-standards-for-linux-command-line-switches-and-arguments | ||
| https://www.gnu.org/software/libc/manual/html_node/Getopt-Long-Options.html | ||
| Ideal Spec: | ||
| The input is a list of lexed argv tokens: e.g. sys.argv[1:] | ||
| For boolean flag options `--{name}` is equivalent to `--{name}=True` | ||
| A single valued argument should be able to be specified as either | ||
| `--{name} {value}` OR `--{name}={value}` | ||
| A multi-valued argument should be specifiable as | ||
| `--{name} {parsable_delimited_values}` OR | ||
| `--{name} {value1} {value2} ... {valueN}` OR | ||
| `--{name}={parsable_delimited_values}` OR | ||
| Note: the above may have ambuguities and we need to look into that | ||
| and potentiall restrict the spec. | ||
| The following is NOT acceptable due because it would cause ambiguities. | ||
| `--{name}={value1} {value2} ... {valueN}` | ||
| Positional arguments can occur: | ||
| (1) at the beginning of the lexed-argv tokens, | ||
| (2) after any single-valued argument | ||
| Positional cannot arguments can occur: | ||
| (3) after a multi-valued space delimited argument. | ||
| TODO: | ||
| - [ ] There is almost no situation where the argparse type should be | ||
| able to be passed as "list", See[1]. Therefore if a Value has a type | ||
| of "list" we should do special handling of it. | ||
| - [ ] A list Value should be able to accept either multiple traditional | ||
| nargs space-separated values, or a comma delimited list of values. | ||
| """ | ||
| import scriptconfig as scfg | ||
| # FIXME: We need make parsing lists a bit more intuitive | ||
| # FIXME: Parsing lists is currently very fragile | ||
| class ExampleConfig(scfg.DataConfig): | ||
| __default__ = { | ||
| 'item1': [], | ||
| 'item2': scfg.Value([], type=list), | ||
| 'item3': scfg.Value([]), | ||
| 'item4': scfg.Value([], nargs='*'), | ||
| } | ||
| config = ExampleConfig() | ||
| print('config._default = {}'.format(ub.urepr(config._default, nl=1))) | ||
| print('config._data = {}'.format(ub.urepr(config._data, nl=1))) | ||
| parser = config.argparse() | ||
| print('parser._actions = {}'.format(ub.urepr(parser._actions, nl=1))) | ||
| # IDEALLY BOTH CASES SHOULD WORK | ||
| config.load(cmdline=[ | ||
| '--item1', 'spam', 'eggs', | ||
| '--item2', 'spam', 'eggs', | ||
| '--item3', 'spam', 'eggs', | ||
| '--item4', 'spam', 'eggs', | ||
| ]) | ||
| print('loaded = ' + ub.urepr(config.asdict(), nl=1)) | ||
| # ub.map_vals(len, config) | ||
| config.load(cmdline=[ | ||
| '--item1=spam,eggs', | ||
| '--item2=spam,eggs', | ||
| '--item3=spam,eggs', | ||
| '--item4=spam,eggs', | ||
| ]) | ||
| print('loaded = ' + ub.urepr(config.asdict(), nl=1)) |
| def test_paths_with_commas(): | ||
| from scriptconfig.value import Value, Path | ||
| self = Value('key') | ||
| self.update('/path/with,commas') | ||
| print('self.value = {!r}'.format(self.value)) | ||
| assert isinstance(self.value, list), 'without specifying types a string with commas will be smartcast' | ||
| self = Value('key', type=str) | ||
| self.update('/path/with,commas') | ||
| print('self.value = {!r}'.format(self.value)) | ||
| assert isinstance(self.value, str), 'specifying a type should prevent smartcast' | ||
| self = Path('key') | ||
| self.update('/path/with,commas') | ||
| print('self.value = {!r}'.format(self.value)) | ||
| assert isinstance(self.value, str), 'specifying a type should prevent smartcast' | ||
| def test_paths_with_commas_in_config(): | ||
| import scriptconfig as scfg | ||
| class TestConfig(scfg.Config): | ||
| __default__ = { | ||
| 'key': scfg.Value(None, type=str), | ||
| } | ||
| kw = { | ||
| 'key': '/path/with,commas', | ||
| } | ||
| config = TestConfig(default=kw, cmdline=False) | ||
| print(config['key']) | ||
| assert isinstance(config['key'], str), 'specifying a type should prevent smartcast' | ||
| # In the past setting cmdline=True did cause an error | ||
| config = TestConfig(default=kw, cmdline=True) | ||
| print(config['key']) | ||
| assert isinstance(config['key'], str), 'specifying a type should prevent smartcast' | ||
| def test_globstr_with_nargs(): | ||
| from os.path import join | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| dpath = ub.Path.appdir('scriptconfig', 'tests', 'files').ensuredir() | ||
| ub.touch(join(dpath, 'file1.txt')) | ||
| ub.touch(join(dpath, 'file2.txt')) | ||
| ub.touch(join(dpath, 'file3.txt')) | ||
| class TestConfig(scfg.Config): | ||
| __default__ = { | ||
| 'paths': scfg.Value(None, nargs='+'), | ||
| } | ||
| cmdline = '--paths {dpath}/*'.format(dpath=dpath) | ||
| config = TestConfig(cmdline=cmdline) | ||
| # ub.cmd(f'echo {dpath}/*', shell=True) | ||
| import glob | ||
| cmdline = '--paths ' + ' '.join(list(glob.glob(join(dpath, '*')))) | ||
| config = TestConfig(cmdline=cmdline) | ||
| cmdline = '--paths=' + ','.join(list(glob.glob(join(dpath, '*')))) | ||
| config = TestConfig(cmdline=cmdline) # NOQA |
| import scriptconfig as scfg | ||
| # Using inheritance and the decorator lets you pickle the object | ||
| # verify legacy configs are pickleable | ||
| #@dataconf | ||
| class Legacy(scfg.Config): | ||
| __default__ = { | ||
| 'default': scfg.Value((256, 256), help='chip size'), | ||
| 'keys0': [1, 2, 3], | ||
| '__default__': {'argparse': 3.3, 'keys0': [4, 5]}, | ||
| 'time_sampling': scfg.Value('soft2'), | ||
| } | ||
| def test_legacy_pickle(): | ||
| print(f'Legacy.__module__={Legacy.__module__}') | ||
| config = Legacy() | ||
| import pickle | ||
| serial = pickle.dumps(config) | ||
| recon = pickle.loads(serial) | ||
| assert recon.to_dict() == config.to_dict() | ||
| assert 'locals' not in str(Legacy) | ||
| assert 'recon' not in str(Legacy) | ||
| class SimpleData(scfg.DataConfig): | ||
| __default__ = { | ||
| 'a': scfg.Value((256, 256), help='chip size'), | ||
| 'b': [1, 2, 3], | ||
| 'c': {'argparse': 3.3, 'keys0': [4, 5]}, | ||
| 'd': scfg.Value('soft2'), | ||
| } | ||
| def test_pickle2(): | ||
| config = SimpleData() | ||
| import pickle | ||
| serial = pickle.dumps(config) | ||
| recon = pickle.loads(serial) | ||
| assert recon.to_dict() == config.to_dict() | ||
| assert 'locals' not in str(SimpleData) | ||
| assert 'recon' not in str(SimpleData) | ||
| # With extra | ||
| @scfg.dataconf | ||
| class DecorDataConf1(scfg.DataConfig): | ||
| __default__ = { | ||
| 'a': scfg.Value((256, 256), help='chip size'), | ||
| 'b': [1, 2, 3], | ||
| 'c': {'argparse': 3.3, 'keys0': [4, 5]}, | ||
| 'd': scfg.Value('soft2'), | ||
| } | ||
| def test_pickle3(): | ||
| config = DecorDataConf1() | ||
| import pickle | ||
| serial = pickle.dumps(config) | ||
| recon = pickle.loads(serial) | ||
| assert recon.to_dict() == config.to_dict() | ||
| assert 'locals' not in str(DecorDataConf1) | ||
| assert 'recon' not in str(DecorDataConf1) | ||
| class DecorDataConf2_(scfg.DataConfig): | ||
| a = scfg.Value((256, 256), help='chip size') | ||
| b = [1, 2, 3] | ||
| c = {'argparse': 3.3, 'keys0': [4, 5]} | ||
| d = scfg.Value('soft2') | ||
| def test_pickle4(): | ||
| DecorDataConf2 = scfg.dataconf(DecorDataConf2_) | ||
| print(sorted(vars())) | ||
| print(f'__name__={__name__}') | ||
| print(f'__package__={__package__}') | ||
| print(f'__module__={__name__}') | ||
| print(f'DecorDataConf2_.__module__={DecorDataConf2_.__module__}') | ||
| print(f'DecorDataConf2.__module__={DecorDataConf2.__module__}') | ||
| config = DecorDataConf2() | ||
| import pickle | ||
| serial = pickle.dumps(config) | ||
| recon = pickle.loads(serial) | ||
| assert recon.to_dict() == config.to_dict() | ||
| assert 'locals' not in str(DecorDataConf2) | ||
| assert 'recon' not in str(DecorDataConf2) |
| def test_post_init_not_called_twice(): | ||
| """ | ||
| xdoctest ~/code/scriptconfig/tests/test_post_init.py test_post_init_not_called_twice | ||
| """ | ||
| import scriptconfig as scfg | ||
| import ubelt as ub | ||
| default = { | ||
| 'option1': scfg.Value((1, 2, 3), type=tuple, alias='a'), | ||
| 'option2': 'bar', | ||
| 'option3': None, | ||
| } | ||
| def postinit(self): | ||
| print('Call PostInit For: self = {}, id={}'.format(ub.urepr(self, nl=1), id(self))) | ||
| # import traceback | ||
| # import sys | ||
| # traceback.print_stack(file=sys.stdout) | ||
| if not hasattr(self, '_post_init_count'): | ||
| self._post_init_count = 0 | ||
| self._post_init_count += 1 | ||
| class MyConfig(scfg.Config): | ||
| __default__ = default | ||
| __post_init__ = postinit | ||
| class MyDataConfig(scfg.DataConfig): | ||
| __default__ = default | ||
| __post_init__ = postinit | ||
| # Single initialization worked correctly in 0.7.10 | ||
| print('-- CONFIG 1 ---') | ||
| config1 = MyDataConfig() | ||
| assert config1._post_init_count == 1 | ||
| print('-- CONFIG 2 ---') | ||
| config2 = MyConfig() | ||
| assert config2._post_init_count == 1 | ||
| # However, in 0.7.10 calling cli caused a double call to __post_init__ | ||
| # Because it initializes the object and then calls load. | ||
| print('-- CONFIG 3 ---') | ||
| config3 = MyDataConfig.cli(argv=[]) | ||
| assert config3._post_init_count == 1 | ||
| print('-- CONFIG 4 ---') | ||
| config4 = MyConfig.cli(argv=[]) | ||
| assert config4._post_init_count == 1 | ||
| # We do expect the load method to call post init a second time if the user | ||
| # calls it, but internally we should prevent it. | ||
| config4.load(data={}) | ||
| assert config4._post_init_count == 2 | ||
| config3.load(data={}) | ||
| assert config4._post_init_count == 2 |
| def test_without_special_options(): | ||
| """ | ||
| The "special options" of "config", "dump", and "dumps" are useful but they | ||
| prevent the user from being able to use them as official config args. | ||
| """ | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| config = None | ||
| # Without using the ``cli`` classmethod there should be no issue with using | ||
| # these "special options". | ||
| config = MyConfig() | ||
| assert config.config is None | ||
| # But, if special options are enabled we cannot have options that conflict | ||
| import pytest | ||
| with pytest.raises(Exception): | ||
| config = MyConfig.cli(argv=['--config=foo'], special_options=True) | ||
| # But setting special_options=False will allow for this | ||
| config = MyConfig.cli(argv=['--config=foo'], special_options=False) | ||
| assert config.config == 'foo' |
| def test_scriptconfig_repr(): | ||
| import scriptconfig as scfg | ||
| import ubelt as ub | ||
| class MyConfig(scfg.DataConfig): | ||
| arg1 = 1 | ||
| arg2 = 2 | ||
| c = MyConfig() | ||
| text = ub.urepr(c, nl=1) | ||
| assert text == ub.codeblock( | ||
| ''' | ||
| MyConfig(**{ | ||
| 'arg1': 1, | ||
| 'arg2': 2, | ||
| }) | ||
| ''') | ||
| class MyConfig(scfg.Config): | ||
| __default__ = dict( | ||
| arg1=1, | ||
| arg2=2, | ||
| ) | ||
| c = MyConfig() | ||
| text = ub.urepr(c, nl=1) | ||
| assert text == ub.codeblock( | ||
| ''' | ||
| MyConfig({ | ||
| 'arg1': 1, | ||
| 'arg2': 2, | ||
| }) | ||
| ''') |
@@ -145,3 +145,3 @@ """ | ||
| See the :mod:`scriptconfig.config` module docs for details and examples on | ||
| getting started as well as :doc:`getting_started docs <getting_started>` | ||
| getting started as well as :doc:`getting_started docs <manual/getting_started>` | ||
| """ | ||
@@ -155,3 +155,3 @@ | ||
| __version__ = '0.7.15' | ||
| __version__ = '0.8.0' | ||
@@ -158,0 +158,0 @@ __submodules__ = { |
@@ -54,2 +54,4 @@ """ | ||
| .. code:: | ||
| --flag > {'flag': True} | ||
@@ -56,0 +58,0 @@ --flag=1 > {'flag': True} |
| Apache License | ||
| Version 2.0, January 2004 | ||
| http://www.apache.org/licenses/ | ||
| TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION | ||
| 1. Definitions. | ||
| "License" shall mean the terms and conditions for use, reproduction, | ||
| and distribution as defined by Sections 1 through 9 of this document. | ||
| "Licensor" shall mean the copyright owner or entity authorized by | ||
| the copyright owner that is granting the License. | ||
| "Legal Entity" shall mean the union of the acting entity and all | ||
| other entities that control, are controlled by, or are under common | ||
| control with that entity. For the purposes of this definition, | ||
| "control" means (i) the power, direct or indirect, to cause the | ||
| direction or management of such entity, whether by contract or | ||
| otherwise, or (ii) ownership of fifty percent (50%) or more of the | ||
| outstanding shares, or (iii) beneficial ownership of such entity. | ||
| "You" (or "Your") shall mean an individual or Legal Entity | ||
| exercising permissions granted by this License. | ||
| "Source" form shall mean the preferred form for making modifications, | ||
| including but not limited to software source code, documentation | ||
| source, and configuration files. | ||
| "Object" form shall mean any form resulting from mechanical | ||
| transformation or translation of a Source form, including but | ||
| not limited to compiled object code, generated documentation, | ||
| and conversions to other media types. | ||
| "Work" shall mean the work of authorship, whether in Source or | ||
| Object form, made available under the License, as indicated by a | ||
| copyright notice that is included in or attached to the work | ||
| (an example is provided in the Appendix below). | ||
| "Derivative Works" shall mean any work, whether in Source or Object | ||
| form, that is based on (or derived from) the Work and for which the | ||
| editorial revisions, annotations, elaborations, or other modifications | ||
| represent, as a whole, an original work of authorship. For the purposes | ||
| of this License, Derivative Works shall not include works that remain | ||
| separable from, or merely link (or bind by name) to the interfaces of, | ||
| the Work and Derivative Works thereof. | ||
| "Contribution" shall mean any work of authorship, including | ||
| the original version of the Work and any modifications or additions | ||
| to that Work or Derivative Works thereof, that is intentionally | ||
| submitted to Licensor for inclusion in the Work by the copyright owner | ||
| or by an individual or Legal Entity authorized to submit on behalf of | ||
| the copyright owner. For the purposes of this definition, "submitted" | ||
| means any form of electronic, verbal, or written communication sent | ||
| to the Licensor or its representatives, including but not limited to | ||
| communication on electronic mailing lists, source code control systems, | ||
| and issue tracking systems that are managed by, or on behalf of, the | ||
| Licensor for the purpose of discussing and improving the Work, but | ||
| excluding communication that is conspicuously marked or otherwise | ||
| designated in writing by the copyright owner as "Not a Contribution." | ||
| "Contributor" shall mean Licensor and any individual or Legal Entity | ||
| on behalf of whom a Contribution has been received by Licensor and | ||
| subsequently incorporated within the Work. | ||
| 2. Grant of Copyright License. Subject to the terms and conditions of | ||
| this License, each Contributor hereby grants to You a perpetual, | ||
| worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
| copyright license to reproduce, prepare Derivative Works of, | ||
| publicly display, publicly perform, sublicense, and distribute the | ||
| Work and such Derivative Works in Source or Object form. | ||
| 3. Grant of Patent License. Subject to the terms and conditions of | ||
| this License, each Contributor hereby grants to You a perpetual, | ||
| worldwide, non-exclusive, no-charge, royalty-free, irrevocable | ||
| (except as stated in this section) patent license to make, have made, | ||
| use, offer to sell, sell, import, and otherwise transfer the Work, | ||
| where such license applies only to those patent claims licensable | ||
| by such Contributor that are necessarily infringed by their | ||
| Contribution(s) alone or by combination of their Contribution(s) | ||
| with the Work to which such Contribution(s) was submitted. If You | ||
| institute patent litigation against any entity (including a | ||
| cross-claim or counterclaim in a lawsuit) alleging that the Work | ||
| or a Contribution incorporated within the Work constitutes direct | ||
| or contributory patent infringement, then any patent licenses | ||
| granted to You under this License for that Work shall terminate | ||
| as of the date such litigation is filed. | ||
| 4. Redistribution. You may reproduce and distribute copies of the | ||
| Work or Derivative Works thereof in any medium, with or without | ||
| modifications, and in Source or Object form, provided that You | ||
| meet the following conditions: | ||
| (a) You must give any other recipients of the Work or | ||
| Derivative Works a copy of this License; and | ||
| (b) You must cause any modified files to carry prominent notices | ||
| stating that You changed the files; and | ||
| (c) You must retain, in the Source form of any Derivative Works | ||
| that You distribute, all copyright, patent, trademark, and | ||
| attribution notices from the Source form of the Work, | ||
| excluding those notices that do not pertain to any part of | ||
| the Derivative Works; and | ||
| (d) If the Work includes a "NOTICE" text file as part of its | ||
| distribution, then any Derivative Works that You distribute must | ||
| include a readable copy of the attribution notices contained | ||
| within such NOTICE file, excluding those notices that do not | ||
| pertain to any part of the Derivative Works, in at least one | ||
| of the following places: within a NOTICE text file distributed | ||
| as part of the Derivative Works; within the Source form or | ||
| documentation, if provided along with the Derivative Works; or, | ||
| within a display generated by the Derivative Works, if and | ||
| wherever such third-party notices normally appear. The contents | ||
| of the NOTICE file are for informational purposes only and | ||
| do not modify the License. You may add Your own attribution | ||
| notices within Derivative Works that You distribute, alongside | ||
| or as an addendum to the NOTICE text from the Work, provided | ||
| that such additional attribution notices cannot be construed | ||
| as modifying the License. | ||
| You may add Your own copyright statement to Your modifications and | ||
| may provide additional or different license terms and conditions | ||
| for use, reproduction, or distribution of Your modifications, or | ||
| for any such Derivative Works as a whole, provided Your use, | ||
| reproduction, and distribution of the Work otherwise complies with | ||
| the conditions stated in this License. | ||
| 5. Submission of Contributions. Unless You explicitly state otherwise, | ||
| any Contribution intentionally submitted for inclusion in the Work | ||
| by You to the Licensor shall be under the terms and conditions of | ||
| this License, without any additional terms or conditions. | ||
| Notwithstanding the above, nothing herein shall supersede or modify | ||
| the terms of any separate license agreement you may have executed | ||
| with Licensor regarding such Contributions. | ||
| 6. Trademarks. This License does not grant permission to use the trade | ||
| names, trademarks, service marks, or product names of the Licensor, | ||
| except as required for reasonable and customary use in describing the | ||
| origin of the Work and reproducing the content of the NOTICE file. | ||
| 7. Disclaimer of Warranty. Unless required by applicable law or | ||
| agreed to in writing, Licensor provides the Work (and each | ||
| Contributor provides its Contributions) on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| implied, including, without limitation, any warranties or conditions | ||
| of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A | ||
| PARTICULAR PURPOSE. You are solely responsible for determining the | ||
| appropriateness of using or redistributing the Work and assume any | ||
| risks associated with Your exercise of permissions under this License. | ||
| 8. Limitation of Liability. In no event and under no legal theory, | ||
| whether in tort (including negligence), contract, or otherwise, | ||
| unless required by applicable law (such as deliberate and grossly | ||
| negligent acts) or agreed to in writing, shall any Contributor be | ||
| liable to You for damages, including any direct, indirect, special, | ||
| incidental, or consequential damages of any character arising as a | ||
| result of this License or out of the use or inability to use the | ||
| Work (including but not limited to damages for loss of goodwill, | ||
| work stoppage, computer failure or malfunction, or any and all | ||
| other commercial damages or losses), even if such Contributor | ||
| has been advised of the possibility of such damages. | ||
| 9. Accepting Warranty or Additional Liability. While redistributing | ||
| the Work or Derivative Works thereof, You may choose to offer, | ||
| and charge a fee for, acceptance of support, warranty, indemnity, | ||
| or other liability obligations and/or rights consistent with this | ||
| License. However, in accepting such obligations, You may act only | ||
| on Your own behalf and on Your sole responsibility, not on behalf | ||
| of any other Contributor, and only if You agree to indemnify, | ||
| defend, and hold each Contributor harmless for any liability | ||
| incurred by, or claims asserted against, such Contributor by reason | ||
| of your accepting any such warranty or additional liability. | ||
| END OF TERMS AND CONDITIONS | ||
| APPENDIX: How to apply the Apache License to your work. | ||
| To apply the Apache License to your work, attach the following | ||
| boilerplate notice, with the fields enclosed by brackets "{}" | ||
| replaced with your own identifying information. (Don't include | ||
| the brackets!) The text should be enclosed in the appropriate | ||
| comment syntax for the file format. We also recommend that a | ||
| file or class name and description of purpose be included on the | ||
| same "printed page" as the copyright notice for easier | ||
| identification within third-party archives. | ||
| Copyright 2022 "Kitware 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. |
| Metadata-Version: 2.1 | ||
| Name: scriptconfig | ||
| Version: 0.7.15 | ||
| Summary: Easy dict-based script configuration with CLI support | ||
| Home-page: https://gitlab.kitware.com/utils/scriptconfig/ | ||
| Author: Kitware Inc., Jon Crall | ||
| Author-email: kitware@kitware.com, jon.crall@kitware.com | ||
| License: Apache 2 | ||
| Classifier: Development Status :: 4 - Beta | ||
| Classifier: Intended Audience :: Developers | ||
| Classifier: Topic :: Software Development :: Libraries :: Python Modules | ||
| Classifier: Topic :: Utilities | ||
| Classifier: License :: OSI Approved :: Apache Software License | ||
| Classifier: Programming Language :: Python :: 3.6 | ||
| Classifier: Programming Language :: Python :: 3.7 | ||
| Classifier: Programming Language :: Python :: 3.8 | ||
| Classifier: Programming Language :: Python :: 3.9 | ||
| Classifier: Programming Language :: Python :: 3.10 | ||
| Classifier: Programming Language :: Python :: 3.11 | ||
| Requires-Python: >=3.6 | ||
| Description-Content-Type: text/x-rst | ||
| License-File: LICENSE | ||
| Requires-Dist: ubelt >=1.2.3 | ||
| Requires-Dist: PyYAML >=5.4.1 ; python_version < "3.10" and python_version >= "3.9" | ||
| Requires-Dist: PyYAML >=6.0 ; python_version < "3.11" and python_version >= "3.10" | ||
| Requires-Dist: PyYAML >=6.0 ; python_version < "3.12" and python_version >= "3.11" | ||
| Requires-Dist: PyYAML >=5.4.1 ; python_version < "3.7" and python_version >= "3.6" | ||
| Requires-Dist: PyYAML >=5.4.1 ; python_version < "3.8" and python_version >= "3.7" | ||
| Requires-Dist: PyYAML >=5.4.1 ; python_version < "3.9" and python_version >= "3.8" | ||
| Requires-Dist: PyYAML >=6.0.1 ; python_version < "4.0" and python_version >= "3.12" | ||
| Provides-Extra: all | ||
| Requires-Dist: ubelt >=1.2.3 ; extra == 'all' | ||
| Requires-Dist: xdoctest >=1.1.3 ; extra == 'all' | ||
| Requires-Dist: argcomplete >=3.0.5 ; extra == 'all' | ||
| Provides-Extra: all-strict | ||
| Requires-Dist: ubelt ==1.2.3 ; extra == 'all-strict' | ||
| Requires-Dist: xdoctest ==1.1.3 ; extra == 'all-strict' | ||
| Requires-Dist: argcomplete ==3.0.5 ; extra == 'all-strict' | ||
| Requires-Dist: coverage ==4.5 ; (python_version < "2.7" and python_version >= "2.6") and extra == 'all-strict' | ||
| Requires-Dist: pytest <=4.6.11,==4.6.0 ; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == 'all-strict' | ||
| Requires-Dist: pytest-cov ==2.8.1 ; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == 'all-strict' | ||
| Requires-Dist: PyYAML ==5.4.1 ; (python_version < "3.10" and python_version >= "3.9") and extra == 'all-strict' | ||
| Requires-Dist: coverage ==5.3.1 ; (python_version < "3.10" and python_version >= "3.9") and extra == 'all-strict' | ||
| Requires-Dist: numpy ==1.19.3 ; (python_version < "3.10" and python_version >= "3.9") and extra == 'all-strict' | ||
| Requires-Dist: pytest ==4.6.0 ; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == 'all-strict' | ||
| Requires-Dist: PyYAML ==6.0 ; (python_version < "3.11" and python_version >= "3.10") and extra == 'all-strict' | ||
| Requires-Dist: numpy ==1.21.6 ; (python_version < "3.11" and python_version >= "3.10") and extra == 'all-strict' | ||
| Requires-Dist: PyYAML ==6.0 ; (python_version < "3.12" and python_version >= "3.11") and extra == 'all-strict' | ||
| Requires-Dist: numpy ==1.23.2 ; (python_version < "3.12" and python_version >= "3.11") and extra == 'all-strict' | ||
| Requires-Dist: coverage ==5.3.1 ; (python_version < "3.4" and python_version >= "2.7") and extra == 'all-strict' | ||
| Requires-Dist: numpy ==1.11.1 ; (python_version < "3.4" and python_version >= "2.7") and extra == 'all-strict' | ||
| Requires-Dist: coverage ==4.3.4 ; (python_version < "3.5" and python_version >= "3.4") and extra == 'all-strict' | ||
| Requires-Dist: numpy ==1.11.1 ; (python_version < "3.5" and python_version >= "3.4") and extra == 'all-strict' | ||
| Requires-Dist: pytest <=4.6.11,==4.6.0 ; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == 'all-strict' | ||
| Requires-Dist: pytest-cov ==2.8.1 ; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == 'all-strict' | ||
| Requires-Dist: coverage ==5.3.1 ; (python_version < "3.6" and python_version >= "3.5") and extra == 'all-strict' | ||
| Requires-Dist: numpy ==1.11.1 ; (python_version < "3.6" and python_version >= "3.5") and extra == 'all-strict' | ||
| Requires-Dist: pytest <=6.1.2,==4.6.0 ; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == 'all-strict' | ||
| Requires-Dist: pytest-cov ==2.9.0 ; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == 'all-strict' | ||
| Requires-Dist: PyYAML ==5.4.1 ; (python_version < "3.7" and python_version >= "3.6") and extra == 'all-strict' | ||
| Requires-Dist: coverage ==6.1.1 ; (python_version < "3.7" and python_version >= "3.6") and extra == 'all-strict' | ||
| Requires-Dist: numpy ==1.12.0 ; (python_version < "3.7" and python_version >= "3.6") and extra == 'all-strict' | ||
| Requires-Dist: pytest ==4.6.0 ; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == 'all-strict' | ||
| Requires-Dist: PyYAML ==5.4.1 ; (python_version < "3.8" and python_version >= "3.7") and extra == 'all-strict' | ||
| Requires-Dist: coverage ==6.1.1 ; (python_version < "3.8" and python_version >= "3.7") and extra == 'all-strict' | ||
| Requires-Dist: numpy ==1.14.5 ; (python_version < "3.8" and python_version >= "3.7") and extra == 'all-strict' | ||
| Requires-Dist: PyYAML ==5.4.1 ; (python_version < "3.9" and python_version >= "3.8") and extra == 'all-strict' | ||
| Requires-Dist: coverage ==6.1.1 ; (python_version < "3.9" and python_version >= "3.8") and extra == 'all-strict' | ||
| Requires-Dist: numpy ==1.19.2 ; (python_version < "3.9" and python_version >= "3.8") and extra == 'all-strict' | ||
| Requires-Dist: PyYAML ==6.0.1 ; (python_version < "4.0" and python_version >= "3.12") and extra == 'all-strict' | ||
| Requires-Dist: numpy ==1.26.0 ; (python_version < "4.0" and python_version >= "3.12") and extra == 'all-strict' | ||
| Requires-Dist: coverage ==6.1.1 ; (python_version >= "3.10") and extra == 'all-strict' | ||
| Requires-Dist: pytest ==6.2.5 ; (python_version >= "3.10.0") and extra == 'all-strict' | ||
| Requires-Dist: omegaconf ==2.2.2 ; (python_version >= "3.6") and extra == 'all-strict' | ||
| Requires-Dist: pytest-cov ==3.0.0 ; (python_version >= "3.6.0") and extra == 'all-strict' | ||
| Requires-Dist: rich-argparse ==1.1.0 ; (python_version >= "3.7") and extra == 'all-strict' | ||
| Requires-Dist: coverage >=4.5 ; (python_version < "2.7" and python_version >= "2.6") and extra == 'all' | ||
| Requires-Dist: pytest <=4.6.11,>=4.6.0 ; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == 'all' | ||
| Requires-Dist: pytest-cov >=2.8.1 ; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == 'all' | ||
| Requires-Dist: PyYAML >=5.4.1 ; (python_version < "3.10" and python_version >= "3.9") and extra == 'all' | ||
| Requires-Dist: coverage >=5.3.1 ; (python_version < "3.10" and python_version >= "3.9") and extra == 'all' | ||
| Requires-Dist: numpy >=1.19.3 ; (python_version < "3.10" and python_version >= "3.9") and extra == 'all' | ||
| Requires-Dist: pytest >=4.6.0 ; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == 'all' | ||
| Requires-Dist: PyYAML >=6.0 ; (python_version < "3.11" and python_version >= "3.10") and extra == 'all' | ||
| Requires-Dist: numpy >=1.21.6 ; (python_version < "3.11" and python_version >= "3.10") and extra == 'all' | ||
| Requires-Dist: PyYAML >=6.0 ; (python_version < "3.12" and python_version >= "3.11") and extra == 'all' | ||
| Requires-Dist: numpy >=1.23.2 ; (python_version < "3.12" and python_version >= "3.11") and extra == 'all' | ||
| Requires-Dist: coverage >=5.3.1 ; (python_version < "3.4" and python_version >= "2.7") and extra == 'all' | ||
| Requires-Dist: numpy >=1.11.1 ; (python_version < "3.4" and python_version >= "2.7") and extra == 'all' | ||
| Requires-Dist: coverage >=4.3.4 ; (python_version < "3.5" and python_version >= "3.4") and extra == 'all' | ||
| Requires-Dist: numpy >=1.11.1 ; (python_version < "3.5" and python_version >= "3.4") and extra == 'all' | ||
| Requires-Dist: pytest <=4.6.11,>=4.6.0 ; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == 'all' | ||
| Requires-Dist: pytest-cov >=2.8.1 ; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == 'all' | ||
| Requires-Dist: coverage >=5.3.1 ; (python_version < "3.6" and python_version >= "3.5") and extra == 'all' | ||
| Requires-Dist: numpy >=1.11.1 ; (python_version < "3.6" and python_version >= "3.5") and extra == 'all' | ||
| Requires-Dist: pytest <=6.1.2,>=4.6.0 ; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == 'all' | ||
| Requires-Dist: pytest-cov >=2.9.0 ; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == 'all' | ||
| Requires-Dist: PyYAML >=5.4.1 ; (python_version < "3.7" and python_version >= "3.6") and extra == 'all' | ||
| Requires-Dist: coverage >=6.1.1 ; (python_version < "3.7" and python_version >= "3.6") and extra == 'all' | ||
| Requires-Dist: numpy >=1.12.0 ; (python_version < "3.7" and python_version >= "3.6") and extra == 'all' | ||
| Requires-Dist: pytest >=4.6.0 ; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == 'all' | ||
| Requires-Dist: PyYAML >=5.4.1 ; (python_version < "3.8" and python_version >= "3.7") and extra == 'all' | ||
| Requires-Dist: coverage >=6.1.1 ; (python_version < "3.8" and python_version >= "3.7") and extra == 'all' | ||
| Requires-Dist: numpy >=1.14.5 ; (python_version < "3.8" and python_version >= "3.7") and extra == 'all' | ||
| Requires-Dist: PyYAML >=5.4.1 ; (python_version < "3.9" and python_version >= "3.8") and extra == 'all' | ||
| Requires-Dist: coverage >=6.1.1 ; (python_version < "3.9" and python_version >= "3.8") and extra == 'all' | ||
| Requires-Dist: numpy >=1.19.2 ; (python_version < "3.9" and python_version >= "3.8") and extra == 'all' | ||
| Requires-Dist: PyYAML >=6.0.1 ; (python_version < "4.0" and python_version >= "3.12") and extra == 'all' | ||
| Requires-Dist: numpy >=1.26.0 ; (python_version < "4.0" and python_version >= "3.12") and extra == 'all' | ||
| Requires-Dist: coverage >=6.1.1 ; (python_version >= "3.10") and extra == 'all' | ||
| Requires-Dist: pytest >=6.2.5 ; (python_version >= "3.10.0") and extra == 'all' | ||
| Requires-Dist: omegaconf >=2.2.2 ; (python_version >= "3.6") and extra == 'all' | ||
| Requires-Dist: pytest-cov >=3.0.0 ; (python_version >= "3.6.0") and extra == 'all' | ||
| Requires-Dist: rich-argparse >=1.1.0 ; (python_version >= "3.7") and extra == 'all' | ||
| Provides-Extra: optional | ||
| Requires-Dist: argcomplete >=3.0.5 ; extra == 'optional' | ||
| Provides-Extra: optional-strict | ||
| Requires-Dist: argcomplete ==3.0.5 ; extra == 'optional-strict' | ||
| Requires-Dist: numpy ==1.19.3 ; (python_version < "3.10" and python_version >= "3.9") and extra == 'optional-strict' | ||
| Requires-Dist: numpy ==1.21.6 ; (python_version < "3.11" and python_version >= "3.10") and extra == 'optional-strict' | ||
| Requires-Dist: numpy ==1.23.2 ; (python_version < "3.12" and python_version >= "3.11") and extra == 'optional-strict' | ||
| Requires-Dist: numpy ==1.11.1 ; (python_version < "3.4" and python_version >= "2.7") and extra == 'optional-strict' | ||
| Requires-Dist: numpy ==1.11.1 ; (python_version < "3.5" and python_version >= "3.4") and extra == 'optional-strict' | ||
| Requires-Dist: numpy ==1.11.1 ; (python_version < "3.6" and python_version >= "3.5") and extra == 'optional-strict' | ||
| Requires-Dist: numpy ==1.12.0 ; (python_version < "3.7" and python_version >= "3.6") and extra == 'optional-strict' | ||
| Requires-Dist: numpy ==1.14.5 ; (python_version < "3.8" and python_version >= "3.7") and extra == 'optional-strict' | ||
| Requires-Dist: numpy ==1.19.2 ; (python_version < "3.9" and python_version >= "3.8") and extra == 'optional-strict' | ||
| Requires-Dist: numpy ==1.26.0 ; (python_version < "4.0" and python_version >= "3.12") and extra == 'optional-strict' | ||
| Requires-Dist: omegaconf ==2.2.2 ; (python_version >= "3.6") and extra == 'optional-strict' | ||
| Requires-Dist: rich-argparse ==1.1.0 ; (python_version >= "3.7") and extra == 'optional-strict' | ||
| Requires-Dist: numpy >=1.19.3 ; (python_version < "3.10" and python_version >= "3.9") and extra == 'optional' | ||
| Requires-Dist: numpy >=1.21.6 ; (python_version < "3.11" and python_version >= "3.10") and extra == 'optional' | ||
| Requires-Dist: numpy >=1.23.2 ; (python_version < "3.12" and python_version >= "3.11") and extra == 'optional' | ||
| Requires-Dist: numpy >=1.11.1 ; (python_version < "3.4" and python_version >= "2.7") and extra == 'optional' | ||
| Requires-Dist: numpy >=1.11.1 ; (python_version < "3.5" and python_version >= "3.4") and extra == 'optional' | ||
| Requires-Dist: numpy >=1.11.1 ; (python_version < "3.6" and python_version >= "3.5") and extra == 'optional' | ||
| Requires-Dist: numpy >=1.12.0 ; (python_version < "3.7" and python_version >= "3.6") and extra == 'optional' | ||
| Requires-Dist: numpy >=1.14.5 ; (python_version < "3.8" and python_version >= "3.7") and extra == 'optional' | ||
| Requires-Dist: numpy >=1.19.2 ; (python_version < "3.9" and python_version >= "3.8") and extra == 'optional' | ||
| Requires-Dist: numpy >=1.26.0 ; (python_version < "4.0" and python_version >= "3.12") and extra == 'optional' | ||
| Requires-Dist: omegaconf >=2.2.2 ; (python_version >= "3.6") and extra == 'optional' | ||
| Requires-Dist: rich-argparse >=1.1.0 ; (python_version >= "3.7") and extra == 'optional' | ||
| Provides-Extra: runtime-strict | ||
| Requires-Dist: ubelt ==1.2.3 ; extra == 'runtime-strict' | ||
| Requires-Dist: PyYAML ==5.4.1 ; (python_version < "3.10" and python_version >= "3.9") and extra == 'runtime-strict' | ||
| Requires-Dist: PyYAML ==6.0 ; (python_version < "3.11" and python_version >= "3.10") and extra == 'runtime-strict' | ||
| Requires-Dist: PyYAML ==6.0 ; (python_version < "3.12" and python_version >= "3.11") and extra == 'runtime-strict' | ||
| Requires-Dist: PyYAML ==5.4.1 ; (python_version < "3.7" and python_version >= "3.6") and extra == 'runtime-strict' | ||
| Requires-Dist: PyYAML ==5.4.1 ; (python_version < "3.8" and python_version >= "3.7") and extra == 'runtime-strict' | ||
| Requires-Dist: PyYAML ==5.4.1 ; (python_version < "3.9" and python_version >= "3.8") and extra == 'runtime-strict' | ||
| Requires-Dist: PyYAML ==6.0.1 ; (python_version < "4.0" and python_version >= "3.12") and extra == 'runtime-strict' | ||
| Provides-Extra: tests | ||
| Requires-Dist: xdoctest >=1.1.3 ; extra == 'tests' | ||
| Provides-Extra: tests-strict | ||
| Requires-Dist: xdoctest ==1.1.3 ; extra == 'tests-strict' | ||
| Requires-Dist: coverage ==4.5 ; (python_version < "2.7" and python_version >= "2.6") and extra == 'tests-strict' | ||
| Requires-Dist: pytest <=4.6.11,==4.6.0 ; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == 'tests-strict' | ||
| Requires-Dist: pytest-cov ==2.8.1 ; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == 'tests-strict' | ||
| Requires-Dist: coverage ==5.3.1 ; (python_version < "3.10" and python_version >= "3.9") and extra == 'tests-strict' | ||
| Requires-Dist: pytest ==4.6.0 ; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == 'tests-strict' | ||
| Requires-Dist: coverage ==5.3.1 ; (python_version < "3.4" and python_version >= "2.7") and extra == 'tests-strict' | ||
| Requires-Dist: coverage ==4.3.4 ; (python_version < "3.5" and python_version >= "3.4") and extra == 'tests-strict' | ||
| Requires-Dist: pytest <=4.6.11,==4.6.0 ; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == 'tests-strict' | ||
| Requires-Dist: pytest-cov ==2.8.1 ; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == 'tests-strict' | ||
| Requires-Dist: coverage ==5.3.1 ; (python_version < "3.6" and python_version >= "3.5") and extra == 'tests-strict' | ||
| Requires-Dist: pytest <=6.1.2,==4.6.0 ; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == 'tests-strict' | ||
| Requires-Dist: pytest-cov ==2.9.0 ; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == 'tests-strict' | ||
| Requires-Dist: coverage ==6.1.1 ; (python_version < "3.7" and python_version >= "3.6") and extra == 'tests-strict' | ||
| Requires-Dist: pytest ==4.6.0 ; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == 'tests-strict' | ||
| Requires-Dist: coverage ==6.1.1 ; (python_version < "3.8" and python_version >= "3.7") and extra == 'tests-strict' | ||
| Requires-Dist: coverage ==6.1.1 ; (python_version < "3.9" and python_version >= "3.8") and extra == 'tests-strict' | ||
| Requires-Dist: coverage ==6.1.1 ; (python_version >= "3.10") and extra == 'tests-strict' | ||
| Requires-Dist: pytest ==6.2.5 ; (python_version >= "3.10.0") and extra == 'tests-strict' | ||
| Requires-Dist: pytest-cov ==3.0.0 ; (python_version >= "3.6.0") and extra == 'tests-strict' | ||
| Requires-Dist: coverage >=4.5 ; (python_version < "2.7" and python_version >= "2.6") and extra == 'tests' | ||
| Requires-Dist: pytest <=4.6.11,>=4.6.0 ; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == 'tests' | ||
| Requires-Dist: pytest-cov >=2.8.1 ; (python_version < "2.8.0" and python_version >= "2.7.0") and extra == 'tests' | ||
| Requires-Dist: coverage >=5.3.1 ; (python_version < "3.10" and python_version >= "3.9") and extra == 'tests' | ||
| Requires-Dist: pytest >=4.6.0 ; (python_version < "3.10.0" and python_version >= "3.7.0") and extra == 'tests' | ||
| Requires-Dist: coverage >=5.3.1 ; (python_version < "3.4" and python_version >= "2.7") and extra == 'tests' | ||
| Requires-Dist: coverage >=4.3.4 ; (python_version < "3.5" and python_version >= "3.4") and extra == 'tests' | ||
| Requires-Dist: pytest <=4.6.11,>=4.6.0 ; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == 'tests' | ||
| Requires-Dist: pytest-cov >=2.8.1 ; (python_version < "3.5.0" and python_version >= "3.4.0") and extra == 'tests' | ||
| Requires-Dist: coverage >=5.3.1 ; (python_version < "3.6" and python_version >= "3.5") and extra == 'tests' | ||
| Requires-Dist: pytest <=6.1.2,>=4.6.0 ; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == 'tests' | ||
| Requires-Dist: pytest-cov >=2.9.0 ; (python_version < "3.6.0" and python_version >= "3.5.0") and extra == 'tests' | ||
| Requires-Dist: coverage >=6.1.1 ; (python_version < "3.7" and python_version >= "3.6") and extra == 'tests' | ||
| Requires-Dist: pytest >=4.6.0 ; (python_version < "3.7.0" and python_version >= "3.6.0") and extra == 'tests' | ||
| Requires-Dist: coverage >=6.1.1 ; (python_version < "3.8" and python_version >= "3.7") and extra == 'tests' | ||
| Requires-Dist: coverage >=6.1.1 ; (python_version < "3.9" and python_version >= "3.8") and extra == 'tests' | ||
| Requires-Dist: coverage >=6.1.1 ; (python_version >= "3.10") and extra == 'tests' | ||
| Requires-Dist: pytest >=6.2.5 ; (python_version >= "3.10.0") and extra == 'tests' | ||
| Requires-Dist: pytest-cov >=3.0.0 ; (python_version >= "3.6.0") and extra == 'tests' | ||
| ScriptConfig | ||
| ============ | ||
| .. # TODO Get CI services running on gitlab | ||
| .. #|CircleCI| |Travis| |Codecov| |ReadTheDocs| | ||
| |GitlabCIPipeline| |GitlabCICoverage| |Appveyor| |Pypi| |PypiDownloads| | ||
| +------------------+--------------------------------------------------+ | ||
| | Read the docs | https://scriptconfig.readthedocs.io | | ||
| +------------------+--------------------------------------------------+ | ||
| | Gitlab (main) | https://gitlab.kitware.com/utils/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| | Github (mirror) | https://github.com/Kitware/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| | Pypi | https://pypi.org/project/scriptconfig | | ||
| +------------------+--------------------------------------------------+ | ||
| The goal of ``scriptconfig`` is to make it easy to be able to define a default | ||
| configuration by **simply defining a dictionary**, and then allow that | ||
| configuration to be modified by either: | ||
| 1. Updating it with another Python dictionary (e.g. ``kwargs``) | ||
| 2. Reading a YAML/JSON configuration file, or | ||
| 3. Inspecting values on ``sys.argv``, in which case we provide a powerful | ||
| command line interface (CLI). | ||
| The simplest way to create a script config is to create a class that inherits | ||
| from ``scriptconfig.DataConfig``. Then, use class variables to define the | ||
| expected keys and default values. | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| class ExampleConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| # Wrap defaults with `Value` to provide metadata | ||
| option1 = scfg.Value('default1', help='option1 help') | ||
| option2 = scfg.Value('default2', help='option2 help') | ||
| option3 = scfg.Value('default3', help='option3 help') | ||
| # Wrapping a default with `Value` is optional | ||
| option4 = 'default4' | ||
| An instance of a config object will work similarly to a dataclass, but it also | ||
| implements methods to duck-type a dictionary. Thus a scriptconfig object can be | ||
| dropped into code that uses an existing dictionary configuration or an existing | ||
| argparse namespace configuration. | ||
| .. code-block:: python | ||
| # Use as a dictionary with defaults | ||
| config = ExampleConfig(option1=123) | ||
| print(config) | ||
| # Can access items like a dictionary | ||
| print(config['option1']) | ||
| # OR Can access items like a namespace | ||
| print(config.option1) | ||
| Use the ``.cli`` classmethod to create an extended argparse command line | ||
| interface. Options to the ``cli`` method are similar to | ||
| ``argparse.ArgumentParser.parse_args``. | ||
| .. code-block:: python | ||
| # Use as a argparse CLI | ||
| config = ExampleConfig.cli(argv=['--option2=overruled']) | ||
| print(config) | ||
| After all that, if you still aren't loving scriptconfig, or you can't use it as | ||
| a dependency in production, you can ask it to convert itself to pure-argparse | ||
| via ``print(ExampleConfig().port_to_argparse())``, and it will print out: | ||
| .. code-block:: python | ||
| import argparse | ||
| parser = argparse.ArgumentParser( | ||
| prog='ExampleConfig', | ||
| description='The docstring will be the description in the CLI help', | ||
| formatter_class=argparse.RawDescriptionHelpFormatter, | ||
| ) | ||
| parser.add_argument('--option1', help='option1 help', default='default1', dest='option1', required=False) | ||
| parser.add_argument('--option2', help='option2 help', default='default2', dest='option2', required=False) | ||
| parser.add_argument('--option3', help='option3 help', default='default3', dest='option3', required=False) | ||
| parser.add_argument('--option4', help='', default='default4', dest='option4', required=False) | ||
| Of course, the above also removes extra features of scriptconfig - so its not | ||
| exactly 1-to-1, but it's close. It's also a good tool for transferring any | ||
| existing intuition about ``argparse`` to ``scriptconfig``. | ||
| Similarly there is a method which can take an existing ArgumentParser as input, | ||
| and produce a scriptconfig definition. Given the above ``parser`` object, | ||
| ``print(scfg.Config.port_from_argparse(parser, style))`` will print out: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| option1 = scfg.Value('default1', help='option1 help') | ||
| option2 = scfg.Value('default2', help='option2 help') | ||
| option3 = scfg.Value('default3', help='option3 help') | ||
| option4 = scfg.Value('default4', help='') | ||
| Goal | ||
| ---- | ||
| The idea is we want to be able to start writing a simple program with a simple | ||
| configuration and allow it to evolve with minimal refactoring. In the early | ||
| stages we will insist that there be little-to-no boilerplate, but as a program | ||
| evolves we will add boilerplate to enhance the featurefull-ness of our program. | ||
| When we start coding we should aim for something like this: | ||
| .. code-block:: python | ||
| def my_function(): | ||
| config = { | ||
| 'simple_option1': 1, | ||
| 'simple_option2': 2, | ||
| } | ||
| # Early algorithmic and debugging logic | ||
| ... | ||
| As we evolve our code, we can plug scriptconfig in like this: | ||
| .. code-block:: python | ||
| def my_function(): | ||
| default_config = { | ||
| 'simple_option1': 1, | ||
| 'simple_option2': 2, | ||
| } | ||
| import scriptconfig | ||
| class MyConfig(scriptconfig.DataConfig): | ||
| __default__ = default_config | ||
| config = MyConfig() | ||
| # Transition algorithmic and debugging logic | ||
| ... | ||
| It's not pretty, but it gives us the ability to a fairly advanced CLI right | ||
| away (i.e by calling the ``.cli`` classmethod) without any major sacrifice to | ||
| code simplicity. However, as a project evolves we may eventually want to | ||
| refactor our CLI to gain full control over the metadata in our configuration an | ||
| CLI. Scriptconfig has a tool to help with this too. Given this janky definition, | ||
| we can port to a more ellegant style. We can run | ||
| ``print(config.port_to_dataconf())`` which prints: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| argparse CLI generated by scriptconfig 0.7.12 | ||
| """ | ||
| simple_option1 = scfg.Value(1, help=None) | ||
| simple_option2 = scfg.Value(2, help=None) | ||
| And then use that to make the refactor much easier. | ||
| The final state of a scriptconfig program might look something like this: | ||
| .. code-block:: python | ||
| import ubelt as ub | ||
| import scriptconfig as scfg | ||
| class MyConfig(scfg.DataConfig): | ||
| """ | ||
| This is my CLI description | ||
| """ | ||
| simple_option1 = scfg.Value(1, help=ub.paragraph( | ||
| ''' | ||
| A reasonably detailed but concise description of an argument. | ||
| About one paragraph is reasonable. | ||
| ''') | ||
| simple_option2 = scfg.Value(2, help='more help is better') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| config = cls.cli(cmdline=cmdline, data=kwargs) | ||
| my_function(config) | ||
| def my_function(config): | ||
| # Continued algorithmic and debugging logic | ||
| ... | ||
| Note that the fundamental impact on the ``...`` -- i.e. the intereting part of | ||
| the function -- remain completely unchanged! From it's point of view, you never | ||
| did anything to the original ``config`` dictionary, because scriptconfig | ||
| duck-typed it at every stage. | ||
| Installation | ||
| ------------ | ||
| The `scriptconfig <https://pypi.org/project/scriptconfig/>`_ package can be installed via pip: | ||
| .. code-block:: bash | ||
| pip install scriptconfig | ||
| To install with argcomplete and rich-argparse support, either install these | ||
| packages separately or use: | ||
| .. code-block:: bash | ||
| pip install scriptconfig[optional] | ||
| Features | ||
| -------- | ||
| - Serializes to JSON | ||
| - Dict-like interface. By default a ``Config`` object operates independent of config files or the command line. | ||
| - Can create command line interfaces | ||
| - Can directly create an independent argparse object | ||
| - Can use special command line loading using ``self.load(cmdline=True)``. This extends the basic argparse interface with: | ||
| - Can specify options as either ``--option value`` or ``--option=value`` | ||
| - Default config options allow for "smartcasting" values like lists and paths | ||
| - Automatically add ``--config``, ``--dumps``, and ``--dump`` CLI options | ||
| when reading cmdline via ``load``. | ||
| - Fuzzy hyphen matching: e.g. ``--foo-bar=2`` and ``--foo_bar=2`` are treated the same for argparse options (note: modal commands do not have this option yet) | ||
| - Inheritance unions configs. | ||
| - Modal configs (see scriptconfig.modal) | ||
| - Integration with `argcomplete <https://pypi.org/project/argcomplete/>`_ for shell autocomplete. | ||
| - Integration with `rich_argparse <https://pypi.org/project/rich_argparse/>`_ for colorful CLI help pages. | ||
| Example Script | ||
| -------------- | ||
| Scriptconfig is used to define a flat configuration dictionary with values that | ||
| can be specified via Python keyword arguments, command line parameters, or a | ||
| YAML config file. Consider the following script that prints its config, opens a | ||
| file, computes its hash, and then prints it to stdout. | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| import hashlib | ||
| class FileHashConfig(scfg.DataConfig): | ||
| """ | ||
| The docstring will be the description in the CLI help | ||
| """ | ||
| fpath = scfg.Value(None, position=1, help='a path to a file to hash') | ||
| hasher = scfg.Value('sha1', choices=['sha1', 'sha512'], help='a name of a hashlib hasher') | ||
| def main(**kwargs): | ||
| config = FileHashConfig.cli(data=kwargs) | ||
| print('config = {!r}'.format(config)) | ||
| fpath = config['fpath'] | ||
| hasher = getattr(hashlib, config['hasher'])() | ||
| with open(fpath, 'rb') as file: | ||
| hasher.update(file.read()) | ||
| hashstr = hasher.hexdigest() | ||
| print('The {hasher} hash of {fpath} is {hashstr}'.format( | ||
| hashstr=hashstr, **config)) | ||
| if __name__ == '__main__': | ||
| main() | ||
| If this script is in a module ``hash_demo.py`` (e.g. in the examples folder of | ||
| this repo), it can be invoked in these following ways. | ||
| Purely from the command line: | ||
| .. code-block:: bash | ||
| # Get help | ||
| python hash_demo.py --help | ||
| # Using key-val pairs | ||
| python hash_demo.py --fpath=$HOME/.bashrc --hasher=sha1 | ||
| # Using a positional arguments and other defaults | ||
| python hash_demo.py $HOME/.bashrc | ||
| From the command line using a YAML config: | ||
| .. code-block:: bash | ||
| # Write out a config file | ||
| echo '{"fpath": "hashconfig.json", "hasher": "sha512"}' > hashconfig.json | ||
| # Use the special `--config` cli arg provided by scriptconfig | ||
| python hash_demo.py --config=hashconfig.json | ||
| # You can also mix and match, this overrides the hasher in the config with sha1 | ||
| python hash_demo.py --config=hashconfig.json --hasher=sha1 | ||
| Lastly you can call it from good ol' Python. | ||
| .. code-block:: python | ||
| import hash_demo | ||
| hash_demo.main(fpath=hash_demo.__file__, hasher='sha512') | ||
| Modal CLIs | ||
| ---------- | ||
| A ModalCLI defines a way to group several smaller scriptconfig CLIs into a | ||
| single parent CLI that chooses between them "modally". E.g. if we define two | ||
| configs: do_foo and do_bar, we use ModalCLI to define a parent program that can | ||
| run one or the other. Let's make this more concrete. | ||
| Consider the code in ``examples/demo_modal.py``: | ||
| .. code-block:: python | ||
| import scriptconfig as scfg | ||
| class DoFooCLI(scfg.DataConfig): | ||
| __command__ = 'do_foo' | ||
| option1 = scfg.Value(None, help='option1') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| self = cls.cli(cmdline=cmdline, data=kwargs) | ||
| print('Called Foo with: ' + str(self)) | ||
| class DoBarCLI(scfg.DataConfig): | ||
| __command__ = 'do_bar' | ||
| option1 = scfg.Value(None, help='option1') | ||
| @classmethod | ||
| def main(cls, cmdline=1, **kwargs): | ||
| self = cls.cli(cmdline=cmdline, data=kwargs) | ||
| print('Called Bar with: ' + str(self)) | ||
| class MyModalCLI(scfg.ModalCLI): | ||
| __version__ = '1.2.3' | ||
| foo = DoFooCLI | ||
| bar = DoBarCLI | ||
| if __name__ == '__main__': | ||
| MyModalCLI().main() | ||
| Running: ``python examples/demo_modal.py --help``, results in: | ||
| .. code-block:: | ||
| usage: demo_modal.py [-h] [--version] {do_foo,do_bar} ... | ||
| options: | ||
| -h, --help show this help message and exit | ||
| --version show version number and exit (default: False) | ||
| commands: | ||
| {do_foo,do_bar} specify a command to run | ||
| do_foo argparse CLI generated by scriptconfig 0.7.12 | ||
| do_bar argparse CLI generated by scriptconfig 0.7.12 | ||
| And if you specify a command, ``python examples/demo_modal.py do_bar --help``, you get the help for that subcommand: | ||
| .. code-block:: | ||
| usage: DoBarCLI [-h] [--option1 OPTION1] | ||
| argparse CLI generated by scriptconfig 0.7.12 | ||
| options: | ||
| -h, --help show this help message and exit | ||
| --option1 OPTION1 option1 (default: None) | ||
| Autocomplete | ||
| ------------ | ||
| If you installed the optional `argcomplete <https://pypi.org/project/argcomplete/>`_ package you will find that pressing | ||
| tab will autocomplete registered arguments for scriptconfig CLIs. See project instructions for details, but on standard Linux | ||
| distributions you can enable global completion via: | ||
| .. code:: bash | ||
| pip install argcomplete | ||
| mkdir -p ~/.bash_completion.d | ||
| activate-global-python-argcomplete --dest ~/.bash_completion.d | ||
| source ~/.bash_completion.d/python-argcomplete | ||
| And then add these lines to your ``.bashrc``: | ||
| .. code:: bash | ||
| if [ -f "$HOME/.bash_completion.d/python-argcomplete" ]; then | ||
| source ~/.bash_completion.d/python-argcomplete | ||
| fi | ||
| Lastly, ensure your Python script has the following two comments at the top: | ||
| .. code:: python | ||
| #!/usr/bin/env python | ||
| # PYTHON_ARGCOMPLETE_OK | ||
| Project Design Goals | ||
| -------------------- | ||
| * Write Python programs that can be invoked either through the commandline | ||
| or via Python itself. | ||
| * Drop in replacement for any dictionary-based configuration system. | ||
| * Intuitive parsing (currently working on this), ideally improve on | ||
| argparse if possible. This means being able to easily specify simple | ||
| lists, numbers, strings, and paths. | ||
| To get started lets consider some example usage: | ||
| .. code-block:: python | ||
| >>> import scriptconfig as scfg | ||
| >>> # In its simplest incarnation, the config class specifies default values. | ||
| >>> # For each configuration parameter. | ||
| >>> class ExampleConfig(scfg.DataConfig): | ||
| >>> num = 1 | ||
| >>> mode = 'bar' | ||
| >>> ignore = ['baz', 'biz'] | ||
| >>> # Creating an instance, starts using the defaults | ||
| >>> config = ExampleConfig() | ||
| >>> assert config['num'] == 1 | ||
| >>> # Or pass in known data. (load as shown in the original example still works) | ||
| >>> kwargs = {'num': 2} | ||
| >>> config = ExampleConfig.cli(default=kwargs, cmdline=False) | ||
| >>> assert config['num'] == 2 | ||
| >>> # The `load` method can also be passed a JSON/YAML file/path. | ||
| >>> config_fpath = '/tmp/foo' | ||
| >>> open(config_fpath, 'w').write('{"mode": "foo"}') | ||
| >>> config.load(config_fpath, cmdline=False) | ||
| >>> assert config['num'] == 2 | ||
| >>> assert config['mode'] == "foo" | ||
| >>> # It is possbile to load only from CLI by setting cmdline=True | ||
| >>> # or by setting it to a custom sys.argv | ||
| >>> config = ExampleConfig.cli(argv=['--num=4']) | ||
| >>> assert config['num'] == 4 | ||
| >>> # Note that using `config.load(cmdline=True)` will just use the | ||
| >>> # contents of sys.argv | ||
| Notice in the above example the keys in your default dictionary are command | ||
| line arguments and values are their defaults. You can augment default values | ||
| by wrapping them in ``scriptconfig.Value`` objects to encapsulate information | ||
| like help documentation or type information. | ||
| .. code-block:: python | ||
| >>> import scriptconfig as scfg | ||
| >>> class ExampleConfig(scfg.Config): | ||
| >>> __default__ = { | ||
| >>> 'num': scfg.Value(1, help='a number'), | ||
| >>> 'mode': scfg.Value('bar', help='mode1 help'), | ||
| >>> 'mode2': scfg.Value('bar', type=str, help='mode2 help'), | ||
| >>> 'ignore': scfg.Value(['baz', 'biz'], help='list of ignore vals'), | ||
| >>> } | ||
| >>> config = ExampleConfig() | ||
| >>> # smartcast can handle lists as long as there are no spaces | ||
| >>> config.load(cmdline=['--ignore=spam,eggs']) | ||
| >>> assert config['ignore'] == ['spam', 'eggs'] | ||
| >>> # Note that the Value type can influence how data is parsed | ||
| >>> config.load(cmdline=['--mode=spam,eggs', '--mode2=spam,eggs']) | ||
| (Note the above example uses the older ``Config`` usage pattern where | ||
| attributes are members of a ``__default__`` dictionary. The ``DataConfig`` | ||
| class should be favored moving forward past version 0.6.2. However, | ||
| the ``__default__`` attribute is always available if you have an existing | ||
| dictionary you want to wrap with scriptconfig. | ||
| Gotchas | ||
| ------- | ||
| **CLI Values with commas:** | ||
| When using ``scriptconfig`` to generate a command line interface, it uses a | ||
| function called ``smartcast`` to try to determine input type when it is not | ||
| explicitly given. If you've ever used a program that tries to be "smart" you'll | ||
| know this can end up with some weird behavior. The case where that happens here | ||
| is when you pass a value that contains commas on the command line. If you don't | ||
| specify the default value as a ``scriptconfig.Value`` with a specified | ||
| ``type``, if will interpret your input as a list of values. In the future we | ||
| may change the behavior of ``smartcast``, or prevent it from being used as a | ||
| default. | ||
| **Boolean flags and positional arguments:** | ||
| ``scriptconfig`` always provides a key/value way to express arguments. However, it also | ||
| recognizes that sometimes you want to just type ``--flag`` and not ``--flag=1``. | ||
| We allow for this for ``Values`` with ``isflag=1``, but this causes a | ||
| corner-case ambituity with positional arguments. For the following example: | ||
| .. code:: python | ||
| class MyConfig(scfg.DataConfig): | ||
| arg1 = scfg.Value(None, position=1) | ||
| flag1 = scfg.Value(False, isflag=True, position=1) | ||
| For ``--flag 1`` We cannot determine if you wanted | ||
| ``{'arg1': 1, 'flag1': False}`` or ``{'arg1': None, 'flag1': True}``. | ||
| This is fixable by either using strict key/value arguments, expressing all | ||
| positional arguments before using flag arguments, or using the `` -- `` | ||
| construct and putting all positional arguments at the end. In the future we may | ||
| raise an AmbiguityError when specifying arguments like this, but for now we | ||
| leave the behavior undefined. | ||
| FAQ | ||
| --- | ||
| Question: How do I override the default values for a scriptconfig object using JSON file? | ||
| Answer: This depends if you want to pass the path to that JSON file via the command line or if you have that file in memory already. There are ways to do either. In the first case you can pass ``--config=<path-to-your-file>`` (assuming you have set the ``cmdline=True`` keyword arg when creating your config object e.g.: ``config = MyConfig(cmdline=True)``. In the second case when you create an instance of the scriptconfig object pass the ``default=<your dict>`` when creating the object: e.g. ``config = MyConfig(default=json.load(open(fpath, 'r')))``. But the special ``--config`` ``--dump`` and ``--dumps`` CLI arg is baked into script config to make this easier. | ||
| Related Software | ||
| ---------------- | ||
| I've never been completely happy with existing config / argument parser | ||
| software. I prefer to not use decorators, so click and to some extend hydra are | ||
| no-gos. Fire is nice when you want a really quick CLI, but is not so nice if | ||
| you ever go to deploy the program in the real world. | ||
| The builtin argparse in Python is pretty good, but I with it was easier to do | ||
| things like allowing arguments to be flags or key/value pairs. This library | ||
| uses argparse under the hood because of its stable and standard backend, but | ||
| that does mean we inherit some of its quirks. | ||
| The configargparse library - like this one - augments argparse with the ability | ||
| to read defaults from config files, but it has some major usage limitations due | ||
| to its implementation and there are better options (like jsonargparse). It also | ||
| does not support the use case of calling the CLI as a Python function very | ||
| well. | ||
| The jsonargparse library is newer than this one, and looks very compelling. I | ||
| feel like the definition of CLIs in this library are complementary and I'm | ||
| considering adding support in this library for jsonargparse because it solves | ||
| the problem of nested configurations and I would like to inherit from that. | ||
| Keep an eye out for this feature in future work. | ||
| Hydra - https://hydra.cc/docs/intro/ | ||
| OmegaConf - https://omegaconf.readthedocs.io/en/latest/index.html | ||
| Argparse - https://docs.python.org/3/library/argparse.html | ||
| JsonArgparse - https://jsonargparse.readthedocs.io/en/stable/index.html | ||
| Fire - https://pypi.org/project/fire/ | ||
| Click - https://pypi.org/project/click/ | ||
| ConfigArgparse - https://pypi.org/project/ConfigArgParse/ | ||
| TODO | ||
| ---- | ||
| - [ ] Nested Modal CLI's | ||
| - [ ] Fuzzy hyphens in ModelCLIs | ||
| - [X] Policy on nested heirachies (currently disallowed) - jsonargparse will be the solution here. | ||
| - [ ] How to best integrate with jsonargparse | ||
| - [ ] Policy on smartcast (currently enabled) | ||
| - [ ] Find a way to gracefully way to make smartcast do less. (e.g. no list parsing, but int is ok, we may think about accepting YAML) | ||
| - [X] Policy on positional arguments (currently experimental) - we have implemented them permissively with one undefined corner case. | ||
| - [X] Fixed length - nope | ||
| - [X] Variable length | ||
| - [X] Can argparse be modified to always allow for them to appear at the beginning or end? - Probably not. | ||
| - [x] Can we get argparse to allow a positional arg change the value of a prefixed arg and still have a sane help menu? | ||
| - [x] Policy on boolean flags - See the ``isflag`` argument of ``scriptconfig.Value`` | ||
| - [x] Improve over argparse's default autogenerated help docs (needs exploration on what is possible with argparse and where extensions are feasible) | ||
| .. |GitlabCIPipeline| image:: https://gitlab.kitware.com/utils/scriptconfig/badges/main/pipeline.svg | ||
| :target: https://gitlab.kitware.com/utils/scriptconfig/-/jobs | ||
| .. |GitlabCICoverage| image:: https://gitlab.kitware.com/utils/scriptconfig/badges/main/coverage.svg | ||
| :target: https://gitlab.kitware.com/utils/scriptconfig/commits/main | ||
| .. # See: https://ci.appveyor.com/project/jon.crall/scriptconfig/settings/badges | ||
| .. |Appveyor| image:: https://ci.appveyor.com/api/projects/status/br3p8lkuvol2vas4/branch/main?svg=true | ||
| :target: https://ci.appveyor.com/project/jon.crall/scriptconfig/branch/main | ||
| .. |Codecov| image:: https://codecov.io/github/Erotemic/scriptconfig/badge.svg?branch=main&service=github | ||
| :target: https://codecov.io/github/Erotemic/scriptconfig?branch=main | ||
| .. |Pypi| image:: https://img.shields.io/pypi/v/scriptconfig.svg | ||
| :target: https://pypi.python.org/pypi/scriptconfig | ||
| .. |PypiDownloads| image:: https://img.shields.io/pypi/dm/scriptconfig.svg | ||
| :target: https://pypistats.org/packages/scriptconfig | ||
| .. |ReadTheDocs| image:: https://readthedocs.org/projects/scriptconfig/badge/?version=latest | ||
| :target: http://scriptconfig.readthedocs.io/en/latest/ |
| scriptconfig/__init__.py,sha256=yFBKFkzbZM8I_ugjwvzNwJpNr29Gb7Q7ehXcI1NN5ao,5894 | ||
| scriptconfig/_ubelt_repr_extension.py,sha256=nCy5pXxlpiXaYlD7kYn6qK5XUmSYvAfaMoIU63XTaus,748 | ||
| scriptconfig/_ubelt_repr_extension.pyi,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1 | ||
| scriptconfig/argparse_ext.py,sha256=K-Cu8MFkRQanigNAMdowWC-oSDQs-EF4k6Fdx75BjWg,27532 | ||
| scriptconfig/argparse_ext.pyi,sha256=Td-o8YEYGF2c3SqQdO9f3Fgi4awIqpoRUwfO1gVMNfo,1521 | ||
| scriptconfig/cli.py,sha256=Rm5IBIgRiTp_sJqm2QjSKt_YpCsUalsm_DMZ7LczWwE,881 | ||
| scriptconfig/cli.pyi,sha256=JjGmHDwl1b5rcYK625DrHwhZqh_7lRMd9gEUoRGayaM,98 | ||
| scriptconfig/config.py,sha256=tcCf400JDx41jQyRowmIRPb-CEt3SobgRXRpS0Bg_T0,69472 | ||
| scriptconfig/config.pyi,sha256=g4mse2QuPKMrcZ8GyoPwILE8f-5rZIs6TTIrIL3flfA,2888 | ||
| scriptconfig/dataconfig.py,sha256=7YCYOI3mrtFwf2_VCn3Sum9GVCUm79V9isFCsZ-orzM,18411 | ||
| scriptconfig/dataconfig.pyi,sha256=RWYX9VxBp8MctvMKr7e_hzoCfs2MJE38bDRGbxc53Mc,1230 | ||
| scriptconfig/dict_like.py,sha256=6_3epaacLGQqTpIrPic_U4FSUstZsWqdHH9mnIJcHT0,3588 | ||
| scriptconfig/dict_like.pyi,sha256=fzSQUa4Pqy-3c-r7tGS7n7mElGqH7hJpEAJX-Eso3Wg,1002 | ||
| scriptconfig/file_like.py,sha256=EGpfwMMmFtbv4iRcqLmKU1ebgkpLhN08Oid_ho0z0SQ,1162 | ||
| scriptconfig/file_like.pyi,sha256=7wiTB0ApSlXo4BE3UWUj28vWQofvh1SdTa0NsNB4fKo,238 | ||
| scriptconfig/modal.py,sha256=pZN9CzaAugA69hhMohpb_d1fXYiJDyi1H5yYp4n5wDM,16456 | ||
| scriptconfig/modal.pyi,sha256=qduXYNdhroxbLTTZaWu0Mc4AEUc5q2zUOngR062ORoc,860 | ||
| scriptconfig/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0 | ||
| scriptconfig/smartcast.py,sha256=iDPoiy4qFWZECsKeicTu5rvqSw2mDgbPN09TWDY2X58,8874 | ||
| scriptconfig/smartcast.pyi,sha256=_FiA9HL-5_5S7aowX0WmSG8RsoaV7FwjK8PUqu03jIo,229 | ||
| scriptconfig/value.py,sha256=4kYlCoXjxTPUhJs5JjZjgnovlb0jkSC1p0MbmoRw0mw,19691 | ||
| scriptconfig/value.pyi,sha256=A1jSnr494hMNyNuLRA8YnDGvP49Njwx8VLmb9W8Qhrk,1870 | ||
| scriptconfig/util/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0 | ||
| scriptconfig/util/util_class.py,sha256=VKuHlGSUtM-8PgirgszK830YFBEjub141rC9WNguYew,3571 | ||
| scriptconfig/util/util_class.pyi,sha256=EKMEujeTZSGT6G37IXOlfGlEAOQL-3vFxfDNxvy8LyQ,157 | ||
| scriptconfig/util/util_exception.py,sha256=M8NugeKRp9ti2sgtmumqNPfeEWwG66W-qBZYUms5N2k,1068 | ||
| scriptconfig-0.7.15.dist-info/LICENSE,sha256=o6jcFk_bwjiPUz6vHK0Ju7RwbFp9eXMwAS2BDnwER-4,11343 | ||
| scriptconfig-0.7.15.dist-info/METADATA,sha256=ZgtjV2RWoJeXE8REPXw5aVHcjJpXUGsyKvh022_SCek,41518 | ||
| scriptconfig-0.7.15.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92 | ||
| scriptconfig-0.7.15.dist-info/top_level.txt,sha256=lT9CMmcZkxEHt4wUGNDG0fNJvpBNYyfrW1CG3x3zyC4,13 | ||
| scriptconfig-0.7.15.dist-info/RECORD,, |
| scriptconfig |
| Wheel-Version: 1.0 | ||
| Generator: bdist_wheel (0.43.0) | ||
| Root-Is-Purelib: true | ||
| Tag: py3-none-any | ||
Sorry, the diff of this file is too big to display
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
367979
Infinity%59
Infinity%5609
Infinity%