Skip to content

Instantly share code, notes, and snippets.

@blaggacao
Last active May 13, 2021 23:53
Show Gist options
  • Save blaggacao/e7ba05c7b4aa36317046ceb843cbed9f to your computer and use it in GitHub Desktop.
Save blaggacao/e7ba05c7b4aa36317046ceb843cbed9f to your computer and use it in GitHub Desktop.
Odoo Studio to Python XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:foo="http://www.foo.org/" xmlns:bar="http://www.bar.org">
<xsl:template name="printIndented">
<xsl:param name="text" />
<xsl:param name="indent" />
<xsl:if test="$text">
<xsl:value-of select="$indent" />
<xsl:variable name="thisLine" select="substring-before($text, '&#10;')" />
<xsl:choose>
<xsl:when test="$thisLine"><!-- $text contains at least one newline -->
<!-- print this line -->
<xsl:value-of select="concat($thisLine, '&#10;')" />
<!-- and recurse to process the rest -->
<xsl:call-template name="printIndented">
<xsl:with-param name="text" select="substring-after($text, '&#10;')" />
<xsl:with-param name="indent" select="$indent" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="$text = '' or $replace = ''or not($replace)" >
<!-- Prevent this routine from hanging -->
<xsl:value-of select="$text" />
</xsl:when>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="Upper">ABCDEFGHIJKLMNOPQRSTUVQXYZ</xsl:variable>
<xsl:variable name="Lower">abcdefghijklmnopqrstuvwxyz</xsl:variable>
&#160;&#160;&#160;&#160;# Python Code, please review and adapt
<xsl:for-each select="odoo/record">
&#160;&#160;&#160;&#160;<xsl:value-of select="substring(field[@name='name'],10,string-length(field[@name='name']))"/> = fields.<xsl:value-of select="translate(substring(field[@name='ttype'],1,1),$Lower,$Upper)"/><xsl:value-of select="substring(field[@name='ttype'],2,string-length(field[@name='ttype']))"/>(<xsl:if test="field[@name='relation'] != ''">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;comodel_name="<xsl:value-of select="field[@name='relation']"/>",</xsl:if>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;string="<xsl:value-of select="field[@name='field_description']"/>",
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;domain="<xsl:value-of select="field[@name='domain']"/>",
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;copied=<xsl:value-of select="field[@name='copied']/@eval"/>,
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;groups=<xsl:value-of select="field[@name='groups']/@eval"/>,<xsl:if test="field[@name='index']/@eval != 'False'">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;index=<xsl:value-of select="field[@name='index']/@eval"/>,</xsl:if><xsl:if test="field[@name='readonly']/@eval != 'False'">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;readonly=<xsl:value-of select="field[@name='readonly']/@eval"/>,</xsl:if><xsl:if test="field[@name='required']/@eval != 'False'">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;required=<xsl:value-of select="field[@name='required']/@eval"/>,</xsl:if><xsl:if test="field[@name='selection']/@eval != 'False'">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;selection=<xsl:value-of select="field[@name='selection']"/>,</xsl:if><xsl:if test="field[@name='store']/@eval != 'False'">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;store=<xsl:value-of select="field[@name='store']/@eval"/>,</xsl:if><xsl:if test="field[@name='track_visibility']/@eval != 'False'">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;track_visibility=<xsl:value-of select="field[@name='track_visibility']/@eval"/>,</xsl:if><xsl:if test="field[@name='track_visibility']/@eval != 'False'">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;translate=<xsl:value-of select="field[@name='translate']/@eval"/>,</xsl:if><xsl:if test="field[@name='related']/@eval != 'False'">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;related="<xsl:value-of select="field[@name='related']"/>",</xsl:if><xsl:if test="field[@name='compute'] != ''">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;compute="_compute_<xsl:value-of select="substring(field[@name='name'],10,string-length(field[@name='name']))"/>",</xsl:if><xsl:if test="field[@name='help'] != ''">
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;help="""<xsl:value-of select="field[@name='help']"/>""",</xsl:if>
)
<xsl:if test="field[@name='compute'] != ''">
def _compute_<xsl:value-of select="substring(field[@name='name'],10,string-length(field[@name='name']))"/>(self):
<xsl:variable name="without-studio-prefix">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="field[@name='compute']" />
<xsl:with-param name="replace">x_studio_</xsl:with-param>
<xsl:with-param name="by"></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="printIndented">
<xsl:with-param name="indent">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;</xsl:with-param>
<xsl:with-param name="text"
><xsl:value-of select="$without-studio-prefix" />
</xsl:with-param>
</xsl:call-template>
<xsl:text>&#xa;</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment