Example #1
0
    def parse_puppetfile_forge_mods(cls, file_contents):
        mod_kwargs = {}
        quotedString = QuotedString('"') ^ QuotedString('\'')

        forge_url_grammar = Suppress('forge') + quotedString
        for url, in forge_url_grammar.searchString(file_contents):
            mod_kwargs['forge_url'] = url

        mod_grammar = Suppress('mod') + quotedString + Suppress(',') + quotedString
        mods = mod_grammar.searchString(file_contents)
        for mod, version in mods:
            user, mod_name = mod.split('/')
            yield cls(user, mod_name, version, **mod_kwargs)
Example #2
0
    def parse_puppetfile_forge_mods(cls, file_contents):
        mod_kwargs = {}
        quotedString = QuotedString('"') ^ QuotedString('\'')

        forge_url_grammar = Suppress('forge') + quotedString
        for url, in forge_url_grammar.searchString(file_contents):
            mod_kwargs['forge_url'] = url

        mod_grammar = Suppress('mod') + quotedString + Suppress(
            ',') + quotedString
        mods = mod_grammar.searchString(file_contents)
        for mod, version in mods:
            user, mod_name = mod.split('/')
            yield cls(user, mod_name, version, **mod_kwargs)
Example #3
0
    def parse_grd_file(self):
        global __line

        try:
            f = open(self.grd_file, 'r')
        except:
            raise

        for j in range(0, 6):
            __line = f.next()

        (self.nx, self.ny) = map(int, __line.split())

        print "Grid file %s (%d, %d)" % (self.grd_file, self.nx, self.ny)

        f.close()

        floatNumber = Regex(r'-?\d+(\.\d*)?([eE][\+-]\d+)?').setParseAction(lambda s, l, t: [float(t[0])])
        integer = Word(nums).setParseAction(lambda s, l, t: [long(t[0])])
        grdline = Suppress('ETA=') + Suppress(integer) + OneOrMore(floatNumber)

        for a in grdline.searchString(file(self.grd_file).read()):
            if len(self.x) < self.ny:
                self.x.append(a.asList())
            else:
                self.y.append(a.asList())
Example #4
0
    def parse_grd_file(self):
        global __line

        try:
            f = open(self.grd_file, 'r')
        except:
            raise

        for j in range(0, 6):
            __line = f.next()

        (self.nx, self.ny) = map(int, __line.split())

        print "Grid file %s (%d, %d)" % (self.grd_file, self.nx, self.ny)

        f.close()

        floatNumber = Regex(r'-?\d+(\.\d*)?([eE][\+-]\d+)?').setParseAction(
            lambda s, l, t: [float(t[0])])
        integer = Word(nums).setParseAction(lambda s, l, t: [long(t[0])])
        grdline = Suppress('ETA=') + Suppress(integer) + OneOrMore(floatNumber)

        for a in grdline.searchString(file(self.grd_file).read()):
            if len(self.x) < self.ny:
                self.x.append(a.asList())
            else:
                self.y.append(a.asList())
Example #5
0
    def _query_to_dict(self, query):
        from pyparsing import Word, alphas, alphanums, oneOf
        from pyparsing import delimitedList, Suppress
        
        operator = oneOf("= > >= < <= like not not like")
        
        _from = Suppress('FROM') + Word(alphanums)
        _select = Suppress('SELECT') + Suppress('(') + delimitedList(Word(alphanums)) + Suppress(')')

        d = {}

        try:
            d['FROM'] = _from.searchString(query)[0][0]
            d['SELECT'] = _select.searchString(query)[0]
        except IndexError:
            return

        return d
Example #6
0
    def get_network_name(self):
        """
        Retruns the name of the network

        Example
        ---------------
        >>> from pgmpy.readwrite import BIFReader
        >>> reader = BIF.BifReader("bif_test.bif")
        >>> reader.network_name()
        'Dog-Problem'
        """
        start = self.network.find('network')
        end = self.network.find('}\n', start)
        # Creating a network attribute
        network_attribute = Suppress('network') + Word(alphanums + '_' + '-') + '{'
        network_name = network_attribute.searchString(self.network[start:end])[0][0]

        return network_name
Example #7
0
    def get_network_name(self):
        """
        Returns the name of the network

        Example
        ---------------
        >>> from ProbabilityModel.readwrite import BIFReader
        >>> reader = BIF.BifReader("bif_test.bif")
        >>> reader.network_name()
        'Dog-Problem'
        """
        start = self.network.find("network")
        end = self.network.find("}\n", start)
        # Creating a network attribute
        network_attribute = Suppress("network") + Word(alphanums + "_" +
                                                       "-") + "{"
        network_name = network_attribute.searchString(
            self.network[start:end])[0][0]

        return network_name
Example #8
0
 def getParametersFromForm(self):
     p = Suppress('{') + Word(alphanums) + Suppress('}')
     rez = p.searchString(self.form)
     params = OrderedDict().fromkeys([pp[0] for pp in rez.asList()], 0)
     return params
Example #9
0
#################
print("Example of an extractor")
print("----------------------")

# simple grammar to match #define's
ident = Word(alphas, alphanums + "_")
macroDef = (Literal("#define") + ident.setResultsName("name") + "=" +
            restOfLine.setResultsName("value"))
for t, s, e in macroDef.scanString(testData):
    print(t.name, ":", t.value)

# or a quick way to make a dictionary of the names and values
# (return only key and value tokens, and construct dict from key-value pairs)
# - empty ahead of restOfLine advances past leading whitespace, does implicit lstrip during parsing
macroDef = Suppress("#define") + ident + Suppress("=") + empty + restOfLine
macros = dict(list(macroDef.searchString(testData)))
print("macros =", macros)
print()

#################
print("Examples of a transformer")
print("----------------------")

# convert C++ namespaces to mangled C-compatible names
scopedIdent = ident + OneOrMore(Literal("::").suppress() + ident)
scopedIdent.setParseAction(lambda t: "_".join(t))

print("(replace namespace-scoped names with C-compatible names)")
print(scopedIdent.transformString(testData))

Example #10
0
def main(argv):
    """
    """
    group = OptionGroup("example", "OptionGroup Example", \
        "Shows all example options",
        option_list = [
            make_option("--example",
                        action="store_true",
                        dest="example",
                        help="An example option."),
        ]
    )

    parser = OptionParser("NAMES ...",
        description="A simple gobject.option example.",
        option_list = [
            make_option("--file", "-f",
                        type="filename",
                        action="store",
                        dest="file",
                        help="A filename option"),
            # ...
        ])

    parser.add_option_group(group)

    parser.parse_args()
    
    lineBody = SkipTo(lineEnd).setParseAction(mustBeNonBlank)

    # now define a line with a trailing lineEnd, to be replaced with a space character
    textLine = lineBody + Suppress(lineEnd).setParseAction(replaceWith(" "))

    # define a paragraph, with a separating lineEnd, to be replaced with a double newline
    para = OneOrMore(textLine) + Suppress(lineEnd).setParseAction(replaceWith("\n\n"))

    # run a test
    test = """
        Now is the
        time for
        all
        good men
        to come to

        the aid of their
        country.
    """
    print para.transformString(test)

    subdir_dir = Word(alphas, alphanums+"_")
    #macroDef = Suppress("SUBDIRS") + Suppress("=") + restOfLine
    subdir_dir_list = Suppress("SUBDIRS") + Suppress("=") + delimitedList( subdir_dir, " ", combine=True )
    macros = list(subdir_dir_list.searchString(subdirs_1line))
    print("1: subdirs of subdirs_1line are {}".format(macros))

    subdir_dir_list = Suppress("SUBDIRS") + Suppress("=") + delimitedList( subdir_dir, " ", combine=True )
    macros = list(subdir_dir_list.searchString(subdirs_2line))
    print("2: subdirs of subdirs_2line are {}".format(macros))

    subdir_dir_list = Suppress("SUBDIRS") + Suppress("=") + delimitedList( subdir_dir, " ", combine=False)
    macros = list(subdir_dir_list.searchString(subdirs_2line))
    print("2: subdirs of subdirs_2line are {}".format(macros))

    subdir_dir_list = Suppress("SUBDIRS") + Suppress("=") + \
        delimitedList( subdir_dir, " ", combine=False) + \
        Suppress(lineEnd)
    macros = list(subdir_dir_list.searchString(subdirs_3line))
    print("3: subdirs of subdirs_3line are {}".format(macros))

    # This returns a list!
    subdir_dir_list = Suppress("SUBDIRS") + Suppress("=") + \
        delimitedList( subdir_dir, " ", combine=False) + \
        SkipTo(lineEnd)
    macros = list(subdir_dir_list.searchString(subdirs_3line))
    print("3: subdirs of subdirs_3line are {}".format(macros))

    # This returns nothing because SUBDIRS is commented out!
    subdir_dir_list = Suppress(lineStart) + Suppress("SUBDIRS") + \
        Suppress("=") + delimitedList( subdir_dir, " ", combine=False) + \
        SkipTo(lineEnd)
    macros = list(subdir_dir_list.searchString(subdirs_4line))
    print("4: subdirs of subdirs_4line are {}".format(macros))

    # This returns 
    subdir_dir_list = Suppress(lineStart) + Suppress("SUBDIRS") + \
        Suppress("=") + delimitedList( subdir_dir, OneOrMore(" "), combine=False) + \
        SkipTo(lineEnd)
    macros = list(subdir_dir_list.searchString(subdirs_5line))
    print("5: subdirs of subdirs_5line are {}".format(macros))
    print("5a: subdirs of subdirs_5line are {}".\
        format(subdir_dir_list.scanString(subdirs_5line)))

    print "group: example ", group.values.example
    print "parser: file", parser.values.file

    res = 1
    try:
        pyparsing()
        print('{}'.format("pyparsing() done."))
        lun_q = r'''Lun:\s*(\d+(?:\s+\d+)*)'''
        s = '''Lun: 0 1 2 3 295 296 297 298'''

        r = re.search(lun_q, s)
        luns = None
        if r:
            luns = r.group(1).split()

            # optionally, also convert luns from strings to integers
            luns = [int(lun) for lun in luns]
            print('luns: {}'.format(luns))
        else:
            print('{}'.format("Got no Luns"))

        print('smthng: {}'.format(smthng()))
        print(re.findall(r"####(.*?)\s(.*?)\s####", string, re.DOTALL))
        
        # Catches testsuite in [SUBDIRS =   testsuite]
        print('1: {}'.format(re.findall(r'''^SUBDIRS =\s+(.*)''', \
            subdirs_1line, re.MULTILINE)))

        # Catches ['SUBDIRS', 'testsuite', 'src'] in [SUBDIRS =   testsuite src]
        # *** THIS IS THE 1! ***
        # Works for an 
        print('2: {}'.format(re.findall(r'''=\s*(\w+(?:\s+\w+)*)\s*''', \
            subdirs_2line, re.MULTILINE)))
        for iter in re.finditer(r'''(\w+)\s+''', \
            subdirs_2line, re.MULTILINE | re.VERBOSE):
            print('iter: {}'.format(iter.span()))

        # Catches ['SUBDIRS', 'testsuite', 'src'] in [SUBDIRS =   testsuite src]
        print('3: {}'.format(re.findall(
            r'''SUBDIRS\s*=\s*(\w+(?:\s+\w+)*)\s*''', subdirs_3line))
        )
        for iter in re.finditer(r'''SUBDIRS\s*=\s*(\w+(?:\s+\w+)*)\s*''', \
            subdirs_3line, re.MULTILINE | re.VERBOSE):
            print('iter: {}'.format(iter.group()))

        # Catches ['SUBDIRS', 'testsuite', 'src'] in [SUBDIRS =   testsuite src]
        print('4: {}'.format(re.findall(
            r'''^SUBDIRS\s*=\s*(\w+(?:\s+\w+)*)\s*''', subdirs_4line))
        )
        for iter in re.finditer(r'''^SUBDIRS\s*=\s*(\w+(?:\s+\w+)*)\s*''', \
            subdirs_4line, re.MULTILINE | re.VERBOSE):
            print('iter: {}'.format(iter.group()))

        """ Reference: <http://stackoverflow.com/questions/8651347/
        regex-to-match-a-capturing-group-one-or-more-times>

        regx = re.compile('(?:(?<= )|(?<=\A)|(?<=\r)|(?<=\n))'
                  '(\d\d)(\d\d)?(\d\d)?'
                  '(?= |\Z|\r|\n)')

        for s in ('   112233  58975  6677  981  897899\r',
                  '\n123456 4433 789101 41586 56 21365899 362547\n',
                  '0101 456899 1 7895'):
            print repr(s),'\n',regx.findall(s),'\n'
        """
        res = 0
    except Exception, err:
        sys.stderr.write("{}".format(err))