Add task capture, work reports, daily digests and private contacts
This commit is contained in:
1 parent
0d814db9ae
commit
d3f4e668c4
4 files changed
+282
-5
No files matched your search
@@ -153,3 +153,8 @@ go test ./internal/server -run TestRemoteCLIHTTPIntegration -count=1
|
||||
```
|
||||
|
||||
偽サーバと一時DBのみを使用します。本番資格情報や外部カレンダーは不要です。
|
||||
## タスクと連絡先
|
||||
|
||||
`pages task --help` で作成・計画・開始/終了・レポートを操作できる。
|
||||
`pages contacts --help` で個人アドレス帳の検索・vCard入出力を行える。
|
||||
[neomutt設定と操作例](../docs/operations/neomutt-tasks.md)を参照。
|
||||
@@ -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:
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import contextlib
|
||||
import io
|
||||
import json
|
||||
import runpy
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
import unittest
|
||||
|
||||
m=runpy.run_path(str(Path(__file__).with_name('pages')))
|
||||
|
||||
class ProductivityTests(unittest.TestCase):
|
||||
def test_mail_privacy_and_long_subject_override(self):
|
||||
raw=b'Subject: Test\r\nFrom: Person <person@example.invalid>\r\nMessage-ID: <id@example.invalid>\r\nContent-Type: text/plain; charset=utf-8\r\n\r\nPRIVATE BODY'
|
||||
title,notes,ident=m['task_mail'](raw)
|
||||
self.assertEqual(title,'Test');self.assertNotIn('PRIVATE BODY',notes);self.assertIn('Message-ID:',notes)
|
||||
self.assertIn('PRIVATE BODY',m['task_mail'](raw,True)[1])
|
||||
raw=raw.replace(b'Subject: Test',b'Subject: '+b'a'*301)
|
||||
with self.assertRaises(m['Failure']):m['task_mail'](raw)
|
||||
self.assertEqual(m['task_mail'](raw,False,'Override')[0],'Override')
|
||||
|
||||
def test_capture_retains_request_until_explicit_retry(self):
|
||||
calls=[]
|
||||
def send(route,**kwargs):
|
||||
calls.append(kwargs['data'].copy())
|
||||
if len(calls)==1:raise m['Failure']('offline')
|
||||
return {'item':{'uuid':kwargs['data']['request_id']}}
|
||||
profile=SimpleNamespace(data={},save=lambda:None)
|
||||
client=SimpleNamespace(json=send)
|
||||
with contextlib.redirect_stdout(io.StringIO()),contextlib.redirect_stderr(io.StringIO()):
|
||||
with self.assertRaises(m['Failure']):m['tasks'](client,profile,['add','Offline task'])
|
||||
ident=next(iter(profile.data['task_outbox']))
|
||||
m['tasks'](client,profile,['retry',ident])
|
||||
self.assertEqual(calls[0],calls[1]);self.assertEqual(profile.data['task_outbox'],{})
|
||||
|
||||
def test_neomutt_query_columns_are_not_forgeable(self):
|
||||
client=SimpleNamespace(json=lambda *a,**kw:{'items':[{'name':'A\tB\nC','emails':['a@example.invalid'],'organization':'Org\nInjected'}]})
|
||||
out=io.StringIO()
|
||||
with contextlib.redirect_stdout(out):m['contacts'](client,['query','A','--neomutt'])
|
||||
rows=out.getvalue().splitlines();self.assertEqual(len(rows),2);self.assertEqual(len(rows[1].split('\t')),3)
|
||||
|
||||
def test_command_help_without_credentials(self):
|
||||
for cmd in [('task','--help'),('contacts','--help'),('task','add','--help')]:
|
||||
with contextlib.redirect_stdout(io.StringIO()),self.assertRaises(SystemExit) as raised:m['main'](list(cmd))
|
||||
self.assertEqual(raised.exception.code,0)
|
||||
@@ -0,0 +1,40 @@
|
||||
"""Called by the Go synthetic HTTP fixture; no production credentials."""
|
||||
import contextlib
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
import runpy
|
||||
import sys
|
||||
|
||||
def run():
|
||||
m=runpy.run_path(str(Path(__file__).with_name('pages')))
|
||||
fixture=json.load(sys.stdin)
|
||||
client=m['Client'](fixture['origin'],fixture['token'])
|
||||
profile=m['Profile']('test',fixture['root'])
|
||||
def task(*words):
|
||||
output=io.StringIO()
|
||||
with contextlib.redirect_stdout(output):m['tasks'](client,profile,list(words))
|
||||
return json.loads(output.getvalue())
|
||||
with profile.locked():
|
||||
prefs=task('preferences','--due-days','2','--estimate','60','--complexity','3')
|
||||
assert prefs['preferences']['due_days']==2
|
||||
item=task('add','CLI task')['item'];ident=item['uuid']
|
||||
assert item['estimate_minutes']==60 and item['due_on']
|
||||
task('start',ident);assert task('status')['item']['task_uuid']==ident
|
||||
task('stop');assert task('status')['item'] is None
|
||||
item=task('update',ident,'--title','Updated CLI task')['item'];assert item['title']=='Updated CLI task'
|
||||
task('done',ident)
|
||||
report=task('report','--json');assert sum(d['points'] for d in report['days'])==7
|
||||
assert any(i['uuid']==ident for i in report['tasks'])
|
||||
path=Path(fixture['root'])/'person.vcf'
|
||||
path.write_text('BEGIN:VCARD\r\nVERSION:4.0\r\nUID:urn:test:cli\r\nFN:CLI Person\r\nEMAIL:cli@example.invalid\r\nEND:VCARD\r\n')
|
||||
output=io.StringIO()
|
||||
with contextlib.redirect_stdout(output):m['contacts'](client,['import',str(path)])
|
||||
contact=json.loads(output.getvalue())['item'];output=io.StringIO()
|
||||
with contextlib.redirect_stdout(output):m['contacts'](client,['query','CLI','--neomutt'])
|
||||
assert 'cli@example.invalid\tCLI Person\t' in output.getvalue()
|
||||
output=io.StringIO()
|
||||
with contextlib.redirect_stdout(output):m['contacts'](client,['export',contact['uuid']])
|
||||
assert 'UID:urn:test:cli' in output.getvalue()
|
||||
|
||||
if __name__=='__main__':run()
|
||||
Reference in new issue
Block a user