예제 #1
0
    def __init__(self):
        # Parse the args
        args = self._parse_args()

        # Create/load config
        config = None
        self.config_path = os.path.join(args.config_dir,
                                        Seedsync.__FILE_CONFIG)
        create_default_config = False
        if os.path.isfile(self.config_path):
            try:
                config = Config.from_file(self.config_path)
            except (ConfigError, PersistError):
                Seedsync.__backup_file(self.config_path)
                # set config to default
                create_default_config = True
        else:
            create_default_config = True

        if create_default_config:
            # Create default config
            config = Seedsync._create_default_config()
            config.to_file(self.config_path)

        # Determine the true value of debug
        is_debug = args.debug or config.general.debug

        # Create context args
        ctx_args = Args()
        ctx_args.local_path_to_scanfs = args.scanfs
        ctx_args.html_path = args.html
        ctx_args.debug = is_debug
        ctx_args.exit = args.exit

        # Logger setup
        # We separate the main log from the web-access log
        logger = self._create_logger(name=Constants.SERVICE_NAME,
                                     debug=is_debug,
                                     logdir=args.logdir)
        Seedsync.logger = logger
        web_access_logger = self._create_logger(
            name=Constants.WEB_ACCESS_LOG_NAME,
            debug=is_debug,
            logdir=args.logdir)
        logger.info(
            "Debug mode is {}.".format("enabled" if is_debug else "disabled"))

        # Create status
        status = Status()

        # Create context
        self.context = Context(logger=logger,
                               web_access_logger=web_access_logger,
                               config=config,
                               args=ctx_args,
                               status=status)

        # Register the signal handlers
        signal.signal(signal.SIGTERM, self.signal)
        signal.signal(signal.SIGINT, self.signal)

        # Print context to log
        self.context.print_to_log()

        # Load the persists
        self.controller_persist_path = os.path.join(
            args.config_dir, Seedsync.__FILE_CONTROLLER_PERSIST)
        self.controller_persist = self._load_persist(
            ControllerPersist, self.controller_persist_path)

        self.auto_queue_persist_path = os.path.join(
            args.config_dir, Seedsync.__FILE_AUTO_QUEUE_PERSIST)
        self.auto_queue_persist = self._load_persist(
            AutoQueuePersist, self.auto_queue_persist_path)