RMF
rmf_xml.py
1## \example rmf_xml.py
2# This example converts an RMF file to xml, in order to display it in
3# an XML viewer such as firefox. Its functionality is largely
4# identical to the \c rmf_xml program. The secondary purpose it to
5# provide an example of extracting all data from an RMF file and
6# converting it to some other source. The data extracted here is
7# completely uninterpreted (eg, cartesian coordinates are treated just
8# the same as other values). See the source code of rmf_xml for a
9# similar example in C++.
10
11import RMF
12
13# don't bother with command line arguments, to keep in simple
14file_name = RMF.get_example_path("simple.rmf3")
15verbose = True
16
17# show the data with the specified key category
18
19
20def show_data_xml(nh, kc):
21 rh = nh.get_file()
22 # get all the keys, we could pull this up in the call stack
23 keys = rh.get_keys(kc)
24 opened = False
25 for k in keys:
26 v = nh.get_value(k)
27 if v is not None:
28 if not opened:
29 print("<", rh.get_name(kc))
30 opened = True
31 name = rh.get_name(k)
32 name.replace(" ", "_")
33 print(name, "=\"" + str(v) + "\"")
34 if opened:
35 print("/>")
36
37
38def show_xml(nh, kcs):
39 name = nh.get_name()
40 name.replace(" ", "_")
41 print("<node name=\"" + name + "\" id=\"" + str(nh.get_id().get_index())\
42 + "\" type=\"" + str(nh.get_type()) + "\"/>")
43 if verbose:
44 for kc in kcs:
45 show_data_xml(nh, kc)
46 children = nh.get_children()
47 for c in children:
48 print("<child>")
49 show_xml(c, kcs)
50 print("</child>")
51
52# open the file, and don't clear the contents
53rh = RMF.open_rmf_file_read_only(file_name)
54rh.set_current_frame(RMF.FrameID(0))
55print("<?xml version=\"1.0\"?>")
56print("<rmf>")
57print("<path>")
58print(file_name)
59print("</path>")
60print("<description>")
61print(rh.get_description())
62print("</description>")
63print("<path>")
64print(input)
65print("</path>")
66kcs = rh.get_categories()
67show_xml(rh.get_root_node(), kcs)
68print("</rmf>")
FileConstHandle open_rmf_file_read_only(std::string path)