python copy picture files based on xml files - python

I want to write a python script to rename pictures with a templated folder structure.
The script should go through a bunch of xml files and pictures and find matches between the picture and the a node in one of the xml files.
Here is a small excerpt from one of the xml files, how they could look like:
<Data Date="2023-01-27">
<CONT FROM="2517817" TILL="2516239" STATION_A="40" STATION_B="140" />
<Alocation ZENDI="020000" Class="G" Number="8095" Location="R" L="1" />
<Pictures>
<B Nr="1" D="B1_3U00_002163.jpg" Station="80" A="142" />
<B Nr="2" D="B2_3U00_002163.jpg" Station="80" A="142" />
<B Nr="3" D="B3_3U00_002163.jpg" Station="80" A="142" />
</Pictures>
</Data>
When I have a match with the picture "B1_3U00_002163.jpg" it should copy this picture like that:
c:\temp\pictures\G8095\2517817_2516239_100_40_140\27.01.2023\placeholder\R\1\80.jpg
visualization of my task
and the other pictures files should be copied like so:
c:\temp\pictures\G8095\2517817_2516239_100_40_140\27.01.2023\placeholder\R\2\80.jpg
c:\temp\pictures\G8095\2517817_2516239_100_40_140\27.01.2023\placeholder\R\3\80.jpg
Do you have recommendations/tips how to start this project? What libraries can i use for this? Any helpful links?
So far im in the process of collecting ideas. I don't expect anything, I'm greatful for any help.

Related

how to edit xml root attributes with python

Recently ive been messing around with editing xml files with a python script for a project im working on but i cant figure out how to edit the attributes of the root element.
for example the xml file would say:
<root width="200">
<element1>
</element1>
</root>
what i want to do is have my code find the width attribute and change it to some other value, i know how to edit elements after the root but not the root itself
code im using for changing attributes
You could use the following module xml.etree.ElementTree. With this module you can set up attributes using xml.etree.ElementTree.Element.set()
Here is an example of snippet you could use:
import xml.etree.ElementTree as ET
tree = ET.parse('input.xml')
root = tree.getroot()
root.set('width','400')
print(root.attrib)
tree.write('output.xml')

Adding a subElement into a subElement in xml with python

I am working with xml files in python, and I want to ask if there is a way to add a subelement into an other subelement into the xml file.
If for the example, the structure of the xml file is as follow, and I want to add a new subelement under the container 'b'. how can I do it ?
<?xml version .....>
<module name=....>
<augment ....>
<container name="a">
</container>
<container name="b">
</container>
</augment>
</module>
If you want to do it in more future-proof way, you may want to use some kind of xml parser, i.e. lxml.etree. You could parse your xml, work on its elements and eventually dump it later back to a file. There is simple working example:
from lxml import etree
xml = '''<module name="x">
<augment name="y">
<container name="a">
</container>
<container name="b">
</container>
</augment>
</module>'''.strip()
xtree = etree.fromstring(xml)
for element in xtree.xpath('.//container[#name="b"]'):
new = etree.Element('something') # create new element to be inserted
new.set('name', 'xyz') # define some attributes for new element
element.append(new) # append it to your currently-processed element
print(etree.tostring(xtree,pretty_print=True).decode('ascii'))
For more see lxml documentation (https://lxml.de/tutorial.html)

Python Script has no effect on Apache Ant Build

I have an ant build file: build.xml, with an existing build target: Rebuild-ALL. that I run from ant eclipse plugin.
The build file contains paths referenced from another file: file.def.
I would like to create a new build target that builds from another path.
I started by doing the following:
Creating a python script that modifies the path in file.def.
I added this script to a target: Change-Paths.
I created a copy of the existing build target: Copy1-Rebuild-ALL
I added my Change-Paths target to the depends of the Copy-Rebuild-ALL.
I also created a second python script to revert the path change in file.def and repeated steps 2 to 4 to have another 2 targets: Revert-Paths and Copy2-Rebuild-ALL.
The problem is that the second build target still takes the path of the first build target even though I can see on my editor that the python script has successfully replaced the paths.
After executing the second target 3 consecutive times it works fine so is there maybe a refresh or a delay I can add to fix this ?
I tried using ant calls instead of depends but still the same issue occurs.
I am still new to ant and build files in general so let me know if there's a better way to do this.
Here is a an example to my build.xml file:
<project name="Build" xmlns:if="ant:if" xmlns:unless="ant:unless" default="REBUILD_ALL" basedir="${ant.file}/../../../..">
<property file="${TOOLS.PATH}/Compiler/file.def" />
<property name="COMPILER" value="${VENDOR.COMPILER}" />
<target name="Change-Paths">
<exec executable="${python.exe}" dir="${basedir}" failonerror="true" inputstring="${TOOLS.PATH}/Compiler/file.def">
<arg line="${SCRIPTS.PATH}/Change_Paths.py" />
</exec>
</target>
<target name="Copy1-Rebuild-ALL" description="Build all files from the project to output executable files" depends="Change-Paths, BuildMakesParallel, LINK_ALL">
</target>
<target name="Revert-Paths">
<exec executable="${python.exe}" dir="${basedir}" failonerror="true" inputstring="${TOOLS.PATH}/Compiler/file.def">
<arg line="${SCRIPTS.PATH}/Revert_Paths.py" />
</exec>
</target>
<target name="Copy2-Rebuild-ALL" description="Build all files from the project to output executable files" depends="Revert-Paths, BuildMakesParallel, LINK_ALL">
</target>
</project>
Also here is an example to file.def:
VENDOR_PATH = c:/VENDOR/toolchains/compiler_name/v1
VENDOR.COMPILER = ${VENDOR_PATH}/bin/compiler_name-gcc.exe
I permanently added the new path in file.def and added a new property in build.xml to hold this path as follows:
VENDOR_PATH_2 = c:/VENDOR/toolchains/compiler_name/v2
VENDOR.COMPILER_2 = ${VENDOR_PATH_2}/bin/compiler_name-gcc.exe
Now the python script will only replace the property name in the macro that compiles files in the build target and it works as expected.

Jenkins reporting from XML file

this is my first post so sorry if there exists answer for a problem I 've got, but I think it is very specific.
My teammates created a very simple testing environment which tests functions in cpp project and it produces report of launched testcases to txt file which looks like this:
0 testcase1 PASS
1 testcase2 PASS
2 testcase2 FAIL
and so on
My task is to prepare an xml file from this txt and put it to Jenkins to generate pretty charts with test run.
Currently I try to parse this txt to JUnitXml file generated by Python lxml library.
Xml file from this txt looks like this:
xml
When I put it to Jenkins it produces me only result of tests which were run, in this case all tests were launched.
What am I missing in my XML file?
Do I need any external library to our test environment which would produce better xml with information about passed, failed testcases?
Cheers.
Here is what you should find in your xml report according to your report :
<testsuite skip="0" failures="1" errors="0" tests="3" name="Name of the tests">
<testcase name="testcase1" classname="This is a name to identify in which class is located the test"/>
<testcase name="testcase2" classname="This is another(or not) name to identify in which class is located the test"/>
<testcase name="testcase3" classname="This is another(or not) name to identify in which class is located the test">
<failure message="Any message of the failing output" type="You can specify the kind of error">
<![CDATA[ "Here you can put a complete stacktrace or
any log message associated with your failure" ]]>
</failure>
</testcase>
</testsuite>
You can find the description (Schema) of the JUnit XML format here

How put baseurl as a parametrs in selenium tests

I have 1 file with selenium tests, with baseURL in it. So now i need to find a way to launch this tests from console with URL as a parametr.
I have one local, developer and production server, so i want to run this tests without hardcode baseURL in it.
Is anyone one have some idea for this?
Waiting for your answers :3
Yes, you can create one Environment file any format would do (.xml, .xlsx, .properties etc).
Then you just mention environment specific details and parse the file. Its better if you use .xml format. In this way you can create node like configuration with parent being the environment name. In this way you can add any number of configuration for different servers.
Something like:-
XML Format:-
<environments>
<environment id="local">
<parameter>
<name>host</name>
<value>LocalURL</value>
</parameter>
</environment>
<environment id="development">
<parameter>
<name>host</name>
<value>DevelopmentURL</value>
</parameter>
</environment>
<environment id="production">
<parameter>
<name>host</name>
<value>ProductionURL</value>
</parameter>
</environment>
</environments>
Properties File Format:-
localUrl = http://local.com
developmentUrl = http://development.com
productionUrl = http://production.com
Any format you can use and write a code to parse it.

Categories

Resources