all repos — dotfiles @ f37fb4bc777bba886e3b75267a0a83bc645d1b98

linux dotfiles

scripts/pd

#!/usr/bin/env python3
# This script opens a temp file in a vim buffer and after wrting and quiting from vim, it writes the contents to the file
# specified by the file_path with a date and timestamp.
# I use this for my journal.

import os
import datetime
import sys
import tempfile
import subprocess, shlex


file_path = r'/home/prithu/Dropbox/Tasker/pd.txt'

# This script keeps checking for the dropbox status every 10s and
# runs 'dropbox stop' and exits once dropbox is done with its
# syncing. this shell script should be in the PATH.
exitdrop=(
"""
#!/bin/bash
stat=$(dropbox status)
if [ "$stat" = "Dropbox isn't running!" ]
then
  echo $stat
  exit 0
fi
while :
do
  if [ "$stat" = "Up to date" ]
  then
    dropbox stop
    exit 0
  fi
  stat=$(dropbox status)
done
""")


entry = None
with tempfile.NamedTemporaryFile(suffix='.pdtemp') as temp:
    command = "vim {}".format(temp.name)
    subprocess.run(shlex.split(command))
    entry = open(temp.name, 'r').read()
    if entry == '':
        print("Nothing was entered...")
        exit()
        
with open(file_path, 'a', encoding='utf8') as fp:
    dt = datetime.datetime.now()
    date_string = dt.strftime('%a, %d %b %Y')
    time_string = dt.strftime('%I:%M %p')
    date_and_time = '[' + date_string + ' | ' + time_string + ']'

    fp.write('\n\n\n===============================\n' + date_and_time + '\n')
    fp.write(entry)

subprocess.run(['dropbox', 'start'])

with open('/tmp/exitdrop.sh', 'w') as tmp:
    tmp.write(exitdrop)
    tmp.close()
    subprocess.run(['chmod', '+x', '/tmp/exitdrop.sh'])
    subprocess.run(['sh', '/tmp/exitdrop.sh'])