14 Commits
html ... master

10 changed files with 281 additions and 52 deletions

2
.gitignore vendored
View File

@@ -1,6 +1,8 @@
*.swp *.swp
*.pyc *.pyc
__pycache__
tags tags
venv venv

18
LICENSE Normal file
View File

@@ -0,0 +1,18 @@
Copyright 2018 Aaron Gutierrez
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,16 +1,38 @@
# cmdasana # cmdasana
A curses CLI for Asana, using the Asana API. A curses CLI for Asana, using the Asana API.
Requirments: ## Requirments
* python 3
* [python-asana](https://github.com/Asana/python-asana) * [python-asana](https://github.com/Asana/python-asana)
* [urwid (included)](http://urwid.org) * [urwid](http://urwid.org)
* python 2 * [python-dateutil](https://github.com/dateutil/dateutil/)
Usage: ## Setup
### Create an Asana OAuth app
See [instructions from Asana](https://asana.com/developers/documentation/getting-started/auth#register-an-app)
on how to create a new app. Use `urn:ietf:wg:oauth:2.0:oob` as the redirect
URL.
Once you create your app, save your client ID and secret in a file `secrets.py`:
```python
CLIENT_ID='...'
CLIENT_SECRET='...'
``` ```
make
./cmdasana.py ### Install dependencies
Using `pip`:
```
pip3 install asana urwid python-dateutil
```
## Usage
```
./main.py
``` ```
When you first cmdasana, you will need to authorize the app in your browser. When you first cmdasana, you will need to authorize the app in your browser.
Copy and paste your OAuth key into the terminal to get started. Copy and paste your OAuth key into the terminal to get started.
## Navigation
Use arrow keys to navigate, `<enter>` to "click", and `<backspace>` to return to
the previous page.

View File

@@ -3,6 +3,7 @@ from models.models import *
class AsanaService(object): class AsanaService(object):
TASK_FIELDS = [ TASK_FIELDS = [
'name', 'name',
'html_notes',
'notes', 'notes',
'assignee.name', 'assignee.name',
'assignee_status', 'assignee_status',
@@ -23,6 +24,14 @@ class AsanaService(object):
'subtasks.name', 'subtasks.name',
] ]
STORY_FIELDS = [
'created_at',
'created_by.name',
'html_text',
'text',
'type'
]
def __init__(self, client): def __init__(self, client):
self.client = client self.client = client
self.completed_tasks = False self.completed_tasks = False
@@ -32,17 +41,6 @@ class AsanaService(object):
def __wrap__(self, Klass, values): def __wrap__(self, Klass, values):
return map(Klass, values) return map(Klass, values)
def get_tasks(self, project_id):
params = {
'completed_since': '' if self.completed_tasks else 'now',
'opt_fields': self.TASK_FIELDS,
}
return self.__wrap__(
Task,
self.client.tasks.find_by_project(project_id, params=params)
)
def get_my_tasks(self): def get_my_tasks(self):
return self.__wrap__( return self.__wrap__(
Task, Task,
@@ -54,6 +52,11 @@ class AsanaService(object):
}) })
) )
def get_project(self, project_id):
return Project(
self.client.projects.find_by_id(project_id)
)
def get_task(self, task_id): def get_task(self, task_id):
return Task(self.client.tasks.find_by_id( return Task(self.client.tasks.find_by_id(
task_id, task_id,
@@ -61,8 +64,21 @@ class AsanaService(object):
'opt_fields': self.TASK_FIELDS 'opt_fields': self.TASK_FIELDS
} }
)) ))
def get_tasks(self, project_id):
params = {
'completed_since': '' if self.completed_tasks else 'now',
'opt_fields': self.TASK_FIELDS,
}
return self.__wrap__(
Task,
self.client.tasks.find_by_project(project_id, params=params)
)
def get_stories(self, task_id): def get_stories(self, task_id):
stories = self.client.stories.find_by_task(task_id) stories = self.client.stories.find_by_task(task_id, params = {
'opt_fields': self.STORY_FIELDS
})
filtered_stories = filter(lambda s: s['type'] == 'comment', stories) filtered_stories = filter(lambda s: s['type'] == 'comment', stories)
return self.__wrap__(Story, filtered_stories) return self.__wrap__(Story, filtered_stories)

View File

@@ -1,4 +1,6 @@
import dateutil from datetime import timezone
import dateutil.parser
from html.parser import HTMLParser
import sys import sys
class AsanaObject(object): class AsanaObject(object):
@@ -25,7 +27,7 @@ class Task(AsanaObject):
return super(Task, self).name() return super(Task, self).name()
def assignee(self): def assignee(self):
if 'assignee' in self.object_dict: if 'assignee' in self.object_dict and self.object_dict['assignee']:
return User(self.object_dict['assignee']) return User(self.object_dict['assignee'])
else: else:
return None return None
@@ -34,18 +36,30 @@ class Task(AsanaObject):
return self.object_dict['assignee_status'] return self.object_dict['assignee_status']
def description(self): def description(self):
if 'notes' in self.object_dict: if 'html_notes' in self.object_dict:
parser = HTMLTextParser()
parser.feed(self.object_dict['html_notes'])
parser.close()
text = parser.get_formatted_text()
if (len(text) > 0):
return text
else:
return ""
elif 'notes' in self.object_dict:
return self.object_dict['notes'] return self.object_dict['notes']
else: else:
return None return ""
def due_date(self): def due_date(self):
if 'due_at' in self.object_dict: if 'due_at' in self.object_dict and self.object_dict['due_at']:
return dateutil.parser.parse(self.object_dict['due_at']) datetime = dateutil.parser.parse(self.object_dict['due_at'])
elif 'due_one' in self.object_dict: datetime = datetime.replace(tzinfo=timezone.utc).astimezone(tz=None)
return dateutil.parser.parse(self.object_dict['due_on']) return datetime.strftime('%b %d, %Y %H:%M')
elif 'due_on' in self.object_dict and self.object_dict['due_on']:
date = dateutil.parser.parse(self.object_dict['due_on'])
return date.strftime('%b %d, %Y')
else: else:
return None return 'no due date'
def parent(self): def parent(self):
if 'parent' in self.object_dict and self.object_dict['parent']: if 'parent' in self.object_dict and self.object_dict['parent']:
@@ -89,6 +103,107 @@ class CustomField(AsanaObject):
return '' return ''
class Strong(object):
def __init__(self, body):
self.body = body
def text_format(self):
return ('strong', self.body.text_format())
class Italic(object):
def __init__(self, body):
self.body = body
def text_format(self):
return ('italic', self.body.text_format())
class Underline(object):
def __init__(self, body):
self.body = body
def text_format(self):
return ('underline', self.body.text_format())
class Link(object):
def __init__(self, body):
self.body = body
def text_format(self):
return ('link', self.body.text_format())
class Tag(object):
def __init__(self, body):
self.body = body
def text_format(self):
return self.body.text_format()
class List(object):
def __init__(self, body):
self.body = body
def text_format(self):
return self.body.text_format()
class ListItem(object):
def __init__(self, body, indent):
self.body = body
self.indent = indent
def text_format(self):
return ('', [(' ' * self.indent), '', self.body.text_format(), '\n'])
class Text(object):
def __init__(self, body):
self.body = body
def text_format(self):
return self.body
class HTMLTextParser(HTMLParser):
def __init__(self):
self.text = []
self.tag_stack = []
self.indent = 0
super().__init__()
def handle_starttag(self, tag, attrs):
if tag == 'strong':
self.tag_stack.append(Strong)
elif tag == 'em':
self.tag_stack.append(Italic)
elif tag == 'u':
self.tag_stack.append(Underline)
elif tag == 'a':
self.tag_stack.append(Link)
elif tag == 'ul' or tag == 'ol':
self.indent += 2
self.tag_stack.append(List)
elif tag == 'li':
self.tag_stack.append(ListItem)
else:
self.tag_stack.append(Tag)
def handle_data(self, data):
self.text.append(Text(data))
def handle_endtag(self, tag):
data = self.text.pop() if len(self.text) > 0 else Text("")
Class = self.tag_stack.pop()
if tag == 'ul' or tag =='ol':
self.indent -= 2
if tag == 'li':
self.text.append(Class(data, self.indent))
else:
self.text.append(Class(data))
def get_formatted_text(self):
formatted = [t.text_format() for t in self.text]
return formatted
class Story(AsanaObject): class Story(AsanaObject):
def creator(self): def creator(self):
if 'created_by' in self.object_dict: if 'created_by' in self.object_dict:
@@ -96,5 +211,18 @@ class Story(AsanaObject):
else: else:
return '' return ''
def created_at(self):
if 'created_at' in self.object_dict:
return dateutil.parser.parse(self.object_dict['created_at']) \
.replace(tzinfo=timezone.utc).astimezone(tz=None)
else:
return ''
def text(self): def text(self):
return self.object_dict['text'] if 'html_text' in self.object_dict:
parser = HTMLTextParser()
parser.feed(self.object_dict['html_text'])
parser.close()
return parser.get_formatted_text()
else:
return [self.object_dict['text']]

View File

@@ -1,10 +1,3 @@
asana==0.6.5 asana==0.8.2
certifi==2017.11.5 python-dateutil==2.8.0
chardet==3.0.4 urwid==2.0.1
idna==2.6
oauthlib==2.0.6
requests==2.14.2
requests-oauthlib==0.6.2
six==1.10.0
urllib3==1.22
urwid==1.3.1

View File

@@ -1,6 +1,7 @@
palette = [ palette = [
('atm_section', 'white,bold', 'dark blue'), ('atm_section', 'white,bold', 'dark blue'),
('author', 'bold,dark blue', ''), ('author', 'bold,dark blue', ''),
('timestamp', 'underline', ''),
('custom_fields', 'dark red', ''), ('custom_fields', 'dark red', ''),
('header', 'bold,light green', ''), ('header', 'bold,light green', ''),
('project', 'yellow', ''), ('project', 'yellow', ''),
@@ -8,6 +9,10 @@ palette = [
('selected', 'standout', ''), ('selected', 'standout', ''),
('task', 'light green', ''), ('task', 'light green', ''),
('text', '', ''), ('text', '', ''),
('strong', 'bold', ''),
('underline', 'underline', ''),
('link', 'underline,light blue', ''),
('italic', 'italics', ''),
('workspace', 'white', 'dark blue'), ('workspace', 'white', 'dark blue'),
] ]

View File

@@ -1,10 +1,11 @@
import urwid import urwid
from datetime import date, datetime
from ui.task_list import TaskRow from ui.task_list import TaskRow
class TaskDetails(object): class TaskDetails(object):
def __init__(self, task, stories, on_subtask_click, on_project_click, def __init__(self, task, stories, on_subtask_click, on_project_click,
on_comment): on_comment, on_assignee_click, on_due_date_click):
self.task = task self.task = task
self.on_subtask_click = on_subtask_click, self.on_subtask_click = on_subtask_click,
self.on_project_click = on_project_click, self.on_project_click = on_project_click,
@@ -12,13 +13,15 @@ class TaskDetails(object):
body = [ body = [
urwid.Text(('task', task.name())), urwid.Text(('task', task.name())),
urwid.Divider('-'), urwid.Divider(''),
Memberships(task, on_subtask_click, on_project_click).component(), Memberships(task, on_subtask_click, on_project_click).component(),
urwid.Divider('-'), urwid.Divider(''),
Assignee(task, on_assignee_click).component(),
DueDate(task, on_due_date_click).component(),
CustomFields(task).component(), CustomFields(task).component(),
urwid.Divider('='), urwid.Divider(''),
urwid.Text(task.description()), urwid.Text(task.description()),
urwid.Divider('-'), urwid.Divider(''),
] ]
if task.subtasks(): if task.subtasks():
@@ -38,6 +41,45 @@ class TaskDetails(object):
def component(self): def component(self):
return self.details return self.details
class Assignee(object):
def __init__(self, task, on_click):
if task.assignee():
assignee = task.assignee().name()
else:
assignee = "unassigned"
self.assignee = urwid.SelectableIcon([('strong', 'Assignee: '), ('', assignee)])
self.on_click = on_click
#urwid.connect_signal(self.assignee, 'keypress', self.on_keypress)
def component(self):
return self.assignee
def on_keypress(self, size, key):
if key == "enter":
self.on_click()
else:
return key
class DueDate(object):
def __init__(self, task, on_click):
due_date = task.due_date()
self.due_date = urwid.SelectableIcon([('strong', 'Due: '), ('', str(task.due_date()))])
self.on_click = on_click
#urwid.connect_signal(self.due_date, 'keypress', self.on_keypress)
def component(self):
return self.due_date
def on_keypress(self, size, key):
if key == "enter":
self.on_click()
else:
return key
class Memberships(object): class Memberships(object):
def __init__(self, task, on_subtask_click, on_project_click): def __init__(self, task, on_subtask_click, on_project_click):
self.on_project_click = on_project_click self.on_project_click = on_project_click
@@ -74,11 +116,13 @@ class CustomFields(object):
class Stories(object): class Stories(object):
def __init__(self, stories): def __init__(self, stories):
components = [urwid.Text([ components = [
('author', s.creator()), urwid.Text([
s.text(), ('timestamp', s.created_at().strftime('%Y-%m-%d %H:%M')),
'\n' ' ',
]) for s in stories] ('author', s.creator()),
] + s.text())
for s in stories]
self.stories = urwid.Pile(components) self.stories = urwid.Pile(components)

View File

@@ -38,7 +38,7 @@ class MyTasks(object):
] + [TaskRow(t, self.callback) for t in self.today] + [ ] + [TaskRow(t, self.callback) for t in self.today] + [
urwid.Text(('atm_section', 'Upcoming')) urwid.Text(('atm_section', 'Upcoming'))
] + [TaskRow(t, self.callback) for t in self.upcoming] + [ ] + [TaskRow(t, self.callback) for t in self.upcoming] + [
urwid.Text(('atm_section', 'Upcoming')) urwid.Text(('atm_section', 'Later'))
] + [TaskRow(t, self.callback) for t in self.later] ] + [TaskRow(t, self.callback) for t in self.later]
) )
), ),

View File

@@ -32,16 +32,17 @@ class Ui(object):
stories, stories,
self.task_details, self.task_details,
self.task_list, self.task_list,
None).component()) None, None, None).component())
thread = Thread(target=runInThread()) thread = Thread(target=runInThread())
thread.start() thread.start()
def task_list(self, id): def task_list(self, id):
self.nav_stack.append(('project', id)) self.nav_stack.append(('project', id))
def runInThread(): def runInThread():
project = self.asana_service.get_project(id)
tasks = self.asana_service.get_tasks(id) tasks = self.asana_service.get_tasks(id)
self.update(TaskList(tasks, self.update(TaskList(tasks,
'TODO: get project name', project.name(),
self.task_details self.task_details
).component()) ).component())
thread = Thread(target=runInThread()) thread = Thread(target=runInThread())