Skip to content

Commit

Permalink
Merge pull request #161 from Linbreux/missing-drawings-from-git
Browse files Browse the repository at this point in the history
Missing drawings from git
  • Loading branch information
Linbreux authored May 27, 2024
2 parents e8a1b13 + d507980 commit c49445c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/wikmd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
HOMEPAGE_DEFAULT = "homepage.md"
HOMEPAGE_TITLE_DEFAULT = "homepage"
IMAGES_ROUTE_DEFAULT = "img"
DRAWINGS_ROUTE_DEFAULT = ".drawings"

HIDE_FOLDER_IN_WIKI = []

Expand Down Expand Up @@ -89,6 +90,7 @@ def __init__(self):
self.homepage = os.getenv("HOMEPAGE") or yaml_config.get("homepage", HOMEPAGE_DEFAULT)
self.homepage_title = os.getenv("HOMEPAGE_TITLE") or yaml_config.get("homepage_title", HOMEPAGE_TITLE_DEFAULT)
self.images_route = os.getenv("IMAGES_ROUTE") or yaml_config.get("images_route", IMAGES_ROUTE_DEFAULT)
self.drawings_route = DRAWINGS_ROUTE_DEFAULT

self.hide_folder_in_wiki = os.getenv("HIDE_FOLDER_IN_WIKI") or yaml_config.get("hide_folder_in_wiki", HIDE_FOLDER_IN_WIKI)

Expand Down
25 changes: 22 additions & 3 deletions src/wikmd/plugins/draw/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ def __init__(self, flask_app: Flask, config: WikmdConfig, web_dep):
self.config = config
self.this_location = os.path.dirname(__file__)
self.web_dep = web_dep
self.drawings_folder = None

def post_setup(self) -> bool:
"""
configure the drawings folder
returns True if folder did not exist and created it
"""
self.drawings_folder = os.path.join(self.config.wiki_directory, self.config.drawings_route)
if not os.path.exists(self.drawings_folder):
os.mkdir(self.drawings_folder)

# allow backward compatibility and move all the contents in the plugins
# drawing folder to the wiki drawings folder
for item in os.listdir(os.path.join(self.this_location, "drawings")):
print(f"copy {item} to {self.drawings_folder}")
shutil.copy2(os.path.join(self.this_location, "drawings",item), os.path.join(self.drawings_folder, item))
return True

return False

def get_plugin_name(self) -> str:
"""
Expand Down Expand Up @@ -47,7 +66,7 @@ def communicate_plugin(self, request):
self.flask_app.logger.info(f"Plug/{self.name} - changing drawing {id}")

# look for folder
location = os.path.join(os.path.dirname(__file__), "drawings", id)
location = os.path.join(self.drawings_folder, id)
if os.path.exists(location):
file = open(location, "w")
file.write(image)
Expand All @@ -60,7 +79,7 @@ def look_for_existing_drawid(self, drawid: str) -> str:
look for a drawId in the wiki/draw folder and return the file as a string
"""
try:
file = open(os.path.join(self.this_location, "drawings", drawid), "r")
file = open(os.path.join(self.drawings_folder, drawid), "r")
return file.read()
except Exception:
print("Did not find the file")
Expand All @@ -70,7 +89,7 @@ def create_draw_file(self, filename: str) -> None:
"""
Copy the default drawing to a new one with this filename
"""
path_to_file = os.path.join(self.this_location, "drawings", filename)
path_to_file = os.path.join(self.drawings_folder, filename)
shutil.copyfile(os.path.join(self.this_location, "default_draw"), path_to_file)
s = open(path_to_file,"r")
result = re.sub("id=\"\"","id=\"" + filename + "\"",s.read())
Expand Down
8 changes: 7 additions & 1 deletion src/wikmd/wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@

UPLOAD_FOLDER_PATH = pathify(cfg.wiki_directory, cfg.images_route)
GIT_FOLDER_PATH = pathify(cfg.wiki_directory, '.git')
DRAWING_FOLDER_PATH = pathify(cfg.wiki_directory, cfg.drawings_route)
HIDDEN_FOLDER_PATH_LIST = [pathify(cfg.wiki_directory, hidden_folder) for hidden_folder in cfg.hide_folder_in_wiki]
HOMEPAGE_PATH = pathify(cfg.wiki_directory, cfg.homepage)
HIDDEN_PATHS = tuple([UPLOAD_FOLDER_PATH, GIT_FOLDER_PATH, HOMEPAGE_PATH] + HIDDEN_FOLDER_PATH_LIST)
HIDDEN_PATHS = tuple([UPLOAD_FOLDER_PATH, GIT_FOLDER_PATH, DRAWING_FOLDER_PATH, HOMEPAGE_PATH] + HIDDEN_FOLDER_PATH_LIST)

_project_folder = Path(__file__).parent
app = Flask(__name__,
Expand Down Expand Up @@ -540,6 +541,11 @@ def setup_wiki_template() -> bool:
app.logger.info("Wiki directory is empty, copy template")
shutil.copytree(root / "wiki_template", cfg.wiki_directory, dirs_exist_ok=True)
return True

for plugin in plugins:
if "post_setup" in dir(plugin):
plugin.post_setup()

return False


Expand Down
1 change: 1 addition & 0 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def test_search():

def test_add_new_file(wiki_path):
"""App can create files."""
wiki.setup_wiki_template()
rv = app.test_client().get("/add_new")
assert rv.status_code == 200
assert b"content" in rv.data
Expand Down

0 comments on commit c49445c

Please sign in to comment.