Esempio n. 1
0
nilvalue = Word("-")
digit = Regex("[0-9]{1}")
nonzero_digit = Regex("[1-9]{1}")
printusascii = printables
sp = White(" ", exact=1)
octet = Regex("[\x00-\xFF]")
utf_8_string = Regex("[\x00-\xFF]*")
BOM = "\xef\xbb\xbf"
bom = Regex(BOM)
msg_utf8 = bom + utf_8_string
msg_any = utf_8_string
msg = Combine(Or([msg_utf8, msg_any])).setResultsName("MSG")
sd_name = CharsNotIn('= ]"', 1, 32)
param_name = sd_name.setResultsName("SD_PARAM_NAME")
param_value = QuotedString(quoteChar='"', escChar="\\", multiline=True)
param_value = param_value.setResultsName("SD_PARAM_VALUE")
sd_id = sd_name.setResultsName("SD_ID")
sd_param = Group(param_name + Regex("=") + param_value)
sd_params = Group(ZeroOrMore(Group(sp + sd_param.setResultsName("SD_PARAM"))))
sd_element = Group("[" + sd_id + sd_params.setResultsName("SD_PARAMS") + "]")
sd_element = sd_element.setResultsName("SD_ELEMENT")
sd_elements = Group(OneOrMore(sd_element))
structured_data = Or([nilvalue, sd_elements.setResultsName("SD_ELEMENTS")])
structured_data = structured_data.setResultsName("STRUCTURED_DATA")
time_hour = Regex("0[0-9]|1[0-9]|2[0-3]")
time_minute = Regex("[0-5][0-9]")
time_second = time_minute
time_secfrac = Regex("\.[0-9]{1,6}")
time_numoffset = Or([Regex("\+"), Regex("-")]) + time_hour + ":" + time_minute
time_offset = Or([Regex("Z"), time_numoffset])
partial_time = time_hour + ":" + time_minute + ":" + time_second + Optional(time_secfrac)
Esempio n. 2
0
from pyparsing import Literal, OneOrMore, QuotedString, SkipTo, stringEnd


"""Givent a string, this parser skips to the starting charcters '<%',
returning everything it skips over as a token. Everything that is
between '<%=' and '%>' are returned as a token labeled
assignment. This is repeated until we run out of '<%' at which point
we return everything up to the end of a string as a token."""

startExp = Literal('<%')
assignExp = QuotedString('<%=', endQuoteChar='%>', multiline=True)
assignExp = assignExp.setResultsName('assignment')

ERBParser = OneOrMore(SkipTo(startExp) + assignExp) + SkipTo(stringEnd)
ERBParser.leaveWhitespace()

class ERBTemplate(object):
    def loadFromFile(self, fileName):
        tokens = ERBParser.parseFile(fileName)

        # Create a dictionary where the key is the position the token
        # was found and and the value is the token's name as set by setResultsName

        typeByPos  = dict( [ (v[1],k) for (k,vlist) in tokens._ParseResults__tokdict.items()
                             for v in vlist ] )
        self.typeByPos = typeByPos
        self.tokens = tokens

    def render(self, context):
        tokens = self.tokens
        tokenCount = len(tokens)
Esempio n. 3
0
nilvalue = Word("-")
digit = Regex("[0-9]{1}")
nonzero_digit = Regex("[1-9]{1}")
printusascii = printables
sp = White(" ", exact=1)
octet = Regex('[\x00-\xFF]')
utf_8_string = Regex('[\x00-\xFF]*')
BOM = '\xef\xbb\xbf'
bom = Regex(BOM)
msg_utf8 = bom + utf_8_string
msg_any = utf_8_string
msg = Combine(Or([msg_utf8, msg_any])).setResultsName('MSG')
sd_name = CharsNotIn('= ]"', 1, 32)
param_name = sd_name.setResultsName('SD_PARAM_NAME')
param_value = QuotedString(quoteChar='"', escChar='\\', multiline=True)
param_value = param_value.setResultsName('SD_PARAM_VALUE')
sd_id = sd_name.setResultsName('SD_ID')
sd_param = Group(param_name + Regex('=') + param_value)
sd_params = Group(ZeroOrMore(Group(sp + sd_param.setResultsName('SD_PARAM'))))
sd_element = Group('[' + sd_id + sd_params.setResultsName('SD_PARAMS') + ']')
sd_element = sd_element.setResultsName('SD_ELEMENT')
sd_elements = Group(OneOrMore(sd_element))
structured_data = Or([nilvalue, sd_elements.setResultsName('SD_ELEMENTS')])
structured_data = structured_data.setResultsName('STRUCTURED_DATA')
time_hour = Regex('0[0-9]|1[0-9]|2[0-3]')
time_minute = Regex('[0-5][0-9]')
time_second = time_minute
time_secfrac = Regex('\.[0-9]{1,6}')
time_numoffset = Or([Regex('\+'), Regex('-')]) + time_hour + ':' + time_minute
time_offset = Or([Regex('Z'), time_numoffset])
partial_time = time_hour + ':' + time_minute + ':' + time_second + \
Esempio n. 4
0
    def __init__(self):
        intNum = Word(nums)
        floatNum = Combine(intNum + Optional("." + intNum))
        string = QuotedString("'") | QuotedString('"')
        regex = QuotedString("/")
        ident = Word( alphas, alphanums + "_" )
        time_period = Keyword("minutes") | Keyword("seconds")

        ordering = Keyword("unique") | Keyword("random")
        string_type = Keyword("random").setResultsName("ordering") + \
            Keyword("string").setResultsName("data_type") + \
            Keyword("of") + Keyword("length") + \
            intNum.setResultsName("length")
        numeric_type = ordering.setResultsName("ordering") + \
            Keyword("number").setResultsName("data_type") + Keyword("from") + \
            floatNum.setResultsName("min") + Keyword("to") + \
            floatNum.setResultsName("max")
        var_type = string_type | numeric_type
        var = Group(Keyword("var").setResultsName("type") + \
            ident.setResultsName("name") +  Keyword("is") + \
            Keyword("a") + var_type)


        ident_list = delimitedList( ident )
        using_ordering = Keyword("randomly") | Keyword("sequentially")
        using = Group(Keyword("using").setResultsName("type") + \
            ident_list.setResultsName("vars")  + Keyword("from") + \
            string.setResultsName("filename") + \
            using_ordering.setResultsName("ordering"))

        pause = Group(Keyword("pause").setResultsName("type") + \
            Keyword("between") + \
            intNum.setResultsName("lower_time") + Keyword("and") + \
            intNum.setResultsName("upper_time") + Keyword("seconds"))

        get = Keyword("get").setResultsName("method")
        post = Keyword("post").setResultsName("method")
        put = Keyword("put").setResultsName("method")
        delete = Keyword("delete").setResultsName("method")
        method = (get | post | put | delete).setResultsName("type")

        url = string.setResultsName("url")
        data = Keyword("with") + Keyword("data") + \
            string.setResultsName("data")

        match = Group( \
            Keyword("ensure") + Keyword("match") + \
            regex.setResultsName("regex"))
        match_list = Group(OneOrMore(match)).setResultsName("matches")

        request = Group(method + \
            Optional(Keyword("all")).setResultsName("all") + \
            url + Optional(data) + Optional(match_list)).setName("request")
        action = request | pause | var | using
        action_list = \
            Group(OneOrMore(action)).setResultsName("actions")

        session = Group( Keyword("create") + \
            Keyword("session") + Keyword("with") + \
            Keyword("weight") + \
            intNum.setResultsName("weight")  + Keyword("as") + \
            string.setResultsName("name")  + \
            ":" + action_list)
        session_list = OneOrMore(session).setResultsName("sessions")
        
        spawn = Group( Keyword("spawn") + \
            Keyword("users") + Keyword("every") + \
            intNum.setResultsName("user_time") + \
            time_period.setResultsName("user_time_units") + \
            Keyword("for") + \
            intNum.setResultsName("max_duration") + \
            time_period.setResultsName("max_duration_units") + \
            Optional( Keyword("up") + Keyword("to") + \
            intNum.setResultsName("max_users") +  Keyword("users")))
        spawn_list = OneOrMore(spawn).setResultsName("spawns")
        load = Group( Keyword("create") + Keyword("load") + ":" + \
            spawn_list).setResultsName("load")

        comment = "#" + restOfLine

        script = session_list + load
        script.ignore(comment)

        self.grammar = script