router bgp ${BGP_AS}
network ${get_subnet(LAN_IP)} mask ${get_netmask(LAN_IP)}<%def name="get_netmask(ip_string)"><%
import ipaddress
return ipaddress.IPv4Interface(ip_string).netmask
%></%def>
<%def name="get_address(ip_string)"><%
import ipaddress
return ipaddress.IPv4Interface(ip_string).ip
%></%def>
<%def name="get_network(ip_string)"><%
import ipaddress
return ipaddress.IPv4Interface(ip_string).network
%></%def>
${get_address(LAN_IP)}
${get_address(${LAN_IP})}<%!
## python module-level code
import ipaddress
%>
<%def name="get_address(LAN_IP)">
<%
return ipaddress.IPv4Interface(LAN_IP).ip
%>
</%def>
${get_address(LAN_IP)}
<%!
## python module-level code
import ipaddress
%>
<%def name="get_address(LAN_IP)">
<%
return ipaddress.IPv4Interface(LAN_IP).ip
%>
</%def> <%!
## python module-level code
import ipaddress
%>
<%def name="get_address(ip_string)">
<%
return ipaddress.IPv4Interface(ip_string).ip
%>
</%def>
! Variable Input: ${LAN_IP}
${get_address(LAN_IP)}
<%!
## python module-level code
import ipaddress
%>
<%def name="get_address(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).ip
except Exception:
return "FAIL_OR_EMPTY"
%>
</%def>
<%def name="get_mask(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).netmask
except Exception:
return "FAIL_OR_EMPTY"
%>
</%def>
<%def name="get_hostmask(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).hostmask
except Exception:
return "FAIL_OR_EMPTY"
%>
</%def>
<%def name="get_net(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).network.network_address
except Exception:
return "FAIL_OR_EMPTY"
%>
</%def>
<%def name="get_network(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).network
except Exception:
return "FAIL_OR_EMPTY"
%>
</%def>
#### DEFINATIONS END ####
ip address ${get_address(LAN_IP)} ${get_mask(LAN_IP)}
ip address ${get_address(WAN_IP)} ${get_mask(WAN_IP)}ip address
10.117.137.171
255.255.255.0
ip address
10.200.2.67
255.255.255.192<%def name="get_network(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).network
except Exception:
return "FAIL_OR_EMPTY"
%>
</%def>
Monday, October 30, 2017 6:24 PM
Option 3 seems to work. Thanks again!I will bundle this snippet with the rest and have another go at it. I owe you one!
On Monday, October 30, 2017 at 1:39:38 PM UTC-5, Jonathan Vanasco wrote:
--
You received this message because you are subscribed to a topic in the Google Groups "Mako Templates for Python" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mako-discuss/usUBe2g3ykY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mako-discuss...@googlegroups.com.
To post to this group, send email to mako-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/mako-discuss.
For more options, visit https://groups.google.com/d/optout.
Monday, October 30, 2017 1:39 PM
I'm sorry, I didn't have coffee this morning and just realized I forgot Option3, which is probably the easiest in your situation --since your code is just Python code - and not about printing HTML - you can just define the functions in a python block. They are part of the modules namespace and won't run into whitespace issues.
so it would look like this:<%!
## python module-level code
import ipaddress
def get_address(ip_string):
try:
return ipaddress.IPv4Interface(ip_string).ip
except Exception:
return "FAIL_OR_EMPTY"
%>
--
You received this message because you are subscribed to a topic in the Google Groups "Mako Templates for Python" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mako-discuss/usUBe2g3ykY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mako-discuss...@googlegroups.com.
To post to this group, send email to mako-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/mako-discuss.
For more options, visit https://groups.google.com/d/optout.
Monday, October 30, 2017 11:42 AM
The whitespace in your output is because of the whitespace in the defs.
Option 1:Since you have VERY simple defs here, it may be better to write them as python functions and inject them into the templates. But you're using a framework which may not allow that, so...
Option2:I think this should work::
<%def name="get_network(ip_string)"><%
try:
return ipaddress.IPv4Interface(ip_string).
--network
except Exception:
return "FAIL_OR_EMPTY"
%></%def>
You received this message because you are subscribed to a topic in the Google Groups "Mako Templates for Python" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mako-discuss/usUBe2g3ykY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mako-discuss...@googlegroups.com.
To post to this group, send email to mako-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/mako-discuss.
For more options, visit https://groups.google.com/d/optout.
Sunday, October 29, 2017 9:41 PM
One more issue but it may be something obvious I am missing. The template is working but the referenced opjects show up on a new line. I tried using a "\" and a "\\" to strip the new line but that did not work (I got a \ or \\ in the output). Here is the template:
<%!
## python module-level code
import
ipaddress
%>
<%def name="get_address(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).ip
except Exception:
return "FAIL_OR_EMPTY"
%>
</%def>
<%def name="get_mask(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).
netmask
except Exception:
return "FAIL_OR_EMPTY"
%>
</%def>
<%def name="get_hostmask(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).
hostmask
except Exception:
return "FAIL_OR_EMPTY"
%>
</%def>
<%def name="get_net(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).network.
network_address
except Exception:
return "FAIL_OR_EMPTY"
%>
</%def>
<%def name="get_network(ip_string)">
<%
try:
return ipaddress.IPv4Interface(ip_string).
network
except Exception:
return "FAIL_OR_EMPTY"
%>
</%def>
#### DEFINATIONS END ####
ip address ${get_address(LAN_IP)} ${get_mask(LAN_IP)}
ip address ${get_address(WAN_IP)} ${get_mask(WAN_IP)}
and here is the output:ip address 10.117.137.171 255.255.255.0 ip address 10.200.2.67 255.255.255.192
The big bit of whitespace before the output is a clue that something is amiss. I would like the "ip address" IP and subnet to be on the same line. Any ideas?On Thursday, October 19, 2017 at 11:48:03 AM UTC-5, Daniel Flick wrote:
--
You received this message because you are subscribed to a topic in the Google Groups "Mako Templates for Python" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mako-discuss/usUBe2g3ykY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mako-discuss...@googlegroups.com.
To post to this group, send email to mako-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/mako-discuss.
For more options, visit https://groups.google.com/d/optout.
Thursday, October 19, 2017 11:48 AM
The problem was with the validation code. Within the python section of the template, the class IPv4Interface will throw an exception due to the invalid value during the validation process. Therefore, the server rejects the form data and the template is not created.Solution: It would work if you add an error handling to the python section of the template.
<%!## python module-level codeimport ipaddress
%><%def name="get_address(ip_string)"><%
try:return ipaddress.IPv4Interface(ip_string).ipexcept Exception:return "FAIL_OR_EMPTY"%>
</%def>! Variable Input: ${LAN_IP}${get_address(LAN_IP)}Explanation: During the server-side validation of the HTML form, the configuration template is rendered with a dummy parameter set to verify the syntax (see file app/forms.py, class ConfigTemplateForm). Your config template is validated with the following parameter set during the form validation:{"hostname": "test","LAN_IP": "test"}
On Tuesday, October 17, 2017 at 8:35:45 PM UTC-5, Daniel Flick wrote:
--
You received this message because you are subscribed to a topic in the Google Groups "Mako Templates for Python" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mako-discuss/usUBe2g3ykY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mako-discuss...@googlegroups.com.
To post to this group, send email to mako-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/mako-discuss.
For more options, visit https://groups.google.com/d/optout.