Skip to content

Commit

Permalink
refactoring menu system from vnm repo to here
Browse files Browse the repository at this point in the history
  • Loading branch information
stebo85 committed Jul 10, 2020
1 parent 7b47c74 commit 13e5dd2
Show file tree
Hide file tree
Showing 7 changed files with 460 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Forcing UNIX LF line endings for text files added to container
config/*.sh text eol=lf
config/rc.xml text eol=lf
config/.bashrc text eol=lf
config/panel text eol=lf
menus/lxde-applications.menu text eol=lf
menus/vnm-applications.menu text eol=lf
menus/submenus/*.directory text eol=lf
menus/applications/*.desktop text eol=lf
119 changes: 119 additions & 0 deletions menus/apps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"AFNI": {
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"apps": {
"afni 20.1.18": {
"version": "20.1.18",
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"exec": "bash /usr/share/fetch_and_run.sh afni 20.1.18 20200702",
"comment": "Install afni 20.1.18",
"terminal": true
}
}
},
"ANTS": {
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"apps": {
"ants 2.3.1": {
"version": "2.3.1",
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"exec": "bash /usr/share/fetch_and_run.sh ants 2.3.1 20200701",
"comment": "Install ants 2.3.1",
"terminal": true
},
"ants 2.3.4": {
"version": "2.3.4",
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"exec": "bash /usr/share/fetch_and_run.sh ants 2.3.4 20200702",
"comment": "Install ants 2.3.4",
"terminal": true
}
}
},
"Convert3D": {
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"apps": {
"Convert3D 1.0.0": {
"version": "1.0.0",
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"exec": "bash /usr/share/fetch_and_run.sh convert3d 1.0.0 20200702",
"comment": "Install Convert3D 1.0.0",
"terminal": true
}
}
},
"FreeSurfer": {
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"apps": {
"FreeSurfer 7.1.0": {
"version": "7.1.0",
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"exec": "bash /usr/share/fetch_and_run.sh freesurfer 7.1.0 20200708",
"comment": "Install FreeSurfer 7.1.0",
"terminal": true
}
}
},
"FSL": {
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"apps": {
"FSL 6.0.1": {
"version": "6.0.1",
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"exec": "bash /usr/share/fetch_and_run.sh fsl 6.0.1 20200702",
"comment": "Install fsl 6.0.1",
"terminal": true
},
"FSL 6.0.2": {
"version": "6.0.2",
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"exec": "bash /usr/share/fetch_and_run.sh fsl 6.0.2 20200701",
"comment": "Install fsl 6.0.2",
"terminal": true
},
"FSL 6.0.3": {
"version": "6.0.3",
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"exec": "bash /usr/share/fetch_and_run.sh fsl 6.0.3 20200702",
"comment": "Install fsl 6.0.3",
"terminal": true
}
}
},
"ITK snap": {
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"apps": {
"ITK snap 3.8.0": {
"version": "3.8.0",
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"exec": "bash /usr/share/fetch_and_run.sh itksnap 3.8.0 20200702 itksnap /root/MRI-crop/MRIcrop-orig.gipl",
"comment": "Start ITK snap 3.8.0",
"terminal": true
}
}
},
"MINC": {
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"apps": {
"MINC 1.9.17": {
"version": "1.9.17",
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"exec": "bash /usr/share/fetch_and_run.sh minc 1.9.17 20200702",
"comment": "Start minc 1.9.17",
"terminal": true
}
}
},
"MRTrix3": {
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"apps": {
"mrtrix3 3.0.0": {
"version": "3.0.0",
"icon": "/root/.config/lxpanel/LXDE/icons/vnm.png",
"exec": "bash /usr/share/fetch_and_run.sh mrtrix3 3.0.0 20200709",
"comment": "Start mrtrix3 3.0.0",
"terminal": true
}
}
}
}
121 changes: 121 additions & 0 deletions menus/build_menu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"""Generate the menu items."""
import configparser
import json
import os
from pathlib import Path
import re
from typing import Text
import xml.etree.ElementTree as et
from xml.dom import minidom


def add_menu(name: Text, icon: Text) -> None:
"""Add a submenu to 'VNM' menu.
Parameters
----------
name : Text
The name of the submenu.
icon : Text
The path to the submenu icon inside the image (Starting with
``'/root/.config/lxpanel/LXDE/icons/'`` for Xfce).
"""
print(f"Adding submenu for '{name}'")
# Generate `.directory` file
entry = configparser.ConfigParser()
entry.optionxform = str
entry["Desktop Entry"] = {
"Name": name.capitalize(),
"Comment": name.capitalize(),
"Icon": icon,
"Type": "Directory",
}
directories_path = "/usr/share/desktop-directories"
directory_name = f"vnm-{name.lower().replace(' ', '-')}.directory"
with open(Path(f"{directories_path}/{directory_name}"), "w",) as directory_file:
entry.write(directory_file, space_around_delimiters=False)
# Add entry to `.menu` file
menu_path = Path("/etc/xdg/menus/vnm-applications.menu")
with open(menu_path, "r") as xml_file:
s = xml_file.read()
s = re.sub(r"\s+(?=<)", "", s)
root = et.fromstring(s)
menu_el = root.findall("./Menu")[0]
sub_el = et.SubElement(menu_el, "Menu")
name_el = et.SubElement(sub_el, "Name")
name_el.text = name.capitalize()
dir_el = et.SubElement(sub_el, "Directory")
dir_el.text = directory_name
include_el = et.SubElement(sub_el, "Include")
and_el = et.SubElement(include_el, "And")
cat_el = et.SubElement(and_el, "Category")
cat_el.text = name.replace(" ", "-")
xmlstr = minidom.parseString(et.tostring(root)).toprettyxml(indent="\t")
with open(menu_path, "w") as f:
f.write('<!DOCTYPE Menu PUBLIC "-//freedesktop//DTD Menu 1.0//EN"\n ')
f.write('"http://www.freedesktop.org/standards/menu-spec/1.0/menu.dtd">\n\n')
f.write(xmlstr[xmlstr.find("?>") + 3 :])
os.chmod(menu_path, 0o644)


def add_app(
name: Text,
version: Text,
icon: Text,
exec: Text,
comment: Text,
category: Text,
terminal: bool = True,
) -> None:
"""Add an application to the menu.
Parameters
----------
name : Text
The name of the application.
version : Text
The version of the applciation.
icon : Text
The path to the icon of the application (Starting with
``'/root/.config/lxpanel/LXDE/icons/'`` for Xfce).
exec : Text
The command to run when clicking on the application item.
comment : Text
The tooltip to show when hovering the item.
category : Text
The category defining the menu in which the application must be added.
terminal : bool
If set to ``True``, a terminal is opened when launching the application.
"""
entry = configparser.ConfigParser()
entry.optionxform = str
entry["Desktop Entry"] = {
"Name": name.capitalize(),
"GenericName": name.capitalize(),
"Comment": comment,
"Exec": exec,
"Icon": icon,
"Type": "Application",
"Categories": category,
"Terminal": str(terminal).lower(),
}
applications_path = "/usr/share/applications"
desktop_path = Path(
f"{applications_path}/vnm-{name.lower().replace(' ', '-')}.desktop"
)
with open(desktop_path, "w",) as desktop_file:
entry.write(desktop_file, space_around_delimiters=False)
os.chmod(desktop_path, 0o644)


if __name__ == "__main__":
# Read applications file
with open(Path("./apps.json"), "r") as json_file:
menu_entries = json.load(json_file)

for menu_name, menu_data in menu_entries.items():
# Add submenu
add_menu(menu_name, menu_data["icon"])
for app_name, app_data in menu_data.get("apps", {}).items():
# Add application
add_app(app_name, category=menu_name.replace(" ", "-"), **app_data)
Binary file added menus/icons/vnm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 13e5dd2

Please sign in to comment.