Qlstudio Qlstudio - 4 months ago 31
Python Question

"InvalidSchema" when attempting to create file with github3.py

Using github3.py 1.0.0a4, I am trying to make a basic "server" program that creates, updates, deletes, and fetches files. However, my program raises "InvalidSchema" on attempt to create a file. Why? I checked my program with the documentation...

Code:

from github3 import login
from github3.repos.repo import Repository

import sys, time

print sys.argv

if len(sys.argv) != 5:
print 'Usage: server.exe username=... password=... [[-create -delete -update -fetch] file] [repo=server]'
sys.exit()

try:
un, pw, mode, f = sys.argv[1].split('='), sys.argv[2].split('='), sys.argv[3], sys.argv[4]
me = login(un, pw)
except:
print 'Usage: server.exe username=... password=... [[-create -delete -update -fetch] file] [repo=server]'
sys.exit()

try:
repo = sys.argv[5]
except IndexError:
repo = 'server'

repo = Repository({'name':repo}, session=me)

if mode == '-create':
f = open(f)
name = f.name
data = f.read()
repo.create_file(name, 'Create '+time.asctime(), data.encode('utf-8'))

if mode == '-delete':
f = open(f)
repo.contents(f.name).delete('Delete '+time.asctime(), data.encode('utf-8'))

if mode == '-update':
f = open(f)
name = f.name
data = f.read()
repo.contents(name).update('Update '+time.asctime(), data.encode('utf-8'))

if mode == '-fetch':
with open(f, mode='w') as f:
f.write(repo.contents(f.name))


Result when running on command line:

C:\Python27>python server.py username=... password=... -create pytest.py
['server.py', 'username=...', 'password=...', '-create', 'pytest.py']
Traceback (most recent call last):
File "server.py", line 30, in <module>
repo.create_file(name, 'Create '+time.asctime(), data.encode('utf-8'))
File "C:\Python27\lib\site-packages\github3\decorators.py", line 33, in auth_wrapper
return func(self, *args, **kwargs)
File "C:\Python27\lib\site-packages\github3\repos\repo.py", line 745, in create_file
json = self._json(self._put(url, data=dumps(data)), 201)
File "C:\Python27\lib\site-packages\github3\models.py", line 199, in _put
return self.session.put(url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 521, in put
return self.request('PUT', url, data=data, **kwargs)
File "C:\Python27\lib\site-packages\github3\session.py", line 88, in request
response = super(GitHubSession, self).request(*args, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 468, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 570, in send
adapter = self.get_adapter(url=request.url)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 644, in get_adapter
raise InvalidSchema("No connection adapters were found for '%s'" % url)
requests.exceptions.InvalidSchema: No connection adapters were found for ':///contents/pytest.py

Answer

un and pw are lists and not strings. Use un = sys.argv[1].split('=')[1] or something like that.

Edited code:

from github3 import login
from github3.repos.repo import Repository

import sys, time

if len(sys.argv) != 5:
    print 'Usage: server.exe username=... password=... [[-create -delete -update -fetch] file] [repo=server]'
    sys.exit()

try:
    un, pw, mode, f = sys.argv[1].split('=')[1], sys.argv[2].split('=')[1], sys.argv[3], sys.argv[4]
    me = login(un, pw)
except:
    print 'Usage: server.exe username=... password=... [[-create -delete -update -fetch] file] [repo=server]'
    sys.exit()

try:
    repo = sys.argv[5]
except IndexError:
    repo = 'server'

repo = me.repository(un, repo)

if mode == '-create':
    f = open(f)
    name = f.name
    data = f.read()
    repo.create_file(name, 'Create '+time.asctime(), data.encode('utf-8'))

if mode == '-delete':
    f = open(f)
    repo.file_contents(f.name).delete('Delete '+time.asctime())

if mode == '-update':
    f = open(f)
    name = f.name
    data = f.read()
    repo.file_contents(name).update('Update '+time.asctime(), f.read().encode('utf-8'))

if mode == '-fetch':
    with open(f, mode='w') as f:
        f.write(repo.file_contents(f.name).decoded)

(Some other bugs like deleting were fixed.)