Add a preferences.json file, change saving file & simplify handling of paths
parent
4247e0a9ff
commit
d8fe89a58e
|
@ -1,6 +1,15 @@
|
||||||
# Change Log
|
# Change Log
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## [0.5-dev]
|
||||||
|
### Added
|
||||||
|
- Locales: ast_ES, be, sv.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- Simplify subtitle.
|
||||||
|
- Simplify customization.
|
||||||
|
- Better handling of dev version.
|
||||||
|
|
||||||
## [0.4.20] - 2017-05-09
|
## [0.4.20] - 2017-05-09
|
||||||
### Fixed
|
### Fixed
|
||||||
- Fix loading of files from data folder.
|
- Fix loading of files from data folder.
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
{
|
||||||
|
"system": "Manjaro Gellivara 17",
|
||||||
|
"default_locale": "en",
|
||||||
|
"autostart_path": "~/autostart/manjaro-hello.desktop",
|
||||||
|
"data_path": "/usr/share/manjaro-hello/data/",
|
||||||
|
"desktop_path": "/usr/share/applications/manjaro-hello.desktop",
|
||||||
|
"installer_path": "/usr/bin/calamares",
|
||||||
|
"live_path": "/run/miso/bootmnt/manjaro",
|
||||||
|
"locale_path": "/usr/share/locale/",
|
||||||
|
"logo_path": "/usr/share/icons/hicolor/64x64/apps/manjaro.png",
|
||||||
|
"save_path": "~/.config/manjaro/manjaro-hello.json",
|
||||||
|
"ui_path": "/usr/share/manjaro-hello/ui/manjaro-hello.glade",
|
||||||
|
"urls": {
|
||||||
|
"development": "https://github.com/manjaro",
|
||||||
|
"chat": "https://kiwiirc.com/client/irc.freenode.net/?nick=manjaro-web|?#manjaro",
|
||||||
|
"donate": "https://manjaro.org/donate",
|
||||||
|
"facebook": "https://www.facebook.com/ManjaroLinux",
|
||||||
|
"forums": "https://forum.manjaro.org",
|
||||||
|
"google+": "https://plus.google.com/118244873957924966264",
|
||||||
|
"mailling": "https://lists.manjaro.org/cgi-bin/mailman/listinfo",
|
||||||
|
"reddit": "https://www.reddit.com/r/ManjaroLinux",
|
||||||
|
"twitter": "https://twitter.com/ManjaroLinux",
|
||||||
|
"wiki": "https://wiki.manjaro.org"
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,12 +0,0 @@
|
||||||
{
|
|
||||||
"development": "https://github.com/manjaro",
|
|
||||||
"chat": "https://kiwiirc.com/client/irc.freenode.net/?nick=manjaro-web|?#manjaro",
|
|
||||||
"donate": "https://manjaro.org/donate",
|
|
||||||
"facebook": "https://www.facebook.com/ManjaroLinux",
|
|
||||||
"forums": "https://forum.manjaro.org",
|
|
||||||
"google+": "https://plus.google.com/118244873957924966264",
|
|
||||||
"mailling": "https://lists.manjaro.org/cgi-bin/mailman/listinfo",
|
|
||||||
"reddit": "https://www.reddit.com/r/ManjaroLinux",
|
|
||||||
"twitter": "https://twitter.com/ManjaroLinux",
|
|
||||||
"wiki": "https://wiki.manjaro.org"
|
|
||||||
}
|
|
|
@ -11,54 +11,39 @@ gi.require_version("Gtk", "3.0")
|
||||||
from gi.repository import Gtk, GdkPixbuf
|
from gi.repository import Gtk, GdkPixbuf
|
||||||
|
|
||||||
|
|
||||||
class ManjaroHello():
|
class Hello():
|
||||||
"""Manjaro Hello"""
|
"""Hello"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# Options
|
|
||||||
self.app = "manjaro-hello"
|
self.app = "manjaro-hello"
|
||||||
system = "Manjaro Gellivara"
|
self.dev = "--dev" in sys.argv
|
||||||
version = "17"
|
|
||||||
live_path = "/run/miso/bootmnt/manjaro"
|
|
||||||
logo_path = "/usr/share/icons/hicolor/64x64/apps/manjaro.png"
|
|
||||||
installer_path = "/usr/bin/calamares"
|
|
||||||
self.default_locale = "en"
|
|
||||||
|
|
||||||
# App vars
|
# Load preferences
|
||||||
dev = "--dev" in sys.argv
|
if self.dev:
|
||||||
self.home_path = os.path.expanduser("~")
|
self.preferences = read_json("data/preferences.json")
|
||||||
self.config_path = self.home_path + "/.config/"
|
self.preferences["data_path"] = "data/"
|
||||||
|
self.preferences["desktop_path"] = os.getcwd() + "/{}.desktop".format(self.app)
|
||||||
if not dev:
|
self.preferences["locale_path"] = "locale/"
|
||||||
self.data_path = "/usr/share/" + self.app + "/data/"
|
self.preferences["ui_path"] = "ui/{}.glade".format(self.app)
|
||||||
self.locale_path = "/usr/share/locale/"
|
|
||||||
ui_path = "/usr/share/" + self.app + "/ui/"
|
|
||||||
self.desktop_path = "/usr/share/applications/" + self.app + ".desktop"
|
|
||||||
else:
|
else:
|
||||||
self.data_path = "data/"
|
self.preferences = read_json("/usr/share/{}/data/preferences.json".format(self.app))
|
||||||
self.locale_path = "locale/"
|
|
||||||
ui_path = "ui/"
|
|
||||||
self.desktop_path = os.getcwd() + "/" + self.app + ".desktop"
|
|
||||||
|
|
||||||
urls_path = self.data_path + "urls.json"
|
# Get saved infos
|
||||||
self.preferences_path = self.config_path + self.app + ".json"
|
self.save = read_json(self.preferences["save_path"])
|
||||||
self.autostart_path = self.config_path + "autostart/" + self.app + ".desktop"
|
if not self.save:
|
||||||
|
self.save = {"locale": None}
|
||||||
# Load important vars
|
|
||||||
self.preferences = self.get_preferences()
|
|
||||||
self.urls = read_json(urls_path)
|
|
||||||
|
|
||||||
# Init window
|
# Init window
|
||||||
self.builder = Gtk.Builder.new_from_file(ui_path + self.app + ".glade")
|
self.builder = Gtk.Builder.new_from_file(self.preferences["ui_path"])
|
||||||
self.builder.connect_signals(self)
|
self.builder.connect_signals(self)
|
||||||
self.window = self.builder.get_object("window")
|
self.window = self.builder.get_object("window")
|
||||||
|
|
||||||
# Subtitle of headerbar
|
# Subtitle of headerbar
|
||||||
self.builder.get_object("headerbar").props.subtitle = "{} {}".format(system, version)
|
self.builder.get_object("headerbar").props.subtitle = self.preferences["system"]
|
||||||
|
|
||||||
# Load logo
|
# Load logo
|
||||||
if os.path.isfile(logo_path):
|
if os.path.isfile(self.preferences["logo_path"]):
|
||||||
logo = GdkPixbuf.Pixbuf.new_from_file(logo_path)
|
logo = GdkPixbuf.Pixbuf.new_from_file(self.preferences["logo_path"])
|
||||||
self.window.set_icon(logo)
|
self.window.set_icon(logo)
|
||||||
self.builder.get_object("distriblogo").set_from_pixbuf(logo)
|
self.builder.get_object("distriblogo").set_from_pixbuf(logo)
|
||||||
self.builder.get_object("aboutdialog").set_logo(logo)
|
self.builder.get_object("aboutdialog").set_logo(logo)
|
||||||
|
@ -76,25 +61,25 @@ class ManjaroHello():
|
||||||
|
|
||||||
# Init translation
|
# Init translation
|
||||||
self.default_texts = {}
|
self.default_texts = {}
|
||||||
gettext.bindtextdomain(self.app, self.locale_path)
|
gettext.bindtextdomain(self.app, self.preferences["locale_path"])
|
||||||
gettext.textdomain(self.app)
|
gettext.textdomain(self.app)
|
||||||
self.builder.get_object("languages").set_active_id(self.get_best_locale())
|
self.builder.get_object("languages").set_active_id(self.get_best_locale())
|
||||||
|
|
||||||
# Load images
|
# Load images
|
||||||
for img in ("google+", "facebook", "twitter", "reddit"):
|
for img in ("google+", "facebook", "twitter", "reddit"):
|
||||||
self.builder.get_object(img).set_from_file(self.data_path + "img/" + img + ".png")
|
self.builder.get_object(img).set_from_file(self.preferences["data_path"] + "img/" + img + ".png")
|
||||||
|
|
||||||
for btn in ("wiki", "forums", "chat", "mailling", "development", "donate"):
|
for btn in ("wiki", "forums", "chat", "mailling", "development", "donate"):
|
||||||
img = Gtk.Image.new_from_file(self.data_path + "img/external-link.png")
|
img = Gtk.Image.new_from_file(self.preferences["data_path"] + "img/external-link.png")
|
||||||
img.set_margin_left(2)
|
img.set_margin_left(2)
|
||||||
self.builder.get_object(btn).set_image(img)
|
self.builder.get_object(btn).set_image(img)
|
||||||
|
|
||||||
# Set autostart switcher state
|
# Set autostart switcher state
|
||||||
self.autostart = os.path.isfile(self.autostart_path)
|
self.autostart = os.path.isfile(self.preferences["autostart_path"])
|
||||||
self.builder.get_object("autostart").set_active(self.autostart)
|
self.builder.get_object("autostart").set_active(self.autostart)
|
||||||
|
|
||||||
# Live systems
|
# Live systems
|
||||||
if os.path.exists(live_path) and os.path.isfile(installer_path):
|
if os.path.exists(self.preferences["live_path"]) and os.path.isfile(self.preferences["installer_path"]):
|
||||||
self.builder.get_object("installlabel").set_visible(True)
|
self.builder.get_object("installlabel").set_visible(True)
|
||||||
self.builder.get_object("install").set_visible(True)
|
self.builder.get_object("install").set_visible(True)
|
||||||
|
|
||||||
|
@ -105,11 +90,11 @@ class ManjaroHello():
|
||||||
:return: locale to use
|
:return: locale to use
|
||||||
:rtype: str
|
:rtype: str
|
||||||
"""
|
"""
|
||||||
path = self.locale_path + "{}/LC_MESSAGES/" + self.app + ".mo"
|
path = self.preferences["locale_path"] + "{}/LC_MESSAGES/" + self.app + ".mo"
|
||||||
if self.preferences["locale"] == self.default_locale:
|
if self.save["locale"] == self.preferences["default_locale"]:
|
||||||
return self.default_locale
|
return self.preferences["default_locale"]
|
||||||
elif os.path.isfile(path.format(self.preferences["locale"])):
|
elif os.path.isfile(path.format(self.save["locale"])):
|
||||||
return self.preferences["locale"]
|
return self.save["locale"]
|
||||||
else:
|
else:
|
||||||
sys_locale = locale.getdefaultlocale()[0]
|
sys_locale = locale.getdefaultlocale()[0]
|
||||||
# If user's locale is supported
|
# If user's locale is supported
|
||||||
|
@ -122,7 +107,7 @@ class ManjaroHello():
|
||||||
elif os.path.isfile(path.format(sys_locale[:2])):
|
elif os.path.isfile(path.format(sys_locale[:2])):
|
||||||
return sys_locale[:2]
|
return sys_locale[:2]
|
||||||
else:
|
else:
|
||||||
return self.default_locale
|
return self.preferences["default_locale"]
|
||||||
|
|
||||||
def set_locale(self, locale):
|
def set_locale(self, locale):
|
||||||
"""Set locale of ui and pages.
|
"""Set locale of ui and pages.
|
||||||
|
@ -130,12 +115,12 @@ class ManjaroHello():
|
||||||
:type locale: str
|
:type locale: str
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
tr = gettext.translation(self.app, self.locale_path, [locale], fallback=True)
|
tr = gettext.translation(self.app, self.preferences["locale_path"], [locale], fallback=True)
|
||||||
tr.install()
|
tr.install()
|
||||||
except OSError:
|
except OSError:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.preferences["locale"] = locale
|
self.save["locale"] = locale
|
||||||
|
|
||||||
# Dirty code to fix an issue with gettext that can't translate strings from glade files
|
# Dirty code to fix an issue with gettext that can't translate strings from glade files
|
||||||
# Redfining all translatables strings
|
# Redfining all translatables strings
|
||||||
|
@ -180,17 +165,16 @@ class ManjaroHello():
|
||||||
label = child.get_children()[0].get_children()[0]
|
label = child.get_children()[0].get_children()[0]
|
||||||
label.set_markup(self.get_page(page))
|
label.set_markup(self.get_page(page))
|
||||||
|
|
||||||
|
|
||||||
def set_autostart(self, autostart):
|
def set_autostart(self, autostart):
|
||||||
"""Set state of autostart.
|
"""Set state of autostart.
|
||||||
:param autostart: wanted autostart state
|
:param autostart: wanted autostart state
|
||||||
:type autostart: bool
|
:type autostart: bool
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
if autostart and not os.path.isfile(self.autostart_path):
|
if autostart and not os.path.isfile(self.preferences["autostart_path"]):
|
||||||
os.symlink(self.desktop_path, self.autostart_path)
|
os.symlink(self.preferences["desktop_path"], self.preferences["autostart_path"])
|
||||||
elif not autostart and os.path.isfile(self.autostart_path):
|
elif not autostart and os.path.isfile(self.preferences["autostart_path"]):
|
||||||
os.unlink(self.autostart_path)
|
os.unlink(self.preferences["autostart_path"])
|
||||||
# Specific to i3
|
# Specific to i3
|
||||||
i3_config = self.home_path + "/.i3/config"
|
i3_config = self.home_path + "/.i3/config"
|
||||||
if os.path.isfile(i3_config):
|
if os.path.isfile(i3_config):
|
||||||
|
@ -207,17 +191,6 @@ class ManjaroHello():
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
print(error)
|
print(error)
|
||||||
|
|
||||||
def save_preferences(self):
|
|
||||||
"""Save preferences in config file."""
|
|
||||||
write_json(self.preferences_path, self.preferences)
|
|
||||||
|
|
||||||
def get_preferences(self):
|
|
||||||
"""Read preferences from config file."""
|
|
||||||
preferences = read_json(self.preferences_path)
|
|
||||||
if not preferences:
|
|
||||||
preferences = {"locale": None}
|
|
||||||
return preferences
|
|
||||||
|
|
||||||
def get_page(self, name):
|
def get_page(self, name):
|
||||||
"""Read page according to language.
|
"""Read page according to language.
|
||||||
:param name: name of page (filename)
|
:param name: name of page (filename)
|
||||||
|
@ -225,9 +198,9 @@ class ManjaroHello():
|
||||||
:return: text to load
|
:return: text to load
|
||||||
:rtype: str
|
:rtype: str
|
||||||
"""
|
"""
|
||||||
filename = self.data_path + "pages/{}/{}".format(self.preferences["locale"], name)
|
filename = self.preferences["data_path"] + "pages/{}/{}".format(self.save["locale"], name)
|
||||||
if not os.path.isfile(filename):
|
if not os.path.isfile(filename):
|
||||||
filename = self.data_path + "pages/{}/{}".format(self.default_locale, name)
|
filename = self.preferences["data_path"] + "pages/{}/{}".format(self.preferences["default_locale"], name)
|
||||||
try:
|
try:
|
||||||
with open(filename, "r") as f:
|
with open(filename, "r") as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
@ -263,10 +236,16 @@ class ManjaroHello():
|
||||||
|
|
||||||
def on_delete_window(self, *args):
|
def on_delete_window(self, *args):
|
||||||
"""Event to quit app."""
|
"""Event to quit app."""
|
||||||
self.save_preferences()
|
write_json(self.preferences["save_path"], self.save)
|
||||||
Gtk.main_quit(*args)
|
Gtk.main_quit(*args)
|
||||||
|
|
||||||
|
|
||||||
|
def fix_path(path):
|
||||||
|
if "~" in path:
|
||||||
|
path = path.replace("~", os.path.expanduser("~"))
|
||||||
|
return path
|
||||||
|
|
||||||
|
|
||||||
def read_json(path):
|
def read_json(path):
|
||||||
"""Read content of a json file.
|
"""Read content of a json file.
|
||||||
:param path: path of file to read
|
:param path: path of file to read
|
||||||
|
@ -274,12 +253,14 @@ def read_json(path):
|
||||||
:return: json content
|
:return: json content
|
||||||
:rtype: str
|
:rtype: str
|
||||||
"""
|
"""
|
||||||
|
path = fix_path(path)
|
||||||
try:
|
try:
|
||||||
with open(path, "r") as f:
|
with open(path, "r") as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
except OSError:
|
except OSError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def write_json(path, content):
|
def write_json(path, content):
|
||||||
"""Write content in a json file.
|
"""Write content in a json file.
|
||||||
:param path: path of file to write
|
:param path: path of file to write
|
||||||
|
@ -287,12 +268,14 @@ def write_json(path, content):
|
||||||
:param content: content to write
|
:param content: content to write
|
||||||
:type path: str
|
:type path: str
|
||||||
"""
|
"""
|
||||||
|
path = fix_path(path)
|
||||||
try:
|
try:
|
||||||
with open(path, "w") as f:
|
with open(path, "w") as f:
|
||||||
json.dump(content, f)
|
json.dump(content, f)
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
print(error)
|
print(error)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
ManjaroHello()
|
Hello()
|
||||||
Gtk.main()
|
Gtk.main()
|
||||||
|
|
Loading…
Reference in New Issue