티스토리 뷰
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | import pprint from pyparsing import Word, Literal, Forward, Group, ZeroOrMore from pyparsing import alphas, printables class Parser: def __init__(self, conf_file): self.conf_file = conf_file def get_str_conf(self): conf_lines = [] str_conf = open(self.conf_file, "r").read() for line in str_conf.strip().split("\n"): index = line.find("#") if index > -1: line = line[:index].strip() if line.find(";") > -1: conf_lines += line.split(";") else: conf_lines.append(line) conf_lines.insert(0, "conf {") conf_lines.append("}") return "\n".join(conf_lines) def syntax(self): lbr = Literal( '{' ).suppress() rbr = Literal( '}' ).suppress() equals = Literal("=").suppress() nonequals = "".join( [ c for c in printables if c != "=" ] ) + " \t" topic = Word( alphas ) key = Word( alphas + " \t") value = Word ( nonequals ) expr = Forward() opt = Group(expr) | key + equals + value opts = ZeroOrMore(opt) expr << topic + Group(lbr + opts + rbr) return expr def parse(self): conf = { 'jobs': [], 'pools': [], 'clients': [], 'schedules': [], 'filesets': [] } str_conf = self.get_str_conf() expr = self.syntax() result = expr.parseString(str_conf).asList() for i in range(len(result[1])): conf_name = result[1][i][0].strip().lower().replace(" ", "_") if conf_name in ["job", "pool", "client"]: keys = [x.strip().lower().replace(" ", "_") for x in result[1][i][1][0::2]] values = result[1][i][1][1::2] data = dict(zip(keys, values)) if conf_name == "job": conf['jobs'].append(data) elif conf_name == "pool": conf['pools'].append(data) elif conf_name == "client": conf['clients'].append(data) elif conf_name == "schedule": keys = [x.strip().lower().replace(" ", "_") for x in result[1][i][1][0::2]] values = result[1][i][1][1::2] run_ids = [] index = 0 for key in keys: if key == "run": run_ids.append(index) index += 1 run_ids.reverse() runs = [] for id in run_ids: keys.pop(id) runs.append(values.pop(id)) data = dict(zip(keys, values)) data['runs'] = runs conf['schedules'].append(data) elif conf_name == "fileset": data = { 'name': "", 'include': { 'option': {}, 'files': [] }, 'exclude': { 'files': [] } } for j in range(len(result[1][i][1])): if type(result[1][i][1][j]) == str: i_key = result[1][i][1][j].strip().lower().replace(" ", "_") if i_key == "name": data['name'] = result[1][i][1][j+1] elif type(result[1][i][1][j]) == list: j_key = result[1][i][1][j][0].strip().lower().replace(" ", "_") if j_key == "include": option_data = {} temp = [] for k in range(len(result[1][i][1][j][1])): if type(result[1][i][1][j][1][k]) == list: k_key = result[1][i][1][j][1][k][0].strip().lower().replace(" ", "_") if k_key == "options": keys = [x.strip().lower().replace(" ", "_") for x in result[1][i][1][j][1][k][1][0::2]] values = result[1][i][1][j][1][k][1][1::2] option_data = dict(zip(keys, values)) else: temp.append(result[1][i][1][j][1][k]) keys = [x.strip().lower().replace(" ", "_") for x in temp[0::2]] values = temp[1::2] file_ids = [] index = 0 for key in keys: if key == "file": file_ids.append(index) index += 1 file_ids.reverse() files = [] for id in file_ids: keys.pop(id) files.append(values.pop(id)) include_data = dict(zip(keys, values)) include_data['files'] = files include_data['option'] = option_data data['include'] = include_data if j_key == "exclude": keys = [x.strip().lower().replace(" ", "_") for x in result[1][i][1][j][1][0::2]] values = result[1][i][1][j][1][1::2] file_ids = [] index = 0 for key in keys: if key == "file": file_ids.append(index) index += 1 file_ids.reverse() files = [] for id in file_ids: keys.pop(id) files.append(values.pop(id)) exclude_data = dict(zip(keys, values)) exclude_data['files'] = files data['exclude'] = exclude_data conf['filesets'].append(data) return conf if __name__ == "__main__": parser = Parser("bacula-test.conf") pprint.pprint(parser.parse()) | cs |
'프로그래밍' 카테고리의 다른 글
PHP Recursive copy & mkdir function (0) | 2016.12.01 |
---|---|
airspeed를 사용한 템플릿 설정 관리 (0) | 2016.10.19 |
pyparsing을 사용한 json 파서 (0) | 2016.10.18 |
PHP Restful API Silex + Swagger UI (0) | 2016.10.13 |
Python 이미지를 스트링으로 변환 (0) | 2016.09.02 |
댓글
warpmemory
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
페이지
TAG
- limits
- error
- client
- code
- engineering
- MariaDB
- command
- configuration
- deview
- File
- mysql
- check
- 번역
- Web
- monitoring
- Windows
- httpd
- Linux
- apache
- 외부링크
- 예제
- 이슈처리
- RESTful
- example
- Module
- Ansible
- 명령어
- PowerShell
- 코드
- Python
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
글 보관함