Files
PyGithub/scripts/add_attribute.py
T
Yossarian King faca4ce1c0 Add support for projects (#854)
Initial solution for [PyGithub support for projects](https://github.com/PyGithub/PyGithub/issues/606) (#606). Adds comprehensive project querying API.

Currently does not support for modifying projects, columns or cards. (I only need this interface for reporting purposes. Of course once I'm done others are more than welcome to add additional features!)

API that was integrated:
https://developer.github.com/v3/projects
https://developer.github.com/v3/projects/columns
https://developer.github.com/v3/projects/cards

Note that the Github project API is in preview mode - it requires a special header in order for requests to be processed, and the API is subject to change without notice.

## Summary
- new classes: Project, ProjectColumn, ProjectCard
- add Organization.get_projects method
- add Repository.get_projects method
- add MainClass.get_project method
- add test cases to exercise all new classes and methods and verify attributes are as expected; replay data is included, recorded (mostly) from my fork of PyGithub
- updated add_attribute script to:
  - use makeXXXAttribute API
  - be able to add to the end of the current set of properties
  - handle both Completable and NonCompletable class types correctly

## Checklist
Not to be merged until these are done:

- [x] remaining Project properties
- [x] remaining ProjectColumn properties
- [x] remaining ProjectCard properties
- [x] support for archived_state parameter when getting cards: all,archived, or not_archived
- [x] get issue from card, not just pull request ("get_content" method?)
- [x] centralize header management for "preview" access to projects API
- [x] add test for retrieving organization projects
- [x] add test for getting issue / pull request content from card
2018-09-11 13:05:16 +08:00

142 lines
5.9 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
############################ Copyrights and license ############################
# #
# Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net> #
# Copyright 2014 Thialfihar <thi@thialfihar.org> #
# Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net> #
# Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com> #
# Copyright 2018 sfdye <tsfdye@gmail.com> #
# Copyright 2018 bbi-yggy <yossarian@blackbirdinteractive.com> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
# #
# 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 <http://www.gnu.org/licenses/>. #
# #
################################################################################
import sys
import os.path
className, attributeName, attributeType = sys.argv[1:4]
if len(sys.argv) > 4:
attributeClassType = sys.argv[4]
else:
attributeClassType = ""
types = {
"string": ("string", None, "self._makeStringAttribute(attributes[\"" + attributeName + "\"])"),
"int": ("integer", None, "self._makeIntAttribute(attributes[\"" + attributeName + "\"])"),
"bool": ("bool", None, "self._makeBoolAttribute(attributes[\"" + attributeName + "\"])"),
"datetime": ("datetime.datetime", "(str, unicode)", "self._makeDatetimeAttribute(attributes[\"" + attributeName + "\"])"),
"class": (":class:`" + attributeClassType + "`", None, "self._makeClassAttribute(" + attributeClassType + ", attributes[\"" + attributeName + "\"])"),
}
attributeDocType, attributeAssertType, attributeValue = types[attributeType]
fileName = os.path.join("github", className + ".py")
with open(fileName) as f:
lines = list(f)
newLines = []
i = 0
added = False
isCompletable = True
isProperty = False
while not added:
line = lines[i].rstrip()
i += 1
if line.startswith("class "):
if "NonCompletableGithubObject" in line:
isCompletable = False
elif line == " @property":
isProperty = True
elif line.startswith(" def "):
attrName = line[8:-7]
# Properties will be inserted after __repr__, but before any other function.
if attrName != "__repr__" and (attrName == "_identity" or attrName > attributeName or not isProperty):
if not isProperty:
newLines.append(" @property")
newLines.append(" def " + attributeName + "(self):")
newLines.append(" \"\"\"")
newLines.append(" :type: " + attributeDocType)
newLines.append(" \"\"\"")
if isCompletable:
newLines.append(" self._completeIfNotSet(self._" + attributeName + ")")
newLines.append(" return self._" + attributeName + ".value")
newLines.append("")
if isProperty:
newLines.append(" @property")
added = True
isProperty = False
newLines.append(line)
added = False
inInit = False
while not added:
line = lines[i].rstrip()
i += 1
if line == " def _initAttributes(self):":
inInit = True
if inInit:
if not line or line.endswith(" = github.GithubObject.NotSet"):
if line:
attrName = line[14:-29]
if not line or attrName > attributeName:
newLines.append(" self._" + attributeName + " = github.GithubObject.NotSet")
added = True
newLines.append(line)
added = False
inUse = False
while not added:
try:
line = lines[i].rstrip()
except IndexError:
line = ""
i += 1
if line == " def _useAttributes(self, attributes):":
inUse = True
if inUse:
if not line or line.endswith(" in attributes: # pragma no branch"):
if line:
attrName = line[12:-36]
if not line or attrName > attributeName:
newLines.append(" if \"" + attributeName + "\" in attributes: # pragma no branch")
if attributeAssertType:
newLines.append(" assert attributes[\"" + attributeName + "\"] is None or isinstance(attributes[\"" + attributeName + "\"], " + attributeAssertType + "), attributes[\"" + attributeName + "\"]")
newLines.append(" self._" + attributeName + " = " + attributeValue)
added = True
newLines.append(line)
while i < len(lines):
line = lines[i].rstrip()
i += 1
newLines.append(line)
with open(fileName, "wb") as f:
for line in newLines:
f.write(line + "\n")