API Reference¶
TodoTxt¶
- class pytodotxt.TodoTxt(filename, encoding='utf-8', parser=None)¶
Convenience wrapper for a single todo.txt file
The most common use is:
todotxt = TodoTxt("todo.txt") todotxt.parse()
Use the
tasksproperty to access the parsed entries.- __init__(filename, encoding='utf-8', parser=None)¶
Create a new TodoTxt instance from
filename- Parameters
filename (
strorpathlib.Path) – the file you wish to useencoding – what encoding to assume for the content of the file
parser (
TodoTxtParser) – This may be a custom parser instance to use instead of the defaultTodoTxtParser.
- add(task)¶
Add a task to this container
You could also just
appendtoself.tasks, but this function call will also update thetodotxtandlinenrproperties oftask.- Parameters
task (
Task) – The task that should be added to this todo.txt file
- property lines¶
Much like
self.tasks, but already sorted by linenr and converted to string
- parse()¶
(Re)parse
self.filenameThis will try to detect the line separator of the file automatically and remember it as
self.linesepfor the time when you save the file back to disk.- Returns
the list of all tasks read from the file.
- save(target=None, safe=True, linesep=None)¶
Save all tasks to disk
If
targetis not provided, thefilenameproperty is being used as the target file to save to.If
safeis set (the default), the file will first be written to a temporary file in the same folder as the target file and after the successful write to disk, it will be moved in place oftarget. This can cause trouble though with folders that are synchronised to some cloud storage.With
linesepyou can specify the line seperator. If it is not set it defaults to the systems default line seperator (or the detected line separator, if the file has been parsed from disk first).- Parameters
target – The file to save to, or
Noneto useself.filenamesafe – Whether or not to save the file in a way that does not destroy the previous file in case on errors.
linesep – The line separator, or
Noneto useself.linesep
- tasks¶
The tasks of this file
- write_to_stream(stream, linesep=None)¶
Save all tasks, ordered by their line number, to the stream
- Parameters
stream – an
io.IOBaselike stream that accepts bytes being written to it.linesep – the line separator, or
Noneto useself.linesep
Task¶
- class pytodotxt.Task(line=None, linenr=None, todotxt=None)¶
A task of a todo.txt file
The usual way to create a task is to create it from an initial string:
task = Task("(B) some task")
or:
task = Task() task.parse("(B) some task")
The inverse operation to parsing is to convert the task to a string:
task = Task("(B) some task") assert str(task) == "(B) some task"
- __init__(line=None, linenr=None, todotxt=None)¶
Create a new task
If no parameters are provided, an empty task is created.
You may provide a todo.txtlike textual representation of a task with the
lineparameter, which will be parsed and all properties will be set accordingly.In case this task belongs to a container file (
todotxtparameter), you can provide thelinenr, if you want to be able to refer to it later-on.Both
todotxtandlinenrparameter are entirely optional and are not connected to any functionality in this class’s functions.To access
key:valueattributes of the task, you can use the convenience notion oftask['attr_key']which will always result in a list. If the task doesn’t have thekeyattribute that list will be empty though.- Parameters
line – is the raw string representation (one line of a todo.txt file).
linenr – is the line number within the
todotxtfile, if any. This is purely optional.todotxt (
NoneorTodoTxt.) – theTodoTxtcontainer to which this task belongs, if any.
- add_attribute(key, value)¶
Add the
key:valueattribute to the end of the task
- add_context(context)¶
Add
@contextto the end of the task- Parameters
context – the name of the project, without the leading
@
- add_project(project)¶
Add
+projectto the end of the task- Parameters
project – the name of the project, without the leading
+
- append(text, add_space=True)¶
Append
textto the end of the task- Parameters
text – The text to append
add_space – Whether or not to add a space before
text.
- property attributes¶
A dict of all
key:valuesattributesThe values portion of the dictionary is always a list.
- bare_description()¶
The description of the task without contexts, projects or other attributes
Some parts of the description may look like attributes, but are not, like URLs. To make sure that these are not stripped from the description, add them to
KEYVALUE_ALLOW.
- completion_date¶
datetime.dateof when the task was completed, orNoneif no completion date given
- property contexts¶
A list of all
@contextattributes
- creation_date¶
datetime.dateof when the task was created, orNoneif no creation date given
- description¶
The descriptive portion of the task (i.e. without the completion marker, dates, and priority)
- is_completed¶
Whether or not the task is completed
- linenr¶
Line number of this task within its todo.txt file (0-based; the first task of a file will have
self.linenr == 0). Do not count on this always being set!
- parse(line)¶
(Re)parse the task
lineis the raw string representation of a task, i.e. one line of a todo.txt file.
- priority¶
Priority of the task, or
Noneif no priority given
- property projects¶
A list of all
+projectattributes
- remove_attribute(key, value=None)¶
Remove attribute
keyfrom the taskIf you provide a
valueonly the attribute with that key and value is removed. If novalueis provided all attributes with that key are removed.- Parameters
key – The name of the attribute to remove.
value – The value of the attribute to remove.
- Returns
Trueon success, otherwiseFalse
- remove_context(context)¶
Remove the first
@contextattribute from the description.- Parameters
context – the name of the context attribute to remove, without the leading
@.- Returns
Trueon success, otherwiseFalse.
- remove_project(project)¶
Remove the first
+projectattribute from the description.- Parameters
project – the name of the project attribute to remove, without the leading
+- Returns
Trueon success, otherwiseFalse.
- remove_tag(text, regex)¶
Remove the first attribute
textfrom the description- Parameters
regex – The regular expression to use for matching the right type of attribute.
- replace_attribute(key, value, newvalue)¶
Replace the value of key:value in place with key:newvalue
- replace_context(context, newcontext)¶
Replace the first occurrence of @context with @newcontext
- replace_project(project, newproject)¶
Replace the first occurrence of @project with @newproject
- replace_tag(value, newvalue, regex)¶
Replace the first
valueattribute withnewvalue- Parameters
regex – the regular expression to use for matching the right type of attribute
- todotxt¶
The
TodoTxtinstance that this task belongs to. Do not count on this always being set!
TodoTxtParser¶
- class pytodotxt.TodoTxtParser(encoding='utf-8', task_type=None)¶
A parser for todo.txt-like formatted strings or files
- __init__(encoding='utf-8', task_type=None)¶
Create a new todo.txt parser
- Parameters
encoding – The encoding to assume for the parsing process
task_type – A subclass of
Taskto use when parsing the tasks.
- linesep¶
The line separator. After running any of the parse functions, this property will be set to the line separator that was detected in the parsed object.
- parse(target)¶
Parse the given object
The behaviour of the parser depends a bit on what you pass in. It might be a bit surprising, but if you pass in a
str,parsewill attempt to find tasks in that string.If you want to parse a file and pass the filename, wrap it in
pathlib.Pathfirst or useparse_filedirectly.When parsing is completed, you can query
linesepto see what the line separator was.- Parameters
target (
pathlib.Path,str,bytes, or anyio.IOBase.) – the object to parse- Returns
a list of tasks found in
target
- parse_file(path)¶
Parse the content of the file at
path- Parameters
path – The file to parse
- Returns
a list of tasks found in
file.
- parse_str(text)¶
Parse tasks from the given text
- Parameters
text – The string containing todo.txt-like tasks
- Returns
a list of tasks found in
text
- parse_stream(stream)¶
Parse an io stream
- Parameters
stream – an io stream to read and parse tasks from
- Returns
a list of tasks found in
stream