|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | + |
| 19 | +try: |
| 20 | + from pygments import highlight |
| 21 | + from pygments.console import ansiformat |
| 22 | + from pygments.formatter import Formatter |
| 23 | + from pygments.formatters import Terminal256Formatter |
| 24 | + from pygments.lexer import bygroups, include, RegexLexer |
| 25 | + from pygments.token import * |
| 26 | + |
| 27 | + import sys |
| 28 | +except ImportError, e: |
| 29 | + print e |
| 30 | + |
| 31 | + |
| 32 | +MONKEY_COLORS = { |
| 33 | + Token: '', |
| 34 | + Whitespace: 'reset', |
| 35 | + Text: 'reset', |
| 36 | + |
| 37 | + Name: 'green', |
| 38 | + Operator: 'teal', |
| 39 | + Operator.Word: 'lightgray', |
| 40 | + String: 'purple', |
| 41 | + |
| 42 | + Keyword: '_red_', |
| 43 | + Error: 'red', |
| 44 | + Literal: 'yellow', |
| 45 | + Number: 'blue', |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +def get_colorscheme(): |
| 50 | + return MONKEY_COLORS |
| 51 | + |
| 52 | + |
| 53 | +class MonkeyLexer(RegexLexer): |
| 54 | + keywords = ['[a-z]*id', '[a-zA-Z]*:'] |
| 55 | + attributes = ['[Tt]rue', '[Ff]alse'] |
| 56 | + params = ['[a-z]*[Nn]ame', 'type', '[Ss]tate'] |
| 57 | + |
| 58 | + uuid_rgx = r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' |
| 59 | + date_rgx = r'[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9:]{8}-[0-9]{4}' |
| 60 | + |
| 61 | + def makelistre(lis): |
| 62 | + return r'(' + r'|'.join(lis) + r')' |
| 63 | + |
| 64 | + tokens = { |
| 65 | + 'root': [ |
| 66 | + (r' ', Whitespace), |
| 67 | + (date_rgx, Number), |
| 68 | + (uuid_rgx, Literal), |
| 69 | + (r'(?:\b\d+\b(?:-\b\d+|%)?)', Number), |
| 70 | + (r'^[-=]*\n', Operator.Word), |
| 71 | + (r'Error', Error), |
| 72 | + (makelistre(keywords), Keyword), |
| 73 | + (makelistre(attributes), Literal), |
| 74 | + (makelistre(params) + r'( = )(.*)', bygroups(Name, Operator, |
| 75 | + String)), |
| 76 | + (makelistre(params), Name), |
| 77 | + (r'(^[a-zA-Z]* )(=)', bygroups(Name, Operator)), |
| 78 | + (r'\S+', Text), |
| 79 | + ] |
| 80 | + } |
| 81 | + |
| 82 | + def analyse_text(text): |
| 83 | + npos = text.find('\n') |
| 84 | + if npos < 3: |
| 85 | + return False |
| 86 | + return text[0] == '[' and text[npos - 1] == ']' |
| 87 | + |
| 88 | + |
| 89 | +class MonkeyFormatter(Formatter): |
| 90 | + def __init__(self, **options): |
| 91 | + Formatter.__init__(self, **options) |
| 92 | + self.colorscheme = get_colorscheme() |
| 93 | + |
| 94 | + def format(self, tokensource, outfile): |
| 95 | + self.encoding = outfile.encoding |
| 96 | + return Formatter.format(self, tokensource, outfile) |
| 97 | + |
| 98 | + def format_unencoded(self, tokensource, outfile): |
| 99 | + for ttype, value in tokensource: |
| 100 | + color = self.colorscheme.get(ttype) |
| 101 | + while color is None: |
| 102 | + ttype = ttype[:-1] |
| 103 | + color = self.colorscheme.get(ttype) |
| 104 | + if color: |
| 105 | + spl = value.split('\n') |
| 106 | + for line in spl[:-1]: |
| 107 | + if line: |
| 108 | + outfile.write(ansiformat(color, line)) |
| 109 | + outfile.write('\n') |
| 110 | + if spl[-1]: |
| 111 | + outfile.write(ansiformat(color, spl[-1])) |
| 112 | + else: |
| 113 | + outfile.write(value) |
| 114 | + |
| 115 | + |
| 116 | +def monkeyprint(text): |
| 117 | + fmter = MonkeyFormatter() |
| 118 | + lexer = MonkeyLexer() |
| 119 | + lexer.encoding = 'utf-8' |
| 120 | + fmter.encoding = 'utf-8' |
| 121 | + highlight(text, lexer, fmter, sys.stdout) |
0 commit comments