Last active
September 26, 2018 09:01
-
-
Save jgsogo/826312a847576fb035c4bb7854784fb2 to your computer and use it in GitHub Desktop.
Conan options truth tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Not a valid Conan (http://conan.io) recipe, just some lines of code to test option evaluation. | |
from conans import ConanFile, CMake, tools | |
from conans.errors import ConanException | |
def try_option_boolean(pkg_option, lambda_cmp): | |
try: | |
return "{}".format(lambda_cmp(pkg_option)) | |
except ConanException as e: | |
return "-" | |
class HelloConan(ConanFile): | |
name = "Hello" | |
version = "0.1" | |
options = { | |
"opt": "ANY", | |
#"opt": ["True", False, None, "", 10, 0, "false", "true", "none", "a"], | |
"true_false_str": ["True", "False"], | |
"misc": [None, "None", "", ], | |
"any": "ANY"} | |
default_options = {"opt": True, | |
"true_false_str": "True", | |
"misc": None, | |
"any": '10'} | |
generators = "cmake" | |
# def configure(self): | |
def source(self): | |
from tabulate import tabulate | |
from collections import defaultdict, OrderedDict | |
results = defaultdict(list) | |
opt_name = "opt" | |
valid_values = ""# str(HelloConan.options["opt"]) | |
# python expressions to run | |
cmps = [("", lambda u: bool(u)), | |
("is None", lambda u: bool(u is None)), | |
("is True", lambda u: bool(u is True)), | |
("is False", lambda u: bool(u is False)), | |
("== None", lambda u: bool(u == None)), | |
("== 'None'", lambda u: bool(u == 'None')), | |
("== 'none'", lambda u: bool(u == 'none')), | |
("== True", lambda u: bool(u == True)), | |
("== 'True'", lambda u: bool(u == 'True')), | |
("== 'true'", lambda u: bool(u == 'true')), | |
("== False", lambda u: bool(u == False)), | |
("== 'False'", lambda u: bool(u == 'False')), | |
("== 'false'", lambda u: bool(u == 'false')), | |
("== ''", lambda u: bool(u == '')), | |
("== 'a'", lambda u: bool(u == 'a')), | |
("== 10", lambda u: bool(u == 10)), | |
("== '10'", lambda u: bool(u == '10')), | |
("== 0", lambda u: bool(u == 0)), | |
("== '0'", lambda u: bool(u == '0')), | |
("== 'other'", lambda u: bool(u == 'other')), | |
] | |
# values to assign | |
values = OrderedDict() | |
values[True] = "True" | |
values["True"] = "\"True\"" | |
values["true"] = "\"true\"" | |
values[False] = "False" | |
values["False"] = "\"False\"" | |
values["false"] = "\"false\"" | |
values[None] = "None" | |
values["None"] = "\"None\"" | |
values["none"] = "\"none\"" | |
values[""] = "\"\"" | |
values["a"] = "\"a\"" | |
values[10] = "10" | |
values["10"] = "\"10\"" | |
values["other"] = "\"other\"" | |
# values[0] = "0" | |
values["0"] = "\"0\"" | |
for it in cmps: | |
results[valid_values].append("self.options.{} {:<10}".format(opt_name, it[0])) | |
for v, v_str in values.items(): | |
try: | |
setattr(self.options, "opt", v) | |
res = try_option_boolean(getattr(self.options, "opt"), it[1]) | |
except: | |
res = "-" | |
results[v_str].append(res) | |
try: | |
setattr(self.options, "opt", 0) # I want the 0 to be the last one | |
res = try_option_boolean(getattr(self.options, "opt"), it[1]) | |
except: | |
res = "-" | |
results["0"].append(res) | |
print(tabulate(results, headers="keys")) | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment