|
| 1 | +import re |
| 2 | + |
| 3 | +INDENT = ' ' |
| 4 | + |
| 5 | +class Conf(object): |
| 6 | + def __init__(self, *args): |
| 7 | + self.blocks = list(args) |
| 8 | + self.servers = [] |
| 9 | + self.upd() |
| 10 | + |
| 11 | + def add(self, *args): |
| 12 | + self.blocks.extend(args) |
| 13 | + self.upd() |
| 14 | + return self.blocks |
| 15 | + |
| 16 | + def remove(self, *args): |
| 17 | + for x in args: |
| 18 | + self.blocks.remove(x) |
| 19 | + self.upd() |
| 20 | + return self.blocks |
| 21 | + |
| 22 | + def filter(self, btype='', name=''): |
| 23 | + flist = [] |
| 24 | + for x in self.blocks: |
| 25 | + if name and isinstance(x, Key) and x.name == name: |
| 26 | + flist.append(x) |
| 27 | + elif isinstance(x, Container) and x.__class__.__name__ == btype and x.value == name: |
| 28 | + flist.append(x) |
| 29 | + elif not name and btype and x.__class__.__name__ == btype: |
| 30 | + flist.append(x) |
| 31 | + return flist |
| 32 | + |
| 33 | + def upd(self): |
| 34 | + svr = [] |
| 35 | + for x in self.blocks: |
| 36 | + if isinstance(x, Server): |
| 37 | + svr.append(x) |
| 38 | + self.servers = svr |
| 39 | + |
| 40 | + def all(self): |
| 41 | + return self.blocks |
| 42 | + |
| 43 | + def as_list(self): |
| 44 | + ret = [] |
| 45 | + for x in self.blocks: |
| 46 | + ret.append(x.as_list()) |
| 47 | + return ret |
| 48 | + |
| 49 | + def as_dict(self): |
| 50 | + return {'conf': [x.as_dict() for x in self.blocks]} |
| 51 | + |
| 52 | + def as_block(self): |
| 53 | + ret = [] |
| 54 | + for x in self.blocks: |
| 55 | + if isinstance(x, (Key, Comment)): |
| 56 | + ret.append(x.as_block()) |
| 57 | + else: |
| 58 | + for y in x.as_block(): |
| 59 | + ret.append(y) |
| 60 | + return ret |
| 61 | + |
| 62 | + |
| 63 | +class Server(object): |
| 64 | + def __init__(self, *args): |
| 65 | + self.blocks = list(args) |
| 66 | + self.locations = [] |
| 67 | + self.comments = [] |
| 68 | + self.keys = [] |
| 69 | + self.upd() |
| 70 | + |
| 71 | + def add(self, *args): |
| 72 | + self.blocks.extend(args) |
| 73 | + self.upd() |
| 74 | + return self.blocks |
| 75 | + |
| 76 | + def remove(self, *args): |
| 77 | + for x in args: |
| 78 | + self.blocks.remove(x) |
| 79 | + self.upd() |
| 80 | + return self.blocks |
| 81 | + |
| 82 | + def filter(self, btype='', name=''): |
| 83 | + flist = [] |
| 84 | + for x in self.blocks: |
| 85 | + if name and isinstance(x, Key) and x.name == name: |
| 86 | + flist.append(x) |
| 87 | + elif isinstance(x, Container) and x.__class__.__name__ == btype and x.value == name: |
| 88 | + flist.append(x) |
| 89 | + elif not name and btype and x.__class__.__name__ == btype: |
| 90 | + flist.append(x) |
| 91 | + return flist |
| 92 | + |
| 93 | + def upd(self): |
| 94 | + l, c, k = [], [], [] |
| 95 | + for x in self.blocks: |
| 96 | + if isinstance(x, Location): |
| 97 | + l.append(x) |
| 98 | + elif isinstance(x, Comment): |
| 99 | + c.append(x) |
| 100 | + elif isinstance(x, Key): |
| 101 | + k.append(x) |
| 102 | + self.locations, self.comments, self.keys = l, c, k |
| 103 | + |
| 104 | + def all(self): |
| 105 | + return self.blocks |
| 106 | + |
| 107 | + def as_list(self): |
| 108 | + ret = [] |
| 109 | + for x in self.blocks: |
| 110 | + ret.append(x.as_list()) |
| 111 | + return ['server', '', ret] |
| 112 | + |
| 113 | + def as_dict(self): |
| 114 | + return {'server': [x.as_dict() for x in self.blocks]} |
| 115 | + |
| 116 | + def as_block(self): |
| 117 | + ret = [] |
| 118 | + ret.append('server {\n') |
| 119 | + for x in self.blocks: |
| 120 | + if isinstance(x, (Key, Comment)): |
| 121 | + ret.append(INDENT + x.as_block()) |
| 122 | + elif isinstance(x, Container): |
| 123 | + y = x.as_block() |
| 124 | + ret.append('\n'+INDENT+y[0]) |
| 125 | + for z in y[1:]: |
| 126 | + ret.append(INDENT+z) |
| 127 | + ret.append('}\n') |
| 128 | + return ret |
| 129 | + |
| 130 | + |
| 131 | +class Container(object): |
| 132 | + def __init__(self, value, *args): |
| 133 | + self.name = '' |
| 134 | + self.value = value |
| 135 | + self.comments = [] |
| 136 | + self.keys = [] |
| 137 | + self.blocks = list(args) |
| 138 | + self.upd() |
| 139 | + |
| 140 | + def add(self, *args): |
| 141 | + self.blocks.extend(args) |
| 142 | + self.upd() |
| 143 | + return self.blocks |
| 144 | + |
| 145 | + def remove(self, *args): |
| 146 | + for x in args: |
| 147 | + self.blocks.remove(x) |
| 148 | + self.upd() |
| 149 | + return self.blocks |
| 150 | + |
| 151 | + def upd(self): |
| 152 | + c, k = [], [] |
| 153 | + for x in self.blocks: |
| 154 | + if isinstance(x, Comment): |
| 155 | + c.append(x) |
| 156 | + elif isinstance(x, Key): |
| 157 | + k.append(x) |
| 158 | + self.comments, self.keys = c, k |
| 159 | + |
| 160 | + def all(self): |
| 161 | + return self.blocks |
| 162 | + |
| 163 | + def as_list(self): |
| 164 | + ret = [] |
| 165 | + for x in self.blocks: |
| 166 | + ret.append(x.as_list()) |
| 167 | + return [self.name, self.value, ret] |
| 168 | + |
| 169 | + def as_dict(self): |
| 170 | + return {'{} {}'.format(self.name, self.value): [x.as_dict() for x in self.blocks]} |
| 171 | + |
| 172 | + def as_block(self): |
| 173 | + ret = [] |
| 174 | + ret.append('{} {} {{\n'.format(self.name, self.value)) |
| 175 | + for x in self.blocks: |
| 176 | + if isinstance(x, (Key, Comment)): |
| 177 | + ret.append(INDENT + x.as_block()) |
| 178 | + elif isinstance(x, Container): |
| 179 | + y = x.as_block() |
| 180 | + ret.append('\n'+INDENT+INDENT+y[0]) |
| 181 | + for z in y[1:]: |
| 182 | + ret.append(INDENT+z) |
| 183 | + else: |
| 184 | + y = x.as_block() |
| 185 | + ret.append(INDENT+y) |
| 186 | + ret.append('}\n') |
| 187 | + return ret |
| 188 | + |
| 189 | + |
| 190 | +class Comment(object): |
| 191 | + def __init__(self, comment): |
| 192 | + self.comment = comment |
| 193 | + |
| 194 | + def as_list(self): |
| 195 | + return [self.comment] |
| 196 | + |
| 197 | + def as_dict(self): |
| 198 | + return {'#': self.comment} |
| 199 | + |
| 200 | + def as_block(self): |
| 201 | + return '# {}\n'.format(self.comment) |
| 202 | + |
| 203 | + |
| 204 | +class Location(Container): |
| 205 | + def __init__(self, value, *args): |
| 206 | + super(Location, self).__init__(value, *args) |
| 207 | + self.name = 'location' |
| 208 | + |
| 209 | + |
| 210 | +class LimitExcept(Container): |
| 211 | + def __init__(self, value, *args): |
| 212 | + super(LimitExcept, self).__init__(value, *args) |
| 213 | + self.name = 'limit_except' |
| 214 | + |
| 215 | + |
| 216 | +class Types(Container): |
| 217 | + def __init__(self, value, *args): |
| 218 | + super(Types, self).__init__(value, *args) |
| 219 | + self.name = 'types' |
| 220 | + |
| 221 | + |
| 222 | +class If(Container): |
| 223 | + def __init__(self, value, *args): |
| 224 | + super(If, self).__init__(value, *args) |
| 225 | + self.name = 'if' |
| 226 | + |
| 227 | + |
| 228 | +class Upstream(Container): |
| 229 | + def __init__(self, value, *args): |
| 230 | + super(Upstream, self).__init__(value, *args) |
| 231 | + self.name = 'upstream' |
| 232 | + |
| 233 | + |
| 234 | +class Key(object): |
| 235 | + def __init__(self, name, value): |
| 236 | + self.name = name |
| 237 | + self.value = value |
| 238 | + |
| 239 | + def as_list(self): |
| 240 | + return [self.name, self.value] |
| 241 | + |
| 242 | + def as_dict(self): |
| 243 | + return {self.name: self.value} |
| 244 | + |
| 245 | + def as_block(self): |
| 246 | + return '{} {};\n'.format(self.name, self.value) |
| 247 | + |
| 248 | + |
| 249 | +def loads(data, conf=True): |
| 250 | + f = Conf() if conf else [] |
| 251 | + lopen = [] |
| 252 | + for line in data.split('\n'): |
| 253 | + if re.match('\s*server\s*{', line): |
| 254 | + s = Server() |
| 255 | + lopen.insert(0, s) |
| 256 | + if re.match('\s*location.*{', line): |
| 257 | + lpath = re.match('\s*location\s*(.*\S+)\s*{', line).group(1) |
| 258 | + l = Location(lpath) |
| 259 | + lopen.insert(0, l) |
| 260 | + if re.match('\s*if.*{', line): |
| 261 | + ifs = re.match('\s*if\s*(.*\S+)\s*{', line).group(1) |
| 262 | + ifs = If(ifs) |
| 263 | + lopen.insert(0, ifs) |
| 264 | + if re.match('\s*upstream.*{', line): |
| 265 | + ups = re.match('\s*upstream\s*(.*\S+)\s*{', line).group(1) |
| 266 | + u = Upstream(ups) |
| 267 | + lopen.insert(0, u) |
| 268 | + if re.match('.*;', line): |
| 269 | + kname, kval = re.match('.*(?:^|^\s*|{\s*)(\S+)\s(.+);', line).group(1, 2) |
| 270 | + k = Key(kname, kval) |
| 271 | + lopen[0].add(k) |
| 272 | + if re.match('.*}', line): |
| 273 | + closenum = len(re.findall('}', line)) |
| 274 | + while closenum > 0: |
| 275 | + if isinstance(lopen[0], Server): |
| 276 | + f.add(lopen[0]) if conf else f.append(lopen[0]) |
| 277 | + lopen.pop(0) |
| 278 | + elif isinstance(lopen[0], Container): |
| 279 | + c = lopen[0] |
| 280 | + lopen.pop(0) |
| 281 | + if lopen: |
| 282 | + lopen[0].add(c) |
| 283 | + else: |
| 284 | + f.add(c) if conf else f.append(c) |
| 285 | + closenum = closenum - 1 |
| 286 | + if re.match('\s*#\s*', line): |
| 287 | + c = Comment(re.match('\s*#\s*(.*)$', line).group(1)) |
| 288 | + if len(lopen): |
| 289 | + lopen[0].add(c) |
| 290 | + else: |
| 291 | + f.add(c) if conf else f.append(c) |
| 292 | + return f |
| 293 | + |
| 294 | +def load(fobj): |
| 295 | + return loads(fobj.read()) |
| 296 | + |
| 297 | +def loadf(path): |
| 298 | + with open(path, 'r') as f: |
| 299 | + return load(f) |
| 300 | + |
| 301 | +def dumps(obj): |
| 302 | + return ''.join(obj.as_block()) |
| 303 | + |
| 304 | +def dump(obj, fobj): |
| 305 | + fobj.write(dumps(obj)) |
| 306 | + return fobj |
| 307 | + |
| 308 | +def dumpf(obj, path): |
| 309 | + with open(path, 'w') as f: |
| 310 | + dump(obj, f) |
| 311 | + return path |
0 commit comments