Forced single instance when watching

This commit is contained in:
Maboroshy 2017-09-14 22:08:58 +04:00 committed by GitHub
parent 8a2a34c114
commit 65ce04845e
2 changed files with 29 additions and 22 deletions

View File

@ -2,9 +2,9 @@
import os
import re
import sys
import time
import argparse
import platform
import collections
import multiprocessing.dummy
@ -53,7 +53,7 @@ def text_to_md(file_attrs, topic_marker):
try:
with open(file_attrs.file_path, 'r') as text_file:
text = text_file.read()
except(UnicodeDecodeError):
except UnicodeDecodeError:
return
topics = re.findall(topic_marker + '(\w*)', text)
@ -159,13 +159,13 @@ def make_flat_list(mixed_list, target_item_type=tuple):
:return: flat list of 'target_item_type'
"""
flat_list = []
for object in mixed_list:
if type(object) == list:
for item in object:
for obj in mixed_list:
if type(obj) == list:
for item in obj:
if type(item) == target_item_type:
flat_list.append(item)
elif type(object) == target_item_type:
flat_list.append(object)
elif type(obj) == target_item_type:
flat_list.append(obj)
return flat_list
@ -210,7 +210,7 @@ def process_by_ext(file_attrs):
return file_to_md(file_attrs, 'attachments')
def write_note_and_delete(note_attrs): # TODO Test
def write_note_and_delete(note_attrs):
"""
Create or append existing note files based on Note_attrs tuples data, then delete the source file
:param note_attrs: Note_attrs named tuple
@ -245,13 +245,22 @@ def write_note_and_delete(note_attrs): # TODO Test
if __name__ == '__main__':
script_path = os.path.dirname(sys.argv[0])
for file in os.listdir(script_path):
if file[-5:] == '.lock':
os.remove(script_path + os.sep + file)
lockfile_path = script_path + os.sep + str(int(time.time())) + '.lock'
open(lockfile_path, 'w').close()
arg_parser = argparse.ArgumentParser(description='A script to turn everything in the inbox directory to markdown notes.')
arg_parser.add_argument('-i', '--inbox', action='store', dest='inbox_dir', required=True,
help="Full absolute path to the inbox directory to organize")
help="Full absolute path to the inbox directory to organize")
arg_parser.add_argument('-f', '--folder', action='store', dest='folder_dir', required=True,
help="Full absolute path to directory where 'media' and 'attachment' directories are")
help="Full absolute path to directory where 'media' and 'attachment' directories are")
arg_parser.add_argument('-m', '--marker', action='store', dest='topic_marker', required=False, default='@',
help="Symbol(s) which start the 'topic' word (for text files)")
help="Symbol(s) which start the 'topic' word (for text files)")
arg_parser.add_argument('-s', '--scan-folder', action='store_true', dest='scan_folder', required=False,
help="Process whole folder rather than only inbox")
arg_parser.add_argument('-p', '--pandoc-bin', action='store', dest='pandoc_bin', required=False,
@ -275,8 +284,8 @@ if __name__ == '__main__':
scan_path = inbox_dir
file_list = []
for dir, subdirs, files in os.walk(scan_path):
for file_path in sorted([dir + os.sep + file for file in files], key=os.path.getmtime):
for root, subdirs, files in os.walk(scan_path):
for file_path in sorted([root + os.sep + file for file in files], key=os.path.getmtime):
file_attrs = process_by_path(file_path)
if file_attrs:
file_list.append([file_attrs])
@ -313,7 +322,8 @@ if __name__ == '__main__':
if file_attrs:
# Wait for all the web page resources saved/synced
if file_path.endswith(('.htm', '.html')): time.sleep(2)
if file_path.endswith(('.htm', '.html')):
time.sleep(2)
obj_to_write = process_by_ext(file_attrs)
else:
return
@ -332,14 +342,11 @@ if __name__ == '__main__':
try:
while True:
time.sleep(5)
if os.path.isfile(lockfile_path):
time.sleep(5)
else:
raise Exception
except:
observer.stop()
observer.join()
# if platform.system() == 'Linux':
# os.system('notify-send "-a" "Inbox script" "Your inbox is organized"') # TODO maybe change to gi.repository: Notify

View File

@ -5,7 +5,7 @@
"resources": ["inbox.py", "md_link.py", "md_convert.py", "safe_path.py"],
"authors": ["@Maboroshy"],
"platforms": ["linux", "macos", "windows"],
"version": "0.2.0",
"version": "0.2.1",
"minAppVersion": "17.05.8",
"description" : "Inbox is a complex script to organize data added from different devices and applications.\n\n<b>It's currently at beta stage, so using it for unrecoverable data is discouraged.</b>\n\nThe script processes files in inbox folder you set or whole note folder (depending on settings) as follows:\n- The script turns all .txt files and files with no extension to .md notes with modification time in the file name.\n- If there's a word that starts with a '@' (configurable) in .txt file, content of that file will be added to .md note named like that word. The content of .txt file with '@topic' in text will be added to 'topic.md' file. Outside of inbox folder the text won't be added to note but will become a new standalone note.\n- Every URL in text file will be converted to markdown link, with web page favicon and title if possible.\n- Any web page, saved as .htm file with '_files' folder, will be converted to .md note.\n- Any image file placed to inbox folder will be moved to media folder. The script will put .md note with the in-line image instead.\n- Any other file placed to inbox folder will be moved to attachments folder. The script will put .md note with a link to the file instead. On Linux the file icon will be put before link.\n- (Inbox folder only) Text/link of file placed to sub-folder of inbox folder will be added to .md note named like the sub-folder. Everything put to 'topic' sub-folder will be added to 'topic.md' file. Topic words won't work in these sub-folders.\n- (Linux only) The script will put thumbnail for .pdf file as a link to it.\n- (Linux only) The script will replace large in-line image with a smaller one as a link to original.\n\n <a href=\"https://github.com/qownnotes/scripts/blob/master/inbox/workflow.md\">Workflow examples</a>\n\n<b>Dependencies:</b>\n<a href=\"https://www.python.org/downloads/\">Python 3.3+ Interpreter</a>;\n(optional, continuous watch mode)<a href=\"http://pythonhosted.org/watchdog/installation.html\">Watchdog module</a>;\n(optional, web page to note)<a href=\"http://pandoc.org/installing.html\">Pandoc</a>;\n(optional, Linux only, pdf thumbnails) ghostscript;\n(optional, Linux only, image thumbnails) imagemagick;\n(optional, Linux only, file icons) python-gobject.\n\n<b>Usage:</b>\nRun the script by toolbar button or menu item. Or, if set to 'continuous watch' mode, it will run itself on load."
}