blob: dc063fa5617b27d2fa3640ec9e17118362164d0a [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
Andreas Wundsamaa4f3cd2013-10-22 22:19:08 -070029import fnmatch
Rich Lane62a56492013-05-10 15:28:04 -070030import os
31
32_test_data_dir = os.path.dirname(os.path.realpath(__file__))
33
34def list_files():
35 """
36 Return a list of the data files in this directory
37
38 These strings are suitable to be passed to read().
39 """
40
41 result = []
42 for dirname, dirnames, filenames in os.walk(_test_data_dir):
43 dirname = os.path.relpath(dirname, _test_data_dir)
44 for filename in filenames:
45 if filename.endswith('.data') and not filename.startswith('.'):
46 result.append(dirname + '/' + filename)
47 return sorted(result)
48
Andreas Wundsamaa4f3cd2013-10-22 22:19:08 -070049def glob(pattern):
50 for f in list_files():
51 if fnmatch.fnmatch(f, pattern):
52 yield f
53
Andreas Wundsame916d6f2013-07-30 11:33:58 -070054def exists(name):
55 return os.path.exists(os.path.join(_test_data_dir, name))
56
Rich Lane62a56492013-05-10 15:28:04 -070057def read(name):
58 """
59 Read, parse, and return a test data file
60
61 @param name Filename relative to the test_data directory
62 @returns A hash from section to the string contents
63
64 A section named "binary" is treated specially: it's treated
65 as a hex dump and parsed into a binary string.
66 """
67
68 section_lines = {}
69 cur_section = None
70
71 with open(os.path.join(_test_data_dir, name)) as f:
72 for line in f:
Rich Lanea0ee6362013-05-16 16:35:35 -070073 line = line.rstrip().partition('#')[0].rstrip()
Rich Lane62a56492013-05-10 15:28:04 -070074 if line == '':
75 continue
76 elif line.startswith('--'):
77 cur_section = line[2:].strip()
78 if cur_section in section_lines:
79 raise Exception("section %s already exists in the test data file")
80 section_lines[cur_section] = []
81 elif cur_section:
82 section_lines[cur_section].append(line)
83 data = { section: '\n'.join(lines) for (section, lines) in section_lines.items() }
84
85 # Special case: convert 'binary' section into binary
86 # The string '00 11\n22 33' results in "\x00\x11\x22\x33"
87 if 'binary' in data:
88 hex_strs = data['binary'].split()
89 data['binary'] = ''.join(map(lambda x: chr(int(x, 16)), hex_strs))
90
91 return data