简体   繁体   中英

How to update yaml file in python with section wise syntax?

I have an yaml file with syntax like this.

logJsonDataConfig: &logJsonDataConfig
  code: logJsonData
  path: /logs/
  ext: .json
queueJsonDataConfig: &queueJsonDataConfig
  code: queueJsonData
  path: /queue/
  ext: .json
gatewayGrpcConfig: &gatewayGrpcConfig
  code: gatewayGrpc
  driverName: tcp
  urlAddress: 0.0.0.0:5051
excelGrpcConfig: &excelGrpcConfig
  code: excelGrpc
  driverName: tcp
  urlAddress: 127.0.0.1:5052
adGrpcConfig: &adGrpcConfig
  code: adGrpc
  driverName: tcp
  urlAddress: 127.0.0.1:5053
restfulWebServiceGrpcConfig: &restfulWebServiceGrpcConfig
  code: restfulWebServiceGrpc
  driverName: tcp
  urlAddress: 127.0.0.1:5054
databaseGrpcConfig: &databaseGrpcConfig
  code: databaseGrpc
  driverName: tcp
  urlAddress: 127.0.0.1:5055
serviceDeskPlusGrpcConfig: &serviceDeskPlusGrpcConfig
  code: serviceDeskPlusGrpc
  driverName: tcp
  urlAddress: 127.0.0.1:5056
recordingAgentGrpcConfig: &recordingAgentGrpcConfig
  code: recordingAgentGrpc
  driverName: tcp
  urlAddress: 127.0.0.1:5057
fileAndFolderGrpcConfig: &fileAndFolderGrpcConfig
  code: fileAndFolderGrpc
  driverName: tcp
  urlAddress: 127.0.0.1:5058
pdfGrpcConfig: &pdfGrpcConfig
  code: pdfGrpc
  driverName: tcp
  urlAddress: 127.0.0.1:5059
ocrGrpcConfig: &ocrGrpcConfig
  code: ocrGrpc
  driverName: tcp
  urlAddress: 127.0.0.1:5060
ewsGrpcConfig: &ewsGrpcConfig
  code: ewsGrpc
  driverName: tcp
  urlAddress: 127.0.0.1:5061
zapConfig: &zapConfig
  code: zap
  level: debug
  enableCaller: true
logrusConfig: &logrusConfig
  code: logrus
  level: debug
  enableCaller: false
logConfig: *zapConfig
excelMicroserviceConfig: &excelMicroserviceConfig
  code: excelMicroservice
  logDataConfig: &logDataConfig
    code: logData
    dataStoreConfig: *logJsonDataConfig
activeDirectoryMicroserviceConfig: &activeDirectoryMicroserviceConfig
  code: activeDirectoryMicroservice
  logDataConfig: &logDataConfig
    code: logData
    dataStoreConfig: *logJsonDataConfig
restfulWebServiceMicroserviceConfig: &restfulWebServiceMicroserviceConfig
  code: restfulWebServiceMicroservice
  logDataConfig: &logDataConfig
    code: logData
    dataStoreConfig: *logJsonDataConfig
recordingAgentMicroserviceConfig: &recordingAgentMicroserviceConfig
  code: recordingAgentMicroservice
  logDataConfig: &logDataConfig
    code: logData
    dataStoreConfig: *logJsonDataConfig
databaseMicroserviceConfig: &databaseMicroserviceConfig
  code: databaseMicroservice
  logDataConfig: &logDataConfig
    code: logData
    dataStoreConfig: *logJsonDataConfig
serviceDeskPlusMicroserviceConfig: &serviceDeskPlusMicroserviceConfig
  code: serviceDeskPlusMicroservice
  logDataConfig: &logDataConfig
    code: logData
    dataStoreConfig: *logJsonDataConfig
fileAndFolderMicroserviceConfig: &fileAndFolderMicroserviceConfig
  code: fileAndFolderMicroservice
  logDataConfig: &logDataConfig
    code: logData
    dataStoreConfig: *logJsonDataConfig
pdfMicroserviceConfig: &pdfMicroserviceConfig
  code: pdfMicroservice
  logDataConfig: &logDataConfig
    code: logData
    dataStoreConfig: *logJsonDataConfig
ocrMicroserviceConfig: &ocrMicroserviceConfig
  code: ocrMicroservice
  logDataConfig: &logDataConfig
    code: logData
    dataStoreConfig: *logJsonDataConfig
ewsMicroserviceConfig: &ewsMicroserviceConfig
  code: ewsMicroservice
  logDataConfig: &logDataConfig
    code: logData
    dataStoreConfig: *logJsonDataConfig
useCaseConfig:
  logJson:
    code: logJson
  queue:
    code: queue
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *queueJsonDataConfig
  excel:
    code: excel
    microserviceConfig: *excelMicroserviceConfig
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *logJsonDataConfig
  activeDirectory:
    code: activeDirectory
    microserviceConfig: *activeDirectoryMicroserviceConfig
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *logJsonDataConfig
  restfulWebService:
    code: restfulWebService
    microserviceConfig: *restfulWebServiceMicroserviceConfig
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *logJsonDataConfig
  database:
    code: database
    microserviceConfig: *databaseMicroserviceConfig
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *logJsonDataConfig
  serviceDeskPlus:
    code: serviceDeskPlus
    microserviceConfig: *serviceDeskPlusMicroserviceConfig
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *logJsonDataConfig
  recordingAgent:
    code: recordingAgent
    microserviceConfig: *recordingAgentMicroserviceConfig
    logDataConfig: &logDataConfig
      code: logData
      dataStroreConfig: *logJsonDataConfig
  fileAndFolder:
    code: fileAndFolder
    microserviceConfig: *fileAndFolderMicroserviceConfig
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *logJsonDataConfig
  pdf:
    code: pdf
    microserviceConfig: *pdfMicroserviceConfig
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *logJsonDataConfig
  ocr:
    code: ocr
    microserviceConfig: *ocrMicroserviceConfig
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *logJsonDataConfig
  ews:
    code: ews
    microserviceConfig: *ewsMicroserviceConfig
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *logJsonDataConfig
  loop:
    code: loop
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *logJsonDataConfig
  ifelse:
    code: ifElse
    logDataConfig: &logDataConfig
      code: logData
      dataStoreConfig: *logJsonDataConfig

The python modules are not working here lik pyyaml as the syntax is bit different but the yaml is still valid I have checked it with yaml checker

So, is there any module in python or any other language, I could add one more section like below if it is not there or simply update the existing one.

snap to add in the yaml

Please suggest, tried all modules but failing with this: pyyaml,python-benedict,ruamel.yaml

I tried yq , eventhough in different sections, it considered lements as duplicate.

python -m yq -Y .gatewayGrpcConfig .\input.yml
yq: Error running jq: ComposerError: found duplicate anchor 'logDataConfig'; first occurrence
  in ".\input.yml", line 64, column 18
second occurrence
  in ".\input.yml", line 69, column 18

Assuming your input is in the file input.yaml , there is no problem loading this with ruamel.yaml . Just ignore the ReusedAnchorWarning :

import sys
import warnings
from pathlib import Path
import ruamel.yaml

warnings.simplefilter("ignore", ruamel.yaml.error.ReusedAnchorWarning)
    
yaml = ruamel.yaml.YAML()
data = yaml.load(Path('input.yaml'))
print(data['useCaseConfig']['excel']['microserviceConfig']['logDataConfig']['dataStoreConfig']['path'])

which gives:

/logs/

Please note that the recommended extension for files containing YAML documents has been .yaml since September 2006.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM