# Path: src/moddy/sim_base.py
# def time_unit_to_factor(unit):
#     '''Convert time unit to factor'''
#     unit = unit.lower()
#     if unit == "s":
#         factor = 1.0
#     elif unit == "ms":
#         factor = MS
#     elif unit == "us":
#         factor = US
#     elif unit == "ns":
#         factor = NS
#     else:
#         raise ValueError("Illegal time unit %s" % (unit))
#     return factor
# 
# Path: src/moddy/utils.py
# def create_dirs_and_open_output_file(file_path):
#     '''
#     Create missing directories to <file_path> and open <file_path> for writing
#     @return: file descriptor
#     '''
#     if os.path.dirname(file_path) != "":
#         os.makedirs(os.path.dirname(file_path), exist_ok=True)
#     return open(file_path, 'w')


import csv
from .sim_base import time_unit_to_factor
from .utils import create_dirs_and_open_output_file
    :param file_name: output filename (including .csv)

    :param kwargs: further arguments

     * time_unit="s" - time unit for all time stamps in table \
         ('s', 'ms', 'us', 'ns')
     * float_comma=',' - Comma character for float numbers

    """
    trc = TraceToCsv(sim.tracing.traced_events(), **kwargs)
    trc.save(file_name)


class TraceToCsv:
    # pylint: disable=too-few-public-methods
    """ class to generate csv trace table """

    def __init__(self, ev_list, time_unit="s", float_comma=","):
        self._ev_list = ev_list
        self._time_unit = time_unit
        self._time_unit_factor = time_unit_to_factor(time_unit)
        self._float_comma = float_comma

    def _time_fmt(self, time):
        return ("%.6f" % (time / self._time_unit_factor)).replace(
            ".", self._float_comma
        )

    def save(self, file_name):
        """ save the trace file """

# Path: src/moddy/sim_base.py
# def time_unit_to_factor(unit):
#     '''Convert time unit to factor'''
#     unit = unit.lower()
#     if unit == "s":
#         factor = 1.0
#     elif unit == "ms":
#         factor = MS
#     elif unit == "us":
#         factor = US
#     elif unit == "ns":
#         factor = NS
#     else:
#         raise ValueError("Illegal time unit %s" % (unit))
#     return factor
# 
# Path: src/moddy/utils.py
# def create_dirs_and_open_output_file(file_path):
#     '''
#     Create missing directories to <file_path> and open <file_path> for writing
#     @return: file descriptor
#     '''
#     if os.path.dirname(file_path) != "":
#         os.makedirs(os.path.dirname(file_path), exist_ok=True)
#     return open(file_path, 'w')


# Path: src/moddy/trace_to_csv.py
import csv
from .sim_base import time_unit_to_factor
from .utils import create_dirs_and_open_output_file

    :param file_name: output filename (including .csv)

    :param kwargs: further arguments

     * time_unit="s" - time unit for all time stamps in table \
         ('s', 'ms', 'us', 'ns')
     * float_comma=',' - Comma character for float numbers

    """
    trc = TraceToCsv(sim.tracing.traced_events(), **kwargs)
    trc.save(file_name)


class TraceToCsv:
    # pylint: disable=too-few-public-methods
    """ class to generate csv trace table """

    def __init__(self, ev_list, time_unit="s", float_comma=","):
        self._ev_list = ev_list
        self._time_unit = time_unit
        self._time_unit_factor = time_unit_to_factor(time_unit)
        self._float_comma = float_comma

    def _time_fmt(self, time):
        return ("%.6f" % (time / self._time_unit_factor)).replace(
            ".", self._float_comma
        )

    def save(self, file_name):
        """ save the trace file """

        trace_file = create_dirs_and_open_output_file(file_name)