2017-08-14 15:37:23 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
2017-09-14 18:08:58 +00:00
|
|
|
import sys
|
2017-08-14 15:37:23 +00:00
|
|
|
import time
|
2017-09-03 16:29:59 +00:00
|
|
|
import argparse
|
2017-08-14 15:37:23 +00:00
|
|
|
import collections
|
|
|
|
import multiprocessing.dummy
|
|
|
|
|
|
|
|
import md_link
|
2017-09-03 16:29:59 +00:00
|
|
|
import md_convert
|
2017-08-14 15:37:23 +00:00
|
|
|
import safe_path
|
|
|
|
|
2017-09-13 18:10:38 +00:00
|
|
|
try:
|
|
|
|
import watchdog.events
|
|
|
|
import watchdog.observers
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
File_attrs = collections.namedtuple('File_attrs', 'file_path folder_dir_path output_dir_path output_file')
|
|
|
|
"""
|
|
|
|
A named tuple which functions use to pass input data - data of files to be processed
|
|
|
|
:param file_path: full absolute path to the file to process
|
|
|
|
:param folder_dir_path: full absolute path to directory where 'media' and 'attachment' directories are
|
|
|
|
:param output_dir_path: full absolute path to directory where resulting text file will be stored
|
|
|
|
:param output_file: empty for new standalone text file with mtime in the name,
|
|
|
|
'*no mtime*' for or new standalone text file without mtime in the name
|
|
|
|
or full absolute path to the text file which will be appended with a new entry
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
Note_attrs = collections.namedtuple('Note_attrs', 'input_file_path output_file_path text mtime title')
|
|
|
|
'''A named tuple which functions use to pass output data - data of notes to be written.
|
|
|
|
:param input_file_path: full absolute path to the file which was processed to this tuple
|
|
|
|
:param output_file_path: full absolute path to the output text file which should be written
|
|
|
|
:param text: content of the text file which should be written
|
|
|
|
:param mtime: modification time of input file as markdown headline to optionally prepend a text
|
|
|
|
:param title: title of a input file as markdown headline to optionally prepend a text'''
|
|
|
|
|
|
|
|
|
|
|
|
def text_to_md(file_attrs, topic_marker):
|
2017-08-14 15:37:23 +00:00
|
|
|
"""
|
2017-09-03 16:29:59 +00:00
|
|
|
This will process specified text file getting its topics and replacing urls with favicons and titles where possible
|
2017-08-14 15:37:23 +00:00
|
|
|
:param file_attrs: File_attrs named tuple
|
2017-09-13 18:10:38 +00:00
|
|
|
:param topic_marker: symbol(s) which start the 'topic' word, if such word present in text, it will go to 'topic.md'
|
2017-08-14 15:37:23 +00:00
|
|
|
:return: list of Note_attrs named tuple
|
|
|
|
"""
|
|
|
|
filename = os.path.splitext(os.path.basename(file_attrs.file_path))[0]
|
|
|
|
mtime = time.localtime(os.path.getmtime(file_attrs.file_path))
|
|
|
|
|
2017-09-08 20:34:13 +00:00
|
|
|
try:
|
|
|
|
with open(file_attrs.file_path, 'r') as text_file:
|
|
|
|
text = text_file.read()
|
2017-09-14 18:08:58 +00:00
|
|
|
except UnicodeDecodeError:
|
2017-09-08 20:34:13 +00:00
|
|
|
return
|
2017-08-14 15:37:23 +00:00
|
|
|
|
2017-09-13 18:10:38 +00:00
|
|
|
topics = re.findall(topic_marker + '(\w*)', text)
|
|
|
|
text = re.sub(topic_marker + '\w*[ ]?', '', text).strip()
|
2017-08-14 15:37:23 +00:00
|
|
|
|
|
|
|
if re.match('^http[s]?://[^\s]*$', text):
|
|
|
|
is_bookmark = True
|
|
|
|
else:
|
|
|
|
is_bookmark = False
|
|
|
|
|
|
|
|
for link in re.findall('(^|\s)(http[s]?://.*)(\s|$)', text, re.MULTILINE | re.IGNORECASE):
|
|
|
|
url = md_link.URL(link[1], file_attrs.folder_dir_path)
|
|
|
|
text = text.replace(link[1], url.md)
|
|
|
|
if is_bookmark:
|
|
|
|
bookmark_title = url.title
|
|
|
|
|
2017-09-03 16:29:59 +00:00
|
|
|
if file_attrs.output_file and file_attrs.output_file != '*no mtime*':
|
|
|
|
output_files = [file_attrs.output_file]
|
2017-08-14 15:37:23 +00:00
|
|
|
headline_title = ''
|
2017-09-03 16:29:59 +00:00
|
|
|
elif topics:
|
|
|
|
output_files = [topic + '.md' for topic in topics]
|
2017-08-14 15:37:23 +00:00
|
|
|
headline_title = ''
|
|
|
|
elif is_bookmark:
|
|
|
|
headline_title = '# {}\n'.format(bookmark_title)
|
2017-09-03 16:29:59 +00:00
|
|
|
if file_attrs.output_file == '*no mtime*':
|
|
|
|
output_files = [bookmark_title + '.md']
|
|
|
|
else:
|
|
|
|
output_files = [time.strftime('%m-%d %H:%M', mtime) + ' ' + bookmark_title + '.md']
|
2017-08-14 15:37:23 +00:00
|
|
|
else:
|
|
|
|
headline_title = '# {}\n'.format(filename)
|
2017-09-03 16:29:59 +00:00
|
|
|
if file_attrs.output_file == '*no mtime*':
|
|
|
|
output_files = [filename + '.md']
|
|
|
|
else:
|
|
|
|
output_files = [time.strftime('%m-%d %H:%M', mtime) + ' ' + filename + '.md']
|
2017-08-14 15:37:23 +00:00
|
|
|
|
|
|
|
output = []
|
|
|
|
for output_file in output_files:
|
|
|
|
output.append(Note_attrs(input_file_path=file_attrs.file_path,
|
|
|
|
output_file_path=file_attrs.output_dir_path + os.sep + safe_path.filename(output_file),
|
|
|
|
text=text,
|
|
|
|
mtime='**{}** \n'.format(time.strftime('%x %a %X', mtime)),
|
|
|
|
title=headline_title))
|
|
|
|
return output
|
|
|
|
|
2017-09-13 18:10:38 +00:00
|
|
|
|
2017-09-03 16:29:59 +00:00
|
|
|
def html_to_md(file_attrs, pandoc_bin='pandoc', pandoc_ver=''):
|
|
|
|
"""
|
|
|
|
This will move specified convert specified html file to markdown and move all in-line images to sub-folder at media directory
|
|
|
|
:param file_attrs: File_attrs named tuple
|
|
|
|
:return: Note_attrs named tuple
|
|
|
|
"""
|
|
|
|
html_file_name_noext = os.path.splitext(os.path.basename(file_attrs.file_path))[0]
|
|
|
|
mtime = time.localtime(os.path.getmtime(file_attrs.file_path))
|
|
|
|
md_text = md_convert.saved_html(file_attrs.file_path, file_attrs.folder_dir_path,
|
|
|
|
pandoc_bin=pandoc_bin, pandoc_ver=pandoc_ver)
|
|
|
|
if not md_text:
|
|
|
|
return
|
|
|
|
|
|
|
|
return Note_attrs(input_file_path=file_attrs.file_path,
|
|
|
|
output_file_path=file_attrs.output_dir_path + os.sep + safe_path.filename(html_file_name_noext + '.md'),
|
|
|
|
text=md_text,
|
|
|
|
mtime='**{}** \n'.format(time.strftime('%x %a %X', mtime)),
|
|
|
|
title='')
|
2017-08-14 15:37:23 +00:00
|
|
|
|
2017-09-13 18:10:38 +00:00
|
|
|
|
2017-08-14 15:37:23 +00:00
|
|
|
def file_to_md(file_attrs, media_dir_name):
|
|
|
|
"""
|
|
|
|
This will move specified file to media_dir_name and put note with a reference to that file instead
|
|
|
|
:param file_attrs: File_attrs named tuple
|
|
|
|
:param media_dir_name: name of sub-directory in folder_dir_path where file will be moved (for non-text files)
|
|
|
|
:return: Note_attrs named tuple
|
|
|
|
"""
|
|
|
|
mtime = time.localtime(os.path.getmtime(file_attrs.file_path))
|
|
|
|
new_filename = str(time.mktime(mtime))[:-2] + '_' + os.path.basename(file_attrs.file_path)
|
|
|
|
new_path = os.path.join(file_attrs.folder_dir_path, media_dir_name, new_filename)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.rename(file_attrs.file_path, new_path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
file = md_link.File(new_path, file_attrs.folder_dir_path, os.path.splitext(os.path.basename(file_attrs.file_path))[0])
|
|
|
|
|
2017-09-03 16:29:59 +00:00
|
|
|
if file_attrs.output_file == '*no mtime*':
|
|
|
|
output_file = file.title + '.md'
|
|
|
|
elif file_attrs.output_file:
|
|
|
|
output_file = file_attrs.output_file
|
2017-08-14 15:37:23 +00:00
|
|
|
else:
|
|
|
|
output_file = time.strftime('%m-%d %H:%M', mtime) + ' ' + file.title + '.md'
|
|
|
|
|
|
|
|
return Note_attrs(input_file_path=file_attrs.file_path,
|
|
|
|
output_file_path=file_attrs.output_dir_path + os.sep + safe_path.filename(output_file),
|
|
|
|
text=file.md,
|
|
|
|
mtime='**{}** \n'.format(time.strftime('%x %a %X', mtime)),
|
|
|
|
title='# {}\n'.format(file.title))
|
|
|
|
|
|
|
|
|
2017-09-13 18:10:38 +00:00
|
|
|
def make_flat_list(mixed_list, target_item_type=tuple):
|
|
|
|
"""
|
|
|
|
Make a list that has lists and 'target_item_type' as items flat, not recursive.
|
|
|
|
:param mixed_list: list to make flat
|
|
|
|
:param target_item_type: type of items in the flat list
|
|
|
|
:return: flat list of 'target_item_type'
|
|
|
|
"""
|
|
|
|
flat_list = []
|
2017-09-14 18:08:58 +00:00
|
|
|
for obj in mixed_list:
|
|
|
|
if type(obj) == list:
|
|
|
|
for item in obj:
|
2017-09-13 18:10:38 +00:00
|
|
|
if type(item) == target_item_type:
|
|
|
|
flat_list.append(item)
|
2017-09-14 18:08:58 +00:00
|
|
|
elif type(obj) == target_item_type:
|
|
|
|
flat_list.append(obj)
|
2017-09-13 18:10:38 +00:00
|
|
|
return flat_list
|
|
|
|
|
2017-08-14 15:37:23 +00:00
|
|
|
|
2017-09-13 18:10:38 +00:00
|
|
|
def process_by_path(file_path):
|
2017-08-14 15:37:23 +00:00
|
|
|
"""
|
2017-09-13 18:10:38 +00:00
|
|
|
Checks if the file is valid for processing and returns File_attrs tuple depending on its path
|
|
|
|
:param file_path: Absolute file path
|
|
|
|
:return: File_attrs named tuple
|
2017-08-14 15:37:23 +00:00
|
|
|
"""
|
2017-09-13 18:10:38 +00:00
|
|
|
if file_path.endswith(('.md', 'notes.sqlite')) \
|
|
|
|
or file_path.startswith((folder_dir + os.sep + 'media', folder_dir + os.sep + 'attachments')) \
|
|
|
|
or os.sep + '.' in file_path[len(folder_dir):] \
|
|
|
|
or '_files' + os.sep in file_path[len(folder_dir):]:
|
|
|
|
return
|
|
|
|
|
|
|
|
if file_path[:len(inbox_dir)] == inbox_dir:
|
|
|
|
if os.path.dirname(file_path) == inbox_dir:
|
|
|
|
return File_attrs(file_path=file_path, folder_dir_path=folder_dir,
|
|
|
|
output_dir_path=inbox_dir, output_file='')
|
|
|
|
else:
|
|
|
|
return File_attrs(file_path=file_path, folder_dir_path=folder_dir,
|
|
|
|
output_dir_path=inbox_dir,
|
|
|
|
output_file=os.path.dirname(file_path)[len(inbox_dir)+1:].replace(os.sep, ' - ') + '.md')
|
|
|
|
else:
|
|
|
|
return File_attrs(file_path=file_path, folder_dir_path=folder_dir,
|
|
|
|
output_dir_path=os.path.dirname(file_path), output_file='*no mtime*')
|
2017-08-14 15:37:23 +00:00
|
|
|
|
2017-09-13 18:10:38 +00:00
|
|
|
|
|
|
|
def process_by_ext(file_attrs):
|
|
|
|
"""
|
|
|
|
This will run different functions to process specified File_attrs tuple based on file extension
|
|
|
|
:param file_attrs: File_attrs named tuple
|
|
|
|
:return: Note_attrs named tuple
|
|
|
|
"""
|
|
|
|
if file_attrs.file_path.endswith('.txt') or not os.path.splitext(file_attrs.file_path)[1]:
|
|
|
|
return text_to_md(file_attrs, args.topic_marker)
|
|
|
|
elif args.pandoc_bin and args.pandoc_ver and file_attrs.file_path.endswith(('.htm', '.html')):
|
|
|
|
return html_to_md(file_attrs, args.pandoc_bin, args.pandoc_ver)
|
|
|
|
elif file_attrs.file_path.endswith(('.jpg', '.png', '.gif')):
|
|
|
|
return file_to_md(file_attrs, 'media')
|
|
|
|
else:
|
|
|
|
return file_to_md(file_attrs, 'attachments')
|
|
|
|
|
|
|
|
|
2017-09-14 18:08:58 +00:00
|
|
|
def write_note_and_delete(note_attrs):
|
2017-09-13 18:10:38 +00:00
|
|
|
"""
|
|
|
|
Create or append existing note files based on Note_attrs tuples data, then delete the source file
|
|
|
|
:param note_attrs: Note_attrs named tuple
|
|
|
|
"""
|
|
|
|
if os.path.isfile(note_attrs.output_file_path):
|
|
|
|
if os.path.dirname(note_attrs.output_file_path) == inbox_dir:
|
|
|
|
note_file_path = note_attrs.output_file_path
|
|
|
|
with open(note_file_path, 'r') as source:
|
|
|
|
content = note_attrs.mtime + note_attrs.text + '\n\n' + source.read()
|
|
|
|
else:
|
|
|
|
i = 1
|
|
|
|
while os.path.isfile(os.path.splitext(note_attrs.output_file_path)[0] + '_' + str(i) + '.md'):
|
|
|
|
i += 1
|
|
|
|
note_file_path = os.path.splitext(note_attrs.output_file_path)[0] + '_' + str(i) + '.md'
|
|
|
|
content = note_attrs.mtime + note_attrs.text
|
|
|
|
else:
|
|
|
|
note_file_path = note_attrs.output_file_path
|
|
|
|
if note_attrs.title:
|
|
|
|
content = note_attrs.title + note_attrs.text
|
2017-08-14 15:37:23 +00:00
|
|
|
else:
|
2017-09-13 18:10:38 +00:00
|
|
|
content = note_attrs.mtime + note_attrs.text
|
|
|
|
|
|
|
|
with open(note_file_path, 'w') as output:
|
|
|
|
output.write(content)
|
2017-08-14 15:37:23 +00:00
|
|
|
|
2017-09-13 18:10:38 +00:00
|
|
|
if os.path.isfile(note_file_path):
|
|
|
|
try:
|
|
|
|
os.remove(note_attrs.input_file_path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-08-14 15:37:23 +00:00
|
|
|
|
2017-09-14 18:08:58 +00:00
|
|
|
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)
|
|
|
|
|
2017-09-03 16:29:59 +00:00
|
|
|
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,
|
2017-09-14 18:08:58 +00:00
|
|
|
help="Full absolute path to the inbox directory to organize")
|
2017-09-03 16:29:59 +00:00
|
|
|
arg_parser.add_argument('-f', '--folder', action='store', dest='folder_dir', required=True,
|
2017-09-14 18:08:58 +00:00
|
|
|
help="Full absolute path to directory where 'media' and 'attachment' directories are")
|
2017-09-03 16:29:59 +00:00
|
|
|
arg_parser.add_argument('-m', '--marker', action='store', dest='topic_marker', required=False, default='@',
|
2017-09-14 18:08:58 +00:00
|
|
|
help="Symbol(s) which start the 'topic' word (for text files)")
|
2017-09-03 16:29:59 +00:00
|
|
|
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,
|
|
|
|
help="Command/path to run pandoc")
|
|
|
|
arg_parser.add_argument('-pv', '--pandoc-ver', action='store', dest='pandoc_ver', required=False,
|
|
|
|
help="Installed pandoc version")
|
2017-09-13 18:10:38 +00:00
|
|
|
arg_parser.add_argument('-w', '--watch', action='store_true', dest='watch_fs', required=False,
|
|
|
|
help="Watch and process new files as they appear after initial scan")
|
2017-09-03 16:29:59 +00:00
|
|
|
args = arg_parser.parse_args()
|
|
|
|
|
|
|
|
inbox_dir = args.inbox_dir
|
|
|
|
folder_dir = args.folder_dir
|
2017-08-14 15:37:23 +00:00
|
|
|
|
|
|
|
os.makedirs(inbox_dir, exist_ok=True)
|
|
|
|
os.makedirs(folder_dir + os.sep + 'media', exist_ok=True)
|
|
|
|
os.makedirs(folder_dir + os.sep + 'attachments', exist_ok=True)
|
|
|
|
|
2017-09-03 16:29:59 +00:00
|
|
|
if args.scan_folder:
|
2017-09-13 18:10:38 +00:00
|
|
|
scan_path = folder_dir
|
|
|
|
else:
|
|
|
|
scan_path = inbox_dir
|
|
|
|
|
|
|
|
file_list = []
|
2017-09-14 18:08:58 +00:00
|
|
|
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):
|
2017-09-13 18:10:38 +00:00
|
|
|
file_attrs = process_by_path(file_path)
|
|
|
|
if file_attrs:
|
|
|
|
file_list.append([file_attrs])
|
2017-08-14 15:37:23 +00:00
|
|
|
|
2017-09-03 16:29:59 +00:00
|
|
|
write_list = multiprocessing.dummy.Pool(100).starmap(process_by_ext, file_list)
|
2017-08-14 15:37:23 +00:00
|
|
|
|
2017-09-13 18:10:38 +00:00
|
|
|
flat_write_list = make_flat_list(write_list, Note_attrs)
|
2017-08-14 15:37:23 +00:00
|
|
|
|
|
|
|
for note_attrs in flat_write_list:
|
2017-09-13 18:10:38 +00:00
|
|
|
write_note_and_delete(note_attrs)
|
|
|
|
|
|
|
|
if args.watch_fs:
|
|
|
|
|
2017-08-14 15:37:23 +00:00
|
|
|
try:
|
2017-09-13 18:10:38 +00:00
|
|
|
import watchdog.events
|
|
|
|
import watchdog.observers
|
|
|
|
except ImportError:
|
|
|
|
print("Can't find Watchdog module. Watching for changes won't work.")
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
class FsEventHandler(watchdog.events.FileSystemEventHandler):
|
|
|
|
def on_any_event(self, event):
|
|
|
|
if event.is_directory:
|
|
|
|
return
|
|
|
|
elif event.event_type == 'created':
|
|
|
|
file_path = event.src_path
|
|
|
|
elif event.event_type == 'moved':
|
|
|
|
file_path = event.dest_path
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
file_attrs = process_by_path(file_path)
|
|
|
|
|
|
|
|
if file_attrs:
|
|
|
|
# Wait for all the web page resources saved/synced
|
2017-09-14 18:08:58 +00:00
|
|
|
if file_path.endswith(('.htm', '.html')):
|
|
|
|
time.sleep(2)
|
2017-09-13 18:10:38 +00:00
|
|
|
obj_to_write = process_by_ext(file_attrs)
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
if type(obj_to_write) == list:
|
|
|
|
for note_attrs in obj_to_write:
|
|
|
|
write_note_and_delete(note_attrs)
|
|
|
|
else:
|
|
|
|
write_note_and_delete(obj_to_write)
|
|
|
|
|
|
|
|
|
2017-09-14 19:25:44 +00:00
|
|
|
lockfile_path = script_path + os.sep + str(int(time.time())) + '.lock'
|
|
|
|
open(lockfile_path, 'w').close()
|
|
|
|
|
2017-09-13 18:10:38 +00:00
|
|
|
event_handler = FsEventHandler()
|
|
|
|
observer = watchdog.observers.Observer()
|
|
|
|
observer.schedule(event_handler, scan_path, recursive=True)
|
|
|
|
observer.start()
|
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
2017-09-14 18:08:58 +00:00
|
|
|
if os.path.isfile(lockfile_path):
|
|
|
|
time.sleep(5)
|
|
|
|
else:
|
|
|
|
raise Exception
|
2017-09-13 18:10:38 +00:00
|
|
|
except:
|
|
|
|
observer.stop()
|
|
|
|
|
|
|
|
observer.join()
|