Add task capture, work reports, daily digests and private contacts
Build and publish container images / images (push) Successful in 16m37s
Build and publish container images / images (push) Successful in 16m37s
This commit is contained in:
1 parent
50b8b0323f
commit
b4792bb175
50 files changed
+2678
-17
No files matched your search
+193
-5
@@ -1,6 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
"""pages — navigate and edit PagesWiki from your terminal (pages-cli)."""
|
||||
import argparse
|
||||
import email.policy
|
||||
import email.parser
|
||||
import uuid
|
||||
import base64
|
||||
import contextlib
|
||||
import difflib
|
||||
@@ -75,16 +78,18 @@ class Client:
|
||||
self.base, self.token, self.lang = origin(base), token, lang
|
||||
self.opener = http.build_opener(NoRedirect())
|
||||
|
||||
def request(self, route, method='GET', data=None, form=None, limit=4 << 20):
|
||||
def request(self, route, method='GET', data=None, form=None, limit=4 << 20, etag=None, content_type=None):
|
||||
if not route.startswith('/') or route.startswith('//'):
|
||||
raise Failure('Invalid API route.')
|
||||
headers = {'Accept': 'application/json', 'User-Agent': 'pages-cli/0.1'}
|
||||
if etag is not None:
|
||||
headers['If-Match'] = etag
|
||||
if self.token:
|
||||
headers['Authorization'] = 'Bearer ' + self.token
|
||||
body = None
|
||||
if data is not None:
|
||||
body = json.dumps(data, ensure_ascii=False).encode()
|
||||
headers['Content-Type'] = 'application/json'
|
||||
body = data if isinstance(data, bytes) else json.dumps(data, ensure_ascii=False).encode()
|
||||
headers['Content-Type'] = content_type or 'application/json'
|
||||
if form is not None:
|
||||
body = url.urlencode(form).encode()
|
||||
headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
||||
@@ -107,7 +112,8 @@ class Client:
|
||||
if not isinstance(payload, dict):
|
||||
payload = {}
|
||||
messages = {303: 'Login redirect refused; log in again.', 401: 'Session expired or authentication required.', 403: 'Access denied.',
|
||||
404: 'Not found or not readable.', 409: 'Conflict: remote revision changed.'}
|
||||
404: 'Not found or not readable.', 409: 'Conflict: remote revision changed.',
|
||||
412: 'Conflict: remote item changed; reload before saving.', 422: 'Invalid fields; check dates, project and required settings.'}
|
||||
raise Failure(messages.get(e.code, 'HTTP request failed (%d); redirects are not followed.' % e.code), e.code, payload) from None
|
||||
except (urllib.error.URLError, TimeoutError, OSError):
|
||||
raise Failure('Connection/TLS failure; no automatic write retry was attempted.') from None
|
||||
@@ -440,7 +446,183 @@ def editor(client, addr, mode):
|
||||
print('Local edit retained:', safe_text(file), file=sys.stderr)
|
||||
|
||||
|
||||
TASK_API = '/_/api/v1/extension/organizer.tasks/'
|
||||
|
||||
|
||||
def task_uuid(value):
|
||||
try:
|
||||
parsed = str(uuid.UUID(value))
|
||||
if parsed != value: raise ValueError()
|
||||
return parsed
|
||||
except (ValueError, AttributeError):
|
||||
raise Failure('Use a canonical task or project UUID.') from None
|
||||
|
||||
|
||||
def task_project(client, value):
|
||||
if value == '': return ''
|
||||
try: return task_uuid(value)
|
||||
except Failure: pass
|
||||
data = client.json(TASK_API+'items?'+url.urlencode({'kind':'project','view':'all','q':value,'limit':100}))
|
||||
matches = [item['uuid'] for item in data.get('items',[]) if item['title'] == value]
|
||||
if len(matches) != 1 or data.get('next'):
|
||||
raise Failure('Project name is missing or ambiguous; use its UUID.')
|
||||
return matches[0]
|
||||
|
||||
|
||||
def task_mail(raw, include_body=False, override_title=None):
|
||||
if len(raw) > 2 << 20: raise Failure('Mail input exceeds 2 MiB; pipe one message at a time.')
|
||||
message = email.parser.BytesParser(policy=email.policy.default).parsebytes(raw)
|
||||
clean = lambda value: ' '.join(safe_text(str(value or '')).split())
|
||||
title = override_title or clean(message.get('Subject')) or '(No subject)'
|
||||
if len(title.encode()) > 300: raise Failure('Mail subject exceeds 300 UTF-8 bytes; use --title.')
|
||||
notes = '\n'.join(key+': '+clean(message.get(key)) for key in ('From','Date','Message-ID') if message.get(key))
|
||||
if include_body:
|
||||
part = message.get_body(preferencelist=('plain',))
|
||||
if part:
|
||||
content = part.get_content()
|
||||
if not isinstance(content,str): raise Failure('Mail body is not readable text.')
|
||||
notes += '\n\n' + content
|
||||
if len(notes.encode()) > 16384: raise Failure('Mail notes exceed 16 KiB; omit --include-body.')
|
||||
return title, notes, clean(message.get('Message-ID')) or hashlib.sha256(raw).hexdigest()
|
||||
|
||||
|
||||
def tasks(client, profile, words):
|
||||
parser = argparse.ArgumentParser(prog='pages task',description='Private task capture, planning and work-time records. Stops do not complete a task.')
|
||||
sub = parser.add_subparsers(dest='action',required=True)
|
||||
p=sub.add_parser('list');p.add_argument('--view',default='all',choices=['all','today','inbox','backlog','someday','review','done','archived','carryover']);p.add_argument('--project');p.add_argument('--q',default='');p.add_argument('--before',default='')
|
||||
sub.add_parser('projects')
|
||||
p=sub.add_parser('project-add');p.add_argument('title');p.add_argument('--notes',default='')
|
||||
p=sub.add_parser('show');p.add_argument('uuid',type=task_uuid)
|
||||
for name in ('add','from-mail','update'):
|
||||
p=sub.add_parser(name)
|
||||
if name=='add':p.add_argument('title')
|
||||
elif name=='update':p.add_argument('uuid',type=task_uuid);p.add_argument('--title')
|
||||
else:p.add_argument('--title');p.add_argument('--include-body',action='store_true')
|
||||
p.add_argument('--notes');p.add_argument('--project');p.add_argument('--assignee')
|
||||
p.add_argument('--due');p.add_argument('--start-on');p.add_argument('--planned');p.add_argument('--review')
|
||||
p.add_argument('--estimate',type=int);p.add_argument('--complexity',type=int,choices=range(1,6))
|
||||
if name=='add':p.add_argument('--request-id',type=task_uuid)
|
||||
for name in ('done','reopen','archive','start'):
|
||||
p=sub.add_parser(name);p.add_argument('uuid',type=task_uuid)
|
||||
sub.add_parser('stop');sub.add_parser('status')
|
||||
p=sub.add_parser('retry');p.add_argument('request_id',type=task_uuid)
|
||||
p=sub.add_parser('report');p.add_argument('--from',dest='start');p.add_argument('--to',dest='end')
|
||||
p=sub.add_parser('preferences');p.add_argument('--project');p.add_argument('--assignee');p.add_argument('--due-days',type=int);p.add_argument('--start-days',type=int);p.add_argument('--planned-days',type=int);p.add_argument('--review-days',type=int);p.add_argument('--estimate',type=int);p.add_argument('--complexity',type=int);p.add_argument('--digest',choices=['on','off']);p.add_argument('--at');p.add_argument('--timezone')
|
||||
for child in sub.choices.values():child.add_argument('--json',action='store_true')
|
||||
a=parser.parse_args(words);action=a.action
|
||||
def emit(data): print(json.dumps(data,ensure_ascii=False,indent=2))
|
||||
def capture(payload):
|
||||
pending=profile.data.setdefault('task_outbox',{})
|
||||
if len(pending)>=20 and payload['request_id'] not in pending:raise Failure('20 pending captures retained. Retry them before adding more.')
|
||||
pending[payload['request_id']]=payload;profile.save()
|
||||
try: result=client.json(TASK_API+'capture',method='POST',data=payload)
|
||||
except Failure:
|
||||
print('Draft retained; retry with: pages task retry '+payload['request_id'],file=sys.stderr);raise
|
||||
del pending[payload['request_id']];profile.save();return result
|
||||
if action in ('list','projects'):
|
||||
query={'kind':'project' if action=='projects' else 'task','view':getattr(a,'view','all'),'limit':100,'q':getattr(a,'q',''),'before':getattr(a,'before','')}
|
||||
if getattr(a,'project',None) is not None:query['project']=task_project(client,a.project)
|
||||
data=client.json(TASK_API+'items?'+url.urlencode(query))
|
||||
if a.json:emit(data)
|
||||
else:
|
||||
for item in data.get('items',[]):print(safe_text('\t'.join([item['uuid'],item['status'],item.get('due_on',''),item['title']])))
|
||||
if data.get('next'):print('Next page: --before '+data['next'],file=sys.stderr)
|
||||
return
|
||||
if action=='show':emit(client.json(TASK_API+'items/'+a.uuid));return
|
||||
if action=='preferences':
|
||||
data=client.json(TASK_API+'preferences');p=data['preferences'];changed=False
|
||||
for attr,key in [('project','project_uuid'),('assignee','assignee_uuid'),('due_days','due_days'),('start_days','start_days'),('planned_days','planned_days'),('review_days','review_days'),('estimate','estimate_minutes'),('complexity','complexity'),('at','digest_time'),('timezone','timezone')]:
|
||||
value=getattr(a,attr)
|
||||
if value is not None:p[key]=task_project(client,value) if attr=='project' else value;changed=True
|
||||
if a.digest:p['digest_enabled']=a.digest=='on';changed=True
|
||||
emit(client.json(TASK_API+'preferences',method='PUT',data=p) if changed else data);return
|
||||
if action=='retry':
|
||||
payload=profile.data.get('task_outbox',{}).get(a.request_id)
|
||||
if not payload:raise Failure('No retained task capture with this request ID.')
|
||||
emit(capture(payload));return
|
||||
if action in ('add','from-mail'):
|
||||
payload={'request_id':getattr(a,'request_id',None) or str(uuid.uuid4()),'title':a.title,'notes':a.notes or ''}
|
||||
if action=='from-mail':
|
||||
title,notes,message_id=task_mail(sys.stdin.buffer.read((2<<20)+1),a.include_body,a.title)
|
||||
payload.update(title=a.title or title,notes=a.notes if a.notes is not None else notes,request_id=str(uuid.uuid5(uuid.NAMESPACE_URL,client.base+'\n'+profile.data.get('username','')+'\n'+message_id)))
|
||||
for attr,key in [('project','project_uuid'),('assignee','assignee_uuid'),('due','due_on'),('start_on','start_on'),('planned','planned_on'),('review','review_on'),('estimate','estimate_minutes'),('complexity','complexity')]:
|
||||
value=getattr(a,attr)
|
||||
if value is not None:payload[key]=task_project(client,value) if attr=='project' else value
|
||||
emit(capture(payload));return
|
||||
if action=='project-add':
|
||||
emit(client.json(TASK_API+'items',method='POST',data={'uuid':str(uuid.uuid4()),'kind':'project','title':a.title,'notes':a.notes,'status':'todo','bucket':'inbox','priority':1}));return
|
||||
if action in ('update','done','reopen','archive'):
|
||||
item=client.json(TASK_API+'items/'+a.uuid)['item']
|
||||
if item['kind']!='task':raise Failure('This operation requires a task.')
|
||||
for attr,key in [('title','title'),('notes','notes'),('project','project_uuid'),('assignee','assignee_uuid'),('due','due_on'),('start_on','start_on'),('planned','planned_on'),('review','review_on'),('estimate','estimate_minutes'),('complexity','complexity')]:
|
||||
value=getattr(a,attr,None)
|
||||
if value is not None:item[key]=task_project(client,value) if attr=='project' else value
|
||||
if action=='done':item['status']='done'
|
||||
elif action=='reopen':item['status']='todo'
|
||||
elif action=='archive':item['archived']=True
|
||||
emit(client.json(TASK_API+'items/'+a.uuid,method='PUT',data=item,etag='"'+str(item['version'])+'"'));return
|
||||
if action=='start':
|
||||
emit(client.json(TASK_API+'work',method='POST',data={'action':'start','uuid':str(uuid.uuid4()),'task_uuid':a.uuid,'version':0}));return
|
||||
if action in ('stop','status'):
|
||||
data=client.json(TASK_API+'work')
|
||||
if action=='stop':
|
||||
if not data['item']:raise Failure('No running work timer in this Domain.')
|
||||
data=client.json(TASK_API+'work',method='POST',data={**data['item'],'action':'stop'})
|
||||
emit(data);return
|
||||
if action=='report':
|
||||
query={k:v for k,v in [('from',a.start),('to',a.end)] if v}
|
||||
data=client.json(TASK_API+'report?'+url.urlencode(query))
|
||||
if a.json:emit(data);return
|
||||
print('Timezone:',safe_text(data['timezone']))
|
||||
print('Date\tCompleted\tPoints\tWork minutes\tPomodoro minutes')
|
||||
for day in data['days']:
|
||||
if day['completed'] or day['work_ms'] or day['focus_ms']:print('%s\t%d\t%d\t%.1f\t%.1f'%(day['date'],day['completed'],day['points'],day['work_ms']/60000,day['focus_ms']/60000))
|
||||
print('\nTask UUID\tWork minutes\tTitle')
|
||||
for task in data['tasks']:print(safe_text('%s\t%.1f\t%s'%(task['uuid'],task['work_ms']/60000,task['title'])))
|
||||
if data['partial']:print('Partial report: narrow the date range.',file=sys.stderr)
|
||||
|
||||
|
||||
def contacts(client, words):
|
||||
parser=argparse.ArgumentParser(prog='pages contacts',description='Private address book; never links contacts to login accounts.')
|
||||
sub=parser.add_subparsers(dest='action',required=True)
|
||||
p=sub.add_parser('list');p.add_argument('--query',default='');p.add_argument('--after',default='')
|
||||
p=sub.add_parser('query');p.add_argument('query');p.add_argument('--neomutt',action='store_true')
|
||||
p=sub.add_parser('show');p.add_argument('uuid',type=task_uuid)
|
||||
p=sub.add_parser('export');p.add_argument('uuid',type=task_uuid)
|
||||
p=sub.add_parser('import');p.add_argument('file',type=Path)
|
||||
a=parser.parse_args(words);api='/_/api/v1/extension/organizer.contacts/'
|
||||
if a.action=='import':
|
||||
with a.file.open('rb') as f:raw=f.read(65537)
|
||||
if len(raw)>65536:raise Failure('One UTF-8 vCard per import, up to 64 KiB. Use the Web importer for multiple cards.')
|
||||
data=client.json(api+'import',method='POST',data=raw,content_type='text/vcard')
|
||||
elif a.action=='export':
|
||||
data=client.json(api+'items/'+a.uuid);sys.stdout.write(data['item']['vcard']);return
|
||||
elif a.action=='show':data=client.json(api+'items/'+a.uuid)
|
||||
else:
|
||||
try: data=client.json(api+'items?'+url.urlencode({'q':a.query,'after':getattr(a,'after','')}))
|
||||
except Failure:
|
||||
if a.action=='query' and a.neomutt: print('PagesWiki address query failed; check login and permissions.')
|
||||
raise
|
||||
if a.action=='query' and a.neomutt:
|
||||
if not any(item['emails'] for item in data.get('items',[])):
|
||||
print('No matching email addresses.')
|
||||
raise Failure('No matching email addresses.')
|
||||
print('PagesWiki contacts'+(' (first 100; narrow query)' if data.get('next') else ''))
|
||||
for item in data.get('items',[]):
|
||||
clean=lambda v:' '.join(safe_text(v).split())
|
||||
for address in item['emails']:
|
||||
# Do not let imported metadata forge query result columns.
|
||||
if '\t' not in address and '\n' not in address and '\r' not in address:
|
||||
print(clean(address)+'\t'+clean(item['name'])+'\t'+clean(item['organization']))
|
||||
return
|
||||
print(json.dumps(data,ensure_ascii=False,indent=2))
|
||||
|
||||
|
||||
def execute(client, profile, words):
|
||||
if words and words[0] in ('contact','contacts'):
|
||||
return contacts(client,words[1:])
|
||||
if words and words[0] in ('task','tasks'):
|
||||
return tasks(client,profile,words[1:])
|
||||
parser = argparse.ArgumentParser(prog='pages')
|
||||
sub = parser.add_subparsers(dest='command', required=True)
|
||||
for command in ('ls', 'cd', 'cat', 'view', 'edit', 'open'):
|
||||
@@ -556,7 +738,7 @@ def shell(client, profile):
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
parser = argparse.ArgumentParser(prog='pages', description=__doc__, epilog='Commands: configure, login, logout, status, shell, ls, cd, pwd, cat, view, get, put, edit, search, open. Use pages COMMAND --help.')
|
||||
parser = argparse.ArgumentParser(prog='pages', description=__doc__, epilog='Commands: configure, login, logout, status, shell, ls, cd, pwd, cat, view, get, put, edit, search, open, task, contacts. Use pages COMMAND --help.')
|
||||
parser.add_argument('--profile', help='Use another configured connection for this invocation')
|
||||
parser.add_argument('--lang', help='auto or an exact language tag for this invocation')
|
||||
parser.add_argument('command', nargs='?', default='shell')
|
||||
@@ -595,8 +777,14 @@ def main(argv=None):
|
||||
username = spec['username']
|
||||
expires = client.login(username, getpass.getpass('Password: '))
|
||||
same_identity = profile.data.get('origin') == client.base and profile.data.get('username') == username
|
||||
pending = profile.data.get('task_outbox', {})
|
||||
if pending and not same_identity:
|
||||
try: client.json('/_/auth/logout', method='POST')
|
||||
except Failure: pass
|
||||
raise Failure('Pending task drafts belong to the previous account. Use a separate profile; the drafts were retained.')
|
||||
cwd = profile.data.get('cwd', '/') if same_identity else '/'
|
||||
profile.data = {'origin': client.base, 'username': username, 'configured_username': spec.get('username', ''), 'token': client.token, 'expires': expires, 'cwd': cwd}
|
||||
if pending: profile.data['task_outbox'] = pending
|
||||
try:
|
||||
profile.save()
|
||||
except OSError:
|
||||
|
||||
Reference in new issue
Block a user