~didrocks/+junk/face-detection-15.04

« back to all changes in this revision

Viewing changes to facedetection/tools.py

  • Committer: Didier Roche
  • Date: 2016-05-10 23:09:11 UTC
  • Revision ID: didier.roche@canonical.com-20160510230911-c7xr490zrj3yrzxd
New version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright (C) 2016 Canonical
 
3
#
 
4
# Authors:
 
5
#  Didier Roche
 
6
#
 
7
# This program is free software; you can redistribute it and/or modify it under
 
8
# the terms of the GNU General Public License as published by the Free Software
 
9
# Foundation; version 3.
 
10
#
 
11
# This program is distributed in the hope that it will be useful, but WITHOUT
 
12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
14
# details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License along with
 
17
# this program; if not, write to the Free Software Foundation, Inc.,
 
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
19
 
 
20
import logging
 
21
import os
 
22
import signal
 
23
import sys
 
24
 
 
25
logger = logging.getLogger(__name__)
 
26
 
 
27
 
 
28
class Singleton(type):
 
29
 
 
30
    _instances = {}
 
31
 
 
32
    def __call__(cls, *args, **kwargs):
 
33
        if cls not in cls._instances:
 
34
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
 
35
        return cls._instances[cls]
 
36
 
 
37
 
 
38
# Backport suppress from python3 to python2
 
39
class suppress:
 
40
    """Context manager to suppress specified exceptions
 
41
 
 
42
    After the exception is suppressed, execution proceeds with the next
 
43
    statement following the with statement.
 
44
 
 
45
         with suppress(FileNotFoundError):
 
46
             os.remove(somefile)
 
47
         # Execution still resumes here if the file was already removed
 
48
    """
 
49
 
 
50
    def __init__(self, *exceptions):
 
51
        self._exceptions = exceptions
 
52
 
 
53
    def __enter__(self):
 
54
        pass
 
55
 
 
56
    def __exit__(self, exctype, excinst, exctb):
 
57
        # Unlike isinstance and issubclass, CPython exception handling
 
58
        # currently only looks at the concrete type hierarchy (ignoring
 
59
        # the instance and subclass checking hooks). While Guido considers
 
60
        # that a bug rather than a feature, it's a fairly hard one to fix
 
61
        # due to various internal implementation details. suppress provides
 
62
        # the simpler issubclass based semantics, rather than trying to
 
63
        # exactly reproduce the limitations of the CPython interpreter.
 
64
        #
 
65
        # See http://bugs.python.org/issue12029 for more details
 
66
        return exctype is not None and issubclass(exctype, self._exceptions)
 
67
 
 
68
 
 
69
def get_data_path():
 
70
    """Return writable data path"""
 
71
    return os.getenv("SNAP_DATA", os.getcwd())