finished conifgparser.py

This commit is contained in:
lyon 2022-07-04 17:42:05 +08:00
parent 8cc90f1e55
commit ce2682a37f
2 changed files with 41 additions and 6 deletions

View File

@ -2,7 +2,7 @@ from PikaStdData import String
class ConfigParser():
content = String('')
content = ''
config_dict = {}
def read(self, file_name):
@ -11,11 +11,11 @@ class ConfigParser():
self._parse()
def read_string(self, content):
self.content = String(content)
self.content = content
self._parse()
def _parse(self):
content = self.content
content = String(self.content)
lines = content.split('\n')
for line in lines:
line = String(line)
@ -77,3 +77,22 @@ class ConfigParser():
val = section_dict[key]
items.append([key, val])
return items
def __str__(self):
content = ''
section_keys = self.config_dict.keys()
for section_item in section_keys:
content += '[' + section_item + ']\n'
section_dict = self.config_dict[section_item]
section_keys = section_dict.keys()
for key in section_keys:
val = section_dict[key]
content += key + ' = ' + val + '\n'
content += '\n'
return content
def write(self, file_name):
self.content = self.__str__(self)
print('Error: write() method not implemented')
print(self.content)
raise

View File

@ -1,8 +1,8 @@
import configparser
from PikaStdLib import MemChecker
config = configparser.ConfigParser()
config.read_string('\
[DEFAULT]\n\
config_string = '[DEFAULT]\n\
ServerAliveInterval = 45\n\
Compression = yes\n\
CompressionLevel = 9\n\
@ -13,20 +13,36 @@ User = hg\n\
\n\
[topsecret.server.com]\n\
Port = 50022\n\
ForwardX11 = no')
ForwardX11 = no'
config.read_string(config_string)
print('\n========== config_string ==========')
print(config_string)
print('\n===== config.sections() =====')
print(config.sections())
print('\n===== config.options("DEFAULT") =====')
print(config.options('DEFAULT'))
mem = MemChecker()
config.set('bitbucket.org', 'User', 'hhdd123')
print('\n========= config["bitbucket.org"] bitbucket.org ===========')
print(config['bitbucket.org'])
section = config['bitbucket.org']
section['User'] = '3833qwe'
print('\n========= config["bitbucket.org"] ===========')
print(config['bitbucket.org'])
print('\n========= config.items("DEFAULT") ===========')
print(config.items('DEFAULT'))
print('\n=============== all ================')
print(config)
print("mem used now: %0.2f kB" % mem.getNow())
print("mem used max: %0.2f kB" % mem.getMax())