Skip to main content

Simple python macro expansion

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smx-0.9.3.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

smx-0.9.3-py2.py3-none-any.whl (36.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file smx-0.9.3.tar.gz.

File metadata

  • Download URL: smx-0.9.3.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.21.0

File hashes

Hashes for smx-0.9.3.tar.gz
Algorithm Hash digest
SHA256 a51133f4a92104234e93ed0e68acbbe61687f506e61055710fae10ec2111266c
MD5 85af6b1ff7062ed67adcd3b905b34831
BLAKE2b-256 29958e0ba9777859e95e8b32b114e89082ffa66b23cdebb25f09c0329622f0e4

See more details on using hashes here.

File details

Details for the file smx-0.9.3-py2.py3-none-any.whl.

File metadata

  • Download URL: smx-0.9.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 36.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.21.0

File hashes

Hashes for smx-0.9.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 56c174ef7008919bd7d9d09938828b75e24672eee1db4ef1ff2dd6b18355b5ed
MD5 b116f70c6275546d7d44d21bb03be69b
BLAKE2b-256 a8c2a3ac5223bdace5b1c4aec746a86bc61321cc474e34ecd6f765f76e6dfc62

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page