aboutsummaryrefslogtreecommitdiff
path: root/pacmd.py
diff options
context:
space:
mode:
Diffstat (limited to 'pacmd.py')
-rw-r--r--pacmd.py52
1 files changed, 31 insertions, 21 deletions
diff --git a/pacmd.py b/pacmd.py
index 42d3ce8..c1071f6 100644
--- a/pacmd.py
+++ b/pacmd.py
@@ -1,26 +1,27 @@
import subprocess, re
# These are not namedtuples because we want them mutable
-class PacmdSection:
+class Section:
def __init__(self, name, items):
self.name = name # string
- self.items = items # [PacmdItem]
+ self.items = items # [Item]
-class PacmdItem:
- def __init__(self, index, children):
+class Item:
+ def __init__(self, index, default, ch):
self.index = index # int
- self.children = children # {key: PacmdNode}
+ self.default = default # bool
+ self.ch = ch # {key: Node}
-class PacmdNode:
- def __init__(self, value, children):
+class Node:
+ def __init__(self, value, ch):
self.value = value # None | string | [string]
- self.children = children # {key: PacmdNode}
+ self.ch = ch # {key: Node}
class Pacmd:
def __init__(self, full_output):
lines = full_output.decode("utf-8").split("\n")
self.infos = [] # [string]
- self.sections = [] # [PacmdSection]
+ self.sections = [] # [Section]
currsect = None
curritem = None
@@ -56,7 +57,7 @@ class Pacmd:
line = line.replace("\t", " ")
m = re.match(r"^( *(\* )?)([^ ].*)", line)
- is_default = m.group(2) is not None # TODO use is_default
+ is_default = m.group(2) is not None
indent = len(m.group(1))
line = m.group(3)
@@ -68,7 +69,7 @@ class Pacmd:
close_currsect()
name = re.search(r" ([^ ]*)\(s\)", line).group(1)
- currsect = PacmdSection(name, [])
+ currsect = Section(name, [])
else:
self.infos.append(line)
@@ -76,7 +77,7 @@ class Pacmd:
close_curritem()
index = re.search(r"index: ([0-9]*)$", line).group(1)
- curritem = PacmdItem(int(index), {})
+ curritem = Item(int(index), is_default, {})
elif indent >= 8:
assert curritem != None
@@ -89,16 +90,16 @@ class Pacmd:
parentval, thisval = None, curritem
for k in currpath:
- parentval, thisval = thisval, thisval.children[k]
+ parentval, thisval = thisval, thisval.ch[k]
if q_is_key_value(line):
key, value = q_get_key_value(line)
- thisval.children[key] = PacmdNode(value, {})
+ thisval.ch[key] = Node(value, {})
currpath.append(key)
elif re.search(r":$", line) is not None:
key = line[:-1]
- thisval.children[key] = PacmdNode(None, {})
+ thisval.ch[key] = Node(None, {})
currpath.append(key)
elif indent_depth >= len(currpath):
@@ -116,17 +117,26 @@ class Pacmd:
close_currsect()
+class PacmdError(Exception):
+ pass
+
def pacmd(*args):
- return Pacmd(subprocess.check_output(["pacmd"] + list(args)))
+ try:
+ return subprocess.check_output(["pacmd"] + list(args), stderr=subprocess.STDOUT)
+ except subprocess.CalledProcessError as e:
+ raise PacmdError(e.output)
+
+def _pacmd_parsed(*args):
+ return Pacmd(pacmd(*args))
def list_all():
- return pacmd("list")
+ return _pacmd_parsed("list")
def list_sinks():
- return pacmd("list-sinks")
+ return _pacmd_parsed("list-sinks")
def list_sink_inputs():
- return pacmd("list-sink-inputs")
+ return _pacmd_parsed("list-sink-inputs")
def _make_indent(n):
@@ -136,11 +146,11 @@ def dump_section(sect):
print("Section " + sect.name + ":")
for item in sect.items:
print(_make_indent(1) + "Item " + str(item.index) + ":")
- for key, node in item.children.items():
+ for key, node in item.ch.items():
dump_node(node, key, 2);
def dump_node(node, key, indent):
print(_make_indent(indent) + key + ":" +
("" if node.value is None else " " + str(node.value)))
- for k2, n2 in node.children.items():
+ for k2, n2 in node.ch.items():
dump_node(n2, k2, indent + 1)