Skip to content

write()

tlras edited this page Jan 24, 2022 · 8 revisions

The write() function will write the provided content to the page.

Syntax

write(pagename, content) OR
write(pagename, content, conflict)

Parameters

  • pagename — The name of the page that you want to edit.
  • content — A string containing what you want to page to be.
  • conflict — The timestamp of when the page was last retrieved. If provided, it will raise a ConflictError if the page has been updated since then to prevent overwriting new edits.

Return value

The timestamp that the edit is recorded as having on the server. Can be used for calculating latency or passing as the conflict parameter to a later write() call.

Warning: The ETT API prohibits you from writing content to a page if it is identical to what it already contains. If this condition occurs, write() will raise an UnmodifiedError.

Example

# Add a line to /chatroom from your terminal!
import ettil
from sys import exit, stderr

page = ettil.get("chatroom")
content = page.content.rstrip()
print("{}\n".format(content))

line = input("> ")
new_content = "{}\n\n{}\n\n".format(content, line)

try:
    try:
        ettil.write("chatroom", new_content)
    except ettil.ConflictError:
        while True:
            confirm = input(
                "Page has been edited since last seen, continue? [y/N]: "
            ).strip().lower()
            
            if confirm == "y":
                ettil.write("chatroom", new_content, page.timestamp)
            elif confirm == "" or confirm == "n":
                pass
            else:
                print("Response not understood, try again.")
                continue
except ettil.WriteError:
    print("Could not edit this page!", file=stderr)
    exit(1)

Functions

Exceptions

  • WriteError
  • ConflictError
  • UnmodifiedError

 

Clone this wiki locally