Skip to main content

Build Status Coverage

Simple python templates

example:
  - key : %os.environ.get(USER)
  - roaming : %if(sys.platform=='win32',1,0)
  %indent(%include(file_name))
  - other : %eval(1 + 1)
  %indent(%python("
import mod
f = open('myfile.in')
f.read()
output(mod.process(f))
 ")

Allows simple macros to be expanded inline. You can from smx import Smx to evaluate, or evaluate from the command line. Options to import all env vars, or modules from the command line are available. Macros can be nested... so %expand(%include(...)) is a valid syntax.

Used for yml templates, config files, kubernates deployments, simple HTML pages, etc.

Install

pip install smx

Use

   > smx file.in > file.out
   > smx --help

Or from python:

   from smx import Smx
   ctx = Smx()
   ctx.expand("%add(1,1)")
   ctx.expand_io(fin, fout)
   ctx.expand_file(filename, in_place=True)

Including code and files

Macro Description
indent(str) each line of the indented string is indented at the level where the indent function was called.
include(str) include the specified file
strip(str) strip a string
expand(str) string is expanded using smx syntax
python(str) string is expanded using python syntax
module(str) string is interpreted as a module and imported

Modules

Macro Description
os.... os functions are included by default, for example %os.path.basename(...)
sys.... sys functions are included by default EG: %sys.platform% can be used

Misc

Macro Description
for(name, range, loop) loop code is expanded for each value in the range
if(val, true-val, false-val) if val is expanded to non-empty, true-val is executed
add(a, b) numbers are added
sub(a, b) numbers are subtracted

Wsgi

Smx includes an wsgi module. The goal is to be able to easily serve template driven pages using smx syntax.

Goals

  • The syntax should be "macroy" not "pythony" ... that way you can tell, at a glance when there's macros going on... vs python going on.
  • Easy to add your own macros by deriving from Smx and adding new functions with the @smx.macro decorator.
  • Easy to import python modules and use them in basically any string context
  • JSON and YAML template friendly
  • Use "as is" in most configuration contexts
  • Unsafe by default, but trivial to use "Safe mode" allowing untrusted execution of a strict set of macro expansions

Caveats

  • Important to remember that all macros result in "strings", not other python types.
  • When context-oriented template programming gets complex, you probably shouldn't be using templates.

Metadata

Release files for smx 0.9.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for smx 0.9.5
File Size Uploaded
smx-0.9.5.tar.gz 15.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for smx 0.9.5
File Interpreter ABI Platform
smx-0.9.5-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 52.8 kB

Release files / smx-0.9.5.tar.gz

Download URL smx-0.9.5.tar.gz
Size 15.2 kB
Tags Source
SHA-256 checksum
How to use checksums
61f415e50c68339d99f943b1c397a3cf7891e6de55519ec7e4718c0816561327
BLAKE2b-256 checksum
How to use checksums
e7c5d71c604b3245f717c2473d338eae6f5350d1d4365400064f0d5e767395e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.21.0

Release files / smx-0.9.5-py2.py3-none-any.whl

Download URL smx-0.9.5-py2.py3-none-any.whl
Size 37.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
6edd5e81828df590f17473d3cb7a18895eb466fa0a070dafdbb7db34de95695c
BLAKE2b-256 checksum
How to use checksums
409446b2d07ce5c28916fefecc80335cf0bdf4cfcd9a6e7379aa5216be4eb6f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.21.0

Release history Release notifications | RSS feed

This release

0.9.5 This release

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.0

1 release file

0.8.5

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page