Skip to content

Commit

Permalink
Merge branch 'master' of github.com:eastein/zmstream
Browse files Browse the repository at this point in the history
Conflicts:
	zmstream.py
  • Loading branch information
eastein committed Aug 26, 2011
2 parents d22d22e + 583562f commit 5cae17c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.pyc
12 changes: 0 additions & 12 deletions README

This file was deleted.

15 changes: 15 additions & 0 deletions README.md
@@ -0,0 +1,15 @@
ZMStream is a library for creating creating an iterable object for image
frames from a ZoneMinder stream.

<A name="toc2-4" title="Platforms" />
Platforms
-------------------

ZMStream has been tested to work on Ubuntu 10.04 and Debian Squeeze, using CPython 2.6.

<A name="toc2-10" title="Examples" />
Examples
-------------------

* Please see test.py for an example.
* Another use case for zmstream is lidless (https://github.com/eastein/lidless).
13 changes: 13 additions & 0 deletions README.txt
@@ -0,0 +1,13 @@
ZMStream is a library for creating creating an iterable object for image
frames from a ZoneMinder stream.

Platforms
-------------------

ZMStream has been tested to work on Ubuntu 10.04 and Debian Squeeze, using CPython 2.6.

Examples
-------------------

* Please see test.py for an example.
* Another use case for zmstream is lidless (https://github.com/eastein/lidless).
18 changes: 17 additions & 1 deletion zmstream.py
Expand Up @@ -4,14 +4,18 @@
import time
import base64

class Timeout(Exception) :
pass

class ZMStreamer(object) :
ST_FILE = 1
ST_SOCKET = 2

ZF_FRAME_HEADER = '--%s\r\n'

def __init__(self, timeout, input_capture, auth=None, boundary=None) :
def __init__(self, timeout, input_capture, failure_timeout=10, auth=None, boundary=None) :
self.timeout = timeout
self.failure_timeout = failure_timeout
self.ok = True
self.auth = auth
if boundary :
Expand Down Expand Up @@ -50,6 +54,13 @@ def __init__(self, timeout, input_capture, auth=None, boundary=None) :

self.chunk = 1024

def __del__(self) :
if hasattr(self, 'fh') :
try :
self.fh.close()
except :
pass

def rf(self, size) :
if self.stream_type == self.ST_FILE :
r = self.fh.read(size)
Expand Down Expand Up @@ -94,11 +105,16 @@ def read_until(self, buf, including) :
def abortcheck(self) :
if not self.ok :
raise StopIteration
if hasattr(self, 'abort_ts') :
if time.time() > self.abort_ts :
raise Timeout

def generate(self) :
buf = ''
# first, read until there's a ZF_FRAME_HEADER
while True :
self.abort_ts = time.time() + self.failure_timeout

# we haven't already aligned to a zoneminder frame. align now.
buf = self.discard_until(buf, ZMStreamer.ZF_FRAME_HEADER % self.boundary)
header = True
Expand Down

0 comments on commit 5cae17c

Please sign in to comment.