简体   繁体   English

如何在 yaml 文件中提及 Mac 主文件夹

[英]How to mention Mac Home folder in yaml file

Can someone tell me how to mention Mac Home folder in yaml file?有人能告诉我如何在 yaml 文件中提及 Mac 主文件夹吗?

I have a settings.yaml file that I use in my Python code to connect to GDrive.我有一个 settings.yaml 文件,我在 Python 代码中使用它来连接到 GDrive。 It exports the credentials to a JSON file and I want that to extract to GDrive folder in Home directory.它将凭据导出到 JSON 文件,我希望将其提取到主目录中的 GDrive 文件夹。

settings.yaml设置.yaml

    client_config_backend: 'settings'
    client_config:
      client_id: "something"
      client_secret: "something"
      auth_uri: "https://accounts.google.com/o/oauth2/auth"
      token_uri: "https://oauth2.googleapis.com/token"

    save_credentials: True
    save_credentials_backend: 'file'
    save_credentials_file: '/Users/machinename/GDrive/credentials.json'

And in the python file, it is mentioned like this:在 python 文件中,它是这样提到的:

gauth = GoogleAuth(settings_file='settings.yaml')
gauth.LoadCredentialsFile('/Users/machinename/GDrive/credentials.json')

So, different people will run this in different machines and the machinename will vary, so I am trying to provide the dynamic version for Home directory.所以,不同的人会在不同的机器上运行它,机器machinename会有所不同,所以我试图为主目录提供动态版本。 I tried ~/GDrive/credentials.json but it did not work.我试过~/GDrive/credentials.json但没有用。

Can someone help how to declare home directory in the yaml file?有人可以帮助如何在 yaml 文件中声明主目录吗?

The home folder is denoted as ~ .主文件夹表示为~
The problem is you need to expand, and you can do by using os :问题是您需要扩展,您可以使用os来完成:

import os

os.path.expanduser("~/GDrive/credentials.json")

UPDATE更新

If you want to store it in the yaml code, it's the same thing:如果要存放在 yaml 代码中,也是一样的:

settings.yaml设置.yaml

client_config_backend: 'settings'
client_config:
  client_id: "something"
  client_secret: "something"
  auth_uri: "https://accounts.google.com/o/oauth2/auth"
  token_uri: "https://oauth2.googleapis.com/token"

save_credentials: True
save_credentials_backend: 'file'
save_credentials_file: '~/GDrive/credentials.json'

Then, in the python:然后,在 python 中:

credentials_file = "settings.yaml"
with open(credentials_file, "r") as f:
    credentials_info = yaml.load(f)
gauth = GoogleAuth(settings_file=credentials_file)
gauth.LoadCredentialsFile(os.path.expanduser(credentials_info['save_credentials_file'))

This may achieve what you wanted这可能会达到你想要的

In settings.yaml
    save_credentials_file: '$HOME/GDrive/credentials.json'
...
In python, put '$HOME/GDrive/credentials.json' into variable save_credentials_file,

gauth.LoadCredentialsFile(os.path.expandvars(save_credentials_file))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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