Michael Walsh | ced4eb0 | 2017-09-19 16:49:13 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | Define variable manipulation functions. |
| 5 | """ |
| 6 | |
| 7 | import os |
| 8 | |
| 9 | try: |
| 10 | from robot.utils import DotDict |
| 11 | except ImportError: |
| 12 | pass |
| 13 | |
| 14 | import collections |
| 15 | |
| 16 | import gen_print as gp |
| 17 | import gen_misc as gm |
| 18 | |
| 19 | |
| 20 | def create_var_dict(*args): |
| 21 | |
| 22 | r""" |
| 23 | Create a dictionary whose keys/values are the arg names/arg values passed |
| 24 | to it and return it to the caller. |
| 25 | |
| 26 | Note: The resulting dictionary will be ordered. |
| 27 | |
| 28 | Description of argument(s): |
| 29 | *args An unlimited number of arguments to be processed. |
| 30 | |
| 31 | Example use: |
| 32 | |
| 33 | first_name = 'Steve' |
| 34 | last_name = 'Smith' |
| 35 | var_dict = create_var_dict(first_name, last_name) |
| 36 | |
| 37 | gp.print_var(var_dict) |
| 38 | |
| 39 | The print-out of the resulting var dictionary is: |
| 40 | var_dict: |
| 41 | var_dict[first_name]: Steve |
| 42 | var_dict[last_name]: Smith |
| 43 | """ |
| 44 | |
| 45 | try: |
| 46 | result_dict = collections.OrderedDict() |
| 47 | except AttributeError: |
| 48 | result_dict = DotDict() |
| 49 | |
| 50 | arg_num = 1 |
| 51 | for arg in args: |
| 52 | arg_name = gp.get_arg_name(None, arg_num, stack_frame_ix=2) |
| 53 | result_dict[arg_name] = arg |
| 54 | arg_num += 1 |
| 55 | |
| 56 | return result_dict |
| 57 | |
| 58 | |
| 59 | default_record_delim = ':' |
| 60 | default_key_val_delim = '.' |
| 61 | |
| 62 | |
| 63 | def join_dict(dict, |
| 64 | record_delim=default_record_delim, |
| 65 | key_val_delim=default_key_val_delim): |
| 66 | |
| 67 | r""" |
| 68 | Join a dictionary's keys and values into a string and return the string. |
| 69 | |
| 70 | Description of argument(s): |
| 71 | dict The dictionary whose keys and values are |
| 72 | to be joined. |
| 73 | record_delim The delimiter to be used to separate |
| 74 | dictionary pairs in the resulting string. |
| 75 | key_val_delim The delimiter to be used to separate keys |
| 76 | from values in the resulting string. |
| 77 | |
| 78 | Example use: |
| 79 | |
| 80 | gp.print_var(var_dict) |
| 81 | str1 = join_dict(var_dict) |
| 82 | gp.pvar(str1) |
| 83 | |
| 84 | Program output. |
| 85 | var_dict: |
| 86 | var_dict[first_name]: Steve |
| 87 | var_dict[last_name]: Smith |
| 88 | str1: |
| 89 | first_name.Steve:last_name.Smith |
| 90 | """ |
| 91 | |
| 92 | format_str = '%s' + key_val_delim + '%s' |
| 93 | return record_delim.join([format_str % (key, value) for (key, value) in |
| 94 | dict.items()]) |
| 95 | |
| 96 | |
| 97 | def split_to_dict(string, |
| 98 | record_delim=default_record_delim, |
| 99 | key_val_delim=default_key_val_delim): |
| 100 | |
| 101 | r""" |
| 102 | Split a string into a dictionary and return it. |
| 103 | |
| 104 | This function is the complement to join_dict. |
| 105 | |
| 106 | Description of argument(s): |
| 107 | string The string to be split into a dictionary. |
| 108 | The string must have the proper delimiters |
| 109 | in it. A string created by join_dict |
| 110 | would qualify. |
| 111 | record_delim The delimiter to be used to separate |
| 112 | dictionary pairs in the input string. |
| 113 | key_val_delim The delimiter to be used to separate |
| 114 | keys/values in the input string. |
| 115 | |
| 116 | Example use: |
| 117 | |
| 118 | gp.print_var(str1) |
| 119 | new_dict = split_to_dict(str1) |
| 120 | gp.print_var(new_dict) |
| 121 | |
| 122 | |
| 123 | Program output. |
| 124 | str1: |
| 125 | first_name.Steve:last_name.Smith |
| 126 | new_dict: |
| 127 | new_dict[first_name]: Steve |
| 128 | new_dict[last_name]: Smith |
| 129 | """ |
| 130 | |
| 131 | try: |
| 132 | result_dict = collections.OrderedDict() |
| 133 | except AttributeError: |
| 134 | result_dict = DotDict() |
| 135 | |
| 136 | raw_keys_values = string.split(record_delim) |
| 137 | for key_value in raw_keys_values: |
| 138 | key_value_list = key_value.split(key_val_delim) |
| 139 | try: |
| 140 | result_dict[key_value_list[0]] = key_value_list[1] |
| 141 | except IndexError: |
| 142 | result_dict[key_value_list[0]] = "" |
| 143 | |
| 144 | return result_dict |
| 145 | |
| 146 | |
| 147 | def create_file_path(file_name_dict, |
| 148 | dir_path="/tmp/", |
| 149 | file_suffix=""): |
| 150 | |
| 151 | r""" |
| 152 | Create a file path using the given parameters and return it. |
| 153 | |
| 154 | Description of argument(s): |
| 155 | file_name_dict A dictionary with keys/values which are to |
| 156 | appear as part of the file name. |
| 157 | dir_path The dir_path that is to appear as part of |
| 158 | the file name. |
| 159 | file_suffix A suffix to be included as part of the |
| 160 | file name. |
| 161 | """ |
| 162 | |
| 163 | dir_path = gm.add_trailing_slash(dir_path) |
| 164 | return dir_path + join_dict(file_name_dict) + file_suffix |
| 165 | |
| 166 | |
| 167 | def parse_file_path(file_path): |
| 168 | |
| 169 | r""" |
| 170 | Parse a file path created by create_file_path and return the result as a |
| 171 | dictionary. |
| 172 | |
| 173 | This function is the complement to create_file_path. |
| 174 | |
| 175 | Description of argument(s): |
| 176 | file_path The file_path. |
| 177 | |
| 178 | Example use: |
| 179 | gp.pvar(boot_results_file_path) |
| 180 | file_path_data = parse_file_path(boot_results_file_path) |
| 181 | gp.pvar(file_path_data) |
| 182 | |
| 183 | Program output. |
| 184 | |
| 185 | boot_results_file_path: |
| 186 | /tmp/pgm_name.obmc_boot_test:openbmc_nickname.beye6:master_pid.2039:boot_re |
| 187 | sults |
| 188 | file_path_data: |
| 189 | file_path_data[dir_path]: /tmp/ |
| 190 | file_path_data[pgm_name]: obmc_boot_test |
| 191 | file_path_data[openbmc_nickname]: beye6 |
| 192 | file_path_data[master_pid]: 2039 |
| 193 | file_path_data[boot_results]: |
| 194 | """ |
| 195 | |
| 196 | try: |
| 197 | result_dict = collections.OrderedDict() |
| 198 | except AttributeError: |
| 199 | result_dict = DotDict() |
| 200 | |
| 201 | dir_path = os.path.dirname(file_path) + os.sep |
| 202 | file_path = os.path.basename(file_path) |
| 203 | |
| 204 | result_dict['dir_path'] = dir_path |
| 205 | |
| 206 | result_dict.update(split_to_dict(file_path)) |
| 207 | |
| 208 | return result_dict |