Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: project re-org to yafti.core model #124

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions yafti/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
"""
Copyright 2023 Marco Ceppi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Copyright 2023 Marco Ceppi
# SPDX-License-Identifier: Apache-2.0
21 changes: 4 additions & 17 deletions yafti/__main__.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
"""
Copyright 2023 Marco Ceppi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Copyright 2023 Marco Ceppi
# SPDX-License-Identifier: Apache-2.0

import logging
from typing import Annotated
Expand All @@ -21,9 +8,9 @@
import yaml

import yafti.setup # noqa
from yafti import log
from yafti.core import log
from yafti.app import Yafti
from yafti.parser import Config
from yafti.core.config import Config


def run(
Expand Down
2 changes: 1 addition & 1 deletion yafti/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from gi.repository import Adw
from pathlib import Path

from yafti.parser import Config, YaftiRunModes, YaftSaveState
from yafti.core.config import Config, YaftiRunModes, YaftSaveState
from yafti.screen.window import Window


Expand Down
2 changes: 2 additions & 0 deletions yafti/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright 2023 Marco Ceppi
# SPDX-License-Identifier: Apache-2.0
2 changes: 2 additions & 0 deletions yafti/cli/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright 2023 Marco Ceppi
# SPDX-License-Identifier: Apache-2.0
2 changes: 2 additions & 0 deletions yafti/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright 2023 Marco Ceppi
# SPDX-License-Identifier: Apache-2.0
19 changes: 3 additions & 16 deletions yafti/abc.py → yafti/core/abc.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
"""
Copyright 2023 Marco Ceppi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# Copyright 2023 Marco Ceppi
# SPDX-License-Identifier: Apache-2.0

import asyncio
from inspect import iscoroutinefunction
Expand All @@ -26,7 +13,7 @@ class YaftiPlugin:


async def _show_screen(condition):
from yafti.registry import PLUGINS
from yafti.core.registry import PLUGINS

plugin_name = list(condition.keys())[0]
plugin = PLUGINS.get(plugin_name)
Expand Down
75 changes: 75 additions & 0 deletions yafti/core/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright 2023 Marco Ceppi
# SPDX-License-Identifier: Apache-2.0

from enum import Enum
from pathlib import Path
from typing import Optional, Any

import yaml
from pydantic import BaseModel, BaseSettings
from pydantic.env_settings import SettingsSourceCallable


class ActionConfig(BaseModel):
pre: Optional[list[dict[str, str | dict]]]
post: Optional[list[dict[str, str | dict]]]


class ScreenConfig(BaseModel):
source: str
values: Optional[dict]


class YaftiRunModes(str, Enum):
changed = "run-on-change"
ignore = "run-once"
disable = "disabled"


class YaftSaveState(str, Enum):
always = "always"
end = "last-screen"


class YaftiProperties(BaseModel):
path: Optional[Path] = Path("~/.config/yafti/last-run")
mode: YaftiRunModes = YaftiRunModes.changed
save_state: YaftSaveState = YaftSaveState.always


class Config(BaseSettings):
title: str
properties: YaftiProperties = YaftiProperties()
actions: Optional[ActionConfig]
screens: Optional[dict[str, ScreenConfig]] # Screens are parsed per plugin

class Config:
env_prefix = "yafti_"
env_nested_delimiter = "__"
env_file = "/etc/yafti.yml"

@classmethod
def customise_sources(
cls,
init_settings: SettingsSourceCallable,
env_settings: SettingsSourceCallable,
file_secret_settings: SettingsSourceCallable,
):
return (
init_settings,
yaml_config_settings_source,
env_settings,
file_secret_settings,
)


def yaml_config_settings_source(settings: BaseSettings) -> dict[str, Any]:
"""
A simple settings source that loads variables from a JSON file
at the project's root.

Here we happen to choose to use the `env_file_encoding` from Config
when reading `config.json`
"""
encoding = settings.__config__.env_file_encoding
return yaml.safe_load(Path(settings.__config__.env_file).read_text(encoding))
9 changes: 8 additions & 1 deletion yafti/log.py → yafti/core/log.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
# Copyright 2023 Marco Ceppi
# SPDX-License-Identifier: Apache-2.0

import logging
import inspect

__all__ = ["info", "warn", "error", "debug", "set_level"]

_l = logging.getLogger("yafti")


def _fmt(msg: dict) -> str:
return " ".join([f"{k}={v}" for k, v in msg.items()])
frame = inspect.stack()[-1]
mod = inspect.getmodule(frame[0])
args = {"module": mod.__name__} | msg
return " ".join([f"{k}={v}" for k, v in args.items()])


def set_level(level):
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions yafti/gtk/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Copyright 2023 Marco Ceppi
# SPDX-License-Identifier: Apache-2.0
63 changes: 0 additions & 63 deletions yafti/parser.py

This file was deleted.

20 changes: 3 additions & 17 deletions yafti/plugin/flatpak.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
# Copyright 2023 Marco Ceppi
# SPDX-License-Identifier: Apache-2.0
"""
Copyright 2023 Marco Ceppi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

\f

Install, remove, list, and manage flatpaks

Configuration usage example:
Expand Down Expand Up @@ -64,7 +50,7 @@

from pydantic import BaseModel, ValidationError, root_validator

from yafti.abc import YaftiPluginReturn
from yafti.core.abc import YaftiPluginReturn
from yafti.plugin.run import Run


Expand Down
37 changes: 7 additions & 30 deletions yafti/plugin/run.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,6 @@
# Copyright 2023 Marco Ceppi
# SPDX-License-Identifier: Apache-2.0
"""
Copyright 2023 Marco Ceppi

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

/f

Run a command on the system

Configuration usage example:
Expand All @@ -38,7 +24,6 @@
from yafti.plugin.run import Run
r = Run()
r.exec(["/usr/bin/whoami"])
f.exec(pkg="com.github.marcoceppi.PackageName", reinstall=True)

r("/usr/bin/whoami")
r(cmd="/usr/bin/whoami")
Expand All @@ -54,11 +39,13 @@

from pydantic import validate_arguments

from yafti import log
from yafti.abc import YaftiPlugin, YaftiPluginReturn
from yafti.core import log
from yafti.core.abc import YaftiPlugin, YaftiPluginReturn


class Run(YaftiPlugin):
"""Run Plugin"""

async def exec(self, cmd: str) -> subprocess.CompletedProcess:
log.debug("running command", cmd=cmd)

Expand All @@ -70,6 +57,7 @@ async def exec(self, cmd: str) -> subprocess.CompletedProcess:
elif which("flatpak-spawn"):
cmd = f"flatpak-spawn --host {cmd}"

log.debug("running command", cmd=cmd, is_container=is_container)
proc = await asyncio.create_subprocess_shell(
cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
Expand All @@ -89,17 +77,6 @@ async def exec(self, cmd: str) -> subprocess.CompletedProcess:
cmd, returncode=proc.returncode, stdout=stdout, stderr=stderr
)

async def install(self, package: str) -> YaftiPluginReturn:
"""Execute a command on the host system

Args:
package: The command to execute

Returns:
An object containing the stdout and stderr from the command
"""
return await self.exec(package)

@validate_arguments
async def __call__(self, cmd: list[str] | str) -> YaftiPluginReturn:
log.debug("run called", cmd=cmd)
Expand Down
4 changes: 2 additions & 2 deletions yafti/screen/consent.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

import yafti.share
from yafti import events
from yafti.abc import YaftiScreen, YaftiScreenConfig
from yafti.registry import PLUGINS
from yafti.core.abc import YaftiScreen, YaftiScreenConfig
from yafti.core.registry import PLUGINS

_xml = """\
<?xml version="1.0" encoding="UTF-8"?>
Expand Down
2 changes: 1 addition & 1 deletion yafti/screen/console.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from gi.repository import Gtk

from yafti.abc import YaftiScreen
from yafti.core.abc import YaftiScreen

_xml = """\
<?xml version="1.0" encoding="UTF-8"?>
Expand Down
Loading
Loading