From bd7abb58772ae1a61fd7eb44308a3a2f60432ad6 Mon Sep 17 00:00:00 2001 From: AKFish Date: Wed, 21 Aug 2013 23:01:40 +0800 Subject: [PATCH] Add update() method --- .gitignore | 1 + github/Consts.py | 49 ++++++++++++++++++++++++++++++++++++++++++ github/GithubObject.py | 41 ++++++++++++++++++++++++++++++++++- 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 github/Consts.py diff --git a/.gitignore b/.gitignore index 3a7eff09..733d3ccd 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ GithubCredentials.py *.cfg *.bat +*.py~ diff --git a/github/Consts.py b/github/Consts.py new file mode 100644 index 00000000..2e551756 --- /dev/null +++ b/github/Consts.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- + +############################ Copyrights and license ############################ +# # +# Copyright 2013 AKFish # +# # +# This file is part of PyGithub. http://jacquev6.github.com/PyGithub/ # +# # +# PyGithub is free software: you can redistribute it and/or modify it under # +# the terms of the GNU Lesser General Public License as published by the Free # +# Software Foundation, either version 3 of the License, or (at your option) # +# any later version. # +# # +# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # +# details. # +# # +# You should have received a copy of the GNU Lesser General Public License # +# along with PyGithub. If not, see . # +# # +################################################################################ + +# TODO: As of Thu Aug 21 22:40:13 (BJT) Chinese Standard Time 2013 +# lots of consts in this project are explict +# should realy round them up and reference them by consts + +################################################################################ +# Helper Function # +################################################################################ +def get(dic, key): + if key in dic: + return key[dic] + return None + + +################################################################################ +# Request Header # +# (Case sensitive) # +################################################################################ +REQ_IF_NONE_MATCH = "If-None-Match" +REQ_IF_MODIFIED_SINCE = "If-Modified-Since" + +################################################################################ +# Response Header # +# (Lower Case) # +################################################################################ +RES_ETAG = "etag" +RES_LAST_MODIFED = "last-modified" diff --git a/github/GithubObject.py b/github/GithubObject.py index f20873b9..ae71cf7c 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -32,6 +32,8 @@ import GithubException import pickle +import Consts + class _NotSetType: def __repr__(self): return "NotSet" @@ -118,8 +120,45 @@ class GithubObject(object): ''' with open(file_name, 'rb') as f: return pickle.load(f) - + @property + def etag(self): + ''' + :type str + ''' + return Consts.get(self._headers, Consts.RES_ETAG) + + @property + def last_modified(self): + ''' + :type str + ''' + return Consts.get(self._headers, Consts.RES_LAST_MODIFED) + + + def update(self): + ''' + Check and update the object with conditional request + :rtype: Boolean value indicating whether the object is changed + ''' + conditionalRequestHeader = dict() + if self.etag is not None: + conditionalRequestHeader[Consts.REQ_IF_NONE_MATCH] = self.etag + if self.last_modified is not None: + conditionalRequestHeader[Consts.REQ_IF_MODIFIED_SINCE] = self.last_modified + + try: + headers, data = self._requester.requestJsonAndCheck( + "GET", + self._url, + conditionalRequestHeader, + None + ) + self._storeAndUseAttributes(data) + self.__completed = True + return True + except: #GithubException.NotModifiedException: + return False class NonCompletableGithubObject(GithubObject): def _completeIfNeeded(self):