Completed
Push — master ( 0bc61b...16fc24 )
by Thomas
8s
created

doorpi/action/SingleActions/take_snapshot.py (4 issues)

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
4
import logging
5
import os
6
logger = logging.getLogger(__name__)
7
logger.debug("%s loaded", __name__)
8
9
SIPPHONE_SECTION = 'SIP-Phone'
10
DOORPI_SECTION = 'DoorPi'
11
12
from doorpi.action.base import SingleAction
13
import doorpi
14
import subprocess as sub
15
16
conf = doorpi.DoorPi().config
17
18
def take_snapshot(size, path, max):
0 ignored issues
show
Bug Best Practice introduced by pistreamer
This seems to re-define the built-in max.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
19
    if not os.path.exists(path):
20
        logger.info('Path (%s) does not exist - creating it now', path)
21
        os.makedirs(path)
22
23
    lastFile = getLastFilename(path)
24
    lastNr = 1
25
    if (len(lastFile) > 0):
26
        lastNr = int(lastFile[:lastFile.rfind(".jpg")])
27
        if (lastNr+1 <= max):
28
            lastNr = lastNr + 1
29
    else:
30
        lastNr = 1
31
    imageFilename = path + str(lastNr) + ".jpg"
32
    # fswebcam automatically selects the first video device
33
    command = "fswebcam --top-banner -b --font luxisr:20 -r " + size + " " + imageFilename
34
    p = sub.Popen(command, shell=True, stdout=sub.PIPE, stderr=sub.PIPE)
35
    output, errors = p.communicate()
0 ignored issues
show
The variable output seems to be unused.
Loading history...
36
    if (len(errors) > 0):
37
        logger.error('error creating snapshot - maybe fswebcam is missing')
38
    else:
39
        logger.info('snapshot created: %s', imageFilename)
40
    return
41
42
43
def getLastFilename(path):
44
    files = [s for s in os.listdir(path)
45
         if os.path.isfile(os.path.join(path, s))]
46
    files.sort(key=lambda s: os.path.getmtime(os.path.join(path, s)))
47
    if (len(files) == 0):
48
        return ''
49
    return files[len(files)-1]
50
51
def get(parameters):
0 ignored issues
show
The argument parameters seems to be unused.
Loading history...
52
    conf = doorpi.DoorPi().config
0 ignored issues
show
Comprehensibility Bug introduced by pistreamer
conf is re-defining a name which is already available in the outer-scope (previously defined on line 16).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
53
    snapshot_size = conf.get_string(DOORPI_SECTION, 'snapshot_size', '1280x720')
54
    snapshot_path = conf.get_string_parsed(DOORPI_SECTION, 'snapshot_path', '!BASEPATH!/../DoorPiWeb/snapshots/')
55
    number_of_snapshots = conf.get_int(DOORPI_SECTION, 'number_of_snapshots', 10)
56
57
    if len(conf.get(SIPPHONE_SECTION, 'capture_device', '')) > 0:
58
        return SnapShotAction(take_snapshot, size = snapshot_size, path = snapshot_path, max = number_of_snapshots)
59
    logger.warning('can not create snapshot - video disabled')
60
61
class SnapShotAction(SingleAction):
62
    pass
63