Skip to content

Commit

Permalink
Drop Python 2 support (#211)
Browse files Browse the repository at this point in the history
* Drop Python 2 support

* Remove some unicode strings

* Update pylint version

* Update pylint

* More cleanup
  • Loading branch information
balloob committed Feb 21, 2018
1 parent 4026c29 commit c15d23e
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 55 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,12 +1,12 @@
sudo: false
language: python
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
install:
- pip install -r requirements.txt
- pip install flake8==3.3.0 pylint==1.7.2
- pip install flake8==3.3.0 pylint==1.8.1
script:
- flake8 --exclude cast_channel_pb2.py,authority_keys_pb2.py,logging_pb2.py pychromecast
- pylint pychromecast
13 changes: 3 additions & 10 deletions README.rst
Expand Up @@ -4,7 +4,7 @@ pychromecast |Build Status|
.. |Build Status| image:: https://travis-ci.org/balloob/pychromecast.svg?branch=master
:target: https://travis-ci.org/balloob/pychromecast

Library for Python 2 and 3 to communicate with the Google Chromecast. It
Library for Python 3.4+ to communicate with the Google Chromecast. It
currently supports:

- Auto discovering connected Chromecasts on the network
Expand All @@ -26,18 +26,11 @@ PyChromecast depends on the Python packages requests, protobuf and
zeroconf. Make sure you have these dependencies installed using
``pip install -r requirements.txt``

Some users running Python 2.7 have `reported`_ that they had to upgrade
their version of pip using ``pip install --upgrade pip`` before they
were able to install the latest version of the dependencies.

.. _reported: https://github.com/balloob/pychromecast/issues/47#issuecomment-107822162

How to use
----------

.. code:: python
>> from __future__ import print_function
>> import time
>> import pychromecast
Expand All @@ -52,13 +45,13 @@ How to use
DeviceStatus(friendly_name='Living Room', model_name='Chromecast', manufacturer='Google Inc.', uuid=UUID('df6944da-f016-4cb8-97d0-3da2ccaa380b'), cast_type='cast')
>> print(cast.status)
CastStatus(is_active_input=True, is_stand_by=False, volume_level=1.0, volume_muted=False, app_id=u'CC1AD845', display_name=u'Default Media Receiver', namespaces=[u'urn:x-cast:com.google.cast.player.message', u'urn:x-cast:com.google.cast.media'], session_id=u'CCA39713-9A4F-34A6-A8BF-5D97BE7ECA5C', transport_id=u'web-9', status_text='')
CastStatus(is_active_input=True, is_stand_by=False, volume_level=1.0, volume_muted=False, app_id='CC1AD845', display_name='Default Media Receiver', namespaces=['urn:x-cast:com.google.cast.player.message', 'urn:x-cast:com.google.cast.media'], session_id='CCA39713-9A4F-34A6-A8BF-5D97BE7ECA5C', transport_id='web-9', status_text='')
>> mc = cast.media_controller
>> mc.play_media('http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', 'video/mp4')
>> mc.block_until_active()
>> print(mc.status)
MediaStatus(current_time=42.458322, content_id=u'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', content_type=u'video/mp4', duration=596.474195, stream_type=u'BUFFERED', idle_reason=None, media_session_id=1, playback_rate=1, player_state=u'PLAYING', supported_media_commands=15, volume_level=1, volume_muted=False)
MediaStatus(current_time=42.458322, content_id='http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4', content_type='video/mp4', duration=596.474195, stream_type='BUFFERED', idle_reason=None, media_session_id=1, playback_rate=1, player_state='PLAYING', supported_media_commands=15, volume_level=1, volume_muted=False)
>> mc.pause()
>> time.sleep(5)
Expand Down
14 changes: 2 additions & 12 deletions examples/blocking.py
@@ -1,11 +1,9 @@
"""
Example that shows how the new Python 2 socket client can be used.
Example that shows how the socket client can be used.
Functions called in this example are blocking which means that
the function doesn't return as long as no result was received.
"""

from __future__ import print_function
import time
import sys
import logging
Expand All @@ -22,9 +20,6 @@
exit()
cast = casts[0]

yt = youtube.YouTubeController()
cast.register_handler(yt)

print()
print(cast.device)
time.sleep(1)
Expand Down Expand Up @@ -65,12 +60,7 @@
elif t == 25:
print("Sending stop command")
cast.media_controller.stop()
elif t == 27:
print("Switching to YouTube")
yt.play_video("L0MK7qz13bU")
elif t == 38:
cast.media_controller.pause()
elif t == 45:
elif t == 32:
cast.quit_app()
break

Expand Down
2 changes: 0 additions & 2 deletions examples/dashcast_blocking.py
Expand Up @@ -4,8 +4,6 @@
Functions called in this example are blocking which means that
the function doesn't return as long as no result was received.
"""

from __future__ import print_function
import time
import sys
import logging
Expand Down
4 changes: 1 addition & 3 deletions examples/non_blocking.py
@@ -1,12 +1,10 @@
"""
Example that shows how the new Python 2 socket client can be used.
Example that shows how the socket client can be used.
All functions (except get_chromecast()) are non-blocking and
return immediately without waiting for the result. You can use
that functionality to include pychromecast into your main loop.
"""

from __future__ import print_function
import time
import select
import sys
Expand Down
12 changes: 2 additions & 10 deletions pychromecast/__init__.py
@@ -1,9 +1,6 @@
"""
PyChromecast: remote control your Chromecast
"""
from __future__ import print_function

import sys
import logging
import fnmatch

Expand All @@ -25,8 +22,6 @@

IDLE_APP_ID = 'E8C28D3C'
IGNORE_CEC = []
# For Python 2.x we need to decode __repr__ Unicode return values to str
NON_UNICODE_REPR = sys.version_info < (3, )


def _get_chromecast_from_host(host, tries=None, retry_wait=None, timeout=None,
Expand Down Expand Up @@ -343,14 +338,11 @@ def __del__(self):
pass

def __repr__(self):
txt = u"Chromecast({!r}, port={!r}, device={!r})".format(
txt = "Chromecast({!r}, port={!r}, device={!r})".format(
self.host, self.port, self.device)
# Python 2.x does not work well with unicode returned from repr
if NON_UNICODE_REPR:
return txt.encode('utf-8')
return txt

def __unicode__(self):
return u"Chromecast({}, {}, {}, {}, {})".format(
return "Chromecast({}, {}, {}, {}, {})".format(
self.host, self.port, self.device.friendly_name,
self.device.model_name, self.device.manufacturer)
3 changes: 2 additions & 1 deletion pychromecast/controllers/media.py
Expand Up @@ -453,6 +453,7 @@ def play_media(self, url, content_type, title=None, thumb=None,
Docs:
https://developers.google.com/cast/docs/reference/messages#MediaData
"""
# pylint: disable=too-many-locals
def app_launched_callback():
"""Plays media after chromecast has switched to requested app."""
self._send_start_play_media(
Expand All @@ -470,7 +471,7 @@ def _send_start_play_media(self, url, content_type, title=None, thumb=None,
metadata=None, subtitles=None,
subtitles_lang='en-US',
subtitles_mime='text/vtt', subtitle_id=1):

# pylint: disable=too-many-locals
msg = {
'media': {
'contentId': url,
Expand Down
10 changes: 5 additions & 5 deletions pychromecast/dial.py
Expand Up @@ -21,11 +21,11 @@
CAST_TYPE_GROUP = 'group'

CAST_TYPES = {
u'chromecast': CAST_TYPE_CHROMECAST,
u'eureka dongle': CAST_TYPE_CHROMECAST,
u'chromecast audio': CAST_TYPE_AUDIO,
u'google home': CAST_TYPE_AUDIO,
u'google cast group': CAST_TYPE_GROUP,
'chromecast': CAST_TYPE_CHROMECAST,
'eureka dongle': CAST_TYPE_CHROMECAST,
'chromecast audio': CAST_TYPE_AUDIO,
'google home': CAST_TYPE_AUDIO,
'google cast group': CAST_TYPE_GROUP,
}


Expand Down
5 changes: 2 additions & 3 deletions pychromecast/discovery.py
Expand Up @@ -2,7 +2,6 @@
import socket
from uuid import UUID

import six
import zeroconf

DISCOVER_TIMEOUT = 5
Expand Down Expand Up @@ -47,10 +46,10 @@ def add_service(self, zconf, typ, name):
return

def get_value(key):
"""Retrieve value and decode for Python 2/3."""
"""Retrieve value and decode to UTF-8."""
value = service.properties.get(key.encode('utf-8'))

if value is None or isinstance(value, six.text_type):
if value is None or isinstance(value, str):
return value
return value.decode('utf-8')

Expand Down
12 changes: 6 additions & 6 deletions pychromecast/socket_client.py
Expand Up @@ -254,9 +254,7 @@ def initialize_connection(self):

self.logger.debug("Connected!")
break
# socket.error is a deprecated alias of OSError in Python 3.3+,
# can be removed when Python 2.x support is dropped
except (OSError, socket.error) as err:
except OSError as err:
self.connecting = True
if self.stop.is_set():
self.logger.error(
Expand Down Expand Up @@ -352,7 +350,7 @@ def run_once(self):
Use run_once() in your own main loop after you
receive something on the socket (get_socket()).
"""
# pylint: disable=too-many-branches
# pylint: disable=too-many-branches, too-many-return-statements

try:
if not self._check_connection():
Expand Down Expand Up @@ -408,6 +406,8 @@ def run_once(self):
if function:
function(data)

return 0

def get_socket(self):
"""
Returns the socket of the connection to use it in you own
Expand Down Expand Up @@ -467,8 +467,8 @@ def _route_message(self, message, data):
_message_to_string(message, data))
except Exception: # pylint: disable=broad-except
self.logger.exception(
(u"Exception caught while sending message to "
u"controller %s: %s"),
("Exception caught while sending message to "
"controller %s: %s"),
type(self._handlers[message.namespace]).__name__,
_message_to_string(message, data))

Expand Down
1 change: 0 additions & 1 deletion requirements.txt
@@ -1,4 +1,3 @@
requests>=2.0
protobuf>=3.0.0
zeroconf>=0.17.7
six>=1.10.0

0 comments on commit c15d23e

Please sign in to comment.