-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpacmd_list_python.py
More file actions
executable file
·55 lines (43 loc) · 1.4 KB
/
Copy pathpacmd_list_python.py
File metadata and controls
executable file
·55 lines (43 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# From: https://gist.github.com/zyga/5534776#gistcomment-3742655
#
# parsing pactl list
#
from pyparsing import *
import os
from subprocess import check_output
import sys
indentStack = [1]
stmt = Forward()
NL = LineEnd()
identifier = Word(alphas, alphanums+"-_.").setName("identifier").setDebug()
sect_def = Group(Group(identifier) + Suppress("#") + Group(Word(nums)))
inner_section = indentedBlock(stmt, indentStack)
section = (sect_def + inner_section)
value_label = originalTextFor(OneOrMore(identifier))
value = Group(value_label
+ Suppress(":")
+ Optional(~NL + Group(Combine(ZeroOrMore(Word(alphanums+'-/=_.') | quotedString(), stopOn=NL)))))
prop_name = Literal("Properties:")
prop_section = indentedBlock(stmt, indentStack)
prop_val_value = Combine(OneOrMore(Word(alphas, alphanums+'-/.') | quotedString(), stopOn=NL))
prop_val = Group(identifier + Suppress("=") + Group(prop_val_value))
prop = (prop_name + prop_section)
stmt << ( section | prop | value | prop_val )
syntax = OneOrMore(stmt)
env = os.environ.copy()
env['LANG'] = 'C'
data = check_output(
['pactl', 'list'], universal_newlines=True, env=env)
if len(sys.argv) == 1:
count = -1
else:
count = int(sys.argv[1])
if count == -1:
partial = data
else:
partial = '\n'.join(data.split('\n')[:count])
print(partial)
parseTree = syntax.parseString(partial)
parseTree.pprint()
#print(parseTree.dump())