#!/usr/bin/python
|
# -*- coding: GBK -*-
|
|
"""
|
ÅäÖÃÎļþ¹ÜÀíÄ£¿é
|
"""
|
|
import os
|
import ConfigParser
|
|
class ConfigReader:
|
"""ÅäÖÃÎļþ¶ÁÈ¡Æ÷µ¥Àý"""
|
|
_instance = None
|
_lock = None # ÑÓ³Ùµ¼Èë±ÜÃâÑ»·ÒÀÀµ
|
|
def __new__(cls):
|
if cls._instance is None:
|
# ÑÓ³Ù»ñÈ¡Ëø£¬±ÜÃâµ¼Èëʱѻ·ÒÀÀµ
|
import threading
|
if cls._lock is None:
|
cls._lock = threading.Lock()
|
|
with cls._lock:
|
if cls._instance is None:
|
cls._instance = super(ConfigReader, cls).__new__(cls)
|
cls._instance._initialized = False
|
return cls._instance
|
|
@classmethod
|
def instance(cls):
|
"""»ñÈ¡µ¥ÀýʵÀý"""
|
if cls._instance is None:
|
cls._instance = cls()
|
return cls._instance
|
|
def __init__(self):
|
if hasattr(self, '_initialized') and self._initialized:
|
return
|
self._initialized = True
|
|
self.config = ConfigParser.ConfigParser()
|
self.config_path = os.path.join(os.path.dirname(__file__), 'Config.ini')
|
self.load_config()
|
|
def load_config(self):
|
"""¼ÓÔØÅäÖÃÎļþ"""
|
if not os.path.exists(self.config_path):
|
self._create_default_config()
|
|
self.config.read(self.config_path)
|
|
def _create_default_config(self):
|
"""´´½¨Ä¬ÈÏÅäÖÃÎļþ"""
|
self.config.add_section('WriteFile')
|
self.config.set('WriteFile', 'WriteMode', '1') # 1=°´Ìì
|
self.config.set('WriteFile', 'MaxFileSize', '1024')
|
self.config.set('WriteFile', 'LogFilePath', '.\\EventLogs')
|
|
self.config.add_section('PacketLog')
|
self.config.set('PacketLog', 'EnablePacketLog', '1')
|
self.config.set('PacketLog', 'PacketLogPath', 'C:\\ServerLog')
|
|
self.config.add_section('Network')
|
self.config.set('Network', 'ListenPort', '60000')
|
|
with open(self.config_path, 'wb') as f:
|
self.config.write(f)
|
|
def get_listen_port(self):
|
"""»ñÈ¡¼àÌý¶Ë¿Ú"""
|
return self.config.getint('Network', 'ListenPort')
|
|
def get_write_mode(self):
|
"""»ñȡдÎļþģʽ: 1=°´Ìì, 2=°´Ð¡Ê±, 3=°´´óС, 4=µ¥Îļþ"""
|
return self.config.getint('WriteFile', 'WriteMode')
|
|
def get_max_file_size(self):
|
"""»ñÈ¡×î´óÎļþ´óС(KB)"""
|
return self.config.getint('WriteFile', 'MaxFileSize')
|
|
def get_log_file_path(self):
|
"""»ñÈ¡ÈÕÖ¾Îļþ·¾¶"""
|
return self.config.get('WriteFile', 'LogFilePath')
|
|
def get_enable_packet_log(self):
|
"""ÊÇ·ñÆôÓ÷â°üÈÕÖ¾"""
|
return self.config.getint('PacketLog', 'EnablePacketLog')
|
|
def get_packet_log_path(self):
|
"""»ñÈ¡·â°üÈÕ־·¾¶"""
|
return self.config.get('PacketLog', 'PacketLogPath')
|
|
def save(self):
|
"""±£´æÅäÖÃ"""
|
with open(self.config_path, 'wb') as f:
|
self.config.write(f)
|