-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved control over code changes (#925)
Implemented the formalised code changes described in PR #920. **Benefits**: Make it possible to improve our control over code changes. Provide predictability for when changes will happen and give users a time-span to a given versions number to adapt to new changes. * Improved deprecation warnings and initial implementation of behavior. * Added a simple Python class for accessing behavior from Python * Added Python interface to behaviours. * Added initialisation of behavior table and simplified the logics. * Fixed a bug in natoi() * Fix documentation strings. * Documented environment variables DLITE_BEHAVIOR_* * Added more instructions to warning in dlite_behavior_get() --------- Co-authored-by: Francesca L. Bleken <48128015+francescalb@users.noreply.github.com>
- Loading branch information
1 parent
c96db68
commit bd8dc6b
Showing
19 changed files
with
867 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* -*- Python -*- (not really, but good for syntax highlighting) */ | ||
|
||
/* Python-specific extensions to dlite-behavior.i */ | ||
|
||
|
||
%extend _DLiteBehavior { | ||
%pythoncode %{ | ||
def asdict(self): | ||
return { | ||
"name": self.name, | ||
"value": bool(self.value), | ||
"version_added": self.version_added, | ||
"version_new": self.version_new, | ||
"version_remove": self.version_remove, | ||
"description": self.description, | ||
} | ||
|
||
def __str__(self): | ||
import json | ||
return json.dumps(self.asdict(), indent=2) | ||
%} | ||
} | ||
|
||
|
||
%pythoncode %{ | ||
|
||
class _Behavior(): | ||
"""A class that provides easy access for setting and getting behavior | ||
settings. | ||
It is used via the singleton `dlite.Behavior`. | ||
The different behavior values can be accessed as attributes. | ||
""" | ||
def __getattr__(self, name): | ||
return bool(_dlite._behavior_get(name)) | ||
|
||
def __setattr__(self, name, value): | ||
_dlite._behavior_set(name, 1 if value else 0) | ||
|
||
def __dir__(self): | ||
return object.__dir__(self) + list(self.get_names()) | ||
|
||
def get_names(self): | ||
"""Return a generator over all registered behavior names.""" | ||
for i in range(_dlite._behavior_nrecords()): | ||
yield _dlite._behavior_recordno(i).name | ||
|
||
def get_record(self, name): | ||
"""Return a record with information about the given behavior.""" | ||
rec = _dlite._behavior_record(name) | ||
if not rec: | ||
raise DLiteKeyError(f"No such behavior record: {name}") | ||
return rec | ||
|
||
|
||
# A singleton for accessing behavior settings. | ||
Behavior = _Behavior() | ||
|
||
%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* -*- C -*- (not really, but good for syntax highlighting) */ | ||
%{ | ||
#include "dlite-behavior.h" | ||
%} | ||
|
||
%rename(_BehaviorRecord) _DLiteBehavior; | ||
struct _DLiteBehavior { | ||
%immutable; | ||
char *name; | ||
char *version_added; | ||
char *version_new; | ||
char *version_remove; | ||
char *description; | ||
%mutable; | ||
int value; | ||
}; | ||
|
||
%rename("%(strip:[dlite])s") ""; | ||
|
||
|
||
%feature("docstring", "\ | ||
Return the number of registered behaviors. | ||
") dlite_behavior_nrecords; | ||
size_t dlite_behavior_nrecords(void); | ||
|
||
%feature("docstring", "\ | ||
Return a pointer to record with the given number or NULL if `n` is out of | ||
range. | ||
") dlite_behavior_recordno; | ||
const struct _DLiteBehavior *dlite_behavior_recordno(size_t n); | ||
|
||
%feature("docstring", "\ | ||
Return a pointer to the given behavior record, or NULL if `name` is | ||
not in the behavior table. | ||
") dlite_behavior_record; | ||
const struct _DLiteBehavior *dlite_behavior_record(const char *name); | ||
|
||
%feature("docstring", "\ | ||
Get value of given behavior. | ||
Returns 1 if the behavior is on, 0 if it is off and a negative | ||
value on error. | ||
") dlite_behavior_get; | ||
int dlite_behavior_get(const char *name); | ||
|
||
%feature("docstring", "\ | ||
Enable given behavior if `value` is non-zero. Disable if `value` is zero. | ||
Returns non-zero on error. | ||
") dlite_behavior_set; | ||
int dlite_behavior_set(const char *name, int value); | ||
|
||
|
||
/* ----------------------------------- | ||
* Target language-spesific extensions | ||
* ----------------------------------- */ | ||
#ifdef SWIGPYTHON | ||
%include "dlite-behavior-python.i" | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -112,3 +112,4 @@ | |
%include "dlite-collection.i" | ||
%include "dlite-path.i" | ||
%include "dlite-mapping.i" | ||
%include "dlite-behavior.i" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.