Welcome To Our Shell

Mister Spy & Souheyl Bypass Shell

Current Path : /proc/thread-self/root/home/ift/52_procpy/fibu/

Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
Upload File :
Current File : //proc/thread-self/root/home/ift/52_procpy/fibu/account3.pyq

#  coding:  utf8

import procpy,os,sys,re,glob,time,codecs
import sqlite3,base64,hashlib,time
import rules

#******************************************************************************

class Account3 (object):

    def __init__ (self,dbfile):
    
        self.home           = re.sub(r"^(.*)[\\\/](.*)$","\\1",rules.__file__)
        self.rules          = rules.Rules(self)
        if not dbfile == ":memory:":
            dbfile = self.home+"/"+dbfile
        self.db             = sqlite3.connect(dbfile)
        self.db.row_factory = sqlite3.Row
        self.mark()

        cursor  = self.db.cursor()
        cursor.execute("create table if not exists buchungen " +
             "(DATUM,BETRAG,KTOA,KTOB,REMARK default '')")
        cursor.execute("create table if not exists ktoname " +
             "(KTO,NAME,DATUMA,DATUMB)")

        try:
            cursor.execute("create index DATUM_IDX  on buchungen (DATUM)"   )
            cursor.execute("create index KTOA_IDX   on buchungen (KTOA)"    )
            cursor.execute("create index KTOB_IDX   on buchungen (KTOB)"    )
        except Exception as e:
            pass

        try:
            cursor.execute("create index KTO_IDX    on ktoname   (KTO)"     )
            cursor.execute("create index DATUMA_IDX on ktoname   (DATUMA)"  )
            cursor.execute("create index DATUMB_IDX on ktoname   (DATUMB)"  )
        except Exception as e:
            pass

        self.mark("0. Datenbank einrichten.")
            

#********************************************************************************

    def mark (self,remark=""):
    
        t = time.clock()
        if 't0' in vars(self):
            print ( ("%9.2f" % ((t-self.t0)*1000)) + " ms fuer:  " + remark )
        self.t0 = t

#********************************************************************************

    def sync (self,*pars):
    
        pars1   = []
        relhome = os.path.relpath(self.home,".")
        for par in pars:
            par = re.sub(r"^\.\.\.",relhome,par)
            m = re.search(r"^(.*?)(\*\*+)(.*)$",par)
            if m:
                zaehler = 0
                while (0 == 0):
                    zaehler = zaehler + 1
                    if zaehler > len(m.group(2)):
                        break
                    pars1.append(m.group(1)+"/".join(["*"]*zaehler)+m.group(3))
            else:
                pars1.append(par)

        if len(pars1) == 0:
            pars1 = ['*.kto','*.xlsx']
        ktofiles = []
        for par in pars1:
            files = glob.glob(par)
            for file in files:
                if not file in (ktofiles):
                    ktofiles.append(file)  #os.path.relpath(file))
        ktodatas = []

        for ktofile in ktofiles:
            ktodatas.append( { "KTOFILE": ktofile, "ROUNDS": 10 } )
            
        rounds = 0
        rtime1 = time.clock()
        while (0 == 0):

            rounds   = rounds + 1
            maxsteps = 1

            print ("=========================================")
            print ("START ROUND " + str(rounds))
            print ("=========================================")

            for ktodata in ktodatas:
        
                print (ktodata['KTOFILE'])
                ktodata["STEPS"] = 0
                while (0 == 0):
                    ktodata["STEPS"] = ktodata["STEPS"] + 1
                    self.read_kto(ktodata,rounds)
                    if not "PROCEED" in self.dbsync(ktodata):
                        break
                    if not ktodata["MERGE"] == "":
                        self.mark("Merge-Conflict: " + ktodata["MERGE"])
                        break
                    if ktodata["STEPS"] > 5:
                        time.sleep(1)
                    if ktodata["STEPS"] > 20:
                        break
                maxsteps = max(maxsteps,ktodata["STEPS"])

            rtime0 = rtime1
            rtime1 = time.clock()
            print ("Benoetigte Zeit: " + ("%7.2f" % (rtime1 - rtime0)) + " Sekunden.")

            if maxsteps < 2:
                break     
            
        merge_conflict_files = []
        for ktodata in ktodatas:
        
            if not ktodata["MERGE"] == "":
                zaehler = "001"  
                while (0 == 0):
                    if os.path.isfile( ktodata["KTOFILE"] + "." + zaehler ):
                        zaehler = ("%03u" % (int(zaehler) + 1) )
                    else:
                        break
                merge_conflict_files.append( ktodata['KTOFILE'] + "." + zaehler )
                open( merge_conflict_files[-1] , "w").write( ktodata['CONTENT'] )
            else:
                open( ktodata['KTOFILE'], "w").write(ktodata['CONTENT'] )


        self.db.commit()
        print ("=========================================")
        if rounds == 1:
            rounds = str(rounds) + " Round."
        else:
            rounds = str(rounds) + " Rounds."
        self.mark("I. Datenbank Commit. " + rounds)
        if len(merge_conflict_files) > 0:
            print ("\n" + "\n".join(merge_conflict_files) + "\n\n")

#********************************************************************************

    def read_kto (self,ktodata,rounds=0):
    
        if ktodata['STEPS'] > 1:
            self.mark("%-70s" % ("------------ " + ktodata['FILENAME'] + " " +
                           str(rounds) + " " + str(ktodata['STEPS']) + " -------------------") )
#        else:
#            self.mark(ktodata["KTOFILE"])

        if not "KTODIR" in ktodata:

            m = re.search(r"^(.*)[\\\/](.*)",ktodata['KTOFILE'])
            if m:
                ktodata['KTODIR']   = m.group(1)
                ktodata['FILENAME'] = m.group(2)
            else:
                ktodata['KTODIR']   = "."
                ktodata['FILENAME'] = ktodata['KTOFILE']

            ktopath = os.path.relpath(ktodata['KTODIR'],self.home) 
            ktopath = re.sub(r"\_\_(.*?)(\\|\/|$)","-",ktopath,9999)
            ktopath = re.sub(r"[\\\/]","-",ktopath,9999)
            ktopath = ktopath.strip("-")
            ktopath = "-" + re.sub(r"^\-+","-",ktopath,9999)
            ktopath = re.sub(r"\-20\d\d(\-|\\|\/|$)","\\1",ktopath)
            ktodata['KTOPATH'] = ktopath
            
            addfiles  = glob.glob(ktodata['KTODIR']+"/*")
            addfiles.sort()
            addtext   = ""
            for addfile in addfiles:

                if not os.path.isfile(addfile):
                    continue
                if re.search(r"\.(kto[\.\d]*|kto|py|pyc|db|db-journal)$",addfile) or "EXCLUDE" in addfile or "~" in addfile:
                    continue
                if re.search(r"(__pycache__)$",addfile):
                    continue
                m = re.search(r"^(.*)[\\\/](.*)$",addfile)
                if not "." in m.group(2):
                    continue
#                print ("ADD1",addfile,m.group(2))
                if os.path.isfile(addfile):
                    addtext = addtext + str( base64.urlsafe_b64encode(
                               hashlib.md5(open(addfile,'r').read().encode("utf8")).digest()),"ascii" )
                if m:
                    addtext =  addtext + m.group(2)
                else:
                    addtext =  addtext + addfile

            ktodata['ADDTEXT'] = addtext
            ktodata['CONTENT'] = open(ktodata['KTOFILE']).read()
            ktodata['MAXA']    = 10
            ktodata['MAXB']    = 10
            ktodata['IMPORT1'] = ""
            ktodata['DBCHANGED'] = False
            ktodata['DBHASH']  = "-"
                
            self.mark("A. Einlesen Konto.")

        else:
            pass
        
        if not 'INT' in ktodata:
            self.rules.rule__raw(ktodata)        
            
        if 'FUNC' in ktodata and ktodata['DBCHANGED']:
            if ktodata['FUNC'] in self.rules.__class__.__dict__:
                self.rules.__class__.__dict__[ktodata['FUNC']](self.rules,ktodata)
                self.mark("B. Rules anwenden.")
            

        m = re.search(r"^(.*?)\((.*?)\)(.*)$",ktodata['CONTENT'],re.DOTALL)

        text1              = m.group(1)
        text2              = m.group(3)
        ktodata['DBHASH']  = m.group(2)[0:6]
        ktodata['KTOHASH'] = m.group(2)[6:]
        if re.search(r"\n\d\d\d\d\d\d\d\d +\-?\d",text2):
            t = "___CONTAINS___BUCHUNGEN___"
        else:
            t = ""
        t                  = t + text1 + text2 + ktodata['ADDTEXT']
        ktodata['NEWHASH'] = self.compute_ktohash(ktodata,t)
        ktodata['IMPORT0'] = ktodata['IMPORT1']
        t                  = re.sub(" +","",t.strip(),99999999)
        ktodata['IMPORT1'] = self.compute_ktohash(ktodata,t)
        ktodata["INT"]     = ""

        m = re.search(r"^(.*?\n|)(\S*) +$",text1,re.DOTALL)
        ktodata['HEADER']  = m.group(1)
        ktodata['UKTO']    = m.group(2)
        m1 = re.search(r"^(.*)\-([123456789IJABCKLMNP])$",ktodata['UKTO'])
        if m1:
            ktodata['INT']  = m1.group(2)
            ktodata['UKTO'] = m1.group(1)
        ktodata['UKTO1'] = "-" + ktodata['UKTO']
        if ktodata["UKTO1"] == "-":
            ktodata["UKTO1"] = ""


        m = re.search(r"^ *([0123456789,]*)\s* +([^\n]*?)(\-?\d+\.\d\d) *\n\s*(.*)$",text2,re.DOTALL)
        ktodata['INTERVAL']  = m.group(1)
        m1 = re.search(r"^(.*)\-([123456789IJABCKLMNP])$",ktodata['INTERVAL'])
        if m1:
            ktodata['INT']      = m1.group(2)
            ktodata['INTERVAL'] = m1.group(1)
        ktodata['NAME']      = m.group(2)
        ktodata['SOLLWERT']  = m.group(3)
        ktodata['BUCHUNGEN'] = m.group(4)
        ktodata['MERGE']     = ''
            
        m = re.search(r"^(.*),(.*)$",ktodata['INTERVAL'])
        if m:
            ktodata['START'] = m.group(1)
            ktodata['ENDE']  = m.group(2)
        else:
            ktodata['START'] = ktodata['INTERVAL']
            ktodata['ENDE']  = ktodata['INTERVAL']
        ktodata['START']   = (ktodata['START'] + "00000000")[0:8]
        ktodata['ENDE']    = (ktodata['ENDE']  + "99999999")[0:8]

        if not ktodata['INT'] == "":    #   Zeitraum bestimmen
            intv = { "1" : ["0101","0199"],
                     "2" : ["0201","0299"],
                     "3" : ["0301","0399"],
                     "4" : ["0401","0499"],
                     "5" : ["0501","0599"],
                     "6" : ["0601","0699"],
                     "7" : ["0701","0799"],
                     "8" : ["0801","0899"],
                     "9" : ["0901","0999"],
                     "A" : ["1001","1099"],
                     "B" : ["1101","1199"],
                     "C" : ["1201","1299"],
                     "I" : ["0101","0399"],
                     "J" : ["0401","0699"],
                     "K" : ["0701","0999"],
                     "L" : ["1001","1299"],
                     "M" : ["0101","0699"],
                     "N" : ["0701","1299"],
                     "P" : ["0101","1299"] } [ ktodata['INT'] ]
            ktodata["START"] = ktodata["START"][0:4] + intv[0]
            ktodata["ENDE"]  = ktodata["ENDE"][0:4]  + intv[1]

        ktodata['FUNC']    = "rule__" + re.sub(r"-","_",re.sub(r"_","__",ktodata['UKTO'],99),99)

        if not 'FILENAME1' in ktodata:
            ktodata['FILENAME1'] = ktodata['FILENAME']
            m = re.search("^("+ktodata['KTOPATH']+")(.*)$",ktodata['UKTO1'])
            if m:
                filename = ktodata['FILENAME']
                filename = re.sub(r"^[\d\-]*","",filename)
                filename = re.sub(r"^\_\_","",filename)
                if not filename[0] == ".":
                    ktodata['FILENAME1'] = m.group(2)[1:] 
                    if not re.search(ktodata['FILENAME']+"$",ktodata['FILENAME1']):_
                        if not ktodata['FILENAME1'] == "":
                            ktodata['FILENAME1'] = ktodata['FILENAME1'] + "__" + ktodata['FILENAME']
                    
                    + "__" + ktodata['FILENAME']
        print ("FILENAME1",ktodata['FILENAME1'])


        self.mark("C. Analysieren Kontotext.")


        
#*************************************************************************************

    def dbsync (self,ktodata):
    
        if os.path.isfile(ktodata['KTOFILE']+".001"):
            os.unlink(ktodata['KTOFILE']+".001")
            ktodata['NEWHASH'] = "xxxxxx"

        dbhash = self.compute_dbhash(ktodata)
        
#        print (ktodata['NEWHASH'])
        if ktodata['NEWHASH'] == "xxxxxx":
            ktodata['DBHASH'] = "."
        if dbhash == "xxxxxx":
            ktodata['NEWHASH'] = "."
        
        self.mark("   --> DBHASH: " + ktodata['DBHASH'] + ", DB_NEW: " + dbhash +
                     ", KTOHASH: " + ktodata['KTOHASH'] + ", NEW_HASH:  " + ktodata['NEWHASH'] +
                     ", IMPORT0: " + ktodata['IMPORT0'] + ", IMPORT1:  " + ktodata['IMPORT1'])

        if ktodata['KTOHASH'] == ktodata['NEWHASH'] or ktodata['NEWHASH'] == 'xxxxxx':  # Kontofile - oder die zugehoerigen Dateien - haben sich nicht geaendert

            if dbhash == ktodata['DBHASH'] and not dbhash == "xxxxxx":   #  Kontoeintraege in der Datenbank haben sich auch nicht geaendert.
                self.mark("H. Konto ist aktuell. Steps: " + str(ktodata["STEPS"]) +".")
                return("Kto is up-to-date")       #  Dann nichts machen

            else:
                ktodata['DBHASH'] = dbhash
                self.get_from_db(ktodata)      #   Kontotext updaten aus der Datenbnk
                self.mark("G1. Kontotext neu schreiben.")

        else:  # Kontofile - oder die zugehoerigen Dateien - haben sich geaendert

            if dbhash == ktodata['DBHASH'] or dbhash == "xxxxxx":  #  Datenbankdaten haben sich nicht geaendert
                                                                                   
                merge_result = ""
                
            else:
            
                cursor = self.db.cursor()
                qstr   =                " select DATUM,BETRAG,KTOA,KTOB,REMARK from buchungen "
                qstr   = qstr + " where (KTOA like '" + ktodata['UKTO1'] + "%' or KTOB like '" + ktodata['UKTO1'] + "%')"
                qstr   = qstr + " and DATUM >= '" + ktodata['START'] + "' and DATUM <= '" + ktodata['ENDE'] + "' "
                qstr   = qstr + " order by DATUM,KTOA,KTOB" 
                cursor.execute(qstr)
                merge_result = self.rules.merge(ktodata,cursor)
                self.mark("E. Mergen. " + merge_result)
                
                if os.path.isfile(ktodata['KTOFILE']+".001"):
                    os.unlink(ktodata['KTOFILE']+".001")
                    merge_result = ""
                
            if merge_result == "":   #  aus den kontodaten in die DB importieren

                if not ktodata['IMPORT1'] == ktodata['IMPORT0']:
                    self.db.cursor().execute("delete from buchungen where (KTOA like '" +    
                                ktodata["UKTO1"] + "%' OR " + " KTOB like '" + ktodata["UKTO1"] + "%') " +
                                    " and DATUM >= '" + ktodata['START'] + "' and DATUM <= '" + ktodata['ENDE'] + "' ")


                    unique_strings = {}
                    for zeile in ktodata['BUCHUNGEN'].split("\n"):
#                        print (333,zeile)
                        m = re.search(r"^(\d\d\d\d\d\d\d\d) +(\-?\d+\.\d\d) +(\S*) +(\S+) +(\-?\d+\.\d\d) +(.*)$",zeile)
                        if m:
#                            print (898)
                            datum  = m.group(1)
                            betrag = str(m.group(2))
                            remark = m.group(6)
                            uniqu  = []
                            ktoa   = "-" + m.group(3)
                            if ktoa == "--":
                                ktoa  = ktodata['UKTO1']
                            elif ktoa[0:2] == "--":
                                ktoa  = ktodata['UKTO1'] + ktoa[1:]
                            else:
                                uniqu.append(ktoa)
                            ktob   = "-" + m.group(4)
                            if ktob == "--":
                                ktob = ktodata['UKTO1']
                            elif ktob[0:2] == "--":
                                ktob = ktodata['UKTO1'] + ktob[1:]
                            else:
                                uniqu.append(ktob)
                            if "Saldovortrag" in remark or ktob == "--11-1805":
                                continue
 
                            if uniqu == []:
                                uniqu = [ktoa,ktob]
                            uniqu.sort()
                            betrag1 = re.sub("\-","",betrag) + ","
                            remark1 = "," + re.sub(r" ","",remark,9999)
                            uniqu   = betrag1 + ",".join(uniqu) + remark1
                            if datum in unique_strings:  #  to avoid double entries
                                if uniqu in unique_strings[datum]:
                                    continue
                                else:
                                    unique_strings[datum].append(uniqu)
                            else:
                                unique_strings[datum] = [uniqu]  #  ,betrag1+"-13-9999"+remark1]

                            qstr = ("insert into buchungen (DATUM,BETRAG,KTOA,KTOB,REMARK) values " +
                                   "('" + datum + "'," + betrag + ",'" + ktoa + "','" + ktob + "','" + remark + "')")
                            self.db.cursor().execute(qstr)
                
                    o = self.compute_dbhash(ktodata)
                    ktodata['DBCHANGED'] = True # not ( ( ktodata['DBHASH'] == o ) and ( ktodata['IMPORT0'] == ktodata['IMPORT1'] ) )
                    ktodata['DBHASH']    = o
                    self.mark("F. Importieren in die Datenbank.")
                else:
                    ktodata['DBCHANGED'] = False

#                    self.mark("F. Kein Datenbank-Update noetig.")
                    
                self.get_from_db(ktodata)      #   Kontotext updaten aus der Datenbnk
                self.mark("G2. Kontotext neu schreiben.")

            else:
                ktodata['MERGE'] = merge_result
 
        return("PROCEED.")                    

#********************************************************************************

    def compute_dbhash (self,ktodata):

        qu_string = ("select group_concat(DATUM),group_concat(BETRAG),group_concat(KTOB),group_concat(REMARK) from " +
                     "buchungen" + " where (KTOA like '" + ktodata['UKTO1'] + "%' or KTOB like '" + ktodata['UKTO1'] + "%') " +
                     " and DATUM >= '" + ktodata['START'] + "' and DATUM <= '" + ktodata['ENDE'] + "' " +
                     " order by DATUM,KTOA,KTOB") 

#        print (qu_string)
        cursor = self.db.cursor()
        cursor.execute(qu_string)
        text1 = cursor.fetchone()
        dbhash_new = [""]
        for o in text1:
            if o:
                dbhash_new.append(re.sub(r" +"," ",o,99999999))
        if dbhash_new == [""]:
            dbhash_new = "xxxxxx"    #   wenn es gar keine Buchungen im Unterkonto gibt, dann dbhash auf "xxxxxx" setzen
        else:
            dbhash_new = base64.urlsafe_b64encode(hashlib.md5("".join(dbhash_new).encode("utf8")).digest())[0:6]
            dbhash_new = str(dbhash_new,"ascii")
        return(dbhash_new)

#********************************************************************************

    def compute_ktohash (self,ktodata,text):

        if not "___CONTAINS___BUCHUNGEN___" in text[0:36]:  #  wenn es gar keine Buchungen im Kontoauszug gibt
            erg = "xxxxxx"
        elif "<<<< HEAD" in text:
            erg = "xxxxxx"
        else:
            erg = str(base64.urlsafe_b64encode(hashlib.md5(text.encode("utf8")).digest()),"ascii" )[0:6]
        return(erg)
        
                                
#*******************************************************************************
        
    def get_from_db (self,ktodata):

        saldovortrag = []
        salden       = {}

        if ktodata["INT"] == "":
            saldovortrag = [0.0]            
            for kto in ("KTOA","KTOB"):
                cursor = self.db.cursor()
                qstr   =                " select sum(BETRAG) from buchungen "
                qstr   = qstr + " where (" + kto + " like '" + ktodata['UKTO1'] + "%')"
                qstr   = qstr + " and DATUM < '" + ktodata['START'] + "' "
                cursor.execute(qstr)
                o = cursor.fetchone()[0]
                if o:
                    saldovortrag[0] = saldovortrag[0] + float(o)
                saldovortrag[0] = -saldovortrag[0]
            salden[""] = saldovortrag[0]


        cursor = self.db.cursor()
        qstr   =                " select DATUM,BETRAG,KTOA,KTOB,REMARK from buchungen "
        qstr   = qstr + " where (KTOA like '" + ktodata['UKTO1'] + "%' or KTOB like '" + ktodata['UKTO1'] + "%')"
        qstr   = qstr + " and DATUM >= '" + ktodata['START'] + "' and DATUM <= '" + ktodata['ENDE'] + "' "
        qstr   = qstr + " order by DATUM,KTOA,KTOB" 
        cursor.execute(qstr)
        
        ukto1  = ktodata['UKTO1']
        ul     = len(ukto1)
        gesamt = 0.00
        maxa   = 9
        maxb   = 9
        maxa0  = "%-" + str(ktodata['MAXA']) + "s"
        maxb0  = "%-" + str(ktodata['MAXB']) + "s"
        text_intern = []
        text_extern = []
        t           = ""
        
        while (0 == 0):    #  Zeilen des Kontos schreiben
           
            if len(saldovortrag) > 0:
                entry        = [ ktodata['START'], saldovortrag[0], ktodata['UKTO1'], "-11-1805", "Saldovortrag"]
                saldovortrag = []
            else:
                entry  = cursor.fetchone()

            if not entry:
                break
            betrag = float(entry[1])
            ktoa   = entry[2]
            ktob   = entry[3]

            anz     = 0
            if ktoa[0:ul] == ukto1:
                ktoa = ktoa[ul:]
#                self.compute_salden(ktoa,betrag)
                if ktoa == "":
                    ktoa = "-"
                anz = anz + 1
            else:
                ktoa = ktoa[1:]
            if ktob[0:ul] == ukto1:
                ktob = ktob[ul:]
#                self.compute_salden(ktob,-betrag)
                if ktob == "":
                    ktob = "-"
                if anz == 0:
                    o      = ktoa
                    ktoa   = ktob
                    ktob   = o
                    betrag = -betrag
                anz = anz + 1
            else:
                ktob = ktob[1:]
         
            datum   = entry[0]
            remark  = entry[4]
            sortidx = datum + ktoa + ktob + remark[0:20]
            ktoa    = re.sub("^"+ukto1,"",ktoa)

            if ktoa == "":
                ktoa = "-"
                
            maxa    = max(maxa,len(ktoa))
            maxb    = max(maxb,len(ktob))
            buchung = datum +  " " + ("%13.2f" % betrag) + "  " + (maxa0 % ktoa)  + "  " +  (maxb0 % ktob) + "  "
            if anz == 1:
                gesamt = gesamt + float(betrag)
                text_extern.append([buchung + ("%13.2f" % gesamt) + "  " + entry[4],sortidx])
            else:
                text_intern.append([buchung + "         0.00  " + entry[4],sortidx])
            if t == "":
                t = "___CONTAINS___BUCHUNGEN___"
            
        text_intern.sort(key=lambda x: x[1])
        text_extern.sort(key=lambda x: x[1])
        
#        self.mark("Start Salden")

        fac = 1
        for kto in ("KTOA","KTOB"):
            qstr   =        " select " + kto + ",sum(BETRAG) from buchungen "
            qstr   = qstr + " where " + kto + " like '" + ktodata['UKTO1'] + "%'"
            qstr   = qstr + " and DATUM >= '" + ktodata['START'] + "' and DATUM <= '" + ktodata['ENDE'] + "' "
            qstr   = qstr + " group by (" + kto + ")" 
            cursor.execute(qstr)
            while (0 == 0):
                entry = cursor.fetchone()
                if not entry:
                    break
                ukto   = entry[0][ul:]
                betrag = float(entry[1])
                while (0 == 0):
                    if not ukto in salden:
                        salden[ukto] = 0.00000001
                    salden[ukto] = salden[ukto] + fac * betrag
                    m = re.search(r"^(.*)\-(.*)$",ukto)
                    if m:
                        ukto = m.group(1)
                    else:
                        break
            fac = -1
#        self.mark("Ende Salden")

#        print (salden)

        ktoliste = list(salden.keys())
        ktoliste.sort()
        konten = []

        text = []
        for o in text_extern:
            text.append(o[0])
        text.append("")

        for kto in ktoliste:
            sp = re.sub(r"[^\-]","",kto)
            text.append( ("%-50s" % re.sub(r"^\-","",kto)) + re.sub(r"\-","    ",sp,9999) + ("%13.2f" % salden[kto]) )
        text.append("")

        for o in text_intern:
            text.append(o[0])
        text.append("")

        if not ktodata['INT'] == "":
            o = "-" + ktodata['INT']
        else:
            o = ""

        text1 = ktodata['HEADER']
        text1 = text1 + ("%-30s" % (ktodata['UKTO'] + o)) + "  "
        text2 = "  "  + ktodata['INTERVAL'] + "  " + ("%13.2f" % float(gesamt) ) + "\n\n"
        text2 = text2 + "\n".join(text) + "\n"

        ktodata['NEWHASH'] = self.compute_ktohash(ktodata,t+text1+text2+ktodata['ADDTEXT'])
        ktodata['CONTENT'] = text1 + "(" + ktodata['DBHASH'] + ktodata['NEWHASH'] + ")" + text2

        if not ktodata['MAXA'] == maxa or not ktodata['MAXB'] == maxb:
            ktodata['MAXA']    = maxa
            ktodata['MAXB']    = maxb
            ktodata['CONTENT'] = ktodata['CONTENT'] + " "  # damit der KTOHASH nicht mehr stimmt und neu berechnet wird

#********************************************************************************

    def ukto_from_ktodir (self,ktodata):
    
        ktopath = os.path.relpath(ktodata['KTODIR'],self.home) 
        ktopath = re.sub(r"\_\_(.*?)(\\|\/|$)","-",ktopath,9999)
        ktopath = re.sub(r"[\\\/]","-",ktopath,9999)
        ktopath = ktopath.strip("-")
        ktopath = re.sub(r"^\-+","-",ktopath,9999)
        ktodata['KTOPATH'] = ktopath

#********************************************************************************

    def assign_ausgaben (self,ktodata,tmpkto,idbuch):    #   Ordnet Ausgabenkonten automatisch zu
    
        buchgrp = {}  #  Hier werden die Buchhaltungsgruppen gehalten
        zeilen  = []
#        idbuch.append("")

        zeilennr = 0
        for zeile in ktodata['CONTENT'].split("\n"):
            zeilen.append(zeile)
            m = re.search(r"^(\d\d\d\d\d\d\d\d) +(\-?\d+\.\d\d) +(\S*) +(\S+) +(\-?\d+\.\d\d) +(.*)$",zeile)
            if m:
                for id in idbuch:
                    parts = None
                    if id == "":
                        parts = []
                    else:
                        m1 = re.search(id,zeile,re.IGNORECASE)
                        if m1:
                            parts = []
                            zaehler = 0
                            while (0 == 0):
                                zaehler = zaehler + 1
                                try:
                                    parts.append(m1.group(zaehler))
                                except:
                                    break
                    if not parts == None:
                        if not id in buchgrp:
                            buchgrp[id] = []
                        buchgrp[id].append(
                           { 'DATUM': m.group(1), 'BETRAG': m.group(2), 'KTOA': m.group(3), 'KTOB': m.group(4),
                             'REMARK': m.group(6), 'ZEILENNR': zeilennr, 'CHANGED': 0, 'PARTS': parts } )
                        break
            zeilennr = zeilennr + 1
            
        for id in buchgrp:

            alle_gegenkonten = {}  #  erstmal ueberhaupt alle gegenkonten zusammensammeln
            for entry in buchgrp[id]:
                alle_gegenkonten[ entry['KTOB'] ] = 1
            alle_gegenkonten = list( alle_gegenkonten.keys() )
            print ("ALLE",alle_gegenkonten)
            if len(alle_gegenkonten) > 1 and tmpkto in alle_gegenkonten:  #  wenn es mehrere gegenkonto gibt
                print (123)
                alle_gegenkonten.remove(tmpkto)       #  neben dem tmpkto, dann den gemeinsamen Start-String berechnen
                common_start = alle_gegenkonten[0]
                for o in alle_gegenkonten:
                    ul           = min(len(common_start),len(o))
                    common_start = common_start[0:ul]
                    o            = o[0:ul]
                    while (0 == 0):
                        if common_start == o:
                            break
                        common_start = common_start[:-1]
                        o            = o[:-1]
                        
                for entry in buchgrp[id]:   #  ueberpruefen, ob die Theorie stimmt, nach der die Gegenkonten gemacht sind
                    if not entry['KTOB'] == tmpkto:  #  bei allen schon zugeordneteten Konten
                        if not entry['KTOB'] == common_start + "".join( entry['PARTS'] ):
                            common_start = None
                            
                print ("COMMON",common_start)

                if common_start == None:
                    continue

                for entry in buchgrp[id]:  #  wenn die Theorie stimmt, dann mit den unbestimmenten Gegenkonten
                    if entry['KTOB'] == tmpkto:    #  genauso verfahren und zuordnen
                        zeilen[ entry['ZEILENNR'] ] = (
                                entry['DATUM'] + "  " + entry['BETRAG'] + "  " +
                                entry['KTOA'] + "  " + common_start + "".join( entry['PARTS'] ) +
                                "  0.00  " + entry['REMARK'] )

        ktodata['CONTENT'] = "\n".join(zeilen)
        
#********************************************************************************

    def xxcompute_salden (self,ukto,betrag):

        while (0 == 0):
            if not ukto in self.salden:
                self.salden[ukto] = 0.0
            self.salden[ukto] = self.salden[ukto] + betrag
            m = re.search(r"^(.+)\-(.*)$",ukto)
            if m:
                ukto = m.group(1)
            else:
                return()

#********************************************************************************

if __name__ == "__main__":
    Account3.__dict__[sys.argv[2]](Account3(sys.argv[1]),*sys.argv[3:])

bypass 1.0, Devloped By El Moujahidin (the source has been moved and devloped)
Email: contact@elmoujehidin.net bypass 1.0, Devloped By El Moujahidin (the source has been moved and devloped) Email: contact@elmoujehidin.net