1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2016 Canonical
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.
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
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
25
logger = logging.getLogger(__name__)
28
class Singleton(type):
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]
38
# Backport suppress from python3 to python2
40
"""Context manager to suppress specified exceptions
42
After the exception is suppressed, execution proceeds with the next
43
statement following the with statement.
45
with suppress(FileNotFoundError):
47
# Execution still resumes here if the file was already removed
50
def __init__(self, *exceptions):
51
self._exceptions = exceptions
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.
65
# See http://bugs.python.org/issue12029 for more details
66
return exctype is not None and issubclass(exctype, self._exceptions)
70
"""Return writable data path"""
71
return os.getenv("SNAP_DATA", os.getcwd())