birchb1024 / yamp

Yet Another Macro Processor - for YAML - Superseded by Goyamp
GNU General Public License v3.0
13 stars 3 forks source link

import mechanism for python_eval #12

Open lgfausak opened 5 years ago

lgfausak commented 5 years ago

While working on a ansible deployment I often find myself needing ip manipulation. I have modified the version of yamp I get by adding this to the top:

import ipaddress

This is super handy for me because I can use it in python_eval, e.g.

- defmacro:
    name: CIDR4-OFFSET
    args: [ NET, INC ]
    value: { python_eval: 'str(ipaddress.IPv4Network(unicode(NET)).network_address + INC)' }

I pass this macro an ip cidr address (e.g. 10.20.30.128/25) and an offset (like 5) and the result is 10.20.30.133. I use this all the time. It might even make sense to have a built in for ip address manipulation.

birchb1024 commented 5 years ago

How about a way to add your own builtins?

- defbuiltin:
  name: CIDR-OFFSET
  args: [$net, $inc]
  module: lgfausak_cidr
  function: cider_offset

This would python import the code specified. The code in lgfausak_cidr.py would contain a python function:

import ipaddress
def cider_offset(tree, args, bindings):

    return str(ipaddress.IPv4Network(unicode(args["$net"])).network_address + args["$inc"])   

The point being users could add their own Python code as required.

lgfausak commented 5 years ago

yup, that would do the trick.