YaK:: Bedstraw Automation [Changes]   [Calendar]   [Search]   [Index]   [PhotoTags]   
[mega_changes]
[photos]

Bedstraw Automation

# cat bedstraw/boxen.model
101001;m;11,21,60;!
101002;m;12,22,60;s;0.5,0.5,0.5
101003;m;13,23,60
101004;m;14,24,60
101005;m;15,25,60

import BaseHTTPServer as B
import sys
import re
import time
import traceback

HOST_NAME = 'xxx.xxx'
PORT_NUMBER = xxx
MAX_BYTES = 1500

NICE = re.compile('^[a-z0-9]+$').match
LINENO = re.compile('^([0-9]+)').match

AFTER = re.compile('a=([0-9]+)').search
MODEL = re.compile('m=([a-z0-9_]+)').search

class MyHandler(B.BaseHTTPRequestHandler):
  def do_GET(s):
    """Respond to a GET request."""
    s.send_response(200)
    s.send_header("Content-type", "text/plain")
    s.end_headers()
    w = s.wfile
    print >>sys.stderr, "\n\nPATH: %s" % s.path

    if s.path == "/favicon.ico":
      return

    try:
      m = MODEL(s.path).group(1)
      a = int(AFTER(s.path).group(1))
      if not NICE(m):
        raise "not nice"
      fd = open('bedstraw/%s.model' % m)
      b = 0
      broke = False
      print >>w, "=%s" % m
      for line in fd:
        line = line.strip()
        i = int(LINENO(line).group(1))
        if i > a:
          print >>w, line
          b += len(line) + 2
        if b > MAX_BYTES:
          broke = True
          break
      if not broke:
        print >>w, "0;eof"
      print >>sys.stderr, "OKAY: %s" % s.path

    except:
      print >>w, "0;FAIL;%s" % repr(sys.exc_info())
      print >>sys.stderr, "0;FAIL;%s" % repr(sys.exc_info())
      traceback.print_exc()
      traceback.print_stack()
      traceback.format_stack()
      sys.stderr.flush()
    if __name__ == '__main__':
  httpd = B.HTTPServer((HOST_NAME, PORT_NUMBER), MyHandler)
  print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  try:
    httpd.serve_forever()
  except KeyboardInterrupt:
    pass
  httpd.server_close()
  print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)


(unless otherwise marked) Copyright 2002-2014 YakPeople. All rights reserved.
(last modified 2010-12-21)       [Login]
(No back references.)