blob: 7a55c11db082d28aa696d886f473cdad5f0995c4 [file] [log] [blame]
Rich Lane62a56492013-05-10 15:28:04 -07001#!/usr/bin/python
2# Copyright 2013, Big Switch Networks, Inc.
3#
4# LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with
5# the following special exception:
6#
7# LOXI Exception
8#
9# As a special exception to the terms of the EPL, you may distribute libraries
10# generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided
11# that copyright and licensing notices generated by LoxiGen are not altered or removed
12# from the LoxiGen Libraries and the notice provided below is (i) included in
13# the LoxiGen Libraries, if distributed in source code form and (ii) included in any
14# documentation for the LoxiGen Libraries, if distributed in binary form.
15#
16# Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler."
17#
18# You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain
19# a copy of the EPL at:
20#
21# http://www.eclipse.org/legal/epl-v10.html
22#
23# Unless required by applicable law or agreed to in writing, software
24# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
26# EPL for the specific language governing permissions and limitations
27# under the EPL.
28
29import os
30
31_test_data_dir = os.path.dirname(os.path.realpath(__file__))
32
33def list_files():
34 """
35 Return a list of the data files in this directory
36
37 These strings are suitable to be passed to read().
38 """
39
40 result = []
41 for dirname, dirnames, filenames in os.walk(_test_data_dir):
42 dirname = os.path.relpath(dirname, _test_data_dir)
43 for filename in filenames:
44 if filename.endswith('.data') and not filename.startswith('.'):
45 result.append(dirname + '/' + filename)
46 return sorted(result)
47
Andreas Wundsame916d6f2013-07-30 11:33:58 -070048def exists(name):
49 return os.path.exists(os.path.join(_test_data_dir, name))
50
Rich Lane62a56492013-05-10 15:28:04 -070051def read(name):
52 """
53 Read, parse, and return a test data file
54
55 @param name Filename relative to the test_data directory
56 @returns A hash from section to the string contents
57
58 A section named "binary" is treated specially: it's treated
59 as a hex dump and parsed into a binary string.
60 """
61
62 section_lines = {}
63 cur_section = None
64
65 with open(os.path.join(_test_data_dir, name)) as f:
66 for line in f:
Rich Lanea0ee6362013-05-16 16:35:35 -070067 line = line.rstrip().partition('#')[0].rstrip()
Rich Lane62a56492013-05-10 15:28:04 -070068 if line == '':
69 continue
70 elif line.startswith('--'):
71 cur_section = line[2:].strip()
72 if cur_section in section_lines:
73 raise Exception("section %s already exists in the test data file")
74 section_lines[cur_section] = []
75 elif cur_section:
76 section_lines[cur_section].append(line)
77 data = { section: '\n'.join(lines) for (section, lines) in section_lines.items() }
78
79 # Special case: convert 'binary' section into binary
80 # The string '00 11\n22 33' results in "\x00\x11\x22\x33"
81 if 'binary' in data:
82 hex_strs = data['binary'].split()
83 data['binary'] = ''.join(map(lambda x: chr(int(x, 16)), hex_strs))
84
85 return data