Topic: Kennzeichnung - der eigenen Bauteilsammlung - ein Tool (in Python)

Hallo,
Da mich die Bauteilekennzeichnung für meine eigenen Bauteil richtig nervt, habe ich die Prozedur jetzt automatisiert.
Es ist ja so das sich im Ordner der eigenen Bauteile (/home/user/.qet/elements ist das unter linux) die Datei qet_labels.xml befindet.
In dieser sind alle Unterordner aufgelistet und wenn es für die Ordner Bauteilekennzeichen geben soll, dann sind diese mit dem <prefix></prefix> Tag dort einzusetzen.
Es ist aber bei einer größeren Ordnerstruktur sehr mühsehlig, das Fehlerfrei einzugeben. Daher ein kleines Python  skript,  das im Verzeichnis /home/user/ die Datei Demo.xml anlegt, in der alle Unterordner schon eingetragen sind und ebenfalls ein das Prefix Tag.
Befindet sich in einem der Unterordner eine Datei mit dem Namen tagfile.txt wird deren erster Buchstabe als Prefix für die Dateien in diesem Ordner gesetzt.

Das ganze beschränkt sich also darauf, einen neuen Ordner in der Benutzersammlung zu erstellen (mit qelectrotech) und dann dort eine (Text-) Datei tagfile.txt (mit einem Editor) anzulegen, die einen Buchstaben enthält der für alle enthaltenen Dateien als Prefix gelten soll.
Danach wird das kleine Script hier gestartet und es entsteht eine Demo.xml Datei im Home Verzeichnis. Diese kann man kontrollieren und/oder in den Ordner /home/user/.qet/elements/ als qet_lables.xml kopieren.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# we saw thislike file structure
__author__ = "Volker Heggemann, Melle, DE"
__copyright__ = "Copyright (C) 2020 Volker Heggemann"
__license__ = "CC BY 3.0 DE"
__version__ = "1.0"

import os
from xml.sax.saxutils import quoteattr as xml_quoteattr

def make_xmlfile(path):

    #recursive function
    def DirAsLessXML(path):
        result = '<category name=%s>\n' % xml_quoteattr(os.path.basename(path))
        prefix = ''
        for item in os.listdir(path):
            itempath = os.path.join(path, item)
            if os.path.isdir(itempath):
                result += '\n'.join('   ' + line for line in
                                    DirAsLessXML(os.path.join(path, item)).split('\n'))
                result += '   \n'
            elif os.path.isfile(itempath):
                if item == 'tagfile.txt':
                    x = open(itempath, 'r')
                    prefix = x.read(1)
            #     pass
            #     #result += '  <file name=%s />\n' % xml_quoteattr(item)

        result += '   <prefix>' + prefix + '</prefix>\n'
        result += '</category>\n'
        return result

    xmltxtfile = '<?xml version="1.0" encoding="utf-8"?>\n<labels>\n' + DirAsLessXML(path) + '\n</labels>'
    return xmltxtfile


def main(inpath='', outfile='demo.xml', show=True):
    """ Main program """
    if inpath == '':
        inpath = os.path.expanduser('~')
    lookpath = inpath + '/.qet/elements/'
    outstr = make_xmlfile(lookpath)

    f = open(inpath + os.path.sep + outfile, "w+")
    f.write(outstr)
    if show:
        print(outstr)

if __name__ == '__main__':
    import plac
    plac.call(main)

Post's attachments

Attachment icon qelectrotech_element.py 1.64 kb, 331 downloads since 2020-03-11 

Re: Kennzeichnung - der eigenen Bauteilsammlung - ein Tool (in Python)

Beispiel:
Mein Verzeichnis sieht so aus (find  /home/user/.qet/elements * -type d )

/home/user/.qet/elements
/home/user/.qet/elements/hersteller
/home/user/.qet/elements/hersteller/eltako   # hier habe ich eine Textdatei tagfile.txt mit dem Inhalt 'K' abgespeichert
/home/user/.qet/elements/netzwerk
/home/user/.qet/elements/netzwerk/netze
/home/user/.qet/elements/netzwerk/telefonie
/home/user/.qet/elements/netzwerk/wlan
/home/user/.qet/elements/10_elektrik
/home/user/.qet/elements/10_elektrik/ueberspannung  # hier habe ich eine Textdatei tagfile.txt mit dem Inhalt 'F' abgespeichert
/home/user/.qet/elements/10_elektrik/schutzschalter   # hier habe ich eine Textdatei tagfile.txt mit dem Inhalt Q' abgespeichert
/home/user/.qet/elements/10_elektrik/netze   # hier habe ich eine Textdatei tagfile.txt mit dem Inhalt 'E' abgespeichert
/home/user/.qet/elements/10_elektrik/adern   # hier habe ich eine Textdatei tagfile.txt mit dem Inhalt 'W' abgespeichert
/home/user/.qet/elements/10_elektrik/kontakte   # hier habe ich eine Textdatei tagfile.txt mit dem Inhalt 'S' abgespeichert
/home/user/.qet/elements/10_elektrik/installation   # hier habe ich eine Textdatei tagfile.txt mit dem Inhalt 'X' abgespeichert
/home/user/.qet/elements/10_elektrik/klemmen   # hier habe ich eine Textdatei tagfile.txt mit dem Inhalt 'X' abgespeichert
/home/user/.qet/elements/10_elektrik/sicherungen   # hier habe ich eine Textdatei tagfile.txt mit dem Inhalt 'F' abgespeichert
/home/user/.qet/elements/10_elektrik/netzwerk
/home/user/.qet/elements/10_elektrik/verweise
/home/user/.qet/elements/10_elektrik/potentialausgleich

Aufruf mit :python3 qelectrotech_element.py
(benötigt plac : also vielleicht vorher noch ein:  pip3 install plac)

dann entsteht die Datei demo.xml mit folgendem Inhalt:

<?xml version="1.0" encoding="utf-8"?>
<labels>
<category name="">
   <category name="hersteller">
      <category name="eltako">
         <prefix>K</prefix>
      </category>
         
      <prefix></prefix>
   </category>
      
   <category name="netzwerk">
      <category name="netze">
         <prefix></prefix>
      </category>
         
      <category name="telefonie">
         <prefix></prefix>
      </category>
         
      <category name="wlan">
         <prefix></prefix>
      </category>
         
      <prefix></prefix>
   </category>
      
   <category name="10_elektrik">
      <category name="ueberspannung">
         <prefix>F</prefix>
      </category>
         
      <category name="schutzschalter">
         <prefix>Q</prefix>
      </category>
         
      <category name="netze">
         <prefix>E</prefix>
      </category>
         
      <category name="adern">
         <prefix>W</prefix>
      </category>
         
      <category name="kontakte">
         <prefix>S</prefix>
      </category>
         
      <category name="installation">
         <prefix>X</prefix>
      </category>
         
      <category name="klemmen">
         <prefix>X</prefix>
      </category>
         
      <category name="sicherungen">
         <prefix>F</prefix>
      </category>
         
      <category name="netzwerk">
         <prefix></prefix>
      </category>
         
      <category name="verweise">
         <prefix></prefix>
      </category>
         
      <category name="potentialausgleich">
         <prefix></prefix>
      </category>
         
      <prefix></prefix>
   </category>
      
   <prefix></prefix>
</category>

</labels>

Re: Kennzeichnung - der eigenen Bauteilsammlung - ein Tool (in Python)

Hallo vohegg,

good job, thanks. nomicons/wink

http://micheles.github.io/plac/

"Le jour où tu découvres le Libre, tu sais que tu ne pourras jamais plus revenir en arrière..."

Re: Kennzeichnung - der eigenen Bauteilsammlung - ein Tool (in Python)

Thx for your answer..

but, i'm fooling around with this, there must be a better way to give Letters to the emements (-folders).
And i found  a solution.
Give me some more hours, and i upload another piece of python code.

regards
Volker

Re: Kennzeichnung - der eigenen Bauteilsammlung - ein Tool (in Python)

Done,
here a short video on what could happend....
https://vimeo.com/397627826

For anyone can use it, you can download Elementsym_tofile.py on my Github account.
https://github.com/vohe/qelectrotech_labels_to_file

How does this work?
For each folder of the user - collection it is possible to set a language description.
So - you add a 'virtual' language description for language 'sy' which contains the symbols label.

This little tool fetch all the labels you set (and possible you can set the labels right in the tool)
After you click on the >>save<< button in the tool, it generates the qet_labels.xml file for you and after a
restart of qelectrotech you could use the symbols with labels.

Feel free to send me your suggestion about this

regards

Re: Kennzeichnung - der eigenen Bauteilsammlung - ein Tool (in Python)

Great,
thanks vohegg for this vidéo Howto and Github source. nomicons/smile

"Le jour où tu découvres le Libre, tu sais que tu ne pourras jamais plus revenir en arrière..."