Skip to content

Commit

Permalink
Merge pull request #39 from josuebrunel/dev
Browse files Browse the repository at this point in the history
fix #38 : yaml support added
  • Loading branch information
josuebrunel committed Oct 10, 2015
2 parents eace2f2 + 3a0d046 commit 526a5c3
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 66 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ docs/_build/

# PyBuilder
target/

# Dev
data.json
data.yml
oauth*.*
secrets.tar
*.swp
26 changes: 11 additions & 15 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
language: python
python:
- '2.7'
- '3.2'
- '3.3'
- '3.4'
- pypy
- pypy3
- '2.7'
- '3.3'
- '3.4'
- '3.5'
- pypy
- pypy3
install:
- pip install -r requirements.txt
- pip install myql coverage coveralls
- pip install -r requirements.txt
- pip install myql coverage coveralls pytest
script:
- coverage run --source=yahoo_oauth -m unittest discover
env:
global:
- secure: UeQ8wCwJBCyvCpqvYyJtJImdsEkIUioFF2UOFtQmw9aT601lvF5wfSohFPWHSGaJaIu+6TiiZgybQwj9EW44tJvb1E20n6AY1rxsL6zUtsWfAFvNF9DxLGEH5RCIPyyCkBxBFIObV8Md58E4VY+vCKdy3RyXbmK8bhC7/ioD7cQwjeKwd46PWgjllW4JxCXSbLkY4FJ3hAHMccE/njpA3dy4LD4+SxPiiOU+HIJoY4GOtRQG3LSNK1ZIMC15OMN954ZLkbLUAf5KZqonc9RoFaZuXgCAAuogIgxUQXO40HmqqIE+HTPvwAT+idH2NIZvvWg9IDVe8MdIgFUuGTTcveAh7KLKZCL0Qv8vHefvFOT+lIEoJ6AVlAjCFBUIInNswUyRySa7UD1JZ6txW8BqyykPNta68n0D4RH9ri+MxPpxcBHmaAbDGAl+qEsLa6gjOmULOIP+0QzrGt5LzomrIKZ77w2VA0sDHhis4wEQCcjSZs8HeixqNeCyV+mabUKWu8pQ6jPCwmOW+XZ/hLDm06x89okeS17ejJRrQdK88h7S0vXgTEX2vdpyQfFReOa/s8ssVDbLUQ2xvLM/zPU8itfV0gK8ANHLBYjNqDW51rYXAJSOQfJVA/4RMk4Hmx8EF8EUs6k5oi+GW9ITso5ohnVLeASMyaEYpmdT8O9e7NQ=
- secure: AHfzuvNGZIWHJeXzNhVVmMI1EPyu6ZBiuVjWP6kox6shq3zYwn5dxcU0tC62nIjYi/QQvnC0x0wpzZqXv/9yMtRzLukg4LFxuBl1o5FzpxGJZZgtKf0GC1u0Cp+b5GCT62Fdpn2LYramWKBHlF05atUYgyST8H2zaHe/SmQnjhmlki4nSJUfVmG40/hrGyemtELIHoQy85ujvjFV18p+7MTL+ySc9E13P1JrgecV8uZ7Ya8ajAHU/gqFFRupdS5cOpje+MuasltCZPcb74xWfECvoZqbWurj4UCRBTs80oxulkmVJTWbzB9KWjoqOSJQqJfI1LIZod6OJEsdVENBEH7HZ6El9U2KZ54FIYDNnO704jPQp/WC8YXFvzZucgsFWb2RS0PZ77CwHLrGdoPn3y8Rm7gHb4QHTOuhQSi9dzl+XdZzVpduaT9Z7LRej/31LOAmrQyRMgoiYiRf183yx8guhSYwaC+EHodCOq0HSKEYfxWVlrwML17vFhjD5A0kOSJ2cc0A7QmOjYlUJ5VI5A57re9PFg5g7yd7TIX+6vZ4GVdv3alCeP58u1QSeH96aTYMeiszI9GVTE5AjdaPD+K28TDb9U+PuqBXILKUH/pDlXtYLNqDPUDiD+seOH1UXKrpG00kTWQ/q7lQ6thHZORfOSNe7i//pdKBy1cfbC8=
- coverage run --source=yahoo_oauth -m pytest -v tests.py
before_install:
- openssl aes-256-cbc -K $encrypted_6d515aab85d8_key -iv $encrypted_6d515aab85d8_iv
-in secrets.tar.enc -out secrets.tar -d
- tar xvf secrets.tar
after_success:
- coverage report
- coveralls
- coverage report
- coveralls
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Wether you use **OAuth1** or **OAuth2**, only **2** parameters are required.
* ___consumer_key___
* ___consumer_secret___

I recommend putting those two into a file. Only ***json*** files are supported
I recommend putting those two into a file. Only ***json*** and ***yaml*** files are supported

```json
{
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ required.
- ***consumer*\ key**\ \_
- ***consumer*\ secret**\ \_

I recommend putting those two into a file. Only ***json*** files are
I recommend putting those two into a file. Only ***json*** and ***yaml*** files are
supported

.. code:: json
Expand Down
44 changes: 25 additions & 19 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
Jinja2>=2.7.3
Markdown>=2.6.2
MarkupSafe>=0.23
PyYAML>=3.11
certifi>=2015.04.28
click>=4.0
coverage>=3.7.1
coveralls>=0.5
docopt>=0.6.2
ghp-import>=0.4.1
livereload>=2.4.0
mkdocs>=0.13.3
myql>=1.2.2
oauthlib>=0.7.2
rauth>=0.7.1
requests>=2.7.0
requests-oauthlib>=0.5.0
six>=1.9.0
tornado>=4.2
Jinja2=>2.8
Markdown=>2.6.2
MarkupSafe=>0.23
PyYAML=>3.11
certifi=>2015.9.6.2
click=>5.1
coverage=>4.0
coveralls=>1.0
docopt=>0.6.2
ghp-import=>0.4.1
livereload=>2.4.0
mkdocs=>0.14.0
myql=>1.2.7
oauthlib=>1.0.3
pluggy=>0.3.1
py=>1.4.30
pytest=>2.8.2
rauth=>0.7.1
requests=>2.8.0
requests-oauthlib=>0.5.0
six=>1.10.0
tornado=>4.2.1
tox=>2.1.1
virtualenv=>13.1.2
yahoo-oauth=>0.1.7
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Wether you use **OAuth1** or **OAuth2**, only **2** parameters are required.
* ___consumer_key___
* ___consumer_secret___

I recommend putting those two into a file. Only ***json*** files are supported
I recommend putting those two into a file. Only ***json*** and ***yaml*** files are supported

```json
{
Expand Down
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
site_name: Yahoo-OAuth
repo_url: https://github.com/josuebrunel/yahoo-oauth
site_url: http://yahoo-oauth.readthedocs.org/en/latest/yahoo-oauth/
use_directory_urls: false
strict: true
site_description: Documentation on the Yahoo-OAuth Library
site_author: Josue Kouka
google_analytics: ['UA-32441224-5', 'yahoo-oauth.readthedocs.org']
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
rauth>=0.7.1
requests>=2.7.0
requests>=2.8.0
pyaml>=3.11
4 changes: 2 additions & 2 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
set -x
if [ ! -z $1 ]; then
TestCase=".${1}"
else
Expand All @@ -10,7 +11,6 @@ else
Test=''
fi

coverage run --source=yahoo_oauth -m unittest discover
#python -m unittest tests$TestCase$Test
coverage run --source=yahoo_oauth -m unittest tests$TestCase$Test
coverage report
coverage html
Binary file modified secrets.tar.enc
Binary file not shown.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

__author__ = 'Josue Kouka'
__email__ = 'josuebrunel@gmail.com'
__version__ = '0.1.7'
__version__ = '0.1.8'

#requirements.txt
with open('requirements.txt') as f:
Expand All @@ -28,9 +28,9 @@ def read(fname):
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.2',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
'Development Status :: 5 - Production/Stable',
Expand Down
50 changes: 35 additions & 15 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,35 @@
import myql
from myql.utils import pretty_json

from yahoo_oauth.yahoo_oauth import json_write_data, json_get_data
from yahoo_oauth.utils import write_data, get_data
from yahoo_oauth.utils import json_write_data, json_get_data
from yahoo_oauth.utils import yaml_write_data, yaml_get_data
from yahoo_oauth import OAuth1, OAuth2

logging.basicConfig(level=logging.DEBUG,format="[%(asctime)s %(levelname)s] [%(name)s.%(module)s.%(funcName)s] %(message)s \n")
oauth_logger = logging.getLogger('yahoo_oauth')

oauth_logger.disabled = False


class TestYahooOAuth(unittest.TestCase):
"""Class to tests Yahoo OAuth module
"""
def test_oauth1(self,):
oauth = OAuth1(None, None, from_file='oauth1.json')
yql = myql.MYQL(oauth=oauth)
response = yql.get_guid('josue_brunel')
logging.debug(pretty_json(response.content))
self.assertEqual(response.status_code,200)

def test_oauth2(self,):
oauth = OAuth2(None, None, from_file='oauth2.yaml')
response = oauth.session.get('https://social.yahooapis.com/v1/me/guid?format=json')
logging.debug(pretty_json(response.content))
self.assertEqual(response.status_code,200)


class TestJSON(unittest.TestCase):

def setUp(self,):
self.d = {'ck':'consumer_key','cs':'consumer_secret'}
Expand All @@ -24,22 +42,24 @@ def tearDown(self):
pass

def test_1_json_write_data(self,):
json_write_data(self.d, 'data.json')
self.assertEquals(os.path.exists('data.json'),True)
write_data(self.d, 'data.json')
self.assertEquals(os.path.exists('data.json'), True)

def test_2_json_get_data(self,):
json_data = json_get_data('data.json')
json_data = get_data('data.json')
self.assertEquals(self.d,json_data)

def test_oauth1(self,):
oauth = OAuth1(None, None, from_file='oauth1.json')
yql = myql.MYQL(oauth=oauth)
response = yql.getGUID('josue_brunel')
logging.debug(pretty_json(response.content))
self.assertEqual(response.status_code,200)

def test_oauth2(self,):
oauth = OAuth2(None, None, from_file='oauth2.json')
response = oauth.session.get('https://social.yahooapis.com/v1/me/guid?format=json')
logging.debug(pretty_json(response.content))
self.assertEqual(response.status_code,200)
class TestYAML(unittest.TestCase):

def setUp(self,):
self.d = {'ck': 'consumer_key', 'cs': 'consumer_key'}

def test_1_yaml_write_data(self,):
yaml_write_data(self.d, 'data.yml')
self.assertEqual(os.path.exists('data.yml'), True)

def test_2_yaml_get_data(self,):
yml_data = yaml_get_data('data.yml')
self.assertEqual(self.d, yml_data)

13 changes: 13 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[tox]
envlist = py27,py33,py34,py35

[testenv]
deps=
-rrequirements.txt
myql
pytest
coverage
commands=
- coverage run --source=yahoo_oauth -m pytest -vs tests.py
- coverage report -m

2 changes: 1 addition & 1 deletion yahoo_oauth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = 'Josue Kouka'
__email__ = 'josuebrunel@gmail.com'
__version__ = "0.1.7"
__version__ = "0.1.8"


from yahoo_oauth.yahoo_oauth import OAuth1, OAuth2
Expand Down
37 changes: 37 additions & 0 deletions yahoo_oauth/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from __future__ import absolute_import, unicode_literals


import os
import json
import yaml

from rauth import OAuth1Service, OAuth2Service

services = {
Expand All @@ -19,6 +23,23 @@

CALLBACK_URI = 'oob'

def get_file_extension(filename):
return os.path.splitext(filename)

def get_data(filename):
"""Calls right function according to file extension
"""
ext = get_file_extension(filename)
func = json_get_data if ext == '.json' else yaml_get_data
return func(filename)

def write_data(data, filename):
"""Call right func to save data according to file extension
"""
ext = get_file_extension(filename)
func = json_write_data if ext == '.json' else yaml_write_data
return func(data, filename)

def json_write_data(json_data, filename):
"""Write json data into a file
"""
Expand All @@ -36,4 +57,20 @@ def json_get_data(filename):

return False

def yaml_get_data(filename):
"""Get data from .yml file
"""
with open(filename, 'rb') as fd:
yaml_data = yaml.load(fd)
return yaml_data
return False

def yaml_write_data(yaml_data, filename):
"""Write data into a .yml file
"""
with open(filename, 'w') as fd:
yaml.dump(yaml_data, fd, default_flow_style=False)
return True

return False

17 changes: 9 additions & 8 deletions yahoo_oauth/yahoo_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

from rauth.utils import parse_utf8_qsl

from yahoo_oauth.utils import json_get_data, json_write_data, services, CALLBACK_URI
from yahoo_oauth.utils import services, CALLBACK_URI
from yahoo_oauth.utils import get_data, write_data
from yahoo_oauth.logger import YahooLogger

logging.setLoggerClass(YahooLogger)
Expand All @@ -39,13 +40,13 @@ def __init__(self, oauth_version, consumer_key, consumer_secret, **kwargs):
base_url : Base url
"""
self.oauth_version = oauth_version
json_data = {}
data = {}

if kwargs.get('from_file'):
logger.debug("Checking ")
self.from_file = kwargs.get('from_file')
json_data = json_get_data(self.from_file)
vars(self).update(json_data)
data = get_data(self.from_file)
vars(self).update(data)
else:
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
Expand Down Expand Up @@ -80,20 +81,20 @@ def __init__(self, oauth_version, consumer_key, consumer_secret, **kwargs):

if vars(self).get('access_token') and vars(self).get('access_token_secret') and vars(self).get('session_handle'):
if not self.token_is_valid():
json_data.update(self.refresh_access_token())
data.update(self.refresh_access_token())
elif vars(self).get('access_token') and vars(self).get('token_type') and vars(self).get('refresh_token'):
if not self.token_is_valid():
json_data.update(self.refresh_access_token())
data.update(self.refresh_access_token())
else:
json_data.update(self.handler())
data.update(self.handler())

# Getting session
if self.oauth_version == 'oauth1':
self.session = self.oauth.get_session((self.access_token, self.access_token_secret))
else:
self.session = self.oauth.get_session(token=self.access_token)

json_write_data(json_data, vars(self).get('from_file','secrets.json'))
write_data(data, vars(self).get('from_file','secrets.json'))


def handler(self,):
Expand Down

0 comments on commit 526a5c3

Please sign in to comment.