예제 #1
0
파일: app.py 프로젝트: einalex/todo
    def push(self):
        """Push todo to gist.github.com"""
        github = Github()
        gist_id = GistId().get()
        token = GithubToken().get()

        github.login(token)

        if not self.todo.name:
            name = "Todo"
        else:
            name = self.todo.name

        files = {
            name: {
                "content": self.todo_content
            }
        }

        log.info(
            "Pushing '%s' to https://gist.github.com/%s .." % (name, gist_id)
        )
        response = github.edit_gist(gist_id, files=files)

        if response.status_code == 200:
            log.ok("Pushed success.")
        elif response.status_code == 401:
            log.warning("Github token out of date, empty the old token")
            GithubToken().save('')  # empty the token!
            self.push()  # and repush
        else:
            log.error("Pushed failed. %d" % response.status_code)
예제 #2
0
파일: app.py 프로젝트: einalex/todo
    def get(self, answer=None):  # add answer to figure which approach to go
        """
        call this method to get a gist_id::
            gist_id = GistId().get()
        what the get() dose:
          1) read the gist_id from file "~/.todo/gist_id"
          2) check if the gist_id if empty
             (yes)-> 1) if the gist_id is empty, ask user to input it or
                     new a gist directly.
                     2) save the new gist_id
          3) return the gist_id
        """

        if self.is_empty:
            print "Tell me the gist's id you want to push to:"
            print " 1. New a gist right now."
            print " 2. Let me input a gist's id."
            if answer is None:
                answer = ask_input.text("Input your answer(1/2):")
            if answer == '2':
                self.save(ask_input.text("Gist id:"))
            elif answer == '1':
                # new a gist
                todo_content = TodoTxt().read()
                todo = parser.parse(todo_content)
                if not todo.name:
                    name = "Todo"
                else:
                    name = todo.name

                files = {
                    name: {
                        "content": todo_content
                    }
                }

                resp = None

                github = Github()
                token = GithubToken().get()
                github.login(token)  # need to login
                log.info("Create a new gist..")
                resp = github.create_gist(files=files, description="Todo")

                if resp.status_code == 201:
                    dct = resp.json()
                    html_url = dct["html_url"].encode("utf8")
                    log.ok("Create success:%s ,"
                           "pushed at file '%s'" % (html_url, name))
                    self.save(dct["id"])
                    sys.exit()
                elif resp.status_code == 401:
                    log.warning("Github access denied, empty the old token")
                    GithubToken().save('')  # empty the token!
                    # and re create
                    self.get(answer='1')
                else:
                    log.error("Create gist failed. %d" % resp.status_code)
            else:  # exit if else input
                log.error("Invalid answer.")
        return self.content