| | 1317 | class Base64(Variable): |
| | 1318 | """Matches a base64 encoded string. |
| | 1319 | |
| | 1320 | >>> from cly.parser import Parser |
| | 1321 | >>> parser = Parser(Grammar(foo=Base64())) |
| | 1322 | >>> parser.parse('Y2x5').vars['foo'] |
| | 1323 | 'cly' |
| | 1324 | >>> parser.parse('aXM=').vars['foo'] |
| | 1325 | 'is' |
| | 1326 | >>> parser.parse('Y29vbA==').vars['foo'] |
| | 1327 | 'cool' |
| | 1328 | >>> # Fails because there is not enough chars |
| | 1329 | >>> parser.parse('Y2x').vars.get('foo') |
| | 1330 | >>> # Fails because there are too many '=' pad characters |
| | 1331 | >>> parser.parse('Y2x5=').vars.get('foo') |
| | 1332 | >>> # Fails because there are too many '=' pad characters |
| | 1333 | >>> parser.parse('Y2x5==').vars.get('foo') |
| | 1334 | >>> # Fails because there are too many '=' pad characters |
| | 1335 | >>> parser.parse('Y2x5===').vars.get('foo') |
| | 1336 | >>> # Fails because there are trailing characters |
| | 1337 | >>> parser.parse('Y29vbA==abc').vars.get('foo') |
| | 1338 | """ |
| | 1339 | pattern = r"""([A-Za-z0-9+/]{4})*(?:([A-Za-z0-9+/]{2}==)|([A-Za-z0-9+/]{3}=))?""" |
| | 1340 | |
| | 1341 | def parse(self, context, match): |
| | 1342 | return match.group().decode('base64_codec') |
| | 1343 | |
| | 1344 | |