alsamixer 使用以下语法将其数据存储在文件中:
control.2 = {
iface : MIXER
name : 'DSP1 Rate'
value:'16kHz'
comment = {
access 'read write'
type ENUMERATED
count 1
item.0 'SYNCCLK rate'
item.1 '8kHz'
item.2 '16kHz'
item.3 'ASYNCCLK rate'
}
有没有一种快速的方法可以将其转换为 python 字典?允许从 python 脚本读取它?
我已经尝试使用 alsaaudio.mixers()
的 pyalsaaudio 可以看到控件的名称,但是对于 alsa 文件,您还可以看到可用的您可以为该控件设置的数据。
我整个下午都在做这件事。希望它能解决您的问题:
#!/usr/bin/env python2.7
import re
import json
def jsonify(config='/var/lib/alsa/asound.state'):
g = re.MULTILINE
p1 = re.compile(r'((?:^|"[^"]+"|\'[^\']+\'|[\s:{}])+)([^\s{}]+)', g)
p2 = re.compile(r'^(\s*\"[\w.]+"(?!\s*[\:]))', g)
p3 = re.compile(r'([\"\}])\n(?!\s*\})', g)
with open(config) as fh:
context = fh.read()
# replace all the quote with quotes
context = context.replace("'", '"')
# surround the name:value objects by quotes
context = re.sub(p1, r'\g<1>"\g<2>"', context)
# add a trailing comma after the values assignment
context = re.sub(p2, r'\g<1>:', context)
# add a colon before the values as a delimiter
context = re.sub(p3, r'\g<1>,\n', context)
return json.loads('{\n%s\n}' % context[:-2])
if __name__ == '__main__':
print 'you should use it as module :D'
我是一名优秀的程序员,十分优秀!