Package core :: Module parsetools |
[hide private]
[frames] | no frames]

# Source Code for Module core.parsetools

      1  # Copyright 2011 the original author or authors.
      2  #
      3  # Licensed under the Apache License, Version 2.0 (the "License");
      4  # you may not use this file except in compliance with the License.
      5  # You may obtain a copy of the License at
      6  #
      7  #      http://www.apache.org/licenses/LICENSE-2.0
      8  #
      9  # Unless required by applicable law or agreed to in writing, software
     10  # distributed under the License is distributed on an "AS IS" BASIS,
     11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  # See the License for the specific language governing permissions and
     13  # limitations under the License.
     14
     15  import org.vertx.java.core.parsetools.RecordParser
     16  from core.buffer import Buffer
     17
     18  __author__ = "Scott Horn"
     19  __email__ = "scott@hornmicro.com"
     20  __credits__ = "Based entirely on work by Tim Fox http://tfox.org"
     21
     22 -class RecordParser(object):
     23      """A helper class which allows you to easily parse protocols which are delimited by a sequence of bytes, or fixed
     24      size records.
     25
     26      Instances of this class take as input Buffer instances containing raw bytes, and output records.
     27      For example, if I had a simple ASCII text protocol delimited by '\\n' and the input was the following:
     28
     29      buffer1:HELLO\\nHOW ARE Y
     30      buffer2:OU?\\nI AM
     31      buffer3: DOING OK
     32      buffer4:\\n
     33
     34      Then the output would be:
     35
     36      buffer1:HELLO
     37      buffer2:HOW ARE YOU?
     38      buffer3:I AM DOING OK
     39
     40      Instances of this class can be changed between delimited mode and fixed size record mode on the fly as
     41      individual records are read, this allows you to parse protocols where, for example, the first 5 records might
     42      all be fixed size (of potentially different sizes), followed by some delimited records, followed by more fixed
     43      size records.
     44
     45      Instances of this class can't currently be used for protocols where the text is encoded with something other than
     46      a 1-1 byte-char mapping. TODO extend this class to cope with arbitrary character encodings.
     47      """
     48
     49 -    def __init__(self, java_parser):
     50          self.java_parser = java_parser
     51
     52 -    def __call__(self, data):
     53          self.input(data)
     54
     55 -    def input(self, data):
     56          """This method is called to provide the parser with data.
     57          @param data: Input buffer  to the parser.
     58          """
     59          self.java_parser.handle(data._to_java_buffer())
     60
     61      @staticmethod
     62 -    def new_delimited(delim, handler):
     63          """Create a new RecordParser instance, initially in delimited mode, and where the delimiter can be represented
     64          by a delimiter string endcoded in latin-1 . Don't use this if your String contains other than latin-1 characters.
     65
     66          Keyword arguments:
     67          @param delim: The delimiter string.
     68          @param handler: the output handler
     69
     70          @return: a new RecordParser
     71          """
     72          return RecordParser(org.vertx.java.core.parsetools.RecordParser.newDelimited(delim, RecordParserHandler(handler)))
     73
     74      @staticmethod
     75 -    def new_fixed(size, handler):
     76          """Create a new RecordParser instance, initially in fixed size mode.
     77
     78          Keyword arguments:
     79          @param size: the initial record size.
     80          @param handler: the output handler
     81
     82          @return: a new RecordParser
     83          """
     84          return RecordParser(org.vertx.java.core.parsetools.RecordParser.newFixed(size, RecordParserHandler(handler)))
     85
     86 -    def delimited_mode(self, delim):
     87          """Flip the parser into delimited mode. This method can be called multiple times with different values
     88          of delim while data is being parsed.
     89
     90          Keyword arguments:
     91          @param delim: the delimiter string.
     92          """
     93          self.java_parser.delimitedMode(delim)
     94
     95 -    def fixed_size_mode(self, size):
     96          """Flip the parser into fixed size mode. This method can be called multiple times with different values
     97          of size while data is being parsed.
     98
     99          Keyword arguments:
    100          @param size: the record size.
    101          """
    102          self.java_parser.fixedSizeMode(size)
    103
    104 -class RecordParserHandler(org.vertx.java.core.Handler):
    105      """ Record Parser handler """
    106 -    def __init__(self, handler):
    107          self.handler = handler
    108
    109 -    def handle(self, buffer):
    110          """ Call the handler after buffer parsed"""
    111          self.handler(Buffer(buffer))
    112