def restore_plug_in_value(default=""): r""" Return a value from a plug-in save file. The name of the value to be restored will be determined by this function based on the lvalue being assigned. Consider the following example: my_var1 = restore_plug_in_value(2) In this example, this function would look for the "my_var1" file in the plug-in save directory, read its value and return it. If no such file exists, the default value of 2 would be returned. Description of argument(s): default The default value to be returned if there is no plug-in save file for the value in question. """ # Get the lvalue from the caller's invocation of this function. lvalue = gp.get_arg_name(0, -1, stack_frame_ix=2) plug_in_save_dir_path = create_plug_in_save_dir() save_file_path = plug_in_save_dir_path + lvalue if os.path.isfile(save_file_path): gp.qprint_timen("Restoring " + lvalue + " value from " + save_file_path + ".") return gm.file_to_list(save_file_path, newlines=0, comments=0, trim=1)[0] else: gp.qprint_timen("Save file " + save_file_path + " does not exist so returning default value.") return default
def restore_plug_in_value(*args, **kwargs): r""" Return a value from a plug-in save file. The args/kwargs are interpreted differently depending on how this function is called. Mode 1 - The output of this function is assigned to a variable: Example: my_var1 = restore_plug_in_value(2) In this mode, the lvalue ("my_var1" in this example) will serve as the name of the value to be restored. Mode 2 - The output of this function is NOT assigned to a variable: Example: if restore_plug_in_value('my_var1', 2): do_something() In this mode, the caller must explicitly provide the name of the value being restored. The args/kwargs are interpreted as follows: Description of argument(s): var_name The name of the value to be restored. Only relevant in mode 1 (see example above). default The default value to be returned if there is no plug-in save file for the value in question. plug_in_package_name See compose_plug_in_save_dir_path for details. """ # Process args. lvalue = gp.get_arg_name(0, -1, stack_frame_ix=2) if lvalue: var_name = lvalue else: var_name, args, kwargs = fa.pop_arg("", *args, **kwargs) default, args, kwargs = fa.pop_arg("", *args, **kwargs) plug_in_package_name, args, kwargs = fa.pop_arg(None, *args, **kwargs) if args or kwargs: error_message = "Programmer error - Too many arguments passed for this function." raise ValueError(error_message) plug_in_save_dir_path = create_plug_in_save_dir(plug_in_package_name) save_file_path = plug_in_save_dir_path + var_name if os.path.isfile(save_file_path): gp.qprint_timen("Restoring " + var_name + " value from " + save_file_path + ".") var_value = gm.file_to_list(save_file_path, newlines=0, comments=0, trim=1)[0] if type(default) is bool: # Convert from string to bool. var_value = (var_value == 'True') if type(default) is int: # Convert from string to int. var_value = int(var_value) else: var_value = default gp.qprint_timen("Save file " + save_file_path + " does not exist so returning default value.") gp.qprint_varx(var_name, var_value) return var_value
def read_boot_lists(dir_path="data/boot_lists/"): r""" Read the contents of all the boot lists files found in the given boot lists directory and return dictionary of the lists. Boot lists are simply files containing a boot test name on each line. These files are useful for categorizing and organizing boot tests. For example, there may be a "Power_on" list, a "Power_off" list, etc. The names of the boot list files will be the keys to the top level dictionary. Each dictionary entry is a list of all the boot tests found in the corresponding file. Here is an abbreviated look at the resulting boot_lists dictionary. boot_lists: boot_lists[All]: boot_lists[All][0]: REST Power On boot_lists[All][1]: REST Power Off ... boot_lists[Code_update]: boot_lists[Code_update][0]: BMC oob hpm boot_lists[Code_update][1]: BMC ib hpm ... Description of argument(s): dir_path The path to the directory containing the boot list files. If this value is a relative path, this function will use the code_base_dir_path as the base directory (see definition above). """ if not dir_path.startswith("/"): # Dir path is relative. dir_path = code_base_dir_path + dir_path # Get a list of all file names in the directory. boot_file_names = os.listdir(dir_path) boot_lists = DotDict() for boot_category in boot_file_names: file_path = gm.which(dir_path + boot_category) boot_list = gm.file_to_list(file_path, newlines=0, comments=0, trim=1) boot_lists[boot_category] = boot_list return boot_lists
def read_boot_lists(dir_path="data/boot_lists/"): r""" Read the contents of all the boot lists files found in the given boot lists directory and return dictionary of the lists. Boot lists are simply files containing a boot test name on each line. These files are useful for categorizing and organizing boot tests. For example, there may be a "Power_on" list, a "Power_off" list, etc. The names of the boot list files will be the keys to the top level dictionary. Each dictionary entry is a list of all the boot tests found in the corresponding file. Here is an abbreviated look at the resulting boot_lists dictionary. boot_lists: boot_lists[All]: boot_lists[All][0]: BMC Power On boot_lists[All][1]: BMC Power Off ... boot_lists[Code_update]: boot_lists[Code_update][0]: BMC oob hpm boot_lists[Code_update][1]: BMC ib hpm ... Description of arguments: dir_path The path to the directory containing the boot list files. If this value is a relative path, this function will use the code_base_dir_path as the base directory (see definition above). """ if not dir_path.startswith("/"): # Dir path is relative. dir_path = code_base_dir_path + dir_path # Get a list of all file names in the directory. boot_file_names = os.listdir(dir_path) boot_lists = DotDict() for boot_category in boot_file_names: file_path = gm.which(dir_path + boot_category) boot_list = gm.file_to_list(file_path, newlines=0, comments=0, trim=1) boot_lists[boot_category] = boot_list return boot_lists
def restore_plug_in_value(default="", plug_in_package_name=None): r""" Return a value from a plug-in save file. The name of the value to be restored will be determined by this function based on the lvalue being assigned. Consider the following example: my_var1 = restore_plug_in_value(2) In this example, this function would look for the "my_var1" file in the plug-in save directory, read its value and return it. If no such file exists, the default value of 2 would be returned. Description of argument(s): default The default value to be returned if there is no plug-in save file for the value in question. plug_in_package_name See compose_plug_in_save_dir_path for details. """ # Get the lvalue from the caller's invocation of this function. lvalue = gp.get_arg_name(0, -1, stack_frame_ix=2) plug_in_save_dir_path = create_plug_in_save_dir(plug_in_package_name) save_file_path = plug_in_save_dir_path + lvalue if os.path.isfile(save_file_path): gp.qprint_timen("Restoring " + lvalue + " value from " + save_file_path + ".") value = gm.file_to_list(save_file_path, newlines=0, comments=0, trim=1)[0] if type(default) is bool: # Convert from string to bool. value = (value == 'True') if type(default) is int: # Convert from string to int. value = int(value) gp.qprint_varx(lvalue, value) return value else: gp.qprint_timen("Save file " + save_file_path + " does not exist so returning default value.") gp.qprint_var(default) return default