Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
Hels15 committed Jan 26, 2023
2 parents c252ca8 + 24a726c commit a42a1b7
Show file tree
Hide file tree
Showing 26 changed files with 1,087 additions and 757 deletions.
110 changes: 2 additions & 108 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,119 +49,13 @@ Generate a new project with the following command:
python -m slobypy generate <project_name>
```


Start streaming JIT compiled data to Sloby:

```bash
python -m slobypy run
```

### SCSS

**Simple scss class**

```py
my_class = SCSS_CLASS(
name="base_class1",
color="red",
position="relative"
)

print(my_class.render())
```

```
.base_class1{
color:red;
position:relative;
}
```

**Simple scss group**

```py
my_first_scss_group = SCSS_GROUP("first_scss_group")

my_class = SCSS_CLASS(
name="my_class",
color="red",
position="relative",
)
my_class_2 = SCSS_CLASS(
name="my_class_2",
color="red",
position="relative",
)

my_first_scss_group.add(my_class)
my_first_scss_group.relationship(my_class, child=my_class_2)
```

```
.my_class{
color:red;
position:relative;
.my_class_2{
color:red;
position:relative;
}}
```

--------------

### HTML

**Simple html element**

```py
p = P("paragraph body", P("child1"))
print(p.render())
```

```
<p>paragraph body<p>child1</p>
</p>
```

-----------
**With properties**

```py
p = P(P("child1"), color="red")
print(p.render())
```

```
<p color="red"><p >child1</p>
</p>
```

------------

### COMPONENT

**Simple component with abstract methods.**

```py
class MyFirstComponent(Component):
def name(self):
return "MyComponentName"

def body(self):
yield P("inside the component body")
```

**Component "registration" with the uri"**


```py
app = SlApp()


@app.component(uri="test/first")
class MyFirstComponent(Component):
...
```
[Examples](https://github.com/FlurryGlo/slobypy/tree/main/examples)

[🎉**Contributors**🎉](https://github.com/FlurryGlo/slobypy/graphs/contributors)
28 changes: 2 additions & 26 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,8 @@ build-backend = "poetry.core.masonry.api"
slobypy = "slobypy.manager:start_typer"

[tool.pyright]
include = [
"slobypy",
"tests/*.py",
"*.py"
]
exclude = ["tests/*", "**/site-packages/**/*.py"]
typeCheckingMode = "strict"
pythonVersion = "3.10"

reportUnnecessaryTypeIgnoreComment = true

reportOverlappingOverload = false
reportPrivateUsage = false
reportUnnecessaryIsInstance = false
reportFunctionMemberAccess = false
reportMissingTypeStubs = false
reportUnusedFunction = false
reportUnusedClass = false
reportConstantRedefinition = false
reportImportCycles = false
reportIncompatibleMethodOverride = false
reportIncompatibleVariableOverride = false

# these are largely due to missing type hints, and make up most of the error count
reportUnknownMemberType = false
reportUnknownParameterType = false
reportUnknownArgumentType = false
reportMissingParameterType = false
reportUnknownVariableType = false
reportMissingTypeArgument = false

4 changes: 1 addition & 3 deletions slobypy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"""A modular, object-oriented, and easy to build full-stack web-applications"""

from .app import SlApp
from .react.component import *
from .react.html import *
from .app import *
4 changes: 2 additions & 2 deletions slobypy/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import slobypy.manager
from . import manager

if __name__ == "__main__":
slobypy.manager.app()
manager.app()
14 changes: 8 additions & 6 deletions slobypy/_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def mount(self):
"""

CONFIG = Template("""\
CONFIG = Template(
"""\
{
"name": "My Project",
"version": "1.0.0",
Expand All @@ -66,9 +67,11 @@ def mount(self):
$preprocessor
"processed_js": ""
}
""")
"""
)

PREPROCESSOR = Template("""
PREPROCESSOR = Template(
"""
import asyncio
from pathlib import Path
Expand Down Expand Up @@ -115,6 +118,5 @@ async def process_css() -> Path:
await css_event.wait()
return Path("css/output.css")
""")

"""
)
73 changes: 45 additions & 28 deletions slobypy/app.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
from __future__ import annotations

# Third-Party
import inspect

# Built-in
from typing import TYPE_CHECKING, Union, Any, Callable, Type
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable

# This Project
from .rpc import Event, RPC
from .react.tools import uri_checker
from .react.router import SloRouter
from .errors.pages import Page404
from .react.router import SloRouter
from .react.tools import uri_checker
from .rpc import RPC, Event

if TYPE_CHECKING:
from .react.component import Component

__all__: tuple[str, ...] = (
"SlApp",
)
__all__: tuple[str, ...] = ("SlApp",)


class SlApp:
"""
Expand All @@ -31,14 +26,16 @@ class SlApp:
### Returns
- None
"""

# Use list to prevent name conflicts
_components = []
only_components = [] # registered components(only)
_components: list[dict[str, Any]] = []
only_components: list[type[Component]] = [] # registered components(only)
rpc: RPC | None = None


@classmethod
def component(cls, uri: str | SloRouter, static: bool = False) -> Callable:
def component(
cls, uri: str | SloRouter, static: bool = False
) -> Callable[[type[Component]], type[Component]]:
"""
This decorator is used to register a component to the app.
Expand All @@ -48,13 +45,24 @@ def component(cls, uri: str | SloRouter, static: bool = False) -> Callable:
### Returns
- Callable: The decorator's wrapped callable
"""
def wrap(component):
cls.add(uri, component, inspect.stack()[1].filename, {"uri": uri}) # add the uri

def wrap(component: type[Component]) -> type[Component]:
cls.add(
uri, component, inspect.stack()[1].filename, {"uri": uri}, static
) # add the uri
return component

return wrap

@classmethod
def add(cls, uri: str | SloRouter, component: Type[Component], source, metadata, static: bool = False) -> None:
def add( # pylint: disable=too-many-arguments
cls,
uri: str | SloRouter,
component: type[Component],
source: str,
metadata: dict[str, Any],
static: bool = False,
) -> None:
"""
This method is used to add a component to the app.
### Arguments
Expand All @@ -64,13 +72,22 @@ def add(cls, uri: str | SloRouter, component: Type[Component], source, metadata,
### Returns
- None
"""
if isinstance(uri, SloRouter):
uri = uri.route
# TODO: Add URI checking regex
cls._components.append(
{"uri": uri_checker(uri), "component": component, "source_path": Path(source), "metadata": metadata, "static": static})
{
"uri": uri_checker(uri),
"component": component,
"source_path": Path(source),
"metadata": metadata,
"static": static,
}
)
cls.only_components.append(component)

@classmethod
def dispatch(cls, event: Union[Event, Any]) -> None:
def dispatch(cls, event: Event | Any) -> None:
"""
This method is used to emit an event to the targeted component.
Expand All @@ -81,14 +98,14 @@ def dispatch(cls, event: Union[Event, Any]) -> None:
- None
"""
for component in cls._components:
if component.name() == event.name:
if component["component"].name() == event.name:
try:
getattr(component, "on_" + event.type)(event)
except AttributeError:
pass

@classmethod
def run(cls, *args, **kwargs):
def run(cls, *args: Any, **kwargs: Any) -> None:
"""
This method is used to run the app.
Expand All @@ -100,11 +117,10 @@ def run(cls, *args, **kwargs):
"""
cls.rpc = RPC(cls, *args, **kwargs)

# Todo:
# Todo:
# - Extend the render with more informal component data.
# - Implement overloads on this classmethod since it return types changes on the types of the passed parameters.
@classmethod
def _render(cls, obj = None, route: str = False) -> tuple[Any, Any] | str | Any:
def _render(cls, obj: Component | None = None, route: str | None = None) -> str:
"""
This method is used to render the app to HTML.
Expand All @@ -123,7 +139,8 @@ def _render(cls, obj = None, route: str = False) -> tuple[Any, Any] | str | Any:
for component in cls._components:
if component["uri"] == route:
return component["component"]().render()
else:
return Page404(route=route).show()
return Page404(route=route).show()

return "".join(component["component"]().render() for component in cls._components)
return "".join(
component["component"]().render() for component in cls._components
)
2 changes: 1 addition & 1 deletion slobypy/css/css_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,4 @@
--tw-bg-opacity: 1;
background-color: rgb(21 128 61 / var(--tw-bg-opacity));
}
"""
"""
3 changes: 3 additions & 0 deletions slobypy/errors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .pages import *
from .react_errors import *
from .scss_errors import *
3 changes: 1 addition & 2 deletions slobypy/errors/pages.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

class Page404:
def __init__(self, route="/") -> None:
def __init__(self, route: str = "/") -> None:
self.message = f"{route} is empty"

def show(self) -> str:
Expand Down
1 change: 0 additions & 1 deletion slobypy/errors/react_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

class URIError(Exception):
"""URIError is raised when the uri is not valid"""
pass


class NotRegistered(Exception):
Expand Down
4 changes: 0 additions & 4 deletions slobypy/errors/scss_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@

class PropertyKeyError(Exception):
"""PropertyKeyError is raised when the property key is not valid"""
pass


class NotSame(Exception):
"""NotSame is raised when the two values are not the same"""
pass


class RelationshipError(Exception):
"""RelationshipError is raised when the relationship is not valid"""
pass


class NoName(Exception):
"""NoName is raised when the name is not valid"""
pass
Loading

0 comments on commit a42a1b7

Please sign in to comment.