-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #877 from OpenC3/python_apis
Additional Python APIs
- Loading branch information
Showing
51 changed files
with
1,944 additions
and
635 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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2023 OpenC3, Inc | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can modify and/or redistribute it | ||
# under the terms of the GNU Affero General Public License | ||
# as published by the Free Software Foundation; version 3 with | ||
# attribution addendums as found in the LICENSE.txt | ||
# | ||
# This program 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. See the | ||
# GNU Affero General Public License for more details. | ||
|
||
# This file may also be used under the terms of a commercial license | ||
# if purchased from OpenC3, Inc.: | ||
|
||
import json | ||
from openc3.api import WHITELIST | ||
from openc3.environment import OPENC3_SCOPE | ||
from openc3.utilities.authorization import authorize | ||
from openc3.models.tool_config_model import ToolConfigModel | ||
|
||
WHITELIST.extend(["list_configs", "load_config", "save_config", "delete_config"]) | ||
|
||
|
||
def list_configs(tool, scope=OPENC3_SCOPE): | ||
authorize(permission="system", scope=scope) | ||
return ToolConfigModel.list_configs(tool, scope=scope) | ||
|
||
|
||
def load_config(tool, name, scope=OPENC3_SCOPE): | ||
authorize(permission="system", scope=scope) | ||
return json.loads(ToolConfigModel.load_config(tool, name, scope=scope)) | ||
|
||
|
||
def save_config(tool, name, data, local_mode=True, scope=OPENC3_SCOPE): | ||
authorize(permission="system_set", scope=scope) | ||
ToolConfigModel.save_config(tool, name, data, local_mode, scope) | ||
|
||
|
||
def delete_config(tool, name, local_mode=True, scope=OPENC3_SCOPE): | ||
authorize(permission="system_set", scope=scope) | ||
ToolConfigModel.delete_config(tool, name, local_mode, scope) |
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 @@ | ||
# Copyright 2023 OpenC3, Inc | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can modify and/or redistribute it | ||
# under the terms of the GNU Affero General Public License | ||
# as published by the Free Software Foundation; version 3 with | ||
# attribution addendums as found in the LICENSE.txt | ||
# | ||
# This program 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. See the | ||
# GNU Affero General Public License for more details. | ||
|
||
# This file may also be used under the terms of a commercial license | ||
# if purchased from OpenC3, Inc.: | ||
|
||
from openc3.api import WHITELIST | ||
from openc3.environment import OPENC3_SCOPE | ||
from openc3.utilities.authorization import authorize | ||
from openc3.models.setting_model import SettingModel | ||
from openc3.utilities.local_mode import LocalMode | ||
|
||
WHITELIST.extend( | ||
["list_settings", "get_all_settings", "get_setting", "get_settings", "save_setting"] | ||
) | ||
|
||
|
||
def list_settings(scope=OPENC3_SCOPE): | ||
authorize(permission="system", scope=scope) | ||
return SettingModel.names(scope=scope) | ||
|
||
|
||
def get_all_settings(scope=OPENC3_SCOPE): | ||
authorize(permission="system", scope=scope) | ||
return SettingModel.all(scope=scope) | ||
|
||
|
||
def get_setting(name, scope=OPENC3_SCOPE): | ||
authorize(permission="system", scope=scope) | ||
setting = SettingModel.get(name=name, scope=scope) | ||
if setting: | ||
return setting["data"] | ||
else: | ||
return None | ||
|
||
|
||
def get_settings(*settings, scope=OPENC3_SCOPE): | ||
authorize(permission="system", scope=scope) | ||
result = [] | ||
for name in settings: | ||
result.append(get_setting(name, scope)) | ||
return result | ||
|
||
|
||
def save_setting(name, data, local_mode=True, scope=OPENC3_SCOPE): | ||
authorize(permission="admin", scope=scope) | ||
SettingModel.set({"name": name, "data": data}, scope=scope) | ||
if local_mode: | ||
LocalMode.save_setting(scope, name, data) |
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright 2023 OpenC3, Inc. | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can modify and/or redistribute it | ||
# under the terms of the GNU Affero General Public License | ||
# as published by the Free Software Foundation; version 3 with | ||
# attribution addendums as found in the LICENSE.txt | ||
# | ||
# This program 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. See the | ||
# GNU Affero General Public License for more details. | ||
|
||
# This file may also be used under the terms of a commercial license | ||
# if purchased from OpenC3, Inc. | ||
|
||
from openc3.models.model import Model | ||
|
||
|
||
class SettingModel(Model): | ||
PRIMARY_KEY = "openc3__settings" | ||
|
||
# NOTE: The following three class methods are used by the ModelController | ||
# and are reimplemented to enable various Model class methods to work | ||
@classmethod | ||
def get(cls, name, scope=None): | ||
return super().get(SettingModel.PRIMARY_KEY, name=name) | ||
|
||
@classmethod | ||
def names(cls, scope=None): | ||
return super().names(SettingModel.PRIMARY_KEY) | ||
|
||
@classmethod | ||
def all(cls, scope=None): | ||
return super().all(SettingModel.PRIMARY_KEY) | ||
|
||
# END NOTE | ||
|
||
def __init__(self, name, data, scope): | ||
super().__init__(SettingModel.PRIMARY_KEY, name=name, scope=scope) | ||
self.data = data | ||
|
||
# @return [Hash] JSON encoding of this model | ||
def as_json(self): | ||
return { | ||
"name": self.name, | ||
"data": self.data, | ||
"updated_at": self.updated_at, | ||
} |
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,42 @@ | ||
# Copyright 2023 OpenC3, Inc. | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can modify and/or redistribute it | ||
# under the terms of the GNU Affero General Public License | ||
# as published by the Free Software Foundation; version 3 with | ||
# attribution addendums as found in the LICENSE.txt | ||
# | ||
# This program 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. See the | ||
# GNU Affero General Public License for more details. | ||
|
||
# This file may also be used under the terms of a commercial license | ||
# if purchased from OpenC3, Inc. | ||
|
||
from openc3.utilities.store import Store | ||
from openc3.utilities.local_mode import LocalMode | ||
from openc3.environment import OPENC3_SCOPE | ||
|
||
|
||
class ToolConfigModel: | ||
@classmethod | ||
def list_configs(cls, tool, scope=OPENC3_SCOPE): | ||
keys = Store.hkeys(f"{scope}__config__{tool}") | ||
return [key.decode() for key in keys] | ||
|
||
@classmethod | ||
def load_config(cls, tool, name, scope=OPENC3_SCOPE): | ||
return Store.hget(f"{scope}__config__{tool}", name).decode() | ||
|
||
@classmethod | ||
def save_config(cls, tool, name, data, local_mode=True, scope=OPENC3_SCOPE): | ||
Store.hset(f"{scope}__config__{tool}", name, data) | ||
if local_mode: | ||
LocalMode.save_tool_config(scope, tool, name, data) | ||
|
||
@classmethod | ||
def delete_config(cls, tool, name, local_mode=True, scope=OPENC3_SCOPE): | ||
Store.hdel(f"{scope}__config__{tool}", name) | ||
if local_mode: | ||
LocalMode.delete_tool_config(scope, tool, name) |
Oops, something went wrong.