您尚未登录。

#1 2014-01-24 22:05:02

rongmu
会员
注册时间: 2013-10-19
帖子: 15

[GNOME] 用 YAML 数据保存用户配置

gnome 不用纯文本保存设置,用户个人配置的保存与恢复实在麻烦……
我写了一个简单的 Ruby 脚本,从 "~/.gset.yaml" 文件读取如下格式的 YAML 数据,然后自动用 gsettings 进行配置。
这样就能用(相对)易读的方式保存 gnome 设置了:

general:
  - schema: org.gnome.settings-daemon.plugins.power
    setting:
      percentage-low: 15
      percentage-critical: 10
      percentage-action: 6
      use-time-for-policy: false

  - schema: org.gnome.desktop.wm.preferences
    setting:
      mouse-button-modifier: "<Alt>"
      resize-with-right-button: true

keybindings:
  - name: terminal
    command: gnome-terminal
    binding: <Super>c

  - name: firefox
    command: firefox
    binding: <Super>k

脚本代码:
https://github.com/rongmu/bin/blob/master/gset
我自己的配置数据,可供参考:
https://gist.github.com/rongmu/8596649
(包括鬼畜的 Gnome Terminal 的设置)

由样例可见数据分为了两个部分:一般设置 和 快捷键。

前者是一般情况,要写设置的 schema。schema 用 dconf 可以轻松地查到,下方面板中的「层次结构」即是。
但是对于使用了 "relocatable schema" 的配置,dconf 中是没有它的「层次结构」信息的,似乎只有通过观察 gsettings list-relocatable-schemas 的命令输出来找它的 schema。此外这类设置还要在 schema 后加上 path:在 dconf 中找到一个选项的位置,用斜杠 "/" 把左侧窗口中的路径树连接起来即是(首尾皆要有斜杠)。如: /org/gnome/terminal/legacy/keybindings/

而快捷键数据因为在 gnome 中保存得比较动态,同时用户也可能会时不时地修改自己的设置,所以单独分出一项,直接写各键绑定的配置值,剩下的由脚本处理。

----

以上,希望对使用 gnome  的小伙伴们有用。

离线

#2 2014-01-24 23:16:02

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,489
个人网站

Re: [GNOME] 用 YAML 数据保存用户配置

我来对比语言啦~以下 Python 3 版 =w=

#!/usr/bin/env python3

import os
from subprocess import call

import yaml

data_file = os.path.expanduser('~/.gset.yaml')
settings = yaml.load(open(data_file))

general = settings['general']
keybindings = settings['keybindings']

def exec(cmd):
  if call(cmd):
    color, result = '32', 'done'
  else:
    color, result = '31', 'failed'

  print('[\33[%sm%s\33[0m] %r' % (color, result, cmd), end='\n\n')

for cfg in general:
  for key, value in cfg['setting'].items():
    exec(["gsettings", "set", cfg['schema'], key, str(value)])

keybinding_path = []
for i, keybinding in enumerate(keybindings):
  path = "/org/gnome/settings-daemon/plugins/media-keys/" \
         "custom-keybindings/custom#%d/" % i
  keybinding_path.append(path)

  for key, value in keybinding.items():
    exec(['gsettings', 'set',
          'org.gnome.settings-daemon.plugins.media-keys.custom-keybinding:'
          + path, key, str(value)])

exec(["gsettings", "set",
      'org.gnome.settings-daemon.plugins.media-keys',
      'custom-keybindings',
      '[%s]' % ', '.join(keybinding_path)])

最近编辑记录 依云 (2014-01-25 12:49:19)

离线

#3 2014-01-25 11:22:33

rongmu
会员
注册时间: 2013-10-19
帖子: 15

Re: [GNOME] 用 YAML 数据保存用户配置

学习了~
咳咳,估计仙子没实测:

Traceback (most recent call last):
  File "gset.py", line 24, in <module>
    exec(["gsettings", "set", cfg['schema'], key, value])
  File "gset.py", line 15, in exec
    if call(cmd):
  File "/usr/lib/python3.3/subprocess.py", line 523, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/lib/python3.3/subprocess.py", line 824, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.3/subprocess.py", line 1386, in _execute_child
    restore_signals, start_new_session, preexec_fn)
TypeError: Can't convert 'int' object to str implicitly

估计或者在 yaml 中或者在脚本中要把数值和布尔值转成字符串,ruby 在这里讨个巧。

最近编辑记录 rongmu (2014-01-25 11:26:55)

离线

#4 2014-01-25 12:51:09

依云
会员
所在地: a.k.a. 百合仙子
注册时间: 2011-08-21
帖子: 8,489
个人网站

Re: [GNOME] 用 YAML 数据保存用户配置

rongmu 说:

学习了~
咳咳,估计仙子没实测:

Traceback (most recent call last):
  File "gset.py", line 24, in <module>
    exec(["gsettings", "set", cfg['schema'], key, value])
  File "gset.py", line 15, in exec
    if call(cmd):
  File "/usr/lib/python3.3/subprocess.py", line 523, in call
    with Popen(*popenargs, **kwargs) as p:
  File "/usr/lib/python3.3/subprocess.py", line 824, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.3/subprocess.py", line 1386, in _execute_child
    restore_signals, start_new_session, preexec_fn)
TypeError: Can't convert 'int' object to str implicitly

估计或者在 yaml 中或者在脚本中要把数值和布尔值转成字符串,ruby 在这里讨个巧。

我没 GNOME 怎么实测……

我没有像你一样拼接字符串然后送给 /bin/sh 执行,所以没办法在构建字符串的时候把所有参数自动弄成字符串插进去。已修。

离线

页脚