Package qpid :: Package messaging :: Module address
[hide private]
[frames] | no frames]

Source Code for Module qpid.messaging.address

  1  # 
  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  import re 
 20  from qpid.lexer import Lexicon, LexError 
 21  from qpid.parser import Parser, ParseError 
 22   
 23  l = Lexicon() 
 24   
 25  LBRACE = l.define("LBRACE", r"\{") 
 26  RBRACE = l.define("RBRACE", r"\}") 
 27  LBRACK = l.define("LBRACK", r"\[") 
 28  RBRACK = l.define("RBRACK", r"\]") 
 29  COLON = l.define("COLON", r":") 
 30  SEMI = l.define("SEMI", r";") 
 31  SLASH = l.define("SLASH", r"/") 
 32  COMMA = l.define("COMMA", r",") 
 33  NUMBER = l.define("NUMBER", r'[+-]?[0-9]*\.?[0-9]+') 
 34  ID = l.define("ID", r'[a-zA-Z_](?:[a-zA-Z0-9_-]*[a-zA-Z0-9_])?') 
 35  STRING = l.define("STRING", r""""(?:[^\\"]|\\.)*"|'(?:[^\\']|\\.)*'""") 
 36  ESC = l.define("ESC", r"\\[^ux]|\\x[0-9a-fA-F][0-9a-fA-F]|\\u[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]") 
 37  SYM = l.define("SYM", r"[.#*%@$^!+-]") 
 38  WSPACE = l.define("WSPACE", r"[ \n\r\t]+") 
 39  EOF = l.eof("EOF") 
 40   
 41  LEXER = l.compile() 
 42   
43 -def lex(st):
44 return LEXER.lex(st)
45
46 -def tok2str(tok):
47 if tok.type is STRING: 48 return eval(tok.value) 49 elif tok.type is ESC: 50 if tok.value[1] == "x": 51 return eval('"%s"' % tok.value) 52 elif tok.value[1] == "u": 53 return eval('u"%s"' % tok.value) 54 else: 55 return tok.value[1] 56 else: 57 return tok.value
58 59 CONSTANTS = { 60 "True": True, 61 "true": True, 62 "False": False, 63 "false": False, 64 "None": None 65 } 66
67 -def tok2obj(tok):
68 if tok.type == ID: 69 return CONSTANTS.get(tok.value, tok.value) 70 elif tok.type in (STRING, NUMBER): 71 return eval(tok.value) 72 else: 73 return tok.value
74
75 -def toks2str(toks):
76 if toks: 77 return "".join(map(tok2str, toks)) 78 else: 79 return None
80
81 -class AddressParser(Parser):
82
83 - def __init__(self, tokens):
84 Parser.__init__(self, [t for t in tokens if t.type is not WSPACE])
85
86 - def parse(self):
87 result = self.address() 88 self.eat(EOF) 89 return result
90
91 - def address(self):
92 name = toks2str(self.eat_until(SLASH, SEMI, EOF)) 93 94 if name is None: 95 raise ParseError(self.next()) 96 97 if self.matches(SLASH): 98 self.eat(SLASH) 99 subject = toks2str(self.eat_until(SEMI, EOF)) 100 else: 101 subject = None 102 103 if self.matches(SEMI): 104 self.eat(SEMI) 105 options = self.map() 106 else: 107 options = None 108 return name, subject, options
109
110 - def map(self):
111 self.eat(LBRACE) 112 113 result = {} 114 while True: 115 if self.matches(NUMBER, STRING, ID, LBRACE, LBRACK): 116 n, v = self.keyval() 117 result[n] = v 118 if self.matches(COMMA): 119 self.eat(COMMA) 120 elif self.matches(RBRACE): 121 break 122 else: 123 raise ParseError(self.next(), COMMA, RBRACE) 124 elif self.matches(RBRACE): 125 break 126 else: 127 raise ParseError(self.next(), NUMBER, STRING, ID, LBRACE, LBRACK, 128 RBRACE) 129 130 self.eat(RBRACE) 131 return result
132
133 - def keyval(self):
134 key = self.value() 135 self.eat(COLON) 136 val = self.value() 137 return (key, val)
138
139 - def value(self):
140 if self.matches(NUMBER, STRING, ID): 141 return tok2obj(self.eat()) 142 elif self.matches(LBRACE): 143 return self.map() 144 elif self.matches(LBRACK): 145 return self.list() 146 else: 147 raise ParseError(self.next(), NUMBER, STRING, ID, LBRACE, LBRACK)
148
149 - def list(self):
150 self.eat(LBRACK) 151 152 result = [] 153 154 while True: 155 if self.matches(RBRACK): 156 break 157 else: 158 result.append(self.value()) 159 if self.matches(COMMA): 160 self.eat(COMMA) 161 elif self.matches(RBRACK): 162 break 163 else: 164 raise ParseError(self.next(), COMMA, RBRACK) 165 166 self.eat(RBRACK) 167 return result
168
169 -def parse(addr):
170 return AddressParser(lex(addr)).parse()
171 172 __all__ = ["parse", "ParseError"] 173