Completely port autostart to python

This commit is contained in:
Rico Tiongson 2019-03-03 20:24:50 +08:00
parent 6c49fa618d
commit 697e0b8802
9 changed files with 107 additions and 86 deletions

View File

@ -1,21 +1,24 @@
from __future__ import print_function
import sys 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.cpp import service as cpp_service
from comfortable_swipe.constants import CONFIG
def main(): def main():
if len(sys.argv) <= 1: if len(sys.argv) <= 1:
service.help() cpp_service.help()
else: else:
dict( dict(
start=cpp_service.start, start=cpp_service.start,
stop=cpp_service.stop, stop=cpp_service.stop,
restart=cpp_service.restart, restart=cpp_service.restart,
autostart=service.autostart,
buffer=cpp_service.buffer, buffer=cpp_service.buffer,
help=cpp_service.help, help=cpp_service.help,
config=cpp_service.config,
debug=cpp_service.debug, 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]]() )[sys.argv[1]]()
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -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)

View File

@ -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')

View File

@ -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')

View File

@ -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__)))

View File

@ -4,7 +4,7 @@
#include "_index.hpp" #include "_index.hpp"
// #include "autostart.cpp" // #include "autostart.cpp"
#include "buffer.cpp" #include "buffer.cpp"
#include "config.cpp" // #include "config.cpp"
#include "debug.cpp" #include "debug.cpp"
#include "help.cpp" #include "help.cpp"
#include "restart.cpp" #include "restart.cpp"

View File

@ -12,7 +12,7 @@ extern "C"
{ {
// void autostart(); // void autostart();
void buffer(); void buffer();
void config(); // void config();
void debug(); void debug();
void help(); void help();
void restart(); void restart();

View File

@ -23,7 +23,7 @@ namespace comfortable_swipe::service::python
// __comfortable_swipe_void_method(autostart); // __comfortable_swipe_void_method(autostart);
__comfortable_swipe_void_method(buffer); __comfortable_swipe_void_method(buffer);
__comfortable_swipe_void_method(help); __comfortable_swipe_void_method(help);
__comfortable_swipe_void_method(config); // __comfortable_swipe_void_method(config);
__comfortable_swipe_void_method(debug); __comfortable_swipe_void_method(debug);
#undef __comfortable_swipe_void_method #undef __comfortable_swipe_void_method
@ -38,7 +38,7 @@ namespace comfortable_swipe::service::python
// { "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" }, { "buffer", &buffer, METH_VARARGS , "parses output of libinput debug-events" },
{ "help", &help, METH_VARARGS , "shows the help dialog" }, { "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" }, { "debug", &debug, METH_VARARGS , "logs raw output from input events taken from libinput" },
{ NULL, NULL, 0, NULL } // sentinel { NULL, NULL, 0, NULL } // sentinel
}; };

View File

@ -11,15 +11,11 @@ from setuptools.command.develop import develop
from setuptools.command.install import install from setuptools.command.install import install
from wheel.bdist_wheel import bdist_wheel 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) __BIN__ = os.path.dirname(sys.executable)
__SHARE__ = os.path.join(sys.prefix, 'local', 'share')
__CWD__ = os.getcwd() __CWD__ = os.getcwd()
__DIR__ = os.path.abspath(os.path.dirname(__file__)) __DIR__ = os.path.dirname(os.path.abspath(__file__))
__RES__ = os.path.join(__DIR__, PYTHON_NAME, 'res')
__URL__ = 'https://github.com/Hikari9/comfortable-swipe-ubuntu' __URL__ = 'https://github.com/Hikari9/comfortable-swipe-ubuntu'
@ -27,11 +23,14 @@ try:
# make sure working directory is here # make sure working directory is here
os.chdir(__DIR__) os.chdir(__DIR__)
# assign config # match constants with source
CONFIG = os.path.join(__SHARE__, NAME, NAME + '.conf') from comfortable_swipe.constants import EXE, RES, CONFIG, DEFAULT_CONFIG
DEFAULT_CONFIG = os.path.join(__RES__, 'defaults.conf')
# prioritize the higher indices # additional constants
NAME = EXE
PYTHON_NAME = NAME.replace('-', '_')
# include old conf paths to list from previous versions
conf_paths = [ conf_paths = [
DEFAULT_CONFIG, DEFAULT_CONFIG,
os.path.join(os.getenv('HOME'), '.config', 'comfortable-swipe', 'comfortable-swipe.conf'), os.path.join(os.getenv('HOME'), '.config', 'comfortable-swipe', 'comfortable-swipe.conf'),
@ -61,10 +60,14 @@ try:
def pre_install(self): def pre_install(self):
# print('running pre_install')
pass pass
def post_install(self): def post_install(self):
print('running post_install')
# create program/config directories # create program/config directories
if not os.path.exists(os.path.dirname(CONFIG)): if not os.path.exists(os.path.dirname(CONFIG)):
os.makedirs(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('warning: depcrecated configuration file at', conf_files[-1])
print(' you have to remove this manually') print(' you have to remove this manually')
# toggle autostart # enable autostart by default
os.chdir(os.getenv('HOME')) from comfortable_swipe import autostart
from comfortable_swipe import service autostart.set_status(autostart.ON)
service.autostart() print('Autostart created at', autostart.target_path())
service.autostart() print('\nInstallation successful\nTry running: {} start'.format(NAME))
print('\nInstallation successful\nTry running "{} start"'.format(NAME))
def pre_uninstall(self): def pre_uninstall(self):
print('running pre_uninstall')
# remove autostart config # remove autostart config
from comfortable_swipe.util import autostart_path from comfortable_swipe import autostart
from comfortable_swipe.service import autostart
if os.path.exists(autostart_path()): if autostart.get_status() is autostart.ON:
autostart() print('Removing autostart at', autostart.target_path())
autostart.set_status(autostart.OFF)
def post_uninstall(self): def post_uninstall(self):
# provide warning for manual removal of configuration file # print('running post_uninstall')
if os.path.exists(CONFIG) and os.path.isfile(CONFIG): pass
print('You have to manually remove {}'.format(CONFIG))
print('Successfully uninstalled', NAME)
@ -121,18 +122,9 @@ try:
class Develop(develop): class Develop(develop):
def run(self): def run(self):
pre_uninstall(self) if self.uninstall else pre_install(self)
if self.uninstall:
pre_uninstall(self)
else:
pre_install(self)
develop.run(self) develop.run(self)
post_uninstall(self) if self.uninstall else post_install(self)
if self.uninstall:
post_uninstall(self)
else:
post_install(self)
# Override command classes here # Override command classes here
cmdclass = dict(Install=Install, develop=Develop, bdist_wheel=bdist_wheel) cmdclass = dict(Install=Install, develop=Develop, bdist_wheel=bdist_wheel)