-
변수 상속/순환저장카테고리 없음 2022. 11. 17. 15:05
Config.py
import xml.etree.ElementTree as ET class Config: # def __init__(self): # self.path = ET.parse('C:/Users/cdy16/PycharmProjects/pythonProject/config/config.xml') # self.filesize = 0 # self.count = 0 # self.level = "" # self.intf_type = "" # self.intf_id = "" # self.readpath = "" # self.savepath = "" path = ET.parse('C:/Users/cdy16/PycharmProjects/pythonProject/config/config.xml') root = path.getroot() filesize = 0 count = 0 level = "" intf_type = "" intf_id = "" readpath = "" savepath = "" def filesize(self): for common in self.root.findall("common/logger"): self.filesize = common.attrib.get("file_size") return self.filesize def buCount(self): for common in self.root.findall("common/logger"): self.count = common.attrib.get("count") return self.count def savepath(self): for intf in self.root.findall("interfaces/interface"): self.savepath = intf.attrib.get("out") changepath = self.savepath.replace('\\', '/') return changepath def readpath(self): for intf in self.root.findall("interfaces/interface"): self.readpath = intf.attrib.get("in") if '\\' in self.readpath: self.readpath = self.readpath.replace('\\', '/') return self.readpath def readConfig(self): root = self.path.getroot() for common in root.findall("common"): logger = common.find("logger") logger_path = logger.attrib.get("path") fsize = logger.attrib.get("file_size") count = logger.attrib.get("count") level = logger.attrib.get("level") print(f"logger path = {logger_path}") print(f"fsize = {fsize}") print(f"count = {count}") print(f"level = {level}") for intfs in root.findall("interfaces/interface"): # intf = intfs.find("interface") rtype = intfs.attrib.get("type") read = intfs.attrib.get("in") write = intfs.attrib.get("out") print(f"rtype = {rtype}") print(f"read = {read}") print(f"write = {write}")
main.py
import logging from logging.handlers import RotatingFileHandler from common.Config import Config def from_config(): conf = Config() conf.readConfig() def log_initialize(): #로그 세팅 logger = logging.getLogger() logger.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') stream_handler = logging.StreamHandler() stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) #Config변수 받기 spath = Config().savepath() fsize = int(Config().filesize()) bu_count = int(Config().buCount()) #순환로거 돌리기 file_handler = RotatingFileHandler(spath, maxBytes=fsize, backupCount=bu_count) file_handler.setFormatter(formatter) logger.addHandler(file_handler) # emp 파일 변경 -> 새로저장 def create_newfile(): logging.info('function started') readfile() logging.info('function ended') def readfile(): readpath = Config().readpath() logging.info('read started') rfile = open(readpath,'r') logging.info('read ended') change_file(rfile) def change_file(rfile): logging.info('change started') cfile = rfile.read().replace(',', '|') logging.info('change ended') save_file(cfile) def save_file(cfile): logging.info('new file saved') f = open('C:/project/result/newfile.txt', 'w') f.write(cfile) f.close() logging.info('file saved') # Press the green button in the gutter to run the script. if __name__ == '__main__': log_initialize() create_newfile()
self.변수 vs 일반변수?
반응형