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    else:
606        # The data type is complex in the sense that it has subordinate parts.
607        format_string = "%" + str(loc_col1_indent) + "s%s\n"
608        buffer = format_string % ("", var_name + ":")
609        loc_col1_indent += 2
610        try:
611            length = len(var_value)
612        except TypeError:
613            length = 0
614        ix = 0
615        loc_trailing_char = "\n"
616        type_is_dict = 0
617        if type(var_value) is dict:
618            type_is_dict = 1
619        try:
620            if type(var_value) is collections.OrderedDict:
621                type_is_dict = 1
622        except AttributeError:
623            pass
624        try:
625            if type(var_value) is DotDict:
626                type_is_dict = 1
627        except NameError:
628            pass
629        try:
630            if type(var_value) is NormalizedDict:
631                type_is_dict = 1
632        except NameError:
633            pass
634        if type_is_dict:
635            for key, value in var_value.iteritems():
636                ix += 1
637                if ix == length:
638                    loc_trailing_char = trailing_char
639                if hex:
640                    # Since hex is being used as a format type, we want it
641                    # turned off when processing integer dictionary values so
642                    # it is not interpreted as a hex indicator.
643                    loc_hex = not (type(value) is int)
644                    buffer += sprint_varx(key, value,
645                                          loc_hex, loc_col1_indent,
646                                          loc_col1_width,
647                                          loc_trailing_char)
648                else:
649                    buffer += sprint_varx(var_name + "[" + key + "]", value,
650                                          hex, loc_col1_indent, loc_col1_width,
651                                          loc_trailing_char)
652        elif type(var_value) in (list, tuple, set):
653            for key, value in enumerate(var_value):
654                ix += 1
655                if ix == length:
656                    loc_trailing_char = trailing_char
657                buffer += sprint_varx(var_name + "[" + str(key) + "]", value,
658                                      hex, loc_col1_indent, loc_col1_width,
659                                      loc_trailing_char)
660        elif type(var_value) is argparse.Namespace:
661            for key in var_value.__dict__:
662                ix += 1
663                if ix == length:
664                    loc_trailing_char = trailing_char
665                cmd_buf = "buffer += sprint_varx(var_name + \".\" + str(key)" \
666                          + ", var_value." + key + ", hex, loc_col1_indent," \
667                          + " loc_col1_width, loc_trailing_char)"
668                exec(cmd_buf)
669        else:
670            var_type = type(var_value).__name__
671            func_name = sys._getframe().f_code.co_name
672            var_value = "<" + var_type + " type not supported by " + \
673                        func_name + "()>"
674            value_format = "%s"
675            loc_col1_indent -= 2
676            # Adjust loc_col1_width.
677            loc_col1_width = loc_col1_width - loc_col1_indent
678            format_string = "%" + str(loc_col1_indent) + "s%-" \
679                + str(loc_col1_width) + "s" + value_format + trailing_char
680            return format_string % ("", str(var_name) + ":", var_value)
681
682        return buffer
683
684    return ""
685
686###############################################################################
687
688
689###############################################################################
690def sprint_var(*args):
691
692    r"""
693    Figure out the name of the first argument for you and then call
694    sprint_varx with it.  Therefore, the following 2 calls are equivalent:
695    sprint_varx("var1", var1)
696    sprint_var(var1)
697    """
698
699    # Get the name of the first variable passed to this function.
700    stack_frame = 2
701    caller_func_name = sprint_func_name(2)
702    if caller_func_name.endswith("print_var"):
703        stack_frame += 1
704    var_name = get_arg_name(None, 1, stack_frame)
705    return sprint_varx(var_name, *args)
706
707###############################################################################
708
709
710###############################################################################
711def sprint_vars(*args):
712
713    r"""
714    Sprint the values of one or more variables.
715
716    Description of args:
717    args:
718        If the first argument is an integer, it will be interpreted to be the
719        "indent" value.
720        If the second argument is an integer, it will be interpreted to be the
721        "col1_width" value.
722        If the third argument is an integer, it will be interpreted to be the
723        "hex" value.
724        All remaining parms are considered variable names which are to be
725        sprinted.
726    """
727
728    if len(args) == 0:
729        return
730
731    # Get the name of the first variable passed to this function.
732    stack_frame = 2
733    caller_func_name = sprint_func_name(2)
734    if caller_func_name.endswith("print_vars"):
735        stack_frame += 1
736
737    parm_num = 1
738
739    # Create list from args (which is a tuple) so that it can be modified.
740    args_list = list(args)
741
742    var_name = get_arg_name(None, parm_num, stack_frame)
743    # See if parm 1 is to be interpreted as "indent".
744    try:
745        if type(int(var_name)) is int:
746            indent = int(var_name)
747            args_list.pop(0)
748            parm_num += 1
749    except ValueError:
750        indent = 0
751
752    var_name = get_arg_name(None, parm_num, stack_frame)
753    # See if parm 1 is to be interpreted as "col1_width".
754    try:
755        if type(int(var_name)) is int:
756            loc_col1_width = int(var_name)
757            args_list.pop(0)
758            parm_num += 1
759    except ValueError:
760        loc_col1_width = col1_width
761
762    var_name = get_arg_name(None, parm_num, stack_frame)
763    # See if parm 1 is to be interpreted as "hex".
764    try:
765        if type(int(var_name)) is int:
766            hex = int(var_name)
767            args_list.pop(0)
768            parm_num += 1
769    except ValueError:
770        hex = 0
771
772    buffer = ""
773    for var_value in args_list:
774        var_name = get_arg_name(None, parm_num, stack_frame)
775        buffer += sprint_varx(var_name, var_value, hex, indent, loc_col1_width)
776        parm_num += 1
777
778    return buffer
779
780###############################################################################
781
782
783###############################################################################
784def lprint_varx(var_name,
785                var_value,
786                hex=0,
787                loc_col1_indent=col1_indent,
788                loc_col1_width=col1_width,
789                log_level=getattr(logging, 'INFO')):
790
791    r"""
792    Send sprint_varx output to logging.
793    """
794
795    logging.log(log_level, sprint_varx(var_name, var_value, hex,
796                loc_col1_indent, loc_col1_width, ""))
797
798###############################################################################
799
800
801###############################################################################
802def lprint_var(*args):
803
804    r"""
805    Figure out the name of the first argument for you and then call
806    lprint_varx with it.  Therefore, the following 2 calls are equivalent:
807    lprint_varx("var1", var1)
808    lprint_var(var1)
809    """
810
811    # Get the name of the first variable passed to this function.
812    stack_frame = 2
813    caller_func_name = sprint_func_name(2)
814    if caller_func_name.endswith("print_var"):
815        stack_frame += 1
816    var_name = get_arg_name(None, 1, stack_frame)
817    lprint_varx(var_name, *args)
818
819###############################################################################
820
821
822###############################################################################
823def sprint_dashes(indent=col1_indent,
824                  width=80,
825                  line_feed=1,
826                  char="-"):
827
828    r"""
829    Return a string of dashes to the caller.
830
831    Description of arguments:
832    indent                          The number of characters to indent the
833                                    output.
834    width                           The width of the string of dashes.
835    line_feed                       Indicates whether the output should end
836                                    with a line feed.
837    char                            The character to be repeated in the output
838                                    string.
839    """
840
841    width = int(width)
842    buffer = " " * int(indent) + char * width
843    if line_feed:
844        buffer += "\n"
845
846    return buffer
847
848###############################################################################
849
850
851###############################################################################
852def sindent(text="",
853            indent=0):
854
855    r"""
856    Pre-pend the specified number of characters to the text string (i.e.
857    indent it) and return it.
858
859    Description of arguments:
860    text                            The string to be indented.
861    indent                          The number of characters to indent the
862                                    string.
863    """
864
865    format_string = "%" + str(indent) + "s%s"
866    buffer = format_string % ("", text)
867
868    return buffer
869
870###############################################################################
871
872
873###############################################################################
874def sprint_call_stack(indent=0,
875                      stack_frame_ix=0):
876
877    r"""
878    Return a call stack report for the given point in the program with line
879    numbers, function names and function parameters and arguments.
880
881    Sample output:
882
883    -------------------------------------------------------------------------
884    Python function call stack
885
886    Line # Function name and arguments
887    ------ ------------------------------------------------------------------
888       424 sprint_call_stack ()
889         4 print_call_stack ()
890        31 func1 (last_name = 'walsh', first_name = 'mikey')
891        59 /tmp/scr5.py
892    -------------------------------------------------------------------------
893
894    Description of arguments:
895    indent                          The number of characters to indent each
896                                    line of output.
897    stack_frame_ix                  The index of the first stack frame which
898                                    is to be returned.
899    """
900
901    buffer = ""
902    buffer += sprint_dashes(indent)
903    buffer += sindent("Python function call stack\n\n", indent)
904    buffer += sindent("Line # Function name and arguments\n", indent)
905    buffer += sprint_dashes(indent, 6, 0) + " " + sprint_dashes(0, 73)
906
907    # Grab the current program stack.
908    current_stack = inspect.stack()
909
910    # Process each frame in turn.
911    format_string = "%6s %s\n"
912    ix = 0
913    for stack_frame in current_stack:
914        if ix < stack_frame_ix:
915            ix += 1
916            continue
917        # I want the line number shown to be the line where you find the line
918        # shown.
919        try:
920            line_num = str(current_stack[ix + 1][2])
921        except IndexError:
922            line_num = ""
923        func_name = str(stack_frame[3])
924        if func_name == "?":
925            # "?" is the name used when code is not in a function.
926            func_name = "(none)"
927
928        if func_name == "<module>":
929            # If the func_name is the "main" program, we simply get the
930            # command line call string.
931            func_and_args = ' '.join(sys.argv)
932        else:
933            # Get the program arguments.
934            arg_vals = inspect.getargvalues(stack_frame[0])
935            function_parms = arg_vals[0]
936            frame_locals = arg_vals[3]
937
938            args_list = []
939            for arg_name in function_parms:
940                # Get the arg value from frame locals.
941                arg_value = frame_locals[arg_name]
942                args_list.append(arg_name + " = " + repr(arg_value))
943            args_str = "(" + ', '.join(map(str, args_list)) + ")"
944
945            # Now we need to print this in a nicely-wrapped way.
946            func_and_args = func_name + " " + args_str
947
948        buffer += sindent(format_string % (line_num, func_and_args), indent)
949        ix += 1
950
951    buffer += sprint_dashes(indent)
952
953    return buffer
954
955###############################################################################
956
957
958###############################################################################
959def sprint_executing(stack_frame_ix=None):
960
961    r"""
962    Print a line indicating what function is executing and with what parameter
963    values.  This is useful for debugging.
964
965    Sample output:
966
967    #(CDT) 2016/08/25 17:54:27 - Executing: func1 (x = 1)
968
969    Description of arguments:
970    stack_frame_ix                  The index of the stack frame whose
971                                    function info should be returned.  If the
972                                    caller does not specify a value, this
973                                    function will set the value to 1 which is
974                                    the index of the caller's stack frame.  If
975                                    the caller is the wrapper function
976                                    "print_executing", this function will bump
977                                    it up by 1.
978    """
979
980    # If user wants default stack_frame_ix.
981    if stack_frame_ix is None:
982        func_name = sys._getframe().f_code.co_name
983        caller_func_name = sys._getframe(1).f_code.co_name
984        if caller_func_name.endswith(func_name[1:]):
985            stack_frame_ix = 2
986        else:
987            stack_frame_ix = 1
988
989    stack_frame = inspect.stack()[stack_frame_ix]
990
991    func_name = str(stack_frame[3])
992    if func_name == "?":
993        # "?" is the name used when code is not in a function.
994        func_name = "(none)"
995
996    if func_name == "<module>":
997        # If the func_name is the "main" program, we simply get the command
998        # line call string.
999        func_and_args = ' '.join(sys.argv)
1000    else:
1001        # Get the program arguments.
1002        arg_vals = inspect.getargvalues(stack_frame[0])
1003        function_parms = arg_vals[0]
1004        frame_locals = arg_vals[3]
1005
1006        args_list = []
1007        for arg_name in function_parms:
1008            # Get the arg value from frame locals.
1009            arg_value = frame_locals[arg_name]
1010            args_list.append(arg_name + " = " + repr(arg_value))
1011        args_str = "(" + ', '.join(map(str, args_list)) + ")"
1012
1013        # Now we need to print this in a nicely-wrapped way.
1014        func_and_args = func_name + " " + args_str
1015
1016    return sprint_time() + "Executing: " + func_and_args + "\n"
1017
1018###############################################################################
1019
1020
1021###############################################################################
1022def sprint_pgm_header(indent=0,
1023                      linefeed=1):
1024
1025    r"""
1026    Return a standardized header that programs should print at the beginning
1027    of the run.  It includes useful information like command line, pid,
1028    userid, program parameters, etc.
1029
1030    Description of arguments:
1031    indent                          The number of characters to indent each
1032                                    line of output.
1033    linefeed                        Indicates whether a line feed be included
1034                                    at the beginning and end of the report.
1035    """
1036
1037    loc_col1_width = col1_width + indent
1038
1039    buffer = ""
1040    if linefeed:
1041        buffer = "\n"
1042
1043    if robot_env:
1044        suite_name = BuiltIn().get_variable_value("${suite_name}")
1045        buffer += sindent(sprint_time("Running test suite \"" + suite_name +
1046                          "\".\n"), indent)
1047
1048    buffer += sindent(sprint_time() + "Running " + pgm_name + ".\n", indent)
1049    buffer += sindent(sprint_time() + "Program parameter values, etc.:\n\n",
1050                      indent)
1051    buffer += sprint_varx("command_line", ' '.join(sys.argv), 0, indent,
1052                          loc_col1_width)
1053    # We want the output to show a customized name for the pid and pgid but
1054    # we want it to look like a valid variable name.  Therefore, we'll use
1055    # pgm_name_var_name which was set when this module was imported.
1056    buffer += sprint_varx(pgm_name_var_name + "_pid", os.getpid(), 0, indent,
1057                          loc_col1_width)
1058    buffer += sprint_varx(pgm_name_var_name + "_pgid", os.getpgrp(), 0, indent,
1059                          loc_col1_width)
1060    userid_num = str(os.geteuid())
1061    try:
1062        username = os.getlogin()
1063    except OSError:
1064        if userid_num == "0":
1065            username = "root"
1066        else:
1067            username = "?"
1068    buffer += sprint_varx("uid", userid_num + " (" + username +
1069                          ")", 0, indent, loc_col1_width)
1070    buffer += sprint_varx("gid", str(os.getgid()) + " (" +
1071                          str(grp.getgrgid(os.getgid()).gr_name) + ")", 0,
1072                          indent, loc_col1_width)
1073    buffer += sprint_varx("host_name", socket.gethostname(), 0, indent,
1074                          loc_col1_width)
1075    try:
1076        DISPLAY = os.environ['DISPLAY']
1077    except KeyError:
1078        DISPLAY = ""
1079    buffer += sprint_varx("DISPLAY", DISPLAY, 0, indent,
1080                          loc_col1_width)
1081    # I want to add code to print caller's parms.
1082
1083    # __builtin__.arg_obj is created by the get_arg module function,
1084    # gen_get_options.
1085    try:
1086        buffer += ga.sprint_args(__builtin__.arg_obj, indent)
1087    except AttributeError:
1088        pass
1089
1090    if robot_env:
1091        # Get value of global parm_list.
1092        parm_list = BuiltIn().get_variable_value("${parm_list}")
1093
1094        for parm in parm_list:
1095            parm_value = BuiltIn().get_variable_value("${" + parm + "}")
1096            buffer += sprint_varx(parm, parm_value, 0, indent, loc_col1_width)
1097
1098        # Setting global program_pid.
1099        BuiltIn().set_global_variable("${program_pid}", os.getpid())
1100
1101    if linefeed:
1102        buffer += "\n"
1103
1104    return buffer
1105
1106###############################################################################
1107
1108
1109###############################################################################
1110def sprint_error_report(error_text="\n",
1111                        indent=2,
1112                        format=None):
1113
1114    r"""
1115    Return a string with a standardized report which includes the caller's
1116    error text, the call stack and the program header.
1117
1118    Description of args:
1119    error_text                      The error text to be included in the
1120                                    report.  The caller should include any
1121                                    needed linefeeds.
1122    indent                          The number of characters to indent each
1123                                    line of output.
1124    format                          Long or short format.  Long includes
1125                                    extras like lines of dashes, call stack,
1126                                    etc.
1127    """
1128
1129    # Process input.
1130    indent = int(indent)
1131    if format is None:
1132        if robot_env:
1133            format = 'short'
1134        else:
1135            format = 'long'
1136    error_text = error_text.rstrip('\n') + '\n'
1137
1138    if format == 'short':
1139        return sprint_error(error_text)
1140
1141    buffer = ""
1142    buffer += sprint_dashes(width=120, char="=")
1143    buffer += sprint_error(error_text)
1144    buffer += "\n"
1145    # Calling sprint_call_stack with stack_frame_ix of 0 causes it to show
1146    # itself and this function in the call stack.  This is not helpful to a
1147    # debugger and is therefore clutter.  We will adjust the stack_frame_ix to
1148    # hide that information.
1149    stack_frame_ix = 2
1150    caller_func_name = sprint_func_name(2)
1151    if caller_func_name.endswith("print_error_report"):
1152        stack_frame_ix += 1
1153    if not robot_env:
1154        buffer += sprint_call_stack(indent, stack_frame_ix)
1155    buffer += sprint_pgm_header(indent)
1156    buffer += sprint_dashes(width=120, char="=")
1157
1158    return buffer
1159
1160###############################################################################
1161
1162
1163###############################################################################
1164def sprint_issuing(cmd_buf,
1165                   test_mode=0):
1166
1167    r"""
1168    Return a line indicating a command that the program is about to execute.
1169
1170    Sample output for a cmd_buf of "ls"
1171
1172    #(CDT) 2016/08/25 17:57:36 - Issuing: ls
1173
1174    Description of args:
1175    cmd_buf                         The command to be executed by caller.
1176    test_mode                       With test_mode set, your output will look
1177                                    like this:
1178
1179    #(CDT) 2016/08/25 17:57:36 - (test_mode) Issuing: ls
1180
1181    """
1182
1183    buffer = sprint_time()
1184    if test_mode:
1185        buffer += "(test_mode) "
1186    buffer += "Issuing: " + cmd_buf + "\n"
1187
1188    return buffer
1189
1190###############################################################################
1191
1192
1193###############################################################################
1194def sprint_pgm_footer():
1195
1196    r"""
1197    Return a standardized footer that programs should print at the end of the
1198    program run.  It includes useful information like total run time, etc.
1199    """
1200
1201    buffer = "\n" + sprint_time() + "Finished running " + pgm_name + ".\n\n"
1202
1203    total_time = time.time() - start_time
1204    total_time_string = "%0.6f" % total_time
1205
1206    buffer += sprint_varx(pgm_name_var_name + "_runtime", total_time_string)
1207    buffer += "\n"
1208
1209    return buffer
1210
1211###############################################################################
1212
1213
1214###############################################################################
1215def sprint(buffer=""):
1216
1217    r"""
1218    Simply return the user's buffer.  This function is used by the qprint and
1219    dprint functions defined dynamically below, i.e. it would not normally be
1220    called for general use.
1221
1222    Description of arguments.
1223    buffer                          This will be returned to the caller.
1224    """
1225
1226    return str(buffer)
1227
1228###############################################################################
1229
1230
1231###############################################################################
1232def sprintn(buffer=""):
1233
1234    r"""
1235    Simply return the user's buffer with a line feed.  This function is used
1236    by the qprint and dprint functions defined dynamically below, i.e. it
1237    would not normally be called for general use.
1238
1239    Description of arguments.
1240    buffer                          This will be returned to the caller.
1241    """
1242
1243    buffer = str(buffer) + "\n"
1244
1245    return buffer
1246
1247###############################################################################
1248
1249
1250###############################################################################
1251def gp_debug_print(buffer):
1252
1253    r"""
1254    Print buffer to stdout only if gen_print_debug is set.
1255
1256    This function is intended for use only by other functions in this module.
1257
1258    Description of arguments:
1259    buffer                          The string to be printed.
1260    """
1261
1262    if not gen_print_debug:
1263        return
1264
1265    if robot_env:
1266        BuiltIn().log_to_console(buffer)
1267    else:
1268        print(buffer)
1269
1270###############################################################################
1271
1272
1273###############################################################################
1274def get_var_value(var_value=None,
1275                  default=1,
1276                  var_name=None):
1277
1278    r"""
1279    Return either var_value, the corresponding global value or default.
1280
1281    If var_value is not None, it will simply be returned.
1282
1283    If var_value is None, this function will return the corresponding global
1284    value of the variable in question.
1285
1286    Note: For global values, if we are in a robot environment,
1287    get_variable_value will be used.  Otherwise, the __builtin__ version of
1288    the variable is returned (which are set by gen_arg.py functions).
1289
1290    If there is no global value associated with the variable, default is
1291    returned.
1292
1293    This function is useful for other functions in setting default values for
1294    parameters.
1295
1296    Example use:
1297
1298    def my_func(quiet=None):
1299
1300      quiet = int(get_var_value(quiet, 0))
1301
1302    Example calls to my_func():
1303
1304    In the following example, the caller is explicitly asking to have quiet be
1305    set to 1.
1306
1307    my_func(quiet=1)
1308
1309    In the following example, quiet will be set to the global value of quiet,
1310    if defined, or to 0 (the default).
1311
1312    my_func()
1313
1314    Description of arguments:
1315    var_value                       The value to be returned (if not equal to
1316                                    None).
1317    default                         The value that is returned if var_value is
1318                                    None and there is no corresponding global
1319                                    value defined.
1320    var_name                        The name of the variable whose value is to
1321                                    be returned.  Under most circumstances,
1322                                    this value need not be provided.  This
1323                                    function can figure out the name of the
1324                                    variable passed as var_value.  One
1325                                    exception to this would be if this
1326                                    function is called directly from a .robot
1327                                    file.
1328    """
1329
1330    if var_value is not None:
1331        return var_value
1332
1333    if var_name is None:
1334        var_name = get_arg_name(None, 1, 2)
1335
1336    if robot_env:
1337        var_value = int(BuiltIn().get_variable_value("${" + var_name + "}",
1338                        default))
1339    else:
1340        var_value = getattr(__builtin__, var_name, default)
1341
1342    return var_value
1343
1344###############################################################################
1345
1346
1347# hidden_text is a list of passwords which are to be replaced with asterisks
1348# by print functions defined in this module.
1349hidden_text = []
1350# password_regex is created based on the contents of hidden_text.
1351password_regex = ""
1352
1353
1354###############################################################################
1355def register_passwords(*args):
1356
1357    r"""
1358    Register one or more passwords which are to be hidden in output produced
1359    by the print functions in this module.
1360
1361    Note:  Blank password values are NOT registered.  They are simply ignored.
1362
1363    Description of argument(s):
1364    args                            One or more password values.  If a given
1365                                    password value is already registered, this
1366                                    function will simply do nothing.
1367    """
1368
1369    global hidden_text
1370    global password_regex
1371
1372    for password in args:
1373        if password == "":
1374            break
1375        if password in hidden_text:
1376            break
1377
1378        # Place the password into the hidden_text list.
1379        hidden_text.append(password)
1380        # Create a corresponding password regular expression.  Escape regex
1381        # special characters too.
1382        password_regex = '(' +\
1383            '|'.join([re.escape(x) for x in hidden_text]) + ')'
1384
1385###############################################################################
1386
1387
1388###############################################################################
1389def replace_passwords(buffer):
1390
1391    r"""
1392    Return the buffer but with all registered passwords replaced by a string
1393    of asterisks.
1394
1395
1396    Description of argument(s):
1397    buffer                          The string to be returned but with
1398                                    passwords replaced.
1399    """
1400
1401    global password_regex
1402
1403    if int(os.environ.get("DEBUG_SHOW_PASSWORDS", "0")):
1404        return buffer
1405
1406    if password_regex == "":
1407        # No passwords to replace.
1408        return buffer
1409
1410    return re.sub(password_regex, "********", buffer)
1411
1412###############################################################################
1413
1414
1415###############################################################################
1416# In the following section of code, we will dynamically create print versions
1417# for each of the sprint functions defined above.  So, for example, where we
1418# have an sprint_time() function defined above that returns the time to the
1419# caller in a string, we will create a corresponding print_time() function
1420# that will print that string directly to stdout.
1421
1422# It can be complicated to follow what's being created by the exec statements
1423# below.  Here is an example of the print_time() function that will be created:
1424
1425# def print_time(*args):
1426#     s_func = getattr(sys.modules[__name__], "sprint_time")
1427#     sys.stdout.write(s_func(*args))
1428#     sys.stdout.flush()
1429
1430# Here are comments describing the 3 lines in the body of the created function.
1431# Create a reference to the "s" version of the given function in s_func (e.g.
1432# if this function name is print_time, we want s_funcname to be "sprint_time").
1433# Call the "s" version of this function passing it all of our arguments.
1434# Write the result to stdout.
1435
1436# func_names contains a list of all print functions which should be created
1437# from their sprint counterparts.
1438func_names = ['print_time', 'print_timen', 'print_error', 'print_varx',
1439              'print_var', 'print_vars', 'print_dashes', 'indent',
1440              'print_call_stack', 'print_func_name', 'print_executing',
1441              'print_pgm_header', 'print_issuing', 'print_pgm_footer',
1442              'print_error_report', 'print', 'printn']
1443
1444# stderr_func_names is a list of functions whose output should go to stderr
1445# rather than stdout.
1446stderr_func_names = ['print_error', 'print_error_report']
1447
1448gp_debug_print("robot_env: " + str(robot_env))
1449for func_name in func_names:
1450    gp_debug_print("func_name: " + func_name)
1451    if func_name in stderr_func_names:
1452        output_stream = "stderr"
1453    else:
1454        output_stream = "stdout"
1455
1456    func_def_line = "def " + func_name + "(*args):"
1457    s_func_line = "    s_func = getattr(sys.modules[__name__], \"s" +\
1458        func_name + "\")"
1459    # Generate the code to do the printing.
1460    if robot_env:
1461        func_print_lines = \
1462            [
1463                "    BuiltIn().log_to_console(replace_passwords" +
1464                "(s_func(*args)),"
1465                " stream='" + output_stream + "',"
1466                " no_newline=True)"
1467            ]
1468    else:
1469        func_print_lines = \
1470            [
1471                "    sys." + output_stream +
1472                ".write(replace_passwords(s_func(*args)))",
1473                "    sys." + output_stream + ".flush()"
1474            ]
1475
1476    # Create an array containing the lines of the function we wish to create.
1477    func_def = [func_def_line, s_func_line] + func_print_lines
1478    # We don't want to try to redefine the "print" function, thus the if
1479    # statement.
1480    if func_name != "print":
1481        pgm_definition_string = '\n'.join(func_def)
1482        gp_debug_print(pgm_definition_string)
1483        exec(pgm_definition_string)
1484
1485    # Insert a blank line which will be overwritten by the next several
1486    # definitions.
1487    func_def.insert(1, "")
1488
1489    # Define the "q" (i.e. quiet) version of the given print function.
1490    func_def[0] = "def q" + func_name + "(*args):"
1491    func_def[1] = "    if get_var_value(None, 0, \"quiet\"): return"
1492    pgm_definition_string = '\n'.join(func_def)
1493    gp_debug_print(pgm_definition_string)
1494    exec(pgm_definition_string)
1495
1496    # Define the "d" (i.e. debug) version of the given print function.
1497    func_def[0] = "def d" + func_name + "(*args):"
1498    func_def[1] = "    if not get_var_value(None, 0, \"debug\"): return"
1499    pgm_definition_string = '\n'.join(func_def)
1500    gp_debug_print(pgm_definition_string)
1501    exec(pgm_definition_string)
1502
1503    # Define the "l" (i.e. log) version of the given print function.
1504    func_def_line = "def l" + func_name + "(*args):"
1505    func_print_lines = \
1506        [
1507            "    logging.log(getattr(logging, 'INFO'), s_func(*args))"
1508        ]
1509
1510    func_def = [func_def_line, s_func_line] + func_print_lines
1511    if func_name != "print_varx" and func_name != "print_var":
1512        pgm_definition_string = '\n'.join(func_def)
1513        gp_debug_print(pgm_definition_string)
1514        exec(pgm_definition_string)
1515
1516    if func_name == "print" or func_name == "printn":
1517        gp_debug_print("")
1518        continue
1519
1520    # Create abbreviated aliases (e.g. spvar is an alias for sprint_var).
1521    alias = re.sub("print_", "p", func_name)
1522    prefixes = ["", "s", "q", "d", "l"]
1523    for prefix in prefixes:
1524        pgm_definition_string = prefix + alias + " = " + prefix + func_name
1525        gp_debug_print(pgm_definition_string)
1526        exec(pgm_definition_string)
1527
1528    gp_debug_print("")
1529
1530###############################################################################
1531