-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
122 lines (97 loc) · 3.54 KB
/
main.py
File metadata and controls
122 lines (97 loc) · 3.54 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import sys
from crontab import CronTab
from PyQt6.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QPushButton, QListWidget,
QInputDialog, QMessageBox, QHBoxLayout
)
# ---------------------- Constants ----------------------
WINDOW_TITLE = "Linux Cron Manager (PyQt6)"
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 400
# ---------------------- Main Class ----------------------
class CronManager(QWidget):
def __init__(self):
super().__init__()
self.cron = CronTab(user=True)
self.setup_window()
self.setup_ui()
self.refresh_jobs()
# ---------- Window Setup ----------
def setup_window(self):
self.setWindowTitle(WINDOW_TITLE)
self.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT)
# ---------- UI Setup ----------
def setup_ui(self):
main_layout = QVBoxLayout()
btn_layout = QHBoxLayout()
# Job List
self.listWidget = QListWidget()
main_layout.addWidget(self.listWidget)
# Buttons
self.create_button("Refresh", self.refresh_jobs, btn_layout)
self.create_button("Add Job", self.add_job, btn_layout)
self.create_button("Delete Job", self.delete_job, btn_layout)
main_layout.addLayout(btn_layout)
self.setLayout(main_layout)
# Helper to create buttons
def create_button(self, text, action, layout):
btn = QPushButton(text)
btn.clicked.connect(action)
layout.addWidget(btn)
# ---------- Cron Functions ----------
def refresh_jobs(self):
"""Reload job list from user crontab."""
self.listWidget.clear()
for job in self.cron:
self.listWidget.addItem(job.sentence)
def add_job(self):
"""Add a new cron job."""
schedule = self.get_input("Schedule", "Cron format: */5 * * * *")
if not schedule:
return
command = self.get_input("Command", "Command to execute:")
if not command:
return
try:
job = self.cron.new(command=command)
job.setall(schedule)
self.cron.write()
self.refresh_jobs()
except Exception as e:
self.show_error(str(e))
def delete_job(self):
"""Delete selected cron job."""
index = self.listWidget.currentRow()
if index < 0:
return self.show_warning("Please select a job to delete.")
if not self.confirm("Delete this job?"):
return
try:
for i, job in enumerate(self.cron):
if i == index:
self.cron.remove(job)
self.cron.write()
break
self.refresh_jobs()
except Exception as e:
self.show_error(str(e))
# ---------- Dialog Helpers ----------
def get_input(self, title, message):
value, ok = QInputDialog.getText(self, title, message)
return value.strip() if ok and value.strip() else None
def show_error(self, message):
QMessageBox.critical(self, "Error", message)
def show_warning(self, message):
QMessageBox.warning(self, "Warning", message)
def confirm(self, message):
result = QMessageBox.question(
self, "Confirm", message,
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
)
return result == QMessageBox.StandardButton.Yes
# ---------------------- App Runner ----------------------
if __name__ == "__main__":
app = QApplication(sys.argv)
window = CronManager()
window.show()
sys.exit(app.exec())