You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.3 KiB
Cheetah
87 lines
2.3 KiB
Cheetah
2 years ago
|
{{ if .include_legacy }}
|
||
6 years ago
|
#!/usr/bin/env python3
|
||
|
|
||
|
'''
|
||
12 years ago
|
Move a document to the correct documents folder
|
||
6 years ago
|
'''
|
||
12 years ago
|
|
||
|
import filecmp
|
||
|
import os
|
||
|
import re
|
||
11 years ago
|
import shutil
|
||
12 years ago
|
import sys
|
||
|
|
||
|
from datetime import date
|
||
5 years ago
|
from unicodedata import normalize
|
||
12 years ago
|
|
||
5 years ago
|
_punctuation_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
|
||
12 years ago
|
|
||
5 years ago
|
def slugify(text, delim='-'):
|
||
|
"""
|
||
|
Generate an ASCII-only slug.
|
||
|
"""
|
||
|
|
||
|
result = []
|
||
|
for word in _punctuation_re.split(text.lower()):
|
||
|
word = normalize('NFKD', word) \
|
||
|
.encode('ascii', 'ignore') \
|
||
|
.decode('utf-8')
|
||
|
|
||
|
if word:
|
||
|
result.append(word)
|
||
|
|
||
|
return delim.join(result)
|
||
12 years ago
|
|
||
|
def usage():
|
||
6 years ago
|
return 'Usage: mv-to-docs [SRC_DOC] [CATEGORY] [MESSAGE]'
|
||
12 years ago
|
|
||
6 years ago
|
def main(file_name='', dir_name='', message = ''):
|
||
|
file_type = file_name.split('.')[-1]
|
||
12 years ago
|
|
||
|
if not os.path.isfile(file_name):
|
||
6 years ago
|
print('ERROR: Cannot find file: {}'.format(file_name))
|
||
12 years ago
|
sys.exit(usage())
|
||
|
|
||
|
file_date = date.fromtimestamp(os.path.getmtime(file_name))
|
||
|
|
||
6 years ago
|
doc_path = os.path.expanduser('~/documents/records/{}'.format(dir_name))
|
||
12 years ago
|
if not os.path.exists(doc_path):
|
||
6 years ago
|
print('could not find path → creating directory: {}'.format(doc_path))
|
||
12 years ago
|
os.mkdir(doc_path)
|
||
|
|
||
5 years ago
|
year = file_date.strftime("%Y")
|
||
|
path_with_year = '{}/{}'.format(doc_path, year)
|
||
12 years ago
|
if not os.path.exists(path_with_year):
|
||
|
os.mkdir(path_with_year)
|
||
|
pass
|
||
|
|
||
5 years ago
|
new_file_path = '{}/{}_{}.{}'.format(path_with_year,
|
||
|
file_date,
|
||
6 years ago
|
message,
|
||
|
file_type)
|
||
12 years ago
|
|
||
|
if not os.path.isfile(new_file_path):
|
||
5 years ago
|
print("Moving file '{}' to '{}'".format(file_name, new_file_path))
|
||
11 years ago
|
shutil.copyfile(file_name, new_file_path)
|
||
|
os.unlink(file_name)
|
||
12 years ago
|
else:
|
||
|
if filecmp.cmp(new_file_path, file_name):
|
||
6 years ago
|
print('Two files already exist and are same: {} and {}'.format(file_name, new_file_path))
|
||
12 years ago
|
else:
|
||
6 years ago
|
print('destination file already exists {}'.format(new_file_path))
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
try:
|
||
|
file_name = sys.argv[1]
|
||
|
dir_name = slugify(sys.argv[2])
|
||
|
except IndexError:
|
||
|
sys.exit(usage())
|
||
|
|
||
|
try:
|
||
|
message = slugify(sys.argv[3])
|
||
|
except IndexError:
|
||
|
message = ""
|
||
12 years ago
|
|
||
6 years ago
|
main(file_name=file_name, dir_name=dir_name, message=message)
|
||
2 years ago
|
{{- end }}
|