__all__ = [
    'add_ninja_rule',
    'get_ninja_rule',
]

import logging
from collections import namedtuple

import iga.env
from iga.core import WriteOnceDict
from iga.error import IgaError


LOG = logging.getLogger(__name__)
LOG.addHandler(logging.NullHandler())


NinjaRule = namedtuple('NinjaRule', 'name command variables')


# Registry of rules.


iga.env.root()[__name__] = WriteOnceDict()


def _ninja_rules():
    return iga.env.root()[__name__]


def add_ninja_rule(*, name, command, **kwargs):
    LOG.info('add ninja rule %r', name)
    variables = dict(kwargs)
    if name in RESERVED_RULE_NAMES:
        raise IgaError('cannot use %r as rule name' % name)
    for key in variables:
        if key not in RULE_VARS_ALL:
            raise IgaError('cannot use %r' % key)
    _ninja_rules()[name] = NinjaRule(
        name=name, command=command, variables=variables,
    )


def get_ninja_rule(name):
    return _ninja_rules()[name]

__all__ = [
    'NinjaRule',
]

from collections import namedtuple

from iga.preconditions import check


RULE_VARS_1_0 = (
    'command',
    'depfile',
    'description',
    'generator',
    'in',
    'in_newline',
    'out',
    'restat',
    'rspfile',
    'rspfile_content',
)


RULE_VARS_1_3 = (
    'deps',
)


RULE_VARS_1_5 = (
    'msvc_deps_prefix',
)


RULE_VARS = RULE_VARS_1_0 + RULE_VARS_1_3 + RULE_VARS_1_5


RESERVED_RULE_NAMES = (
    'phony',
)


class NinjaRule(namedtuple('NinjaRule', 'name command variables')):

    kind = __name__ + '.NinjaRule'

    @staticmethod
    def make(name, command, **kwargs):
        check(name in RESERVED_RULE_NAMES, 'cannot use %r as rule name', name)
        variables = dict(kwargs)
        for key in variables:
            check(key in RULE_VARS, 'cannot use %r', key)
        return NinjaRule(name=name, command=command, variables=variables)

Rewrite ninja to work with global registry