#just a proof of concept with a lot of options and option groups
import autopath
from config import *
from option import *

all_modules = ['amon', 'sphynx', 'zephir']

example__optiondescription = OptionDescription("objspace", "Object Space Options", [
    ChoiceOption("name", "Object Space name",
                 ["std", "flow", "thunk", "dump", "taint"],
                 "std"),

    OptionDescription("opcodes", "opcodes to enable in the interpreter", [
        BoolOption("CALL_LIKELY_BUILTIN", "emit a special bytecode for likely calls to builtin functions",
                   default=False,
                   requires=[("translation.stackless", False)]),
        BoolOption("CALL_METHOD", "emit a special bytecode for expr.name()",
                   default=False),
        ]),

    BoolOption("nofaking", "disallow faking in the object space",
               default=False,
               requires=[
                   ("objspace.usemodules.posix", True),
                   ("objspace.usemodules.time", True),
                   ("objspace.usemodules.errno", True)],
               ),

    OptionDescription("usemodules", "Which Modules should be used", [
        BoolOption(modname, "use module %s" % (modname, ),
                   default=True,
                   requires= ['amon'],
                   )
        for modname in all_modules]),

    BoolOption("allworkingmodules", "use as many working modules as possible",
               default=True,
               ),

    BoolOption("translationmodules",
          "use only those modules that are needed to run translate.py on pypy",
               default=False,
               ),

    BoolOption("geninterp", "specify whether geninterp should be used",
               default=True),

    BoolOption("logbytecodes",
               "keep track of bytecode usage",
               default=False),

    BoolOption("usepycfiles", "Write and read pyc files when importing",
               default=True),

    BoolOption("lonepycfiles", "Import pyc files with no matching py file",
               default=False,
               requires=[("objspace.usepycfiles", True)]),

    StrOption("soabi",
              "Tag to differentiate extension modules built for different Python interpreters",
              default=None),

    BoolOption("honor__builtins__",
               "Honor the __builtins__ key of a module dictionary",
               default=False),

    BoolOption("disable_call_speedhacks",
               "make sure that all calls go through space.call_args",
               default=False),

    BoolOption("timing",
               "timing of various parts of the interpreter (simple profiling)",
               default=False),

    OptionDescription("std", "Standard Object Space Options", [
        BoolOption("withtproxy", "support transparent proxies",
                   default=True),

        BoolOption("withsmallint", "use tagged integers",
                   default=False,
                   requires=[("objspace.std.withprebuiltint", False),
                             ("translation.taggedpointers", True)]),

        BoolOption("withprebuiltint", "prebuild commonly used int objects",
                   default=False),

        IntOption("prebuiltintfrom", "lowest integer which is prebuilt",
                  default=-5),

        IntOption("prebuiltintto", "highest integer which is prebuilt",
                  default=100),

        BoolOption("withstrjoin", "use strings optimized for addition",
                   default=False),

        BoolOption("withstrslice", "use strings optimized for slicing",
                   default=False),

        BoolOption("withstrbuf", "use strings optimized for addition (ver 2)",
                   default=False),

        BoolOption("withprebuiltchar",
                   "use prebuilt single-character string objects",
                   default=False),

        BoolOption("sharesmallstr",
                   "always reuse the prebuilt string objects "
                   "(the empty string and potentially single-char strings)",
                   default=False),

        BoolOption("withrope", "use ropes as the string implementation",
                   default=False,
                   requires=[("objspace.std.withstrslice", False),
                             ("objspace.std.withstrjoin", False),
                             ("objspace.std.withstrbuf", False)],
                   ),

        BoolOption("withropeunicode", "use ropes for the unicode implementation",
                   default=False,
                   requires=[("objspace.std.withrope", True)]),

        BoolOption("withcelldict",
                   "use dictionaries that are optimized for being used as module dicts",
                   default=False,
                   requires=[("objspace.opcodes.CALL_LIKELY_BUILTIN", False),
                             ("objspace.honor__builtins__", False)]),

        BoolOption("withdictmeasurement",
                   "create huge files with masses of information "
                   "about dictionaries",
                   default=False),

        BoolOption("withmapdict",
                   "make instances really small but slow without the JIT",
                   default=False,
                   requires=[("objspace.std.getattributeshortcut", True),
                             ("objspace.std.withtypeversion", True),
                       ]),

        BoolOption("withrangelist",
                   "enable special range list implementation that does not "
                   "actually create the full list until the resulting "
                   "list is mutated",
                   default=False),

        BoolOption("withtypeversion",
                   "version type objects when changing them",
                   default=False,
                   # weakrefs needed, because of get_subclasses()
                   requires=[("translation.rweakref", True)]),

        BoolOption("withmethodcache",
                   "try to cache method lookups",
                   default=False,
                   requires=[("objspace.std.withtypeversion", True),
                             ("translation.rweakref", True)]),
        BoolOption("withmethodcachecounter",
                   "try to cache methods and provide a counter in __pypy__. "
                   "for testing purposes only.",
                   default=False,
                   requires=[("objspace.std.withmethodcache", True)]),
        IntOption("methodcachesizeexp",
                  " 2 ** methodcachesizeexp is the size of the of the method cache ",
                  default=11),
        BoolOption("optimized_int_add",
                   "special case the addition of two integers in BINARY_ADD",
                   default=False),
        BoolOption("optimized_comparison_op",
                   "special case the comparison of integers",
                   default=False),
        BoolOption("optimized_list_getitem",
                   "special case the 'list[integer]' expressions",
                   default=False),
        BoolOption("builtinshortcut",
                   "a shortcut for operations between built-in types",
                   default=False),
        BoolOption("getattributeshortcut",
                   "track types that override __getattribute__",
                   default=False),
        BoolOption("newshortcut",
                   "cache and shortcut calling __new__ from builtin types",
                   default=False),        

        BoolOption("logspaceoptypes",
                   "a instrumentation option: before exit, print the types seen by "
                   "certain simpler bytecodes",
                   default=False),
        ChoiceOption("multimethods", "the multimethod implementation to use",
                     ["doubledispatch", "mrd"],
                     default="mrd"),
        BoolOption("immutable_builtintypes",
                   "Forbid the changing of builtin types", default=True),
     ]),
])

# ____________________________________________________________

def get_combined_translation_config(other_optdescr=None,
                                    existing_config=None,
                                    overrides=None,
                                    translating=False):
    if overrides is None:
        overrides = {}
    d = BoolOption("translating",
                   "indicates whether we are translating currently",
                   default=False)
    if other_optdescr is None:
        children = []
        newname = ""
    else:
        children = [other_optdescr]
        newname = other_optdescr._name
    descr = OptionDescription("eole", "all options", children)
    config = Config(descr, **overrides)
    if translating:
        config.translating = True
    if existing_config is not None:
        for child in existing_config._cfgimpl_descr._children:
            if child._name == newname:
                continue
            value = getattr(existing_config, child._name)
            config._cfgimpl_values[child._name] = value
    return config

def get_example_config(overrides=None, translating=False):
    return get_combined_translation_config(
            example__optiondescription, overrides=overrides,
            translating=translating)

# ____________________________________________________________

def test_example_option():
    config = get_example_config()
    result = ['objspace.name', 'objspace.opcodes.CALL_LIKELY_BUILTIN',
    'objspace.opcodes.CALL_METHOD', 'objspace.nofaking',
    'objspace.usemodules.amon', 'objspace.usemodules.sphynx',
    'objspace.usemodules.zephir', 'objspace.allworkingmodules',
    'objspace.translationmodules', 'objspace.geninterp',
    'objspace.logbytecodes', 'objspace.usepycfiles', 'objspace.lonepycfiles',
    'objspace.soabi', 'objspace.honor__builtins__',
    'objspace.disable_call_speedhacks', 'objspace.timing',
    'objspace.std.withtproxy', 'objspace.std.withsmallint',
    'objspace.std.withprebuiltint', 'objspace.std.prebuiltintfrom',
    'objspace.std.prebuiltintto', 'objspace.std.withstrjoin',
    'objspace.std.withstrslice', 'objspace.std.withstrbuf',
    'objspace.std.withprebuiltchar', 'objspace.std.sharesmallstr',
    'objspace.std.withrope', 'objspace.std.withropeunicode',
    'objspace.std.withcelldict', 'objspace.std.withdictmeasurement',
    'objspace.std.withmapdict', 'objspace.std.withrangelist',
    'objspace.std.withtypeversion', 'objspace.std.withmethodcache',
    'objspace.std.withmethodcachecounter', 'objspace.std.methodcachesizeexp',
    'objspace.std.optimized_int_add', 'objspace.std.optimized_comparison_op',
    'objspace.std.optimized_list_getitem', 'objspace.std.builtinshortcut',
    'objspace.std.getattributeshortcut', 'objspace.std.newshortcut',
    'objspace.std.logspaceoptypes', 'objspace.std.multimethods',
    'objspace.std.immutable_builtintypes']

    assert config.getpaths(allpaths=True) == result 
