# Buggy code
def new_load_metric_vars(metric_vars_file):
    """
    Load the metric variables for a check from a metric check variables file

    :param metric_vars_file: the path and filename to the metric variables files
    :type metric_vars_file: str
    :return: the metric_vars module object or ``False``
    :rtype: list

    """
    metric_vars = False
    metric_vars_got = False
    if os.path.isfile(metric_vars_file):
        logger.info(
            "loading metric variables from metric_check_file - %s"
            % (str(metric_vars_file))
        )

        metric_vars = []
        with open(metric_vars_file) as f:
            for line in f:
                add_line = line.replace("\n", "")
                metric_vars.append(add_line)

        # Bug #1460: panorama check file fails
        with open(metric_vars_file) as f:
            try:
                metric_vars = imp.load_source("metric_vars", "", f)
                metric_vars_got = True
            except:
                current_logger.info(traceback.format_exc())
                msg = "failed to import metric variables - metric_check_file"
                current_logger.error("error :: %s - %s" % (msg, str(metric_vars_file)))
                metric_vars = False

        if settings.ENABLE_DEBUG and metric_vars_got:
            current_logger.info(
                "metric_vars determined - metric variable - metric - %s"
                % str(metric_vars.metric)
            )
    else:
        current_logger.error(
            "error :: metric_vars_file not found - %s" % (str(metric_vars_file))
        )

    return metric_vars


# Traceback (AttributeError)
2016-08-22 16:42:05 :: 7874 :: Traceback (most recent call last):
File "/opt/skyline/github/skyline/skyline/panorama/panorama.py", line 297, in spin_process
metric_vars.from_timestamp
AttributeError: 'module' object has no attribute 'from_timestamp'

2016-08-22 16:42:05 :: 7874 :: error :: failed to read from_timestamp variable from check file - /opt/skyline/panaroma/check/1471884121.stats.statsd.graphiteStats.flush_length.txt

# Fixed code
def new_load_metric_vars(self, metric_vars_file):
    """
    Load the metric variables for a check from a metric check variables file

    :param metric_vars_file: the path and filename to the metric variables files
    :type metric_vars_file: str
    :return: the metric_vars module object or ``False``
    :rtype: list

    """
    metric_vars = False
    metric_vars_got = False
    if os.path.isfile(metric_vars_file):
        logger.info(
            "loading metric variables from metric_check_file - %s"
            % (str(metric_vars_file))
        )
    else:
        logger.error(
            "error :: loading metric variables from metric_check_file - file not found - %s"
            % (str(metric_vars_file))
        )
        return False

    metric_vars = []
    with open(metric_vars_file) as f:
        for line in f:
            no_new_line = line.replace("\n", "")
            no_equal_line = no_new_line.replace(" = ", ",")
            array = str(no_equal_line.split(",", 1))
            add_line = literal_eval(array)
            metric_vars.append(add_line)

    string_keys = ["metric", "anomaly_dir", "added_by"]
    float_keys = ["value"]
    int_keys = ["from_timestamp", "metric_timestamp", "added_at", "full_duration"]
    array_keys = ["algorithms", "triggered_algorithms"]
    boolean_keys = ["graphite_metric", "run_crucible_tests"]

    metric_vars_array = []
    for var_array in metric_vars:
        key = None
        value = None
        if var_array[0] in string_keys:
            key = var_array[0]
            value_str = str(var_array[1]).replace("'", "")
            value = str(value_str)
            if var_array[0] == "metric":
                metric = value
        if var_array[0] in float_keys:
            key = var_array[0]
            value_str = str(var_array[1]).replace("'", "")
            value = float(value_str)
        if var_array[0] in int_keys:
            key = var_array[0]
            value_str = str(var_array[1]).replace("'", "")
            value = int(value_str)
        if var_array[0] in array_keys:
            key = var_array[0]
            array_value_str = str(var_array[1]).replace("'", "")
            value = literal_eval(str(var_array[1]))
            # value = value_str
        if var_array[0] in boolean_keys:
            key = var_array[0]
            if str(var_array[1]) == "True":
                value = True
            else:
                value = False
        if key:
            metric_vars_array.append([key, value])

        if len(metric_vars_array) == 0:
            logger.error(
                "error :: loading metric variables - none found"
                % (str(metric_vars_file))
            )
            return False

        if settings.ENABLE_DEBUG:
            logger.info(
                "debug :: metric_vars determined - metric variable - metric - %s"
                % str(metric_vars.metric)
            )

    logger.info("debug :: metric_vars for %s" % str(metric))
    logger.info("debug :: %s" % str(metric_vars_array))

    return metric_vars_array