Some style improvements. - Used yapf to clean things up - add a flake8 config - add .python-version file for pyenv - add yapf style config Change-Id: I1bda89cff8fca2c770fc81b94e76e398dd64662c
diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..f61c53b --- /dev/null +++ b/.flake8
@@ -0,0 +1,3 @@ +[flake8] +max-line-length=80 +ignore = E111 # indentation is not a multiple of four 2-space indentation is used here. \ No newline at end of file
diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..9bbf492 --- /dev/null +++ b/.python-version
@@ -0,0 +1 @@ +2.7.14
diff --git a/.style.yapf b/.style.yapf new file mode 100644 index 0000000..4861caf --- /dev/null +++ b/.style.yapf
@@ -0,0 +1,3 @@ +[style] +based_on_style = google +indent_width = 2
diff --git a/client.py b/client.py index e6662cd..2c90c87 100644 --- a/client.py +++ b/client.py
@@ -11,7 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - """The GCI API Client. A thin wrapper around the GCI API. Exmple usage: @@ -34,15 +33,16 @@ class GCIAPIClient(object): """GCIAPIClient provides a thin wrapper around the GCI Task API. - A GCIAPIClient simplifies working with tasks by forming the HTTP requests on - behalf of the caller. + A GCIAPIClient simplifies working with tasks by forming the HTTP requests on + behalf of the caller. - Attributes: - url_prefix: A string prefix for the codin URL - headers: A dictionary of HTTP headers - """ + Attributes: + url_prefix: A string prefix for the codin URL + headers: A dictionary of HTTP headers + """ - def __init__(self, auth_token=None, + def __init__(self, + auth_token=None, url_prefix='https://codein.withgoogle.com/', debug=False): self.url_prefix = urlparse.urljoin(url_prefix, 'api/program/current/') @@ -64,32 +64,36 @@ def ListTasks(self, page=1): """Fetches a list of tasks. - Args: - page: Which page of results to return. + Args: + page: Which page of results to return. - Returns: - A JSON encoded list of tasks. + Returns: + A JSON encoded list of tasks. - Raises: - HTTPError: a 4XX client error or 5XX server error response was returned. - """ - r = requests.get(self._Url('tasks'), headers=self.headers, - params={'page': page}) + Raises: + HTTPError: a 4XX client error or 5XX server error + response was returned. + """ + r = requests.get( + self._Url('tasks'), headers=self.headers, params={ + 'page': page + }) r.raise_for_status() return r.json() def GetTask(self, task_id): """Fetches a single task. - Args: - task_id: An integer id for the task. + Args: + task_id: An integer id for the task. - Returns: - A JSON encoded task. + Returns: + A JSON encoded task. - Raises: - HTTPError: a 4XX client error or 5XX server error response was returned. - """ + Raises: + HTTPError: a 4XX client error or 5XX server error + response was returned. + """ r = requests.get(self._Url('tasks/%d' % task_id), headers=self.headers) r.raise_for_status() return r.json() @@ -97,57 +101,57 @@ def NewTask(self, task): """Creates a single new task. - Args: - task: A task object. + Args: + task: A task object. - Returns: - A JSON encoded response. + Returns: + A JSON encoded response. - Raises: - HTTPError: a 4XX client error or 5XX server error response was returned. - """ - r = requests.post(self._Url('tasks'), headers=self.headers, - data=json.dumps(task)) + Raises: + HTTPError: a 4XX client error or 5XX server error + response was returned. + """ + r = requests.post( + self._Url('tasks'), headers=self.headers, data=json.dumps(task)) r.raise_for_status() return r.json() def UpdateTask(self, task_id, task): """Modifies a single task. - Args: - task_id: An integer id for the task. - task: A task object. + Args: + task_id: An integer id for the task. + task: A task object. - Returns: - A JSON encoded response. + Returns: + A JSON encoded response. - Raises: - HTTPError: a 4XX client error or 5XX server error response was returned. - """ - r = ( - requests.put( - self._Url('tasks/%d' % task_id), data=json.dumps(task), - headers=self.headers)) + Raises: + HTTPError: a 4XX client error or 5XX server error + response was returned. + """ + r = (requests.put( + self._Url('tasks/%d' % task_id), + data=json.dumps(task), + headers=self.headers)) r.raise_for_status() return r.json() def DeleteTask(self, task_id): """Deletes a single task. - Args: - task_id: An integer id for the task. + Args: + task_id: An integer id for the task. - Returns: - A JSON encoded response, if there is content in the response. - Otherwise None. + Returns: + A JSON encoded response, if there is content in the response. + Otherwise None. - Raises: - HTTPError: a 4XX client error or 5XX server error response was returned. - """ - r = ( - requests.delete( - self._Url('tasks/%d' % task_id), - headers=self.headers)) + Raises: + HTTPError: a 4XX client error or 5XX server error + response was returned. + """ + r = (requests.delete(self._Url('tasks/%d' % task_id), headers=self.headers)) r.raise_for_status() # DELETE returns nothing on success, don't try and parse it. if r.content: @@ -157,33 +161,38 @@ def ListTaskInstances(self, page=1): """Fetches a list of tasks. - Args: - page: Which page of results to return. + Args: + page: Which page of results to return. - Returns: - A JSON encoded list of task instances. + Returns: + A JSON encoded list of task instances. - Raises: - HTTPError: a 4XX client error or 5XX server error response was returned. - """ - r = requests.get(self._Url('instances'), headers=self.headers, - params={'page': page}) + Raises: + + HTTPError: a 4XX client error or 5XX server error + response was returned. + """ + r = requests.get( + self._Url('instances'), headers=self.headers, params={ + 'page': page + }) r.raise_for_status() return r.json() def GetTaskInstance(self, task_instance_id): """Fetches a single task. - Args: - task_instance_id: An integer id for the task instance. + Args: + task_instance_id: An integer id for the task instance. - Returns: - A JSON encoded task instance. + Returns: + A JSON encoded task instance. - Raises: - HTTPError: a 4XX client error or 5XX server error response was returned. - """ - r = requests.get(self._Url('instances/%d' % task_instance_id), - headers=self.headers) + Raises: + HTTPError: a 4XX client error or 5XX server error + response was returned. + """ + r = requests.get( + self._Url('instances/%d' % task_instance_id), headers=self.headers) r.raise_for_status() return r.json()
diff --git a/csv_uploader.py b/csv_uploader.py index 0d76d52..44680dc 100755 --- a/csv_uploader.py +++ b/csv_uploader.py
@@ -12,7 +12,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - """A simple CSV uploader client for the GCI API. Usage: @@ -28,23 +27,26 @@ import csv import requests + import client as gciclient - argparser = argparse.ArgumentParser(description='GCI CSV Task Uploader.') -argparser.add_argument('--apikey', type=str, nargs='?', required=True, - help='api key') -argparser.add_argument('--url', type=str, nargs='?', - default='https://codein.withgoogle.com', - help='server url') -argparser.add_argument('--publish', action='store_true', - help='publish uploaded tasks') -argparser.add_argument('-v', '--verbose', action='store_true', - help='enable verbose logging') -argparser.add_argument('--debug', action='store_true', - help='enable debug request logging') -argparser.add_argument('files', nargs=argparse.REMAINDER, - help='csv file to upload') +argparser.add_argument( + '--apikey', type=str, nargs='?', required=True, help='api key') +argparser.add_argument( + '--url', + type=str, + nargs='?', + default='https://codein.withgoogle.com', + help='server url') +argparser.add_argument( + '--publish', action='store_true', help='publish uploaded tasks') +argparser.add_argument( + '-v', '--verbose', action='store_true', help='enable verbose logging') +argparser.add_argument( + '--debug', action='store_true', help='enable debug request logging') +argparser.add_argument( + 'files', nargs=argparse.REMAINDER, help='csv file to upload') FLAGS = argparser.parse_args() @@ -52,13 +54,13 @@ def upload(client, filename): """Creates new tasks for each line in the csv pointed to by filename. - Args: - client: A GCIAPIClient to form and make the requests. - filename: A string filename containing the csv encoded tasks. + Args: + client: A GCIAPIClient to form and make the requests. + filename: A string filename containing the csv encoded tasks. - Raises: - none - """ + Raises: + none + """ with open(filename, 'rb') as csvfile: taskreader = csv.DictReader(csvfile, skipinitialspace=True) @@ -69,9 +71,9 @@ t['categories'] = (task['categories'].split(',') if len(task['categories']) else []) t['tags'] = task['tags'].split(',') if len(task['tags']) else [] - t['is_beginner'] = ( - True if task['is_beginner'].lower() in ['yes', 'true', '1'] - else False) + t['is_beginner'] = (True if + task['is_beginner'].lower() in ['yes', 'true', '1'] + else False) # yapf disable t['time_to_complete_in_days'] = int(task['time_to_complete_in_days']) t['max_instances'] = int(task['max_instances']) @@ -86,9 +88,7 @@ def main(): client = gciclient.GCIAPIClient( - auth_token=FLAGS.apikey, - url_prefix=FLAGS.url, - debug=FLAGS.debug) + auth_token=FLAGS.apikey, url_prefix=FLAGS.url, debug=FLAGS.debug) for filename in FLAGS.files: upload(client, filename)
diff --git a/list_instances.py b/list_instances.py index 044117b..bebf000 100755 --- a/list_instances.py +++ b/list_instances.py
@@ -12,7 +12,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - """List all task instances for your Organization via the GCI API.""" import argparse @@ -20,31 +19,31 @@ import client as gciclient - argparser = argparse.ArgumentParser(description='GCI Task Instances') -argparser.add_argument('--apikey', type=str, nargs='?', required=True, - help='api key') -argparser.add_argument('--url', type=str, nargs='?', - default='https://codein.withgoogle.com', - help='server url') -argparser.add_argument('--debug', action='store_true', - help='enable debug request logging') +argparser.add_argument( + '--apikey', type=str, nargs='?', required=True, help='api key') +argparser.add_argument( + '--url', + type=str, + nargs='?', + default='https://codein.withgoogle.com', + help='server url') +argparser.add_argument( + '--debug', action='store_true', help='enable debug request logging') FLAGS = argparser.parse_args() def main(): client = gciclient.GCIAPIClient( - auth_token=FLAGS.apikey, - url_prefix=FLAGS.url, - debug=FLAGS.debug) + auth_token=FLAGS.apikey, url_prefix=FLAGS.url, debug=FLAGS.debug) next_page = 1 while next_page > 0: instances = client.ListTaskInstances(page=next_page) for ti in instances['results']: print '\t'.join([ - str(ti['id']), + str(ti['id']), # yapf: disable ti['status'].encode('utf-8'), ti['student_display_name'].encode('utf-8'), ti['task_definition_name'].encode('utf-8')
diff --git a/list_tasks.py b/list_tasks.py index 098a495..ec067f3 100755 --- a/list_tasks.py +++ b/list_tasks.py
@@ -12,7 +12,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. - """List all tasks for your Organization via the GCI API.""" import argparse @@ -20,24 +19,24 @@ import client as gciclient - argparser = argparse.ArgumentParser(description='GCI Tasks') -argparser.add_argument('--apikey', type=str, nargs='?', required=True, - help='api key') -argparser.add_argument('--url', type=str, nargs='?', - default='https://codein.withgoogle.com', - help='server url') -argparser.add_argument('--debug', action='store_true', - help='enable debug request logging') +argparser.add_argument( + '--apikey', type=str, nargs='?', required=True, help='api key') +argparser.add_argument( + '--url', + type=str, + nargs='?', + default='https://codein.withgoogle.com', + help='server url') +argparser.add_argument( + '--debug', action='store_true', help='enable debug request logging') FLAGS = argparser.parse_args() def main(): client = gciclient.GCIAPIClient( - auth_token=FLAGS.apikey, - url_prefix=FLAGS.url, - debug=FLAGS.debug) + auth_token=FLAGS.apikey, url_prefix=FLAGS.url, debug=FLAGS.debug) next_page = 1 while next_page > 0: