{"action":"created","comment":{"url":"https://api.github.com/repos/evhub/coconut/pulls/comments/75206352","id":75206352,"diff_hunk":"@@ -0,0 +1,174 @@\n+#!/usr/bin/env python\n+\n+#-----------------------------------------------------------------------------------------------------------------------\n+# INFO:\n+#-----------------------------------------------------------------------------------------------------------------------\n+\n+\"\"\"\n+Authors: Evan Hubinger, Fred Buchanan\n+License: Apache 2.0\n+Description: Utility functions for the main command module.\n+\"\"\"\n+\n+#-----------------------------------------------------------------------------------------------------------------------\n+# IMPORTS:\n+#-----------------------------------------------------------------------------------------------------------------------\n+\n+from __future__ import print_function, absolute_import, unicode_literals, division\n+\n+from coconut.const import default_encoding, color_codes, end_color_code\n+from coconut.compiler.exceptions import printerr\n+from coconut.root import *\n+\n+import os\n+import traceback\n+\n+#-----------------------------------------------------------------------------------------------------------------------\n+# FUNCTIONS:\n+#-----------------------------------------------------------------------------------------------------------------------\n+\n+\n+def openfile(filename, opentype=\"r+\"):\n+    \"\"\"Returns an open file object.\"\"\"\n+    return open(filename, opentype, encoding=default_encoding) # using open from .root\n+\n+def writefile(openedfile, newcontents):\n+    \"\"\"Sets the contents of a file.\"\"\"\n+    openedfile.seek(0)\n+    openedfile.truncate()\n+    openedfile.write(newcontents)\n+\n+def readfile(openedfile):\n+    \"\"\"Reads the contents of a file.\"\"\"\n+    openedfile.seek(0)\n+    return str(openedfile.read())\n+\n+def showpath(path):\n+    \"\"\"Formats a path for displaying.\"\"\"\n+    return os.path.basename(path)\n+\n+def fixpath(path):\n+    \"\"\"Uniformly formats a path.\"\"\"\n+    return os.path.normpath(os.path.realpath(path))\n+\n+def rem_encoding(code):\n+    \"\"\"Removes encoding declarations from Python code so it can be passed to exec.\"\"\"\n+    old_lines = code.splitlines()\n+    new_lines = []\n+    for i in range(min(2, len(old_lines))):\n+        line = old_lines[i]\n+        if not (line.startswith(\"#\") and \"coding\" in line):\n+            new_lines.append(line)\n+    new_lines += old_lines[2:]\n+    return \"\\n\".join(new_lines)\n+\n+def try_eval(code, in_vars):\n+    \"\"\"Try to evaluate the given code, otherwise execute it.\"\"\"\n+    try:\n+        return eval(code, in_vars)\n+    except SyntaxError:\n+        pass # exit the exception context before executing code\n+    exec(code, in_vars)\n+\n+def escape_color(code):\n+    \"\"\"Generates an ANSII color code.\"\"\"\n+    return \"\\033[\" + str(code) + \"m\"\n+\n+#-----------------------------------------------------------------------------------------------------------------------\n+# CLASSES:\n+#-----------------------------------------------------------------------------------------------------------------------\n+\n+class Runner(object):\n+    \"\"\"Compiled Python executor.\"\"\"\n+    def __init__(self, proc=None, exit=None, path=None):\n+        \"\"\"Creates the executor.\"\"\"\n+        self.exit = exit\n+        self.vars = {\"__name__\": \"__main__\"}\n+        if path is not None:\n+            self.vars[\"__file__\"] = fixpath(path)\n+        if proc is not None:\n+            self.run(proc.headers(\"code\"))\n+            self.fixpickle()\n+\n+    def fixpickle(self):\n+        \"\"\"Fixes pickling of Coconut header objects.\"\"\"\n+        from coconut import __coconut__\n+        for var in self.vars:\n+            if not var.startswith(\"__\") and var in dir(__coconut__):\n+                self.vars[var] = getattr(__coconut__, var)\n+\n+    def run(self, code, err=False, run_func=None):\n+        \"\"\"Executes Python code.\"\"\"\n+        try:\n+            if run_func is None:\n+                exec(code, self.vars)\n+            else:\n+                return run_func(code, self.vars)\n+        except (Exception, KeyboardInterrupt):\n+            if err:\n+                raise\n+            else:\n+                traceback.print_exc()\n+        except SystemExit:\n+            if self.exit is None:\n+                raise\n+            else:\n+                self.exit()\n+\n+class Console(object):\n+    \"\"\"Manages printing and reading data to the console.\"\"\"\n+    color_code = None\n+    on = True\n+\n+    def __init__(self, main_sig=\"\", debug_sig=\"\"):\n+        \"\"\"Creates the console.\"\"\"\n+        self.main_sig, self.debug_sig = main_sig, debug_sig\n+\n+    def set_color(self, color=None):\n+        \"\"\"Set output color.\"\"\"\n+        if color:\n+            color = color.replace(\"_\", \"\")\n+            if color in color_codes:\n+                self.color_code = color_codes[color]\n+            else:\n+                try:\n+                    color = int(color)\n+                except ValueError:\n+                    raise CoconutException('unrecognized color \"'+color+'\" (enter a valid color name or code)')\n+                else:\n+                    if 0 < color <= 256:\n+                        self.color_code = color\n+                    else:\n+                        raise CoconutException('color code '+str(color)+' out of range (must obey 0 < color code <= 256)')\n+        else:\n+            self.color_code = None\n+\n+    def add_color(self, inputstring):\n+        \"\"\"Adds the specified color to the string.\"\"\"\n+        if self.color_code is None:\n+            return inputstring\n+        else:\n+            return escape_color(self.color_code) + inputstring + escape_color(end_color_code)\n+\n+    def display(self, messages, sig=\"\", debug=False):\n+        \"\"\"Prints messages.\"\"\"\n+        if self.on:\n+            message = \" \".join(str(msg) for msg in messages)\n+            for line in message.splitlines():\n+                msg = self.add_color(sig + line)\n+                if debug is True:\n+                    printerr(msg)\n+                else:\n+                    print(msg)\n+\n+    def print(self, *messages):\n+        \"\"\"Prints messages with color.\"\"\"\n+        self.display(messages)\n+\n+    def printerr(self, *messages):\n+        \"\"\"Prints error messages with color and debug signature.\"\"\"\n+        self.display(messages, self.debug_sig, True)\n+\n+    def show(self, *messages):\n+        \"\"\"Prints messages with color and main signature.\"\"\"\n+        self.display(messages, self.main_sig)","path":"coconut/command/utils.py","position":174,"original_position":174,"commit_id":"b29a0ab2ac9f2cce94a95f5f7ac0aa03176ff857","original_commit_id":"b29a0ab2ac9f2cce94a95f5f7ac0aa03176ff857","user":{"login":"evhub","id":1337598,"avatar_url":"https://avatars.githubusercontent.com/u/1337598?v=3","gravatar_id":"","url":"https://api.github.com/users/evhub","html_url":"https://github.com/evhub","followers_url":"https://api.github.com/users/evhub/followers","following_url":"https://api.github.com/users/evhub/following{/other_user}","gists_url":"https://api.github.com/users/evhub/gists{/gist_id}","starred_url":"https://api.github.com/users/evhub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/evhub/subscriptions","organizations_url":"https://api.github.com/users/evhub/orgs","repos_url":"https://api.github.com/users/evhub/repos","events_url":"https://api.github.com/users/evhub/events{/privacy}","received_events_url":"https://api.github.com/users/evhub/received_events","type":"User","site_admin":false},"body":"Please make sure all files end with a newline. In Sublime, this can be automated with `\"ensure_newline_at_eof_on_save\": true`.","created_at":"2016-08-17T21:01:21Z","updated_at":"2016-08-17T21:01:21Z","html_url":"https://github.com/evhub/coconut/pull/145#discussion_r75206352","pull_request_url":"https://api.github.com/repos/evhub/coconut/pulls/145","_links":{"self":{"href":"https://api.github.com/repos/evhub/coconut/pulls/comments/75206352"},"html":{"href":"https://github.com/evhub/coconut/pull/145#discussion_r75206352"},"pull_request":{"href":"https://api.github.com/repos/evhub/coconut/pulls/145"}}},"pull_request":{"url":"https://api.github.com/repos/evhub/coconut/pulls/145","id":81353798,"html_url":"https://github.com/evhub/coconut/pull/145","diff_url":"https://github.com/evhub/coconut/pull/145.diff","patch_url":"https://github.com/evhub/coconut/pull/145.patch","issue_url":"https://api.github.com/repos/evhub/coconut/issues/145","number":145,"state":"open","locked":false,"title":"Refactored command.py and compiler.py into modules with multiple files","user":{"login":"Boscillator","id":8217910,"avatar_url":"https://avatars.githubusercontent.com/u/8217910?v=3","gravatar_id":"","url":"https://api.github.com/users/Boscillator","html_url":"https://github.com/Boscillator","followers_url":"https://api.github.com/users/Boscillator/followers","following_url":"https://api.github.com/users/Boscillator/following{/other_user}","gists_url":"https://api.github.com/users/Boscillator/gists{/gist_id}","starred_url":"https://api.github.com/users/Boscillator/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Boscillator/subscriptions","organizations_url":"https://api.github.com/users/Boscillator/orgs","repos_url":"https://api.github.com/users/Boscillator/repos","events_url":"https://api.github.com/users/Boscillator/events{/privacy}","received_events_url":"https://api.github.com/users/Boscillator/received_events","type":"User","site_admin":false},"body":"Moved the utilities to another file","created_at":"2016-08-15T18:33:11Z","updated_at":"2016-08-17T21:01:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"1a16a7a2a534249ee969f13e92c4aa0c97bfa16f","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/evhub/coconut/pulls/145/commits","review_comments_url":"https://api.github.com/repos/evhub/coconut/pulls/145/comments","review_comment_url":"https://api.github.com/repos/evhub/coconut/pulls/comments{/number}","comments_url":"https://api.github.com/repos/evhub/coconut/issues/145/comments","statuses_url":"https://api.github.com/repos/evhub/coconut/statuses/b29a0ab2ac9f2cce94a95f5f7ac0aa03176ff857","head":{"label":"Boscillator:develop","ref":"develop","sha":"b29a0ab2ac9f2cce94a95f5f7ac0aa03176ff857","user":{"login":"Boscillator","id":8217910,"avatar_url":"https://avatars.githubusercontent.com/u/8217910?v=3","gravatar_id":"","url":"https://api.github.com/users/Boscillator","html_url":"https://github.com/Boscillator","followers_url":"https://api.github.com/users/Boscillator/followers","following_url":"https://api.github.com/users/Boscillator/following{/other_user}","gists_url":"https://api.github.com/users/Boscillator/gists{/gist_id}","starred_url":"https://api.github.com/users/Boscillator/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Boscillator/subscriptions","organizations_url":"https://api.github.com/users/Boscillator/orgs","repos_url":"https://api.github.com/users/Boscillator/repos","events_url":"https://api.github.com/users/Boscillator/events{/privacy}","received_events_url":"https://api.github.com/users/Boscillator/received_events","type":"User","site_admin":false},"repo":{"id":65681410,"name":"coconut","full_name":"Boscillator/coconut","owner":{"login":"Boscillator","id":8217910,"avatar_url":"https://avatars.githubusercontent.com/u/8217910?v=3","gravatar_id":"","url":"https://api.github.com/users/Boscillator","html_url":"https://github.com/Boscillator","followers_url":"https://api.github.com/users/Boscillator/followers","following_url":"https://api.github.com/users/Boscillator/following{/other_user}","gists_url":"https://api.github.com/users/Boscillator/gists{/gist_id}","starred_url":"https://api.github.com/users/Boscillator/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Boscillator/subscriptions","organizations_url":"https://api.github.com/users/Boscillator/orgs","repos_url":"https://api.github.com/users/Boscillator/repos","events_url":"https://api.github.com/users/Boscillator/events{/privacy}","received_events_url":"https://api.github.com/users/Boscillator/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Boscillator/coconut","description":"Simple, elegant, Pythonic functional programming.","fork":true,"url":"https://api.github.com/repos/Boscillator/coconut","forks_url":"https://api.github.com/repos/Boscillator/coconut/forks","keys_url":"https://api.github.com/repos/Boscillator/coconut/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Boscillator/coconut/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Boscillator/coconut/teams","hooks_url":"https://api.github.com/repos/Boscillator/coconut/hooks","issue_events_url":"https://api.github.com/repos/Boscillator/coconut/issues/events{/number}","events_url":"https://api.github.com/repos/Boscillator/coconut/events","assignees_url":"https://api.github.com/repos/Boscillator/coconut/assignees{/user}","branches_url":"https://api.github.com/repos/Boscillator/coconut/branches{/branch}","tags_url":"https://api.github.com/repos/Boscillator/coconut/tags","blobs_url":"https://api.github.com/repos/Boscillator/coconut/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Boscillator/coconut/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Boscillator/coconut/git/refs{/sha}","trees_url":"https://api.github.com/repos/Boscillator/coconut/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Boscillator/coconut/statuses/{sha}","languages_url":"https://api.github.com/repos/Boscillator/coconut/languages","stargazers_url":"https://api.github.com/repos/Boscillator/coconut/stargazers","contributors_url":"https://api.github.com/repos/Boscillator/coconut/contributors","subscribers_url":"https://api.github.com/repos/Boscillator/coconut/subscribers","subscription_url":"https://api.github.com/repos/Boscillator/coconut/subscription","commits_url":"https://api.github.com/repos/Boscillator/coconut/commits{/sha}","git_commits_url":"https://api.github.com/repos/Boscillator/coconut/git/commits{/sha}","comments_url":"https://api.github.com/repos/Boscillator/coconut/comments{/number}","issue_comment_url":"https://api.github.com/repos/Boscillator/coconut/issues/comments{/number}","contents_url":"https://api.github.com/repos/Boscillator/coconut/contents/{+path}","compare_url":"https://api.github.com/repos/Boscillator/coconut/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Boscillator/coconut/merges","archive_url":"https://api.github.com/repos/Boscillator/coconut/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Boscillator/coconut/downloads","issues_url":"https://api.github.com/repos/Boscillator/coconut/issues{/number}","pulls_url":"https://api.github.com/repos/Boscillator/coconut/pulls{/number}","milestones_url":"https://api.github.com/repos/Boscillator/coconut/milestones{/number}","notifications_url":"https://api.github.com/repos/Boscillator/coconut/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Boscillator/coconut/labels{/name}","releases_url":"https://api.github.com/repos/Boscillator/coconut/releases{/id}","deployments_url":"https://api.github.com/repos/Boscillator/coconut/deployments","created_at":"2016-08-14T18:48:45Z","updated_at":"2016-08-14T18:48:46Z","pushed_at":"2016-08-16T18:22:58Z","git_url":"git://github.com/Boscillator/coconut.git","ssh_url":"git@github.com:Boscillator/coconut.git","clone_url":"https://github.com/Boscillator/coconut.git","svn_url":"https://github.com/Boscillator/coconut","homepage":"http://coconut-lang.org","size":1600,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"evhub:develop","ref":"develop","sha":"70ad82a2c0ecb247a5beff2664b18a0c5298dab6","user":{"login":"evhub","id":1337598,"avatar_url":"https://avatars.githubusercontent.com/u/1337598?v=3","gravatar_id":"","url":"https://api.github.com/users/evhub","html_url":"https://github.com/evhub","followers_url":"https://api.github.com/users/evhub/followers","following_url":"https://api.github.com/users/evhub/following{/other_user}","gists_url":"https://api.github.com/users/evhub/gists{/gist_id}","starred_url":"https://api.github.com/users/evhub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/evhub/subscriptions","organizations_url":"https://api.github.com/users/evhub/orgs","repos_url":"https://api.github.com/users/evhub/repos","events_url":"https://api.github.com/users/evhub/events{/privacy}","received_events_url":"https://api.github.com/users/evhub/received_events","type":"User","site_admin":false},"repo":{"id":24804430,"name":"coconut","full_name":"evhub/coconut","owner":{"login":"evhub","id":1337598,"avatar_url":"https://avatars.githubusercontent.com/u/1337598?v=3","gravatar_id":"","url":"https://api.github.com/users/evhub","html_url":"https://github.com/evhub","followers_url":"https://api.github.com/users/evhub/followers","following_url":"https://api.github.com/users/evhub/following{/other_user}","gists_url":"https://api.github.com/users/evhub/gists{/gist_id}","starred_url":"https://api.github.com/users/evhub/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/evhub/subscriptions","organizations_url":"https://api.github.com/users/evhub/orgs","repos_url":"https://api.github.com/users/evhub/repos","events_url":"https://api.github.com/users/evhub/events{/privacy}","received_events_url":"https://api.github.com/users/evhub/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/evhub/coconut","description":"Simple, elegant, Pythonic functional programming.","fork":false,"url":"https://api.github.com/repos/evhub/coconut","forks_url":"https://api.github.com/repos/evhub/coconut/forks","keys_url":"https://api.github.com/repos/evhub/coconut/keys{/key_id}","collaborators_url":"https://api.github.com/repos/evhub/coconut/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/evhub/coconut/teams","hooks_url":"https://api.github.com/repos/evhub/coconut/hooks","issue_events_url":"https://api.github.com/repos/evhub/coconut/issues/events{/number}","events_url":"https://api.github.com/repos/evhub/coconut/events","assignees_url":"https://api.github.com/repos/evhub/coconut/assignees{/user}","branches_url":"https://api.github.com/repos/evhub/coconut/branches{/branch}","tags_url":"https://api.github.com/repos/evhub/coconut/tags","blobs_url":"https://api.github.com/repos/evhub/coconut/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/evhub/coconut/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/evhub/coconut/git/refs{/sha}","trees_url":"https://api.github.com/repos/evhub/coconut/git/trees{/sha}","statuses_url":"https://api.github.com/repos/evhub/coconut/statuses/{sha}","languages_url":"https://api.github.com/repos/evhub/coconut/languages","stargazers_url":"https://api.github.com/repos/evhub/coconut/stargazers","contributors_url":"https://api.github.com/repos/evhub/coconut/contributors","subscribers_url":"https://api.github.com/repos/evhub/coconut/subscribers","subscription_url":"https://api.github.com/repos/evhub/coconut/subscription","commits_url":"https://api.github.com/repos/evhub/coconut/commits{/sha}","git_commits_url":"https://api.github.com/repos/evhub/coconut/git/commits{/sha}","comments_url":"https://api.github.com/repos/evhub/coconut/comments{/number}","issue_comment_url":"https://api.github.com/repos/evhub/coconut/issues/comments{/number}","contents_url":"https://api.github.com/repos/evhub/coconut/contents/{+path}","compare_url":"https://api.github.com/repos/evhub/coconut/compare/{base}...{head}","merges_url":"https://api.github.com/repos/evhub/coconut/merges","archive_url":"https://api.github.com/repos/evhub/coconut/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/evhub/coconut/downloads","issues_url":"https://api.github.com/repos/evhub/coconut/issues{/number}","pulls_url":"https://api.github.com/repos/evhub/coconut/pulls{/number}","milestones_url":"https://api.github.com/repos/evhub/coconut/milestones{/number}","notifications_url":"https://api.github.com/repos/evhub/coconut/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/evhub/coconut/labels{/name}","releases_url":"https://api.github.com/repos/evhub/coconut/releases{/id}","deployments_url":"https://api.github.com/repos/evhub/coconut/deployments","created_at":"2014-10-04T23:43:11Z","updated_at":"2016-08-16T01:12:14Z","pushed_at":"2016-08-17T20:58:53Z","git_url":"git://github.com/evhub/coconut.git","ssh_url":"git@github.com:evhub/coconut.git","clone_url":"https://github.com/evhub/coconut.git","svn_url":"https://github.com/evhub/coconut","homepage":"http://coconut-lang.org","size":1611,"stargazers_count":705,"watchers_count":705,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":24,"mirror_url":null,"open_issues_count":18,"forks":24,"open_issues":18,"watchers":705,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/evhub/coconut/pulls/145"},"html":{"href":"https://github.com/evhub/coconut/pull/145"},"issue":{"href":"https://api.github.com/repos/evhub/coconut/issues/145"},"comments":{"href":"https://api.github.com/repos/evhub/coconut/issues/145/comments"},"review_comments":{"href":"https://api.github.com/repos/evhub/coconut/pulls/145/comments"},"review_comment":{"href":"https://api.github.com/repos/evhub/coconut/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/evhub/coconut/pulls/145/commits"},"statuses":{"href":"https://api.github.com/repos/evhub/coconut/statuses/b29a0ab2ac9f2cce94a95f5f7ac0aa03176ff857"}}}}
{"actor":{"display_login":"evhub"}}