def set_file_properties(file_, datetime_format_=skj_std.arguments_values['timeformat']):
    ''' Get number of lines, min/max time value, min/max data and check time formatting on every line
        status: finished
        raise: ValueError
        return: dict / None
    '''
    with open(file_, mode="r", encoding="utf-8") as f:
        first_line = f.readline() # Read first line

        while first_line.isspace(): # Skip lines containing only whitespaces
            first_line = f.readline()
        if first_line == '': # Skip files containing only whitespaces
            return None

        num_of_lines = 1  # First line is read for setting first_line/time_max before the loop
        first_line = first_line.strip() # Remove trailing/foregoing whitespaces

        # Default values for date/data min/max are the values of first record
        from skj_checker_common import check_time_format, check_float_ok
        data_min = check_float_ok(first_line[first_line.rfind(' '):].strip(), False) # Last fields belongs to data
        data_max = data_min 
        time_min = check_time_format((first_line[:first_line.rfind(' ')], datetime_format_), False)  # raise ValueError 
        time_max = time_min

        for one_line in f:
            if one_line.isspace(): # Skip lines containing only whitespaces
                continue

            num_of_lines += 1  # Count lines in file
            one_line = one_line.strip() # Prepare line for processing

            # Grab data(float) from record
            data_from_line = check_float_ok(one_line[one_line.rfind(' '):].strip(), False)
            date_from_line = check_time_format((one_line[:one_line.rfind(' ')], datetime_format_), False)

            # Find min/max in data
            data_max = max(data_max, data_from_line)
            data_min = min(data_min, data_from_line)

            # Find min/max in date
            time_max = max(time_max, date_from_line)
            time_min = min(time_min, date_from_line)

    return {"path": file_, "num_of_lines": num_of_lines, "time_min": time_min, "time_max": time_max, 
            "date_column": 1, "data_column": len(first_line.split()), "data_min": data_min,
            "data_max": data_max}
Beispiel #2
0
def parse_directives():
    ''' Parse configuration file directives
            status: finished
            return: None
            raise: IOError
    '''
    config_file = dict()

    for argument in skj_std.arguments_repeatable:
        config_file[argument] = list()

    try:
        with open(skj_std.arguments_values['config'], mode="r", encoding="utf-8") as cnf_file:
            for line in cnf_file:
                option = line.partition('#')[0].strip().split(None, 1)
                if len(option) > 0:
                    # Check if it is a valid directive 
                    if option[0].lower() not in skj_std.arguments_defaults:
                        raise ValueError(option[0] + " is not a configuration directive")
                    if len(option) == 1:
                        raise ValueError(option[0] + " does not have configuration value")
                    if option[0].lower() in ["speed", "fps", "time"]: # should be created as list of floats from argparse
                        from skj_checker_common import check_float_ok
                        option[1] = check_float_ok(option[1]) # Check && convert string to float if possible

                    # Store valid directives
                    if option[0].lower() in skj_std.arguments_repeatable:
                        config_file[option[0].lower()].append(option[1])
                    else:
                        config_file[option[0].lower()] = option[1]
    except (IOError, IndexError, TypeError, ValueError) as exception_msg:
        if skj_std.arguments_values['ignoreerrors']:
            skj_std.print_msg_verbose(err_=skj_std.create_error_msg("PYTHON", exception_msg))
            return
        else:
            raise IOError(skj_std.create_error_msg("PYTHON", exception_msg, False))
    else:
        add_directives_to_args(config_file, skj_std.arguments_repeatable)