Пример #1
0
 def __init__(self, name, schedule=None):
     self.name = name
     self.schedule = schedule
     if not zoom.system.is_setup:
         zoom.system.setup()
     self.logger = get_logger(name)
     self.instance = Instance(self.name)
Пример #2
0
    def setup(self,
              instance_path=None,
              server=request.server,
              timer=SystemTimer(timeit.default_timer())):
        """set up the system"""

        if instance_path is None:
            instance_path = Instance('system').path

        self.debugging = True
        self.timer = timer
        self.start_time = timer.start_time
        self.lib_path = os.path.split(os.path.abspath(__file__))[0]

        if not os.path.exists(os.path.join(instance_path, 'dz.conf')):
            msg = 'instance missing %s' % os.path.abspath(instance_path)
            raise Exception(msg)
        self.instance_path = os.path.abspath(instance_path)

        if '.' not in sys.path:
            sys.path.insert(0, '.')

        # system config file
        self.config = config = cfg.Config(instance_path, server)

        # connect to the database and stores
        db_engine = config.get('database', 'engine', 'mysql')
        db_host = config.get('database', 'dbhost', 'database')
        db_name = config.get('database', 'dbname', 'zoomdev')
        db_user = config.get('database', 'dbuser', 'testuser')
        db_pass = config.get('database', 'dbpass', 'password')

        # legacy database module
        self.database = old_database(
            db_engine,
            db_host,
            db_name,
            db_user,
            db_pass,
            )

        # database module
        db_params = dict(
            engine=db_engine,
            host=db_host,
            db=db_name,
            user=db_user,
            )
        if db_pass:
            db_params['passwd'] = db_pass
        # pylint: disable=invalid-name, star-args
        self.db = new_db(**db_params)

        self.db_debug = config.get('database', 'debug', '0') not in NEGATIVE
        self.db.debug = self.db_debug
        self.database.debug = self.db_debug

        # message queues
        from zoom.queues import Queues
        self.queues = Queues(self.db)

        from zoom.store import EntityStore
        settings_store = EntityStore(self.database, settings.SystemSettings)
        self.settings = settings.Settings(settings_store, config, 'system')

        if not os.path.exists(config.sites_path):
            raise Exception('sites missing %s' % config.sites_path)

        self.debugging = config.get('errors', 'debugging', '0') == '1'

        self.request = request
        self.server = server
        self.server_name = server  # deprecated

        # get current site directory
        self.root = request.instance
        self.uri = config.get('site', 'uri', '/')
        if self.uri[-1] == '/':
            self.uri = self.uri[:-1]

        # get site info
        self.site = Site(
            name='',
            theme='',
            home=os.path.join(config.sites_path, server),
            data_path=os.path.join(config.sites_path, server,
                                   config.get('data', 'path', 'data')),
            url=self.uri,
            tracking_id=config.get('site', 'tracking_id', ''),
            )

        # csrf validation
        self.csrf_validation = (
            config.get('site', 'csrf_validation', True) not in
            ['0', 'False', 'off', 'no', True]
            )

        # secure cookies
        self.secure_cookies = (
            config.get('sessions', 'secure_cookies', False) not in
            ['0', 'False', 'off', 'no', False]
        )

        # users and groups
        self.guest = config.get('users', 'default', 'guest')
        self.administrator_group = \
            system.config.get('users', 'administrator_group', 'administrators')
        self.manager_group = config.get('users', 'manager_group', 'managers')
        self.managers = config.get('users', 'managers', 'managers')
        self.developers = config.get('users', 'developers', 'developers')
        self.administrators = \
            config.get('users', 'administrators', 'administrators')

        # apps
        self.index = config.get('apps', 'index', 'index')
        self.home = config.get('apps', 'home', 'home')

        # background processing
        self.background = config.get('background', 'run', True) not in NEGATIVE

        # users (experimental)
        self.users = UserStore(self.db)

        # email settings
        self.from_addr = system.config.get('mail', 'from_addr')
        self.mail_delivery = system.config.get('mail', 'delivery', 'immediate')

        # load theme
        self.themes_path = existing(config.get(
            'theme',
            'path',
            os.path.join(self.root, 'themes'))
        )
        self.theme = (
            self.themes_path and
            self.settings.get('theme_name') or
            config.get('theme', 'name', 'default')
        )
        self.set_theme(self.theme)

        self.app = NoApp()
        self.site_name = ''
        self.warnings = []
        self.errors = []
        self.messages = []

        self.styles = OrderedSet()
        self.css = OrderedSet()
        self.libs = OrderedSet()
        self.js = OrderedSet()
        self.head = OrderedSet()
        self.tail = OrderedSet()

        self.helpers = {}

        self.show_errors = config.get('error', 'users', '0') == '1'

        self.profile = config.get('system', 'profile', '0') == '1'

        self.track_visits = config.get(
            'system',
            'track_visits',
            '0').lower() in POSITIVE

        self.logging = config.get(
            'log',
            'logging',
            True) not in ['0', 'False', 'off', 'no', False]

        self.session = zoom.session.Session(self)
        self.session.load_session()

        self.is_setup = True
Пример #3
0
class Service(object):
    """background processing service

    Use this class to create a service.
    """

    def __init__(self, name, schedule=None):
        self.name = name
        self.schedule = schedule
        if not zoom.system.is_setup:
            zoom.system.setup()
        self.logger = get_logger(name)
        self.instance = Instance(self.name)

    def process(self, *jobs):
        """process service requests for all sites"""
        parser = argparse.ArgumentParser('Run {!r} background services'.format(self.name))
        parser.add_argument('-q', '--quiet', action='store_true', help='supresss output')
        parser.add_argument('-n', '--dryrun', action='store_true', help='don\'t actually run the services, just show if they exists and would have been run.')
        parser.add_argument('-d', '--debug', action='store_true', help='show all debugging information as services run')
        args = parser.parse_args()

        if args.debug:
            console_hanlder.setLevel(logging.DEBUG)

        if args.quiet:
            console_handler.setLevel(logging.ERROR)

        self.logger.debug('service {} starting'.format(
            self.name,
        ))

        workers = [Worker(self, job) for job in jobs]
        self.instance.run(*workers)

        self.logger.debug('service {} done'.format(
            self.name,
        ))

    def every(self, interval, *jobs):
        """process jobs on an time interval"""
        parser = argparse.ArgumentParser('Run {!r} background services'.format(self.name))
        parser.add_argument('-q', '--quiet', action='store_true', help='supresss output')
        parser.add_argument('-n', '--dryrun', action='store_true', help='don\'t actually run the services, just show if they exists and would have been run.')
        parser.add_argument('-d', '--debug', action='store_true', help='show all debugging information as services run')
        parser.add_argument('-f', '--force', action='store_true', help='force services to run ahead of schedule')
        args = parser.parse_args()

        if args.debug:
            debug_log = '../debug.log'
            debug_handler = logging.FileHandler(debug_log, 'a')
            debug_handler.setLevel(logging.DEBUG)
            debug_handler.setFormatter(csv_formatter)
            logger.addHandler(debug_handler)

            console_handler.setLevel(logging.DEBUG)

        if args.quiet:
            console_handler.setLevel(logging.ERROR)

        self.logger.debug('service {!r} starting'.format(
            self.name,
        ))

        scheduler = Scheduler(self.name, args.force)
        if scheduler.its_time(interval):
            self.instance.run(*jobs)

        self.logger.debug('service {!r} done'.format(
            self.name,
        ))


    def run(self, job, a0=None, *a, **k):
        """queue a job"""
        Worker(self, job).queue.put(a0, *a, **k)

    def call(self, job, *a, **k):
        """queue a job expecting a result"""
        queue = Worker(self, job).queue
        id = queue.put(*a, **k)
        result = queue.join([id])[0]
        return result