-
Notifications
You must be signed in to change notification settings - Fork 0
/
caffeine.py
148 lines (105 loc) · 4.21 KB
/
caffeine.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
MIT License
Copyright (c) 2024 kaffa
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
"""
## 说明
本软件适用于 Windows 下使用 TiddlyWiki Single File + Github Pages 使用。它与 aio.cmd 一起使用实现写作保存后自动推送 GitHub。
## 使用方法
在保存 TiddlyWiki 条目之前,先打开命令行,进入 TiddlyWiki 目录,再运行脚本 `caffeine.py`。例如:
```shell
cd /d d:/your-tiddlywiki-folder/
python caffeine.py
```
## 默认路径
下载目录是浏览器下载目录,也可以通过命令行参数在运行时指定:
```python
python caffeine.py /path-to-your-downloaded-tiddlywiki.html-file
```
## macOS
它很容易移植到 macOS 因为它使用 watchdog。
"""
"""
## Description
This software is applicable for using TiddlyWiki Single File + Github Pages on Windows.
It is used together with aio.cmd to achieve the automatic push to GitHub after writing and saving.
## Usage
Before saving the TiddlyWiki entry, open the command line first, enter the TiddlyWiki directory, and then run the script `caffeine.py`. For example:
```shell
cd /d d:/your-tiddlywiki-folder/
python caffeine.py
```
## Default path
The download directory is the browser download directory, and it can also be specified at runtime through command line parameters:
```python
python caffeine.py /path-to-your-downloaded-tiddlywiki.html-file
```
## macOS
It can be easily ported to macOS as it uses watchdog.
"""
import os
import sys
import time
import re
import subprocess
import shutil
import threading
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class Watcher:
path = sys.argv[1] if len(sys.argv) > 1 else os.getenv('USERPROFILE') + '\\Downloads'
def __init__(self):
self.observer = Observer()
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.path, recursive=True)
self.observer.start()
try:
while True:
time.sleep(5)
except:
self.observer.stop()
print("Error")
self.observer.join()
def run(path):
shutil.copyfile(path, 'index.html')
subprocess.Popen(f'cmd /c "aio.cmd"', shell=True)
class Handler(FileSystemEventHandler):
timer = None
@staticmethod
def on_any_event(event):
print(event.event_type)
if event.is_directory:
return None
elif event.event_type == 'created':
# Take any action here when a file is first created.
print("Received created event - %s." % event.src_path)
elif event.event_type == 'moved':
# Take any action here when a file is first created.
print("Received moved event - %s." % event.src_path)
elif event.event_type == 'modified':
# Taken any action here when a file is modified.
print("Received modified event - %s." % event.src_path)
if re.match(r"tiddlywiki( \([\d+]*?\))?\.html", event.src_path.split('\\').pop()):
if Handler.timer:
Handler.timer.cancel()
Handler.timer = threading.Timer(2, lambda: run(event.src_path))
Handler.timer.start()
if __name__ == '__main__':
w = Watcher()
w.run()