Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
T
topydo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
topydo
Commits
cd7fc25d
Commit
cd7fc25d
authored
May 31, 2015
by
Bram Schoenmakers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduced 'json' subcommand to convert todo.txt to JSON.
parent
33d3b444
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
200 additions
and
0 deletions
+200
-0
test/JsonCommandTest.py
test/JsonCommandTest.py
+71
-0
test/data/ListCommandTest.json
test/data/ListCommandTest.json
+1
-0
test/data/ListCommandUnicodeTest.json
test/data/ListCommandUnicodeTest.json
+1
-0
topydo/Commands.py
topydo/Commands.py
+1
-0
topydo/commands/JsonCommand.py
topydo/commands/JsonCommand.py
+55
-0
topydo/lib/JsonPrinter.py
topydo/lib/JsonPrinter.py
+64
-0
topydo/lib/TodoBase.py
topydo/lib/TodoBase.py
+7
-0
No files found.
test/JsonCommandTest.py
0 → 100644
View file @
cd7fc25d
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import
codecs
import
re
import
sys
import
unittest
from
topydo.lib.Config
import
config
from
topydo.commands.JsonCommand
import
JsonCommand
from
test.CommandTest
import
CommandTest
,
utf8
from
test.TestFacilities
import
load_file_to_todolist
class
JsonCommandTest
(
CommandTest
):
def
setUp
(
self
):
super
(
JsonCommandTest
,
self
).
setUp
()
self
.
todolist
=
load_file_to_todolist
(
"test/data/ListCommandTest.txt"
)
def
test_json
(
self
):
command
=
JsonCommand
([
""
],
self
.
todolist
,
self
.
out
,
self
.
error
)
command
.
execute
()
self
.
assertFalse
(
self
.
todolist
.
is_dirty
())
jsontext
=
""
with
codecs
.
open
(
'test/data/ListCommandTest.json'
,
'r'
,
encoding
=
'utf-8'
)
as
json
:
jsontext
=
json
.
read
()
self
.
assertEqual
(
self
.
output
,
jsontext
)
self
.
assertEqual
(
self
.
errors
,
""
)
def
test_help
(
self
):
command
=
JsonCommand
([
"help"
],
self
.
todolist
,
self
.
out
,
self
.
error
)
command
.
execute
()
self
.
assertEqual
(
self
.
output
,
""
)
self
.
assertEqual
(
self
.
errors
,
command
.
usage
()
+
"
\
n
\
n
"
+
command
.
help
()
+
"
\
n
"
)
class
JsonCommandUnicodeTest
(
CommandTest
):
def
setUp
(
self
):
super
(
JsonCommandUnicodeTest
,
self
).
setUp
()
self
.
todolist
=
load_file_to_todolist
(
"test/data/ListCommandUnicodeTest.txt"
)
def
test_json_unicode
(
self
):
command
=
JsonCommand
([
""
],
self
.
todolist
,
self
.
out
,
self
.
error
)
command
.
execute
()
self
.
assertFalse
(
self
.
todolist
.
is_dirty
())
jsontext
=
""
with
codecs
.
open
(
'test/data/ListCommandUnicodeTest.json'
,
'r'
,
encoding
=
'utf-8'
)
as
json
:
jsontext
=
json
.
read
()
self
.
assertEqual
(
self
.
output
,
utf8
(
jsontext
))
self
.
assertEqual
(
self
.
errors
,
""
)
if
__name__
==
'__main__'
:
unittest
.
main
()
test/data/ListCommandTest.json
0 → 100644
View file @
cd7fc25d
[{
"completed"
:
false
,
"completion_date"
:
null
,
"contexts"
:
[
"Context2"
],
"creation_date"
:
null
,
"priority"
:
"C"
,
"projects"
:
[
"Project1"
],
"source"
:
"(C) Foo @Context2 Not@Context +Project1 Not+Project"
,
"tags"
:
[],
"text"
:
"Foo @Context2 Not@Context +Project1 Not+Project"
},
{
"completed"
:
false
,
"completion_date"
:
null
,
"contexts"
:
[],
"creation_date"
:
null
,
"priority"
:
"C"
,
"projects"
:
[],
"source"
:
"(C) Drink beer @ home"
,
"tags"
:
[],
"text"
:
"Drink beer @ home"
},
{
"completed"
:
false
,
"completion_date"
:
null
,
"contexts"
:
[],
"creation_date"
:
null
,
"priority"
:
"C"
,
"projects"
:
[],
"source"
:
"(C) 13 + 29 = 42"
,
"tags"
:
[],
"text"
:
"13 + 29 = 42"
},
{
"completed"
:
false
,
"completion_date"
:
null
,
"contexts"
:
[
"Context1"
],
"creation_date"
:
null
,
"priority"
:
"D"
,
"projects"
:
[
"Project2"
],
"source"
:
"(D) Bar @Context1 +Project2 p:1"
,
"tags"
:
[[
"p"
,
"1"
]],
"text"
:
"Bar @Context1 +Project2"
}]
test/data/ListCommandUnicodeTest.json
0 → 100644
View file @
cd7fc25d
[{
"completed"
:
false
,
"completion_date"
:
null
,
"contexts"
:
[],
"creation_date"
:
null
,
"priority"
:
"C"
,
"projects"
:
[],
"source"
:
"(C) And some spécial tag:◄"
,
"tags"
:
[[
"tag"
,
"◄"
]],
"text"
:
"And some spécial"
}]
topydo/Commands.py
View file @
cd7fc25d
...
@@ -34,6 +34,7 @@ _SUBCOMMAND_MAP = {
...
@@ -34,6 +34,7 @@ _SUBCOMMAND_MAP = {
'edit'
:
'EditCommand'
,
'edit'
:
'EditCommand'
,
'exit'
:
'ExitCommand'
,
# used for the prompt
'exit'
:
'ExitCommand'
,
# used for the prompt
'ical'
:
'IcalCommand'
,
'ical'
:
'IcalCommand'
,
'json'
:
'JsonCommand'
,
'ls'
:
'ListCommand'
,
'ls'
:
'ListCommand'
,
'lscon'
:
'ListContextCommand'
,
'lscon'
:
'ListContextCommand'
,
'listcon'
:
'ListContextCommand'
,
'listcon'
:
'ListContextCommand'
,
...
...
topydo/commands/JsonCommand.py
0 → 100644
View file @
cd7fc25d
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Implements a subcommand that outputs an JSON file.
"""
from
topydo.lib.JsonPrinter
import
JsonPrinter
from
topydo.commands.ListCommand
import
ListCommand
class
JsonCommand
(
ListCommand
):
def
__init__
(
self
,
p_args
,
p_todolist
,
p_out
=
lambda
a
:
None
,
p_err
=
lambda
a
:
None
,
p_prompt
=
lambda
a
:
None
):
super
(
JsonCommand
,
self
).
__init__
(
p_args
,
p_todolist
,
p_out
,
p_err
,
p_prompt
)
self
.
printer
=
JsonPrinter
()
def
_print
(
self
):
self
.
out
(
str
(
self
.
_view
()))
def
execute
(
self
):
return
super
(
JsonCommand
,
self
).
execute
()
def
usage
(
self
):
return
"""Synopsis: json [-x] [expression]"""
def
help
(
self
):
return
"""
\
Similar to the 'ls' subcommand, except that the todos are printed in JSON
format such that other applications can process it.
By default prints the active todo items, possibly filtered by the given
expression.
For the supported options, please refer to the help text of 'ls'
(topydo help ls).
Note: topydo does not support reading JSON files, this is merely a dump.
"""
topydo/lib/JsonPrinter.py
0 → 100644
View file @
cd7fc25d
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2015 Bram Schoenmakers <me@bramschoenmakers.nl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Provides a printer that transforms a list of Todo items to a JSON structure
such that other applications can process it.
"""
import
json
from
topydo.lib.PrettyPrinter
import
Printer
def
_convert_todo
(
p_todo
):
""" Converts a Todo instance to a dictionary. """
creation_date
=
p_todo
.
creation_date
()
completion_date
=
p_todo
.
completion_date
()
result
=
{
'source'
:
p_todo
.
source
(),
'text'
:
p_todo
.
text
(),
'priority'
:
p_todo
.
priority
(),
'completed'
:
p_todo
.
is_completed
(),
'tags'
:
p_todo
.
tags
(),
'projects'
:
list
(
p_todo
.
projects
()),
'contexts'
:
list
(
p_todo
.
contexts
()),
'creation_date'
:
creation_date
.
isoformat
()
if
creation_date
else
None
,
'completion_date'
:
completion_date
.
isoformat
()
if
completion_date
else
None
}
return
result
class
JsonPrinter
(
Printer
):
"""
A printer that converts a list of Todo items to a string in JSON format.
"""
def
__init__
(
self
):
super
(
JsonPrinter
,
self
).
__init__
()
def
print_todo
(
self
,
p_todo
):
return
json
.
dumps
(
_convert_todo
(
p_todo
),
ensure_ascii
=
False
,
sort_keys
=
True
)
def
print_list
(
self
,
p_todos
):
result
=
[]
for
todo
in
p_todos
:
result
.
append
(
_convert_todo
(
todo
))
return
json
.
dumps
(
result
,
ensure_ascii
=
False
,
sort_keys
=
True
)
topydo/lib/TodoBase.py
View file @
cd7fc25d
...
@@ -130,6 +130,13 @@ class TodoBase(object):
...
@@ -130,6 +130,13 @@ class TodoBase(object):
value
=
p_value
if
p_value
!=
""
else
r'\
S+
'
value
=
p_value
if
p_value
!=
""
else
r'\
S+
'
self.src = re.sub(r'
\
s
?
\
b' + p_key + '
:
' + value + r'
\
b', '', self.src)
self.src = re.sub(r'
\
s
?
\
b' + p_key + '
:
' + value + r'
\
b', '', self.src)
def tags(self):
"""
Returns a list of tuples with key-value pairs representing tags in
this todo item.
"""
return self.fields['
tags
']
def set_priority(self, p_priority):
def set_priority(self, p_priority):
"""
"""
Sets the priority of the todo. Must be a single capital letter [A-Z],
Sets the priority of the todo. Must be a single capital letter [A-Z],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment