Package: tempgen
Templated GeneratorTemplated generator is a utility, that allows users generate files from templates with embedded template entries that could be edited and automatically transformed, saving time that would be otherwise spend on manually editing generic documents.The project includes python module, available on pypi, for use inside python scripts, cli and gui applications to edit template entries and quickly generate documents.Template entriesGenerally, template entries are stringified and enclosed in double curly brackets valid JSON objects. Here is an example:{{{"id": "total", "title": "Total cost", "value": "2500", "post": [{"fn": "append", "args": [" U.S. Dollars"]}], "group": "primary", "order": "1"}}}Minimal template entry contains id and value properties. Entry ids can be not unique and for each entry, the value specified once as replacement will be substituted and transforms (for respective entry) will be applied. This allows to edit value associated with id encountered multiple times over multiple templates only once.
For instance loaded templates can contain:{{{"id": "total", "value": "2500", "post": [{"fn": "append", "args": [" U.S. Dollars"]}]}}}
{{{"id": "total", "value": "2500", "post": [{"fn": "append", "args": ["$"]}]}}}
{{{"id": "total", "value": "2500"}}}In GUI application all three will be represented by one input with default value of "2500". Given replacements dictionary (for cli and using as module) of { "total": "42" }, or if input in this field equals "42" (for gui application), these will be represented in generated documents as42 U.S. Dollars
42$
42Entry object propertiesproperty nametyperequireddescriptionidstringyesid used to reference entry in replacementstitlestringnohuman-readable title that will be used to represent entry field in tempgen guivaluestringyesdefault value that will be used as replacement if no changes are madeprearray of objectsnoarray of transform objects that will be applied to value property before it becomes available for editingpostarray of objectsnoarray of transform objects that will be applied to value property on generated document savegroupstringnogroup tag name, entries with equal group property value will be grouped together in tempgen guiorderstringnoorder number, used to determine entry position among other group entries in tempgen gui, entries with lower order value will appear earlierautocompleteobjectnospecifies which suggestions should be displayed on user input in entry representation in tempgen_guiTransform object propertiesTransforms available by default are listedhere.
These are functions, that are applied on "value" property sequentially eitherwhen template is loaded, if transform is in "pre" listwhen generated document is being saved, if transform is in "post" listTransforms are provided with value (from entry) automatically, additional arguments could be provided via "args" array.Note: When using as a module, one can easily modify the dictionary by changing tempgen instance 'transforms' attributeproperty nametyperequireddescriptionfnstringyesname of transform functionargsarray of stringsnohuman-readable title that will be used to represent entry field in tempgen guiAutocomplete object propertiesproperty nametyperequireddescriptionexternalstringnofull name of json file (must be in the same directory as the template it is referenced from) containing array of autocomplete suggestion strings, used to fill data propertydataarray of stringsnolist of values that will be used in as suggestions for entry value in tempgen guiGetting StartedTempgen GUI/CLI standalone executablesDownload latest tempgen_gui/tempgen_clireleaseUnpack archive to any directoryRun tempgen_gui/tempgen_cli binaryInstallation as moduleTo install frompypi:pip install tempgenUsage as modulefromtempgen.moduleimportTempgent=Tempgen()# create new independent tempgen instancet.transforms.keys()# get list of supported transforms, you can add new on the fly, by setting t.transforms dictionary key to desired function name and value to this function# dict_keys(['append', 'inverted_date', 'ru_date_month_as_string_year', 'ru_monetary_string_replace', 'ru_monetary_as_string', 'ru_monetary_ending_append'])t.parsers.keys()# get list of supported file extensions, refer to documentation on how to add new extensions support# dict_keys(['.docx', '.xlsx', '.md', '.txt', '.odt', '.ods'])t.load_template(absolute_path_to_template)# parse templatet.get_templates()# get list of loaded templatest.get_fields()# get reference to parsed entriest.save_result(absolute_path_to_template,target_name_without_extension,{"id_from_template_text":"desired value"})Supported extensionsdocxxlsxmdtxtodtodsDevelopmentEnvironment setupInstall Python 3.7+InstallvirtualenvpipinstallvirtualenvClone this projectFrom project directory, runvirtualenv.envNote: This will create a virtual environment using the Python version
thatvirtualenvwas run with (which will be the version it was installed
with). To use a specific Python version, run:virtualenv--python=<path_to_other_python_version>.env# For example, this might look likevirtualenv--python=/usr/bin/python3.6.envAssuming you are using thebashshell, run:source.env/bin/activateFor other shells, see the otheractivate.*scripts in the.env/bin/directory. If you are on Windows, run:.env\Scripts\activate.batInstall all of the required packages usingpipinstall-rrequirements.txtModule/cli/gui code runningWith virtual environment active, execute one of the following commands fromsrcproject directory:python-mtempgen
python-mtempgen_cli
python-mtempgen_guiTestingThis project uses pytest and mostly relies on snapshot testing. To trigger tests runpytestoptionally with -vv and -s flags for verbosity and prints.Note: After each code modification that presumes expected generated output file changes, update snapshots by runningpytest--snapshot-updateCreating portable executablesThis project employs pyinstaller to create binaries. To generate executables from sources on your PC:Enter the virtual environment (runsource .env/bin/activateor OS/shell equivalent).Run the following command to create bundles with binaries for tempgen_cli and tempgen_gui in project's dist directorypythonpackage.pyGenerated archives will be placed inartifactsdirectoryPackaging moduleRun the following command to package tempgen module:python-mpipinstall--upgradebuild
python-mbuildGenerated archive and .whl package will be placed indistdirectory.Generating documentationRun the following command to generate updated documentation from collected docstrings and place it indocsdirectory:pdocsrc\tempgen--output-dirdocs-dnumpyContributionsPR are always welcome!