Added proper folder generation and manifest setup

This commit is contained in:
itdominator 2023-04-01 22:46:18 -05:00
parent fb3564a3aa
commit bc7577a725
8 changed files with 169 additions and 45 deletions

View File

@ -40,7 +40,7 @@ class Controller(SignalsMixins, ControllerData):
event_system.emit_and_await("load_notebook") event_system.emit_and_await("load_notebook")
if settings.get_active_notebook(): if settings.get_active_notebook():
event_system.emit("load_notebook_data") self._load_notebook_data()
logger.info(f"Made it past {self.__class__} loading...") logger.info(f"Made it past {self.__class__} loading...")
@ -55,7 +55,7 @@ class Controller(SignalsMixins, ControllerData):
def _subscribe_to_events(self): def _subscribe_to_events(self):
event_system.subscribe("handle_file_from_ipc", self.handle_file_from_ipc) event_system.subscribe("handle_file_from_ipc", self.handle_file_from_ipc)
event_system.subscribe("tggl_top_main_menubar", self._tggl_top_main_menubar) event_system.subscribe("load_notebook_data", self._load_notebook_data)
def _load_widgets(self): def _load_widgets(self):
CreateNotebookWidget() CreateNotebookWidget()
@ -73,5 +73,16 @@ class Controller(SignalsMixins, ControllerData):
def get_core_widget(self): def get_core_widget(self):
return self.core_widget return self.core_widget
def _tggl_top_main_menubar(self): def _load_notebook_data(self):
print("_tggl_top_main_menubar > stub...") path = settings.get_active_notebook()
sections = os.listdir(path)
logger.info(f"Loading Notebook sections from: {path}")
if len(sections) == 0:
settings.generate_section_manifest()
sections = os.listdir(path)
for section in sections:
_section = os.path.join(path, section)
manifest = os.path.join(_section, "MANIFEST")
event_system.emit("create_section_view", (None, None, manifest))

View File

@ -11,12 +11,17 @@ from .pages_tab_widget import PagesTabWidget
class Page(Gtk.ScrolledWindow): class Page(Gtk.ScrolledWindow):
def __init__(self, close_tab): def __init__(self, close_tab, section_pth, manifest_pth):
super(Page, self).__init__() super(Page, self).__init__()
self._close_tab = close_tab self._close_tab = close_tab
self._tab_widget = PagesTabWidget(self, self._close_tab) self._tab_widget = PagesTabWidget(self, self._close_tab)
self._manifest_pth = settings.generate_page_manifest(section_pth) if not manifest_pth else manifest_pth
self._name = None
self._date = None
self._setup_styling() self._setup_styling()
self._setup_signals() self._setup_signals()
self._subscribe_to_events() self._subscribe_to_events()
@ -45,3 +50,16 @@ class Page(Gtk.ScrolledWindow):
def get_tab_widget(self): def get_tab_widget(self):
return self._tab_widget return self._tab_widget
def _load_page_manifest(self, manifest_pth = None):
if manifest_pth:
self._manifest_pth = manifest_pth
with open(self._manifest_pth, "r") as f:
lines = f.readlines()
for line in lines:
parts = line.split("=")
if parts[0] == "name":
self._name = parts[1].strip()
if parts[0] == "date":
self._date = parts[1].strip()

View File

@ -1,4 +1,5 @@
# Python imports # Python imports
import os
# Lib imports # Lib imports
import gi import gi
@ -11,16 +12,21 @@ from ..page_widget import Page
class PagesException(Exception):
...
class Pages(Gtk.Notebook): class Pages(Gtk.Notebook):
def __init__(self, close_tab): def __init__(self, close_tab, label = None, section_manifest_pth = None):
super(Pages, self).__init__() super(Pages, self).__init__()
self._close_tab = close_tab self._tab_widget = SectionsTabWidget(self, close_tab, label)
self._tab_widget = SectionsTabWidget(self, self._close_tab) self._section_pth = os.path.dirname(section_manifest_pth)
self._setup_styling() self._setup_styling()
self._setup_signals() self._setup_signals()
self._load_widgets() self._load_widgets()
self._load_pages_data()
self.show_all() self.show_all()
@ -56,22 +62,41 @@ class Pages(Gtk.Notebook):
self.set_action_widget(end_box, 1) self.set_action_widget(end_box, 1)
def _close_tab(self, button, page, eve = None): def _close_tab(self, button, page, eve = None):
if self.get_n_pages() == 1:
return
page_num = self.page_num(page) page_num = self.page_num(page)
self.remove_page(page_num) self.remove_page(page_num)
def create_page_view(self, widget = None, eve = None): def create_page_view(self, widget = None, eve = None, manifest_pth = None):
page = Page(self._close_tab) page = Page(self._close_tab, self._section_pth, manifest_pth)
page_num = self.append_page(page, page.get_tab_widget()) page_num = self.append_page(page, page.get_tab_widget())
self.set_tab_detachable(page, False) self.set_tab_detachable(page, False)
self.set_tab_reorderable(page, True) self.set_tab_reorderable(page, True)
self.show_all() page.show_all()
self.set_current_page(page_num) self.set_current_page(page_num)
def get_tab_widget(self): def get_tab_widget(self):
return self._tab_widget return self._tab_widget
def _text_search(self, widget = None, eve = None): def _text_search(self, widget = None, eve = None):
... ...
def _load_pages_data(self):
if not self._section_pth:
raise PagesException("No section path provided...")
path = self._section_pth
pages = settings.clean_pages_list( os.listdir(path) )
logger.info(f"Loading Page parts from: {path}")
if len(pages) == 0:
settings.generate_page_manifest(path)
pages = settings.clean_pages_list( os.listdir(path) )
for page in pages:
_page = os.path.join(path, page)
manifest = os.path.join(_page, "MANIFEST")
self.create_page_view(None, None, manifest)

View File

@ -13,12 +13,5 @@ from .template.tab_header_template import TabHeaderTemplate
class PagesTabWidget(TabHeaderTemplate): class PagesTabWidget(TabHeaderTemplate):
""" docstring for PagesTabWidget """ """ docstring for PagesTabWidget """
ccount = 0 def __init__(self, container, close_tab, label = None):
def __new__(cls, *args, **kwargs): super(PagesTabWidget, self).__init__(container, close_tab, label)
obj = super(PagesTabWidget, cls).__new__(cls)
cls.ccount += 1
return obj
def __init__(self, container, close_tab):
super(PagesTabWidget, self).__init__(container, close_tab)

View File

@ -15,6 +15,10 @@ class Sections(Gtk.Notebook):
def __init__(self): def __init__(self):
super(Sections, self).__init__() super(Sections, self).__init__()
self._manifest_pth = None
self._name = None
self._date = None
self._setup_styling() self._setup_styling()
self._setup_signals() self._setup_signals()
self._subscribe_to_events() self._subscribe_to_events()
@ -27,10 +31,10 @@ class Sections(Gtk.Notebook):
self.set_scrollable(True) self.set_scrollable(True)
def _setup_signals(self): def _setup_signals(self):
... event_system.subscribe("create_section_view", self.create_section_view)
def _subscribe_to_events(self): def _subscribe_to_events(self):
event_system.subscribe("load_notebook_data", self._load_notebook_data) ...
def _load_widgets(self): def _load_widgets(self):
start_box = Gtk.Box() start_box = Gtk.Box()
@ -39,7 +43,7 @@ class Sections(Gtk.Notebook):
add_btn = Gtk.Button() add_btn = Gtk.Button()
add_btn.set_image( Gtk.Image.new_from_icon_name("add", 4) ) add_btn.set_image( Gtk.Image.new_from_icon_name("add", 4) )
add_btn.set_always_show_image(True) add_btn.set_always_show_image(True)
add_btn.connect("released", self.create_pages_view) add_btn.connect("released", self.create_section_view)
end_box.add(add_btn) end_box.add(add_btn)
@ -51,23 +55,37 @@ class Sections(Gtk.Notebook):
self.set_action_widget(end_box, 1) self.set_action_widget(end_box, 1)
def _close_tab(self, button, pages, eve = None): def _close_tab(self, button, pages, eve = None):
if self.get_n_pages() == 1:
return
page_num = self.page_num(pages) page_num = self.page_num(pages)
self.remove_page(page_num) self.remove_page(page_num)
def create_pages_view(self, widget = None, eve = None): def create_section_view(self, widget = None, eve = None, manifest_pth = None):
pages = Pages(self._close_tab) self._manifest_pth = manifest_pth
page_num = self.append_page(pages, pages.get_tab_widget()) if not manifest_pth:
self._manifest_pth = settings.generate_section_manifest()
self.set_tab_detachable(pages, True) self._load_section_manifest()
self.set_tab_reorderable(pages, True) pages_view = Pages(self._close_tab, self._name, self._manifest_pth)
page_num = self.append_page(pages_view, pages_view.get_tab_widget())
self.show_all() self.set_tab_detachable(pages_view, True)
self.set_tab_reorderable(pages_view, True)
pages_view.show_all()
self.set_current_page(page_num) self.set_current_page(page_num)
def _load_notebook_data(self):
path = settings.get_active_notebook() def _load_section_manifest(self, manifest_pth = None):
sections = os.listdir(path) if manifest_pth:
logger.info(f"Loading Notebook sections from: {path}") self._manifest_pth = manifest_pth
if len(sections) == 0:
section = os.path.join(path, "New Section") with open(self._manifest_pth, "r") as f:
os.mkdir(section) lines = f.readlines()
for line in lines:
parts = line.split("=")
if parts[0] == "name":
self._name = parts[1].strip()
if parts[0] == "date":
self._date = parts[1].strip()

View File

@ -13,5 +13,5 @@ from .template.tab_header_template import TabHeaderTemplate
class SectionsTabWidget(TabHeaderTemplate): class SectionsTabWidget(TabHeaderTemplate):
""" docstring for SectionsTabWidget """ """ docstring for SectionsTabWidget """
def __init__(self, container, close_tab): def __init__(self, container, close_tab, label = None):
super(SectionsTabWidget, self).__init__(container, close_tab) super(SectionsTabWidget, self).__init__(container, close_tab, label)

View File

@ -19,11 +19,13 @@ class TabHeaderTemplate(Gtk.Box):
return obj return obj
def __init__(self, container, close_tab): def __init__(self, container, close_tab, label = None):
super(TabHeaderTemplate, self).__init__() super(TabHeaderTemplate, self).__init__()
self.INDEX = self.ccount self.INDEX = self.ccount
logger.info(label)
self.NAME = f"tab_{self.INDEX}" self.NAME = f"tab_{self.INDEX}"
self.LABEL = self.NAME if not label else label
self._container = container self._container = container
self._close_tab = close_tab # NOTE: Close method in tab_mixin self._close_tab = close_tab # NOTE: Close method in tab_mixin
@ -32,7 +34,6 @@ class TabHeaderTemplate(Gtk.Box):
self._subscribe_to_events() self._subscribe_to_events()
self._load_widgets() self._load_widgets()
self.set_tab_label()
self.show_all() self.show_all()
@ -53,9 +54,10 @@ class TabHeaderTemplate(Gtk.Box):
# NOTE: Setup with settings and from file # NOTE: Setup with settings and from file
label.set_xalign(0.0) label.set_xalign(0.0)
label.set_margin_left(25) label.set_margin_left(10)
label.set_margin_right(25) label.set_margin_right(10)
label.set_hexpand(True) label.set_hexpand(True)
label.set_label(self.LABEL)
close.set_always_show_image(True) close.set_always_show_image(True)
close.set_hexpand(False) close.set_hexpand(False)

View File

@ -1,7 +1,9 @@
# Python imports # Python imports
import os import datetime
import json
import inspect import inspect
import json
import os
import uuid
# Lib imports # Lib imports
@ -99,6 +101,61 @@ class Settings(StartCheckMixin):
self._builder.connect_signals(handlers) self._builder.connect_signals(handlers)
def generate_section_manifest(self):
path = self.get_active_notebook()
sections = os.listdir(path)
i = 0
if len(sections) == 0:
return self._generate_manifest(i, path)
sections = os.listdir(path)
for section in sections:
index = section.split("_")[0]
try:
i = int(index)
except Exception:
...
return self._generate_manifest(i, path)
def generate_page_manifest(self, path):
pages = self.clean_pages_list( os.listdir(path) )
i = 0
if len(pages) == 0:
return self._generate_manifest(i, path, "New Page")
pages = self.clean_pages_list( os.listdir(path) )
for page in pages:
index = page.split("_")[0]
try:
i = int(index)
except Exception:
...
return self._generate_manifest(i, path, "New Page")
def _generate_manifest(self, i, path, name = "New Section"):
i += 1
section = os.path.join(path, f"{i}_{uuid.uuid4().hex}")
manifest = os.path.join(section, "MANIFEST")
os.mkdir(section)
with open(manifest, "w") as f:
f.write(f"name={name}\ndate={datetime.datetime.now()}")
f.close()
return manifest
def clean_pages_list(self, pages):
for i, page in enumerate(pages):
if page == "MANIFEST":
del pages[i]
break
return pages
def set_main_window(self, window): self._main_window = window def set_main_window(self, window): self._main_window = window
def set_builder(self, builder) -> any: self._builder = builder def set_builder(self, builder) -> any: self._builder = builder
def set_active_notebook(self, path: str): self._active_notebook = path def set_active_notebook(self, path: str): self._active_notebook = path