Code generation from jinja2 ast - parsing

Let's say I have a jinja template
<style>
p{
font-family : 'Times New Roman',Times,serif;
font-size:15px;
}
</style>
<p>------- Information -------- </p>
{% set p1 = getP1('dataset') %}
<table style='width:100%'>
<tr>
<th>Information</th>
</tr>
<tr>
<td>A</td>
<td>{{p1.a}}</td>
</tr>
<tr>
<td>B</td>
<td>{{p1.b}}</td>
</tr>
<tr>
<td>C</td>
<td>{{p1.c}}</td>
</tr>
</table>
I can parse the code with jinja2 to the jinja2 Template, where the code is the string which I pasted above.
In [58]: from jinja2 import Environment
In [59]: env = Environment()
In [60]: m = env.parse(code)
In [61]: m
Out[61]: Template(body=[Output(nodes=[TemplateData(data=u"\n<style>\np{\n\tfont-family : 'Times New Roman',Times,serif;\n\tfont-size:15px;\n}\n</style>\n\n<p>------- Information -------- </p>\n\n")]), Assign(target=Name(name='p1', ctx='store'), node=Call(node=Name(name='getP1', ctx='load'), args=[Const(value=u'dataset')], kwargs=[], dyn_args=None, dyn_kwargs=None)), Output(nodes=[TemplateData(data=u"\n\n<table style='width:100%'>\n\t<tr>\n\t\t<th>Information</th>\n\t</tr>\n\t<tr>\n\t\t<td>A</td>\n\t\t<td>"), Getattr(node=Name(name='p1', ctx='load'), attr='a', ctx='load'), TemplateData(data=u'</td>\n\t</tr>\n\t<tr>\n\t\t<td>B</td>\n\t\t<td>'), Getattr(node=Name(name='p1', ctx='load'), attr='b', ctx='load'), TemplateData(data=u'</td>\n\t</tr>\n\t<tr>\n\t\t<td>C</td>\n\t\t<td>'), Getattr(node=Name(name='p1', ctx='load'), attr='c', ctx='load'), TemplateData(data=u'</td>\n\t</tr>\n</table>')])])
How can I convert back the template to the code?
In the case of python code, we have packages like astor.
According to this issue, jinja2 doesn't support that functionality ! so How can I write a code to do that?

Related

error when sending jelly template using email ext plugin with jenkins

I want to send the cppcheck report through email but I had never used the email ext plugin before, so I found this Sending Cppcheck result/report on email from Jenkins using email-ext plugin with a nice jelly template to do so, I have also followed the steps in the documentation, to send my own templates in a pipeline job, I have created my template with a .jelly extension and set it into my $JENKINS_HOME\email-templates although the email-templates folder did not exist, so I am not sure if it should be created beforehand just by installing the plugin or what I am missing. This is my stage
stage('Mail cppcheck report') {
steps{
script{
def mailRecipients = "email#email.net"
def jobName = currentBuild.fullDisplayName
mimeType: 'text/html'
emailext body: '''${JELLY_SCRIPT, template="custom"}''',
subject: "[Jenkins] ${jobName}",
to: "${mailRecipients}"
}
}
}
And this is the template I want to use but I keep getting the following error JellyException: Could not parse Jelly script : null even though the email is sent the email body is just the error mentioned above.
<j:set var="cppcheckAction" value="${it.getAction('org.jenkinsci.plugins.cppcheck.CppcheckBuildAction')}" />
<j:if test="${cppcheckAction!=null}">
<j:set var="cppcheckResult" value="${cppcheckAction.getResult()}" />
<j:if test="${cppcheckResult!=null}">
<TABLE width="100%">
<TR><TD class="bg1" colspan="2"><B>CPPCHECK RESULT</B></TD></TR>
<TR bgcolor="white"><TD class="test_failed" colspan="2"><B><li>Found: ${cppcheckResult.report.getNumberTotal()}</li></B></TD></TR>
</TABLE>
<BR/>
</j:if>
</j:if>
I have also tried with some other templates and I get no error but I receive an empty email instead, like with this one.
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define">
<html>
<j:set var="cppcheckAction" value="${it.getAction('org.jenkinsci.plugins.cppcheck.CppcheckBuildAction')}" />
<j:if test="${cppcheckAction!=null}">
<j:set var="cppcheckResult" value="${cppcheckAction.getResult()}" />
<j:if test="${cppcheckResult!=null}">
<h2>Summary</h2>
<style type="text/css">
#cppcheckStatistics { width: auto; }
#cppcheckStatistics .number { text-align: right; }
</style>
<table class="pane sortable" id="cppcheckStatistics">
<thead>
<tr>
<td class="pane-header">Severity</td>
<td class="pane-header">Count</td>
<td class="pane-header">Delta</td>
</tr>
</thead>
<tbody>
<tr>
<td class="pane">Error</td>
<td class="pane number">${cppcheckResult.statistics.getNumberErrorSeverity()}</td>
<td class="pane number">${cppcheckResult.getDiff().getNumberErrorSeverity()}</td>
</tr>
<tr>
<td class="pane">Warning</td>
<td class="pane number">${cppcheckResult.statistics.getNumberWarningSeverity()}</td>
<td class="pane number">${cppcheckResult.getDiff().getNumberWarningSeverity()}</td>
</tr>
<tr>
<td class="pane">Style</td>
<td class="pane number">${cppcheckResult.statistics.getNumberStyleSeverity()}</td>
<td class="pane number">${cppcheckResult.getDiff().getNumberStyleSeverity()}</td>
</tr>
<tr>
<td class="pane">Performance</td>
<td class="pane number">${cppcheckResult.statistics.getNumberPerformanceSeverity()}</td>
<td class="pane number">${cppcheckResult.getDiff().getNumberPerformanceSeverity()}</td>
</tr>
<tr>
<td class="pane">Portability</td>
<td class="pane number">${cppcheckResult.statistics.getNumberPortabilitySeverity()}</td>
<td class="pane number">${cppcheckResult.getDiff().getNumberPortabilitySeverity()}</td>
</tr>
<tr>
<td class="pane">Information</td>
<td class="pane number">${cppcheckResult.statistics.getNumberInformationSeverity()}</td>
<td class="pane number">${cppcheckResult.getDiff().getNumberInformationSeverity()}</td>
</tr>
<tr>
<td class="pane">No category</td>
<td class="pane number">${cppcheckResult.statistics.getNumberNoCategorySeverity()}</td>
<td class="pane number">${cppcheckResult.getDiff().getNumberNoCategorySeverity()}</td>
</tr>
</tbody>
<tfoot>
<tr class="sortbottom">
<td class="pane-header">Total</td>
<td class="pane-header number"><B>${cppcheckResult.report.getNumberTotal()}</B></td>
<td class="pane-header number"><B>${cppcheckResult.getDiff().getNumberTotal()}</B></td>
</tr>
</tfoot>
</table>
</j:if>
</j:if>
</html>
</j:jelly>
However if I use the default template emailext body: '''${JELLY_SCRIPT, template="html"}''' it works fine

How to retrieve column that is dynamically displayed in a table form

I would like to retrieve each row of a datatable that is dynamically created. I created a form with the post method to get these rows but I don't how what to write in my view function to have these in a panda DataFrame.
<form action="{%url 'livedb_model:validation' property.id roomtype.id %}?normal_price={{normal_price}}&week_day_multi={{week_day_multi}}&week_end_multi={{week_end_multi}}&max_discount={{max_discount}}&max_markup={{max_markup}}&coccCategory={{coccCategory}}&agressiveness={{agressiveness}}" method="post">
{% csrf_token %}
<input class='btn btn-primary btn-large btn-block align-self-center'type='submit' value='submit'>
<div id="datatable" class="table-editable align-self-stretch">
<table class="table table-bordered table-responsive-md table-striped text-center">
<tr>
<th class="text-center">Date</th>
<th class="text-center">Availability</th>
<th class="text-center">Current Price</th>
<th class="text-center">Current Occupancy</th>
<th class="text-center">Advised Price</th>
</tr>
{% for day in invdf.itertuples %}
<tr>
{% if day.availability.room %}
<td class="pt-3-half" id='date'>{{day.date|date:'M d,Y'}}</td>
<td class="pt-3-half">{{day.allotment}}</td>
<td class="pt-3-half">{{day.price|floatformat:2}}</td>
<td class="pt-3-half">{{day.occupancy|floatformat:2}}</td>
<td class="pt-3-half" id='price_advised' contenteditable="true">{{day.price_advised|floatformat:2}}</td>
{% else %}
<td class="pt-3-half" id='date'>{{day.date|date:'M d,Y'}}</td>
<td class="pt-3-half">{{day.allotment}}</td>
<td class="pt-3-half">{{day.price|floatformat:2}}</td>
<td class="pt-3-half">{{day.occupancy|floatformat:2}}</td>
<td class="pt-3-half" id='price_advised' contenteditable="true">{{day.price_advised|floatformat:2}}</td>
{% endif %}
</tr>
{% endfor%}
</div>
</form>
For instance I would like to write something like in my view:
def validation(request,id,rt_id):
if request.method=='POST':
price_advised=request.POST.get(ADVISED PRICE COLUMN)
Could you help me on this?
Thank you
you can use javascript add parameter before submitting form

Bind list of maps to a table in dart

I have defined the following data model in dart file:
#observable List alist = toObservable([{'rel':'self', 'name':'John'},
{'rel':'Father', 'name':'tom'},
{'rel':'Mother', 'name':'jane'}]);
and table in html as follows:
<table id="summaryTable">
<thead>
<tr>
<th>Name</th>
<th>Ralationship to Me</th>
<th>Update History</th>
<th>Remove Relative</th>
</tr>
</thead>
<tr repeat = "{{p in alist}}">
<td>
{{p['name']}}
</td>
<td>
{{p['rel']}}
</td>
<td>
<button on-click="{{updateClicked}}">Update</button>
</td>
<td>
</td>
</tr>
</table>
The table is not getting populated. What should be changed?
It's hard to tell as you didn't provide much code but at least the template is missing in
<tr template repeat="{{p in alist}}">

Need help: xpath query

i want to exclude the IMG Values in this table:
<table class="info">
<tbody>
<tr><th class="round-top" colspan="2">Status</th></tr>
<tr class="even">
<td class="key">Schaltprogramm aktiv</td>
<td class="value"><img height="15" src="./pics/ste-symbol_an-97b765.png"></td>
</tr>
</tbody>
</table>
I want to exclude the values in this table:
<table class="info">
<tbody>
<tr><th class="round-top" colspan="2">Warmwasser</th></tr>
<tr class="even">
<td class="key">WW-Isttemp.</td>
<td class="value">49,0 °C</td>
</tr>
<tr class="odd">
<td class="key round-leftbottom">WW-Solltemp.</td>
<td class="value round-rightbottom">46,5 °C</td>
</tr>
</tbody></table>
Here are my poorly test:
$nodelist = $xpath->query( "//tr/td[substring(#class,1,5)='value']" );
$imgs = $xpath->query("//table[#class='info']/tr/td/img");
What must i do to exclude the IMG Values in the variable "nodelist"?
Any idea?
I think (but am not certain) that you want $nodelist to contain all the td elements whose class attribute begins with the string value but not if they contain an img element.
If that's a correct interpretation of your question, change
$nodelist = $xpath->query( "//tr/td[substring(#class,1,5)='value']" );
to
$nodelist = $xpath->query( "//tr/td[substring(#class,1,5)='value'][not(img)]" );
I'm not clear what it is you want to do about the temperature values in your second sample table; you might want to edit the question to make clearer whether you want to retrieve them or not.

TCPDF, php variable values do not appear on the saved pdf file

I created a pdf using TCPDF and I filled it with some php variables and the current date using heredoc syntax
Everything is fine when the pdf is generated and viewed in the browser, but on the saved pdf file the php variable values are not shown, only the date...
I declare my variables at the top like so:
$name = $_POST['name'];
$score = $_POST['percentage'];
$ku_number = $_POST['ku-number'];
$date = Date('d - m - Y');
Only $date is shown on the saved pdf.
Any suggestion?
Thank you
Edit: code to pass the variables to TCPDF
// Print a text
$html = <<<EOF
<!-- EXAMPLE OF CSS STYLE -->
<style>
table{
text-align:center;
color:#000;
}
</style>
<table id="name" cellpadding="0">
<tr>
<td width="10" height="80"> </td>
<td width="620"> </td>
<td width="10"> </td>
</tr>
<tr>
<td> </td>
<td width="620">$name</td>
<td> </td>
</tr>
<tr>
<td width="10"> </td>
<td> </td>
<td width="10"> </td>
</tr>
</table>
<table id="score" cellpadding="0" >
<tr>
<td width="10" height="180"> </td>
<td width="620"> </td>
<td width="10"> </td>
</tr>
<tr>
<td> </td>
<td width="620">$score</td>
<td> </td>
</tr>
<tr>
<td width="10" height="207"> </td>
<td> </td>
<td width="10"> </td>
</tr>
</table>
<table id="ku-number" cellpadding="0" >
<tr height="2">
<td width="50" height="2"></td>
<td width="620" height="2"></td>
<td width="10" height="2"></td>
</tr>
<tr>
<td> </td>
<td width="620" height="20"></td>
<td> </td>
</tr>
<tr>
<td width="50"> </td>
<td>$ku_number</td>
<td > </td>
</tr>
</table>
<table id="date" cellpadding="0" >
<tr height="2">
<td width="50" height="2"></td>
<td width="620" height="2"></td>
<td width="10" height="2"></td>
</tr>
<tr>
<td> </td>
<td width="420" height="20">$date</td>
<td> </td>
</tr>
<tr>
<td width="50"> </td>
<td></td>
<td > </td>
</tr>
</table>
EOF;
$pdf->writeHTML($html, true, false, true, false, '');
//Close and output PDF document
$pdf->Output('certificate.pdf', 'I');
I had the same problem. I found a solution by experimenting things myself mentioned as follows:
Please use concatenation to break $html string into parts. This will surely solve the problem. e.g. I used something like this:
$html = 'HTML CONTENT BREAKS HERE' . $variable_name . 'HTML CONTENT CONTINUES HERE' ;
Normally, most developers will use PHP variable within $html value,
$html = 'HTML CONTENT echo php variable HTML CONTENT' ;
I hope this will work.
Using $_SESSION to store the variables before outputting the PDF solved the problem
Credits go to Yuri Stuken
For more refined results:
Outputting the final PDF:
When you’ve finished creating all required cells, images, links, text etc. you have to call the Output() method to actually get your hands on your dynamically created PDF. This can take no parameters in which case the PDF is sent to the browser, more commonly though, developers specify the filename and the destination of the generated PDF. The destination can be one of four values, these are:
I: send the file inline to the browser.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name.
S: return the document as a string.
You can see my code sets the destination value as F:
$pdf->Output(”./pdfs/example.pdf”, “F”);
referenced from:this
Have a bAlaNCeD Life !

Resources