File: ls.py

package info (click to toggle)
python-pyxs 0.4.2~git20190115.97f14313-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 328 kB
  • sloc: python: 1,196; makefile: 93
file content (39 lines) | stat: -rw-r--r-- 860 bytes parent folder | download | duplicates (3)
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
# -*- coding: utf-8 -*-
"""
    ls
    ~~

    ``xenstore-ls`` implementation with :mod:`pyxs`.

    :copyright: (c) 2011 by Selectel, see AUTHORS for more details.
"""

from __future__ import print_function

import posixpath
import sys

from pyxs.client import Client


def main(client, top):
    # ``xenstore-ls`` doesn't render top level.
    depth = top.count(b"/") + (top != b"/")

    for path, value, children in client.walk(top):
        if path == top:
            continue

        node = posixpath.basename(path)
        indent = " " * (path.count(b"/") - depth)
        print("{0}{1} = \"{2}\"".format(indent, node, value))


if __name__ == "__main__":
    try:
        [path] = sys.argv[1:] or ["/"]
    except ValueError:
        sys.exit("usage: %prog PATH")

    with Client() as client:
        main(client, posixpath.normpath(path).encode())