From 697e0b88023098ff2cfd11e75cd207dbd1db95d8 Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Sun, 3 Mar 2019 20:24:50 +0800 Subject: [PATCH] Completely port autostart to python --- comfortable_swipe/__main__.py | 13 ++++--- comfortable_swipe/autostart.py | 58 ++++++++++++++++++++++++++++++ comfortable_swipe/constants.py | 9 +++++ comfortable_swipe/service.py | 14 -------- comfortable_swipe/util.py | 27 -------------- cpp/service/_index.cpp | 2 +- cpp/service/_index.hpp | 2 +- cpp/service/_python.cpp | 4 +-- setup.py | 64 +++++++++++++++------------------- 9 files changed, 107 insertions(+), 86 deletions(-) create mode 100644 comfortable_swipe/autostart.py create mode 100644 comfortable_swipe/constants.py delete mode 100644 comfortable_swipe/service.py delete mode 100644 comfortable_swipe/util.py diff --git a/comfortable_swipe/__main__.py b/comfortable_swipe/__main__.py index 71fc9a9..67e7314 100644 --- a/comfortable_swipe/__main__.py +++ b/comfortable_swipe/__main__.py @@ -1,21 +1,24 @@ +from __future__ import print_function + import sys -from comfortable_swipe import service +from comfortable_swipe.autostart import toggle_status from comfortable_swipe.cpp import service as cpp_service +from comfortable_swipe.constants import CONFIG def main(): if len(sys.argv) <= 1: - service.help() + cpp_service.help() else: dict( start=cpp_service.start, stop=cpp_service.stop, restart=cpp_service.restart, - autostart=service.autostart, buffer=cpp_service.buffer, help=cpp_service.help, - config=cpp_service.config, debug=cpp_service.debug, - status=cpp_service.status + status=cpp_service.status, + autostart=lambda: print('Autostart switched', toggle_status()), + config=lambda: print(CONFIG), )[sys.argv[1]]() if __name__ == '__main__': diff --git a/comfortable_swipe/autostart.py b/comfortable_swipe/autostart.py new file mode 100644 index 0000000..2de4ba6 --- /dev/null +++ b/comfortable_swipe/autostart.py @@ -0,0 +1,58 @@ +from __future__ import print_function + +import os +import sys +from distutils.spawn import find_executable +from comfortable_swipe.constants import EXE, RES + + +# status enums +OFF = 'off' +ON = 'on' + + +# the target path to the autostart desktop file +def target_path(): + return os.path.join( + os.getenv('XDG_CONFIG_HOME', os.path.join(os.getenv('HOME'), '.config')), + 'autostart', + EXE + '.desktop' + ) + + +# path to the autostart template file to be copied +def template_path(): + return os.path.join(RES, EXE + '.desktop') + + +# parsed contents of the template file +def template(raw=False): + with open(template_path(), 'r') as file: + contents = file.read() + if raw: + return contents + return contents.replace('Exec=' + EXE, 'Exec={} {}'.format(sys.executable, find_executable(EXE))) + + +# gets the current autostart status +def get_status(): + return ON if os.path.exists(target_path()) else OFF + + +# sets the autostart status +def set_status(status=ON): + if status is ON: + with open(target_path(), 'w') as file: + file.write(template()) + elif status is OFF: + if os.path.exists(target_path()): + os.remove(target_path()) + else: + raise ValueError('invalid status for autostart') + + return status + + +# toggles autostart status +def toggle_status(): + return set_status(OFF if get_status() == ON else OFF) diff --git a/comfortable_swipe/constants.py b/comfortable_swipe/constants.py new file mode 100644 index 0000000..805dbea --- /dev/null +++ b/comfortable_swipe/constants.py @@ -0,0 +1,9 @@ +import os +import sys + + +EXE = 'comfortable-swipe' +DIR = os.path.dirname(os.path.abspath(__file__)) +RES = os.path.join(DIR, 'res') +CONFIG = os.path.join(sys.prefix, 'local', 'share', EXE, EXE + '.conf') +DEFAULT_CONFIG = os.path.join(RES, 'defaults.conf') diff --git a/comfortable_swipe/service.py b/comfortable_swipe/service.py deleted file mode 100644 index 700b31d..0000000 --- a/comfortable_swipe/service.py +++ /dev/null @@ -1,14 +0,0 @@ -from __future__ import print_function - -import os -from comfortable_swipe.util import autostart_path, autostart_template - -def autostart(): - autostart = autostart_path() - if os.path.exists(autostart): - os.remove(autostart) - print('Autostart switched off') - else: - with open(autostart, 'w') as file: - file.write(autostart_template()) - print('Autostart switched on') diff --git a/comfortable_swipe/util.py b/comfortable_swipe/util.py deleted file mode 100644 index f2bd143..0000000 --- a/comfortable_swipe/util.py +++ /dev/null @@ -1,27 +0,0 @@ -import os -import sys - -from distutils.spawn import find_executable - -__EXE__ = 'comfortable-swipe' -__DIR__ = os.path.dirname(__file__) -__RES__ = os.path.join(__DIR__, 'res') - - -def conf_filename(): - return os.path.join(sys.prefix, 'local', 'share', __EXE__, __EXE__ + '.conf') - - -def autostart_path(): - return os.path.join( - os.getenv('XDG_CONFIG_HOME', os.path.join(os.getenv('HOME'), '.config')), - 'autostart', - __EXE__ + '.desktop' - ) - - -def autostart_template(): - autostart_template_filename = os.path.join(__RES__, __EXE__ + '.desktop') - with open(autostart_template_filename, 'r') as file: - contents = file.read() - return contents.replace('Exec=' + __EXE__, 'Exec={} {}'.format(sys.executable, find_executable(__EXE__))) diff --git a/cpp/service/_index.cpp b/cpp/service/_index.cpp index 2ba7069..7e73b15 100644 --- a/cpp/service/_index.cpp +++ b/cpp/service/_index.cpp @@ -4,7 +4,7 @@ #include "_index.hpp" // #include "autostart.cpp" #include "buffer.cpp" -#include "config.cpp" +// #include "config.cpp" #include "debug.cpp" #include "help.cpp" #include "restart.cpp" diff --git a/cpp/service/_index.hpp b/cpp/service/_index.hpp index f7770d5..dbb77be 100644 --- a/cpp/service/_index.hpp +++ b/cpp/service/_index.hpp @@ -12,7 +12,7 @@ extern "C" { // void autostart(); void buffer(); - void config(); + // void config(); void debug(); void help(); void restart(); diff --git a/cpp/service/_python.cpp b/cpp/service/_python.cpp index 8cea13d..1316948 100644 --- a/cpp/service/_python.cpp +++ b/cpp/service/_python.cpp @@ -23,7 +23,7 @@ namespace comfortable_swipe::service::python // __comfortable_swipe_void_method(autostart); __comfortable_swipe_void_method(buffer); __comfortable_swipe_void_method(help); - __comfortable_swipe_void_method(config); + // __comfortable_swipe_void_method(config); __comfortable_swipe_void_method(debug); #undef __comfortable_swipe_void_method @@ -38,7 +38,7 @@ namespace comfortable_swipe::service::python // { "autostart", &autostart, METH_VARARGS , "automatically run on startup (toggleable)" }, { "buffer", &buffer, METH_VARARGS , "parses output of libinput debug-events" }, { "help", &help, METH_VARARGS , "shows the help dialog" }, - { "config", &config, METH_VARARGS , "locates the config file " }, + // { "config", &config, METH_VARARGS , "locates the config file " }, { "debug", &debug, METH_VARARGS , "logs raw output from input events taken from libinput" }, { NULL, NULL, 0, NULL } // sentinel }; diff --git a/setup.py b/setup.py index 9d23fec..258c983 100644 --- a/setup.py +++ b/setup.py @@ -11,15 +11,11 @@ from setuptools.command.develop import develop from setuptools.command.install import install from wheel.bdist_wheel import bdist_wheel -NAME = 'comfortable-swipe' -PYTHON_NAME = NAME.replace('-', '_') -VERSION = '1.1.0-beta' +VERSION = '1.1.0-beta' __BIN__ = os.path.dirname(sys.executable) -__SHARE__ = os.path.join(sys.prefix, 'local', 'share') __CWD__ = os.getcwd() -__DIR__ = os.path.abspath(os.path.dirname(__file__)) -__RES__ = os.path.join(__DIR__, PYTHON_NAME, 'res') +__DIR__ = os.path.dirname(os.path.abspath(__file__)) __URL__ = 'https://github.com/Hikari9/comfortable-swipe-ubuntu' @@ -27,11 +23,14 @@ try: # make sure working directory is here os.chdir(__DIR__) - # assign config - CONFIG = os.path.join(__SHARE__, NAME, NAME + '.conf') - DEFAULT_CONFIG = os.path.join(__RES__, 'defaults.conf') + # match constants with source + from comfortable_swipe.constants import EXE, RES, CONFIG, DEFAULT_CONFIG - # prioritize the higher indices + # additional constants + NAME = EXE + PYTHON_NAME = NAME.replace('-', '_') + + # include old conf paths to list from previous versions conf_paths = [ DEFAULT_CONFIG, os.path.join(os.getenv('HOME'), '.config', 'comfortable-swipe', 'comfortable-swipe.conf'), @@ -61,10 +60,14 @@ try: def pre_install(self): + # print('running pre_install') pass def post_install(self): + + print('running post_install') + # create program/config directories if not os.path.exists(os.path.dirname(CONFIG)): os.makedirs(os.path.dirname(CONFIG)) @@ -86,28 +89,26 @@ try: print('warning: depcrecated configuration file at', conf_files[-1]) print(' you have to remove this manually') - # toggle autostart - os.chdir(os.getenv('HOME')) - from comfortable_swipe import service - service.autostart() - service.autostart() - - print('\nInstallation successful\nTry running "{} start"'.format(NAME)) + # enable autostart by default + from comfortable_swipe import autostart + autostart.set_status(autostart.ON) + print('Autostart created at', autostart.target_path()) + print('\nInstallation successful\nTry running: {} start'.format(NAME)) def pre_uninstall(self): + print('running pre_uninstall') # remove autostart config - from comfortable_swipe.util import autostart_path - from comfortable_swipe.service import autostart - if os.path.exists(autostart_path()): - autostart() + from comfortable_swipe import autostart + + if autostart.get_status() is autostart.ON: + print('Removing autostart at', autostart.target_path()) + autostart.set_status(autostart.OFF) def post_uninstall(self): - # provide warning for manual removal of configuration file - if os.path.exists(CONFIG) and os.path.isfile(CONFIG): - print('You have to manually remove {}'.format(CONFIG)) - print('Successfully uninstalled', NAME) + # print('running post_uninstall') + pass @@ -121,18 +122,9 @@ try: class Develop(develop): def run(self): - - if self.uninstall: - pre_uninstall(self) - else: - pre_install(self) - + pre_uninstall(self) if self.uninstall else pre_install(self) develop.run(self) - - if self.uninstall: - post_uninstall(self) - else: - post_install(self) + post_uninstall(self) if self.uninstall else post_install(self) # Override command classes here cmdclass = dict(Install=Install, develop=Develop, bdist_wheel=bdist_wheel)