Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate log setup in examples #841

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Example scripts.

Scripts need to be invoked with the -m flag, e.g.:
python3 -m examples.youtube_example --cast "Kitchen" --show-debug
"""
25 changes: 4 additions & 21 deletions examples/bbciplayer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
# pylint: disable=invalid-name

import argparse
import logging
import sys
from time import sleep
import json

import zeroconf
import pychromecast
from pychromecast import quick_play

from .common import add_log_arguments, configure_logging

# Enable deprecation warnings etc.
if not sys.warnoptions:
import warnings
Expand Down Expand Up @@ -47,13 +47,7 @@
help="Add known host (IP), can be used multiple times",
action="append",
)
parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
parser.add_argument(
"--show-discovery-debug", help="Enable discovery debug log", action="store_true"
)
parser.add_argument(
"--show-zeroconf-debug", help="Enable zeroconf debug log", action="store_true"
)
add_log_arguments(parser)
parser.add_argument(
"--media_id", help='MediaID (default: "%(default)s")', default=MEDIA_ID
)
Expand All @@ -68,18 +62,7 @@
)
args = parser.parse_args()

if args.show_debug:
fmt = "%(asctime)s %(levelname)s (%(threadName)s) [%(name)s] %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.DEBUG)
logging.getLogger("pychromecast.dial").setLevel(logging.INFO)
logging.getLogger("pychromecast.discovery").setLevel(logging.INFO)
if args.show_discovery_debug:
logging.getLogger("pychromecast.dial").setLevel(logging.DEBUG)
logging.getLogger("pychromecast.discovery").setLevel(logging.DEBUG)
if args.show_zeroconf_debug:
print("Zeroconf version: " + zeroconf.__version__)
logging.getLogger("zeroconf").setLevel(logging.DEBUG)
configure_logging(args)

chromecasts, browser = pychromecast.get_listed_chromecasts(
friendly_names=[args.cast], known_hosts=args.known_host
Expand Down
25 changes: 4 additions & 21 deletions examples/bbcsounds_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
# pylint: disable=invalid-name

import argparse
import logging
import sys
from time import sleep
import json

import zeroconf
import pychromecast
from pychromecast import quick_play

from .common import add_log_arguments, configure_logging

# Enable deprecation warnings etc.
if not sys.warnoptions:
import warnings
Expand Down Expand Up @@ -54,13 +54,7 @@
help="Add known host (IP), can be used multiple times",
action="append",
)
parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
parser.add_argument(
"--show-discovery-debug", help="Enable discovery debug log", action="store_true"
)
parser.add_argument(
"--show-zeroconf-debug", help="Enable zeroconf debug log", action="store_true"
)
add_log_arguments(parser)
parser.add_argument(
"--media_id", help='MediaID (default: "%(default)s")', default=MEDIA_ID
)
Expand All @@ -79,18 +73,7 @@
if args.media_id == MEDIA_ID:
args.is_live = DEFAULT_MEDIA_ID_IS_LIVE

if args.show_debug:
fmt = "%(asctime)s %(levelname)s (%(threadName)s) [%(name)s] %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.DEBUG)
logging.getLogger("pychromecast.dial").setLevel(logging.INFO)
logging.getLogger("pychromecast.discovery").setLevel(logging.INFO)
if args.show_discovery_debug:
logging.getLogger("pychromecast.dial").setLevel(logging.DEBUG)
logging.getLogger("pychromecast.discovery").setLevel(logging.DEBUG)
if args.show_zeroconf_debug:
print("Zeroconf version: " + zeroconf.__version__)
logging.getLogger("zeroconf").setLevel(logging.DEBUG)
configure_logging(args)

chromecasts, browser = pychromecast.get_listed_chromecasts(
friendly_names=[args.cast], known_hosts=args.known_host
Expand Down
25 changes: 3 additions & 22 deletions examples/bubbleupnp_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
# pylint: disable=invalid-name

import argparse
import logging
import sys
from time import sleep

import zeroconf

import pychromecast
from pychromecast import quick_play

from .common import add_log_arguments, configure_logging

# Enable deprecation warnings etc.
if not sys.warnoptions:
Expand Down Expand Up @@ -41,13 +39,7 @@
help="Add known host (IP), can be used multiple times",
action="append",
)
parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
parser.add_argument(
"--show-discovery-debug", help="Enable discovery debug log", action="store_true"
)
parser.add_argument(
"--show-zeroconf-debug", help="Enable zeroconf debug log", action="store_true"
)
add_log_arguments(parser)
parser.add_argument(
"--url", help='Media url (default: "%(default)s")', default=MEDIA_URL
)
Expand All @@ -56,18 +48,7 @@
)
args = parser.parse_args()

if args.show_debug:
fmt = "%(asctime)s %(levelname)s (%(threadName)s) [%(name)s] %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.DEBUG)
logging.getLogger("pychromecast.dial").setLevel(logging.INFO)
logging.getLogger("pychromecast.discovery").setLevel(logging.INFO)
if args.show_discovery_debug:
logging.getLogger("pychromecast.dial").setLevel(logging.DEBUG)
logging.getLogger("pychromecast.discovery").setLevel(logging.DEBUG)
if args.show_zeroconf_debug:
print("Zeroconf version: " + zeroconf.__version__)
logging.getLogger("zeroconf").setLevel(logging.DEBUG)
configure_logging(args)

chromecasts, browser = pychromecast.get_listed_chromecasts(
friendly_names=[args.cast], known_hosts=args.known_host
Expand Down
34 changes: 34 additions & 0 deletions examples/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Common helpers and utilities shared by examples."""

import argparse
import logging

import zeroconf


def add_log_arguments(parser: argparse.ArgumentParser) -> None:
"""Add arguments to control logging to the parser."""
parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
parser.add_argument(
"--show-discovery-debug", help="Enable discovery debug log", action="store_true"
)
parser.add_argument(
"--show-zeroconf-debug", help="Enable zeroconf debug log", action="store_true"
)


def configure_logging(args: argparse.Namespace) -> None:
"""Configure logging according to command line arguments."""
fmt = "%(asctime)s %(levelname)s (%(threadName)s) [%(name)s] %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.INFO)

if args.show_debug:
logging.getLogger("pychromecast.dial").setLevel(logging.INFO)
logging.getLogger("pychromecast.discovery").setLevel(logging.INFO)
if args.show_discovery_debug:
logging.getLogger("pychromecast.dial").setLevel(logging.DEBUG)
logging.getLogger("pychromecast.discovery").setLevel(logging.DEBUG)
if args.show_zeroconf_debug:
print("Zeroconf version: " + zeroconf.__version__)
logging.getLogger("zeroconf").setLevel(logging.DEBUG)
26 changes: 4 additions & 22 deletions examples/dashcast_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
# pylint: disable=invalid-name

import argparse
import logging
import sys
import time

import zeroconf

import pychromecast
from pychromecast.controllers import dashcast

from .common import add_log_arguments, configure_logging

# Enable deprecation warnings etc.
if not sys.warnoptions:
import warnings
Expand All @@ -33,27 +32,10 @@
help="Add known host (IP), can be used multiple times",
action="append",
)
parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
parser.add_argument(
"--show-discovery-debug", help="Enable discovery debug log", action="store_true"
)
parser.add_argument(
"--show-zeroconf-debug", help="Enable zeroconf debug log", action="store_true"
)
add_log_arguments(parser)
args = parser.parse_args()

if args.show_debug:
fmt = "%(asctime)s %(levelname)s (%(threadName)s) [%(name)s] %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.DEBUG)
logging.getLogger("pychromecast.dial").setLevel(logging.INFO)
logging.getLogger("pychromecast.discovery").setLevel(logging.INFO)
if args.show_discovery_debug:
logging.getLogger("pychromecast.dial").setLevel(logging.DEBUG)
logging.getLogger("pychromecast.discovery").setLevel(logging.DEBUG)
if args.show_zeroconf_debug:
print("Zeroconf version: " + zeroconf.__version__)
logging.getLogger("zeroconf").setLevel(logging.DEBUG)
configure_logging(args)

chromecasts, browser = pychromecast.get_listed_chromecasts(
friendly_names=[args.cast], known_hosts=args.known_host
Expand Down
26 changes: 4 additions & 22 deletions examples/default_media_receiver_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
# pylint: disable=invalid-name

import argparse
import logging
import sys
from time import sleep

import zeroconf

import pychromecast
from pychromecast import quick_play

from .common import add_log_arguments, configure_logging

# Enable deprecation warnings etc.
if not sys.warnoptions:
import warnings
Expand All @@ -39,30 +38,13 @@
help="Add known host (IP), can be used multiple times",
action="append",
)
parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
parser.add_argument(
"--show-discovery-debug", help="Enable discovery debug log", action="store_true"
)
parser.add_argument(
"--show-zeroconf-debug", help="Enable zeroconf debug log", action="store_true"
)
add_log_arguments(parser)
parser.add_argument(
"--url", help='Media url (default: "%(default)s")', default=MEDIA_URL
)
args = parser.parse_args()

if args.show_debug:
fmt = "%(asctime)s %(levelname)s (%(threadName)s) [%(name)s] %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.DEBUG)
logging.getLogger("pychromecast.dial").setLevel(logging.INFO)
logging.getLogger("pychromecast.discovery").setLevel(logging.INFO)
if args.show_discovery_debug:
logging.getLogger("pychromecast.dial").setLevel(logging.DEBUG)
logging.getLogger("pychromecast.discovery").setLevel(logging.DEBUG)
if args.show_zeroconf_debug:
print("Zeroconf version: " + zeroconf.__version__)
logging.getLogger("zeroconf").setLevel(logging.DEBUG)
configure_logging(args)

chromecasts, browser = pychromecast.get_listed_chromecasts(
friendly_names=[args.cast], known_hosts=args.known_host
Expand Down
24 changes: 4 additions & 20 deletions examples/discovery_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# pylint: disable=invalid-name

import argparse
import logging
import sys
import time
from uuid import UUID
Expand All @@ -14,6 +13,8 @@
import pychromecast
from pychromecast import CastInfo

from .common import add_log_arguments, configure_logging

# Enable deprecation warnings etc.
if not sys.warnoptions:
import warnings
Expand All @@ -33,30 +34,13 @@
help="Zeroconf will be used even if --known-host is present",
action="store_true",
)
parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
parser.add_argument(
"--show-discovery-debug", help="Enable discovery debug log", action="store_true"
)
parser.add_argument(
"--show-zeroconf-debug", help="Enable zeroconf debug log", action="store_true"
)
add_log_arguments(parser)
parser.add_argument(
"--verbose", help="Full display of discovered devices", action="store_true"
)
args = parser.parse_args()

if args.show_debug:
fmt = "%(asctime)s %(levelname)s (%(threadName)s) [%(name)s] %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.DEBUG)
logging.getLogger("pychromecast.dial").setLevel(logging.INFO)
logging.getLogger("pychromecast.discovery").setLevel(logging.INFO)
if args.show_discovery_debug:
logging.getLogger("pychromecast.dial").setLevel(logging.DEBUG)
logging.getLogger("pychromecast.discovery").setLevel(logging.DEBUG)
if args.show_zeroconf_debug:
print("Zeroconf version: " + zeroconf.__version__)
logging.getLogger("zeroconf").setLevel(logging.DEBUG)
configure_logging(args)


def list_devices() -> None:
Expand Down
26 changes: 4 additions & 22 deletions examples/discovery_example2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
# pylint: disable=invalid-name

import argparse
import logging
import sys

import zeroconf

import pychromecast

from .common import add_log_arguments, configure_logging

# Enable deprecation warnings etc.
if not sys.warnoptions:
import warnings
Expand All @@ -25,30 +24,13 @@
help="Add known host (IP), can be used multiple times",
action="append",
)
parser.add_argument("--show-debug", help="Enable debug log", action="store_true")
parser.add_argument(
"--show-discovery-debug", help="Enable discovery debug log", action="store_true"
)
parser.add_argument(
"--show-zeroconf-debug", help="Enable zeroconf debug log", action="store_true"
)
add_log_arguments(parser)
parser.add_argument(
"--verbose", help="Full display of discovered devices", action="store_true"
)
args = parser.parse_args()

if args.show_debug:
fmt = "%(asctime)s %(levelname)s (%(threadName)s) [%(name)s] %(message)s"
datefmt = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(format=fmt, datefmt=datefmt, level=logging.DEBUG)
logging.getLogger("pychromecast.dial").setLevel(logging.INFO)
logging.getLogger("pychromecast.discovery").setLevel(logging.INFO)
if args.show_discovery_debug:
logging.getLogger("pychromecast.dial").setLevel(logging.DEBUG)
logging.getLogger("pychromecast.discovery").setLevel(logging.DEBUG)
if args.show_zeroconf_debug:
print("Zeroconf version: " + zeroconf.__version__)
logging.getLogger("zeroconf").setLevel(logging.DEBUG)
configure_logging(args)

devices, browser = pychromecast.discovery.discover_chromecasts(
known_hosts=args.known_host
Expand Down
Loading