Debian Codegraph @

examples/helloworld.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 helloworld
4 ~~~~~~~~~~
5
6 A minimal working example, showing :mod:`pyxs` API usage.
7
8 :copyright: (c) 2016 by pyxs authors and contributors,
9 see AUTHORS for more details.
10 """
11
12 from __future__ import print_function
13
14 from pyxs import Client, PyXSError
15
16
17 if __name__ == "__main__":
18 with Client() as c:
19 # a) write-read.
20 c[b"/foo/bar"] = b"baz"
21 print(c[b"/foo/bar"]) # ==> "baz"
22
23 # b) exceptions! let's try to read a non-existant path.
24 try:
25 c[b"/path/to/something/useless"]
26 except PyXSError as e:
27 print(e)
28
29 # c) okay, time to delete that /foo/bar path.
30 del c[b"/foo/bar"]
31
32 try:
33 c[b"/foo/bar"]
34 except PyXSError as e:
35 print("`/foo/bar` is no moar!")
36
37 # d) directory listing and permissions.
38 c.mkdir(b"/foo/bar")
39 c.mkdir(b"/foo/baz")
40 c.mkdir(b"/foo/boo")
41 print("Here's what `/foo` has: ", c.list(b"/foo"))
42 print(c.get_perms(b"/foo"))
43
44 # e) let's watch some paths!
45 c.write(b"/foo/bar", b"baz")
46 with c.monitor() as m:
47 m.watch(b"/foo/bar", b"baz")
48 print("Watching ... do ``xenstore-write /foo/bar <anything>``.")
49 print(next(m.wait())) # 1st.
50 print(next(m.wait())) # 2nd.
51 m.unwatch(b"/foo/bar", b"baz")
52
53 # f) domain management commands.
54 print(c.get_domain_path(0))
55 print("domain 0 introduced: ", c.is_domain_introduced(0))
56
57 # g) transactions.
58 print("Creating a `/bar/foo` within a transaction.")
59 while True:
60 c.transaction()
61 c[b"/bar/foo"] = b"baz"
62 if c.commit():
63 break
64
65 print("Transaction committed. Let's check it: "
66 "/bar/foo =", c[b"/bar/foo"])