1# Copyright (c) 2009 The Hewlett-Packard Development Company
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27from __future__ import print_function
28from six import string_types
29
30import os
31import sys
32
33class PairContainer(object):
34    def __init__(self, pairs=None):
35        self.pairs = {}
36        if pairs:
37            self.pairs.update(pairs)
38
39    def __contains__(self, item):
40        return item in self.pairs
41
42    def __getitem__(self, item):
43        return self.pairs[item]
44
45    def __setitem__(self, item, value):
46        self.pairs[item] = value
47
48    def get(self, item, failobj=None):
49        return self.pairs.get(item, failobj)
50
51class Location(object):
52    def __init__(self, filename, lineno, no_warning=False):
53        if not isinstance(filename, string_types):
54            raise AttributeError, \
55                "filename must be a string, found '%s'" % (type(filename), )
56        if not isinstance(lineno, (int, long)):
57            raise AttributeError, \
58                "filename must be an integer, found '%s'" % (type(lineno), )
59        self.filename = filename
60        self.lineno = lineno
61        self.no_warning = no_warning
62
63    def __str__(self):
64        return '%s:%d' % (os.path.basename(self.filename), self.lineno)
65
66    def warning(self, message, *args):
67        if self.no_warning:
68            return
69        if args:
70            message = message % args
71        #raise Exception, "%s: Warning: %s" % (self, message)
72        print("%s: Warning: %s" % (self, message), file=sys.stderr)
73
74    def error(self, message, *args):
75        if args:
76            message = message % args
77        raise Exception, "%s: Error: %s" % (self, message)
78        sys.exit("\n%s: Error: %s" % (self, message))
79
80__all__ = [ 'PairContainer', 'Location' ]
81