RMF
rmf_xml.py

This example converts an RMF file to xml, in order to display it in an XML viewer such as firefox. Its functionality is largely identical to the rmf_xml program. The secondary purpose it to provide an example of extracting all data from an RMF file and converting it to some other source. The data extracted here is completely uninterpreted (eg, cartesian coordinates are treated just the same as other values). See the source code of rmf_xml for a similar example in C++.

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 
11 from __future__ import print_function
12 import RMF
13 
14 # don't bother with command line arguments, to keep in simple
15 file_name = RMF.get_example_path("simple.rmf3")
16 verbose = True
17 
18 # show the data with the specified key category
19 
20 
21 def show_data_xml(nh, kc):
22  rh = nh.get_file()
23  # get all the keys, we could pull this up in the call stack
24  keys = rh.get_keys(kc)
25  opened = False
26  for k in keys:
27  v = nh.get_value(k)
28  if v is not None:
29  if not opened:
30  print("<", rh.get_name(kc))
31  opened = True
32  name = rh.get_name(k)
33  name.replace(" ", "_")
34  print(name, "=\"" + str(v) + "\"")
35  if opened:
36  print("/>")
37 
38 
39 def show_xml(nh, kcs):
40  name = nh.get_name()
41  name.replace(" ", "_")
42  print("<node name=\"" + name + "\" id=\"" + str(nh.get_id().get_index())\
43  + "\" type=\"" + str(nh.get_type()) + "\"/>")
44  if verbose:
45  for kc in kcs:
46  show_data_xml(nh, kc)
47  children = nh.get_children()
48  for c in children:
49  print("<child>")
50  show_xml(c, kcs)
51  print("</child>")
52 
53 # open the file, and don't clear the contents
54 rh = RMF.open_rmf_file_read_only(file_name)
55 rh.set_current_frame(RMF.FrameID(0))
56 print("<?xml version=\"1.0\"?>")
57 print("<rmf>")
58 print("<path>")
59 print(file_name)
60 print("</path>")
61 print("<description>")
62 print(rh.get_description())
63 print("</description>")
64 print("<path>")
65 print(input)
66 print("</path>")
67 kcs = rh.get_categories()
68 show_xml(rh.get_root_node(), kcs)
69 print("</rmf>")