Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | This module provides many valuable print functions such as sprint_var, |
| 5 | sprint_time, sprint_error, sprint_call_stack. |
| 6 | """ |
| 7 | |
| 8 | import sys |
| 9 | import os |
| 10 | import time |
| 11 | import inspect |
| 12 | import re |
| 13 | import grp |
| 14 | import socket |
| 15 | import argparse |
George Keishing | 3b7115a | 2018-08-02 10:48:17 -0500 | [diff] [blame] | 16 | try: |
| 17 | import __builtin__ |
| 18 | except ImportError: |
| 19 | import builtins as __builtin__ |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 20 | import logging |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 21 | import collections |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 22 | from wrap_utils import * |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 23 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 24 | try: |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 25 | robot_env = 1 |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 26 | from robot.utils import DotDict |
Michael Walsh | 8e6deb4 | 2017-01-27 14:22:41 -0600 | [diff] [blame] | 27 | from robot.utils import NormalizedDict |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 28 | from robot.libraries.BuiltIn import BuiltIn |
Michael Walsh | b150015 | 2017-04-12 15:42:43 -0500 | [diff] [blame] | 29 | # Having access to the robot libraries alone does not indicate that we |
| 30 | # are in a robot environment. The following try block should confirm that. |
| 31 | try: |
| 32 | var_value = BuiltIn().get_variable_value("${SUITE_NAME}", "") |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 33 | except BaseException: |
Michael Walsh | b150015 | 2017-04-12 15:42:43 -0500 | [diff] [blame] | 34 | robot_env = 0 |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 35 | except ImportError: |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 36 | robot_env = 0 |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 37 | |
| 38 | import gen_arg as ga |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 39 | |
| 40 | # Setting these variables for use both inside this module and by programs |
| 41 | # importing this module. |
Michael Walsh | bf60565 | 2017-09-01 12:33:26 -0500 | [diff] [blame] | 42 | pgm_file_path = sys.argv[0] |
| 43 | pgm_name = os.path.basename(pgm_file_path) |
Michael Walsh | 3ba8ecd | 2018-04-24 11:33:25 -0500 | [diff] [blame] | 44 | pgm_dir_path = os.path.normpath(re.sub("/" + pgm_name, "", pgm_file_path)) +\ |
| 45 | os.path.sep |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 46 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 47 | |
| 48 | # Some functions (e.g. sprint_pgm_header) have need of a program name value |
| 49 | # that looks more like a valid variable name. Therefore, we'll swap odd |
| 50 | # characters like "." out for underscores. |
| 51 | pgm_name_var_name = pgm_name.replace(".", "_") |
| 52 | |
| 53 | # Initialize global values used as defaults by print_time, print_var, etc. |
| 54 | col1_indent = 0 |
| 55 | |
| 56 | # Calculate default column width for print_var functions based on environment |
| 57 | # variable settings. The objective is to make the variable values line up |
| 58 | # nicely with the time stamps. |
| 59 | col1_width = 29 |
Michael Walsh | b150015 | 2017-04-12 15:42:43 -0500 | [diff] [blame] | 60 | |
| 61 | NANOSECONDS = os.environ.get('NANOSECONDS', '1') |
| 62 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 63 | |
| 64 | if NANOSECONDS == "1": |
| 65 | col1_width = col1_width + 7 |
| 66 | |
Michael Walsh | b150015 | 2017-04-12 15:42:43 -0500 | [diff] [blame] | 67 | SHOW_ELAPSED_TIME = os.environ.get('SHOW_ELAPSED_TIME', '1') |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 68 | |
| 69 | if SHOW_ELAPSED_TIME == "1": |
| 70 | if NANOSECONDS == "1": |
| 71 | col1_width = col1_width + 14 |
| 72 | else: |
| 73 | col1_width = col1_width + 7 |
| 74 | |
| 75 | # Initialize some time variables used in module functions. |
| 76 | start_time = time.time() |
Michael Walsh | 4fea2cf | 2018-08-22 17:48:18 -0500 | [diff] [blame] | 77 | # sprint_time_last_seconds is used to calculate elapsed seconds. |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 78 | sprint_time_last_seconds = [start_time, start_time] |
Michael Walsh | 4fea2cf | 2018-08-22 17:48:18 -0500 | [diff] [blame] | 79 | # Define global index for the sprint_time_last_seconds list. |
| 80 | last_seconds_ix = 0 |
| 81 | |
| 82 | |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 83 | def set_last_seconds_ix(ix): |
| 84 | r""" |
| 85 | Set the "last_seconds_ix" module variable to the index value. |
| 86 | |
| 87 | Description of argument(s): |
| 88 | ix The index value to be set into the module |
| 89 | global last_seconds_ix variable. |
| 90 | """ |
| 91 | global last_seconds_ix |
| 92 | last_seconds_ix = ix |
| 93 | |
| 94 | |
Michael Walsh | 4fea2cf | 2018-08-22 17:48:18 -0500 | [diff] [blame] | 95 | # Since output from the lprint_ functions goes to a different location than |
| 96 | # the output from the print_ functions (e.g. a file vs. the console), |
| 97 | # sprint_time_last_seconds has been created as a list rather than a simple |
| 98 | # integer so that it can store multiple sprint_time_last_seconds values. |
| 99 | # Standard print_ functions defined in this file will use |
| 100 | # sprint_time_last_seconds[0] and the lprint_ functions will use |
| 101 | # sprint_time_last_seconds[1]. |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 102 | def standard_print_last_seconds_ix(): |
| 103 | r""" |
| 104 | Return the standard print last_seconds index value to the caller. |
| 105 | """ |
| 106 | return 0 |
| 107 | |
| 108 | |
Michael Walsh | 4fea2cf | 2018-08-22 17:48:18 -0500 | [diff] [blame] | 109 | def lprint_last_seconds_ix(): |
| 110 | r""" |
| 111 | Return lprint last_seconds index value to the caller. |
| 112 | """ |
| 113 | return 1 |
| 114 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 115 | |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 116 | # The user can set environment variable "GEN_PRINT_DEBUG" to get debug output |
| 117 | # from this module. |
| 118 | gen_print_debug = int(os.environ.get('GEN_PRINT_DEBUG', 0)) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 119 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 120 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 121 | def sprint_func_name(stack_frame_ix=None): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 122 | r""" |
| 123 | Return the function name associated with the indicated stack frame. |
| 124 | |
| 125 | Description of arguments: |
| 126 | stack_frame_ix The index of the stack frame whose |
| 127 | function name should be returned. If the |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 128 | caller does not specify a value, this |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 129 | function will set the value to 1 which is |
| 130 | the index of the caller's stack frame. If |
| 131 | the caller is the wrapper function |
| 132 | "print_func_name", this function will bump |
| 133 | it up by 1. |
| 134 | """ |
| 135 | |
| 136 | # If user specified no stack_frame_ix, we'll set it to a proper default |
| 137 | # value. |
| 138 | if stack_frame_ix is None: |
| 139 | func_name = sys._getframe().f_code.co_name |
| 140 | caller_func_name = sys._getframe(1).f_code.co_name |
| 141 | if func_name[1:] == caller_func_name: |
| 142 | stack_frame_ix = 2 |
| 143 | else: |
| 144 | stack_frame_ix = 1 |
| 145 | |
| 146 | func_name = sys._getframe(stack_frame_ix).f_code.co_name |
| 147 | |
| 148 | return func_name |
| 149 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 150 | |
Michael Walsh | 6f0362c | 2019-03-25 14:05:14 -0500 | [diff] [blame] | 151 | def work_around_inspect_stack_cwd_failure(): |
| 152 | r""" |
| 153 | Work around the inspect.stack() getcwd() failure by making "/tmp" the |
| 154 | current working directory. |
| 155 | |
| 156 | If the current working directory has been deleted, inspect.stack() will |
| 157 | fail with "OSError: [Errno 2] No such file or directory" because it tries |
| 158 | to do a getcwd(). |
| 159 | |
| 160 | This function will try to prevent this failure by detecting the scenario |
| 161 | in advance and making "/tmp" the current working directory. |
| 162 | """ |
| 163 | try: |
| 164 | os.getcwd() |
| 165 | except OSError: |
| 166 | os.chdir("/tmp") |
| 167 | |
| 168 | |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 169 | def get_line_indent(line): |
| 170 | r""" |
| 171 | Return the number of spaces at the beginning of the line. |
| 172 | """ |
| 173 | |
| 174 | return len(line) - len(line.lstrip(' ')) |
| 175 | |
| 176 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 177 | # get_arg_name is not a print function per se. I have included it in this |
| 178 | # module because it is used by sprint_var which is found in this module. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 179 | def get_arg_name(var, |
| 180 | arg_num=1, |
| 181 | stack_frame_ix=1): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 182 | r""" |
| 183 | Return the "name" of an argument passed to a function. This could be a |
| 184 | literal or a variable name. |
| 185 | |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 186 | Description of arguments: |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 187 | var The variable whose name you want returned. |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 188 | arg_num The arg number whose name is to be |
| 189 | returned. To illustrate how arg_num is |
| 190 | processed, suppose that a programmer codes |
| 191 | this line: "rc, outbuf = my_func(var1, |
| 192 | var2)" and suppose that my_func has this |
| 193 | line of code: "result = gp.get_arg_name(0, |
| 194 | arg_num, 2)". If arg_num is positive, the |
| 195 | indicated argument is returned. For |
| 196 | example, if arg_num is 1, "var1" would be |
| 197 | returned, If arg_num is 2, "var2" would be |
| 198 | returned. If arg_num exceeds the number |
| 199 | of arguments, get_arg_name will simply |
| 200 | return a complete list of the arguments. |
| 201 | If arg_num is 0, get_arg_name will return |
| 202 | the name of the target function as |
| 203 | specified in the calling line ("my_func" |
| 204 | in this case). To clarify, if the caller |
| 205 | of the target function uses an alias |
| 206 | function name, the alias name would be |
| 207 | returned. If arg_num is negative, an |
| 208 | lvalue variable name is returned. |
| 209 | Continuing with the given example, if |
| 210 | arg_num is -2 the 2nd parm to the left of |
| 211 | the "=" ("rc" in this case) should be |
| 212 | returned. If arg_num is -1, the 1st parm |
| 213 | to the left of the "=" ("out_buf" in this |
| 214 | case) should be returned. If arg_num is |
| 215 | less than -2, an entire dictionary is |
| 216 | returned. The keys to the dictionary for |
| 217 | this example would be -2 and -1. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 218 | stack_frame_ix The stack frame index of the target |
| 219 | function. This value must be 1 or |
| 220 | greater. 1 would indicate get_arg_name's |
| 221 | stack frame. 2 would be the caller of |
| 222 | get_arg_name's stack frame, etc. |
| 223 | |
| 224 | Example 1: |
| 225 | |
| 226 | my_var = "mike" |
| 227 | var_name = get_arg_name(my_var) |
| 228 | |
| 229 | In this example, var_name will receive the value "my_var". |
| 230 | |
| 231 | Example 2: |
| 232 | |
| 233 | def test1(var): |
| 234 | # Getting the var name of the first arg to this function, test1. |
| 235 | # Note, in this case, it doesn't matter what you pass as the first arg |
| 236 | # to get_arg_name since it is the caller's variable name that matters. |
| 237 | dummy = 1 |
| 238 | arg_num = 1 |
| 239 | stack_frame = 2 |
| 240 | var_name = get_arg_name(dummy, arg_num, stack_frame) |
| 241 | |
| 242 | # Mainline... |
| 243 | |
| 244 | another_var = "whatever" |
| 245 | test1(another_var) |
| 246 | |
| 247 | In this example, var_name will be set to "another_var". |
| 248 | |
| 249 | """ |
| 250 | |
| 251 | # Note: I wish to avoid recursion so I refrain from calling any function |
| 252 | # that calls this function (i.e. sprint_var, valid_value, etc.). |
| 253 | |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 254 | # The user can set environment variable "GET_ARG_NAME_DEBUG" to get debug |
| 255 | # output from this function. |
| 256 | local_debug = int(os.environ.get('GET_ARG_NAME_DEBUG', 0)) |
| 257 | # In addition to GET_ARG_NAME_DEBUG, the user can set environment |
| 258 | # variable "GET_ARG_NAME_SHOW_SOURCE" to have this function include source |
| 259 | # code in the debug output. |
| 260 | local_debug_show_source = int( |
| 261 | os.environ.get('GET_ARG_NAME_SHOW_SOURCE', 0)) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 262 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 263 | if stack_frame_ix < 1: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 264 | print_error("Programmer error - Variable \"stack_frame_ix\" has an" |
| 265 | + " invalid value of \"" + str(stack_frame_ix) + "\". The" |
| 266 | + " value must be an integer that is greater than or equal" |
| 267 | + " to 1.\n") |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 268 | return |
| 269 | |
| 270 | if local_debug: |
| 271 | debug_indent = 2 |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 272 | print("") |
| 273 | print_dashes(0, 120) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 274 | print(sprint_func_name() + "() parms:") |
| 275 | print_varx("var", var, 0, debug_indent) |
| 276 | print_varx("arg_num", arg_num, 0, debug_indent) |
| 277 | print_varx("stack_frame_ix", stack_frame_ix, 0, debug_indent) |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 278 | print("") |
| 279 | print_call_stack(debug_indent, 2) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 280 | |
Michael Walsh | 6f0362c | 2019-03-25 14:05:14 -0500 | [diff] [blame] | 281 | work_around_inspect_stack_cwd_failure() |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 282 | for count in range(0, 2): |
| 283 | try: |
| 284 | frame, filename, cur_line_no, function_name, lines, index = \ |
| 285 | inspect.stack()[stack_frame_ix] |
| 286 | except IndexError: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 287 | print_error("Programmer error - The caller has asked for" |
| 288 | + " information about the stack frame at index \"" |
| 289 | + str(stack_frame_ix) + "\". However, the stack" |
| 290 | + " only contains " + str(len(inspect.stack())) |
| 291 | + " entries. Therefore the stack frame index is out" |
| 292 | + " of range.\n") |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 293 | return |
| 294 | if filename != "<string>": |
| 295 | break |
| 296 | # filename of "<string>" may mean that the function in question was |
| 297 | # defined dynamically and therefore its code stack is inaccessible. |
| 298 | # This may happen with functions like "rqprint_var". In this case, |
| 299 | # we'll increment the stack_frame_ix and try again. |
| 300 | stack_frame_ix += 1 |
| 301 | if local_debug: |
| 302 | print("Adjusted stack_frame_ix...") |
| 303 | print_varx("stack_frame_ix", stack_frame_ix, 0, debug_indent) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 304 | |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 305 | real_called_func_name = sprint_func_name(stack_frame_ix) |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 306 | |
| 307 | module = inspect.getmodule(frame) |
| 308 | |
| 309 | # Though I would expect inspect.getsourcelines(frame) to get all module |
| 310 | # source lines if the frame is "<module>", it doesn't do that. Therefore, |
| 311 | # for this special case, I will do inspect.getsourcelines(module). |
| 312 | if function_name == "<module>": |
| 313 | source_lines, source_line_num =\ |
| 314 | inspect.getsourcelines(module) |
| 315 | line_ix = cur_line_no - source_line_num - 1 |
| 316 | else: |
| 317 | source_lines, source_line_num =\ |
| 318 | inspect.getsourcelines(frame) |
| 319 | line_ix = cur_line_no - source_line_num |
| 320 | |
| 321 | if local_debug: |
| 322 | print("\n Variables retrieved from inspect.stack() function:") |
| 323 | print_varx("frame", frame, 0, debug_indent + 2) |
| 324 | print_varx("filename", filename, 0, debug_indent + 2) |
| 325 | print_varx("cur_line_no", cur_line_no, 0, debug_indent + 2) |
| 326 | print_varx("function_name", function_name, 0, debug_indent + 2) |
| 327 | print_varx("lines", lines, 0, debug_indent + 2) |
| 328 | print_varx("index", index, 0, debug_indent + 2) |
| 329 | print_varx("source_line_num", source_line_num, 0, debug_indent) |
| 330 | print_varx("line_ix", line_ix, 0, debug_indent) |
| 331 | if local_debug_show_source: |
| 332 | print_varx("source_lines", source_lines, 0, debug_indent) |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 333 | print_varx("real_called_func_name", real_called_func_name, 0, |
| 334 | debug_indent) |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 335 | |
| 336 | # Get a list of all functions defined for the module. Note that this |
| 337 | # doesn't work consistently when _run_exitfuncs is at the top of the stack |
| 338 | # (i.e. if we're running an exit function). I've coded a work-around |
| 339 | # below for this deficiency. |
| 340 | all_functions = inspect.getmembers(module, inspect.isfunction) |
| 341 | |
| 342 | # Get called_func_id by searching for our function in the list of all |
| 343 | # functions. |
| 344 | called_func_id = None |
| 345 | for func_name, function in all_functions: |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 346 | if func_name == real_called_func_name: |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 347 | called_func_id = id(function) |
| 348 | break |
| 349 | # NOTE: The only time I've found that called_func_id can't be found is |
| 350 | # when we're running from an exit function. |
| 351 | |
| 352 | # Look for other functions in module with matching id. |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 353 | aliases = set([real_called_func_name]) |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 354 | for func_name, function in all_functions: |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 355 | if func_name == real_called_func_name: |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 356 | continue |
| 357 | func_id = id(function) |
| 358 | if func_id == called_func_id: |
| 359 | aliases.add(func_name) |
| 360 | |
| 361 | # In most cases, my general purpose code above will find all aliases. |
| 362 | # However, for the odd case (i.e. running from exit function), I've added |
| 363 | # code to handle pvar, qpvar, dpvar, etc. aliases explicitly since they |
| 364 | # are defined in this module and used frequently. |
| 365 | # pvar is an alias for print_var. |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 366 | aliases.add(re.sub("print_var", "pvar", real_called_func_name)) |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 367 | |
Michael Walsh | 3f24827 | 2018-06-01 13:59:35 -0500 | [diff] [blame] | 368 | # The call to the function could be encased in a recast (e.g. |
| 369 | # int(func_name())). |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 370 | recast_regex = "([^ ]+\\([ ]*)?" |
| 371 | import_name_regex = "([a-zA-Z0-9_]+\\.)?" |
Michael Walsh | 3f24827 | 2018-06-01 13:59:35 -0500 | [diff] [blame] | 372 | func_name_regex = recast_regex + import_name_regex + "(" +\ |
| 373 | '|'.join(aliases) + ")" |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 374 | pre_args_regex = ".*" + func_name_regex + "[ ]*\\(" |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 375 | |
| 376 | # Search backward through source lines looking for the calling function |
| 377 | # name. |
| 378 | found = False |
| 379 | for start_line_ix in range(line_ix, 0, -1): |
| 380 | # Skip comment lines. |
| 381 | if re.match(r"[ ]*#", source_lines[start_line_ix]): |
| 382 | continue |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 383 | if re.match(pre_args_regex, source_lines[start_line_ix]): |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 384 | found = True |
| 385 | break |
| 386 | if not found: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 387 | print_error("Programmer error - Could not find the source line with" |
| 388 | + " a reference to function \"" + real_called_func_name |
| 389 | + "\".\n") |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 390 | return |
| 391 | |
Michael Walsh | 82acf00 | 2017-05-04 14:33:05 -0500 | [diff] [blame] | 392 | # Search forward through the source lines looking for a line whose |
| 393 | # indentation is the same or less than the start line. The end of our |
| 394 | # composite line should be the line preceding that line. |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 395 | start_indent = get_line_indent(source_lines[start_line_ix]) |
Michael Walsh | 37cd29d | 2018-05-24 13:19:18 -0500 | [diff] [blame] | 396 | end_line_ix = line_ix |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 397 | for end_line_ix in range(line_ix + 1, len(source_lines)): |
| 398 | if source_lines[end_line_ix].strip() == "": |
| 399 | continue |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 400 | line_indent = get_line_indent(source_lines[end_line_ix]) |
Michael Walsh | 82acf00 | 2017-05-04 14:33:05 -0500 | [diff] [blame] | 401 | if line_indent <= start_indent: |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 402 | end_line_ix -= 1 |
| 403 | break |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 404 | if start_line_ix != 0: |
| 405 | # Check to see whether the start line is a continuation of the prior |
Michael Walsh | a52e9eb | 2018-09-10 13:56:01 -0500 | [diff] [blame] | 406 | # line. |
| 407 | prior_line = source_lines[start_line_ix - 1] |
| 408 | prior_line_stripped = re.sub(r"[ ]*\\([\r\n]$)", " \\1", prior_line) |
| 409 | prior_line_indent = get_line_indent(prior_line) |
| 410 | if prior_line != prior_line_stripped and\ |
| 411 | prior_line_indent < start_indent: |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 412 | start_line_ix -= 1 |
Michael Walsh | a52e9eb | 2018-09-10 13:56:01 -0500 | [diff] [blame] | 413 | # Remove the backslash (continuation char) from prior line. |
| 414 | source_lines[start_line_ix] = prior_line_stripped |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 415 | |
| 416 | # Join the start line through the end line into a composite line. |
| 417 | composite_line = ''.join(map(str.strip, |
Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 418 | source_lines[start_line_ix:end_line_ix + 1])) |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 419 | # Insert one space after first "=" if there isn't one already. |
| 420 | composite_line = re.sub("=[ ]*([^ ])", "= \\1", composite_line, 1) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 421 | |
Michael Walsh | 3f24827 | 2018-06-01 13:59:35 -0500 | [diff] [blame] | 422 | lvalue_regex = "[ ]*=[ ]+" + func_name_regex + ".*" |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 423 | lvalue_string = re.sub(lvalue_regex, "", composite_line) |
Michael Walsh | 3f24827 | 2018-06-01 13:59:35 -0500 | [diff] [blame] | 424 | if lvalue_string == composite_line: |
| 425 | # i.e. the regex did not match so there are no lvalues. |
| 426 | lvalue_string = "" |
Michael Walsh | 37762f9 | 2018-08-07 14:59:18 -0500 | [diff] [blame] | 427 | lvalues_list = list(filter(None, map(str.strip, lvalue_string.split(",")))) |
Michael Walsh | 3f24827 | 2018-06-01 13:59:35 -0500 | [diff] [blame] | 428 | try: |
| 429 | lvalues = collections.OrderedDict() |
| 430 | except AttributeError: |
| 431 | # A non-ordered dict doesn't look as nice when printed but it will do. |
| 432 | lvalues = {} |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 433 | ix = len(lvalues_list) * -1 |
| 434 | for lvalue in lvalues_list: |
| 435 | lvalues[ix] = lvalue |
| 436 | ix += 1 |
Michael Walsh | 3f24827 | 2018-06-01 13:59:35 -0500 | [diff] [blame] | 437 | lvalue_prefix_regex = "(.*=[ ]+)?" |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 438 | called_func_name_regex = lvalue_prefix_regex + func_name_regex +\ |
| 439 | "[ ]*\\(.*" |
Michael Walsh | 3f24827 | 2018-06-01 13:59:35 -0500 | [diff] [blame] | 440 | called_func_name = re.sub(called_func_name_regex, "\\4", composite_line) |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 441 | arg_list_etc = "(" + re.sub(pre_args_regex, "", composite_line) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 442 | if local_debug: |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 443 | print_varx("aliases", aliases, 0, debug_indent) |
Michael Walsh | 3f24827 | 2018-06-01 13:59:35 -0500 | [diff] [blame] | 444 | print_varx("import_name_regex", import_name_regex, 0, debug_indent) |
| 445 | print_varx("func_name_regex", func_name_regex, 0, debug_indent) |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 446 | print_varx("pre_args_regex", pre_args_regex, 0, debug_indent) |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 447 | print_varx("start_line_ix", start_line_ix, 0, debug_indent) |
| 448 | print_varx("end_line_ix", end_line_ix, 0, debug_indent) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 449 | print_varx("composite_line", composite_line, 0, debug_indent) |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 450 | print_varx("lvalue_regex", lvalue_regex, 0, debug_indent) |
| 451 | print_varx("lvalue_string", lvalue_string, 0, debug_indent) |
| 452 | print_varx("lvalues", lvalues, 0, debug_indent) |
Michael Walsh | 3f24827 | 2018-06-01 13:59:35 -0500 | [diff] [blame] | 453 | print_varx("called_func_name_regex", called_func_name_regex, 0, |
| 454 | debug_indent) |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 455 | print_varx("called_func_name", called_func_name, 0, debug_indent) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 456 | print_varx("arg_list_etc", arg_list_etc, 0, debug_indent) |
| 457 | |
| 458 | # Parse arg list... |
| 459 | # Initialize... |
| 460 | nest_level = -1 |
| 461 | arg_ix = 0 |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 462 | args_list = [""] |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 463 | for ix in range(0, len(arg_list_etc)): |
| 464 | char = arg_list_etc[ix] |
| 465 | # Set the nest_level based on whether we've encounted a parenthesis. |
| 466 | if char == "(": |
| 467 | nest_level += 1 |
| 468 | if nest_level == 0: |
| 469 | continue |
| 470 | elif char == ")": |
| 471 | nest_level -= 1 |
| 472 | if nest_level < 0: |
| 473 | break |
| 474 | |
| 475 | # If we reach a comma at base nest level, we are done processing an |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 476 | # argument so we increment arg_ix and initialize a new args_list entry. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 477 | if char == "," and nest_level == 0: |
| 478 | arg_ix += 1 |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 479 | args_list.append("") |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 480 | continue |
| 481 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 482 | # For any other character, we append it it to the current arg list |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 483 | # entry. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 484 | args_list[arg_ix] += char |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 485 | |
| 486 | # Trim whitespace from each list entry. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 487 | args_list = [arg.strip() for arg in args_list] |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 488 | |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 489 | if arg_num < 0: |
| 490 | if abs(arg_num) > len(lvalues): |
| 491 | argument = lvalues |
| 492 | else: |
| 493 | argument = lvalues[arg_num] |
| 494 | elif arg_num == 0: |
| 495 | argument = called_func_name |
Michael Walsh | 2750b44 | 2018-05-18 14:49:11 -0500 | [diff] [blame] | 496 | else: |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 497 | if arg_num > len(args_list): |
| 498 | argument = args_list |
| 499 | else: |
| 500 | argument = args_list[arg_num - 1] |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 501 | |
| 502 | if local_debug: |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 503 | print_varx("args_list", args_list, 0, debug_indent) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 504 | print_varx("argument", argument, 0, debug_indent) |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 505 | print_dashes(0, 120) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 506 | |
| 507 | return argument |
| 508 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 509 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 510 | def sprint_time(buffer=""): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 511 | r""" |
| 512 | Return the time in the following format. |
| 513 | |
| 514 | Example: |
| 515 | |
| 516 | The following python code... |
| 517 | |
| 518 | sys.stdout.write(sprint_time()) |
| 519 | sys.stdout.write("Hi.\n") |
| 520 | |
| 521 | Will result in the following type of output: |
| 522 | |
| 523 | #(CDT) 2016/07/08 15:25:35 - Hi. |
| 524 | |
| 525 | Example: |
| 526 | |
| 527 | The following python code... |
| 528 | |
| 529 | sys.stdout.write(sprint_time("Hi.\n")) |
| 530 | |
| 531 | Will result in the following type of output: |
| 532 | |
| 533 | #(CDT) 2016/08/03 17:12:05 - Hi. |
| 534 | |
| 535 | The following environment variables will affect the formatting as |
| 536 | described: |
| 537 | NANOSECONDS This will cause the time stamps to be |
| 538 | precise to the microsecond (Yes, it |
| 539 | probably should have been named |
| 540 | MICROSECONDS but the convention was set |
| 541 | long ago so we're sticking with it). |
| 542 | Example of the output when environment |
| 543 | variable NANOSECONDS=1. |
| 544 | |
| 545 | #(CDT) 2016/08/03 17:16:25.510469 - Hi. |
| 546 | |
| 547 | SHOW_ELAPSED_TIME This will cause the elapsed time to be |
| 548 | included in the output. This is the |
| 549 | amount of time that has elapsed since the |
| 550 | last time this function was called. The |
| 551 | precision of the elapsed time field is |
| 552 | also affected by the value of the |
| 553 | NANOSECONDS environment variable. Example |
| 554 | of the output when environment variable |
| 555 | NANOSECONDS=0 and SHOW_ELAPSED_TIME=1. |
| 556 | |
| 557 | #(CDT) 2016/08/03 17:17:40 - 0 - Hi. |
| 558 | |
| 559 | Example of the output when environment variable NANOSECONDS=1 and |
| 560 | SHOW_ELAPSED_TIME=1. |
| 561 | |
| 562 | #(CDT) 2016/08/03 17:18:47.317339 - 0.000046 - Hi. |
| 563 | |
| 564 | Description of arguments. |
| 565 | buffer This will be appended to the formatted |
| 566 | time string. |
| 567 | """ |
| 568 | |
| 569 | global NANOSECONDS |
| 570 | global SHOW_ELAPSED_TIME |
| 571 | global sprint_time_last_seconds |
Michael Walsh | 4fea2cf | 2018-08-22 17:48:18 -0500 | [diff] [blame] | 572 | global last_seconds_ix |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 573 | |
| 574 | seconds = time.time() |
| 575 | loc_time = time.localtime(seconds) |
| 576 | nanoseconds = "%0.6f" % seconds |
| 577 | pos = nanoseconds.find(".") |
| 578 | nanoseconds = nanoseconds[pos:] |
| 579 | |
| 580 | time_string = time.strftime("#(%Z) %Y/%m/%d %H:%M:%S", loc_time) |
| 581 | if NANOSECONDS == "1": |
| 582 | time_string = time_string + nanoseconds |
| 583 | |
| 584 | if SHOW_ELAPSED_TIME == "1": |
| 585 | cur_time_seconds = seconds |
| 586 | math_string = "%9.9f" % cur_time_seconds + " - " + "%9.9f" % \ |
Michael Walsh | 4fea2cf | 2018-08-22 17:48:18 -0500 | [diff] [blame] | 587 | sprint_time_last_seconds[last_seconds_ix] |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 588 | elapsed_seconds = eval(math_string) |
| 589 | if NANOSECONDS == "1": |
| 590 | elapsed_seconds = "%11.6f" % elapsed_seconds |
| 591 | else: |
| 592 | elapsed_seconds = "%4i" % elapsed_seconds |
Michael Walsh | 4fea2cf | 2018-08-22 17:48:18 -0500 | [diff] [blame] | 593 | sprint_time_last_seconds[last_seconds_ix] = cur_time_seconds |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 594 | time_string = time_string + " - " + elapsed_seconds |
| 595 | |
| 596 | return time_string + " - " + buffer |
| 597 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 598 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 599 | def sprint_timen(buffer=""): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 600 | r""" |
| 601 | Append a line feed to the buffer, pass it to sprint_time and return the |
| 602 | result. |
| 603 | """ |
| 604 | |
| 605 | return sprint_time(buffer + "\n") |
| 606 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 607 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 608 | def sprint_error(buffer=""): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 609 | r""" |
| 610 | Return a standardized error string. This includes: |
| 611 | - A time stamp |
| 612 | - The "**ERROR**" string |
| 613 | - The caller's buffer string. |
| 614 | |
| 615 | Example: |
| 616 | |
| 617 | The following python code... |
| 618 | |
| 619 | print(sprint_error("Oops.\n")) |
| 620 | |
| 621 | Will result in the following type of output: |
| 622 | |
| 623 | #(CDT) 2016/08/03 17:12:05 - **ERROR** Oops. |
| 624 | |
| 625 | Description of arguments. |
| 626 | buffer This will be appended to the formatted |
| 627 | error string. |
| 628 | """ |
| 629 | |
| 630 | return sprint_time() + "**ERROR** " + buffer |
| 631 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 632 | |
Michael Walsh | 3f24827 | 2018-06-01 13:59:35 -0500 | [diff] [blame] | 633 | # Implement "constants" with functions. |
| 634 | def digit_length_in_bits(): |
| 635 | r""" |
| 636 | Return the digit length in bits. |
| 637 | """ |
| 638 | |
| 639 | return 4 |
| 640 | |
| 641 | |
| 642 | def word_length_in_digits(): |
| 643 | r""" |
| 644 | Return the word length in digits. |
| 645 | """ |
| 646 | |
| 647 | return 8 |
| 648 | |
| 649 | |
| 650 | def bit_length(number): |
| 651 | r""" |
| 652 | Return the bit length of the number. |
| 653 | |
| 654 | Description of argument(s): |
| 655 | number The number to be analyzed. |
| 656 | """ |
| 657 | |
| 658 | if number < 0: |
| 659 | # Convert negative numbers to positive and subtract one. The |
| 660 | # following example illustrates the reason for this: |
| 661 | # Consider a single nibble whose signed values can range from -8 to 7 |
| 662 | # (0x8 to 0x7). A value of 0x7 equals 0b0111. Therefore, its length |
| 663 | # in bits is 3. Since the negative bit (i.e. 0b1000) is not set, the |
| 664 | # value 7 clearly will fit in one nibble. With -8 = 0x8 = 0b1000, you |
| 665 | # have the smallest negative value that will fit. Note that it |
| 666 | # requires 3 bits of 0. So by converting a number value of -8 to a |
| 667 | # working_number of 7, this function can accurately calculate the |
| 668 | # number of bits and therefore nibbles required to represent the |
| 669 | # number in print. |
| 670 | working_number = abs(number) - 1 |
| 671 | else: |
| 672 | working_number = number |
| 673 | |
| 674 | # Handle the special case of the number 0. |
| 675 | if working_number == 0: |
| 676 | return 0 |
| 677 | |
| 678 | return len(bin(working_number)) - 2 |
| 679 | |
| 680 | |
| 681 | def get_req_num_hex_digits(number): |
| 682 | r""" |
| 683 | Return the required number of hex digits required to display the given |
| 684 | number. |
| 685 | |
| 686 | The returned value will always be rounded up to the nearest multiple of 8. |
| 687 | |
| 688 | Description of argument(s): |
| 689 | number The number to be analyzed. |
| 690 | """ |
| 691 | |
| 692 | if number < 0: |
| 693 | # Convert negative numbers to positive and subtract one. The |
| 694 | # following example illustrates the reason for this: |
| 695 | # Consider a single nibble whose signed values can range from -8 to 7 |
| 696 | # (0x8 to 0x7). A value of 0x7 equals 0b0111. Therefore, its length |
| 697 | # in bits is 3. Since the negative bit (i.e. 0b1000) is not set, the |
| 698 | # value 7 clearly will fit in one nibble. With -8 = 0x8 = 0b1000, you |
| 699 | # have the smallest negative value that will fit. Note that it |
| 700 | # requires 3 bits of 0. So by converting a number value of -8 to a |
| 701 | # working_number of 7, this function can accurately calculate the |
| 702 | # number of bits and therefore nibbles required to represent the |
| 703 | # number in print. |
| 704 | working_number = abs(number) - 1 |
| 705 | else: |
| 706 | working_number = number |
| 707 | |
| 708 | # Handle the special case of the number 0. |
| 709 | if working_number == 0: |
| 710 | return word_length_in_digits() |
| 711 | |
| 712 | num_length_in_bits = bit_length(working_number) |
| 713 | num_hex_digits, remainder = divmod(num_length_in_bits, |
| 714 | digit_length_in_bits()) |
| 715 | if remainder > 0: |
| 716 | # Example: the number 7 requires 3 bits. The divmod above produces, |
| 717 | # 0 with remainder of 3. So because we have a remainder, we increment |
| 718 | # num_hex_digits from 0 to 1. |
| 719 | num_hex_digits += 1 |
| 720 | |
| 721 | # Check to see whether the negative bit is set. This is the left-most |
| 722 | # bit in the highest order digit. |
| 723 | negative_mask = 2 ** (num_hex_digits * 4 - 1) |
| 724 | if working_number & negative_mask: |
| 725 | # If a number that is intended to be positive has its negative bit |
| 726 | # on, an additional digit will be required to represent it correctly |
| 727 | # in print. |
| 728 | num_hex_digits += 1 |
| 729 | |
| 730 | num_words, remainder = divmod(num_hex_digits, word_length_in_digits()) |
| 731 | if remainder > 0 or num_words == 0: |
| 732 | num_words += 1 |
| 733 | |
| 734 | # Round up to the next word length in digits. |
| 735 | return num_words * word_length_in_digits() |
| 736 | |
| 737 | |
| 738 | def dft_num_hex_digits(): |
| 739 | r""" |
| 740 | Return the default number of hex digits to be used to represent a hex |
| 741 | number in print. |
| 742 | |
| 743 | The value returned is a function of sys.maxsize. |
| 744 | """ |
| 745 | |
| 746 | global _gen_print_dft_num_hex_digits_ |
| 747 | try: |
| 748 | return _gen_print_dft_num_hex_digits_ |
| 749 | except NameError: |
| 750 | _gen_print_dft_num_hex_digits_ = get_req_num_hex_digits(sys.maxsize) |
| 751 | return _gen_print_dft_num_hex_digits_ |
| 752 | |
| 753 | |
Michael Walsh | 8646d96 | 2019-01-21 14:36:13 -0600 | [diff] [blame] | 754 | def is_dict(var_value): |
| 755 | r""" |
| 756 | Return 1 if var_value is a type of dictionary and 0 if it is not. |
| 757 | """ |
| 758 | |
| 759 | type_is_dict = 0 |
| 760 | if isinstance(var_value, dict): |
| 761 | type_is_dict = 1 |
| 762 | try: |
| 763 | if isinstance(var_value, collections.OrderedDict): |
| 764 | type_is_dict = 1 |
| 765 | except AttributeError: |
| 766 | pass |
| 767 | try: |
| 768 | if isinstance(var_value, DotDict): |
| 769 | type_is_dict = 1 |
| 770 | except NameError: |
| 771 | pass |
| 772 | try: |
| 773 | if isinstance(var_value, NormalizedDict): |
| 774 | type_is_dict = 1 |
| 775 | except NameError: |
| 776 | pass |
| 777 | return type_is_dict |
| 778 | |
| 779 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 780 | def sprint_varx(var_name, |
| 781 | var_value, |
| 782 | hex=0, |
| 783 | loc_col1_indent=col1_indent, |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 784 | loc_col1_width=col1_width, |
Michael Walsh | d286903 | 2018-03-22 16:12:11 -0500 | [diff] [blame] | 785 | trailing_char="\n", |
| 786 | key_list=None): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 787 | r""" |
| 788 | Print the var name/value passed to it. If the caller lets loc_col1_width |
| 789 | default, the printing lines up nicely with output generated by the |
| 790 | print_time functions. |
| 791 | |
| 792 | Note that the sprint_var function (defined below) can be used to call this |
| 793 | function so that the programmer does not need to pass the var_name. |
| 794 | sprint_var will figure out the var_name. The sprint_var function is the |
| 795 | one that would normally be used by the general user. |
| 796 | |
| 797 | For example, the following python code: |
| 798 | |
| 799 | first_name = "Mike" |
| 800 | print_time("Doing this...\n") |
| 801 | print_varx("first_name", first_name) |
| 802 | print_time("Doing that...\n") |
| 803 | |
| 804 | Will generate output like this: |
| 805 | |
| 806 | #(CDT) 2016/08/10 17:34:42.847374 - 0.001285 - Doing this... |
| 807 | first_name: Mike |
| 808 | #(CDT) 2016/08/10 17:34:42.847510 - 0.000136 - Doing that... |
| 809 | |
| 810 | This function recognizes several complex types of data such as dict, list |
| 811 | or tuple. |
| 812 | |
| 813 | For example, the following python code: |
| 814 | |
| 815 | my_dict = dict(one=1, two=2, three=3) |
| 816 | print_var(my_dict) |
| 817 | |
| 818 | Will generate the following output: |
| 819 | |
| 820 | my_dict: |
| 821 | my_dict[three]: 3 |
| 822 | my_dict[two]: 2 |
| 823 | my_dict[one]: 1 |
| 824 | |
| 825 | Description of arguments. |
| 826 | var_name The name of the variable to be printed. |
| 827 | var_value The value of the variable to be printed. |
| 828 | hex This indicates that the value should be |
| 829 | printed in hex format. It is the user's |
| 830 | responsibility to ensure that a var_value |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 831 | contains a valid hex number. For string |
| 832 | var_values, this will be interpreted as |
| 833 | show_blanks which means that blank values |
Michael Walsh | d995cb0 | 2017-02-07 14:46:01 -0600 | [diff] [blame] | 834 | will be printed as "<blank>". For dict |
| 835 | var_values, this will be interpreted as |
| 836 | terse format where keys are not repeated |
| 837 | in the output. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 838 | loc_col1_indent The number of spaces to indent the output. |
| 839 | loc_col1_width The width of the output column containing |
| 840 | the variable name. The default value of |
| 841 | this is adjusted so that the var_value |
| 842 | lines up with text printed via the |
| 843 | print_time function. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 844 | trailing_char The character to be used at the end of the |
| 845 | returned string. The default value is a |
| 846 | line feed. |
Michael Walsh | d286903 | 2018-03-22 16:12:11 -0500 | [diff] [blame] | 847 | key_list A list of which dictionary keys should be |
| 848 | printed. All others keys will be skipped. |
| 849 | Each value in key_list will be regarded |
| 850 | as a regular expression and it will be |
| 851 | regarded as anchored to the beginning and |
| 852 | ends of the dictionary key being |
| 853 | referenced. For example if key_list is |
| 854 | ["one", "two"], the resulting regex used |
| 855 | will be "^one|two$", i.e. only keys "one" |
| 856 | and "two" from the var_value dictionary |
| 857 | will be printed. As another example, if |
| 858 | the caller were to specify a key_list of |
| 859 | ["one.*"], then only dictionary keys whose |
| 860 | names begin with "one" will be printed. |
| 861 | Note: This argument pertains only to |
| 862 | var_values which are dictionaries. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 863 | """ |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 864 | |
| 865 | # Determine the type |
Michael Walsh | 37762f9 | 2018-08-07 14:59:18 -0500 | [diff] [blame] | 866 | try: |
| 867 | int_types = (int, long) |
| 868 | except NameError: |
| 869 | int_types = (int,) |
| 870 | try: |
| 871 | string_types = (str, unicode) |
| 872 | except NameError: |
George Keishing | 36efbc0 | 2018-12-12 10:18:23 -0600 | [diff] [blame] | 873 | string_types = (bytes, str) |
Michael Walsh | 37762f9 | 2018-08-07 14:59:18 -0500 | [diff] [blame] | 874 | simple_types = int_types + string_types + (float, bool) |
| 875 | if type(var_value) in simple_types \ |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 876 | or var_value is None: |
| 877 | # The data type is simple in the sense that it has no subordinate |
| 878 | # parts. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 879 | # Adjust loc_col1_width. |
| 880 | loc_col1_width = loc_col1_width - loc_col1_indent |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 881 | # See if the user wants the output in hex format. |
| 882 | if hex: |
Michael Walsh | 37762f9 | 2018-08-07 14:59:18 -0500 | [diff] [blame] | 883 | if type(var_value) not in int_types: |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 884 | value_format = "%s" |
Michael Walsh | 2795edc | 2016-12-13 16:00:33 -0600 | [diff] [blame] | 885 | if var_value == "": |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 886 | var_value = "<blank>" |
| 887 | else: |
Michael Walsh | 3f24827 | 2018-06-01 13:59:35 -0500 | [diff] [blame] | 888 | num_hex_digits = max(dft_num_hex_digits(), |
| 889 | get_req_num_hex_digits(var_value)) |
| 890 | # Convert a negative number to its positive twos complement |
| 891 | # for proper printing. For example, instead of printing -1 as |
| 892 | # "0x-000000000000001" it will be printed as |
| 893 | # "0xffffffffffffffff". |
| 894 | var_value = var_value & (2 ** (num_hex_digits * 4) - 1) |
| 895 | value_format = "0x%0" + str(num_hex_digits) + "x" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 896 | else: |
| 897 | value_format = "%s" |
| 898 | format_string = "%" + str(loc_col1_indent) + "s%-" \ |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 899 | + str(loc_col1_width) + "s" + value_format + trailing_char |
Michael Walsh | 3383e65 | 2017-09-01 17:10:59 -0500 | [diff] [blame] | 900 | if value_format == "0x%08x": |
| 901 | return format_string % ("", str(var_name) + ":", |
| 902 | var_value & 0xffffffff) |
| 903 | else: |
| 904 | return format_string % ("", str(var_name) + ":", var_value) |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 905 | elif isinstance(var_value, type): |
Michael Walsh | 20a87ab | 2017-06-30 17:00:30 -0500 | [diff] [blame] | 906 | return sprint_varx(var_name, str(var_value).split("'")[1], hex, |
Michael Walsh | d286903 | 2018-03-22 16:12:11 -0500 | [diff] [blame] | 907 | loc_col1_indent, loc_col1_width, trailing_char, |
| 908 | key_list) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 909 | else: |
| 910 | # The data type is complex in the sense that it has subordinate parts. |
| 911 | format_string = "%" + str(loc_col1_indent) + "s%s\n" |
| 912 | buffer = format_string % ("", var_name + ":") |
| 913 | loc_col1_indent += 2 |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 914 | try: |
| 915 | length = len(var_value) |
| 916 | except TypeError: |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 917 | length = 0 |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 918 | ix = 0 |
| 919 | loc_trailing_char = "\n" |
Michael Walsh | 8646d96 | 2019-01-21 14:36:13 -0600 | [diff] [blame] | 920 | if is_dict(var_value): |
Michael Walsh | 37762f9 | 2018-08-07 14:59:18 -0500 | [diff] [blame] | 921 | for key, value in var_value.items(): |
Michael Walsh | d286903 | 2018-03-22 16:12:11 -0500 | [diff] [blame] | 922 | if key_list is not None: |
| 923 | key_list_regex = "^" + "|".join(key_list) + "$" |
| 924 | if not re.match(key_list_regex, key): |
| 925 | continue |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 926 | ix += 1 |
| 927 | if ix == length: |
| 928 | loc_trailing_char = trailing_char |
Michael Walsh | d995cb0 | 2017-02-07 14:46:01 -0600 | [diff] [blame] | 929 | if hex: |
Michael Walsh | 0f2ea5f | 2017-02-20 15:55:00 -0600 | [diff] [blame] | 930 | # Since hex is being used as a format type, we want it |
| 931 | # turned off when processing integer dictionary values so |
| 932 | # it is not interpreted as a hex indicator. |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 933 | loc_hex = not (isinstance(value, int)) |
Michael Walsh | f7b8a00 | 2017-08-29 10:38:39 -0500 | [diff] [blame] | 934 | buffer += sprint_varx("[" + key + "]", value, |
Michael Walsh | 0f2ea5f | 2017-02-20 15:55:00 -0600 | [diff] [blame] | 935 | loc_hex, loc_col1_indent, |
| 936 | loc_col1_width, |
Michael Walsh | d286903 | 2018-03-22 16:12:11 -0500 | [diff] [blame] | 937 | loc_trailing_char, |
| 938 | key_list) |
Michael Walsh | d995cb0 | 2017-02-07 14:46:01 -0600 | [diff] [blame] | 939 | else: |
Michael Walsh | 1173a52 | 2018-05-21 17:24:51 -0500 | [diff] [blame] | 940 | buffer += sprint_varx(var_name + "[" + str(key) + "]", |
| 941 | value, hex, loc_col1_indent, |
| 942 | loc_col1_width, loc_trailing_char, |
| 943 | key_list) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 944 | elif type(var_value) in (list, tuple, set): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 945 | for key, value in enumerate(var_value): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 946 | ix += 1 |
| 947 | if ix == length: |
| 948 | loc_trailing_char = trailing_char |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 949 | buffer += sprint_varx(var_name + "[" + str(key) + "]", value, |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 950 | hex, loc_col1_indent, loc_col1_width, |
Michael Walsh | d286903 | 2018-03-22 16:12:11 -0500 | [diff] [blame] | 951 | loc_trailing_char, key_list) |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 952 | elif isinstance(var_value, argparse.Namespace): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 953 | for key in var_value.__dict__: |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 954 | ix += 1 |
| 955 | if ix == length: |
| 956 | loc_trailing_char = trailing_char |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 957 | cmd_buf = "buffer += sprint_varx(var_name + \".\" + str(key)" \ |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 958 | + ", var_value." + key + ", hex, loc_col1_indent," \ |
Michael Walsh | d286903 | 2018-03-22 16:12:11 -0500 | [diff] [blame] | 959 | + " loc_col1_width, loc_trailing_char, key_list)" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 960 | exec(cmd_buf) |
| 961 | else: |
| 962 | var_type = type(var_value).__name__ |
| 963 | func_name = sys._getframe().f_code.co_name |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 964 | var_value = "<" + var_type + " type not supported by " + \ |
| 965 | func_name + "()>" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 966 | value_format = "%s" |
| 967 | loc_col1_indent -= 2 |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 968 | # Adjust loc_col1_width. |
| 969 | loc_col1_width = loc_col1_width - loc_col1_indent |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 970 | format_string = "%" + str(loc_col1_indent) + "s%-" \ |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 971 | + str(loc_col1_width) + "s" + value_format + trailing_char |
Michael Walsh | 0f2ea5f | 2017-02-20 15:55:00 -0600 | [diff] [blame] | 972 | return format_string % ("", str(var_name) + ":", var_value) |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 973 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 974 | return buffer |
| 975 | |
| 976 | return "" |
| 977 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 978 | |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 979 | def sprint_var(var_value, |
| 980 | hex=0, |
| 981 | loc_col1_indent=col1_indent, |
| 982 | loc_col1_width=col1_width, |
Michael Walsh | d286903 | 2018-03-22 16:12:11 -0500 | [diff] [blame] | 983 | trailing_char="\n", |
| 984 | key_list=None): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 985 | r""" |
| 986 | Figure out the name of the first argument for you and then call |
| 987 | sprint_varx with it. Therefore, the following 2 calls are equivalent: |
| 988 | sprint_varx("var1", var1) |
| 989 | sprint_var(var1) |
| 990 | """ |
| 991 | |
| 992 | # Get the name of the first variable passed to this function. |
| 993 | stack_frame = 2 |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 994 | caller_func_name = sprint_func_name(2) |
| 995 | if caller_func_name.endswith("print_var"): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 996 | stack_frame += 1 |
| 997 | var_name = get_arg_name(None, 1, stack_frame) |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 998 | return sprint_varx(var_name, var_value=var_value, hex=hex, |
| 999 | loc_col1_indent=loc_col1_indent, |
| 1000 | loc_col1_width=loc_col1_width, |
Michael Walsh | d286903 | 2018-03-22 16:12:11 -0500 | [diff] [blame] | 1001 | trailing_char=trailing_char, |
| 1002 | key_list=key_list) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1003 | |
| 1004 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 1005 | def sprint_vars(*args): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 1006 | r""" |
| 1007 | Sprint the values of one or more variables. |
| 1008 | |
| 1009 | Description of args: |
| 1010 | args: |
| 1011 | If the first argument is an integer, it will be interpreted to be the |
| 1012 | "indent" value. |
| 1013 | If the second argument is an integer, it will be interpreted to be the |
| 1014 | "col1_width" value. |
| 1015 | If the third argument is an integer, it will be interpreted to be the |
| 1016 | "hex" value. |
| 1017 | All remaining parms are considered variable names which are to be |
| 1018 | sprinted. |
| 1019 | """ |
| 1020 | |
| 1021 | if len(args) == 0: |
| 1022 | return |
| 1023 | |
| 1024 | # Get the name of the first variable passed to this function. |
| 1025 | stack_frame = 2 |
| 1026 | caller_func_name = sprint_func_name(2) |
| 1027 | if caller_func_name.endswith("print_vars"): |
| 1028 | stack_frame += 1 |
| 1029 | |
| 1030 | parm_num = 1 |
| 1031 | |
| 1032 | # Create list from args (which is a tuple) so that it can be modified. |
| 1033 | args_list = list(args) |
| 1034 | |
| 1035 | var_name = get_arg_name(None, parm_num, stack_frame) |
| 1036 | # See if parm 1 is to be interpreted as "indent". |
| 1037 | try: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 1038 | if isinstance(int(var_name), int): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 1039 | indent = int(var_name) |
| 1040 | args_list.pop(0) |
| 1041 | parm_num += 1 |
| 1042 | except ValueError: |
| 1043 | indent = 0 |
| 1044 | |
| 1045 | var_name = get_arg_name(None, parm_num, stack_frame) |
| 1046 | # See if parm 1 is to be interpreted as "col1_width". |
| 1047 | try: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 1048 | if isinstance(int(var_name), int): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 1049 | loc_col1_width = int(var_name) |
| 1050 | args_list.pop(0) |
| 1051 | parm_num += 1 |
| 1052 | except ValueError: |
| 1053 | loc_col1_width = col1_width |
| 1054 | |
| 1055 | var_name = get_arg_name(None, parm_num, stack_frame) |
| 1056 | # See if parm 1 is to be interpreted as "hex". |
| 1057 | try: |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 1058 | if isinstance(int(var_name), int): |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 1059 | hex = int(var_name) |
| 1060 | args_list.pop(0) |
| 1061 | parm_num += 1 |
| 1062 | except ValueError: |
| 1063 | hex = 0 |
| 1064 | |
| 1065 | buffer = "" |
| 1066 | for var_value in args_list: |
| 1067 | var_name = get_arg_name(None, parm_num, stack_frame) |
| 1068 | buffer += sprint_varx(var_name, var_value, hex, indent, loc_col1_width) |
| 1069 | parm_num += 1 |
| 1070 | |
| 1071 | return buffer |
| 1072 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 1073 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1074 | def sprint_dashes(indent=col1_indent, |
| 1075 | width=80, |
| 1076 | line_feed=1, |
| 1077 | char="-"): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1078 | r""" |
| 1079 | Return a string of dashes to the caller. |
| 1080 | |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1081 | Description of arguments: |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1082 | indent The number of characters to indent the |
| 1083 | output. |
| 1084 | width The width of the string of dashes. |
| 1085 | line_feed Indicates whether the output should end |
| 1086 | with a line feed. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1087 | char The character to be repeated in the output |
| 1088 | string. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1089 | """ |
| 1090 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1091 | width = int(width) |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 1092 | buffer = " " * int(indent) + char * width |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1093 | if line_feed: |
| 1094 | buffer += "\n" |
| 1095 | |
| 1096 | return buffer |
| 1097 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1098 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1099 | def sindent(text="", |
| 1100 | indent=0): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1101 | r""" |
| 1102 | Pre-pend the specified number of characters to the text string (i.e. |
| 1103 | indent it) and return it. |
| 1104 | |
| 1105 | Description of arguments: |
| 1106 | text The string to be indented. |
| 1107 | indent The number of characters to indent the |
| 1108 | string. |
| 1109 | """ |
| 1110 | |
| 1111 | format_string = "%" + str(indent) + "s%s" |
| 1112 | buffer = format_string % ("", text) |
| 1113 | |
| 1114 | return buffer |
| 1115 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1116 | |
Michael Walsh | 662e13b | 2019-03-01 15:54:08 -0600 | [diff] [blame] | 1117 | func_line_style_std = None |
| 1118 | func_line_style_short = 1 |
| 1119 | |
| 1120 | |
| 1121 | def sprint_func_line(stack_frame, style=None): |
Michael Walsh | 47aa2a4 | 2018-12-10 15:06:02 -0600 | [diff] [blame] | 1122 | r""" |
| 1123 | For the given stack_frame, return a formatted string containing the |
| 1124 | function name and all its arguments. |
| 1125 | |
| 1126 | Example: |
| 1127 | |
| 1128 | func1(last_name = 'walsh', first_name = 'mikey') |
| 1129 | |
| 1130 | Description of argument(s): |
| 1131 | stack_frame A stack frame (such as is returned by |
| 1132 | inspect.stack()). |
Michael Walsh | 662e13b | 2019-03-01 15:54:08 -0600 | [diff] [blame] | 1133 | style Indicates the style or formatting of the |
| 1134 | result string. Acceptable values are |
| 1135 | shown above. |
| 1136 | |
| 1137 | Description of styles: |
| 1138 | func_line_style_std The standard formatting. |
| 1139 | func_line_style_short 1) The self parm (associated with methods) |
| 1140 | will be dropped. 2) The args and kwargs |
| 1141 | values will be treated as special. In |
| 1142 | both cases the arg name ('args' or |
| 1143 | 'kwargs') will be dropped and only the |
| 1144 | values will be shown. |
Michael Walsh | 47aa2a4 | 2018-12-10 15:06:02 -0600 | [diff] [blame] | 1145 | """ |
| 1146 | |
| 1147 | func_name = str(stack_frame[3]) |
| 1148 | if func_name == "?": |
| 1149 | # "?" is the name used when code is not in a function. |
| 1150 | func_name = "(none)" |
| 1151 | |
| 1152 | if func_name == "<module>": |
| 1153 | # If the func_name is the "main" program, we simply get the command |
| 1154 | # line call string. |
| 1155 | func_and_args = ' '.join(sys.argv) |
| 1156 | else: |
| 1157 | # Get the program arguments. |
| 1158 | (args, varargs, keywords, locals) =\ |
| 1159 | inspect.getargvalues(stack_frame[0]) |
| 1160 | |
| 1161 | args_list = [] |
| 1162 | for arg_name in filter(None, args + [varargs, keywords]): |
| 1163 | # Get the arg value from frame locals. |
| 1164 | arg_value = locals[arg_name] |
| 1165 | if arg_name == 'self': |
Michael Walsh | 662e13b | 2019-03-01 15:54:08 -0600 | [diff] [blame] | 1166 | if style == func_line_style_short: |
| 1167 | continue |
Michael Walsh | 47aa2a4 | 2018-12-10 15:06:02 -0600 | [diff] [blame] | 1168 | # Manipulations to improve output for class methods. |
| 1169 | func_name = arg_value.__class__.__name__ + "." + func_name |
| 1170 | args_list.append(arg_name + " = <self>") |
Michael Walsh | 662e13b | 2019-03-01 15:54:08 -0600 | [diff] [blame] | 1171 | elif (style == func_line_style_short |
| 1172 | and arg_name == 'args' |
| 1173 | and type(arg_value) in (list, tuple)): |
| 1174 | if len(arg_value) == 0: |
| 1175 | continue |
| 1176 | args_list.append(repr(', '.join(arg_value))) |
| 1177 | elif (style == func_line_style_short |
| 1178 | and arg_name == 'kwargs' |
| 1179 | and type(arg_value) is dict): |
| 1180 | for key, value in arg_value.items(): |
| 1181 | args_list.append(key + "=" + repr(value)) |
Michael Walsh | 47aa2a4 | 2018-12-10 15:06:02 -0600 | [diff] [blame] | 1182 | else: |
| 1183 | args_list.append(arg_name + " = " + repr(arg_value)) |
| 1184 | args_str = "(" + ', '.join(map(str, args_list)) + ")" |
| 1185 | |
| 1186 | # Now we need to print this in a nicely-wrapped way. |
| 1187 | func_and_args = func_name + args_str |
| 1188 | |
| 1189 | return func_and_args |
| 1190 | |
| 1191 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1192 | def sprint_call_stack(indent=0, |
Michael Walsh | 662e13b | 2019-03-01 15:54:08 -0600 | [diff] [blame] | 1193 | stack_frame_ix=0, |
| 1194 | style=None): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1195 | r""" |
| 1196 | Return a call stack report for the given point in the program with line |
| 1197 | numbers, function names and function parameters and arguments. |
| 1198 | |
| 1199 | Sample output: |
| 1200 | |
| 1201 | ------------------------------------------------------------------------- |
| 1202 | Python function call stack |
| 1203 | |
| 1204 | Line # Function name and arguments |
| 1205 | ------ ------------------------------------------------------------------ |
Michael Walsh | 47aa2a4 | 2018-12-10 15:06:02 -0600 | [diff] [blame] | 1206 | 424 sprint_call_stack() |
| 1207 | 4 print_call_stack() |
| 1208 | 31 func1(last_name = 'walsh', first_name = 'mikey') |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1209 | 59 /tmp/scr5.py |
| 1210 | ------------------------------------------------------------------------- |
| 1211 | |
| 1212 | Description of arguments: |
| 1213 | indent The number of characters to indent each |
| 1214 | line of output. |
| 1215 | stack_frame_ix The index of the first stack frame which |
| 1216 | is to be returned. |
Michael Walsh | 662e13b | 2019-03-01 15:54:08 -0600 | [diff] [blame] | 1217 | style See the sprint_line_func prolog above for |
| 1218 | details. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1219 | """ |
| 1220 | |
| 1221 | buffer = "" |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1222 | buffer += sprint_dashes(indent) |
| 1223 | buffer += sindent("Python function call stack\n\n", indent) |
| 1224 | buffer += sindent("Line # Function name and arguments\n", indent) |
| 1225 | buffer += sprint_dashes(indent, 6, 0) + " " + sprint_dashes(0, 73) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1226 | |
| 1227 | # Grab the current program stack. |
Michael Walsh | 6f0362c | 2019-03-25 14:05:14 -0500 | [diff] [blame] | 1228 | work_around_inspect_stack_cwd_failure() |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1229 | current_stack = inspect.stack() |
| 1230 | |
| 1231 | # Process each frame in turn. |
| 1232 | format_string = "%6s %s\n" |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1233 | ix = 0 |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1234 | for stack_frame in current_stack: |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1235 | if ix < stack_frame_ix: |
| 1236 | ix += 1 |
| 1237 | continue |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 1238 | # I want the line number shown to be the line where you find the line |
| 1239 | # shown. |
| 1240 | try: |
| 1241 | line_num = str(current_stack[ix + 1][2]) |
| 1242 | except IndexError: |
| 1243 | line_num = "" |
Michael Walsh | 662e13b | 2019-03-01 15:54:08 -0600 | [diff] [blame] | 1244 | func_and_args = sprint_func_line(stack_frame, style=style) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1245 | |
Michael Walsh | 23e7f49 | 2017-01-10 11:34:47 -0600 | [diff] [blame] | 1246 | buffer += sindent(format_string % (line_num, func_and_args), indent) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1247 | ix += 1 |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1248 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1249 | buffer += sprint_dashes(indent) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1250 | |
| 1251 | return buffer |
| 1252 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1253 | |
Michael Walsh | 662e13b | 2019-03-01 15:54:08 -0600 | [diff] [blame] | 1254 | def sprint_executing(stack_frame_ix=None, style=None): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1255 | r""" |
| 1256 | Print a line indicating what function is executing and with what parameter |
| 1257 | values. This is useful for debugging. |
| 1258 | |
| 1259 | Sample output: |
| 1260 | |
Michael Walsh | 47aa2a4 | 2018-12-10 15:06:02 -0600 | [diff] [blame] | 1261 | #(CDT) 2016/08/25 17:54:27 - Executing: func1(x = 1) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1262 | |
| 1263 | Description of arguments: |
| 1264 | stack_frame_ix The index of the stack frame whose |
| 1265 | function info should be returned. If the |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1266 | caller does not specify a value, this |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1267 | function will set the value to 1 which is |
| 1268 | the index of the caller's stack frame. If |
| 1269 | the caller is the wrapper function |
| 1270 | "print_executing", this function will bump |
| 1271 | it up by 1. |
Michael Walsh | 662e13b | 2019-03-01 15:54:08 -0600 | [diff] [blame] | 1272 | style See the sprint_line_func prolog above for |
| 1273 | details. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1274 | """ |
| 1275 | |
| 1276 | # If user wants default stack_frame_ix. |
| 1277 | if stack_frame_ix is None: |
| 1278 | func_name = sys._getframe().f_code.co_name |
| 1279 | caller_func_name = sys._getframe(1).f_code.co_name |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1280 | if caller_func_name.endswith(func_name[1:]): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1281 | stack_frame_ix = 2 |
| 1282 | else: |
| 1283 | stack_frame_ix = 1 |
| 1284 | |
Michael Walsh | 6f0362c | 2019-03-25 14:05:14 -0500 | [diff] [blame] | 1285 | work_around_inspect_stack_cwd_failure() |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1286 | stack_frame = inspect.stack()[stack_frame_ix] |
| 1287 | |
Michael Walsh | 662e13b | 2019-03-01 15:54:08 -0600 | [diff] [blame] | 1288 | func_and_args = sprint_func_line(stack_frame, style) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1289 | |
| 1290 | return sprint_time() + "Executing: " + func_and_args + "\n" |
| 1291 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1292 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1293 | def sprint_pgm_header(indent=0, |
| 1294 | linefeed=1): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1295 | r""" |
| 1296 | Return a standardized header that programs should print at the beginning |
| 1297 | of the run. It includes useful information like command line, pid, |
| 1298 | userid, program parameters, etc. |
| 1299 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1300 | Description of arguments: |
| 1301 | indent The number of characters to indent each |
| 1302 | line of output. |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1303 | linefeed Indicates whether a line feed be included |
| 1304 | at the beginning and end of the report. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1305 | """ |
| 1306 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1307 | loc_col1_width = col1_width + indent |
| 1308 | |
| 1309 | buffer = "" |
| 1310 | if linefeed: |
| 1311 | buffer = "\n" |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1312 | |
Michael Walsh | db6e68a | 2017-05-23 17:55:31 -0500 | [diff] [blame] | 1313 | if robot_env: |
| 1314 | suite_name = BuiltIn().get_variable_value("${suite_name}") |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 1315 | buffer += sindent(sprint_time("Running test suite \"" + suite_name |
| 1316 | + "\".\n"), indent) |
Michael Walsh | db6e68a | 2017-05-23 17:55:31 -0500 | [diff] [blame] | 1317 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1318 | buffer += sindent(sprint_time() + "Running " + pgm_name + ".\n", indent) |
| 1319 | buffer += sindent(sprint_time() + "Program parameter values, etc.:\n\n", |
| 1320 | indent) |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1321 | buffer += sprint_varx("command_line", ' '.join(sys.argv), 0, indent, |
| 1322 | loc_col1_width) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1323 | # We want the output to show a customized name for the pid and pgid but |
| 1324 | # we want it to look like a valid variable name. Therefore, we'll use |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1325 | # pgm_name_var_name which was set when this module was imported. |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1326 | buffer += sprint_varx(pgm_name_var_name + "_pid", os.getpid(), 0, indent, |
| 1327 | loc_col1_width) |
| 1328 | buffer += sprint_varx(pgm_name_var_name + "_pgid", os.getpgrp(), 0, indent, |
| 1329 | loc_col1_width) |
Michael Walsh | 86de0d2 | 2016-12-05 10:13:15 -0600 | [diff] [blame] | 1330 | userid_num = str(os.geteuid()) |
| 1331 | try: |
| 1332 | username = os.getlogin() |
| 1333 | except OSError: |
| 1334 | if userid_num == "0": |
| 1335 | username = "root" |
| 1336 | else: |
| 1337 | username = "?" |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 1338 | buffer += sprint_varx("uid", userid_num + " (" + username |
| 1339 | + ")", 0, indent, loc_col1_width) |
| 1340 | buffer += sprint_varx("gid", str(os.getgid()) + " (" |
| 1341 | + str(grp.getgrgid(os.getgid()).gr_name) + ")", 0, |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1342 | indent, loc_col1_width) |
| 1343 | buffer += sprint_varx("host_name", socket.gethostname(), 0, indent, |
| 1344 | loc_col1_width) |
Michael Walsh | 86de0d2 | 2016-12-05 10:13:15 -0600 | [diff] [blame] | 1345 | try: |
| 1346 | DISPLAY = os.environ['DISPLAY'] |
| 1347 | except KeyError: |
| 1348 | DISPLAY = "" |
| 1349 | buffer += sprint_varx("DISPLAY", DISPLAY, 0, indent, |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1350 | loc_col1_width) |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1351 | # I want to add code to print caller's parms. |
| 1352 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1353 | # __builtin__.arg_obj is created by the get_arg module function, |
| 1354 | # gen_get_options. |
| 1355 | try: |
| 1356 | buffer += ga.sprint_args(__builtin__.arg_obj, indent) |
| 1357 | except AttributeError: |
| 1358 | pass |
| 1359 | |
Michael Walsh | db6e68a | 2017-05-23 17:55:31 -0500 | [diff] [blame] | 1360 | if robot_env: |
| 1361 | # Get value of global parm_list. |
| 1362 | parm_list = BuiltIn().get_variable_value("${parm_list}") |
| 1363 | |
| 1364 | for parm in parm_list: |
| 1365 | parm_value = BuiltIn().get_variable_value("${" + parm + "}") |
| 1366 | buffer += sprint_varx(parm, parm_value, 0, indent, loc_col1_width) |
| 1367 | |
| 1368 | # Setting global program_pid. |
| 1369 | BuiltIn().set_global_variable("${program_pid}", os.getpid()) |
| 1370 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1371 | if linefeed: |
| 1372 | buffer += "\n" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1373 | |
| 1374 | return buffer |
| 1375 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1376 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1377 | def sprint_error_report(error_text="\n", |
Michael Walsh | db6e68a | 2017-05-23 17:55:31 -0500 | [diff] [blame] | 1378 | indent=2, |
| 1379 | format=None): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1380 | r""" |
| 1381 | Return a string with a standardized report which includes the caller's |
| 1382 | error text, the call stack and the program header. |
| 1383 | |
| 1384 | Description of args: |
| 1385 | error_text The error text to be included in the |
| 1386 | report. The caller should include any |
| 1387 | needed linefeeds. |
| 1388 | indent The number of characters to indent each |
| 1389 | line of output. |
Michael Walsh | db6e68a | 2017-05-23 17:55:31 -0500 | [diff] [blame] | 1390 | format Long or short format. Long includes |
| 1391 | extras like lines of dashes, call stack, |
| 1392 | etc. |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1393 | """ |
| 1394 | |
Michael Walsh | db6e68a | 2017-05-23 17:55:31 -0500 | [diff] [blame] | 1395 | # Process input. |
| 1396 | indent = int(indent) |
| 1397 | if format is None: |
| 1398 | if robot_env: |
| 1399 | format = 'short' |
| 1400 | else: |
| 1401 | format = 'long' |
| 1402 | error_text = error_text.rstrip('\n') + '\n' |
| 1403 | |
| 1404 | if format == 'short': |
| 1405 | return sprint_error(error_text) |
| 1406 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1407 | buffer = "" |
| 1408 | buffer += sprint_dashes(width=120, char="=") |
| 1409 | buffer += sprint_error(error_text) |
| 1410 | buffer += "\n" |
| 1411 | # Calling sprint_call_stack with stack_frame_ix of 0 causes it to show |
| 1412 | # itself and this function in the call stack. This is not helpful to a |
| 1413 | # debugger and is therefore clutter. We will adjust the stack_frame_ix to |
| 1414 | # hide that information. |
Michael Walsh | 9c75f67 | 2017-09-12 17:11:35 -0500 | [diff] [blame] | 1415 | stack_frame_ix = 1 |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1416 | caller_func_name = sprint_func_name(2) |
| 1417 | if caller_func_name.endswith("print_error_report"): |
| 1418 | stack_frame_ix += 1 |
Michael Walsh | 7bfa9ab | 2018-11-16 15:24:26 -0600 | [diff] [blame] | 1419 | buffer += sprint_call_stack(indent, stack_frame_ix) |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1420 | buffer += sprint_pgm_header(indent) |
| 1421 | buffer += sprint_dashes(width=120, char="=") |
| 1422 | |
| 1423 | return buffer |
| 1424 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1425 | |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 1426 | def sprint_issuing(cmd_buf, |
| 1427 | test_mode=0): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1428 | r""" |
| 1429 | Return a line indicating a command that the program is about to execute. |
| 1430 | |
| 1431 | Sample output for a cmd_buf of "ls" |
| 1432 | |
| 1433 | #(CDT) 2016/08/25 17:57:36 - Issuing: ls |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1434 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1435 | Description of args: |
| 1436 | cmd_buf The command to be executed by caller. |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1437 | test_mode With test_mode set, your output will look |
| 1438 | like this: |
| 1439 | |
| 1440 | #(CDT) 2016/08/25 17:57:36 - (test_mode) Issuing: ls |
| 1441 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1442 | """ |
| 1443 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1444 | buffer = sprint_time() |
| 1445 | if test_mode: |
| 1446 | buffer += "(test_mode) " |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1447 | if type(cmd_buf) is list: |
| 1448 | # Assume this is a robot command in the form of a list. |
| 1449 | cmd_buf = ' '.join([str(element) for element in cmd_buf]) |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1450 | buffer += "Issuing: " + cmd_buf + "\n" |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1451 | |
| 1452 | return buffer |
| 1453 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1454 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1455 | def sprint_pgm_footer(): |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1456 | r""" |
| 1457 | Return a standardized footer that programs should print at the end of the |
| 1458 | program run. It includes useful information like total run time, etc. |
| 1459 | """ |
| 1460 | |
| 1461 | buffer = "\n" + sprint_time() + "Finished running " + pgm_name + ".\n\n" |
| 1462 | |
| 1463 | total_time = time.time() - start_time |
| 1464 | total_time_string = "%0.6f" % total_time |
| 1465 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1466 | buffer += sprint_varx(pgm_name_var_name + "_runtime", total_time_string) |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1467 | buffer += "\n" |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1468 | |
| 1469 | return buffer |
| 1470 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1471 | |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1472 | def sprint(buffer=""): |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1473 | r""" |
| 1474 | Simply return the user's buffer. This function is used by the qprint and |
| 1475 | dprint functions defined dynamically below, i.e. it would not normally be |
| 1476 | called for general use. |
| 1477 | |
| 1478 | Description of arguments. |
| 1479 | buffer This will be returned to the caller. |
| 1480 | """ |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1481 | |
Michael Walsh | 95e4510 | 2018-02-09 12:44:43 -0600 | [diff] [blame] | 1482 | try: |
| 1483 | return str(buffer) |
| 1484 | except UnicodeEncodeError: |
| 1485 | return buffer |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1486 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1487 | |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1488 | def sprintn(buffer=""): |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1489 | r""" |
| 1490 | Simply return the user's buffer with a line feed. This function is used |
| 1491 | by the qprint and dprint functions defined dynamically below, i.e. it |
| 1492 | would not normally be called for general use. |
| 1493 | |
| 1494 | Description of arguments. |
| 1495 | buffer This will be returned to the caller. |
| 1496 | """ |
| 1497 | |
Michael Walsh | 95e4510 | 2018-02-09 12:44:43 -0600 | [diff] [blame] | 1498 | try: |
| 1499 | buffer = str(buffer) + "\n" |
| 1500 | except UnicodeEncodeError: |
| 1501 | buffer = buffer + "\n" |
Michael Walsh | bec416d | 2016-11-10 08:54:52 -0600 | [diff] [blame] | 1502 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1503 | return buffer |
| 1504 | |
Michael Walsh | 168eb0f | 2017-12-01 15:35:32 -0600 | [diff] [blame] | 1505 | |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1506 | def gp_print(buffer, |
| 1507 | stream='stdout'): |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1508 | r""" |
| 1509 | Print the buffer using either sys.stdout.write or BuiltIn().log_to_console |
| 1510 | depending on whether we are running in a robot environment. |
| 1511 | |
| 1512 | This function is intended for use only by other functions in this module. |
| 1513 | |
| 1514 | Description of arguments: |
| 1515 | buffer The string to be printed. |
| 1516 | stream Either "stdout" or "stderr". |
| 1517 | """ |
| 1518 | |
| 1519 | if robot_env: |
| 1520 | BuiltIn().log_to_console(buffer, stream=stream, no_newline=True) |
| 1521 | else: |
| 1522 | if stream == "stdout": |
| 1523 | sys.stdout.write(buffer) |
| 1524 | sys.stdout.flush() |
| 1525 | else: |
| 1526 | sys.stderr.write(buffer) |
| 1527 | sys.stderr.flush() |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1528 | |
| 1529 | |
Michael Walsh | 168eb0f | 2017-12-01 15:35:32 -0600 | [diff] [blame] | 1530 | def gp_log(buffer): |
Michael Walsh | 168eb0f | 2017-12-01 15:35:32 -0600 | [diff] [blame] | 1531 | r""" |
| 1532 | Log the buffer using either python logging or BuiltIn().log depending on |
| 1533 | whether we are running in a robot environment. |
| 1534 | |
| 1535 | This function is intended for use only by other functions in this module. |
| 1536 | |
| 1537 | Description of arguments: |
| 1538 | buffer The string to be logged. |
| 1539 | """ |
| 1540 | |
| 1541 | if robot_env: |
| 1542 | BuiltIn().log(buffer) |
| 1543 | else: |
| 1544 | logging.warning(buffer) |
| 1545 | |
| 1546 | |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1547 | def gp_debug_print(buffer): |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1548 | r""" |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1549 | Print with gp_print only if gen_print_debug is set. |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1550 | |
| 1551 | This function is intended for use only by other functions in this module. |
| 1552 | |
| 1553 | Description of arguments: |
| 1554 | buffer The string to be printed. |
| 1555 | """ |
| 1556 | |
| 1557 | if not gen_print_debug: |
| 1558 | return |
| 1559 | |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1560 | gp_print(buffer) |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1561 | |
| 1562 | |
Michael Walsh | b150015 | 2017-04-12 15:42:43 -0500 | [diff] [blame] | 1563 | def get_var_value(var_value=None, |
| 1564 | default=1, |
| 1565 | var_name=None): |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1566 | r""" |
Michael Walsh | b150015 | 2017-04-12 15:42:43 -0500 | [diff] [blame] | 1567 | Return either var_value, the corresponding global value or default. |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1568 | |
Michael Walsh | b150015 | 2017-04-12 15:42:43 -0500 | [diff] [blame] | 1569 | If var_value is not None, it will simply be returned. |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1570 | |
Michael Walsh | b150015 | 2017-04-12 15:42:43 -0500 | [diff] [blame] | 1571 | If var_value is None, this function will return the corresponding global |
| 1572 | value of the variable in question. |
| 1573 | |
| 1574 | Note: For global values, if we are in a robot environment, |
| 1575 | get_variable_value will be used. Otherwise, the __builtin__ version of |
| 1576 | the variable is returned (which are set by gen_arg.py functions). |
| 1577 | |
| 1578 | If there is no global value associated with the variable, default is |
| 1579 | returned. |
| 1580 | |
| 1581 | This function is useful for other functions in setting default values for |
| 1582 | parameters. |
| 1583 | |
| 1584 | Example use: |
| 1585 | |
| 1586 | def my_func(quiet=None): |
| 1587 | |
| 1588 | quiet = int(get_var_value(quiet, 0)) |
| 1589 | |
| 1590 | Example calls to my_func(): |
| 1591 | |
| 1592 | In the following example, the caller is explicitly asking to have quiet be |
| 1593 | set to 1. |
| 1594 | |
| 1595 | my_func(quiet=1) |
| 1596 | |
| 1597 | In the following example, quiet will be set to the global value of quiet, |
| 1598 | if defined, or to 0 (the default). |
| 1599 | |
| 1600 | my_func() |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1601 | |
| 1602 | Description of arguments: |
Michael Walsh | b150015 | 2017-04-12 15:42:43 -0500 | [diff] [blame] | 1603 | var_value The value to be returned (if not equal to |
| 1604 | None). |
| 1605 | default The value that is returned if var_value is |
| 1606 | None and there is no corresponding global |
| 1607 | value defined. |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1608 | var_name The name of the variable whose value is to |
Michael Walsh | b150015 | 2017-04-12 15:42:43 -0500 | [diff] [blame] | 1609 | be returned. Under most circumstances, |
| 1610 | this value need not be provided. This |
| 1611 | function can figure out the name of the |
| 1612 | variable passed as var_value. One |
| 1613 | exception to this would be if this |
| 1614 | function is called directly from a .robot |
| 1615 | file. |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1616 | """ |
| 1617 | |
Michael Walsh | b150015 | 2017-04-12 15:42:43 -0500 | [diff] [blame] | 1618 | if var_value is not None: |
| 1619 | return var_value |
| 1620 | |
| 1621 | if var_name is None: |
| 1622 | var_name = get_arg_name(None, 1, 2) |
| 1623 | |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1624 | if robot_env: |
Michael Walsh | c653744 | 2017-06-06 15:33:52 -0500 | [diff] [blame] | 1625 | var_value = BuiltIn().get_variable_value("${" + var_name + "}", |
| 1626 | default) |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1627 | else: |
| 1628 | var_value = getattr(__builtin__, var_name, default) |
| 1629 | |
| 1630 | return var_value |
| 1631 | |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1632 | |
Michael Walsh | 052ff81 | 2018-05-18 16:09:09 -0500 | [diff] [blame] | 1633 | def get_stack_var(var_name, |
| 1634 | default="", |
| 1635 | init_stack_ix=2): |
Michael Walsh | 052ff81 | 2018-05-18 16:09:09 -0500 | [diff] [blame] | 1636 | r""" |
| 1637 | Starting with the caller's stack level, search upward in the call stack, |
| 1638 | for a variable named var_name and return its value. If the variable |
| 1639 | cannot be found, return default. |
| 1640 | |
| 1641 | Example code: |
| 1642 | |
| 1643 | def func12(): |
| 1644 | my_loc_var1 = get_stack_var('my_var1', "default value") |
| 1645 | |
| 1646 | def func11(): |
| 1647 | my_var1 = 11 |
| 1648 | func12() |
| 1649 | |
| 1650 | In this example, get_stack_var will find the value of my_var1 in func11's |
| 1651 | stack and will therefore return the value 11. Therefore, my_loc_var1 |
| 1652 | would get set to 11. |
| 1653 | |
| 1654 | Description of argument(s): |
| 1655 | var_name The name of the variable to be searched |
| 1656 | for. |
| 1657 | default The value to return if the the variable |
| 1658 | cannot be found. |
| 1659 | init_stack_ix The initial stack index from which to |
| 1660 | begin the search. 0 would be the index of |
| 1661 | this func1tion ("get_stack_var"), 1 would |
| 1662 | be the index of the function calling this |
| 1663 | function, etc. |
| 1664 | """ |
| 1665 | |
Michael Walsh | 6f0362c | 2019-03-25 14:05:14 -0500 | [diff] [blame] | 1666 | work_around_inspect_stack_cwd_failure() |
Michael Walsh | 052ff81 | 2018-05-18 16:09:09 -0500 | [diff] [blame] | 1667 | return next((frame[0].f_locals[var_name] |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 1668 | for frame in inspect.stack()[init_stack_ix:] |
| 1669 | if var_name in frame[0].f_locals), default) |
Michael Walsh | 052ff81 | 2018-05-18 16:09:09 -0500 | [diff] [blame] | 1670 | |
| 1671 | |
Michael Walsh | 82acf00 | 2017-05-04 14:33:05 -0500 | [diff] [blame] | 1672 | # hidden_text is a list of passwords which are to be replaced with asterisks |
| 1673 | # by print functions defined in this module. |
| 1674 | hidden_text = [] |
| 1675 | # password_regex is created based on the contents of hidden_text. |
| 1676 | password_regex = "" |
| 1677 | |
| 1678 | |
Michael Walsh | 82acf00 | 2017-05-04 14:33:05 -0500 | [diff] [blame] | 1679 | def register_passwords(*args): |
Michael Walsh | 82acf00 | 2017-05-04 14:33:05 -0500 | [diff] [blame] | 1680 | r""" |
| 1681 | Register one or more passwords which are to be hidden in output produced |
| 1682 | by the print functions in this module. |
| 1683 | |
| 1684 | Note: Blank password values are NOT registered. They are simply ignored. |
| 1685 | |
| 1686 | Description of argument(s): |
| 1687 | args One or more password values. If a given |
| 1688 | password value is already registered, this |
| 1689 | function will simply do nothing. |
| 1690 | """ |
| 1691 | |
| 1692 | global hidden_text |
| 1693 | global password_regex |
| 1694 | |
| 1695 | for password in args: |
| 1696 | if password == "": |
| 1697 | break |
| 1698 | if password in hidden_text: |
| 1699 | break |
| 1700 | |
| 1701 | # Place the password into the hidden_text list. |
| 1702 | hidden_text.append(password) |
| 1703 | # Create a corresponding password regular expression. Escape regex |
| 1704 | # special characters too. |
| 1705 | password_regex = '(' +\ |
| 1706 | '|'.join([re.escape(x) for x in hidden_text]) + ')' |
| 1707 | |
Michael Walsh | 82acf00 | 2017-05-04 14:33:05 -0500 | [diff] [blame] | 1708 | |
Michael Walsh | 82acf00 | 2017-05-04 14:33:05 -0500 | [diff] [blame] | 1709 | def replace_passwords(buffer): |
Michael Walsh | 82acf00 | 2017-05-04 14:33:05 -0500 | [diff] [blame] | 1710 | r""" |
| 1711 | Return the buffer but with all registered passwords replaced by a string |
| 1712 | of asterisks. |
| 1713 | |
| 1714 | |
| 1715 | Description of argument(s): |
| 1716 | buffer The string to be returned but with |
| 1717 | passwords replaced. |
| 1718 | """ |
| 1719 | |
| 1720 | global password_regex |
| 1721 | |
| 1722 | if int(os.environ.get("DEBUG_SHOW_PASSWORDS", "0")): |
| 1723 | return buffer |
| 1724 | |
| 1725 | if password_regex == "": |
| 1726 | # No passwords to replace. |
| 1727 | return buffer |
| 1728 | |
| 1729 | return re.sub(password_regex, "********", buffer) |
| 1730 | |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1731 | |
| 1732 | def create_print_wrapper_funcs(func_names, |
| 1733 | stderr_func_names, |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1734 | replace_dict, |
| 1735 | func_prefix=""): |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1736 | r""" |
| 1737 | Generate code for print wrapper functions and return the generated code as |
| 1738 | a string. |
| 1739 | |
| 1740 | To illustrate, suppose there is a "print_foo_bar" function in the |
| 1741 | func_names list. |
| 1742 | This function will... |
| 1743 | - Expect that there is an sprint_foo_bar function already in existence. |
| 1744 | - Create a print_foo_bar function which calls sprint_foo_bar and prints |
Michael Walsh | faafa9c | 2018-06-27 16:39:31 -0500 | [diff] [blame] | 1745 | the result. |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1746 | - Create a qprint_foo_bar function which calls upon sprint_foo_bar only if |
Michael Walsh | faafa9c | 2018-06-27 16:39:31 -0500 | [diff] [blame] | 1747 | global value quiet is 0. |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1748 | - Create a dprint_foo_bar function which calls upon sprint_foo_bar only if |
Michael Walsh | faafa9c | 2018-06-27 16:39:31 -0500 | [diff] [blame] | 1749 | global value debug is 1. |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1750 | |
| 1751 | Also, code will be generated to define aliases for each function as well. |
| 1752 | Each alias will be created by replacing "print_" in the function name with |
| 1753 | "p" For example, the alias for print_foo_bar will be pfoo_bar. |
| 1754 | |
| 1755 | Description of argument(s): |
| 1756 | func_names A list of functions for which print |
| 1757 | wrapper function code is to be generated. |
| 1758 | stderr_func_names A list of functions whose generated code |
| 1759 | should print to stderr rather than to |
| 1760 | stdout. |
| 1761 | replace_dict Please see the create_func_def_string |
| 1762 | function in wrap_utils.py for details on |
| 1763 | this parameter. This parameter will be |
| 1764 | passed directly to create_func_def_string. |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1765 | func_prefix Prefix to be pre-pended to the generated |
| 1766 | function name. |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1767 | """ |
| 1768 | |
| 1769 | buffer = "" |
| 1770 | |
| 1771 | for func_name in func_names: |
| 1772 | if func_name in stderr_func_names: |
| 1773 | replace_dict['output_stream'] = "stderr" |
| 1774 | else: |
| 1775 | replace_dict['output_stream'] = "stdout" |
| 1776 | |
| 1777 | s_func_name = "s" + func_name |
| 1778 | q_func_name = "q" + func_name |
| 1779 | d_func_name = "d" + func_name |
| 1780 | |
| 1781 | # We don't want to try to redefine the "print" function, thus the |
| 1782 | # following if statement. |
| 1783 | if func_name != "print": |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1784 | func_def = create_func_def_string(s_func_name, |
| 1785 | func_prefix + func_name, |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1786 | print_func_template, |
| 1787 | replace_dict) |
| 1788 | buffer += func_def |
| 1789 | |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1790 | func_def = create_func_def_string(s_func_name, |
| 1791 | func_prefix + "q" + func_name, |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1792 | qprint_func_template, replace_dict) |
| 1793 | buffer += func_def |
| 1794 | |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1795 | func_def = create_func_def_string(s_func_name, |
| 1796 | func_prefix + "d" + func_name, |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1797 | dprint_func_template, replace_dict) |
| 1798 | buffer += func_def |
| 1799 | |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1800 | func_def = create_func_def_string(s_func_name, |
| 1801 | func_prefix + "l" + func_name, |
Michael Walsh | 168eb0f | 2017-12-01 15:35:32 -0600 | [diff] [blame] | 1802 | lprint_func_template, replace_dict) |
| 1803 | buffer += func_def |
| 1804 | |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1805 | # Create abbreviated aliases (e.g. spvar is an alias for sprint_var). |
| 1806 | alias = re.sub("print_", "p", func_name) |
| 1807 | alias = re.sub("print", "p", alias) |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1808 | prefixes = [func_prefix + "", "s", func_prefix + "q", |
| 1809 | func_prefix + "d", func_prefix + "l"] |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1810 | for prefix in prefixes: |
| 1811 | if alias == "p": |
| 1812 | continue |
| 1813 | func_def = prefix + alias + " = " + prefix + func_name |
| 1814 | buffer += func_def + "\n" |
| 1815 | |
| 1816 | return buffer |
Michael Walsh | 82acf00 | 2017-05-04 14:33:05 -0500 | [diff] [blame] | 1817 | |
| 1818 | |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1819 | # In the following section of code, we will dynamically create print versions |
| 1820 | # for each of the sprint functions defined above. So, for example, where we |
| 1821 | # have an sprint_time() function defined above that returns the time to the |
Michael Walsh | 7423c01 | 2016-10-04 10:27:21 -0500 | [diff] [blame] | 1822 | # caller in a string, we will create a corresponding print_time() function |
| 1823 | # that will print that string directly to stdout. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1824 | |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1825 | # It can be complicated to follow what's being created below. Here is an |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1826 | # example of the print_time() function that will be created: |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1827 | |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1828 | # def print_time(buffer=''): |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1829 | # gp_print(replace_passwords(sprint_time(buffer=buffer)), stream='stdout') |
| 1830 | |
| 1831 | # For each print function defined below, there will also be a qprint, a |
| 1832 | # dprint and an lprint version defined (e.g. qprint_time, dprint_time, |
| 1833 | # lprint_time). |
| 1834 | |
| 1835 | # The q version of each print function will only print if the quiet variable |
| 1836 | # is 0. |
| 1837 | # The d version of each print function will only print if the debug variable |
| 1838 | # is 1. |
| 1839 | # The l version of each print function will print the contents as log data. |
| 1840 | # For conventional programs, this means use of the logging module. For robot |
| 1841 | # programs it means use of the BuiltIn().log() function. |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1842 | |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1843 | # Templates for the various print wrapper functions. |
| 1844 | print_func_template = \ |
| 1845 | [ |
Joy Onyerikwu | 004ad3c | 2018-06-11 16:29:56 -0500 | [diff] [blame] | 1846 | " <mod_qualifier>gp_print(<mod_qualifier>replace_passwords(" |
| 1847 | + "<call_line>), stream='<output_stream>')" |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1848 | ] |
| 1849 | |
| 1850 | qprint_func_template = \ |
| 1851 | [ |
Michael Walsh | 589ae76 | 2019-03-19 13:22:39 -0500 | [diff] [blame] | 1852 | " quiet_default = <mod_qualifier>get_var_value(None, 0, \"quiet\")", |
| 1853 | " quiet = <mod_qualifier>get_stack_var(\"quiet\", quiet_default)", |
| 1854 | " if int(quiet): return" |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1855 | ] + print_func_template |
| 1856 | |
| 1857 | dprint_func_template = \ |
| 1858 | [ |
Michael Walsh | 589ae76 | 2019-03-19 13:22:39 -0500 | [diff] [blame] | 1859 | " debug_default = <mod_qualifier>get_var_value(None, 0, \"debug\")", |
| 1860 | " debug = <mod_qualifier>get_stack_var(\"debug\", debug_default)", |
| 1861 | " if not int(debug): return" |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1862 | ] + print_func_template |
| 1863 | |
Michael Walsh | 168eb0f | 2017-12-01 15:35:32 -0600 | [diff] [blame] | 1864 | lprint_func_template = \ |
| 1865 | [ |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1866 | " <mod_qualifier>set_last_seconds_ix(<mod_qualifier>" |
| 1867 | + "lprint_last_seconds_ix())", |
| 1868 | " <mod_qualifier>gp_log(<mod_qualifier>replace_passwords" |
| 1869 | + "(<call_line>))", |
| 1870 | " <mod_qualifier>set_last_seconds_ix(<mod_qualifier>" |
| 1871 | + "standard_print_last_seconds_ix())" |
Michael Walsh | 168eb0f | 2017-12-01 15:35:32 -0600 | [diff] [blame] | 1872 | ] |
| 1873 | |
Michael Walsh | 81c0234 | 2018-01-05 15:43:28 -0600 | [diff] [blame] | 1874 | replace_dict = {'output_stream': 'stdout', 'mod_qualifier': ''} |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1875 | |
Michael Walsh | 61c1298 | 2019-03-28 12:38:01 -0500 | [diff] [blame] | 1876 | gp_debug_print("robot_env: " + str(robot_env) + "\n") |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1877 | |
| 1878 | # func_names contains a list of all print functions which should be created |
| 1879 | # from their sprint counterparts. |
| 1880 | func_names = ['print_time', 'print_timen', 'print_error', 'print_varx', |
Michael Walsh | 1817632 | 2016-11-15 15:11:21 -0600 | [diff] [blame] | 1881 | 'print_var', 'print_vars', 'print_dashes', 'indent', |
| 1882 | 'print_call_stack', 'print_func_name', 'print_executing', |
| 1883 | 'print_pgm_header', 'print_issuing', 'print_pgm_footer', |
| 1884 | 'print_error_report', 'print', 'printn'] |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1885 | |
Michael Walsh | 2ee77cd | 2017-03-08 11:50:17 -0600 | [diff] [blame] | 1886 | # stderr_func_names is a list of functions whose output should go to stderr |
| 1887 | # rather than stdout. |
| 1888 | stderr_func_names = ['print_error', 'print_error_report'] |
Michael Walsh | de79173 | 2016-09-06 14:25:24 -0500 | [diff] [blame] | 1889 | |
Michael Walsh | fd2733c | 2017-11-13 11:36:20 -0600 | [diff] [blame] | 1890 | func_defs = create_print_wrapper_funcs(func_names, stderr_func_names, |
| 1891 | replace_dict) |
| 1892 | gp_debug_print(func_defs) |
| 1893 | exec(func_defs) |