Skip to content

Commit

Permalink
Add remove view feature
Browse files Browse the repository at this point in the history
  • Loading branch information
kengoon committed Oct 4, 2023
1 parent f8efcdf commit 02fc815
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 10 deletions.
9 changes: 4 additions & 5 deletions mvc4kivy/add_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

from . import ArgumentParserWithHelp
from .create_project import (
chek_camel_case_name_project,
check_camel_case_name_project,
create_common_responsive_module,
create_controller,
create_model,
Expand Down Expand Up @@ -102,14 +102,13 @@ def main():
if "database.py" in os.listdir(os.path.join(path_to_project, "Model"))
else "no"
)
module_name = chek_camel_case_name_project(name_view)
module_name = check_camel_case_name_project(name_view)
if not module_name:
parser.error(
"The name of the screen should be written in camel case style. "
"\nFor example - 'MyFirstScreen'"
)
module_name = "_".join([name.lower() for name in module_name])
path_to_project = path_to_project
create_model(name_view, module_name, name_database, path_to_project)

# Create controller.
Expand All @@ -132,13 +131,13 @@ def main():
create_view(name_view, module_name, [name_view], path_to_project)
create_common_responsive_module([name_view], path_to_project)
# Create 'View.screens.py module'.
create_screens_data(name_view, module_name, path_to_project)
update_screens_data(name_view, module_name, path_to_project)
Logger.info(
f"KivyMD: The {name_view} view has been added to the project..."
)


def create_screens_data(
def update_screens_data(
name_view: str, module_name: str, path_to_project: str
) -> None:
with open(
Expand Down
6 changes: 3 additions & 3 deletions mvc4kivy/create_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ def main():
create_main()

for name in name_screen:
module_name = chek_camel_case_name_project(name)
module_name = check_camel_case_name_project(name)
if not module_name:
parser.error(
"The name of the screen should be written in camel case style. "
Expand Down Expand Up @@ -1009,7 +1009,7 @@ def create_view(
name_screen: str,
module_name: str,
use_responsive: list,
path_to_project: str,
path_to_project: str
) -> None:
path_to_view = os.path.join(path_to_project, "View", name_screen)
path_to_components = os.path.join(path_to_view, "components")
Expand Down Expand Up @@ -1168,7 +1168,7 @@ def check_databases() -> None:
)


def chek_camel_case_name_project(name_project) -> Union[bool, list]:
def check_camel_case_name_project(name_project) -> Union[bool, list]:
result = re.findall(r"[A-Z][^A-Z]*", name_project)
return False if len(result) == 1 else result

Expand Down
135 changes: 135 additions & 0 deletions mvc4kivy/remove_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import os
import posixpath
import re

from mvc4kivy import ArgumentParserWithHelp
from mvc4kivy.create_project import check_camel_case_name_project
from shutil import rmtree


screens_data = """%s
screens = {%s
}"""

screens_comment = """# The screen's dictionary contains the objects of the models and controllers
# of the screens of the application.
"""


def main():
"""The function for removing view(s) to the project."""

parser = create_argument_parser()
args = parser.parse_args()

# pattern_name isn't used currently, will be used if new patterns is added in future
pattern_name = args.pattern # noqa F841
path_to_project = args.directory
name_view = args.name
if not os.path.exists(path_to_project):
parser.error(f"Project <{path_to_project}> does not exist...")

if name_view[-6:] != "Screen":
parser.error(
f"The name of the <{name_view}> screen should contain the word "
f"'Screen' at the end.\n"
"For example - '--name_screen MyFirstScreen ...'"
)

if name_view in os.listdir(os.path.join(path_to_project, "View")):
parser.error(
f"The <{name_view}> view also exists in the <{path_to_project}> project..."
)

module_name = check_camel_case_name_project(name_view)
if not module_name:
parser.error(
"The name of the screen should be written in camel case style. "
"\nFor example - 'MyFirstScreen'"
)
module_name = "_".join([name.lower() for name in module_name])
remove_view(name_view, module_name, path_to_project)
update_screens_data(name_view, module_name, path_to_project)


def remove_view(
name_screen: str,
module_name: str,
path_to_project: str
) -> None:
path_to_view = os.path.join(path_to_project, "View", name_screen)
path_to_controller = os.path.join(path_to_view, "Controller", "components")
path_to_model = os.path.join(path_to_view, "Model", module_name)
rmtree(path_to_view)
os.remove(path_to_controller)
os.remove(path_to_model)


def update_screens_data(
name_view: str, module_name: str, path_to_project: str
) -> None:
with open(
os.path.join(path_to_project, "View", "screens.py")
) as screen_module:
screen_module = screen_module.read()
imports = re.findall(
"from Model.*Model|from Controller.*Controller|from View.*View", screen_module
)
screens = ""
path_to_view = os.path.join(path_to_project, "View")

for name in os.listdir(path_to_view):
if os.path.isdir(os.path.join(path_to_view, name)):
res = re.findall(r"[A-Z][^A-Z]*", name)
# if res and len(res) == 2 and res[-1] == "Screen":
if res and len(res) > 1 and res[-1] == "Screen":
snake_case = "_"
screens += (
"\n '%s': {"
"\n 'model': %s,"
"\n 'controller': %s,"
"\n 'view': %s,"
"\n 'kv': %s"
"\n },\n"
% (
f"{' '.join(res).lower()}",
f'{name}Model',
f'{name}Controller',
f'{name}View',
f'"{posixpath.join("./View", name, f"{snake_case.join(res).lower()}.kv")}"',
)
)

imports.remove(f"from Model.{module_name} import {name_view}Model")
imports.remove(
f"from Controller.{module_name} import {name_view}Controller"
)
imports.remove(f"from View.{name_view}.{module_name} import {name_view}View")
imports.insert(0, screens_comment)
screens = screens_data % ("\n".join(imports), screens)

with open(
os.path.join(path_to_project, "View", "screens.py"), "w"
) as screen_module:
screen_module.write(screens)


def create_argument_parser() -> ArgumentParserWithHelp:
parser = ArgumentParserWithHelp(
prog="remove_view.py",
allow_abbrev=False,
)
parser.add_argument(
"pattern",
help="the name of the pattern with which the view will be removed.",
)
parser.add_argument(
"directory",
help="the directory of the project to which you want to remove a view.",
)
parser.add_argument(
"name",
help="the name of the view to add to remove from an existing project.",
)
return parser
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mvc4kivy"
version = "0.2.7"
version = "0.3.0"
description = "tool to create MVC project for kivy"
authors = ["Kenechukwu Akubue <kengoon19@gmail.com>"]
readme = "README.md"
Expand All @@ -10,7 +10,7 @@ name = "kivy"
url = "https://kivy.org/downloads/simple"

[tool.poetry.dependencies]
python = "^3.7"
python = "^3.8"
kivy = "^2.0.0"

[build-system]
Expand All @@ -20,3 +20,4 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry.scripts]
m4k-createproject = "mvc4kivy.create_project:main"
m4k-addview = "mvc4kivy.add_view:main"
m4k-rmview = "mvc4kivy.add_view:main"

0 comments on commit 02fc815

Please sign in to comment.