all repos — dotfiles @ 66318dd19348e042835aa3db8974f5d98a49d954

linux dotfiles

scripts/pd (view raw)

 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
#!/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'])