1#!/usr/bin/env python
2
3r"""
4This module provides many valuable print functions such as sprint_var,
5sprint_time, sprint_error, sprint_call_stack.
6"""
7
8import sys
9import os
10import time
11import inspect
12import re
13import grp
14import socket
15import argparse
16import __builtin__
17import logging
18import collections
19
20try:
21    robot_env = 1
22    from robot.utils import DotDict
23    from robot.utils import NormalizedDict
24    from robot.libraries.BuiltIn import BuiltIn
25    # Having access to the robot libraries alone does not indicate that we
26    # are in a robot environment.  The following try block should confirm that.
27    try:
28        var_value = BuiltIn().get_variable_value("${SUITE_NAME}", "")
29    except:
30        robot_env = 0
31except ImportError:
32    robot_env = 0
33
34import gen_arg as ga
35
36# Setting these variables for use both inside this module and by programs
37# importing this module.
38pgm_dir_path = sys.argv[0]
39pgm_name = os.path.basename(pgm_dir_path)
40pgm_dir_name = re.sub("/" + pgm_name, "", pgm_dir_path) + "/"
41
42
43# Some functions (e.g. sprint_pgm_header) have need of a program name value
44# that looks more like a valid variable name.  Therefore, we'll swap odd
45# characters like "." out for underscores.
46pgm_name_var_name = pgm_name.replace(".", "_")
47
48# Initialize global values used as defaults by print_time, print_var, etc.
49col1_indent = 0
50
51# Calculate default column width for print_var functions based on environment
52# variable settings.  The objective is to make the variable values line up
53# nicely with the time stamps.
54col1_width = 29
55
56NANOSECONDS = os.environ.get('NANOSECONDS', '1')
57
58
59if NANOSECONDS == "1":
60    col1_width = col1_width + 7
61
62SHOW_ELAPSED_TIME = os.environ.get('SHOW_ELAPSED_TIME', '1')
63
64if SHOW_ELAPSED_TIME == "1":
65    if NANOSECONDS == "1":
66        col1_width = col1_width + 14
67    else:
68        col1_width = col1_width + 7
69
70# Initialize some time variables used in module functions.
71start_time = time.time()
72sprint_time_last_seconds = start_time
73
74# The user can set environment variable "GEN_PRINT_DEBUG" to get debug output
75# from this module.
76gen_print_debug = int(os.environ.get('GEN_PRINT_DEBUG', 0))
77
78
79###############################################################################
80def sprint_func_name(stack_frame_ix=None):
81
82    r"""
83    Return the function name associated with the indicated stack frame.
84
85    Description of arguments:
86    stack_frame_ix                  The index of the stack frame whose
87                                    function name should be returned.  If the
88                                    caller does not specify a value, this
89                                    function will set the value to 1 which is
90                                    the index of the caller's stack frame.  If
91                                    the caller is the wrapper function
92                                    "print_func_name", this function will bump
93                                    it up by 1.
94    """
95
96    # If user specified no stack_frame_ix, we'll set it to a proper default
97    # value.
98    if stack_frame_ix is None:
99        func_name = sys._getframe().f_code.co_name
100        caller_func_name = sys._getframe(1).f_code.co_name
101        if func_name[1:] == caller_func_name:
102            stack_frame_ix = 2
103        else:
104            stack_frame_ix = 1
105
106    func_name = sys._getframe(stack_frame_ix).f_code.co_name
107
108    return func_name
109
110###############################################################################
111
112
113# get_arg_name is not a print function per se.  I have included it in this
114# module because it is used by sprint_var which is found in this module.
115###############################################################################
116def get_arg_name(var,
117                 arg_num=1,
118                 stack_frame_ix=1):
119
120    r"""
121    Return the "name" of an argument passed to a function.  This could be a
122    literal or a variable name.
123
124    Description of arguments:
125    var                             The variable whose name you want returned.
126    arg_num                         The arg number (1 through n) whose name
127                                    you wish to have returned.  This value
128                                    should not exceed the number of arguments
129                                    allowed by the target function.
130    stack_frame_ix                  The stack frame index of the target
131                                    function.  This value must be 1 or
132                                    greater.  1 would indicate get_arg_name's
133                                    stack frame.  2 would be the caller of
134                                    get_arg_name's stack frame, etc.
135
136    Example 1:
137
138    my_var = "mike"
139    var_name = get_arg_name(my_var)
140
141    In this example, var_name will receive the value "my_var".
142
143    Example 2:
144
145    def test1(var):
146        # Getting the var name of the first arg to this function, test1.
147        # Note, in this case, it doesn't matter what you pass as the first arg
148        # to get_arg_name since it is the caller's variable name that matters.
149        dummy = 1
150        arg_num = 1
151        stack_frame = 2
152        var_name = get_arg_name(dummy, arg_num, stack_frame)
153
154    # Mainline...
155
156    another_var = "whatever"
157    test1(another_var)
158
159    In this example, var_name will be set to "another_var".
160
161    """
162
163    # Note: I wish to avoid recursion so I refrain from calling any function
164    # that calls this function (i.e. sprint_var, valid_value, etc.).
165
166    # The user can set environment variable "GET_ARG_NAME_DEBUG" to get debug
167    # output from this function.
168    local_debug = int(os.environ.get('GET_ARG_NAME_DEBUG', 0))
169    # In addition to GET_ARG_NAME_DEBUG, the user can set environment
170    # variable "GET_ARG_NAME_SHOW_SOURCE" to have this function include source
171    # code in the debug output.
172    local_debug_show_source = int(
173        os.environ.get('GET_ARG_NAME_SHOW_SOURCE', 0))
174
175    if arg_num < 1:
176        print_error("Programmer error - Variable \"arg_num\" has an invalid" +
177                    " value of \"" + str(arg_num) + "\".  The value must be" +
178                    " an integer that is greater than 0.\n")
179        # What is the best way to handle errors?  Raise exception?  I'll
180        # revisit later.
181        return
182    if stack_frame_ix < 1:
183        print_error("Programmer error - Variable \"stack_frame_ix\" has an" +
184                    " invalid value of \"" + str(stack_frame_ix) + "\".  The" +
185                    " value must be an integer that is greater than or equal" +
186                    " to 1.\n")
187        return
188
189    if local_debug:
190        debug_indent = 2
191        print("")
192        print_dashes(0, 120)
193        print(sprint_func_name() + "() parms:")
194        print_varx("var", var, 0, debug_indent)
195        print_varx("arg_num", arg_num, 0, debug_indent)
196        print_varx("stack_frame_ix", stack_frame_ix, 0, debug_indent)
197        print("")
198        print_call_stack(debug_indent, 2)
199
200    for count in range(0, 2):
201        try:
202            frame, filename, cur_line_no, function_name, lines, index = \
203                inspect.stack()[stack_frame_ix]
204        except IndexError:
205            print_error("Programmer error - The caller has asked for" +
206                        " information about the stack frame at index \"" +
207                        str(stack_frame_ix) + "\".  However, the stack" +
208                        " only contains " + str(len(inspect.stack())) +
209                        " entries.  Therefore the stack frame index is out" +
210                        " of range.\n")
211            return
212        if filename != "<string>":
213            break
214        # filename of "<string>" may mean that the function in question was
215        # defined dynamically and therefore its code stack is inaccessible.
216        # This may happen with functions like "rqprint_var".  In this case,
217        # we'll increment the stack_frame_ix and try again.
218        stack_frame_ix += 1
219        if local_debug:
220            print("Adjusted stack_frame_ix...")
221            print_varx("stack_frame_ix", stack_frame_ix, 0, debug_indent)
222
223    called_func_name = sprint_func_name(stack_frame_ix)
224
225    module = inspect.getmodule(frame)
226
227    # Though I would expect inspect.getsourcelines(frame) to get all module
228    # source lines if the frame is "<module>", it doesn't do that.  Therefore,
229    # for this special case, I will do inspect.getsourcelines(module).
230    if function_name == "<module>":
231        source_lines, source_line_num =\
232            inspect.getsourcelines(module)
233        line_ix = cur_line_no - source_line_num - 1
234    else:
235        source_lines, source_line_num =\
236            inspect.getsourcelines(frame)
237        line_ix = cur_line_no - source_line_num
238
239    if local_debug:
240        print("\n  Variables retrieved from inspect.stack() function:")
241        print_varx("frame", frame, 0, debug_indent + 2)
242        print_varx("filename", filename, 0, debug_indent + 2)
243        print_varx("cur_line_no", cur_line_no, 0, debug_indent + 2)
244        print_varx("function_name", function_name, 0, debug_indent + 2)
245        print_varx("lines", lines, 0, debug_indent + 2)
246        print_varx("index", index, 0, debug_indent + 2)
247        print_varx("source_line_num", source_line_num, 0, debug_indent)
248        print_varx("line_ix", line_ix, 0, debug_indent)
249        if local_debug_show_source:
250            print_varx("source_lines", source_lines, 0, debug_indent)
251        print_varx("called_func_name", called_func_name, 0, debug_indent)
252
253    # Get a list of all functions defined for the module.  Note that this
254    # doesn't work consistently when _run_exitfuncs is at the top of the stack
255    # (i.e. if we're running an exit function).  I've coded a work-around
256    # below for this deficiency.
257    all_functions = inspect.getmembers(module, inspect.isfunction)
258
259    # Get called_func_id by searching for our function in the list of all
260    # functions.
261    called_func_id = None
262    for func_name, function in all_functions:
263        if func_name == called_func_name:
264            called_func_id = id(function)
265            break
266    # NOTE: The only time I've found that called_func_id can't be found is
267    # when we're running from an exit function.
268
269    # Look for other functions in module with matching id.
270    aliases = set([called_func_name])
271    for func_name, function in all_functions:
272        if func_name == called_func_name:
273            continue
274        func_id = id(function)
275        if func_id == called_func_id:
276            aliases.add(func_name)
277
278    # In most cases, my general purpose code above will find all aliases.
279    # However, for the odd case (i.e. running from exit function), I've added
280    # code to handle pvar, qpvar, dpvar, etc. aliases explicitly since they
281    # are defined in this module and used frequently.
282    # pvar is an alias for print_var.
283    aliases.add(re.sub("print_var", "pvar", called_func_name))
284
285    func_regex = ".*(" + '|'.join(aliases) + ")[ ]*\("
286
287    # Search backward through source lines looking for the calling function
288    # name.
289    found = False
290    for start_line_ix in range(line_ix, 0, -1):
291        # Skip comment lines.
292        if re.match(r"[ ]*#", source_lines[start_line_ix]):
293            continue
294        if re.match(func_regex, source_lines[start_line_ix]):
295            found = True
296            break
297    if not found:
298        print_error("Programmer error - Could not find the source line with" +
299                    " a reference to function \"" + called_func_name + "\".\n")
300        return
301
302    # Search forward through the source lines looking for a line whose
303    # indentation is the same or less than the start line.  The end of our
304    # composite line should be the line preceding that line.
305    start_indent = len(source_lines[start_line_ix]) -\
306        len(source_lines[start_line_ix].lstrip(' '))
307    end_line_ix = line_ix
308    for end_line_ix in range(line_ix + 1, len(source_lines)):
309        if source_lines[end_line_ix].strip() == "":
310            continue
311        line_indent = len(source_lines[end_line_ix]) -\
312            len(source_lines[end_line_ix].lstrip(' '))
313        if line_indent <= start_indent:
314            end_line_ix -= 1
315            break
316
317    # Join the start line through the end line into a composite line.
318    composite_line = ''.join(map(str.strip,
319                             source_lines[start_line_ix:end_line_ix + 1]))
320
321    # arg_list_etc = re.sub(".*" + called_func_name, "", composite_line)
322    arg_list_etc = "(" + re.sub(func_regex, "", composite_line)
323    if local_debug:
324        print_varx("aliases", aliases, 0, debug_indent)
325        print_varx("func_regex", func_regex, 0, debug_indent)
326        print_varx("start_line_ix", start_line_ix, 0, debug_indent)
327        print_varx("end_line_ix", end_line_ix, 0, debug_indent)
328        print_varx("composite_line", composite_line, 0, debug_indent)
329        print_varx("arg_list_etc", arg_list_etc, 0, debug_indent)
330
331    # Parse arg list...
332    # Initialize...
333    nest_level = -1
334    arg_ix = 0
335    args_list = [""]
336    for ix in range(0, len(arg_list_etc)):
337        char = arg_list_etc[ix]
338        # Set the nest_level based on whether we've encounted a parenthesis.
339        if char == "(":
340            nest_level += 1
341            if nest_level == 0:
342                continue
343        elif char == ")":
344            nest_level -= 1
345            if nest_level < 0:
346                break
347
348        # If we reach a comma at base nest level, we are done processing an
349        # argument so we increment arg_ix and initialize a new args_list entry.
350        if char == "," and nest_level == 0:
351            arg_ix += 1
352            args_list.append("")
353            continue
354
355        # For any other character, we append it it to the current arg list
356        # entry.
357        args_list[arg_ix] += char
358
359    # Trim whitespace from each list entry.
360    args_list = [arg.strip() for arg in args_list]
361
362    if arg_num > len(args_list):
363        print_error("Programmer error - The caller has asked for the name of" +
364                    " argument number \"" + str(arg_num) + "\" but there " +
365                    "were only \"" + str(len(args_list)) + "\" args used:\n" +
366                    sprint_varx("args_list", args_list))
367        return
368
369    argument = args_list[arg_num - 1]
370
371    if local_debug:
372        print_varx("args_list", args_list, 0, debug_indent)
373        print_varx("argument", argument, 0, debug_indent)
374        print_dashes(0, 120)
375
376    return argument
377
378###############################################################################
379
380
381###############################################################################
382def sprint_time(buffer=""):
383
384    r"""
385    Return the time in the following format.
386
387    Example:
388
389    The following python code...
390
391    sys.stdout.write(sprint_time())
392    sys.stdout.write("Hi.\n")
393
394    Will result in the following type of output:
395
396    #(CDT) 2016/07/08 15:25:35 - Hi.
397
398    Example:
399
400    The following python code...
401
402    sys.stdout.write(sprint_time("Hi.\n"))
403
404    Will result in the following type of output:
405
406    #(CDT) 2016/08/03 17:12:05 - Hi.
407
408    The following environment variables will affect the formatting as
409    described:
410    NANOSECONDS                     This will cause the time stamps to be
411                                    precise to the microsecond (Yes, it
412                                    probably should have been named
413                                    MICROSECONDS but the convention was set
414                                    long ago so we're sticking with it).
415                                    Example of the output when environment
416                                    variable NANOSECONDS=1.
417
418    #(CDT) 2016/08/03 17:16:25.510469 - Hi.
419
420    SHOW_ELAPSED_TIME               This will cause the elapsed time to be
421                                    included in the output.  This is the
422                                    amount of time that has elapsed since the
423                                    last time this function was called.  The
424                                    precision of the elapsed time field is
425                                    also affected by the value of the
426                                    NANOSECONDS environment variable.  Example
427                                    of the output when environment variable
428                                    NANOSECONDS=0 and SHOW_ELAPSED_TIME=1.
429
430    #(CDT) 2016/08/03 17:17:40 -    0 - Hi.
431
432    Example of the output when environment variable NANOSECONDS=1 and
433    SHOW_ELAPSED_TIME=1.
434
435    #(CDT) 2016/08/03 17:18:47.317339 -    0.000046 - Hi.
436
437    Description of arguments.
438    buffer                          This will be appended to the formatted
439                                    time string.
440    """
441
442    global NANOSECONDS
443    global SHOW_ELAPSED_TIME
444    global sprint_time_last_seconds
445
446    seconds = time.time()
447    loc_time = time.localtime(seconds)
448    nanoseconds = "%0.6f" % seconds
449    pos = nanoseconds.find(".")
450    nanoseconds = nanoseconds[pos:]
451
452    time_string = time.strftime("#(%Z) %Y/%m/%d %H:%M:%S", loc_time)
453    if NANOSECONDS == "1":
454        time_string = time_string + nanoseconds
455
456    if SHOW_ELAPSED_TIME == "1":
457        cur_time_seconds = seconds
458        math_string = "%9.9f" % cur_time_seconds + " - " + "%9.9f" % \
459            sprint_time_last_seconds
460        elapsed_seconds = eval(math_string)
461        if NANOSECONDS == "1":
462            elapsed_seconds = "%11.6f" % elapsed_seconds
463        else:
464            elapsed_seconds = "%4i" % elapsed_seconds
465        sprint_time_last_seconds = cur_time_seconds
466        time_string = time_string + " - " + elapsed_seconds
467
468    return time_string + " - " + buffer
469
470###############################################################################
471
472
473###############################################################################
474def sprint_timen(buffer=""):
475
476    r"""
477    Append a line feed to the buffer, pass it to sprint_time and return the
478    result.
479    """
480
481    return sprint_time(buffer + "\n")
482
483###############################################################################
484
485
486###############################################################################
487def sprint_error(buffer=""):
488
489    r"""
490    Return a standardized error string.  This includes:
491      - A time stamp
492      - The "**ERROR**" string
493      - The caller's buffer string.
494
495    Example:
496
497    The following python code...
498
499    print(sprint_error("Oops.\n"))
500
501    Will result in the following type of output:
502
503    #(CDT) 2016/08/03 17:12:05 - **ERROR** Oops.
504
505    Description of arguments.
506    buffer                          This will be appended to the formatted
507                                    error string.
508    """
509
510    return sprint_time() + "**ERROR** " + buffer
511
512###############################################################################
513
514
515###############################################################################
516def sprint_varx(var_name,
517                var_value,
518                hex=0,
519                loc_col1_indent=col1_indent,
520                loc_col1_width=col1_width,
521                trailing_char="\n"):
522
523    r"""
524    Print the var name/value passed to it.  If the caller lets loc_col1_width
525    default, the printing lines up nicely with output generated by the
526    print_time functions.
527
528    Note that the sprint_var function (defined below) can be used to call this
529    function so that the programmer does not need to pass the var_name.
530    sprint_var will figure out the var_name.  The sprint_var function is the
531    one that would normally be used by the general user.
532
533    For example, the following python code:
534
535    first_name = "Mike"
536    print_time("Doing this...\n")
537    print_varx("first_name", first_name)
538    print_time("Doing that...\n")
539
540    Will generate output like this:
541
542    #(CDT) 2016/08/10 17:34:42.847374 -    0.001285 - Doing this...
543    first_name:                                       Mike
544    #(CDT) 2016/08/10 17:34:42.847510 -    0.000136 - Doing that...
545
546    This function recognizes several complex types of data such as dict, list
547    or tuple.
548
549    For example, the following python code:
550
551    my_dict = dict(one=1, two=2, three=3)
552    print_var(my_dict)
553
554    Will generate the following output:
555
556    my_dict:
557      my_dict[three]:                                 3
558      my_dict[two]:                                   2
559      my_dict[one]:                                   1
560
561    Description of arguments.
562    var_name                        The name of the variable to be printed.
563    var_value                       The value of the variable to be printed.
564    hex                             This indicates that the value should be
565                                    printed in hex format.  It is the user's
566                                    responsibility to ensure that a var_value
567                                    contains a valid hex number.  For string
568                                    var_values, this will be interpreted as
569                                    show_blanks which means that blank values
570                                    will be printed as "<blank>".  For dict
571                                    var_values, this will be interpreted as
572                                    terse format where keys are not repeated
573                                    in the output.
574    loc_col1_indent                 The number of spaces to indent the output.
575    loc_col1_width                  The width of the output column containing
576                                    the variable name.  The default value of
577                                    this is adjusted so that the var_value
578                                    lines up with text printed via the
579                                    print_time function.
580    trailing_char                   The character to be used at the end of the
581                                    returned string.  The default value is a
582                                    line feed.
583    """
584
585    # Determine the type
586    if type(var_value) in (int, float, bool, str, unicode) \
587       or var_value is None:
588        # The data type is simple in the sense that it has no subordinate
589        # parts.
590        # Adjust loc_col1_width.
591        loc_col1_width = loc_col1_width - loc_col1_indent
592        # See if the user wants the output in hex format.
593        if hex:
594            if type(var_value) not in (int, long):
595                value_format = "%s"
596                if var_value == "":
597                    var_value = "<blank>"
598            else:
599                value_format = "0x%08x"
600        else:
601            value_format = "%s"
602        format_string = "%" + str(loc_col1_indent) + "s%-" \
603            + str(loc_col1_width) + "s" + value_format + trailing_char
604        return format_string % ("", str(var_name) + ":", var_value)
605    elif type(var_value) is type:
606        return sprint_varx(var_name, str(var_value).split("'")[1], hex,
607                           loc_col1_indent, loc_col1_width, trailing_char)
608    else:
609        # The data type is complex in the sense that it has subordinate parts.
610        format_string = "%" + str(loc_col1_indent) + "s%s\n"
611        buffer = format_string % ("", var_name + ":")
612        loc_col1_indent += 2
613        try:
614            length = len(var_value)
615        except TypeError:
616            length = 0
617        ix = 0
618        loc_trailing_char = "\n"
619        type_is_dict = 0
620        if type(var_value) is dict:
621            type_is_dict = 1
622        try:
623            if type(var_value) is collections.OrderedDict:
624                type_is_dict = 1
625        except AttributeError:
626            pass
627        try:
628            if type(var_value) is DotDict:
629                type_is_dict = 1
630        except NameError:
631            pass
632        try:
633            if type(var_value) is NormalizedDict:
634                type_is_dict = 1
635        except NameError:
636            pass
637        if type_is_dict:
638            for key, value in var_value.iteritems():
639                ix += 1
640                if ix == length:
641                    loc_trailing_char = trailing_char
642                if hex:
643                    # Since hex is being used as a format type, we want it
644                    # turned off when processing integer dictionary values so
645                    # it is not interpreted as a hex indicator.
646                    loc_hex = not (type(value) is int)
647                    buffer += sprint_varx(key, value,
648                                          loc_hex, loc_col1_indent,
649                                          loc_col1_width,
650                                          loc_trailing_char)
651                else:
652                    buffer += sprint_varx(var_name + "[" + key + "]", value,
653                                          hex, loc_col1_indent, loc_col1_width,
654                                          loc_trailing_char)
655        elif type(var_value) in (list, tuple, set):
656            for key, value in enumerate(var_value):
657                ix += 1
658                if ix == length:
659                    loc_trailing_char = trailing_char
660                buffer += sprint_varx(var_name + "[" + str(key) + "]", value,
661                                      hex, loc_col1_indent, loc_col1_width,
662                                      loc_trailing_char)
663        elif type(var_value) is argparse.Namespace:
664            for key in var_value.__dict__:
665                ix += 1
666                if ix == length:
667                    loc_trailing_char = trailing_char
668                cmd_buf = "buffer += sprint_varx(var_name + \".\" + str(key)" \
669                          + ", var_value." + key + ", hex, loc_col1_indent," \
670                          + " loc_col1_width, loc_trailing_char)"
671                exec(cmd_buf)
672        else:
673            var_type = type(var_value).__name__
674            func_name = sys._getframe().f_code.co_name
675            var_value = "<" + var_type + " type not supported by " + \
676                        func_name + "()>"
677            value_format = "%s"
678            loc_col1_indent -= 2
679            # Adjust loc_col1_width.
680            loc_col1_width = loc_col1_width - loc_col1_indent
681            format_string = "%" + str(loc_col1_indent) + "s%-" \
682                + str(loc_col1_width) + "s" + value_format + trailing_char
683            return format_string % ("", str(var_name) + ":", var_value)
684
685        return buffer
686
687    return ""
688
689###############################################################################
690
691
692###############################################################################
693def sprint_var(*args):
694
695    r"""
696    Figure out the name of the first argument for you and then call
697    sprint_varx with it.  Therefore, the following 2 calls are equivalent:
698    sprint_varx("var1", var1)
699    sprint_var(var1)
700    """
701
702    # Get the name of the first variable passed to this function.
703    stack_frame = 2
704    caller_func_name = sprint_func_name(2)
705    if caller_func_name.endswith("print_var"):
706        stack_frame += 1
707    var_name = get_arg_name(None, 1, stack_frame)
708    return sprint_varx(var_name, *args)
709
710###############################################################################
711
712
713###############################################################################
714def sprint_vars(*args):
715
716    r"""
717    Sprint the values of one or more variables.
718
719    Description of args:
720    args:
721        If the first argument is an integer, it will be interpreted to be the
722        "indent" value.
723        If the second argument is an integer, it will be interpreted to be the
724        "col1_width" value.
725        If the third argument is an integer, it will be interpreted to be the
726        "hex" value.
727        All remaining parms are considered variable names which are to be
728        sprinted.
729    """
730
731    if len(args) == 0:
732        return
733
734    # Get the name of the first variable passed to this function.
735    stack_frame = 2
736    caller_func_name = sprint_func_name(2)
737    if caller_func_name.endswith("print_vars"):
738        stack_frame += 1
739
740    parm_num = 1
741
742    # Create list from args (which is a tuple) so that it can be modified.
743    args_list = list(args)
744
745    var_name = get_arg_name(None, parm_num, stack_frame)
746    # See if parm 1 is to be interpreted as "indent".
747    try:
748        if type(int(var_name)) is int:
749            indent = int(var_name)
750            args_list.pop(0)
751            parm_num += 1
752    except ValueError:
753        indent = 0
754
755    var_name = get_arg_name(None, parm_num, stack_frame)
756    # See if parm 1 is to be interpreted as "col1_width".
757    try:
758        if type(int(var_name)) is int:
759            loc_col1_width = int(var_name)
760            args_list.pop(0)
761            parm_num += 1
762    except ValueError:
763        loc_col1_width = col1_width
764
765    var_name = get_arg_name(None, parm_num, stack_frame)
766    # See if parm 1 is to be interpreted as "hex".
767    try:
768        if type(int(var_name)) is int:
769            hex = int(var_name)
770            args_list.pop(0)
771            parm_num += 1
772    except ValueError:
773        hex = 0
774
775    buffer = ""
776    for var_value in args_list:
777        var_name = get_arg_name(None, parm_num, stack_frame)
778        buffer += sprint_varx(var_name, var_value, hex, indent, loc_col1_width)
779        parm_num += 1
780
781    return buffer
782
783###############################################################################
784
785
786###############################################################################
787def lprint_varx(var_name,
788                var_value,
789                hex=0,
790                loc_col1_indent=col1_indent,
791                loc_col1_width=col1_width,
792                log_level=getattr(logging, 'INFO')):
793
794    r"""
795    Send sprint_varx output to logging.
796    """
797
798    logging.log(log_level, sprint_varx(var_name, var_value, hex,
799                loc_col1_indent, loc_col1_width, ""))
800
801###############################################################################
802
803
804###############################################################################
805def lprint_var(*args):
806
807    r"""
808    Figure out the name of the first argument for you and then call
809    lprint_varx with it.  Therefore, the following 2 calls are equivalent:
810    lprint_varx("var1", var1)
811    lprint_var(var1)
812    """
813
814    # Get the name of the first variable passed to this function.
815    stack_frame = 2
816    caller_func_name = sprint_func_name(2)
817    if caller_func_name.endswith("print_var"):
818        stack_frame += 1
819    var_name = get_arg_name(None, 1, stack_frame)
820    lprint_varx(var_name, *args)
821
822###############################################################################
823
824
825###############################################################################
826def sprint_dashes(indent=col1_indent,
827                  width=80,
828                  line_feed=1,
829                  char="-"):
830
831    r"""
832    Return a string of dashes to the caller.
833
834    Description of arguments:
835    indent                          The number of characters to indent the
836                                    output.
837    width                           The width of the string of dashes.
838    line_feed                       Indicates whether the output should end
839                                    with a line feed.
840    char                            The character to be repeated in the output
841                                    string.
842    """
843
844    width = int(width)
845    buffer = " " * int(indent) + char * width
846    if line_feed:
847        buffer += "\n"
848
849    return buffer
850
851###############################################################################
852
853
854###############################################################################
855def sindent(text="",
856            indent=0):
857
858    r"""
859    Pre-pend the specified number of characters to the text string (i.e.
860    indent it) and return it.
861
862    Description of arguments:
863    text                            The string to be indented.
864    indent                          The number of characters to indent the
865                                    string.
866    """
867
868    format_string = "%" + str(indent) + "s%s"
869    buffer = format_string % ("", text)
870
871    return buffer
872
873###############################################################################
874
875
876###############################################################################
877def sprint_call_stack(indent=0,
878                      stack_frame_ix=0):
879
880    r"""
881    Return a call stack report for the given point in the program with line
882    numbers, function names and function parameters and arguments.
883
884    Sample output:
885
886    -------------------------------------------------------------------------
887    Python function call stack
888
889    Line # Function name and arguments
890    ------ ------------------------------------------------------------------
891       424 sprint_call_stack ()
892         4 print_call_stack ()
893        31 func1 (last_name = 'walsh', first_name = 'mikey')
894        59 /tmp/scr5.py
895    -------------------------------------------------------------------------
896
897    Description of arguments:
898    indent                          The number of characters to indent each
899                                    line of output.
900    stack_frame_ix                  The index of the first stack frame which
901                                    is to be returned.
902    """
903
904    buffer = ""
905    buffer += sprint_dashes(indent)
906    buffer += sindent("Python function call stack\n\n", indent)
907    buffer += sindent("Line # Function name and arguments\n", indent)
908    buffer += sprint_dashes(indent, 6, 0) + " " + sprint_dashes(0, 73)
909
910    # Grab the current program stack.
911    current_stack = inspect.stack()
912
913    # Process each frame in turn.
914    format_string = "%6s %s\n"
915    ix = 0
916    for stack_frame in current_stack:
917        if ix < stack_frame_ix:
918            ix += 1
919            continue
920        # I want the line number shown to be the line where you find the line
921        # shown.
922        try:
923            line_num = str(current_stack[ix + 1][2])
924        except IndexError:
925            line_num = ""
926        func_name = str(stack_frame[3])
927        if func_name == "?":
928            # "?" is the name used when code is not in a function.
929            func_name = "(none)"
930
931        if func_name == "<module>":
932            # If the func_name is the "main" program, we simply get the
933            # command line call string.
934            func_and_args = ' '.join(sys.argv)
935        else:
936            # Get the program arguments.
937            arg_vals = inspect.getargvalues(stack_frame[0])
938            function_parms = arg_vals[0]
939            frame_locals = arg_vals[3]
940
941            args_list = []
942            for arg_name in function_parms:
943                # Get the arg value from frame locals.
944                arg_value = frame_locals[arg_name]
945                args_list.append(arg_name + " = " + repr(arg_value))
946            args_str = "(" + ', '.join(map(str, args_list)) + ")"
947
948            # Now we need to print this in a nicely-wrapped way.
949            func_and_args = func_name + " " + args_str
950
951        buffer += sindent(format_string % (line_num, func_and_args), indent)
952        ix += 1
953
954    buffer += sprint_dashes(indent)
955
956    return buffer
957
958###############################################################################
959
960
961###############################################################################
962def sprint_executing(stack_frame_ix=None):
963
964    r"""
965    Print a line indicating what function is executing and with what parameter
966    values.  This is useful for debugging.
967
968    Sample output:
969
970    #(CDT) 2016/08/25 17:54:27 - Executing: func1 (x = 1)
971
972    Description of arguments:
973    stack_frame_ix                  The index of the stack frame whose
974                                    function info should be returned.  If the
975                                    caller does not specify a value, this
976                                    function will set the value to 1 which is
977                                    the index of the caller's stack frame.  If
978                                    the caller is the wrapper function
979                                    "print_executing", this function will bump
980                                    it up by 1.
981    """
982
983    # If user wants default stack_frame_ix.
984    if stack_frame_ix is None:
985        func_name = sys._getframe().f_code.co_name
986        caller_func_name = sys._getframe(1).f_code.co_name
987        if caller_func_name.endswith(func_name[1:]):
988            stack_frame_ix = 2
989        else:
990            stack_frame_ix = 1
991
992    stack_frame = inspect.stack()[stack_frame_ix]
993
994    func_name = str(stack_frame[3])
995    if func_name == "?":
996        # "?" is the name used when code is not in a function.
997        func_name = "(none)"
998
999    if func_name == "<module>":
1000        # If the func_name is the "main" program, we simply get the command
1001        # line call string.
1002        func_and_args = ' '.join(sys.argv)
1003    else:
1004        # Get the program arguments.
1005        arg_vals = inspect.getargvalues(stack_frame[0])
1006        function_parms = arg_vals[0]
1007        frame_locals = arg_vals[3]
1008
1009        args_list = []
1010        for arg_name in function_parms:
1011            # Get the arg value from frame locals.
1012            arg_value = frame_locals[arg_name]
1013            args_list.append(arg_name + " = " + repr(arg_value))
1014        args_str = "(" + ', '.join(map(str, args_list)) + ")"
1015
1016        # Now we need to print this in a nicely-wrapped way.
1017        func_and_args = func_name + " " + args_str
1018
1019    return sprint_time() + "Executing: " + func_and_args + "\n"
1020
1021###############################################################################
1022
1023
1024###############################################################################
1025def sprint_pgm_header(indent=0,
1026                      linefeed=1):
1027
1028    r"""
1029    Return a standardized header that programs should print at the beginning
1030    of the run.  It includes useful information like command line, pid,
1031    userid, program parameters, etc.
1032
1033    Description of arguments:
1034    indent                          The number of characters to indent each
1035                                    line of output.
1036    linefeed                        Indicates whether a line feed be included
1037                                    at the beginning and end of the report.
1038    """
1039
1040    loc_col1_width = col1_width + indent
1041
1042    buffer = ""
1043    if linefeed:
1044        buffer = "\n"
1045
1046    if robot_env:
1047        suite_name = BuiltIn().get_variable_value("${suite_name}")
1048        buffer += sindent(sprint_time("Running test suite \"" + suite_name +
1049                          "\".\n"), indent)
1050
1051    buffer += sindent(sprint_time() + "Running " + pgm_name + ".\n", indent)
1052    buffer += sindent(sprint_time() + "Program parameter values, etc.:\n\n",
1053                      indent)
1054    buffer += sprint_varx("command_line", ' '.join(sys.argv), 0, indent,
1055                          loc_col1_width)
1056    # We want the output to show a customized name for the pid and pgid but
1057    # we want it to look like a valid variable name.  Therefore, we'll use
1058    # pgm_name_var_name which was set when this module was imported.
1059    buffer += sprint_varx(pgm_name_var_name + "_pid", os.getpid(), 0, indent,
1060                          loc_col1_width)
1061    buffer += sprint_varx(pgm_name_var_name + "_pgid", os.getpgrp(), 0, indent,
1062                          loc_col1_width)
1063    userid_num = str(os.geteuid())
1064    try:
1065        username = os.getlogin()
1066    except OSError:
1067        if userid_num == "0":
1068            username = "root"
1069        else:
1070            username = "?"
1071    buffer += sprint_varx("uid", userid_num + " (" + username +
1072                          ")", 0, indent, loc_col1_width)
1073    buffer += sprint_varx("gid", str(os.getgid()) + " (" +
1074                          str(grp.getgrgid(os.getgid()).gr_name) + ")", 0,
1075                          indent, loc_col1_width)
1076    buffer += sprint_varx("host_name", socket.gethostname(), 0, indent,
1077                          loc_col1_width)
1078    try:
1079        DISPLAY = os.environ['DISPLAY']
1080    except KeyError:
1081        DISPLAY = ""
1082    buffer += sprint_varx("DISPLAY", DISPLAY, 0, indent,
1083                          loc_col1_width)
1084    # I want to add code to print caller's parms.
1085
1086    # __builtin__.arg_obj is created by the get_arg module function,
1087    # gen_get_options.
1088    try:
1089        buffer += ga.sprint_args(__builtin__.arg_obj, indent)
1090    except AttributeError:
1091        pass
1092
1093    if robot_env:
1094        # Get value of global parm_list.
1095        parm_list = BuiltIn().get_variable_value("${parm_list}")
1096
1097        for parm in parm_list:
1098            parm_value = BuiltIn().get_variable_value("${" + parm + "}")
1099            buffer += sprint_varx(parm, parm_value, 0, indent, loc_col1_width)
1100
1101        # Setting global program_pid.
1102        BuiltIn().set_global_variable("${program_pid}", os.getpid())
1103
1104    if linefeed:
1105        buffer += "\n"
1106
1107    return buffer
1108
1109###############################################################################
1110
1111
1112###############################################################################
1113def sprint_error_report(error_text="\n",
1114                        indent=2,
1115                        format=None):
1116
1117    r"""
1118    Return a string with a standardized report which includes the caller's
1119    error text, the call stack and the program header.
1120
1121    Description of args:
1122    error_text                      The error text to be included in the
1123                                    report.  The caller should include any
1124                                    needed linefeeds.
1125    indent                          The number of characters to indent each
1126                                    line of output.
1127    format                          Long or short format.  Long includes
1128                                    extras like lines of dashes, call stack,
1129                                    etc.
1130    """
1131
1132    # Process input.
1133    indent = int(indent)
1134    if format is None:
1135        if robot_env:
1136            format = 'short'
1137        else:
1138            format = 'long'
1139    error_text = error_text.rstrip('\n') + '\n'
1140
1141    if format == 'short':
1142        return sprint_error(error_text)
1143
1144    buffer = ""
1145    buffer += sprint_dashes(width=120, char="=")
1146    buffer += sprint_error(error_text)
1147    buffer += "\n"
1148    # Calling sprint_call_stack with stack_frame_ix of 0 causes it to show
1149    # itself and this function in the call stack.  This is not helpful to a
1150    # debugger and is therefore clutter.  We will adjust the stack_frame_ix to
1151    # hide that information.
1152    stack_frame_ix = 2
1153    caller_func_name = sprint_func_name(2)
1154    if caller_func_name.endswith("print_error_report"):
1155        stack_frame_ix += 1
1156    if not robot_env:
1157        buffer += sprint_call_stack(indent, stack_frame_ix)
1158    buffer += sprint_pgm_header(indent)
1159    buffer += sprint_dashes(width=120, char="=")
1160
1161    return buffer
1162
1163###############################################################################
1164
1165
1166###############################################################################
1167def sprint_issuing(cmd_buf,
1168                   test_mode=0):
1169
1170    r"""
1171    Return a line indicating a command that the program is about to execute.
1172
1173    Sample output for a cmd_buf of "ls"
1174
1175    #(CDT) 2016/08/25 17:57:36 - Issuing: ls
1176
1177    Description of args:
1178    cmd_buf                         The command to be executed by caller.
1179    test_mode                       With test_mode set, your output will look
1180                                    like this:
1181
1182    #(CDT) 2016/08/25 17:57:36 - (test_mode) Issuing: ls
1183
1184    """
1185
1186    buffer = sprint_time()
1187    if test_mode:
1188        buffer += "(test_mode) "
1189    buffer += "Issuing: " + cmd_buf + "\n"
1190
1191    return buffer
1192
1193###############################################################################
1194
1195
1196###############################################################################
1197def sprint_pgm_footer():
1198
1199    r"""
1200    Return a standardized footer that programs should print at the end of the
1201    program run.  It includes useful information like total run time, etc.
1202    """
1203
1204    buffer = "\n" + sprint_time() + "Finished running " + pgm_name + ".\n\n"
1205
1206    total_time = time.time() - start_time
1207    total_time_string = "%0.6f" % total_time
1208
1209    buffer += sprint_varx(pgm_name_var_name + "_runtime", total_time_string)
1210    buffer += "\n"
1211
1212    return buffer
1213
1214###############################################################################
1215
1216
1217###############################################################################
1218def sprint(buffer=""):
1219
1220    r"""
1221    Simply return the user's buffer.  This function is used by the qprint and
1222    dprint functions defined dynamically below, i.e. it would not normally be
1223    called for general use.
1224
1225    Description of arguments.
1226    buffer                          This will be returned to the caller.
1227    """
1228
1229    return str(buffer)
1230
1231###############################################################################
1232
1233
1234###############################################################################
1235def sprintn(buffer=""):
1236
1237    r"""
1238    Simply return the user's buffer with a line feed.  This function is used
1239    by the qprint and dprint functions defined dynamically below, i.e. it
1240    would not normally be called for general use.
1241
1242    Description of arguments.
1243    buffer                          This will be returned to the caller.
1244    """
1245
1246    buffer = str(buffer) + "\n"
1247
1248    return buffer
1249
1250###############################################################################
1251
1252
1253###############################################################################
1254def gp_debug_print(buffer):
1255
1256    r"""
1257    Print buffer to stdout only if gen_print_debug is set.
1258
1259    This function is intended for use only by other functions in this module.
1260
1261    Description of arguments:
1262    buffer                          The string to be printed.
1263    """
1264
1265    if not gen_print_debug:
1266        return
1267
1268    if robot_env:
1269        BuiltIn().log_to_console(buffer)
1270    else:
1271        print(buffer)
1272
1273###############################################################################
1274
1275
1276###############################################################################
1277def get_var_value(var_value=None,
1278                  default=1,
1279                  var_name=None):
1280
1281    r"""
1282    Return either var_value, the corresponding global value or default.
1283
1284    If var_value is not None, it will simply be returned.
1285
1286    If var_value is None, this function will return the corresponding global
1287    value of the variable in question.
1288
1289    Note: For global values, if we are in a robot environment,
1290    get_variable_value will be used.  Otherwise, the __builtin__ version of
1291    the variable is returned (which are set by gen_arg.py functions).
1292
1293    If there is no global value associated with the variable, default is
1294    returned.
1295
1296    This function is useful for other functions in setting default values for
1297    parameters.
1298
1299    Example use:
1300
1301    def my_func(quiet=None):
1302
1303      quiet = int(get_var_value(quiet, 0))
1304
1305    Example calls to my_func():
1306
1307    In the following example, the caller is explicitly asking to have quiet be
1308    set to 1.
1309
1310    my_func(quiet=1)
1311
1312    In the following example, quiet will be set to the global value of quiet,
1313    if defined, or to 0 (the default).
1314
1315    my_func()
1316
1317    Description of arguments:
1318    var_value                       The value to be returned (if not equal to
1319                                    None).
1320    default                         The value that is returned if var_value is
1321                                    None and there is no corresponding global
1322                                    value defined.
1323    var_name                        The name of the variable whose value is to
1324                                    be returned.  Under most circumstances,
1325                                    this value need not be provided.  This
1326                                    function can figure out the name of the
1327                                    variable passed as var_value.  One
1328                                    exception to this would be if this
1329                                    function is called directly from a .robot
1330                                    file.
1331    """
1332
1333    if var_value is not None:
1334        return var_value
1335
1336    if var_name is None:
1337        var_name = get_arg_name(None, 1, 2)
1338
1339    if robot_env:
1340        var_value = BuiltIn().get_variable_value("${" + var_name + "}",
1341                                                 default)
1342    else:
1343        var_value = getattr(__builtin__, var_name, default)
1344
1345    return var_value
1346
1347###############################################################################
1348
1349
1350# hidden_text is a list of passwords which are to be replaced with asterisks
1351# by print functions defined in this module.
1352hidden_text = []
1353# password_regex is created based on the contents of hidden_text.
1354password_regex = ""
1355
1356
1357###############################################################################
1358def register_passwords(*args):
1359
1360    r"""
1361    Register one or more passwords which are to be hidden in output produced
1362    by the print functions in this module.
1363
1364    Note:  Blank password values are NOT registered.  They are simply ignored.
1365
1366    Description of argument(s):
1367    args                            One or more password values.  If a given
1368                                    password value is already registered, this
1369                                    function will simply do nothing.
1370    """
1371
1372    global hidden_text
1373    global password_regex
1374
1375    for password in args:
1376        if password == "":
1377            break
1378        if password in hidden_text:
1379            break
1380
1381        # Place the password into the hidden_text list.
1382        hidden_text.append(password)
1383        # Create a corresponding password regular expression.  Escape regex
1384        # special characters too.
1385        password_regex = '(' +\
1386            '|'.join([re.escape(x) for x in hidden_text]) + ')'
1387
1388###############################################################################
1389
1390
1391###############################################################################
1392def replace_passwords(buffer):
1393
1394    r"""
1395    Return the buffer but with all registered passwords replaced by a string
1396    of asterisks.
1397
1398
1399    Description of argument(s):
1400    buffer                          The string to be returned but with
1401                                    passwords replaced.
1402    """
1403
1404    global password_regex
1405
1406    if int(os.environ.get("DEBUG_SHOW_PASSWORDS", "0")):
1407        return buffer
1408
1409    if password_regex == "":
1410        # No passwords to replace.
1411        return buffer
1412
1413    return re.sub(password_regex, "********", buffer)
1414
1415###############################################################################
1416
1417
1418###############################################################################
1419# In the following section of code, we will dynamically create print versions
1420# for each of the sprint functions defined above.  So, for example, where we
1421# have an sprint_time() function defined above that returns the time to the
1422# caller in a string, we will create a corresponding print_time() function
1423# that will print that string directly to stdout.
1424
1425# It can be complicated to follow what's being created by the exec statements
1426# below.  Here is an example of the print_time() function that will be created:
1427
1428# def print_time(*args):
1429#     s_func = getattr(sys.modules[__name__], "sprint_time")
1430#     sys.stdout.write(s_func(*args))
1431#     sys.stdout.flush()
1432
1433# Here are comments describing the 3 lines in the body of the created function.
1434# Create a reference to the "s" version of the given function in s_func (e.g.
1435# if this function name is print_time, we want s_funcname to be "sprint_time").
1436# Call the "s" version of this function passing it all of our arguments.
1437# Write the result to stdout.
1438
1439# func_names contains a list of all print functions which should be created
1440# from their sprint counterparts.
1441func_names = ['print_time', 'print_timen', 'print_error', 'print_varx',
1442              'print_var', 'print_vars', 'print_dashes', 'indent',
1443              'print_call_stack', 'print_func_name', 'print_executing',
1444              'print_pgm_header', 'print_issuing', 'print_pgm_footer',
1445              'print_error_report', 'print', 'printn']
1446
1447# stderr_func_names is a list of functions whose output should go to stderr
1448# rather than stdout.
1449stderr_func_names = ['print_error', 'print_error_report']
1450
1451gp_debug_print("robot_env: " + str(robot_env))
1452for func_name in func_names:
1453    gp_debug_print("func_name: " + func_name)
1454    if func_name in stderr_func_names:
1455        output_stream = "stderr"
1456    else:
1457        output_stream = "stdout"
1458
1459    func_def_line = "def " + func_name + "(*args):"
1460    s_func_line = "    s_func = getattr(sys.modules[__name__], \"s" +\
1461        func_name + "\")"
1462    # Generate the code to do the printing.
1463    if robot_env:
1464        func_print_lines = \
1465            [
1466                "    BuiltIn().log_to_console(replace_passwords" +
1467                "(s_func(*args)),"
1468                " stream='" + output_stream + "',"
1469                " no_newline=True)"
1470            ]
1471    else:
1472        func_print_lines = \
1473            [
1474                "    sys." + output_stream +
1475                ".write(replace_passwords(s_func(*args)))",
1476                "    sys." + output_stream + ".flush()"
1477            ]
1478
1479    # Create an array containing the lines of the function we wish to create.
1480    func_def = [func_def_line, s_func_line] + func_print_lines
1481    # We don't want to try to redefine the "print" function, thus the if
1482    # statement.
1483    if func_name != "print":
1484        pgm_definition_string = '\n'.join(func_def)
1485        gp_debug_print(pgm_definition_string)
1486        exec(pgm_definition_string)
1487
1488    # Insert a blank line which will be overwritten by the next several
1489    # definitions.
1490    func_def.insert(1, "")
1491
1492    # Define the "q" (i.e. quiet) version of the given print function.
1493    func_def[0] = "def q" + func_name + "(*args):"
1494    func_def[1] = "    if int(get_var_value(None, 0, \"quiet\")): return"
1495    pgm_definition_string = '\n'.join(func_def)
1496    gp_debug_print(pgm_definition_string)
1497    exec(pgm_definition_string)
1498
1499    # Define the "d" (i.e. debug) version of the given print function.
1500    func_def[0] = "def d" + func_name + "(*args):"
1501    func_def[1] = "    if not int(get_var_value(None, 0, \"debug\")): return"
1502    pgm_definition_string = '\n'.join(func_def)
1503    gp_debug_print(pgm_definition_string)
1504    exec(pgm_definition_string)
1505
1506    # Define the "l" (i.e. log) version of the given print function.
1507    func_def_line = "def l" + func_name + "(*args):"
1508    func_print_lines = \
1509        [
1510            "    logging.log(getattr(logging, 'INFO'), s_func(*args))"
1511        ]
1512
1513    func_def = [func_def_line, s_func_line] + func_print_lines
1514    if func_name != "print_varx" and func_name != "print_var":
1515        pgm_definition_string = '\n'.join(func_def)
1516        gp_debug_print(pgm_definition_string)
1517        exec(pgm_definition_string)
1518
1519    if func_name == "print" or func_name == "printn":
1520        gp_debug_print("")
1521        continue
1522
1523    # Create abbreviated aliases (e.g. spvar is an alias for sprint_var).
1524    alias = re.sub("print_", "p", func_name)
1525    prefixes = ["", "s", "q", "d", "l"]
1526    for prefix in prefixes:
1527        pgm_definition_string = prefix + alias + " = " + prefix + func_name
1528        gp_debug_print(pgm_definition_string)
1529        exec(pgm_definition_string)
1530
1531    gp_debug_print("")
1532
1533###############################################################################
1534