blob: 7fd2eeb24ac1fe106b42de3e9d91a2384c8c366e [file] [log] [blame]
Joey Armstrong44fa7d82022-11-01 17:46:04 -04001# -*- python -*-
2## -----------------------------------------------------------------------
3## Intent: This module contains general helper methods
4## -----------------------------------------------------------------------
5
6# -----------------------------------------------------------------------
7# Copyright 2022 Open Networking Foundation (ONF) and the ONF Contributors
8#
9# Licensed under the Apache License, Version 2.0 (the "License");
10# you may not use this file except in compliance with the License.
11# You may obtain a copy of the License at
12#
13# http://www.apache.org/licenses/LICENSE-2.0
14#
15# Unless required by applicable law or agreed to in writing, software
16# distributed under the License is distributed on an "AS IS" BASIS,
17# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18# See the License for the specific language governing permissions and
19# limitations under the License.
20# -----------------------------------------------------------------------
21
22##-------------------##
23##---] IMPORTS [---##
24##-------------------##
25import sys
26import pprint
27
28## ---------------------------------------------------------------------------
29## ---------------------------------------------------------------------------
30def iam():
31 """Return name of a called method."""
32
33 func_name = sys._getframe(1).f_code.co_name # pylint: disable=protected-access
34 iam = "%s::%s" % (__name__, func_name)
35 return iam
36
37## -----------------------------------------------------------------------
38## Intent: Display a message then exit with non-zero status.
39## This method cannot be intercepted by try/except
40## -----------------------------------------------------------------------
41def error(msg, exit_with=None, fatal=None):
42 """Display a message then exit with non-zero status.
43
44 :param msg: Error mesage to display.
45 :type msg: string
46
47 :param exit_with: Shell exit status.
48 :type exit_with: int, optional (default=2)
49
50 :param fatal: When true raise an exception.
51 :type fatal: bool (default=False)
52
53 """
54
55 if exit_with is None:
56 exit_with = 2
57
58 if fatal is None:
59 fatal = false
60
61 if msg:
62 if fatal:
63 raise Exception("ERROR: %s" % msg)
64 else:
65 print("")
66 print("ERROR: %s" % msg)
67
68 sys.exit(exit_with)
69
70# EOF