{"action":"created","comment":{"url":"https://api.github.com/repos/ClusterHQ/eliot/pulls/comments/37183768","id":37183768,"diff_hunk":"@@ -415,6 +424,286 @@ def __exit__(self, type, exception, traceback):\n         self.finish(exception)\n \n \n+class WrongTask(Exception):\n+    \"\"\"\n+    Tried to add a message to an action, but the message was from another\n+    task.\n+    \"\"\"\n+\n+    def __init__(self, action, message):\n+        Exception.__init__(\n+            self, 'Tried to add {} to {}. Expected task_uuid = {}, got {}'.format(\n+                message, action, action.task_uuid, message.task_uuid))\n+\n+\n+class WrongTaskLevel(Exception):\n+    \"\"\"\n+    Tried to add a message to an action, but the task level of the message\n+    indicated that it was not a direct child.\n+    \"\"\"\n+\n+    def __init__(self, action, message):\n+        Exception.__init__(\n+            self, 'Tried to add {} to {}, but {} is not a sibling of {}'.format(\n+                message, action, message.task_level, action.task_level))\n+\n+\n+class WrongActionType(Exception):\n+    \"\"\"\n+    Tried to end a message with a different action_type to the beginning.\n+    \"\"\"\n+\n+    def __init__(self, action, message):\n+        error_msg = 'Tried to end {} with {}. Expected action_type = {}, got {}'\n+        Exception.__init__(\n+            self, error_msg.format(\n+                action, message, action.action_type,\n+                message.contents.get(ACTION_TYPE_FIELD, '<undefined>')))\n+\n+\n+class InvalidStatus(Exception):\n+    \"\"\"\n+    Tried to end a message with an invalid status.\n+    \"\"\"\n+\n+    def __init__(self, action, message):\n+        error_msg = 'Tried to end {} with {}. Expected status {} or {}, got {}'\n+        Exception.__init__(\n+            self, error_msg.format(\n+                action, message, SUCCEEDED_STATUS, FAILED_STATUS,\n+                message.contents.get(ACTION_STATUS_FIELD, '<undefined>')))\n+\n+\n+class DuplicateChild(Exception):\n+    \"\"\"\n+    Tried to add a child to an action that already had a child at that task\n+    level.\n+    \"\"\"\n+\n+    def __init__(self, action, message):\n+        Exception.__init__(\n+            self, 'Tried to add {} to {}, but already had child at {}'.format(\n+                message, action, message.task_level))\n+\n+\n+class InvalidStartMessage(Exception):\n+    \"\"\"\n+    Tried to start an action with an invalid message.\n+    \"\"\"\n+\n+    def __init__(self, message, reason):\n+        Exception.__init__(\n+            self, 'Invalid start message {}: {}'.format(message, reason))\n+\n+    @classmethod\n+    def wrong_status(cls, message):\n+        return cls(message, 'must have status \"STARTED\"')\n+\n+    @classmethod\n+    def wrong_task_level(cls, message):\n+        return cls(message, 'first message must have task level ending in 1')\n+\n+\n+class WrittenAction(PClass):\n+    \"\"\"\n+    An Action that has been logged.\n+\n+    @ivar action_type: The type of the action,\n+        e.g. C{\"yourapp:subsystem:dosomething\"}\n+\n+    @ivar status: One of C{STARTED_STATUS}, C{SUCCEEDED_STATUS}, or\n+        C{FAILED_STATUS}.\n+\n+    @ivar task_uuid: The UUID of the task to which this action belongs.\n+\n+    @ivar TaskLevel task_level: The level of the task in which the action occurs.\n+\n+    @ivar start_time: The Unix timestamp of when the action started.\n+\n+    @ivar end_time: The Unix timestamp of when the action ended, or C{None} if\n+        there has been no end message.\n+\n+    @ivar exception: If the action failed, the name of the exception that was\n+        raised to cause it to fail. If the action succeeded, or hasn't finished\n+        yet, then C{None}.\n+\n+    @ivar reason: The reason the action failed. If the action succeeded, or\n+        hasn't finished yet, then C{None}.\n+\n+    @ivar _children: A L{pvector} of L{WrittenAction} and L{WrittenMessage}\n+        that make up this action.\n+    \"\"\"\n+\n+    # Broad philosophy:\n+    # - Have a way of constructing known-valid instances (at the very least,\n+    #   this is useful for tests)\n+    # - Have a thing that takes messages out of order and hangs on to them\n+    #   until it can construct known-valid instances\n+    # - Be able to recover the original messages\n+\n+    action_type = field(type=unicode, mandatory=True)\n+    status = field(type=unicode, mandatory=True,\n+                   invariant=lambda x: (x in VALID_STATUSES, 'Invalid status'))\n+    task_uuid = field(type=UUID, mandatory=True)\n+    task_level = field(type=TaskLevel, mandatory=True)\n+    start_time = field(type=float, mandatory=True)\n+    end_time = field(type=optional(float), mandatory=True)\n+    # XXX: Actually a map to either WrittenMessage or WrittenAction, but\n+    # pyrsistent doesn't support recursive data types:\n+    # https://github.com/tobgu/pyrsistent/issues/48\n+    _children = pmap_field(TaskLevel, object)\n+    exception = field(type=optional(unicode), mandatory=True)\n+    reason = field(type=optional(unicode), mandatory=True)\n+\n+    # XXX: Possible invariants:\n+    # - start_time <= end_time","path":"eliot/_action.py","position":174,"original_position":174,"commit_id":"47b6ab1586d0eef4a5ce59255fc65ca55774c9c5","original_commit_id":"47b6ab1586d0eef4a5ce59255fc65ca55774c9c5","user":{"login":"jml","id":125674,"avatar_url":"https://avatars.githubusercontent.com/u/125674?v=3","gravatar_id":"","url":"https://api.github.com/users/jml","html_url":"https://github.com/jml","followers_url":"https://api.github.com/users/jml/followers","following_url":"https://api.github.com/users/jml/following{/other_user}","gists_url":"https://api.github.com/users/jml/gists{/gist_id}","starred_url":"https://api.github.com/users/jml/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jml/subscriptions","organizations_url":"https://api.github.com/users/jml/orgs","repos_url":"https://api.github.com/users/jml/repos","events_url":"https://api.github.com/users/jml/events{/privacy}","received_events_url":"https://api.github.com/users/jml/received_events","type":"User","site_admin":false},"body":"Quite. I think it would be an interesting thing for a log display tool to highlight.","created_at":"2015-08-17T12:48:29Z","updated_at":"2015-08-17T12:48:29Z","html_url":"https://github.com/ClusterHQ/eliot/pull/202#discussion_r37183768","pull_request_url":"https://api.github.com/repos/ClusterHQ/eliot/pulls/202","_links":{"self":{"href":"https://api.github.com/repos/ClusterHQ/eliot/pulls/comments/37183768"},"html":{"href":"https://github.com/ClusterHQ/eliot/pull/202#discussion_r37183768"},"pull_request":{"href":"https://api.github.com/repos/ClusterHQ/eliot/pulls/202"}}},"pull_request":{"url":"https://api.github.com/repos/ClusterHQ/eliot/pulls/202","id":42448411,"html_url":"https://github.com/ClusterHQ/eliot/pull/202","diff_url":"https://github.com/ClusterHQ/eliot/pull/202.diff","patch_url":"https://github.com/ClusterHQ/eliot/pull/202.patch","issue_url":"https://api.github.com/repos/ClusterHQ/eliot/issues/202","number":202,"state":"open","locked":false,"title":"WrittenAction implementation","user":{"login":"jml","id":125674,"avatar_url":"https://avatars.githubusercontent.com/u/125674?v=3","gravatar_id":"","url":"https://api.github.com/users/jml","html_url":"https://github.com/jml","followers_url":"https://api.github.com/users/jml/followers","following_url":"https://api.github.com/users/jml/following{/other_user}","gists_url":"https://api.github.com/users/jml/gists{/gist_id}","starred_url":"https://api.github.com/users/jml/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jml/subscriptions","organizations_url":"https://api.github.com/users/jml/orgs","repos_url":"https://api.github.com/users/jml/repos","events_url":"https://api.github.com/users/jml/events{/privacy}","received_events_url":"https://api.github.com/users/jml/received_events","type":"User","site_admin":false},"body":"Adds a `WrittenAction` implementation to parallel the `WrittenMessage` implementation. \r\n\r\nThe focus is on being able to make something that represents an action that you can trust. Another way to express this is that I'm trying to provide a definition in Python of what an action actually is (rather than a way of writing out an action). This means there's a hefty emphasis on validation.\r\n\r\nAs yet does not provide the ability to assemble a `WrittenAction` from a stream of messages, nor to assemble nested `WrittenAction` objects. I can add those if you'd like, but I thought the patch was big enough as it stands to be a reviewable chunk. Also, if what I think an action is is wrong, then there's not much point going further.\r\n\r\nThe tests are all written with Hypothesis. I don't know if this is actually a good idea. Although it looks like I'm adding 13 tests, I'm actually adding 2600. Currently the test suite takes ~34s on my laptop.\r\n\r\nI've also introduced a testtools dependency, because Itamar implied that would be OK. It makes a lot of the aggregate assertions nicer.\r\n\r\nThis branch incorporates changes from #200 and would be much easier to review if that were merged.\r\n\r\n<!-- Reviewable:start -->\r\n[<img src=\"https://reviewable.io/review_button.png\" height=40 alt=\"Review on Reviewable\"/>](https://reviewable.io/reviews/clusterhq/eliot/202)\r\n<!-- Reviewable:end -->\r\n","created_at":"2015-08-14T11:54:40Z","updated_at":"2015-08-17T12:48:29Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d743fd6e1d5b4eece0f4df34fd569256911d2de4","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ClusterHQ/eliot/pulls/202/commits","review_comments_url":"https://api.github.com/repos/ClusterHQ/eliot/pulls/202/comments","review_comment_url":"https://api.github.com/repos/ClusterHQ/eliot/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ClusterHQ/eliot/issues/202/comments","statuses_url":"https://api.github.com/repos/ClusterHQ/eliot/statuses/47b6ab1586d0eef4a5ce59255fc65ca55774c9c5","head":{"label":"ClusterHQ:parse-actions-for-real","ref":"parse-actions-for-real","sha":"47b6ab1586d0eef4a5ce59255fc65ca55774c9c5","user":{"login":"ClusterHQ","id":7913449,"avatar_url":"https://avatars.githubusercontent.com/u/7913449?v=3","gravatar_id":"","url":"https://api.github.com/users/ClusterHQ","html_url":"https://github.com/ClusterHQ","followers_url":"https://api.github.com/users/ClusterHQ/followers","following_url":"https://api.github.com/users/ClusterHQ/following{/other_user}","gists_url":"https://api.github.com/users/ClusterHQ/gists{/gist_id}","starred_url":"https://api.github.com/users/ClusterHQ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ClusterHQ/subscriptions","organizations_url":"https://api.github.com/users/ClusterHQ/orgs","repos_url":"https://api.github.com/users/ClusterHQ/repos","events_url":"https://api.github.com/users/ClusterHQ/events{/privacy}","received_events_url":"https://api.github.com/users/ClusterHQ/received_events","type":"Organization","site_admin":false},"repo":{"id":18601900,"name":"eliot","full_name":"ClusterHQ/eliot","owner":{"login":"ClusterHQ","id":7913449,"avatar_url":"https://avatars.githubusercontent.com/u/7913449?v=3","gravatar_id":"","url":"https://api.github.com/users/ClusterHQ","html_url":"https://github.com/ClusterHQ","followers_url":"https://api.github.com/users/ClusterHQ/followers","following_url":"https://api.github.com/users/ClusterHQ/following{/other_user}","gists_url":"https://api.github.com/users/ClusterHQ/gists{/gist_id}","starred_url":"https://api.github.com/users/ClusterHQ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ClusterHQ/subscriptions","organizations_url":"https://api.github.com/users/ClusterHQ/orgs","repos_url":"https://api.github.com/users/ClusterHQ/repos","events_url":"https://api.github.com/users/ClusterHQ/events{/privacy}","received_events_url":"https://api.github.com/users/ClusterHQ/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ClusterHQ/eliot","description":"Eliot: Logging for Complex & Distributed Systems","fork":false,"url":"https://api.github.com/repos/ClusterHQ/eliot","forks_url":"https://api.github.com/repos/ClusterHQ/eliot/forks","keys_url":"https://api.github.com/repos/ClusterHQ/eliot/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ClusterHQ/eliot/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ClusterHQ/eliot/teams","hooks_url":"https://api.github.com/repos/ClusterHQ/eliot/hooks","issue_events_url":"https://api.github.com/repos/ClusterHQ/eliot/issues/events{/number}","events_url":"https://api.github.com/repos/ClusterHQ/eliot/events","assignees_url":"https://api.github.com/repos/ClusterHQ/eliot/assignees{/user}","branches_url":"https://api.github.com/repos/ClusterHQ/eliot/branches{/branch}","tags_url":"https://api.github.com/repos/ClusterHQ/eliot/tags","blobs_url":"https://api.github.com/repos/ClusterHQ/eliot/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ClusterHQ/eliot/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ClusterHQ/eliot/git/refs{/sha}","trees_url":"https://api.github.com/repos/ClusterHQ/eliot/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ClusterHQ/eliot/statuses/{sha}","languages_url":"https://api.github.com/repos/ClusterHQ/eliot/languages","stargazers_url":"https://api.github.com/repos/ClusterHQ/eliot/stargazers","contributors_url":"https://api.github.com/repos/ClusterHQ/eliot/contributors","subscribers_url":"https://api.github.com/repos/ClusterHQ/eliot/subscribers","subscription_url":"https://api.github.com/repos/ClusterHQ/eliot/subscription","commits_url":"https://api.github.com/repos/ClusterHQ/eliot/commits{/sha}","git_commits_url":"https://api.github.com/repos/ClusterHQ/eliot/git/commits{/sha}","comments_url":"https://api.github.com/repos/ClusterHQ/eliot/comments{/number}","issue_comment_url":"https://api.github.com/repos/ClusterHQ/eliot/issues/comments{/number}","contents_url":"https://api.github.com/repos/ClusterHQ/eliot/contents/{+path}","compare_url":"https://api.github.com/repos/ClusterHQ/eliot/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ClusterHQ/eliot/merges","archive_url":"https://api.github.com/repos/ClusterHQ/eliot/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ClusterHQ/eliot/downloads","issues_url":"https://api.github.com/repos/ClusterHQ/eliot/issues{/number}","pulls_url":"https://api.github.com/repos/ClusterHQ/eliot/pulls{/number}","milestones_url":"https://api.github.com/repos/ClusterHQ/eliot/milestones{/number}","notifications_url":"https://api.github.com/repos/ClusterHQ/eliot/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ClusterHQ/eliot/labels{/name}","releases_url":"https://api.github.com/repos/ClusterHQ/eliot/releases{/id}","created_at":"2014-04-09T15:12:19Z","updated_at":"2015-08-14T08:02:47Z","pushed_at":"2015-08-14T20:37:20Z","git_url":"git://github.com/ClusterHQ/eliot.git","ssh_url":"git@github.com:ClusterHQ/eliot.git","clone_url":"https://github.com/ClusterHQ/eliot.git","svn_url":"https://github.com/ClusterHQ/eliot","homepage":"http://pypi.python.org/pypi/eliot","size":1512,"stargazers_count":120,"watchers_count":120,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":12,"mirror_url":null,"open_issues_count":76,"forks":12,"open_issues":76,"watchers":120,"default_branch":"master"}},"base":{"label":"ClusterHQ:master","ref":"master","sha":"7e43088db03726c0efa00485a0858c8fa75df85e","user":{"login":"ClusterHQ","id":7913449,"avatar_url":"https://avatars.githubusercontent.com/u/7913449?v=3","gravatar_id":"","url":"https://api.github.com/users/ClusterHQ","html_url":"https://github.com/ClusterHQ","followers_url":"https://api.github.com/users/ClusterHQ/followers","following_url":"https://api.github.com/users/ClusterHQ/following{/other_user}","gists_url":"https://api.github.com/users/ClusterHQ/gists{/gist_id}","starred_url":"https://api.github.com/users/ClusterHQ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ClusterHQ/subscriptions","organizations_url":"https://api.github.com/users/ClusterHQ/orgs","repos_url":"https://api.github.com/users/ClusterHQ/repos","events_url":"https://api.github.com/users/ClusterHQ/events{/privacy}","received_events_url":"https://api.github.com/users/ClusterHQ/received_events","type":"Organization","site_admin":false},"repo":{"id":18601900,"name":"eliot","full_name":"ClusterHQ/eliot","owner":{"login":"ClusterHQ","id":7913449,"avatar_url":"https://avatars.githubusercontent.com/u/7913449?v=3","gravatar_id":"","url":"https://api.github.com/users/ClusterHQ","html_url":"https://github.com/ClusterHQ","followers_url":"https://api.github.com/users/ClusterHQ/followers","following_url":"https://api.github.com/users/ClusterHQ/following{/other_user}","gists_url":"https://api.github.com/users/ClusterHQ/gists{/gist_id}","starred_url":"https://api.github.com/users/ClusterHQ/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ClusterHQ/subscriptions","organizations_url":"https://api.github.com/users/ClusterHQ/orgs","repos_url":"https://api.github.com/users/ClusterHQ/repos","events_url":"https://api.github.com/users/ClusterHQ/events{/privacy}","received_events_url":"https://api.github.com/users/ClusterHQ/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ClusterHQ/eliot","description":"Eliot: Logging for Complex & Distributed Systems","fork":false,"url":"https://api.github.com/repos/ClusterHQ/eliot","forks_url":"https://api.github.com/repos/ClusterHQ/eliot/forks","keys_url":"https://api.github.com/repos/ClusterHQ/eliot/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ClusterHQ/eliot/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ClusterHQ/eliot/teams","hooks_url":"https://api.github.com/repos/ClusterHQ/eliot/hooks","issue_events_url":"https://api.github.com/repos/ClusterHQ/eliot/issues/events{/number}","events_url":"https://api.github.com/repos/ClusterHQ/eliot/events","assignees_url":"https://api.github.com/repos/ClusterHQ/eliot/assignees{/user}","branches_url":"https://api.github.com/repos/ClusterHQ/eliot/branches{/branch}","tags_url":"https://api.github.com/repos/ClusterHQ/eliot/tags","blobs_url":"https://api.github.com/repos/ClusterHQ/eliot/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ClusterHQ/eliot/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ClusterHQ/eliot/git/refs{/sha}","trees_url":"https://api.github.com/repos/ClusterHQ/eliot/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ClusterHQ/eliot/statuses/{sha}","languages_url":"https://api.github.com/repos/ClusterHQ/eliot/languages","stargazers_url":"https://api.github.com/repos/ClusterHQ/eliot/stargazers","contributors_url":"https://api.github.com/repos/ClusterHQ/eliot/contributors","subscribers_url":"https://api.github.com/repos/ClusterHQ/eliot/subscribers","subscription_url":"https://api.github.com/repos/ClusterHQ/eliot/subscription","commits_url":"https://api.github.com/repos/ClusterHQ/eliot/commits{/sha}","git_commits_url":"https://api.github.com/repos/ClusterHQ/eliot/git/commits{/sha}","comments_url":"https://api.github.com/repos/ClusterHQ/eliot/comments{/number}","issue_comment_url":"https://api.github.com/repos/ClusterHQ/eliot/issues/comments{/number}","contents_url":"https://api.github.com/repos/ClusterHQ/eliot/contents/{+path}","compare_url":"https://api.github.com/repos/ClusterHQ/eliot/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ClusterHQ/eliot/merges","archive_url":"https://api.github.com/repos/ClusterHQ/eliot/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ClusterHQ/eliot/downloads","issues_url":"https://api.github.com/repos/ClusterHQ/eliot/issues{/number}","pulls_url":"https://api.github.com/repos/ClusterHQ/eliot/pulls{/number}","milestones_url":"https://api.github.com/repos/ClusterHQ/eliot/milestones{/number}","notifications_url":"https://api.github.com/repos/ClusterHQ/eliot/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ClusterHQ/eliot/labels{/name}","releases_url":"https://api.github.com/repos/ClusterHQ/eliot/releases{/id}","created_at":"2014-04-09T15:12:19Z","updated_at":"2015-08-14T08:02:47Z","pushed_at":"2015-08-14T20:37:20Z","git_url":"git://github.com/ClusterHQ/eliot.git","ssh_url":"git@github.com:ClusterHQ/eliot.git","clone_url":"https://github.com/ClusterHQ/eliot.git","svn_url":"https://github.com/ClusterHQ/eliot","homepage":"http://pypi.python.org/pypi/eliot","size":1512,"stargazers_count":120,"watchers_count":120,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":12,"mirror_url":null,"open_issues_count":76,"forks":12,"open_issues":76,"watchers":120,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ClusterHQ/eliot/pulls/202"},"html":{"href":"https://github.com/ClusterHQ/eliot/pull/202"},"issue":{"href":"https://api.github.com/repos/ClusterHQ/eliot/issues/202"},"comments":{"href":"https://api.github.com/repos/ClusterHQ/eliot/issues/202/comments"},"review_comments":{"href":"https://api.github.com/repos/ClusterHQ/eliot/pulls/202/comments"},"review_comment":{"href":"https://api.github.com/repos/ClusterHQ/eliot/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ClusterHQ/eliot/pulls/202/commits"},"statuses":{"href":"https://api.github.com/repos/ClusterHQ/eliot/statuses/47b6ab1586d0eef4a5ce59255fc65ca55774c9c5"}}}}