Make BaseFormat more robust #138
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
BaseFormat
only registers formatter classes in a 1D dictionary. Although, the 1D dictionary serves its purpose by letting built-in formatter classes (JSONFormat
,YAMLFormat
,CSVFormat
) use/modify it, but once a plugin uses theBaseFormat
dictionary as well by defining its own formatter classes, and once the plugin is loaded at elAPI runtime, the dictionary is inadvertently modified by the plugin, and is modified for all for good. Because of this reason, we couldn't useBaseFormat.supported_formatter_names
for CLI documentation indoc.py
, and used hard-coded strings instead.With this PR,
BaseFormat
will register formatter classes in a 2D dictionary that is package-aware. We introducepackage_identifier
attribute toBaseFormat
and__PACKAGE_IDENTIFIER__
forsrc/styles
sub-package.Internal plugins and third-party plugins should define their own formatter classes under a package/sub-package so Python magic method
__package__
* is notNone
or an empty string, and it can be passed topackage_identifier
.BaseFormat
will usepackage_identifier
as a key in_registry
soNEWFormat
can be stored in a sub-dictionary ofpackage_identifier
.BaseFormat
will populatepackage_identifier
first with built-in formatter classes.BaseFormat
will override an existing formatter class found in thepackage_identifier
sub-dictionary if the new formatter class'spattern
matches.None
toname
attribute. As a precaution, assigningelapi.styles.__PACKAGE_IDENTIFIER__
topackage_identifier
whilename == None
will throwValueError
so as to not let the developers remove built-in formatter classes. E.g.:It should be clear by now, to access the registered formatter classes,
Format
(along withHighlight
), newly renamedRegisterFormattingLanguage
andCLIFormat
must also be passed thepackage_identifier
. E.g.:When a formatter class is not part a package,
package_identifier
can be spoofed and made to work out the following way:Other changes
uhd-urz
optional dependencies (tenacity
,python-dateutil
) are bumped to latest versions.References
*: https://stackoverflow.com/a/48833828