feedwarrior

Slim, portable tooling for creating and distributing decentralized append logs
git clone git://git.defalsify.org/logwarrior.git
Info | Log | Files | Refs | README | LICENSE

add.py (2901B)


      1 # standard imports
      2 import sys
      3 import os
      4 from email.message import EmailMessage
      5 from email.mime.multipart import MIMEMultipart
      6 import email
      7 import logging
      8 import uuid
      9 import json
     10 import gzip
     11 import tempfile
     12 import base64
     13 import uuid
     14 
     15 # local imports
     16 import feedwarrior
     17 from feedwarrior import entry as feedentry
     18 from feedwarrior.adapters import fileadapter
     19 from feedwarrior.entry import extension
     20 from feedwarrior.common import task_ids_to_uuids, check_task_uuids
     21 
     22 logg = logging.getLogger()
     23 
     24 
     25 def get_editor():
     26     return 'vim'
     27 
     28 def parse_args(argparser):
     29     argparser.add_argument('-z', action='store_true', help='compress entry with gzip')
     30     argparser.add_argument('--task-id', dest='task_id', type=int, action='append', help='add taskwarrior task id relations translated to uuis (cannot be used with --task-uuid')
     31     argparser.add_argument('--task-uuid', dest='task_uuid', type=str, action='append', help='add taskwarrior task uuid relations (cannot be used with --task-id')
     32     argparser.add_argument('-s', type=str, help='entry subject')
     33     return True
     34 
     35 
     36 def check_args(args):
     37     pass
     38 
     39 
     40 # TODO: move logic to package to get symmetry with the show.py logic
     41 def execute(config, feed, args):
     42     task_uuids = []
     43     if args.task_id != None:
     44         task_uuids += task_ids_to_uuids(config.task_dir, args.task_id)
     45 
     46     if args.task_uuid != None:
     47         task_uuids += check_task_uuids(config.task_dir, args.task_uuid)
     48     
     49     d = tempfile.TemporaryDirectory()
     50     t = tempfile.NamedTemporaryFile(mode='w+', dir=d.name)
     51     editor_path = get_editor()
     52     os.system('{} {}'.format(get_editor(), t.name))
     53     f = open(t.name, 'rb')
     54     s = os.stat(t.name)
     55     t.close()
     56     logg.debug('file {} {}'.format(t.name, s.st_size))
     57     if s.st_size == 0:
     58         logg.error('empty input')
     59         sys.stderr.write('No input. aborting.\n')
     60         sys.exit(1)
     61     content = f.read(s.st_size)
     62     f.close()
     63 
     64     entry_date = str(email.utils.formatdate())
     65     subject = args.s
     66     if subject == None:
     67         subject = entry_date
     68     m = EmailMessage()
     69     m.add_header('Content-Type', 'text/plain')
     70     m.add_header('Content-Disposition', 'inline')
     71     m.add_header('Content-Transfer-Encoding', 'base64')
     72     m.set_param('filename', subject)
     73     m.set_param('filename', subject, 'Content-Disposition')
     74     bsf = base64.encodebytes(content)
     75     m.set_payload(bsf.decode('utf-8'))
     76 
     77     
     78     mm = MIMEMultipart()
     79     mm.attach(m)
     80     mm.add_header('Subject', subject)
     81     mm.add_header('Date', entry_date)
     82 
     83     entry = feedentry.from_multipart(mm)
     84     for t in task_uuids:
     85         uu = feedwarrior.common.parse_uuid(t)
     86         entry.add_extension(feedwarrior.extension.TASKWARRIOR, uu)
     87 
     88     uu = str(entry.uuid)
     89     logg.debug('adding entry {}'.format(uu))
     90     
     91     fa = fileadapter(config.data_dir, feed.uuid)
     92     fa.put(entry.uuid, entry, compress=args.z)
     93    
     94     feed.add(entry)
     95     return str(entry.uuid)