Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
beat
beat.editor
Commits
0bf57ddd
Commit
0bf57ddd
authored
Jul 02, 2019
by
Samuel GAIST
Browse files
[decorators] Moved frozen decorator out of utils
And added test for it.
parent
d0b2494f
Changes
14
Hide whitespace changes
Inline
Side-by-side
beat/editor/decorators.py
0 → 100644
View file @
0bf57ddd
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# #
# Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.editor module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
"""
Decorators
"""
from
functools
import
wraps
def
frozen
(
cls
):
"""
Don't allow new attributes to be added outside of init
Based on https://stackoverflow.com/a/29368642/5843716
"""
cls
.
__frozen
=
False
def
frozensetattr
(
self
,
key
,
value
):
"""Don't allow attributes to be added outside of __init__"""
if
self
.
__frozen
and
not
hasattr
(
self
,
key
):
raise
RuntimeError
(
"Class {} is frozen. Cannot set {} = {}"
.
format
(
cls
.
__name__
,
key
,
value
)
)
else
:
object
.
__setattr__
(
self
,
key
,
value
)
def
init_decorator
(
func
):
@
wraps
(
func
)
def
wrapper
(
self
,
*
args
,
**
kwargs
):
func
(
self
,
*
args
,
**
kwargs
)
self
.
__frozen
=
True
return
wrapper
cls
.
__setattr__
=
frozensetattr
cls
.
__init__
=
init_decorator
(
cls
.
__init__
)
return
cls
beat/editor/test/test_decorators.py
0 → 100644
View file @
0bf57ddd
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.editor module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
import
pytest
from
..decorators
import
frozen
def
test_frozen
():
@
frozen
class
FrozenClass
:
pass
frozen_cls_instance
=
FrozenClass
()
with
pytest
.
raises
(
RuntimeError
):
frozen_cls_instance
.
value
=
None
beat/editor/utils.py
View file @
0bf57ddd
...
...
@@ -35,7 +35,6 @@ import logging
import
simplejson
as
json
import
pkg_resources
from
functools
import
wraps
from
packaging
import
version
from
PyQt5
import
QtCore
...
...
@@ -89,40 +88,6 @@ def setup_logger(name, verbosity):
return
logger
def
frozen
(
cls
):
"""
Don't allow new attributes to be added outside of init
Based on https://stackoverflow.com/a/29368642/5843716
"""
cls
.
__frozen
=
False
def
frozensetattr
(
self
,
key
,
value
):
"""Don't allow attributes to be added outside of __init__"""
if
self
.
__frozen
and
not
hasattr
(
self
,
key
):
print
(
"Class {} is frozen. Cannot set {} = {}"
.
format
(
cls
.
__name__
,
key
,
value
)
)
else
:
object
.
__setattr__
(
self
,
key
,
value
)
def
init_decorator
(
func
):
@
wraps
(
func
)
def
wrapper
(
self
,
*
args
,
**
kwargs
):
func
(
self
,
*
args
,
**
kwargs
)
self
.
__frozen
=
True
return
wrapper
cls
.
__setattr__
=
frozensetattr
cls
.
__init__
=
init_decorator
(
cls
.
__init__
)
return
cls
def
dataformat_basetypes
():
"""Returns the list of base types that can be used for dataformat"""
...
...
beat/editor/widgets/algorithmeditor.py
View file @
0bf57ddd
...
...
@@ -24,7 +24,7 @@
###############################################################################
from
..backend.asset
import
AssetType
from
..
util
s
import
frozen
from
..
decorator
s
import
frozen
from
.editor
import
AbstractAssetEditor
...
...
beat/editor/widgets/assetwidget.py
View file @
0bf57ddd
...
...
@@ -44,7 +44,7 @@ from PyQt5.QtWidgets import QMessageBox
from
..backend.asset
import
AssetType
from
..backend.asset
import
Asset
from
..
util
s
import
frozen
from
..
decorator
s
import
frozen
from
.editor
import
PlaceholderEditor
from
.algorithmeditor
import
AlgorithmEditor
...
...
beat/editor/widgets/databaseeditor.py
View file @
0bf57ddd
...
...
@@ -54,7 +54,7 @@ from beat.core.protocoltemplate import ProtocolTemplate
from
..backend.assetmodel
import
AssetModel
from
..backend.asset
import
AssetType
from
..
util
s
import
frozen
from
..
decorator
s
import
frozen
from
..utils
import
is_Qt_equal_or_higher
from
.editor
import
AbstractAssetEditor
...
...
beat/editor/widgets/dataformateditor.py
View file @
0bf57ddd
...
...
@@ -40,7 +40,7 @@ from PyQt5.QtWidgets import QSpinBox
from
PyQt5.QtWidgets
import
QVBoxLayout
from
PyQt5.QtWidgets
import
QWidget
from
..
util
s
import
frozen
from
..
decorator
s
import
frozen
from
..utils
import
dataformat_basetypes
from
..backend.asset
import
AssetType
...
...
beat/editor/widgets/experimenteditor.py
View file @
0bf57ddd
...
...
@@ -24,7 +24,7 @@
###############################################################################
from
..backend.asset
import
AssetType
from
..
util
s
import
frozen
from
..
decorator
s
import
frozen
from
.editor
import
AbstractAssetEditor
...
...
beat/editor/widgets/libraryeditor.py
View file @
0bf57ddd
...
...
@@ -23,7 +23,7 @@
# #
###############################################################################
from
..
util
s
import
frozen
from
..
decorator
s
import
frozen
from
..backend.asset
import
AssetType
from
..backend.assetmodel
import
AssetModel
...
...
beat/editor/widgets/plottereditor.py
View file @
0bf57ddd
...
...
@@ -38,7 +38,7 @@ from PyQt5.QtWidgets import QLineEdit
from
PyQt5.QtWidgets
import
QPushButton
from
PyQt5.QtWidgets
import
QWidget
from
..
util
s
import
frozen
from
..
decorator
s
import
frozen
from
..backend.asset
import
AssetType
from
..backend.assetmodel
import
AssetModel
...
...
beat/editor/widgets/plotterparameterseditor.py
View file @
0bf57ddd
...
...
@@ -24,7 +24,7 @@
###############################################################################
from
..backend.asset
import
AssetType
from
..
util
s
import
frozen
from
..
decorator
s
import
frozen
from
.editor
import
AbstractAssetEditor
...
...
beat/editor/widgets/protocoltemplateeditor.py
View file @
0bf57ddd
...
...
@@ -40,7 +40,7 @@ from PyQt5.QtWidgets import QTableWidgetItem
from
PyQt5.QtWidgets
import
QVBoxLayout
from
PyQt5.QtWidgets
import
QWidget
from
..
util
s
import
frozen
from
..
decorator
s
import
frozen
from
..backend.asset
import
AssetType
from
..backend.assetmodel
import
AssetModel
...
...
beat/editor/widgets/spinboxes.py
View file @
0bf57ddd
...
...
@@ -34,7 +34,7 @@ from PyQt5.QtGui import QValidator
from
PyQt5.QtWidgets
import
QAbstractSpinBox
from
..
util
s
import
frozen
from
..
decorator
s
import
frozen
@
frozen
...
...
beat/editor/widgets/toolchaineditor.py
View file @
0bf57ddd
...
...
@@ -24,7 +24,7 @@
###############################################################################
from
..backend.asset
import
AssetType
from
..
util
s
import
frozen
from
..
decorator
s
import
frozen
from
.editor
import
AbstractAssetEditor
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment