Welcome, guest | Sign In | My Account | Store | Cart

A parsing engine that allows you to define sets of patterns and callbacks, and process any I/O object in Pyton that has a readline() method.

Python, 164 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env python
####################################################################################
#                                                                                  #
# Copyright (c) 2013, Mike 'Fuzzy' Partin <fuzzy@fu-manchu.org>                    #
# All rights reserved.                                                             #
#                                                                                  #
# Redistribution and use in source and binary forms, with or without               #
# modification, are permitted provided that the following conditions are met:      #
#                                                                                  #
# 1. Redistributions of source code must retain the above copyright notice, this   #
#    list of conditions and the following disclaimer.                              #
# 2. Redistributions in binary form must reproduce the above copyright notice,     #
#    this list of conditions and the following disclaimer in the documentation     #
#    and/or other materials provided with the distribution.                        #
#                                                                                  #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND  #
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED    #
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           #
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR  #
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES   #
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;     #
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND      #
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       #
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS    # 
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                     #
#                                                                                  #
# The views and conclusions contained in the software and documentation are those  #
# of the authors and should not be interpreted as representing official policies,  #
# either expressed or implied, of the FreeBSD Project.                             #
#                                                                                  #
####################################################################################

############
## __NOTE__
############
#
# This is the processing engine code that provides the core of the DSL
#

import re, types

class InvalidArgument(Exception):
    """ Inavlid argument exception """
    pass
    
class Slurp:
    
    def __init__(self, ioObj=None):
        """ Instantiate a Slurp object
        
        Example:
        
        obj = Slurp(open('/etc/fstab', 'r'))
        # This will work for sockets and FIFO's as well
        
        """
        if ioObj != None:
            self.ioObj    = ioObj
            self.triggers = []
        else:
            raise InvalidArgument("A valid I/O object (file, socket, fifo) is required.\n")
            
    def register(self, patt=None, cback=None, final=True):
        """ Register a callback and associated pattern
        
        Slurp.register(patt=None, cback=None, final=True)
        
        Register a handler, using cback, when matching patt.
        If final is True, then Slurp will stop processing for that trigger, once it has
        matched it (this is on a per line basis).
        
        Example:
        
        obj = Slurp(open('/etc/fstab', 'r'))
        # Print only commented out lines
        obj.register('^#.*$', print)
        # *or*
        obj.register('^#.*$', print, False) # This will allow multiple callbacks to match        
        
        """
        # Check argument non-None..ness first and foremost
        if patt != None and cback != None:
            # Then check types
            if type(patt) != types.StringType or type(cback) != types.FunctionType:
                raise InvalidArgument
            else:
                # Check to see that our list of callbacks doesn't have one of these
                # instances already in it.
                for itm in self.triggers:
                    if itm[0] == patt:
                        return False
                # Ok, passed that, lets add it in
                self.triggers.append([patt, cback, final])
                return True
        else:
            raise InvalidArgument
            
    def process(self):
        """ Begin processing a given stream

        Example:

        obj = Slurp(open('/etc/fstab', 'r'))
        obj.register('^#.*$', print)
        obj.process()

        """
        # CAVEAT: The IO object *MUST* have a .readline() interface. This isn't much
        # of a showstopper as it's common, but a simple workaround would be to impliment
        # my own readline method (scanning for \n would work reasonably well) here inside
        # of Slurp to fall back on. I haven't hit a need for that yet, but this should
        # serve to remind me about it, if it ever comes up.
        buff = self.ioObj.readline()
        while buff:
            for pair in self.triggers:
                tmp_re = re.compile(pair[0])
                if tmp_re.match(buff):
                    # Call the callback with the line of text as the only argument
                    pair[1](buff.strip())
                    # Now lets see if we are a final measure
                    if pair[2] == False:
                        break
            # And refill the well.
            buff = self.ioObj.readline()

############
## __NOTE__
############
#
# This is the main execution logic
#

import sy
s
class Builder:
    def __init__(self, ioObj=None):
        if hasattr(ioObj, 'readline'):
            self.obj   = {}
            self.Slurp = Slurp(ioObj)
            handlers   = [
                # set the package name and version
                # Example: package 'ruby', '2.0.0-p0'
                ('^package\ +.*$',
                 self.setPackage),
                # set the uri or uri's for the package
                # Example: fetch_ur <uri>, <uri>, <uri>
                ('^fetch_uri\ +.*$', 
                 self.setUris),
                #
                ('^depends\ +.*$',
                 self.addDepends),
                #
                ('^configure_args\ +.*$',
                 self.configureArgs),
                #
                ('^make_args\ +.*$',
                 self.makeArgs),
            ]

    def setPackage(self):
        pass
    
    def setUris(self):
        pass