# Path: confduino/proglist.py
# def programmers_txt():
#     """path of programmers.txt."""
#     x = arduino_path() / 'hardware' / 'arduino' / 'programmers.txt'
#     assert x.exists()
#     return x
# 
# def programmers():
#     """read programmers from programmers.txt."""
#     return read_properties(programmers_txt())
# 
# Path: confduino/progremove.py
# @entrypoint
# def remove_programmer(programmer_id):
#     """remove programmer.
# 
#     :param programmer_id: programmer id (e.g. 'avrisp')
#     :rtype: None
# 
#     """
# 
#     log.debug('remove %s', programmer_id)
#     lines = programmers_txt().lines()
#     lines = filter(
#         lambda x: not x.strip().startswith(programmer_id + '.'), lines)
#     programmers_txt().write_lines(lines)
# 
# Path: confduino/util.py
# def bunch2properties(parent, data):
#     lines = []
#     try:
#         for key, value in data.items():
#             # recursive call
#             lines += bunch2properties(parent + '.' + key, value)
#     except AttributeError:
#         lines += [parent + '=' + str(data)]
#     return lines


from confduino.proglist import programmers_txt, programmers
from confduino.progremove import remove_programmer
from confduino.util import bunch2properties
import logging

log = logging.getLogger(__name__)

# TODO: how to pass options as cli argument?


def install_programmer(programmer_id, programmer_options, replace_existing=False):
    """install programmer in programmers.txt.

    :param programmer_id: string identifier
    :param programmer_options: dict like
    :param replace_existing: bool
    :rtype: None

    """
    doaction = 0
    if programmer_id in programmers().keys():
        log.debug('programmer already exists: %s', programmer_id)
        if replace_existing:
            log.debug('remove programmer: %s', programmer_id)
            remove_programmer(programmer_id)
            doaction = 1
    else:
        doaction = 1

    if doaction:

# Path: confduino/proglist.py
# def programmers_txt():
#     """path of programmers.txt."""
#     x = arduino_path() / 'hardware' / 'arduino' / 'programmers.txt'
#     assert x.exists()
#     return x
# 
# def programmers():
#     """read programmers from programmers.txt."""
#     return read_properties(programmers_txt())
# 
# Path: confduino/progremove.py
# @entrypoint
# def remove_programmer(programmer_id):
#     """remove programmer.
# 
#     :param programmer_id: programmer id (e.g. 'avrisp')
#     :rtype: None
# 
#     """
# 
#     log.debug('remove %s', programmer_id)
#     lines = programmers_txt().lines()
#     lines = filter(
#         lambda x: not x.strip().startswith(programmer_id + '.'), lines)
#     programmers_txt().write_lines(lines)
# 
# Path: confduino/util.py
# def bunch2properties(parent, data):
#     lines = []
#     try:
#         for key, value in data.items():
#             # recursive call
#             lines += bunch2properties(parent + '.' + key, value)
#     except AttributeError:
#         lines += [parent + '=' + str(data)]
#     return lines


# Path: confduino/proginstall.py
from confduino.proglist import programmers_txt, programmers
from confduino.progremove import remove_programmer
from confduino.util import bunch2properties
import logging


log = logging.getLogger(__name__)

# TODO: how to pass options as cli argument?


def install_programmer(programmer_id, programmer_options, replace_existing=False):
    """install programmer in programmers.txt.

    :param programmer_id: string identifier
    :param programmer_options: dict like
    :param replace_existing: bool
    :rtype: None

    """
    doaction = 0
    if programmer_id in programmers().keys():
        log.debug('programmer already exists: %s', programmer_id)
        if replace_existing:
            log.debug('remove programmer: %s', programmer_id)
            remove_programmer(programmer_id)
            doaction = 1
    else:
        doaction = 1

    if doaction:

        lines = bunch2properties(programmer_id, programmer_options)