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

several updates #11

Merged
merged 6 commits into from
Aug 25, 2024
Merged
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
65 changes: 64 additions & 1 deletion kahi/KahiBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def empty_work(self):
return {
"titles": [],
"updated": [],
"subtitle": "",
"abstract": "",
"keywords": [],
"types": [],
Expand All @@ -107,6 +106,70 @@ def empty_work(self):
"groups": []
}

def empty_event(self):
return {
"titles": [],
"updated": [],
"abstract": "",
"types": [],
"external_ids": [],
"external_urls": [],
"date_held": None,
"year_held": None,
"author_count": None,
"authors": [],
"ranking": [],
"groups": []
}

def empty_project(self):
return {
"titles": [],
"updated": [],
"abstract": "",
"types": [],
"external_ids": [],
"external_urls": [],
"date_init": None,
"date_end": None,
"year_init": None,
"year_end": None,
"author_count": None,
"authors": [],
"ranking": [],
"groups": []
}

def empty_patent(self):
return {
"titles": [],
"updated": [],
"types": [],
"external_ids": [],
"external_urls": [],
"author_count": None,
"authors": [],
"ranking": [],
"groups": []
}

def empty_work_others(self):
return {
"titles": [],
"updated": [],
"abstract": "",
"keywords": [],
"types": [],
"external_ids": [],
"external_urls": [],
"date_published": None,
"year_published": None,
"author_count": None,
"authors": [],
"ranking": [],
"groups": []
}

def run(self):
"""
entry point for the execution of the plugin, this method must be implemented
Expand Down
89 changes: 89 additions & 0 deletions kahi/PluginGenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
from kahi import templates
from shutil import copytree, move, rmtree
import pathlib
import os


class PluginGenerator:
"""
Class to generate a kahi plugin from the template.
The generated plugin is a python package ready to install with pip and it can be deployed in pipy.
"""

def __init__(self, name: str, prefix: str = "Kahi"):
"""
Initializes the class with a given name and prefix. The default prefix is "Kahi".

Parameters:
____________
name:str
plugin name for the new project
prefix:str
prefix for the generated project, default is Kahi
"""
self.name = name
self.tname = "template"
self.prefix = prefix
self.template_path = os.path.join(
str(pathlib.Path(templates.__file__).parent.absolute()), "plugin")
self.template_path = os.path.join(
self.template_path, "Kahi_template")

def replace(self, filename, old, new):
"""
Replaces all occurrences of old with new in a given file.

Parameters:
____________
filename:str
filename to replace the string
old:str
string to be replaced
new:str
new string to write
"""
content = ""
with open(filename, "r") as file:
content = file.read()
content = content.replace(old, new)
file.close()
with open(filename, "w") as file:
file.write(str(content))
file.close()

def generate(self, path: str = os.getcwd()):
"""
Generates the plugin in a given path. Copies the template to the output path,
renames the package folder, and replaces the template name with the plugin name in the README.md,
setup.py, and MANIFEST.in files.

Parameters:
____________
path:str
path to save the plugin project.
"""
output_path = os.path.join(os.getcwd(), f"{self.prefix}_{self.name}")
readme = os.path.join(output_path, "README.md")
setup = os.path.join(output_path, "setup.py")
manifest = os.path.join(output_path, "MANIFEST.in")
package_folder_old = os.path.join(output_path, "kahi_template")
package_folder_new = os.path.join(
output_path, f"{self.prefix}_{self.name}".lower())
package_file_old = os.path.join(
package_folder_new, f"{self.prefix}_{self.tname}.py")
package_file_new = os.path.join(
package_folder_new, f"{self.prefix}_{self.name}.py")

copytree(self.template_path, output_path)
move(package_folder_old, package_folder_new)

rmtree(os.path.join(output_path, "__pycache__"), ignore_errors=True)
rmtree(os.path.join(package_folder_new,
"__pycache__"), ignore_errors=True)

os.rename(package_file_old, package_file_new)

self.replace(package_file_new, self.tname, self.name)
self.replace(readme, self.tname, self.name)
self.replace(setup, self.tname, self.name)
self.replace(manifest, self.tname, self.name)
2 changes: 1 addition & 1 deletion kahi/_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# flake8: noqa
__version__ = '0.0.10-alpha'
__version__ = '0.1.0-alpha'


def get_version():
Expand Down
Loading