1

OS X コマンドsystem_profiler SPAudioDataTypeは、付録 1 に示す出力を提供します。

これは私には解析が非常に難しいようです。

例えば:

  • Connectionの取得Speaker

grepfor^\s+Connection: (.*)$は役に立ちません: キャプチャ グループ\1には値がありますが、下にネストされていないConnection行の値も取得されます。Speaker

興味深いことに、 で解析する方が簡単かもしれませんsed。たとえば、Audio の行、Speaker の行、...、次のテキストの正規表現が表示されるのを待ちます:

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 に答える