示例#1
0
def main():
    import optparse
    parser = optparse.OptionParser('usage: %prog <prefix>')
    parser.add_option('-p',
                      '--pretty',
                      action='store_true',
                      default=False,
                      help='Pretty-print JSON objects')
    ZmqSubscriber.addOptions(parser, 'zmqGrep')
    opts, args = parser.parse_args()
    if len(args) != 1:
        parser.error('expected exactly 1 arg')
    logging.basicConfig(level=logging.DEBUG)

    # set up networking
    s = ZmqSubscriber(**ZmqSubscriber.getOptionValues(opts))
    s.start()

    # subscribe to the message we want
    topic = args[0]
    if opts.pretty:
        s.subscribeJson(topic, handleMessagePretty)
    else:
        s.subscribeRaw(topic, handleMessageSimple)

    zmqLoop()
示例#2
0
def main():
    import optparse
    parser = optparse.OptionParser('usage: %prog <prefix>')
    parser.add_option('-p', '--pretty',
                      action='store_true', default=False,
                      help='Pretty-print JSON objects')
    ZmqSubscriber.addOptions(parser, 'zmqGrep')
    opts, args = parser.parse_args()
    if len(args) != 1:
        parser.error('expected exactly 1 arg')
    logging.basicConfig(level=logging.DEBUG)

    # set up networking
    s = ZmqSubscriber(**ZmqSubscriber.getOptionValues(opts))
    s.start()

    # subscribe to the message we want
    topic = args[0]
    if opts.pretty:
        s.subscribeJson(topic, handleMessagePretty)
    else:
        s.subscribeRaw(topic, handleMessageSimple)

    zmqLoop()
示例#3
0
class FilePublisher(object):
    def __init__(self, opts):
        self.sources = ([PatternFileSource(x) for x in opts.watchFilePattern] +
                        [DirectoryFileSource(x) for x in opts.watchDirectory] +
                        [SymlinkFileSource(x) for x in opts.watchSymlink])

        self.subtopic = opts.subtopic

        self.pollTimer = None
        self.stopPollingTime = None
        self.imageProcessor = None
        self.tmpDir = tempfile.mkdtemp(prefix='filePublisher')

        opts.moduleName = opts.moduleName.format(subtopic=opts.subtopic)
        self.publisher = ZmqPublisher(**ZmqPublisher.getOptionValues(opts))
        self.subscriber = ZmqSubscriber(**ZmqSubscriber.getOptionValues(opts))

    def getTopic(self, msgType):
        return 'geocamUtil.filePublisher.%s.%s' % (self.subtopic, msgType)

    def start(self):
        self.publisher.start()
        self.subscriber.start()
        self.subscriber.subscribeJson(self.getTopic('request'),
                                      self.handleRequest)

    def handleRequest(self, topic, requestDict):
        logging.debug('handleRequest %s', json.dumps(requestDict))
        self.requestTimeout = requestDict['timeout']
        self.pollPeriod = requestDict['pollPeriod']
        self.timestampSpacing = requestDict.get('timestampSpacing')
        if 'imageResize' in requestDict or 'imageCrop' in requestDict or 'imageFormat' in requestDict:
            self.imageProcessor = ImageProcessor(resize=requestDict.get('imageResize'),
                                                 crop=requestDict.get('imageCrop'),
                                                 fmt=requestDict.get('imageFormat'),
                                                 tmpDir=self.tmpDir)
        else:
            self.imageProcessor = None

        self.stopPollingTime = time.time() + self.requestTimeout

        self.publisher.sendRaw(self.getTopic('response'), 'ok')

        if self.pollTimer:
            self.pollTimer.stop()
            self.pollTimer = None
        self.pollTimer = ioloop.PeriodicCallback(self.pollHandler,
                                                 self.pollPeriod * 1000)
        self.pollTimer.start()

    def pollHandler(self):
        try:
            self.pollHandler0()
        except:  # pylint: disable=W0702
            logging.warning('%s', traceback.format_exc())

    def pollHandler0(self):
        logging.debug('pollHandler')

        if time.time() > self.stopPollingTime:
            logging.info('request timed out, stopping polling')
            self.pollTimer.stop()
            self.pollTimer = None

        for source in self.sources:
            newFileInfo = source.checkForNewFileAndRemember(self.timestampSpacing)
            if newFileInfo:
                self.publishFile(newFileInfo)

    def publishFile(self, fileInfo):
        path, mtime = fileInfo
        if self.imageProcessor and self.imageProcessor.isImage(path):
            processedPath = self.imageProcessor.processImage(path)
            logging.debug('sending %s', processedPath)
            self.publisher.sendJson(self.getTopic('file'),
                                    {'file': getFileDict(processedPath, mtime)})
            os.unlink(processedPath)
        else:
            logging.debug('sending %s', path)
            self.publisher.sendJson(self.getTopic('file'),
                                    {'file': getFileDict(path, mtime)})
示例#4
0
class FileReceiver(object):
    def __init__(self, opts):
        self.request = {
            'timeout': opts.timeout,
            'pollPeriod': opts.pollPeriod,
        }
        if opts.timestampSpacing:
            self.request['timestampSpacing'] = opts.timestampSpacing
        if opts.imageResize:
            self.request['imageResize'] = parseImageResize(opts.imageResize)
        if opts.imageCrop:
            self.request['imageCrop'] = parseImageCrop(opts.imageCrop)
        if opts.imageFormat:
            self.request['imageFormat'] = opts.imageFormat

        self.outputDirectory = opts.output
        self.subtopic = opts.subtopic
        self.noRequest = opts.noRequest

        opts.moduleName = opts.moduleName.format(subtopic=opts.subtopic)
        self.publisher = ZmqPublisher(**ZmqPublisher.getOptionValues(opts))
        self.subscriber = ZmqSubscriber(**ZmqSubscriber.getOptionValues(opts))

        self.requestPeriod = 0.5 * opts.timeout

    def getTopic(self, msgType):
        return 'geocamUtil.filePublisher.%s.%s' % (self.subtopic, msgType)

    def start(self):
        self.publisher.start()
        self.subscriber.start()

        self.subscriber.subscribeJson(self.getTopic('file'),
                                      self.handleFile)
        self.subscriber.subscribeRaw(self.getTopic('response'),
                                     self.handleResponse)

        if not self.noRequest:
            self.sendRequest()
            requestTimer = ioloop.PeriodicCallback(self.sendRequest,
                                                   self.requestPeriod * 1000)
            requestTimer.start()

    def sendRequest(self):
        logging.debug('sendRequest')
        self.publisher.sendJson(self.getTopic('request'),
                                self.request)

    def handleResponse(self, topic, msg):
        logging.debug('received response: %s', repr(msg))
        # nothing to do

    def handleFile(self, topic, msg):
        try:
            self.handleFile0(topic, msg)
        except:  # pylint: disable=W0702
            logging.warning('%s', traceback.format_exc())

    def handleFile0(self, topic, msg):
        f = msg['file']
        outputPath = os.path.join(self.outputDirectory, f['filename'])
        _fmt, data = f['contents'].split(':', 1)
        contents = base64.b64decode(data)
        file(outputPath, 'w').write(contents)
        logging.debug('wrote %s bytes to %s', len(contents), outputPath)
示例#5
0
class FileReceiver(object):
    def __init__(self, opts):
        self.request = {
            'timeout': opts.timeout,
            'pollPeriod': opts.pollPeriod,
        }
        if opts.timestampSpacing:
            self.request['timestampSpacing'] = opts.timestampSpacing
        if opts.imageResize:
            self.request['imageResize'] = parseImageResize(opts.imageResize)
        if opts.imageCrop:
            self.request['imageCrop'] = parseImageCrop(opts.imageCrop)
        if opts.imageFormat:
            self.request['imageFormat'] = opts.imageFormat

        self.outputDirectory = opts.output
        self.subtopic = opts.subtopic
        self.noRequest = opts.noRequest

        opts.moduleName = opts.moduleName.format(subtopic=opts.subtopic)
        self.publisher = ZmqPublisher(**ZmqPublisher.getOptionValues(opts))
        self.subscriber = ZmqSubscriber(**ZmqSubscriber.getOptionValues(opts))

        self.requestPeriod = 0.5 * opts.timeout

    def getTopic(self, msgType):
        return 'geocamUtil.filePublisher.%s.%s' % (self.subtopic, msgType)

    def start(self):
        self.publisher.start()
        self.subscriber.start()

        self.subscriber.subscribeJson(self.getTopic('file'),
                                      self.handleFile)
        self.subscriber.subscribeRaw(self.getTopic('response'),
                                     self.handleResponse)

        if not self.noRequest:
            self.sendRequest()
            requestTimer = ioloop.PeriodicCallback(self.sendRequest,
                                                   self.requestPeriod * 1000)
            requestTimer.start()

    def sendRequest(self):
        logging.debug('sendRequest')
        self.publisher.sendJson(self.getTopic('request'),
                                self.request)

    def handleResponse(self, topic, msg):
        logging.debug('received response: %s', repr(msg))
        # nothing to do

    def handleFile(self, topic, msg):
        try:
            self.handleFile0(topic, msg)
        except:  # pylint: disable=W0702
            logging.warning('%s', traceback.format_exc())

    def handleFile0(self, topic, msg):
        f = msg['file']
        outputPath = os.path.join(self.outputDirectory, f['filename'])
        _fmt, data = f['contents'].split(':', 1)
        contents = base64.b64decode(data)
        file(outputPath, 'w').write(contents)
        logging.debug('wrote %s bytes to %s', len(contents), outputPath)