I have a yaml string representing a menu. After I have a dictionary from it, I try to get URL simply said the key url
of Parameter
and every first element of list for other element that is not Parameter
.
Here what I tried
import yaml
import jmespath
import pprint
setting = """
GeneralSetting:
- Description: General Setting
MenuSetting:
- Description: Menu Setting
- Setting:
- AutoMenu:
- Description: Register menu
- HelpText: This is for auto create menu
- Value:
- Sample:
- Parameter:
- icon: fa fa-birthday-cake
- Active: Y
- Sample: [Sample]
- Sample Account: [SampleAccount]
- Multi Journal:
- Parameter:
- icon: fas fa-book
- url: MultiJournal
- Foreign Exchange:
- Parameter:
- icon: fa fa-money-bill-alt
- url: ForeignExchange
- Loan Contract:
- Parameter:
- icon: fa fa-capsules
- Active: Y
- Loan Contract: [LoanContract,1]
- Loan Report:
- Loan Detail Listing: [LoanDetailListing,1]
- Loan Application Detail Listing: [ReportR003,1]
"""
toDict = yaml.load(setting, yaml.SafeLoader)
pp = pprint.PrettyPrinter(indent=2)
# ALL
ALL = jmespath.search('MenuSetting[].Setting[].AutoMenu[].Value[].*[]', toDict)
ParentURL = jmespath.search('MenuSetting[].Setting[].AutoMenu[].Value[].*[0][].Parameter[].url', toDict)
NotParameter = {key:value for item in ALL for it in item for key,value in it.items() if key != "Parameter"}
# pp.pprint(NotParameter)
# print ('-----')
SubURL = jmespath.search('*[0]', NotParameter)
SubSubURL = jmespath.search('*[].*[][]', NotParameter)
pp.pprint(ParentURL)
print('---')
pp.pprint(SubURL)
print('---')
pp.pprint(SubSubURL)
From this, I could done well only for the Parent URL which is url
under Parameter
such as [MultiJournal,ForeignExchange]
but not for child and sub-child.
I just want the final result of url as a list of this
[Sample,SampleAccount,MultiJournal,ForeignExchange,LoanContract,LoanDetailListing,Report/R003]
I tried several way but I still could not get the result of that?
Any way that I could get list of value as that? Thanks