I’m working on an academic project where I need to create a python script to change credentials from a yaml file. This script should read the yaml file, then look for the value of Oauth 1 in a spreadsheet (authentication column) and get the updated Oauth 2, finally create a new ymal file with auth 2. I would like to know where I can start.
I have a credentials.yaml with the content below.
name: gcp_credentials_project
info: 1798
project_id: acd-univer-proj-mod-pers
consumer_key: "cfb45ac0098b723f84219e5c12a9532067abf19a7"
oauth_token: "52871f93b604e239a431de879b3c2e45082768f02d"
oauth_version: 1.0
oauth_signature_metho: "HMAC-SHA1"
The yaml file should be parsed and updated (oauth_version) as below.
name: gcp_credentials_project
info: 1798
project_id: acd-univer-proj-mod-pers
consumer_key: "cfb45ac0098b723f84219e5c12a9532067abf19a7"
oauth_token: "52871f93b604e239a431de879b3c2e45082768f02d"
oauth_version: 2.0
oauth_signature_metho: "HMAC-SHA1"
5
The quotes on the values for the keys consumer_key
and oauth_token
are superfluous.
If you need to preserve them, as you indicate, you’ll find that is difficult to do with PyYAML.
Additionally PyYAML only suport YAML 1.1, outdated 15 years ago.
I recommend you use ruamel.yaml
for this kind of round-tripping (read/modify/save) of YAML (disclaimer: I am the author of that package).
In addition to being able to preserve quotes, it preserves comments, handles keys that are collections and YAML 1.2
import sys
import pathlib
import ruamel.yaml
file_path = pathlib.Path('input.yaml')
yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True
data = yaml.load(file_path)
data['oauth_version'] = 2.0
# This shows the content, on stdout. To update the file use: yaml.dump(data, file_path)
yaml.dump(data, sys.stdout)
which gives:
name: gcp_credentials_project
info: 1798
project_id: acd-univer-proj-mod-pers
consumer_key: "cfb45ac0098b723f84219e5c12a9532067abf19a7"
oauth_token: "52871f93b604e239a431de879b3c2e45082768f02d"
oauth_version: 2.0
oauth_signature_metho: "HMAC-SHA1"
The input and expected output you showed have leading spaces, which I assumed are because of unfamiliarity with SO formatting. If these spaces really need to be in the output, then can be added in a simple postprocessing step with the optional transform
argument of dump()
.