File: utils.py

package info (click to toggle)
python-logi-circle 0.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 300 kB
  • sloc: python: 1,685; xml: 16; sh: 5; makefile: 4
file content (41 lines) | stat: -rw-r--r-- 1,193 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"""Utilities library shared by the Logi, Camera and Activity classes."""
# coding: utf-8
# vim:sw=4:ts=4:et:
import logging
import slugify

_LOGGER = logging.getLogger(__name__)


def _write_to_file(data, filename, open_mode='wb'):  # pragma: no cover
    """Write binary object directly to file."""
    with open(filename, open_mode) as file_handle:
        file_handle.write(data)


async def _stream_to_file(stream, filename, open_mode='wb'):
    """Stream aiohttp response to file."""
    with open(filename, open_mode) as file_handle:
        while True:
            chunk = await stream.read(1024)
            if not chunk:
                break
            file_handle.write(chunk)


def _get_ids_for_cameras(cameras):
    """Get list of camera IDs from cameras"""
    return list(map(lambda camera: camera.id, cameras))


def _get_camera_from_id(camera_id, cameras):
    """Get Camera object from ID"""
    camera = list(filter(lambda cam: camera_id == cam.id, cameras))
    if camera:
        return camera[0]
    raise ValueError("No camera found with ID %s" % (camera_id))


def _slugify_string(text):
    """Slugify a given text."""
    return slugify.slugify(text, separator='_')