]> SALOME platform Git repositories - modules/kernel.git/blob - src/KERNEL_PY/kernel/__init__.py
Salome HOME
Merge from V6_main 01/04/2013
[modules/kernel.git] / src / KERNEL_PY / kernel / __init__.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 __all__ = [ "deprecation", "logger", "termcolor", "logconfig" ]
22
23 # WARN: This file SHOULD NOT import salome stuff so that modules of the
24 # package could be used outside of a SALOME session context when
25 # possible. For example logger.py, datamodeler.py, enumerate.py,
26 # diclookup.py, ... does not need a SALOME running application.
27
28 #
29 # ==============================================================================
30 # Generic serialization functions for the module datamodeler
31 # ==============================================================================
32 #
33 import pickle
34
35 def serialize(dataToSerialize):
36     """
37     Creates a string description of the specified data.
38     .. attribute:: dataToSerialize
39     a data object to serialize
40     """
41     serialstring = pickle.dumps(dataToSerialize)
42     return serialstring
43
44 def unserialize(stringToUnserialize):
45     """
46     Creates a data object from its string description. The string description
47     is supposed to be obtained by a call to serialize.
48     .. attribute:: stringToUnserialize
49     a string description of the object
50     """
51     data = pickle.loads(stringToUnserialize)
52     return data
53
54 class Callable:
55     """
56     This class is used to create class-method (see MetaData)
57     """
58     def __init__(self, anycallable):
59         self.__call__ = anycallable
60 #
61 # ==============================================================================
62 # Basic use cases and unit tests
63 # ==============================================================================
64 #
65
66 def TEST_serialization():
67     from testdata import TestData
68     ref_name = "my study case"
69     studyData = TestData()
70     studyData.setName(ref_name)
71
72     print "serialize data ..."
73     serialString = serialize(studyData)
74     print "unserialize data ..."
75     unserialData = unserialize(serialString)
76
77     res_name = unserialData.getName()
78     print res_name
79     if ( ref_name != res_name ):
80         return False
81
82     unserialData.log()
83     unserialData.setName("an other name")
84
85     return True
86
87 if __name__ == "__main__":
88     import unittester
89     unittester.run("salome/kernel/__init__","TEST_serialization")