1

OS X 命令system_profiler SPAudioDataType给出的输出如附录 1 所示

这对我来说看起来很难解析。

例如:

  • 得到ConnectionSpeaker

grepfor^\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并记住所有看到的缩进级别,以便能够将与前一个级别相比缩进减少的节点重新附加到其父级或作为更高级别嵌套的兄弟级。

附录1


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

4

2 回答 2

1

这仍然是一个“OSX 管理员”问题。使用-xml标志

system_profiler -xml SPAudioDataType

它输出一个准备解析的 pList 文件。

于 2013-03-29T17:14:10.487 回答
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 选择器这样的搜索功能..

你怎么看?不正确?

于 2013-03-29T18:52:23.617 回答