def __init__(self, tmpdir): # type: (LocalPath) -> None self.settings = Settings() self.settings.database = db_url(tmpdir) self.plugins = PluginProxy([]) # Reinitialize the global plugin proxy with an empty set of plugins in case a previous test # initialized plugins. This can go away once a plugin proxy is injected into everything # that needs it instead of maintained as a global. set_global_plugin_proxy(self.plugins) self.initialize_database() self.session = SessionFactory(self.settings).create_session() self.graph = GroupGraph() session_factory = SingletonSessionFactory(self.session) self.repository_factory = GraphRepositoryFactory( self.settings, self.plugins, session_factory, self.graph) self.sql_repository_factory = SQLRepositoryFactory( self.settings, self.plugins, session_factory) self.service_factory = ServiceFactory(self.settings, self.plugins, self.repository_factory) self.usecase_factory = UseCaseFactory(self.settings, self.plugins, self.service_factory) self._transaction_service = self.service_factory.create_transaction_service( )
def api_app(session, standard_graph): # type: (Session, GroupGraph) -> GrouperApplication settings = ApiSettings() set_global_settings(settings) session_factory = SingletonSessionFactory(session) usecase_factory = create_graph_usecase_factory( settings, PluginProxy([]), session_factory, standard_graph ) return create_api_application(standard_graph, settings, usecase_factory)
def open_database(self) -> None: self.session = SessionFactory(self.settings).create_session() session_factory = SingletonSessionFactory(self.session) self.repository_factory = GraphRepositoryFactory( self.settings, self.plugins, session_factory, self.graph) self.sql_repository_factory = SQLRepositoryFactory( self.settings, self.plugins, session_factory) self.service_factory = ServiceFactory(self.settings, self.plugins, self.repository_factory) self.usecase_factory = UseCaseFactory(self.settings, self.plugins, self.service_factory) self._transaction_service = self.service_factory.create_transaction_service( )
def initialize(self, *args: Any, **kwargs: Any) -> None: self.graph = Graph() self.session = self.settings["session"]() # type: Session self.template_engine = self.settings[ "template_engine"] # type: FrontendTemplateEngine self.plugins = get_plugin_proxy() session_factory = SingletonSessionFactory(self.session) self.usecase_factory = create_graph_usecase_factory( settings(), self.plugins, session_factory) if self.get_argument("_profile", False): self.perf_collector = Collector() self.perf_trace_uuid = str(uuid4()) # type: Optional[str] self.perf_collector.start() else: self.perf_collector = None self.perf_trace_uuid = None self._request_start_time = datetime.utcnow()
def initialize(self, *args, **kwargs): # type: (*Any, **Any) -> None self.graph = Graph() self.session = self.settings["session"]() # type: Session self.template_engine = self.settings[ "template_engine"] # type: FrontendTemplateEngine self.plugins = get_plugin_proxy() session_factory = SingletonSessionFactory(self.session) self.usecase_factory = create_graph_usecase_factory( settings(), self.plugins, session_factory) if self.get_argument("_profile", False): self.perf_collector = Collector() self.perf_trace_uuid = str(uuid4()) # type: Optional[str] self.perf_collector.start() else: self.perf_collector = None self.perf_trace_uuid = None self._request_start_time = datetime.utcnow() stats.log_rate("requests", 1) stats.log_rate("requests_{}".format(self.__class__.__name__), 1) logging.error("initialized")
def main(sys_argv=sys.argv, session=None): # type: (List[str], Optional[Session]) -> None description_msg = "Grouper Control" parser = argparse.ArgumentParser(description=description_msg) parser.add_argument("-c", "--config", default=default_settings_path(), help="Path to config file.") parser.add_argument("-d", "--database-url", type=str, default=None, help="Override database URL in config.") parser.add_argument("-q", "--quiet", action="count", default=0, help="Decrease logging verbosity.") parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase logging verbosity.") parser.add_argument( "-V", "--version", action="version", version="%%(prog)s %s" % __version__, help="Display version information.", ) subparsers = parser.add_subparsers(dest="command") CtlCommandFactory.add_all_parsers(subparsers) # Add parsers for legacy commands that have not been refactored. for subcommand_module in [group, oneoff, service_account, shell]: subcommand_module.add_parser(subparsers) # type: ignore args = parser.parse_args(sys_argv[1:]) # Construct the CtlSettings object used for all commands, and set it as the global Settings # object. All code in grouper.ctl.* takes the CtlSettings object as an argument if needed, but # it may call other legacy code that requires the global Settings object be present. settings = CtlSettings.global_settings_from_config(args.config) if args.database_url: settings.database = args.database_url # Construct a session factory, which is passed into all the legacy commands that haven't been # converted to usecases yet. if session: session_factory = SingletonSessionFactory( session) # type: SessionFactory else: session_factory = SessionFactory(settings) log_level = get_loglevel(args, base=logging.INFO) logging.basicConfig(level=log_level, format=settings.log_format) if log_level < 0: sa_log.setLevel(logging.INFO) # Initialize plugins. The global plugin proxy is used by legacy code. try: plugins = PluginProxy.load_plugins(settings, "grouper-ctl") except PluginsDirectoryDoesNotExist as e: logging.fatal("Plugin directory does not exist: {}".format(e)) sys.exit(1) set_global_plugin_proxy(plugins) # Set up factories. usecase_factory = create_sql_usecase_factory(settings, plugins, session_factory) command_factory = CtlCommandFactory(settings, usecase_factory) # Old-style subcommands store a func in callable when setting up their arguments. New-style # subcommands are handled via a factory that constructs and calls the correct object. if getattr(args, "func", None): args.func(args, settings, session_factory) else: command = command_factory.construct_command(args.command) command.run(args)