회원 로그인
|
Config Parser
http://nefaria.com/2012/08/simple-configuration-file-parser-python
#cfg
#Example configuration file #Foo Bar-er v1.0 foo_dir="/var/lib/foo" bar_dir="/var/lib/bar" foo_all_the_bars="1" bar_all_the_foos = "yes, do it"#code
# SimpleConfigParser # Inspired by: # http://www.decalage.info/fr/python/configparser class SimpleConfigParser(): def __init__(self, comment_char = '#', option_char = '=', allow_duplicates = False, strip_quotes = True): self.comment_char = comment_char self.option_char = option_char self.allow_duplicates = allow_duplicates self.strip_quotes = True def parse_config(self, filename): self.options = {} config_file = open(filename) for line in config_file: if self.comment_char in line: line, comment = line.split(self.comment_char, 1) if self.option_char in line: option, value = line.split(self.option_char, 1) option = option.strip() value = value.strip() value = value.strip('"'') if self.allow_duplicates: if option in self.options: if not type(self.options[option]) == list: old_value = self.options[option] self.options[option] = [value] + [old_value] else: self.options[option] += [value] else: self.options[option] = value else: self.options[option] = value config_file.close() return self.options#
>>> from simpleconfig import SimpleConfigParser >>> scp = SimpleConfigParser() >>> scp.parse_config('example.cfg') {'bar_dir': '/var/lib/bar', 'foo_all_the_bars': '1', 'foo_dir': '/var/lib/foo', 'bar_all_the_foos': 'yes, do it'}#
>>> from simpleconfig import SimpleConfigParser >>> scp = SimpleConfigParser(allow_duplicates = True) >>> nagios_config = scp.parse_config('/etc/nagios3/nagios.cfg') >>> nagios_config['cfg_dir'] ['/etc/nagios3/conf.d', '/etc/nagios-plugins/config']
댓글 0개
| 엮인글 0개
285개(14/15페이지)
번호 | 제목 | 글쓴이 | 조회 | 날짜 |
---|---|---|---|---|
25 | [VisualStudio] Visual Studio Express 2012 다운로드/설치 | 푸딩뱃살 | 2731 | 2013.04.06 20:12 |
24 | [Python] DC 이효리 겔러리에서 사진 추출하기 | artsOne | 934 | 2009.03.27 01:54 |
23 | [Python] 클래스 안의 함수 실행 | artsOne | 2546 | 2012.09.20 11:54 |
22 | [Python] 기본 함수들 | artsOne | 4882 | 2012.09.12 15:39 |
>> | [Python] Config Parser | artsOne | 2265 | 2012.08.28 13:55 |
20 | [Python] win32 오픈오피스 실행 | artsOne | 2154 | 2012.08.01 17:38 |
19 | [Python] 문자열의 기호들을 출력하기 | artsOne | 2868 | 2009.04.17 11:59 |
18 | [Python] python 자료형 / 자료형 출력 | artsOne | 3394 | 2009.03.30 00:54 |
17 | [Python] win32 모듈로 Excel 사용하기 | artsOne | 4113 | 2008.03.18 02:24 |
16 | [Python] 용어 정리 | artsone | 3565 | 2008.02.26 23:11 |
15 | [Python] Python 9강 - 파일 | artsone | 9963 | 2007.11.03 02:59 |
14 | [Python] Python 8강 - 객체의 복사 및 형 변환 | artsone | 5220 | 2007.11.01 04:15 |
13 | [Python] Python 7강 - 사전 | artsone | 2871 | 2007.10.30 03:26 |
12 | [Python] Python 6강 - 튜플 | artsone | 3139 | 2007.10.24 05:30 |
11 | [Python] Python 5강 - 리스트 | artsone | 7338 | 2007.10.23 22:13 |
10 | [Python] Python 4강 - 문자열 | artsone | 3700 | 2007.10.20 03:04 |
9 | [Python] Python 3강 - 수치 자료형과 연산자 | artsone | 4197 | 2007.10.18 17:01 |
8 | [Python] Python 2강 - 파이썬 문과 기본 자료형 | artsone | 5315 | 2007.10.15 02:25 |
7 | [Python] Python 1강 - 파이썬이란? | artsOne | 3419 | 2007.10.11 23:36 |
6 | [Python] [스크랩] Python은 무엇인가? | artsone | 2001 | 2008.02.26 16:20 |