Package conduit :: Package utils :: Module Singleton
[hide private]

Source Code for Module conduit.utils.Singleton

 1  #!/usr/bin/env python 
 2  # -*- coding: UTF8 -*- 
 3  # 
 4  #  GObjectSingleton.py 
 5  #  Copyright (c) 2006 INdT (Instituto Nokia de Tecnologia) 
 6  #  Author: Eduardo de Barros Lima <eduardo.lima@indt.org.br> 
 7  # 
 8  #  This program is free software; you can redistribute it and/or 
 9  #  modify it under the terms of the GNU Lesser General Public License as 
10  #  published by the Free Software Foundation; either version 2.1 of the 
11  #  License, or (at your option) any later version. 
12  # 
13  #  This program is distributed in the hope that it will be useful, 
14  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
15  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
16  #  GNU Lesser General Public License for more details. 
17  # 
18  #  You should have received a copy of the GNU Lesser General Public License 
19  #  along with this program; if not, write to the Free Software 
20  #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 
21  #  USA 
22   
23  import gobject 
24   
25 -class GObjectSingleton(gobject.GObjectMeta):
26
27 - def __init__(cls, name, base, dict):
28 gobject.GObjectMeta.__init__(cls, name, base, dict) 29 cls.__instance = None 30 cls.__copy__ = lambda self: self 31 cls.__deepcopy__ = lambda self, memo=None: self
32
33 - def __call__(cls, *args, **kwargs):
34 if not cls.__instance: 35 cls.__instance = super(GObjectSingleton, cls).__call__(*args, **kwargs) 36 return cls.__instance
37
38 -class Singleton:
39 """ 40 A model that implements the Singleton pattern. 41 """ 42 43 __metaclass__ = GObjectSingleton 44 45 pass
46