A
avidfan
Help with pyparsing and dealing with null values
I am trying to parse a log file (web.out) similar to this:
-----------------------------------------------------------
MBeanName: "mtg-model:Name=mtg-model_managed2,Type=Server"
AcceptBacklog: 50
AdministrationPort: 0
AutoKillIfFailed: false
AutoRestart: true
COM: mtg-model_managed2
COMEnabled: false
CachingDisabled: true
ClasspathServletDisabled: false
ClientCertProxyEnabled: false
Cluster: mtg-model-cluster
ClusterRuntime: mtg-model-cluster
ClusterWeight: 100
CompleteCOMMessageTimeout: -1
CompleteHTTPMessageTimeout: -1
CompleteIIOPMessageTimeout: -1
CompleteMessageTimeout: 60
CompleteT3MessageTimeout: -1
CustomIdentityKeyStoreFileName:
CustomIdentityKeyStorePassPhrase:
CustomIdentityKeyStorePassPhraseEncrypted:
CustomIdentityKeyStoreType:
CustomTrustKeyStoreFileName:
CustomTrustKeyStorePassPhrase:
CustomTrustKeyStorePassPhraseEncrypted:
CustomTrustKeyStoreType:
DefaultIIOPPassword:
DefaultIIOPPasswordEncrypted:
DefaultIIOPUser:
DefaultInternalServletsDisabled: false
DefaultProtocol: t3
DefaultSecureProtocol: t3s
DefaultTGIOPPassword:
DefaultTGIOPPasswordEncrypted: ******
DefaultTGIOPUser: guest
DomainLogFilter:
EnabledForDomainLog: true
ExecuteQueues: weblogic.kernel.Default,foglight
ExpectedToRun: false
ExternalDNSName:
ExtraEjbcOptions:
ExtraRmicOptions:
GracefulShutdownTimeout: 0
-----------------------------------------------------------
and I need the indented values (eventually) in a dictionary. As you
can see, some of the fields have a value, and some do not. It appears
that the code I have so far is not dealing with the null values and
colons as I had planned. Here is the code:
-----------------------------------------------------------
from pyparsing import *
input = open("web.out", 'r')
data = input.read()
end = Literal("\n").suppress()
all = SkipTo(end)
colon = Literal(":").suppress()
MBeanName = Literal("MBeanName:")
ServerName = dblQuotedString
identity = Word(alphas, alphanums+"._*/,-")
pairs = Group(identity + colon + Optional(identity) +all)
logEntry = MBeanName + ServerName.setResultsName("servername") +
OneOrMore(pairs)
for tokens in logEntry.searchString(data):
print
print "ServerName =\t "+ tokens.servername
for t in tokens:
print t
print
print 50*"-"
-------------------------------------------------------------
which is giving me this:
-------------------------------------------------------------
ServerName = "mtg-model:Name=mtg-modelserver_map501,Type=Server"
MBeanName:
"mtg-model:Name=mtg-modelserver_map501,Type=Server"
['AcceptBacklog', '50']
['AdministrationPort', '0']
['AutoKillIfFailed', 'false', 'AutoRestart: true']
['COM', 'mtg-modelserver_map501', 'COMEnabled: false']
['CachingDisabled', 'true', 'ClasspathServletDisabled: false']
['ClientCertProxyEnabled', 'false', 'Cluster:']
['ClusterRuntime', 'ClusterWeight', ': 100']
['CompleteCOMMessageTimeout', '-1']
['CompleteHTTPMessageTimeout', '-1']
['CompleteIIOPMessageTimeout', '-1']
['CompleteMessageTimeout', '60']
['CompleteT3MessageTimeout', '-1']
['CustomIdentityKeyStoreFileName', 'CustomIdentityKeyStorePassPhrase',
':']
['CustomIdentityKeyStorePassPhraseEncrypted',
'CustomIdentityKeyStoreType', ':']
['CustomTrustKeyStoreFileName', 'CustomTrustKeyStorePassPhrase', ':']
['CustomTrustKeyStorePassPhraseEncrypted', 'CustomTrustKeyStoreType',
':']
['DefaultIIOPPassword', 'DefaultIIOPPasswordEncrypted', ':']
['DefaultIIOPUser', 'DefaultInternalServletsDisabled', ': false']
['DefaultProtocol', 't3', 'DefaultSecureProtocol: t3s']
['DefaultTGIOPPassword', 'DefaultTGIOPPasswordEncrypted', ': ******']
['DefaultTGIOPUser', 'guest', 'DomainLogFilter:']
['EnabledForDomainLog', 'true', 'ExecuteQueues:
weblogic.kernel.Default,foglight']
['ExpectedToRun', 'false', 'ExternalDNSName:']
['ExtraEjbcOptions', 'ExtraRmicOptions', ':']
['GracefulShutdownTimeout', '0']
----------------------------------------------------------------
instead of this (one to one):
----------------------------------------------------------------
ServerName = "mtg-model:Name=mtg-modelserver_map501,Type=Server"
MBeanName:
"mtg-model:Name=mtg-modelserver_map501,Type=Server"
['AcceptBacklog', '50']
['AdministrationPort', '0']
['AutoKillIfFailed', 'false']
['AutoRestart', 'true']
['COM', 'mtg-modelserver_map501']
['COMEnabled', 'false']
['CachingDisabled', 'true']
['ClasspathServletDisabled', false']
['ClientCertProxyEnabled', 'false']
['Cluster', 'mtg-model-cluster']
['ClusterRuntime', 'mtg-model-cluster']
['ClusterWeight', '100']
['CompleteCOMMessageTimeout', '-1']
['CompleteHTTPMessageTimeout', '-1']
['CompleteIIOPMessageTimeout', '-1']
['CompleteMessageTimeout', '60']
['CompleteT3MessageTimeout', '-1']
['CustomIdentityKeyStoreFileName', '']
['CustomIdentityKeyStorePassPhrase', '']
['CustomIdentityKeyStorePassPhraseEncrypted', '']
['CustomIdentityKeyStoreType', '']
['CustomTrustKeyStoreFileName', '']
['CustomTrustKeyStorePassPhrase', '']
['CustomTrustKeyStorePassPhraseEncrypted', '']
['CustomTrustKeyStoreType', '']
['DefaultIIOPPassword', '']
['DefaultIIOPPasswordEncrypted', '']
['DefaultIIOPUser', '']
['DefaultInternalServletsDisabled', 'false']
['DefaultProtocol', 't3']
['DefaultSecureProtocol: t3s']
['DefaultTGIOPPassword', '']
['DefaultTGIOPPasswordEncrypted', '******']
['DefaultTGIOPUser', 'guest']
['DomainLogFilter', '']
['EnabledForDomainLog', 'true']
['ExecuteQueues', 'weblogic.kernel.Default,foglight']
['ExpectedToRun', 'false']
['ExternalDNSName', '']
['ExtraEjbcOptions', '']
['ExtraRmicOptions', '']
['GracefulShutdownTimeout', '0']
------------------------------------------------------------------
Can anyone offer any advice on this? I would certainly appreciate any
input.
Thanks!
I am trying to parse a log file (web.out) similar to this:
-----------------------------------------------------------
MBeanName: "mtg-model:Name=mtg-model_managed2,Type=Server"
AcceptBacklog: 50
AdministrationPort: 0
AutoKillIfFailed: false
AutoRestart: true
COM: mtg-model_managed2
COMEnabled: false
CachingDisabled: true
ClasspathServletDisabled: false
ClientCertProxyEnabled: false
Cluster: mtg-model-cluster
ClusterRuntime: mtg-model-cluster
ClusterWeight: 100
CompleteCOMMessageTimeout: -1
CompleteHTTPMessageTimeout: -1
CompleteIIOPMessageTimeout: -1
CompleteMessageTimeout: 60
CompleteT3MessageTimeout: -1
CustomIdentityKeyStoreFileName:
CustomIdentityKeyStorePassPhrase:
CustomIdentityKeyStorePassPhraseEncrypted:
CustomIdentityKeyStoreType:
CustomTrustKeyStoreFileName:
CustomTrustKeyStorePassPhrase:
CustomTrustKeyStorePassPhraseEncrypted:
CustomTrustKeyStoreType:
DefaultIIOPPassword:
DefaultIIOPPasswordEncrypted:
DefaultIIOPUser:
DefaultInternalServletsDisabled: false
DefaultProtocol: t3
DefaultSecureProtocol: t3s
DefaultTGIOPPassword:
DefaultTGIOPPasswordEncrypted: ******
DefaultTGIOPUser: guest
DomainLogFilter:
EnabledForDomainLog: true
ExecuteQueues: weblogic.kernel.Default,foglight
ExpectedToRun: false
ExternalDNSName:
ExtraEjbcOptions:
ExtraRmicOptions:
GracefulShutdownTimeout: 0
-----------------------------------------------------------
and I need the indented values (eventually) in a dictionary. As you
can see, some of the fields have a value, and some do not. It appears
that the code I have so far is not dealing with the null values and
colons as I had planned. Here is the code:
-----------------------------------------------------------
from pyparsing import *
input = open("web.out", 'r')
data = input.read()
end = Literal("\n").suppress()
all = SkipTo(end)
colon = Literal(":").suppress()
MBeanName = Literal("MBeanName:")
ServerName = dblQuotedString
identity = Word(alphas, alphanums+"._*/,-")
pairs = Group(identity + colon + Optional(identity) +all)
logEntry = MBeanName + ServerName.setResultsName("servername") +
OneOrMore(pairs)
for tokens in logEntry.searchString(data):
print "ServerName =\t "+ tokens.servername
for t in tokens:
print t
print 50*"-"
-------------------------------------------------------------
which is giving me this:
-------------------------------------------------------------
ServerName = "mtg-model:Name=mtg-modelserver_map501,Type=Server"
MBeanName:
"mtg-model:Name=mtg-modelserver_map501,Type=Server"
['AcceptBacklog', '50']
['AdministrationPort', '0']
['AutoKillIfFailed', 'false', 'AutoRestart: true']
['COM', 'mtg-modelserver_map501', 'COMEnabled: false']
['CachingDisabled', 'true', 'ClasspathServletDisabled: false']
['ClientCertProxyEnabled', 'false', 'Cluster:']
['ClusterRuntime', 'ClusterWeight', ': 100']
['CompleteCOMMessageTimeout', '-1']
['CompleteHTTPMessageTimeout', '-1']
['CompleteIIOPMessageTimeout', '-1']
['CompleteMessageTimeout', '60']
['CompleteT3MessageTimeout', '-1']
['CustomIdentityKeyStoreFileName', 'CustomIdentityKeyStorePassPhrase',
':']
['CustomIdentityKeyStorePassPhraseEncrypted',
'CustomIdentityKeyStoreType', ':']
['CustomTrustKeyStoreFileName', 'CustomTrustKeyStorePassPhrase', ':']
['CustomTrustKeyStorePassPhraseEncrypted', 'CustomTrustKeyStoreType',
':']
['DefaultIIOPPassword', 'DefaultIIOPPasswordEncrypted', ':']
['DefaultIIOPUser', 'DefaultInternalServletsDisabled', ': false']
['DefaultProtocol', 't3', 'DefaultSecureProtocol: t3s']
['DefaultTGIOPPassword', 'DefaultTGIOPPasswordEncrypted', ': ******']
['DefaultTGIOPUser', 'guest', 'DomainLogFilter:']
['EnabledForDomainLog', 'true', 'ExecuteQueues:
weblogic.kernel.Default,foglight']
['ExpectedToRun', 'false', 'ExternalDNSName:']
['ExtraEjbcOptions', 'ExtraRmicOptions', ':']
['GracefulShutdownTimeout', '0']
----------------------------------------------------------------
instead of this (one to one):
----------------------------------------------------------------
ServerName = "mtg-model:Name=mtg-modelserver_map501,Type=Server"
MBeanName:
"mtg-model:Name=mtg-modelserver_map501,Type=Server"
['AcceptBacklog', '50']
['AdministrationPort', '0']
['AutoKillIfFailed', 'false']
['AutoRestart', 'true']
['COM', 'mtg-modelserver_map501']
['COMEnabled', 'false']
['CachingDisabled', 'true']
['ClasspathServletDisabled', false']
['ClientCertProxyEnabled', 'false']
['Cluster', 'mtg-model-cluster']
['ClusterRuntime', 'mtg-model-cluster']
['ClusterWeight', '100']
['CompleteCOMMessageTimeout', '-1']
['CompleteHTTPMessageTimeout', '-1']
['CompleteIIOPMessageTimeout', '-1']
['CompleteMessageTimeout', '60']
['CompleteT3MessageTimeout', '-1']
['CustomIdentityKeyStoreFileName', '']
['CustomIdentityKeyStorePassPhrase', '']
['CustomIdentityKeyStorePassPhraseEncrypted', '']
['CustomIdentityKeyStoreType', '']
['CustomTrustKeyStoreFileName', '']
['CustomTrustKeyStorePassPhrase', '']
['CustomTrustKeyStorePassPhraseEncrypted', '']
['CustomTrustKeyStoreType', '']
['DefaultIIOPPassword', '']
['DefaultIIOPPasswordEncrypted', '']
['DefaultIIOPUser', '']
['DefaultInternalServletsDisabled', 'false']
['DefaultProtocol', 't3']
['DefaultSecureProtocol: t3s']
['DefaultTGIOPPassword', '']
['DefaultTGIOPPasswordEncrypted', '******']
['DefaultTGIOPUser', 'guest']
['DomainLogFilter', '']
['EnabledForDomainLog', 'true']
['ExecuteQueues', 'weblogic.kernel.Default,foglight']
['ExpectedToRun', 'false']
['ExternalDNSName', '']
['ExtraEjbcOptions', '']
['ExtraRmicOptions', '']
['GracefulShutdownTimeout', '0']
------------------------------------------------------------------
Can anyone offer any advice on this? I would certainly appreciate any
input.
Thanks!