OS X命令system_profiler SPAudioDataType
给出附录1
这对我来说很难解析。
例如:
Connection
Speaker
grep
^\s+Connection: (.*)$
没有帮助:捕获论坛\1
将具有Connection
值,但也会为行而不是选择值嵌套在Speaker
下。
有趣的是,使用sed
解析可能更容易。例如等待看到线路音频,然后线扬声器,...,然后正则表达:
之后的文本。
我可以grep
MULTILINE
在整篇文章中首先匹配Speaker
,然后Connection
,跳过换行符/空白。
是否有一个库可以从不同缩进的文本行构建嵌套节点的对象模型?
有没有办法查询类似于CSS选择器的对象模型?例如Audio > Speaker > Connection > Value
我喜欢YAML考虑空格的方式,但这不会解析为YAML。
Java或Python中的库非常适合学习。
我自己编码,然后我决定问:
def main():
c = 0
with open(sys.argv[1]) as f:
for l in (l.rstrip() for l in f):
m = l.lstrip()
if not m:
continue
i = len(l) - len(m)
if i < c:
pass # Towards parent
elif i > c:
pass # A child
else:
pass # A sibling
c = i
我怀疑我需要假设第一个节点将处于缩进0
并记住所有看到的缩进级别,以便能够重新附加减少的节点缩进与其父级的先前级别相比或作为更高级别嵌套的兄弟级别。
Audio:
Intel High Definition Audio:
Audio ID: 29
Headphone:
Connection: Combination Output
Speaker:
Connection: Internal
Line Input:
Connection: Combination Input
Internal Microphone:
Connection: Internal
S/PDIF Optical Digital Audio Input:
Connection: Combination Input
S/PDIF Optical Digital Audio Output:
Connection: Combination Output
External Microphone / iPhone Headset:
Connection: Combination Output
HDMI / DisplayPort Output:
Connection: Display
Devices:
Built-in Microphone:
Default Input Device: Yes
Input Channels: 2
Manufacturer: Apple Inc.
Current SampleRate: 44100
Transport: Built-in
Built-in Input:
Input Channels: 2
Manufacturer: Apple Inc.
Current SampleRate: 44100
Transport: Built-in
Built-in Output:
Default Output Device: Yes
Default System Output Device: Yes
Manufacturer: Apple Inc.
Output Channels: 2
Current SampleRate: 44100
Transport: Built-in
答案 0 :(得分:1)
答案 1 :(得分:0)
我将其编码为解析:
import sys
import asciitree
class Node(object):
def __init__(self, name, indent, parent):
self.name = name
self.indent = indent
self.parent = parent
self.children = []
def add(self, child):
self.children.append(child)
def __str__(self):
return self.name
def main():
current_indent = -1
root_node = Node('Root', current_indent, None)
current_node = root_node
with open(sys.argv[1]) as file_to_parse:
for scanned_line in (l.rstrip() for l in file_to_parse):
line_content = scanned_line.lstrip()
if not line_content:
continue
indent = len(scanned_line) - len(line_content)
while True:
if indent > current_node.indent:
parent = current_node
break
elif indent == current_node.indent:
parent = current_node.parent
break
else:
current_node = current_node.parent
child = Node(line_content, indent, parent)
parent.add(child)
current_node = child
print(asciitree.draw_tree(root_node))
if __name__ == '__main__':
main()
产生这个对象模型:
Root
+--Audio:
+--Intel High Definition Audio:
| +--Audio ID: 29
| +--Headphone:
| | +--Connection: Combination Output
| +--Speaker:
| | +--Connection: Internal
| +--Line Input:
| | +--Connection: Combination Input
| +--Internal Microphone:
| | +--Connection: Internal
| +--S/PDIF Optical Digital Audio Input:
| | +--Connection: Combination Input
| +--S/PDIF Optical Digital Audio Output:
| | +--Connection: Combination Output
| +--External Microphone / iPhone Headset:
| | +--Connection: Combination Output
| +--HDMI / DisplayPort Output:
| +--Connection: Display
+--Devices:
+--Built-in Microphone:
| +--Default Input Device: Yes
| +--Input Channels: 2
| +--Manufacturer: Apple Inc.
| +--Current SampleRate: 44100
| +--Transport: Built-in
+--Built-in Input:
| +--Input Channels: 2
| +--Manufacturer: Apple Inc.
| +--Current SampleRate: 44100
| +--Transport: Built-in
+--Built-in Output:
| +--Default Output Device: Yes
| +--Default System Output Device: Yes
| +--Manufacturer: Apple Inc.
| +--Output Channels: 2
| +--Current SampleRate: 44100
| +--Transport: Built-in
+--After Effects 11.0:
| +--Manufacturer: Apple, Inc.
| +--Current SampleRate: 0
| +--Transport: Unknown
+--Prelude 1.0:
| +--Manufacturer: Apple, Inc.
| +--Current SampleRate: 0
| +--Transport: Unknown
+--Premiere Pro 6.0:
+--Manufacturer: Apple, Inc.
+--Current SampleRate: 0
+--Transport: Unknown
我现在需要实现像CSS选择器这样的搜索功能..
你怎么看?不正确的?