コード例 #1
0
 def __init__(self,
              view,
              key,
              iclass,
              ctxid=None,
              match_src=None,
              match_pairs=None,
              load=True):
     self.view = view
     self.ndb = view.ndb
     self.sources = view.ndb.sources
     self.ctxid = ctxid
     self.schema = view.ndb.schema
     self.match_src = match_src or tuple()
     self.match_pairs = match_pairs or dict()
     self.changed = set()
     self.iclass = iclass
     self.utable = self.utable or self.table
     self.errors = []
     self.atime = time.time()
     self.log = self.ndb.log.channel('rtnl_object')
     self.log.debug('init')
     self.state = State()
     self.state.set('invalid')
     self.snapshot_deps = []
     self.load_event = threading.Event()
     self.load_event.set()
     self.lock = threading.Lock()
     self.kspec = self.schema.compiled[self.table]['idx']
     self.knorm = self.schema.compiled[self.table]['norm_idx']
     self.spec = self.schema.compiled[self.table]['all_names']
     self.names = self.schema.compiled[self.table]['norm_names']
     self._apply_script = []
     if isinstance(key, dict):
         self.chain = key.pop('ndb_chain', None)
         create = key.pop('create', False)
     else:
         self.chain = None
         create = False
     ckey = self.complete_key(key)
     if create:
         if ckey is not None:
             raise KeyError('object exists')
         for name in key:
             self[name] = key[name]
         # FIXME -- merge with complete_key()
         if 'target' not in self:
             self.load_value('target', self.view.default_target)
     else:
         if ckey is None:
             raise KeyError('object does not exists')
         self.key = ckey
         if load:
             if ctxid is None:
                 self.load_sql()
             else:
                 self.load_sql(table=self.table)
コード例 #2
0
ファイル: rtnl_object.py プロジェクト: groupvrg/pyroute2
 def __init__(self,
              view,
              key,
              iclass,
              ctxid=None,
              match_src=None,
              match_pairs=None):
     self.view = view
     self.ndb = view.ndb
     self.sources = view.ndb.sources
     self.ctxid = ctxid
     self.schema = view.ndb.schema
     self.match_src = match_src or tuple()
     self.match_pairs = match_pairs or dict()
     self.changed = set()
     self.iclass = iclass
     self.utable = self.utable or self.table
     self.errors = []
     self.log = Log()
     self.log.append('init')
     self.state = State()
     self.state.set('invalid')
     self.snapshot_deps = []
     self.load_event = threading.Event()
     self.lock = threading.Lock()
     self.kspec = ('target', ) + self.schema.indices[self.table]
     self.spec = self.schema.compiled[self.table]['all_names']
     self.names = self.schema.compiled[self.table]['norm_names']
     create = key.pop('create', False) if isinstance(key, dict) else False
     ckey = self.complete_key(key)
     if create and ckey is not None:
         raise KeyError('object exists')
     elif not create and ckey is None:
         raise KeyError('object does not exists')
     elif create:
         for name in key:
             self[name] = key[name]
         # FIXME -- merge with complete_key()
         if 'target' not in self:
             self.load_value('target', 'localhost')
     elif ctxid is None:
         self.key = ckey
         self.load_sql()
     else:
         self.key = ckey
         self.load_sql(table=self.table)
コード例 #3
0
    def __init__(self, ndb, **spec):
        self.th = None
        self.nl = None
        self.ndb = ndb
        self.evq = self.ndb._event_queue
        # the target id -- just in case
        self.target = spec['target']
        self.kind = spec.pop('kind', 'local')
        self.persistent = spec.pop('persistent', True)
        self.event = spec.pop('event')
        # RTNL API
        self.nl_prime = self.vmap[self.kind]
        self.nl_kwarg = spec
        #
        if self.ndb.messenger is not None:
            self.ndb.messenger.targets.add(self.target)
        #
        self.shutdown = threading.Event()
        self.started = threading.Event()
        self.lock = threading.RLock()
        self.shutdown_lock = threading.RLock()
        self.started.clear()
        self.log = ndb.log.channel('sources.%s' % self.target)
        self.state = State(log=self.log)
        self.state.set('init')
        self.ndb.schema.execute(
            '''
                                INSERT INTO sources (f_target, f_kind)
                                VALUES (%s, %s)
                                ''' %
            (self.ndb.schema.plch, self.ndb.schema.plch),
            (self.target, self.kind))
        for key, value in spec.items():
            vtype = 'int' if isinstance(value, int) else 'str'
            self.ndb.schema.execute(
                '''
                                    INSERT INTO sources_options
                                    (f_target, f_name, f_type, f_value)
                                    VALUES (%s, %s, %s, %s)
                                    ''' %
                (self.ndb.schema.plch, self.ndb.schema.plch,
                 self.ndb.schema.plch, self.ndb.schema.plch),
                (self.target, key, vtype, value))

        self.load_sql()
コード例 #4
0
ファイル: source.py プロジェクト: groupvrg/pyroute2
    def __init__(self, ndb, **spec):
        self.th = None
        self.nl = None
        self.ndb = ndb
        self.evq = self.ndb._event_queue
        # the target id -- just in case
        self.target = spec.pop('target')
        kind = spec.pop('kind', 'local')
        self.persistent = spec.pop('persistent', True)
        self.event = spec.pop('event')
        if not self.event:
            self.event = SyncStart()
        # RTNL API
        self.nl_prime = self.vmap[kind]
        self.nl_kwarg = spec
        #
        self.shutdown = threading.Event()
        self.started = threading.Event()
        self.lock = threading.RLock()
        self.started.clear()
        self.state = State()
        self.log = Log()
        self.state.set('init')
        self.ndb.schema.execute(
            '''
                                INSERT INTO sources (f_target, f_kind)
                                VALUES (%s, %s)
                                ''' %
            (self.ndb.schema.plch, self.ndb.schema.plch), (self.target, kind))
        for key, value in spec.items():
            vtype = 'int' if isinstance(value, int) else 'str'
            self.ndb.schema.execute(
                '''
                                    INSERT INTO options (f_target,
                                                         f_name,
                                                         f_type,
                                                         f_value)
                                    VALUES (%s, %s, %s, %s)
                                    ''' %
                (self.ndb.schema.plch, self.ndb.schema.plch,
                 self.ndb.schema.plch, self.ndb.schema.plch),
                (self.target, key, vtype, value))

        self.load_sql()
コード例 #5
0
 def __init__(self,
              view,
              key,
              iclass,
              ctxid=None,
              load=True,
              master=None,
              check=True,
              auth_managers=None):
     self.view = view
     self.ndb = view.ndb
     self.sources = view.ndb.sources
     self.master = master
     self.ctxid = ctxid
     self.schema = view.ndb.schema
     self.changed = set()
     self.iclass = iclass
     self.utable = self.utable or self.table
     self.errors = []
     self.atime = time.time()
     self.log = self.ndb.log.channel('rtnl_object')
     self.log.debug('init')
     if auth_managers is None:
         auth_managers = [AuthManager(None, self.ndb.log.channel('auth'))]
     self.auth_managers = auth_managers
     self.state = State()
     self.state.set('invalid')
     self.snapshot_deps = []
     self.load_event = threading.Event()
     self.load_event.set()
     self.load_debug = False
     self.lock = threading.Lock()
     self.kspec = self.schema.compiled[self.table]['idx']
     self.knorm = self.schema.compiled[self.table]['norm_idx']
     self.spec = self.schema.compiled[self.table]['all_names']
     self.names = self.schema.compiled[self.table]['norm_names']
     self.last_save = None
     if self.event_map is None:
         self.event_map = {}
     self._apply_script = []
     if isinstance(key, dict):
         self.chain = key.pop('ndb_chain', None)
         create = key.pop('create', False)
     else:
         self.chain = None
         create = False
     exists = self.exists(key)
     ckey = self.complete_key(key)
     if create:
         if check & exists:
             raise KeyError('object exists')
         for name in key:
             self[self.iclass.nla2name(name)] = key[name]
         # FIXME -- merge with complete_key()
         if 'target' not in self:
             self.load_value('target', self.view.default_target)
     else:
         if not exists:
             raise KeyError('object does not exists')
         self.key = ckey
         if load:
             if ctxid is None:
                 self.load_sql()
             else:
                 self.load_sql(table=self.table)
     self._init_complete = True