def start(self, *pages): """ Takes a list of Page instances, creates html files, and starts the server. """ # Make sure at least one page has been given: if not (pages): print "*Can't start server - no pages provided." return # Make sure pages/ directory exists: if not (os.path.exists(PAGES_DIR)): os.system("mkdir %s" % PAGES_DIR) # Remove old pages if any: if (os.listdir(PAGES_DIR)): os.system("rm %s/*" % PAGES_DIR) # We treat the first page passed in as the home page and create # an index.html page in the base directory that redirects to it: home = pages[0] with open(INDEX, 'w') as index: with open(INDEX_TEMPLATE, 'r') as index_template: index.write(index_template.read() % home.filename) # Generate a list of links for the sidebar: links = '' for page in pages: links += '<li><a href="%s">%s</a></li>\n' % (page.filename, page.title) # Add sidebar to each page and write them to files: for page in pages: path = "%s/%s" % (PAGES_DIR, page.filename) with open(path, 'w') as f: f.write(str(page) % links) # The server is started as a subprocess using PyBBIO's SafeProcess. # This way it will be non-blocking and stop automatically during # PyBBIO's cleanup routine. self._server_process = SafeProcess(target=self._server.serve_forever) self._server_process.start() if (self.blocking): try: while (True): delay(10000) except KeyboardInterrupt: pass
def setup(): p = SafeProcess(target=foo) p.start()