Пример #1
0
def start_server():
    import sys

    global config
    global configfile

    configfile_expanded = os.path.expanduser(configfile)

    if not os.path.exists(configfile_expanded):
        with open(configfile_expanded, 'wb') as c:
            # write minimal default config
            c.write("[evopedia]\n" +
                    "version = 0.3.0\n" +
                    "listen_address = 127.0.0.1\n" +
                    "port = 8080\n" +
                    "use_gps = yes\n" +
                    "maptile_repositories = " +
                        get_default_repositories() + "\n" +
                    "data_directory = ~/\n")

    config = ConfigParser.RawConfigParser()
    config.read(configfile_expanded)

    port = config.getint('evopedia', 'port')
    address = config.get('evopedia', 'listen_address')
    use_gps = config.get('evopedia', 'use_gps')
    repostring = config.get('evopedia', 'maptile_repositories')
    if repostring == '':
        config.set('evopedia', 'maptile_repositories',
                get_default_repositories())
        try:
            with open(configfile_expanded, 'wb') as f:
                config.write(f)
        except:
            print("Unable to write config file.")
    data_dir = config.get('evopedia', 'data_directory')

    data_dir = os.path.expanduser(data_dir)
    if not os.path.exists(data_dir):
        print("Data directory %s not found." % data_dir)

    global tile_repository
    tile_repository = TileRepo.parse_tilerepos(repostring)
    print "Using map tile repositories " + str([x.title
                                            for x in tile_repository])

    global gps_handler
    if use_gps == 'yes' or use_gps == '1':
        print("Enabling GPS...")
        gps_handler = GPSHandler.handler_factory()

    global storage
    global storage_class
    print "Using datafile storage."
    from datafile_storage import DatafileStorage, DatafileInitializationError
    storage_class = DatafileStorage
    storage = DatafileStorage()
    try:
        storage.storage_init_read(data_dir)
    except DatafileInitializationError:
        print("Error opening storage.")
        import traceback
        traceback.print_exc()
    except Exception:
        print("Error opening storage.")
        import traceback
        traceback.print_exc()

    import socket

    try:
        server = ThreadingHTTPServer((address, port), EvopediaHandler)
    except socket.error:
        print 'Socket error. Perhaps there is already some server ' + \
                'running. Exiting.'
        sys.exit(1)
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down server'
        server.socket.close()
        sys.exit(0)
Пример #2
0
                                                title.encode('utf-8')))
                self.end_headers()
            else:
                self.output_error_msg_page("Error finding random page...")
            return
        elif parts[0] == 'choose_data':
            self.output_data_selector(parts, dict)
            return
        elif parts[0] == 'set_data':
            data_dir = dict['path'][0]
            print "Changing datafile storage to %s." % data_dir
            global storage_class
            from datafile_storage import DatafileStorage, \
                                DatafileInitializationError
            storage_class = DatafileStorage
            storage = DatafileStorage()
            try:
                storage.storage_init_read(data_dir)
            except Exception, e:
                self.output_error_msg_page('Error opening datafile storage '
                        'at %s:<br />%s' % (data_dir, str(e)))
                return

            if storage.is_readable():
                global config
                global configfile
                config.set('evopedia', 'data_directory', data_dir)
                with open(os.path.expanduser(configfile), 'wb') as f:
                    config.write(f)
            self.send_response(302)
            self.send_header('Location', '/')