# -*- coding: cp1252 -*- ''' Created on 15.08.2016 @author: sth2mt ''' import os as os import json class Treiber(object): ''' classdocs ''' # Achtung dies ist Pseudo Singleton -> variablen werden statisch verwendet. Aufgeraumt wird erst wenn alle instanzen weg sindwird nur einmal instaniiert !!!! ObjCount = 0 _Thread_name = "JasonCfg" stout = False fJason = file JasonExt = 'json' JasonPath = "/home/tool/config" def __init__(self): ''' Constructor ''' Treiber.ObjCount += 1 self.printf("%i.Construktor %s Anzahl" % (Treiber.ObjCount,Treiber._Thread_name)) pass def __del__(self): ''' Destructor ''' self.printf("%i Destruktor %s" % (Treiber.ObjCount,Treiber._Thread_name)) if not Treiber.fJason.closed: Treiber.fJason.close() if Treiber.ObjCount == 1: self.printf("Reste aufraumen") if Treiber.ObjCount > 0: Treiber.ObjCount -= 1 pass def ReadFile(self, sFileName, sPath=JasonPath): try: self.printf(Treiber._Thread_name+".ReadFile()") # Typschilddatei suchen sPathFile = sPath + "/" + sFileName + "." + Treiber.JasonExt self.printf(sPathFile) if (os.path.isfile(sPathFile)): Treiber.fJason = open(sPathFile,'r') self.printf("ReadFile():open(%s)"%sPathFile) sInhalt = Treiber.fJason.read() Treiber.fJason.close() self.printf("ReadFile(): %s"% sInhalt) return sInhalt else: self.printf("ReadFile():*.typ Nix gefunden") #return None # Nix gefunden except EnvironmentError,e: print "ReadFile.json error", e return None def WriteFile(self, sJson, sFileName, sPath=JasonPath): try: self.printf(Treiber._Thread_name+".WriteFile()") # Typschilddatei suchen sPathFile = sPath + "/" + sFileName + "." + Treiber.JasonExt self.printf(sPathFile) if (os.path.isfile(sPathFile)): Treiber.fJason = open(sPathFile,'w') self.printf("WriteFile():open(%s)"%sPathFile) Treiber.fJason.write(sJson) Treiber.fJason.close() self.printf("WriteFile(): %s"% sJson) return True else: self.printf("WriteFile():*.typ Nix gefunden") return False # Nix gefunden except EnvironmentError,e: print "WriteFile().json error", e return False def Decode(self, sJson): try: self.printf(Treiber._Thread_name+".Decode()") pydecode = json.loads(sJson) return pydecode except EnvironmentError,e: print "Decode().json error", e return None pass def Encode(self, pydecode): try: self.printf(Treiber._Thread_name+".Encode()") sJson = json.dumps(pydecode,indent=2,sort_keys=True) return sJson except EnvironmentError,e: print "Encode().json error", e return None pass def SetValue(self, pydecode, Value, *keys): try: self.printf(Treiber._Thread_name+".SetValue()") anzahlParam=len(keys) #print keys if anzahlParam == 1: pydecode[keys[0]]=Value if anzahlParam == 2: pydecode[keys[0]][keys[1]]=Value if anzahlParam == 3: pydecode[keys[0]][keys[1]][keys[2]]=Value if anzahlParam == 4: pydecode[keys[0]][keys[1]][keys[2]][keys[3]]=Value if anzahlParam == 5: pydecode[keys[0]][keys[1]][keys[2]][keys[3]][keys[4]]=Value return pydecode except KeyError,e: self.printf( "SetValue().json Error Key %s not found"% e) return None pass def GetValue(self, pydecode, *keys): try: self.printf(Treiber._Thread_name+".GetValue()") anzahlParam=len(keys) #print keys if anzahlParam == 1: return pydecode[keys[0]] if anzahlParam == 2: return pydecode[keys[0]][keys[1]] if anzahlParam == 3: return pydecode[keys[0]][keys[1]][keys[2]] if anzahlParam == 4: return pydecode[keys[0]][keys[1]][keys[2]][keys[3]] if anzahlParam == 5: return pydecode[keys[0]][keys[1]][keys[2]][keys[3]][keys[4]] except KeyError,e: self.printf ("GetValue().json Error Key %s not found"% e) return None pass def printf(self,string): if Treiber.stout: print string pass