Exemple #1
0
    def __init__(self,
                 progress_file,
                 beat_func=None,
                 start=None,
                 end=None,
                 sleeper=None,
                 init_progress=None):
        self._context = Context()
        if start:
            self.set_start(start)
        if end:
            self.set_end(end)

        self._beat_func = beat_func
        self._sleeper = sleeper
        self._logger = None
        self._stopped = False
        self._paused = False
        self._to_skip = None
        self._forward_to = None

        self._current_node = None

        self._progress = Progress(progress_file,
                                  non_header_names=('_not_exist_key_', ))
        if init_progress:
            self._progress.append(init_progress)
        self._data = self._progress.copy_data(True)

        if '_global' not in self._data:
            self._data['_global'] = ObjectDict()
        if '_internal' not in self._data:
            self._data['_internal'] = ObjectDict()
Exemple #2
0
    def execute(self, sql, args=None, commit=True):
        if re.match(RE_WRITE_ST, sql):
            cursor = self._cursorW
            is_w = True
        else:
            is_w = False
            cursor = self._cursorR

        if not args: args = ()
        elif not isinstance(args, tuple):
            args = tuple(args)

        try:
            cursor.execute(sql, args)
            if is_w and commit and not self._autocommit:
                self.cn.commit()
        except:
            if not self.cn.is_connected():
                self.connect()
                cursor.execute(sql, args)
                if is_w and commit and not self._autocommit:
                    self.cn.commit()
            else:
                raise

        if cursor.with_rows:
            return ObjectDict(rows=cursor.fetchall())
        else:
            return cursor.rowcount, cursor.lastrowid
Exemple #3
0
    def _load(self):
        '''
        read all data in file and compact as one dict
        @return: ObjectDcit
        '''
        if not os.path.exists(self._path):
            utils.makedirs(self._path)
            self._fd = open(self._path, 'w+b')
        else:
            self._fd = open(self._path, 'r+b')

        fd = self._fd

        bak = '%s.bak' % self._path
        if os.path.exists(bak):
            fd = open(bak, 'r+b')

        data = ObjectDict()
        while 1:
            bs = fd.read(self.INT_SIZE)
            if bs:
                data_size = struct.unpack('I', bs)[0]
                if data_size:
                    bs = fd.read(data_size)
                    data.update(
                        json.loads(bs.decode('utf8'),
                                   object_hook=objectdict_json_hook))
                else:
                    raise Exception('corrupted progress data')
            else:
                break

        if fd != self._fd:
            fd.close()
            self.compact(True)

        self._data = data
Exemple #4
0
 def to_odict(self, *only, **kw):
     return ObjectDict(self._dict(*only, **kw))
Exemple #5
0
def objectdict_json_hook(dct):
    if 'task' in dct:
        dct['task'] = Task(dct['task'])
    return ObjectDict(dct)
Exemple #6
0
    def work(self, task, allow_re_work=False):
        '''work on task
        work will be resumed from the last node where stopped
        '''

        node = self.get_node(self._data._internal.current_node_id)

        if isinstance(node, EndNode):
            if not allow_re_work and self._data.task and str(
                    self._data.task) == str(task):
                raise WorkOnFinished('not allowed to work on finished task')

            self._data = ObjectDict(_internal=ObjectDict(),
                                    _global=self._data._global)
            self.current_node = node = self._start

        else:
            if self._data.task and str(self._data.task) != str(task):
                if self._to_skip and self._to_skip == self._data.task.id:
                    pass
                else:
                    raise WorkOnNewWithUnfinished(
                        'new task: %s is different with task in progress: %s' %
                        (str(task), str(self._data.task)))
            elif self._data.task:
                self.logger.info('work on unfinished task: %s' %
                                 self._data.task)

        self._data.task = task
        self._stopped = False

        while 1:
            if self._beat_func:
                self._beat_func()

            if self._stopped:
                self.logger.info('stop workflow')
                break

            if self._paused:
                self.sleep(1)
                continue

            if self._to_skip and (self._to_skip == self._data.task.id or str(
                    self._to_skip) == str(self._data.task)):
                ret = self.forward_to_end()
                if ret:
                    self._to_skip = None
                    forcmd = self._forcmd
                    self._forcmd = None
                    return forcmd
                else:
                    self._to_skip = None
                    self.logger.error('skip task:%s failed' % self._to_skip)

            self.current_node = node

            if self._forward_to:
                node = self.get_node(self._forward_to)
                self._current_node = node
                self._forward_to = None
                self.logger.debug('forward to %s' % node)

            if not self._data._internal.call_stat:
                self._dump_progress(0)
                self._data._internal.current_return = node.func()
                self._dump_progress(1)

            if isinstance(node, ConditionNode):
                if self._data._internal.current_return:
                    node = node.yes
                else:
                    node = node.no
            elif isinstance(node, GatewayNode):
                node = node.route(self._data._internal.current_return)
            else:
                node = node.next
            #important!!!
            self._data._internal.call_stat = 0

            if isinstance(node, EndNode):
                self.current_node = node
                self._dump_progress(0)
                self.logger.debug('work to end node')
                self._progress.compact()
                return True
Exemple #7
0
# -*- coding: utf-8 -*-
'''
Created on Jan 18, 2016

@author: sunmoonone
'''
from common.utils import ObjectDict
import os
import yaml
import logging
import logging.config as logging_config

settings = ObjectDict()

settings.http = ObjectDict({
    'auth_login_id': 'uid',
    'auth_login_path': '/login',
    'template_path': 'templates',
    'allow_cross_site': False
})
settings.profile = ObjectDict()
settings.profile.enabled = False

settings.kmq = ObjectDict()
settings.kmq.encoding = 'utf-8'

settings.test = ObjectDict()


class _Logger(logging.Logger):
    def clone(self, name):