PyEclipseLaunch/PyEclipseLaunch.py

151 lines
5.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# Python Eclipse Launcher by Alexander Schulz-Rosengarten
# Modified by Nis Wechselberg <enbewe@enbewe.de>
#
# To the extent possible under law, the person who associated CC0 with
# Python Eclipse Launcher has waived all copyright and related or
# neighboring rights to Python Eclipse Launcher.
#
# You should have received a copy of the CC0 legalcode along with this
# work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
"""
Python Eclipse Launcher
Small launcher application to access the different eclipse installations
in a common main directory.
"""
from subprocess import Popen
import sys
import os
import re
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
# The common main directory for the eclipse installations
ECLIPSES_DIR = "/home/enbewe/Software/Eclipse"
###### ## ## ####
## ## ## ## ##
## ## ## ##
## #### ## ## ##
## ## ## ## ##
## ## ## ## ##
###### ####### ####
class LauncherWindow(Gtk.ApplicationWindow):
"""GTK window construction"""
def __init__(self, app):
Gtk.Window.__init__(self, title="Eclipse Launcher", application=app)
# configure window
self.set_default_size(200, 400)
#self.set_resizable(False)
self.set_position(Gtk.WindowPosition.CENTER)
self.set_icon_from_file(get_resource_path(ECLIPSES_DIR+"/icon.png"))
# the scrolled panel
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.set_border_width(15)
scrolled_window.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
# a grid
grid = Gtk.Grid()
# some space between the row of the grid
grid.set_row_spacing(2)
eclipses = []
for entry in [edir for edir in os.listdir(ECLIPSE_DIR_RES) if is_eclipse_dir(edir)]:
eclipses.append(entry)
eclipses.sort(key=natural_sort_key)
if eclipses:
eclipse_label = Gtk.Button()
eclipse_label.set_label(to_label(eclipses[0]))
eclipse_label.set_relief(Gtk.ReliefStyle.NONE)
eclipse_label.connect("clicked", start_eclipse, eclipses[0])
eclipse_label.set_hexpand(True)
grid.attach(eclipse_label, 0, 0, 1, 1)
for eclipse in eclipses[1:]:
next_eclipse_label = Gtk.Button()
next_eclipse_label.set_label(to_label(eclipse))
next_eclipse_label.set_relief(Gtk.ReliefStyle.NONE)
next_eclipse_label.connect("clicked", start_eclipse, eclipse)
next_eclipse_label.set_hexpand(True)
grid.attach_next_to(
next_eclipse_label, eclipse_label, Gtk.PositionType.BOTTOM, 1, 1)
eclipse_label = next_eclipse_label
# add the grid to the scrolled window
scrolled_window.add_with_viewport(grid)
# add the scrolled window to the window
self.add(scrolled_window)
class LauncherApp(Gtk.Application):
"""Main GTK application"""
def __init__(self):
Gtk.Application.__init__(self)
def do_activate(self):
"""Create and activate a window"""
win = LauncherWindow(self)
win.show_all()
def do_startup(self):
"""Application startup handler"""
Gtk.Application.do_startup(self)
## ## ######## #### ## #### ######## ## ##
## ## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ####
## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ##
## ## ## ## ## ## ## ##
####### ## #### ######## #### ## ##
def get_resource_path(rel_path):
"""Resolve a relative or absolute path"""
dir_of_py_file = os.path.dirname(__file__)
rel_path_to_resource = os.path.join(dir_of_py_file, rel_path)
abs_path_to_resource = os.path.abspath(rel_path_to_resource)
return abs_path_to_resource
def start_eclipse(_self, eclipse):
"""Fork eclipse process"""
menv = os.environ.copy()
# menv["SWT_GTK3"] = "0"
Popen([ECLIPSE_DIR_RES+"/"+eclipse+"/eclipse/eclipse"], env=menv, start_new_session=True)
sys.exit(1)
def to_label(name):
"""Extract button label from folder name"""
return " ".join(map(str.capitalize, name.split("-")))
def natural_sort_key(line, _nsre=re.compile('([0-9]+)')):
"""Extract sort keys from given lines"""
return [int(text) if text.isdigit() else text.lower() for text in re.split(_nsre, line)]
def is_eclipse_dir(dirname):
"""Check if the given path points to an eclipse installation"""
path = ECLIPSE_DIR_RES+"/"+dirname
return os.path.isdir(path) and os.path.isfile(path+"/eclipse/eclipse")
## ## ### #### ## ##
### ### ## ## ## ### ##
#### #### ## ## ## #### ##
## ### ## ## ## ## ## ## ##
## ## ######### ## ## ####
## ## ## ## ## ## ###
## ## ## ## #### ## ##
# Create and run the application
ECLIPSE_DIR_RES = get_resource_path(ECLIPSES_DIR)
EXIT_STATE = LauncherApp().run(sys.argv)
# exit with the value returned by running the program
sys.exit(EXIT_STATE)