本文整理汇总了Python中serializer.Serializer.serialize方法的典型用法代码示例。如果您正苦于以下问题:Python Serializer.serialize方法的具体用法?Python Serializer.serialize怎么用?Python Serializer.serialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类serializer.Serializer
的用法示例。
在下文中一共展示了Serializer.serialize方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testSimpleSerialization
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import serialize [as 别名]
def testSimpleSerialization(self):
m = self.createTestMessage()
serialized = StringIO(Serializer.serialize(m))
deserialized = Serializer.deserialize(serialized, TestMessage1)
self.assertDictEqual(m.__dict__, deserialized.__dict__)
示例2: RPCProxy
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import serialize [as 别名]
class RPCProxy(object):
def __init__(self, uri, method=None, namespaces=None, sid=''):
self.__serviceURL = uri
self.__serviceName = method
self.namespaces = isinstance(namespaces, ClassLoader) and namespaces or \
ClassLoader(namespaces or [])
self._seril = Serializer(self.namespaces)
self.sid = sid
self.logger = None
self.start_call_listener = []
self.end_call_listener = []
def __call__(self, *args, **kw):
args = self._seril.serialize((args, kw))
post_data = {"method": self.__serviceName,
'params': args,
'id':'httprpc',
'sid':self.sid}
#@todo xx
for l in self.start_call_listener:
l(name=self.__serviceName, args=args, kw_args=kw)
rpc_response = self.post_request(self.__serviceURL, post_data)
try:
respdata = rpc_response.read()
ret, e = self._seril.deserialize(respdata)
except Exception, e:
raise RPCException("Failed to deserialize response data:%s, exception:%s" %
(respdata, e))
finally:
示例3: TestSerialize
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import serialize [as 别名]
class TestSerialize(unittest.TestCase):
@classmethod
def _clearMsgDefs(self):
# Clear all non-test messages
try:
while True:
messages.MESSAGES.pop()
except IndexError:
pass
@classmethod
def setUpClass(self):
self._clearMsgDefs()
messages.MESSAGES.append(__import__("test_complex_pb2", globals(), locals(), [], -1))
def setUp(self):
self.calls = 0
self.s = Serializer()
def test_unnamed_serialize(self):
enc = self.s.serialize("TestComplex", [("normal", ["m1", "m2"])])
def f(obj):
self.assertTrue(obj.IsInitialized())
self.assertEqual(obj.nesteds[0].normal, "normal")
self.assertEqual(obj.nesteds[0].multiple[0], "m1")
self.assertEqual(obj.nesteds[0].multiple[1], "m2")
self.calls += 1
self.s.add_handler("TestComplex", f)
self.s.unserialize(enc)
self.assertEqual(self.calls, 1)
def test_named_serialize(self):
enc = self.s.serialize("TestComplex",
nesteds = [{
"normal": "normal",
"multiple": [
"m1",
"m2"
]
}]
)
def f(obj):
self.assertTrue(obj.IsInitialized())
self.assertEqual(obj.nesteds[0].normal, "normal")
self.assertEqual(obj.nesteds[0].multiple[0], "m1")
self.assertEqual(obj.nesteds[0].multiple[1], "m2")
self.calls += 1
self.s.add_handler("TestComplex", f)
self.s.unserialize(enc)
self.assertEqual(self.calls, 1)
def test_nested_length(self):
with self.assertRaises(FieldTooLongException):
self.s.serialize("TestComplex", [("", ["longerthantwentycharacters"])])
def test_obj_onto_repeated(self):
with self.assertRaises(FieldWrongTypeException):
self.s.serialize("TestComplex", [("", ("m1",))])
def test_repeated_onto_obj(self):
with self.assertRaises(FieldWrongTypeException):
self.s.serialize("TestComplex", [["", ["m1"]]])
def test_unicode_strings(self):
sbs = '}\xc8A\xc1\x8a}D\xe8\xea\x93}'
enc = self.s.serialize("TestComplex", [(sbs, ["m1", "m2"])])
def f(obj):
self.assertTrue(obj.IsInitialized())
for i in range(len(sbs)):
self.assertEqual(ord(obj.nesteds[0].normal[i]), ord(sbs[i]))
self.assertEqual(obj.nesteds[0].multiple[0], "m1")
self.assertEqual(obj.nesteds[0].multiple[1], "m2")
self.calls += 1
self.s.add_handler("TestComplex", f)
self.s.unserialize(enc)
self.assertEqual(self.calls, 1)
def test_optional_missing(self):
enc = self.s.serialize("TestOptional")
def f(obj):
self.assertTrue(obj.IsInitialized())
self.assertTrue(not obj.field)
self.calls += 1
self.s.add_handler("TestOptional", f)
self.s.unserialize(enc)
self.assertEqual(self.calls, 1)
def test_optional_set(self):
enc = self.s.serialize("TestOptional", "test")
def f(obj):
#.........这里部分代码省略.........
示例4: serialize_to_string
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import serialize [as 别名]
def serialize_to_string(self):
serializer = Serializer()
return serializer.serialize(self)
示例5: TestSerialize
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import serialize [as 别名]
class TestSerialize(unittest.TestCase):
@classmethod
def _clearMsgDefs(self):
# Clear all non-test messages
try:
while True:
messages.MESSAGES.pop()
except IndexError:
pass
@classmethod
def setUpClass(self):
self._clearMsgDefs()
messages.MESSAGES.append(__import__("test_pb2", globals(), locals(), [], -1))
self.blank_args = [1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, True, 0, "a", "b", "c", "d"]
self.neg_args = [-1.0, 0, -1.0, 0, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, -1, 0, 0, 0, 0, -1, -1, -1, -1, 1, 1, "", "", "", ""]
self.illegal_len_args = [
[0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""]
]
self.too_long_args = [
[0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32768, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -32769, 0, 0, "", "", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "aaa", "", ""],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", "", "", "aaa"]
]
def setUp(self):
self.s = Serializer()
def test_id(self):
enc = self.s.serialize("Test", *self.blank_args)
(name, ) = struct.unpack_from('20s', enc)
self.assertTrue(name.startswith('test_pb2Test'))
def test_values(self):
enc = self.s.serialize("Test", *self.blank_args)
def f(obj):
self.assertTrue(obj.IsInitialized())
for (_, value) in obj.ListFields():
self.assertIn(value, self.blank_args)
self.s.add_handler("Test", f)
self.s.unserialize(enc)
def test_explicit_call(self):
enc = self.s.serialize("test.Test", *self.blank_args)
def f(obj):
self.assertTrue(obj.IsInitialized())
for (_, value) in obj.ListFields():
self.assertIn(value, self.blank_args)
self.s.add_handler("Test", f)
self.s.unserialize(enc)
def test_negative_values(self):
enc = self.s.serialize("Test", *self.neg_args)
def f(obj):
self.assertTrue(obj.IsInitialized())
for (_, value) in obj.ListFields():
self.assertIn(value, self.neg_args)
self.s.add_handler("Test", f)
self.s.unserialize(enc)
def test_illegal_length_option(self):
for args in self.illegal_len_args:
with self.assertRaises(FieldLengthUnsupportedException):
self.s.serialize("Test", *args)
def test_too_long_option(self):
for args in self.too_long_args:
with self.assertRaises(FieldTooLongException):
self.s.serialize("Test", *args)
def test_unknown_message(self):
with self.assertRaises(UnknownMessageException):
self.s.serialize("Unknown")
def test_unknown_message_explicit(self):
with self.assertRaises(UnknownMessageException):
#.........这里部分代码省略.........
示例6: SyncObj
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import serialize [as 别名]
class SyncObj(object):
def __init__(self, selfNodeAddr, otherNodesAddrs, conf=None):
if conf is None:
self.__conf = SyncObjConf()
else:
self.__conf = conf
self.__selfNodeAddr = selfNodeAddr
self.__otherNodesAddrs = otherNodesAddrs
self.__unknownConnections = {} # descr => _Connection
self.__raftState = _RAFT_STATE.FOLLOWER
self.__raftCurrentTerm = 0
self.__votesCount = 0
self.__raftLeader = None
self.__raftElectionDeadline = time.time() + self.__generateRaftTimeout()
self.__raftLog = [] # (command, logID, term)
self.__raftLog.append((None, 1, self.__raftCurrentTerm))
self.__raftCommitIndex = 1
self.__raftLastApplied = 1
self.__raftNextIndex = {}
self.__raftMatchIndex = {}
self.__lastSerializedTime = time.time()
self.__forceLogCompaction = False
self.__socket = None
self.__resolver = DnsCachingResolver(self.__conf.dnsCacheTime, self.__conf.dnsFailCacheTime)
self.__serializer = Serializer(self.__conf.fullDumpFile, self.__conf.logCompactionBatchSize)
self.__poller = createPoller()
self.__isInitialized = False
self.__lastInitTryTime = 0
self._methodToID = {}
self._idToMethod = {}
methods = sorted([m for m in dir(self) if callable(getattr(self, m))])
for i, method in enumerate(methods):
self._methodToID[method] = i
self._idToMethod[i] = getattr(self, method)
self.__thread = None
self.__mainThread = None
self.__initialised = None
self.__commandsQueue = Queue.Queue(self.__conf.commandsQueueSize)
self.__nodes = []
self.__newAppendEntriesTime = 0
self.__commandsWaitingCommit = collections.defaultdict(list) # logID => [(termID, callback), ...]
self.__commandsLocalCounter = 0
self.__commandsWaitingReply = {} # commandLocalCounter => callback
self.__properies = set()
for key in self.__dict__:
self.__properies.add(key)
if self.__conf.autoTick:
self.__mainThread = threading.current_thread()
self.__initialised = threading.Event()
self.__thread = threading.Thread(target=SyncObj._autoTickThread, args=(weakref.proxy(self),))
self.__thread.start()
while not self.__initialised.is_set():
pass
else:
self.__initInTickThread()
def __initInTickThread(self):
try:
self.__lastInitTryTime = time.time()
self.__bind()
self.__nodes = []
for nodeAddr in self.__otherNodesAddrs:
self.__nodes.append(Node(self, nodeAddr))
self.__raftNextIndex[nodeAddr] = 0
self.__raftMatchIndex[nodeAddr] = 0
self.__needLoadDumpFile = True
self.__isInitialized = True
except:
LOG_CURRENT_EXCEPTION()
def _applyCommand(self, command, callback):
try:
self.__commandsQueue.put_nowait((command, callback))
except Queue.Full:
callback(None, FAIL_REASON.QUEUE_FULL)
def _checkCommandsToApply(self):
for i in xrange(self.__conf.maxCommandsPerTick):
if self.__raftLeader is None and self.__conf.commandsWaitLeader:
break
try:
command, callback = self.__commandsQueue.get_nowait()
except Queue.Empty:
break
requestNode, requestID = None, None
if isinstance(callback, tuple):
requestNode, requestID = callback
if self.__raftState == _RAFT_STATE.LEADER:
idx, term = self.__getCurrentLogIndex() + 1, self.__raftCurrentTerm
self.__raftLog.append((command, idx, term))
if requestNode is None:
#.........这里部分代码省略.........
示例7: read
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import serialize [as 别名]
def read(self, size=-1, offset=0):
"""
Returns an attached object representation as YAML file (bytestring).
"""
return Serializer.serialize(self.model_instance) # binary?
示例8: SyncObj
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import serialize [as 别名]
class SyncObj(object):
def __init__(self, selfNodeAddr, otherNodesAddrs, conf=None):
if conf is None:
self.__conf = SyncObjConf()
else:
self.__conf = conf
if self.__conf.password is not None:
if not HAS_CRYPTO:
raise ImportError("Please install 'cryptography' module")
self.__encryptor = getEncryptor(self.__conf.password)
else:
self.__encryptor = None
self.__selfNodeAddr = selfNodeAddr
self.__otherNodesAddrs = otherNodesAddrs
self.__unknownConnections = {} # descr => _Connection
self.__raftState = _RAFT_STATE.FOLLOWER
self.__raftCurrentTerm = 0
self.__votedFor = None
self.__votesCount = 0
self.__raftLeader = None
self.__raftElectionDeadline = time.time() + self.__generateRaftTimeout()
self.__raftLog = [] # (command, logID, term)
self.__raftLog.append(('', 1, self.__raftCurrentTerm))
self.__raftCommitIndex = 1
self.__raftLastApplied = 1
self.__raftNextIndex = {}
self.__raftMatchIndex = {}
self.__lastSerializedTime = time.time()
self.__forceLogCompaction = False
self.__leaderCommitIndex = None
self.__onReadyCalled = False
self.__startTime = time.time()
globalDnsResolver().setTimeouts(self.__conf.dnsCacheTime, self.__conf.dnsFailCacheTime)
self.__serializer = Serializer(self.__conf.fullDumpFile, self.__conf.logCompactionBatchSize)
self.__isInitialized = False
self.__lastInitTryTime = 0
self._poller = createPoller()
host, port = selfNodeAddr.split(':')
self.__server = TcpServer(self._poller, host, port, onNewConnection=self.__onNewConnection,
sendBufferSize=self.__conf.sendBufferSize,
recvBufferSize=self.__conf.recvBufferSize,
connectionTimeout=self.__conf.connectionTimeout)
self._methodToID = {}
self._idToMethod = {}
methods = sorted([m for m in dir(self) if callable(getattr(self, m))])
for i, method in enumerate(methods):
self._methodToID[method] = i
self._idToMethod[i] = getattr(self, method)
self.__thread = None
self.__mainThread = None
self.__initialised = None
self.__commandsQueue = Queue.Queue(self.__conf.commandsQueueSize)
self.__nodes = []
self.__newAppendEntriesTime = 0
self.__commandsWaitingCommit = collections.defaultdict(list) # logID => [(termID, callback), ...]
self.__commandsLocalCounter = 0
self.__commandsWaitingReply = {} # commandLocalCounter => callback
self.__properies = set()
for key in self.__dict__:
self.__properies.add(key)
if self.__conf.autoTick:
self.__mainThread = threading.current_thread()
self.__initialised = threading.Event()
self.__thread = threading.Thread(target=SyncObj._autoTickThread, args=(weakref.proxy(self),))
self.__thread.start()
while not self.__initialised.is_set():
pass
else:
self.__initInTickThread()
def __initInTickThread(self):
try:
self.__lastInitTryTime = time.time()
self.__server.bind()
self.__nodes = []
for nodeAddr in self.__otherNodesAddrs:
self.__nodes.append(Node(self, nodeAddr))
self.__raftNextIndex[nodeAddr] = 0
self.__raftMatchIndex[nodeAddr] = 0
self.__needLoadDumpFile = True
self.__isInitialized = True
except:
LOG_CURRENT_EXCEPTION()
def _applyCommand(self, command, callback):
try:
self.__commandsQueue.put_nowait((command, callback))
except Queue.Full:
self.__callErrCallback(FAIL_REASON.QUEUE_FULL, callback)
def _checkCommandsToApply(self):
#.........这里部分代码省略.........
示例9: SyncObj
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import serialize [as 别名]
class SyncObj(object):
def __init__(self, selfNodeAddr, otherNodesAddrs, conf=None):
"""
Main SyncObj class, you should inherit your own class from it.
:param selfNodeAddr: address of the current node server, 'host:port'
:type selfNodeAddr: str
:param otherNodesAddrs: addresses of partner nodes, ['host1:port1', 'host2:port2', ...]
:type otherNodesAddrs: list of str
:param conf: configuration object
:type conf: SyncObjConf
"""
if conf is None:
self.__conf = SyncObjConf()
else:
self.__conf = conf
self.__conf.validate()
if self.__conf.password is not None:
if not HAS_CRYPTO:
raise ImportError("Please install 'cryptography' module")
self.__encryptor = getEncryptor(self.__conf.password)
else:
self.__encryptor = None
self.__selfNodeAddr = selfNodeAddr
self.__otherNodesAddrs = otherNodesAddrs
self.__unknownConnections = {} # descr => _Connection
self.__raftState = _RAFT_STATE.FOLLOWER
self.__raftCurrentTerm = 0
self.__votedFor = None
self.__votesCount = 0
self.__raftLeader = None
self.__raftElectionDeadline = time.time() + self.__generateRaftTimeout()
self.__raftLog = createJournal(self.__conf.journalFile)
if len(self.__raftLog) == 0:
self.__raftLog.add(_bchr(_COMMAND_TYPE.NO_OP), 1, self.__raftCurrentTerm)
self.__raftCommitIndex = 1
self.__raftLastApplied = 1
self.__raftNextIndex = {}
self.__raftMatchIndex = {}
self.__lastSerializedTime = time.time()
self.__lastSerializedEntry = None
self.__forceLogCompaction = False
self.__leaderCommitIndex = None
self.__onReadyCalled = False
self.__changeClusterIDx = None
self.__noopIDx = None
self.__destroying = False
self.__recvTransmission = ''
self.__startTime = time.time()
globalDnsResolver().setTimeouts(self.__conf.dnsCacheTime, self.__conf.dnsFailCacheTime)
self.__serializer = Serializer(self.__conf.fullDumpFile,
self.__conf.logCompactionBatchSize,
self.__conf.useFork,
self.__conf.serializer,
self.__conf.deserializer,
self.__conf.serializeChecker)
self.__isInitialized = False
self.__lastInitTryTime = 0
self._poller = createPoller(self.__conf.pollerType)
if selfNodeAddr is not None:
bindAddr = self.__conf.bindAddress or selfNodeAddr
host, port = bindAddr.split(':')
self.__server = TcpServer(self._poller, host, port, onNewConnection=self.__onNewConnection,
sendBufferSize=self.__conf.sendBufferSize,
recvBufferSize=self.__conf.recvBufferSize,
connectionTimeout=self.__conf.connectionTimeout)
self._methodToID = {}
self._idToMethod = {}
methods = sorted([m for m in dir(self) if callable(getattr(self, m))])
for i, method in enumerate(methods):
self._methodToID[method] = i
self._idToMethod[i] = getattr(self, method)
self.__thread = None
self.__mainThread = None
self.__initialised = None
self.__bindedEvent = threading.Event()
self.__bindRetries = 0
self.__commandsQueue = FastQueue(self.__conf.commandsQueueSize)
if not self.__conf.appendEntriesUseBatch:
self.__pipeNotifier = PipeNotifier(self._poller)
self.__nodes = []
self.__readonlyNodes = []
self.__readonlyNodesCounter = 0
self.__lastReadonlyCheck = 0
self.__newAppendEntriesTime = 0
self.__commandsWaitingCommit = collections.defaultdict(list) # logID => [(termID, callback), ...]
self.__commandsLocalCounter = 0
self.__commandsWaitingReply = {} # commandLocalCounter => callback
#.........这里部分代码省略.........
示例10: sync
# 需要导入模块: from serializer import Serializer [as 别名]
# 或者: from serializer.Serializer import serialize [as 别名]
def sync(self, obj_to_sync, cascade=False):
""" syncs a given object to the server (updates or creates a new one).
cascade: True/False
Arguments:
obj_to_sync:a python object to sync. If an object has gnode attribute,
it means it will be updated on the server. If no gnode
attribute is found a new object will be submitted.
cascade: sync all children recursively (True/False)
"""
supp_models = [m for k, m in models_map.items() if \
not k in ['property', 'value']]
if not obj_to_sync.__class__ in supp_models:
raise TypeError('Objects of that type are not supported.')
processed = [] # collector of permalinks of processed objects
to_clean = [] # collector of ids of objects to clean parent
stack = [ obj_to_sync ] # a stack of objects to sync
self._cache.add_object(obj_to_sync) # if not yet there
while len( stack ) > 0:
obj = stack[0] # take first object from stack
success = False # flag to indicate success of the syncing
cls = self._meta.get_type_by_obj( obj ) # type of the object, like 'segment'
# bloody workaround for duplications because of NEO
obj_descr = self._meta.get_gnode_descr(obj)
if obj_descr and obj_descr['permalink'] in processed:
stack.remove( obj )
continue
# 1. validate class type
if not obj.__class__ in supported_models:
# skip this object completely
stack.remove( obj )
print_status('Object %s is not supported.\n' % cut_to_render( obj.__repr__() ))
continue
# 2. pre-push new/changed array data to the server (+put in cache)
# data_refs is a dict like {'signal': 'http://host:/neo/signal/148348', ...}
try:
data_refs = self.__push_data_from_obj( obj )
except (errors.FileUploadError, errors.UnitsError), e:
# skip this object completely
stack.remove( obj )
print_status('%s skipped: %s\n' % (cut_to_render(obj.__repr__(), 15), str(e)))
continue
# 3. pre-sync related metadata if exists (+put in cache)
if hasattr(obj, 'metadata'):
metadata = getattr(obj, 'metadata')
if isinstance(metadata, Metadata):
to_sync = []
for name, prp in metadata.__dict__.items():
if prp.value:
if not self._meta.get_gnode_descr(prp.value):
to_sync.insert(0, prp.value) # sync value if never synced
if not self._meta.get_gnode_descr(prp):
to_sync.insert(0, prp) # sync property if never synced
if not prp.parent:
print_status('Cannot sync %s for %s: section is not defined.\n' % \
(name, cut_to_render( obj.__repr__() )))
stack.remove( prp )
continue # move to other property
if not self._meta.get_gnode_descr(prp.parent):
to_sync.insert(0, prp.parent) # sync parent section
if to_sync: # sync what's needed first
stack = to_sync + stack
continue
# 4. sync main object
try:
json_obj = Serializer.serialize(obj, self._meta, data_refs)
# TODO ideally the JSON object representation should be unique
# and this code excluded
for k in list( json_obj['fields'].keys() ):
if k.endswith('_set') or k == 'shared_with':
json_obj['fields'].pop( k, None )
raw_json = self._remote.save( json_obj )
if not raw_json == 304:
# update local in-memory object with newly acquired params
self._meta.set_gnode_descr(obj, raw_json)
# a list of children in the gnode attribute in all parent
# objects for obj must be updated with a newly synced child. it
# should be done here, not at the end of the sync, to keep
# objects updated in case the sync fails.
Serializer.update_parent_children(obj, self._meta)
#.........这里部分代码省略.........