Completely port autostart to python
This commit is contained in:
parent
6c49fa618d
commit
697e0b8802
@ -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__':
|
||||
|
||||
58
comfortable_swipe/autostart.py
Normal file
58
comfortable_swipe/autostart.py
Normal 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)
|
||||
9
comfortable_swipe/constants.py
Normal file
9
comfortable_swipe/constants.py
Normal 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')
|
||||
@ -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')
|
||||
@ -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__)))
|
||||
@ -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"
|
||||
|
||||
@ -12,7 +12,7 @@ extern "C"
|
||||
{
|
||||
// void autostart();
|
||||
void buffer();
|
||||
void config();
|
||||
// void config();
|
||||
void debug();
|
||||
void help();
|
||||
void restart();
|
||||
|
||||
@ -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
|
||||
};
|
||||
|
||||
64
setup.py
64
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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user