def set_get_session(oso: Oso, get_session_func): """Set the function that oso uses to expose a SQLAlchemy session to the policy :param oso: The Oso instance used to evaluate the policy. :type oso: Oso :param get_session_func: A function that returns a SQLAlchemy session :type get_session_func: lambda The session can be accessed from polar via the OsoSession constant. E.g., .. code-block:: polar OsoSession.get().query(...) """ _OsoSession.set_get_session(get_session_func) oso.register_constant(_OsoSession, "OsoSession")
def main(): # parser = argparse.ArgumentParser(description="An epic Polar adventure.") # parser.add_argument( # "-l", "--load", type=str, nargs=1, help="the filename of a saved game" # ) # args = parser.parse_args() # if args.load: # GAME.load_saved(args.load) oso = Oso() oso.register_class(Game) oso.register_class(Room) oso.register_class(Passage) oso.register_class(Player) oso.register_class(Collection) oso.register_class(Object) oso.register_class(Animal) oso.register_class(Food) oso.register_class(Container) oso.register_class(Takeable) oso.register_class(Mushroomy) oso.register_class(Soup) oso.register_class(Source) oso.register_class(Wand) oso.register_class(Wet) oso.register_class(OnFire) oso.register_class(Leafy) oso.register_constant(GAME, "GAME") oso.register_constant(PLAYER, "PLAYER") oso.register_constant(ROOMS, "Rooms") oso.register_constant(PASSAGES, "Passages") oso.register_constant(OBJECTS, "Objects") oso.load_file("world.polar") oso.load_file("commands.polar") oso.load_file("tests.polar") oso.repl()
assert list(oso.query_rule("floatLists")) assert list(oso.query_rule("intDicts")) assert list(oso.query_rule("comparisons")) assert list(oso.query_rule("testForall")) assert list(oso.query_rule("testRest")) assert list(oso.query_rule("testMatches", A("hello"))) assert list(oso.query_rule("testMethodCalls", A("hello"), B.C("hello"))) assert list(oso.query_rule("testOr")) assert list(oso.query_rule("testHttpAndPathMapper")) assert list(oso.query_rule("testUnifyClass", A)) # Test that cut doesn't return anything. assert not list(oso.query_rule("testCut")) # Test that a constant can be called. oso.register_constant("Math", math) oso.load_str("?= Math.factorial(5) == 120;") # Test built-in type specializers. assert list(oso.query('builtinSpecializers(true, "Boolean")')) assert not list(oso.query('builtinSpecializers(false, "Boolean")')) assert list(oso.query('builtinSpecializers(2, "Integer")')) assert list(oso.query('builtinSpecializers(1, "Integer")')) assert not list(oso.query('builtinSpecializers(0, "Integer")')) assert not list(oso.query('builtinSpecializers(-1, "Integer")')) assert list(oso.query('builtinSpecializers(1.0, "Float")')) assert not list(oso.query('builtinSpecializers(0.0, "Float")')) assert not list(oso.query('builtinSpecializers(-1.0, "Float")')) assert list(oso.query('builtinSpecializers(["foo", "bar", "baz"], "List")')) assert not list(oso.query('builtinSpecializers(["bar", "foo", "baz"], "List")')) assert list(oso.query('builtinSpecializers({foo: "foo"}, "Dictionary")'))
assert list(oso.query_rule("floatLists")) assert list(oso.query_rule("intDicts")) assert list(oso.query_rule("comparisons")) assert list(oso.query_rule("testForall")) assert list(oso.query_rule("testRest")) assert list(oso.query_rule("testMatches", A("hello"))) assert list(oso.query_rule("testMethodCalls", A("hello"), B.C("hello"))) assert list(oso.query_rule("testOr")) assert list(oso.query_rule("testHttpAndPathMapper")) assert list(oso.query_rule("testUnifyClass", A)) # Test that cut doesn't return anything. assert not list(oso.query_rule("testCut")) # Test that a constant can be called. oso.register_constant(math, "MyMath") oso.load_str("?= MyMath.factorial(5) == 120;") # Test built-in type specializers. assert list(oso.query('builtinSpecializers(true, "Boolean")')) assert not list(oso.query('builtinSpecializers(false, "Boolean")')) assert list(oso.query('builtinSpecializers(2, "Integer")')) assert list(oso.query('builtinSpecializers(1, "Integer")')) assert not list(oso.query('builtinSpecializers(0, "Integer")')) assert not list(oso.query('builtinSpecializers(-1, "Integer")')) assert list(oso.query('builtinSpecializers(1.0, "Float")')) assert not list(oso.query('builtinSpecializers(0.0, "Float")')) assert not list(oso.query('builtinSpecializers(-1.0, "Float")')) assert list(oso.query('builtinSpecializers(["foo", "bar", "baz"], "List")')) assert not list( oso.query('builtinSpecializers(["bar", "foo", "baz"], "List")'))