From d8da3388334b0f367ec85fe0bda419f4fac4796c Mon Sep 17 00:00:00 2001 From: Rico Tiongson Date: Sun, 3 Mar 2019 19:24:50 +0800 Subject: [PATCH] Move autostart to python --- MANIFEST.in | 1 + comfortable_swipe/__main__.py | 17 +++++----- comfortable_swipe/cpp/.gitkeep | 0 .../res/comfortable-swipe.desktop | 8 +++++ .../res/defaults.conf | 0 comfortable_swipe/service.py | 14 ++++++++ comfortable_swipe/util.py | 26 +++++++++++++++ {lib => cpp}/_index.cpp | 0 {lib => cpp}/_index.hpp | 0 {lib => cpp}/_macro.cpp | 0 {lib => cpp}/_python.cpp | 2 +- {lib => cpp}/comfortable-swipe | 0 {lib => cpp}/comfortable-swipe.cpp | 0 {lib => cpp}/gesture/_index.cpp | 0 {lib => cpp}/gesture/_index.hpp | 0 {lib => cpp}/gesture/swipe_gesture.cpp | 0 {lib => cpp}/gesture/swipe_gesture.h | 0 {lib => cpp}/gesture/swipe_gesture.regex.cpp | 0 {lib => cpp}/gesture/xdo_gesture.cpp | 0 {lib => cpp}/gesture/xdo_gesture.h | 0 {lib => cpp}/service/_index.cpp | 2 +- {lib => cpp}/service/_index.hpp | 2 +- {lib => cpp}/service/_python.cpp | 4 +-- {lib => cpp}/service/autostart.cpp | 0 {lib => cpp}/service/buffer.cpp | 0 {lib => cpp}/service/config.cpp | 0 {lib => cpp}/service/debug.cpp | 0 {lib => cpp}/service/help.cpp | 0 {lib => cpp}/service/restart.cpp | 0 {lib => cpp}/service/start.cpp | 0 {lib => cpp}/service/status.cpp | 0 {lib => cpp}/service/stop.cpp | 0 {lib => cpp}/util/_index.cpp | 0 {lib => cpp}/util/_index.hpp | 0 {lib => cpp}/util/_python.cpp | 0 {lib => cpp}/util/autostart_filename.cpp | 0 {lib => cpp}/util/conf_filename.cpp | 0 {lib => cpp}/util/read_config_file.cpp | 0 setup.py | 33 ++++++++++--------- 39 files changed, 81 insertions(+), 28 deletions(-) create mode 100644 MANIFEST.in create mode 100644 comfortable_swipe/cpp/.gitkeep create mode 100644 comfortable_swipe/res/comfortable-swipe.desktop rename defaults.conf => comfortable_swipe/res/defaults.conf (100%) create mode 100644 comfortable_swipe/service.py create mode 100644 comfortable_swipe/util.py rename {lib => cpp}/_index.cpp (100%) rename {lib => cpp}/_index.hpp (100%) rename {lib => cpp}/_macro.cpp (100%) rename {lib => cpp}/_python.cpp (86%) rename {lib => cpp}/comfortable-swipe (100%) rename {lib => cpp}/comfortable-swipe.cpp (100%) rename {lib => cpp}/gesture/_index.cpp (100%) rename {lib => cpp}/gesture/_index.hpp (100%) rename {lib => cpp}/gesture/swipe_gesture.cpp (100%) rename {lib => cpp}/gesture/swipe_gesture.h (100%) rename {lib => cpp}/gesture/swipe_gesture.regex.cpp (100%) rename {lib => cpp}/gesture/xdo_gesture.cpp (100%) rename {lib => cpp}/gesture/xdo_gesture.h (100%) rename {lib => cpp}/service/_index.cpp (92%) rename {lib => cpp}/service/_index.hpp (94%) rename {lib => cpp}/service/_python.cpp (94%) rename {lib => cpp}/service/autostart.cpp (100%) rename {lib => cpp}/service/buffer.cpp (100%) rename {lib => cpp}/service/config.cpp (100%) rename {lib => cpp}/service/debug.cpp (100%) rename {lib => cpp}/service/help.cpp (100%) rename {lib => cpp}/service/restart.cpp (100%) rename {lib => cpp}/service/start.cpp (100%) rename {lib => cpp}/service/status.cpp (100%) rename {lib => cpp}/service/stop.cpp (100%) rename {lib => cpp}/util/_index.cpp (100%) rename {lib => cpp}/util/_index.hpp (100%) rename {lib => cpp}/util/_python.cpp (100%) rename {lib => cpp}/util/autostart_filename.cpp (100%) rename {lib => cpp}/util/conf_filename.cpp (100%) rename {lib => cpp}/util/read_config_file.cpp (100%) diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..99782df --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include comfortable_swipe/res/comfortable-swipe.desktop diff --git a/comfortable_swipe/__main__.py b/comfortable_swipe/__main__.py index 8186c7d..71fc9a9 100644 --- a/comfortable_swipe/__main__.py +++ b/comfortable_swipe/__main__.py @@ -1,20 +1,21 @@ import sys from comfortable_swipe import service +from comfortable_swipe.cpp import service as cpp_service def main(): if len(sys.argv) <= 1: service.help() else: dict( - start=service.start, - stop=service.stop, - restart=service.restart, + start=cpp_service.start, + stop=cpp_service.stop, + restart=cpp_service.restart, autostart=service.autostart, - buffer=service.buffer, - help=service.help, - config=service.config, - debug=service.debug, - status=service.status + buffer=cpp_service.buffer, + help=cpp_service.help, + config=cpp_service.config, + debug=cpp_service.debug, + status=cpp_service.status )[sys.argv[1]]() if __name__ == '__main__': diff --git a/comfortable_swipe/cpp/.gitkeep b/comfortable_swipe/cpp/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/comfortable_swipe/res/comfortable-swipe.desktop b/comfortable_swipe/res/comfortable-swipe.desktop new file mode 100644 index 0000000..fc0cf02 --- /dev/null +++ b/comfortable_swipe/res/comfortable-swipe.desktop @@ -0,0 +1,8 @@ +[Desktop Entry] +Type=Application +Exec=comfortable-swipe start +Hidden=false +NoDisplay=false +X-GNOME-Autostart-enabled=true +Name=Comfortable Swipe +Comment=3 or 4 finger swipe gestures diff --git a/defaults.conf b/comfortable_swipe/res/defaults.conf similarity index 100% rename from defaults.conf rename to comfortable_swipe/res/defaults.conf diff --git a/comfortable_swipe/service.py b/comfortable_swipe/service.py new file mode 100644 index 0000000..700b31d --- /dev/null +++ b/comfortable_swipe/service.py @@ -0,0 +1,14 @@ +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 new file mode 100644 index 0000000..2bc9d6c --- /dev/null +++ b/comfortable_swipe/util.py @@ -0,0 +1,26 @@ +import os + +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=' + find_executable(__EXE__)) diff --git a/lib/_index.cpp b/cpp/_index.cpp similarity index 100% rename from lib/_index.cpp rename to cpp/_index.cpp diff --git a/lib/_index.hpp b/cpp/_index.hpp similarity index 100% rename from lib/_index.hpp rename to cpp/_index.hpp diff --git a/lib/_macro.cpp b/cpp/_macro.cpp similarity index 100% rename from lib/_macro.cpp rename to cpp/_macro.cpp diff --git a/lib/_python.cpp b/cpp/_python.cpp similarity index 86% rename from lib/_python.cpp rename to cpp/_python.cpp index fcddd3e..ca5b849 100644 --- a/lib/_python.cpp +++ b/cpp/_python.cpp @@ -2,7 +2,7 @@ #define __COMFORTABLE_SWIPE__python_cpp__ #include "service/_python.cpp" -#include "util/_python.cpp" +// #include "util/_python.cpp" #include "comfortable-swipe.cpp" #endif /* __COMFORTABLE_SWIPE__python_cpp__ */ diff --git a/lib/comfortable-swipe b/cpp/comfortable-swipe similarity index 100% rename from lib/comfortable-swipe rename to cpp/comfortable-swipe diff --git a/lib/comfortable-swipe.cpp b/cpp/comfortable-swipe.cpp similarity index 100% rename from lib/comfortable-swipe.cpp rename to cpp/comfortable-swipe.cpp diff --git a/lib/gesture/_index.cpp b/cpp/gesture/_index.cpp similarity index 100% rename from lib/gesture/_index.cpp rename to cpp/gesture/_index.cpp diff --git a/lib/gesture/_index.hpp b/cpp/gesture/_index.hpp similarity index 100% rename from lib/gesture/_index.hpp rename to cpp/gesture/_index.hpp diff --git a/lib/gesture/swipe_gesture.cpp b/cpp/gesture/swipe_gesture.cpp similarity index 100% rename from lib/gesture/swipe_gesture.cpp rename to cpp/gesture/swipe_gesture.cpp diff --git a/lib/gesture/swipe_gesture.h b/cpp/gesture/swipe_gesture.h similarity index 100% rename from lib/gesture/swipe_gesture.h rename to cpp/gesture/swipe_gesture.h diff --git a/lib/gesture/swipe_gesture.regex.cpp b/cpp/gesture/swipe_gesture.regex.cpp similarity index 100% rename from lib/gesture/swipe_gesture.regex.cpp rename to cpp/gesture/swipe_gesture.regex.cpp diff --git a/lib/gesture/xdo_gesture.cpp b/cpp/gesture/xdo_gesture.cpp similarity index 100% rename from lib/gesture/xdo_gesture.cpp rename to cpp/gesture/xdo_gesture.cpp diff --git a/lib/gesture/xdo_gesture.h b/cpp/gesture/xdo_gesture.h similarity index 100% rename from lib/gesture/xdo_gesture.h rename to cpp/gesture/xdo_gesture.h diff --git a/lib/service/_index.cpp b/cpp/service/_index.cpp similarity index 92% rename from lib/service/_index.cpp rename to cpp/service/_index.cpp index 94a6fb5..2ba7069 100644 --- a/lib/service/_index.cpp +++ b/cpp/service/_index.cpp @@ -2,7 +2,7 @@ #define __COMFORTABLE_SWIPE__service_index_cpp__ #include "_index.hpp" -#include "autostart.cpp" +// #include "autostart.cpp" #include "buffer.cpp" #include "config.cpp" #include "debug.cpp" diff --git a/lib/service/_index.hpp b/cpp/service/_index.hpp similarity index 94% rename from lib/service/_index.hpp rename to cpp/service/_index.hpp index 2ff0052..f7770d5 100644 --- a/lib/service/_index.hpp +++ b/cpp/service/_index.hpp @@ -10,7 +10,7 @@ extern "C" { namespace service { - void autostart(); + // void autostart(); void buffer(); void config(); void debug(); diff --git a/lib/service/_python.cpp b/cpp/service/_python.cpp similarity index 94% rename from lib/service/_python.cpp rename to cpp/service/_python.cpp index e44e0f0..8cea13d 100644 --- a/lib/service/_python.cpp +++ b/cpp/service/_python.cpp @@ -20,7 +20,7 @@ namespace comfortable_swipe::service::python __comfortable_swipe_void_method(start); __comfortable_swipe_void_method(stop); __comfortable_swipe_void_method(restart); - __comfortable_swipe_void_method(autostart); + // __comfortable_swipe_void_method(autostart); __comfortable_swipe_void_method(buffer); __comfortable_swipe_void_method(help); __comfortable_swipe_void_method(config); @@ -35,7 +35,7 @@ namespace comfortable_swipe::service::python { "start", &start, METH_VARARGS , "starts 3/4-finger gesture service" }, { "stop", &stop, METH_VARARGS , "stops 3/4-finger gesture service" }, { "restart", &restart, METH_VARARGS , "stops then starts 3/4-finger gesture service" }, - { "autostart", &autostart, METH_VARARGS , "automatically run on startup (toggleable)" }, + // { "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 " }, diff --git a/lib/service/autostart.cpp b/cpp/service/autostart.cpp similarity index 100% rename from lib/service/autostart.cpp rename to cpp/service/autostart.cpp diff --git a/lib/service/buffer.cpp b/cpp/service/buffer.cpp similarity index 100% rename from lib/service/buffer.cpp rename to cpp/service/buffer.cpp diff --git a/lib/service/config.cpp b/cpp/service/config.cpp similarity index 100% rename from lib/service/config.cpp rename to cpp/service/config.cpp diff --git a/lib/service/debug.cpp b/cpp/service/debug.cpp similarity index 100% rename from lib/service/debug.cpp rename to cpp/service/debug.cpp diff --git a/lib/service/help.cpp b/cpp/service/help.cpp similarity index 100% rename from lib/service/help.cpp rename to cpp/service/help.cpp diff --git a/lib/service/restart.cpp b/cpp/service/restart.cpp similarity index 100% rename from lib/service/restart.cpp rename to cpp/service/restart.cpp diff --git a/lib/service/start.cpp b/cpp/service/start.cpp similarity index 100% rename from lib/service/start.cpp rename to cpp/service/start.cpp diff --git a/lib/service/status.cpp b/cpp/service/status.cpp similarity index 100% rename from lib/service/status.cpp rename to cpp/service/status.cpp diff --git a/lib/service/stop.cpp b/cpp/service/stop.cpp similarity index 100% rename from lib/service/stop.cpp rename to cpp/service/stop.cpp diff --git a/lib/util/_index.cpp b/cpp/util/_index.cpp similarity index 100% rename from lib/util/_index.cpp rename to cpp/util/_index.cpp diff --git a/lib/util/_index.hpp b/cpp/util/_index.hpp similarity index 100% rename from lib/util/_index.hpp rename to cpp/util/_index.hpp diff --git a/lib/util/_python.cpp b/cpp/util/_python.cpp similarity index 100% rename from lib/util/_python.cpp rename to cpp/util/_python.cpp diff --git a/lib/util/autostart_filename.cpp b/cpp/util/autostart_filename.cpp similarity index 100% rename from lib/util/autostart_filename.cpp rename to cpp/util/autostart_filename.cpp diff --git a/lib/util/conf_filename.cpp b/cpp/util/conf_filename.cpp similarity index 100% rename from lib/util/conf_filename.cpp rename to cpp/util/conf_filename.cpp diff --git a/lib/util/read_config_file.cpp b/cpp/util/read_config_file.cpp similarity index 100% rename from lib/util/read_config_file.cpp rename to cpp/util/read_config_file.cpp diff --git a/setup.py b/setup.py index dd3a6bd..9d23fec 100644 --- a/setup.py +++ b/setup.py @@ -11,26 +11,29 @@ 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' + __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') __URL__ = 'https://github.com/Hikari9/comfortable-swipe-ubuntu' -NAME = 'comfortable-swipe' -PYTHON_NAME = NAME.replace('-', '_') -VERSION = '1.1.0-beta' try: # make sure working directory is here os.chdir(__DIR__) - # assign program variables + # assign config CONFIG = os.path.join(__SHARE__, NAME, NAME + '.conf') + DEFAULT_CONFIG = os.path.join(__RES__, 'defaults.conf') # prioritize the higher indices conf_paths = [ - os.path.join(__DIR__, 'defaults.conf'), + DEFAULT_CONFIG, os.path.join(os.getenv('HOME'), '.config', 'comfortable-swipe', 'comfortable-swipe.conf'), os.path.join('/usr/local/share', 'comfortable-swipe', 'comfortable-swipe.conf'), CONFIG @@ -47,11 +50,11 @@ try: README = README_file.read() # read C++ libraries for comfortable swipe - extension_names = ['service', 'util'] + extension_names = ['service'] extensions = [Extension( - name='{}.{}'.format(PYTHON_NAME, extension_name), + name='{}.cpp.{}'.format(PYTHON_NAME, extension_name), define_macros=list(cpp_macros.items()), - sources=[os.path.join(__DIR__, 'lib', '_python.cpp')], + sources=[os.path.join('cpp', '_python.cpp')], extra_compile_args=['-O2', '-Wno-unused-result'], libraries=['xdo'] ) for extension_name in extension_names] @@ -74,7 +77,7 @@ try: # new installation or upgrading from old version, copy to new location copyfile(conf_files[-1], CONFIG) - if conf_files[-1] == os.path.join(__DIR__, 'defaults.conf'): + if conf_files[-1] == DEFAULT_CONFIG: # new installation - copy default configuration print('Copying configuration file to', CONFIG) @@ -94,11 +97,10 @@ try: def pre_uninstall(self): # remove autostart config - from comfortable_swipe import util - autostart_filename = str(util.autostart_filename) - if os.path.exists(autostart_filename): - print('Removing autostart', autostart_filename) - os.remove(autostart_filename) + from comfortable_swipe.util import autostart_path + from comfortable_swipe.service import autostart + if os.path.exists(autostart_path()): + autostart() def post_uninstall(self): @@ -145,8 +147,9 @@ try: author='Rico Tiongson', author_email='thericotiongson@gmail.com', url=__URL__, - # zip_safe=False, + zip_safe=False, packages=find_packages(), + include_package_data=True, entry_points=dict(console_scripts=['{}=comfortable_swipe.__main__:main'.format(NAME)]), ext_modules=extensions, cmdclass=cmdclass