SimpleParse

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
calo
User
Beiträge: 52
Registriert: Freitag 8. Dezember 2006, 21:35
Wohnort: Stuttgart

Hallo,

irgendwie scheine ich auf dem Schlauch zu stehen. Ich habe folgenden Code für SimpleParse erstellt. Ich habe erst damit angefangen. Der Dispatcher ist noch ganz am Beginn. Eigentlich müsste es also Fehlermeldungen hageln. Der Code liefert aber nur eine leere Liste. Was habe ich vergessen? :?:

Code: Alles auswählen

from simpleparse.parser import Parser
from simpleparse.dispatchprocessor import *
from simpleparse.common import numbers
import pprint

# #############################################################################
class LMProcessor(DispatchProcessor):
    int_unsigned = numbers.IntInterpreter()

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def __init__(self):
        self.feature = {}

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def flex_code(self, (tag, left, right, sublist), buffer):
        dcode = dispatchList(self, sublist, buffer)

        return self.feature
    
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def feat_summary(self, (tag, left, right, sublist), buffer):
        dcode = dispatchList(self, sublist, buffer)

        return self.feature

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def tok_sum(self, (tag, left, right, sublist), buffer):

        return dispatchList(self, sublist, buffer)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def feat_line(self, (tag, left, right, sublist), buffer):
        
        return dispatchList(self, sublist, buffer)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def lic_typ(self, (tag, left, right, sublist), buffer):
        
        return dispatchList(self, sublist, buffer)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def tok_usage(self, (tag, left, right, sublist), buffer):
        
        return dispatchList(self, sublist, buffer)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def user(self, (tag, left, right, sublist), buffer):

        return dispatchList(self, sublist, buffer)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def srv_name(self, (tag, left, right, sublist), buffer):

        return dispatchList(self, sublist, buffer)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def other_info(self, (tag, left, right, sublist), buffer):

        return dispatchList(self, sublist, buffer)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def out1(self, tagl, buffer):

        return str(getString(tagl, buffer))

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def version(self, tagl, buffer):

        return str(getString(tagl, buffer))

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def srv_prt_hndl(self, tagl, buffer):

        return dispatchList(self, sublist, buffer)

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def date(self, tagl, buffer):

        return str(getString(tagl, buffer))

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def port(self, tagl, buffer):

        return str(getString(tagl, buffer))

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def handle(self, tagl, buffer):

        return str(getString(tagl, buffer))

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def feature(self, tagl, buffer):

        return str(getString(tagl, buffer))

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def tok_total(self, tagl, buffer):

        return str(getString(tagl, buffer))

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def tok_inuse(self, tagl, buffer):

        return str(getString(tagl, buffer))

    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    def tok_num(self, tagl, buffer):

        return str(getString(tagl, buffer))


declaration = r''' # grammar for lmstat-code
flexlm_file  := flex_code
flex_code    := feat_summary+
feat_summary := tok_sum, feat_line?, lic_typ?, tok_usage*

tok_sum      := "Users of", ST, feature, ":", ST, "(Total of", ST, tok_total, ST, "licenses issued;  Total of", ST, tok_inuse, ST, "licenses in use)", ST, NEWLINE
feat_line    := ST, '"', feature, '"', ST, other_info, NEWLINE
lic_typ      := ST, typ, ST, "license", ST, NEWLINE
tok_usage    := user, ST, srv_name, ST, out1, ST, version, ST, srv_prt_hndl, KOMMA, ST, "start", ST, date, KOMMA, ST, tok_num, ST, "licenses", ST, NEWLINE

user         := int_unsigned
srv_name     := [a-zA-Z0-9.-]+
other_info   := -[\n]*
out1         := [a-zA-Z/]
version      := "(v", int_unsigned, ".", int_unsigned, ")"
srv_prt_hndl := "(", srv_name, "/", port, ST, handle, ")"
date         := [a-zA-Z/:]
port         := int_unsigned
handle       := int_unsigned
feature      := [a-zA-Z], [a-zA-Z0-9_]*
tok_total    := int_unsigned
tok_inuse    := int_unsigned
tok_num      := int_unsigned
typ          := [a-zA-Z]*

<KOMMA>      := ","
<ST>         := " \t"*
<NEWLINE>    := "\n"+
'''

flm_code = """Users of feature_1:  (Total of 37 licenses issued;  Total of 37 licenses in use)
  "feature_1" v61.9, vendor: AnythingLM, expiry: 23-apr-2018
  floating license
    103605 cae-srv-01 /dev/tty (v61.9) (liv-srv-6/27009 925), start Fri 4/6 21:21, 18 licenses
    107994 cae-srv-02 /dev/tty (v61.9) (liv-srv-6/27009 814), start Fri 4/6 21:42, 19 licenses
Users of feature_2:  (Total of 65536 licenses issued;  Total of 24 licenses in use)
  "feature_2" v61.9, vendor: AnythingLM, expiry: 23-apr-2018
  floating license
    103605 cae-srv-01 /dev/tty (v61.7) (liv-srv-6/27009 581), start Fri 4/6 21:21, 24 licenses
Users of feature_3:  (Total of 37 licenses issued;  Total of 37 licenses in use)
  "feature_3" v61.9, vendor: AnythingLM, expiry: 23-apr-2018
  floating license
    103605 cae-srv-01 /dev/tty (v61.7) (liv-srv-6/27009 789), start Fri 4/6 21:21, 18 licenses
    107994 cae-srv-02 /dev/tty (v61.7) (liv-srv-6/27009 610), start Fri 4/6 21:42, 19 licenses
"""

# #############################################################################
if __name__ =="__main__":
    my_proc = LMProcessor()
    parser = Parser(declaration, "flexlm_file" )
    success, results, next = parser.parse(flm_code, processor=my_proc)
    print results
    #pprint.pprint(results[0])
Ich brauche nur einen kurzen Anstoß, dann würde ich selber weitermachen.

Vielen Dank im Voraus :D

Calo
calo
User
Beiträge: 52
Registriert: Freitag 8. Dezember 2006, 21:35
Wohnort: Stuttgart

Hallo,

ich sitze leider immer noch an diesem Problem. Es ist noch nicht einmal mein erstes SimpleParse-Modul und sehe einfach nicht was das Problem ist.

Ich habe es jetzt deutlich reduziert. Leider liefert es weiterhin einfach nur eine leere Liste als Ergebnis: ( 0, [], 0)

Code: Alles auswählen

from simpleparse.parser import Parser
from simpleparse.dispatchprocessor import DispatchProcessor
from simpleparse.common import numbers

# Processor-Class
class LMProcessor(DispatchProcessor):
    int_unsigned = numbers.IntInterpreter()
        
    def flexlm_file(self, (tag, left, right, sublist), buffer):
        self.feature = {}
        print "flexlm_file"
        return dispatch(self, sublist, buffer)

    def tok_sum(self, (tag, left, right, sublist), buffer):
        print "tok_sum"
        return dispatchList(self, sublist, buffer)

    def feature(self, tagl, buffer):
        return str(getString(tagl, buffer))

    def tok_total(self, tagl, buffer):
        return str(getString(tagl, buffer))

    def tok_inuse(self, tagl, buffer):
        return str(getString(tagl, buffer))

# Grammar-Declaration
declaration = r"""# My FlexLM-Grammar
flexlm_file  := tok_sum+
tok_sum      := 'Users of', ST, feature, ':', ST, '(Total of', ST, tok_total, ST, 'licenses issued;  Total of', ST, tok_inuse, ST, 'licenses in use)', ST, NEWLINE

feature      := [a-zA-Z], [_a-zA-Z0-9]*
tok_total    := int_unsigned
tok_inuse    := int_unsigned

<ST>         := ' \t'*
<NEWLINE>    := '\n'+
"""

# Code to parse
flm_code = """# FlexLM-Output
Users of feature_1:  (Total of 37 licenses issued;  Total of 37 licenses in use)
Users of feature_2:  (Total of 65536 licenses issued;  Total of 24 licenses in use)
"""

# Main
parser = Parser(declaration, "flexlm_file" )
print parser.parse(flm_code, processor=LMProcessor())
OT: Gibt es eigentlich hier im Forum auch die Möglichkeit seine eigenen Beiträge zu editieren. Laut FAQ schon. Aber ich finde diesen Knopf "Ändere Beitrag" nicht.

Calo
__deets__
User
Beiträge: 14522
Registriert: Mittwoch 14. Oktober 2015, 14:29

Zur metafrage: die Bearbeitung ist nur eine gewisse weil möglich, um nachträgliches sinnentstellendes verändern zu verhindern.
calo
User
Beiträge: 52
Registriert: Freitag 8. Dezember 2006, 21:35
Wohnort: Stuttgart

Hallo, mittlerweile habe ich den Fehler selber gefunden. Zeile 36 in der ˋGrammar-Declarationˋ (siehe weiter oben):

Falsch (bisher):

Code: Alles auswählen

<ST>         := ' \t'*
Richtig:

Code: Alles auswählen

<ST>         := [ \t]*
Tja, der Teufel steckt im Detail. :wink:
Antworten