-
Notifications
You must be signed in to change notification settings - Fork 5
/
mynotes
executable file
·102 lines (90 loc) · 3.39 KB
/
mynotes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#! /usr/bin/python3
# -*- coding:Utf-8 -*-
"""
MyNotes - Sticky notes/post-it
Copyright 2016-2019 Juliette Monsel <[email protected]>
MyNotes is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MyNotes is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Main
"""
import logging
import os
import sys
import traceback
import signal
from tkinter import Tk
from tkinter.ttk import Style
import argparse
from mynoteslib.constants import save_config, PIDFILE
from mynoteslib.app import App
from mynoteslib.messagebox import showerror
from mynoteslib.version import __version__
# parse command line arguments
parser = argparse.ArgumentParser(description=_("MyNotes - Post-it system tray app"),
epilog=_("Report bugs to https://github.com/j4321/MyNotes/issues."))
parser.add_argument('-V', '--version', help=_('show version and exit'), action='store_true')
group = parser.add_mutually_exclusive_group()
group.add_argument('-H', '--hide-all', help=_('hide all notes'), action='store_true')
group.add_argument('-S', '--show-all', help=_('show all notes'), action='store_true')
group.add_argument('-T', '--toggle-all', help=_('toggle all notes'), action='store_true')
args = parser.parse_args()
if args.version:
print('mynotes ' + __version__)
sys.exit()
pid = str(os.getpid())
# check weher MyNotes is already running
if os.path.isfile(PIDFILE):
with open(PIDFILE) as fich:
old_pid = fich.read().strip()
if os.path.exists("/proc/%s" % old_pid):
with open("/proc/%s/cmdline" % old_pid) as file:
cmdline = file.read()
if 'mynotes' in cmdline: # MyNotes is already runnning
if args.show_all:
# send signal to mynotes to show all notes
os.kill(int(old_pid), signal.SIGUSR1)
sys.exit()
elif args.hide_all:
# send signal to mynotes to hide all notes
os.kill(int(old_pid), signal.SIGUSR2)
sys.exit()
elif args.toggle_all:
# send signal to mynotes to toggle all notes
os.kill(int(old_pid), signal.SIGVTALRM)
sys.exit()
else:
# MyNotes is already runnning
root = Tk()
root.withdraw()
s = Style(root)
s.theme_use("clam")
showerror(_("Error"), _("MyNotes is already running, if not delete {pidfile}.").format(pidfile=PIDFILE))
sys.exit()
else:
# it is an old .pid file
os.remove(PIDFILE)
else:
# it is an old pid file
os.remove(PIDFILE)
open(PIDFILE, 'w').write(pid)
try:
app = App(args)
app.mainloop()
except Exception as e:
showerror(_("Error"), str(type(e)), traceback.format_exc(), True)
finally:
try:
app.save()
save_config()
os.unlink(PIDFILE)
logging.shutdown()
except Exception:
pass