From 7c0d6a295a1bf2f0cf394f547a64b869f92aa96d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20=C3=89vrard?= Date: Tue, 24 Feb 2026 22:39:38 +0100 Subject: [PATCH] Allow to specify some XML fields value as relative to the subdirectory path --- trytond/CHANGELOG | 1 + trytond/doc/topics/modules/index.rst | 4 ++++ trytond/trytond/convert.py | 14 ++++++++++++-- trytond/trytond/modules/__init__.py | 3 ++- trytond/trytond/tryton.rnc | 1 + 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/trytond/CHANGELOG b/trytond/CHANGELOG index 67b0c9a8b9..a63e33f94f 100644 --- a/trytond/CHANGELOG +++ b/trytond/CHANGELOG @@ -1,3 +1,4 @@ +* Add path attribute on XML fields to specfiy a relative path value * Allow to include subdirectories in tryton.cfg * Add route for login / logout with cookie * Add contextual ``_log`` to force logging events diff --git a/trytond/doc/topics/modules/index.rst b/trytond/doc/topics/modules/index.rst index 1a6fa95ce8..62c6e4d2fd 100644 --- a/trytond/doc/topics/modules/index.rst +++ b/trytond/doc/topics/modules/index.rst @@ -218,6 +218,10 @@ Here is the list of valid tags: ``pyson`` Convert the evaluated value into :ref:`PYSON ` string. + ``path`` + Prepend to the value the directory name of the path of the configuration + file importing the XML file. + ``depends`` Set value only if all modules in the comma separated module list value are activated. diff --git a/trytond/trytond/convert.py b/trytond/trytond/convert.py index e41ab3ec15..159a4c3785 100644 --- a/trytond/trytond/convert.py +++ b/trytond/trytond/convert.py @@ -2,6 +2,7 @@ # this repository contains the full copyright notices and license terms. import datetime import logging +import os.path import re import time from collections import defaultdict @@ -194,6 +195,7 @@ class RecordTagHandler: self.current_field = None self.cdata = None self.start_cdata = None + self.is_path_field = False def startElement(self, name, attributes): @@ -240,6 +242,7 @@ class RecordTagHandler: ref_attr = attributes.get('ref', '') eval_attr = attributes.get('eval', '') pyson_attr = bool(int(attributes.get('pyson', '0'))) + self.is_path_field = bool(int(attributes.get('path', '0'))) context = {} context['time'] = time @@ -310,6 +313,10 @@ class RecordTagHandler: self.values[self.current_field] = \ CDATA_END.sub('', self.values[self.current_field]) self.cdata = 'done' + if self.is_path_field: + self.values[self.current_field] = os.path.join( + self.mh.base_path, self.values[self.current_field]) + self.is_path_field = False self.current_field = None return self @@ -385,6 +392,7 @@ class TrytondXmlHandler(sax.handler.ContentHandler): self.skip_data = False self.modules = modules self.languages = languages + self.base_path = None # Tag handlders are used to delegate the processing self.taghandlerlist = { @@ -402,11 +410,11 @@ class TrytondXmlHandler(sax.handler.ContentHandler): self.sax_parser.setFeature(sax.handler.feature_namespaces, 0) self.sax_parser.setContentHandler(self) - def parse_xmlstream(self, stream): + def parse_xmlstream(self, stream, base_path): """ Take a byte stream has input and parse the xml content. """ - + self.base_path = base_path source = sax.InputSource() source.setByteStream(stream) @@ -415,6 +423,8 @@ class TrytondXmlHandler(sax.handler.ContentHandler): self.sax_parser.parse(source) except Exception as e: raise ParsingError("in %s" % self.current_state()) from e + + self.base_path = None return self.to_delete def startElement(self, name, attributes): diff --git a/trytond/trytond/modules/__init__.py b/trytond/trytond/modules/__init__.py index c505ac555e..15514f7252 100644 --- a/trytond/trytond/modules/__init__.py +++ b/trytond/trytond/modules/__init__.py @@ -315,7 +315,8 @@ def load_module_graph(graph, pool, update=None, lang=None, indexes=None): # Feed the parser with xml content: with tools.file_open( os.path.join(module, filename), 'rb') as fp: - tryton_parser.parse_xmlstream(fp) + tryton_parser.parse_xmlstream( + fp, base_path=os.path.dirname(filename)) modules_todo.append((module, list(tryton_parser.to_delete))) diff --git a/trytond/trytond/tryton.rnc b/trytond/trytond/tryton.rnc index 9df9a30dc5..3a12da3bf4 100644 --- a/trytond/trytond/tryton.rnc +++ b/trytond/trytond/tryton.rnc @@ -20,6 +20,7 @@ attlist.field &= attribute search { text }? attlist.field &= attribute ref { text }? attlist.field &= attribute eval { text }? attlist.field &= attribute pyson { "0" | "1" }? +attlist.field &= attribute path { "0" | "1" }? attlist.field &= attribute type { "xml" }? attlist.field &= attribute depends { text }? menuitem = element menuitem { attlist.menuitem, empty }