Ejemplo n.º 1
0
    def main(self, path):
        #import tornado stuff
        import tornado.web, tornado.httpserver, tornado.ioloop, tornado.options
        from tornado.options import options
        from config import options_setup
        from whirlwind.db.mongo import Mongo

        #parse the app config
        tornado.options.parse_config_file(
            os.path.join(path, 'config/settings.py'))
        #parse the command line args
        tornado.options.parse_command_line()

        #connect to our db using our options set in settings.py
        Mongo.create(host=options.db_host, port=options.db_port)

        #init our url routes
        url_routes = self.init_routes()

        #init a logger
        self.init_logging(options.log)

        #add in any app settings
        settings = {
            "static_path": options.static_path,
            "cookie_secret": options.cookie_secret,
            "login_url": options.login_url,
        }

        #setup the controller action routes
        self.application = tornado.web.Application(url_routes, **settings)

        #instantiate a server instance
        http_server = tornado.httpserver.HTTPServer(self.application)

        #bind server to port
        http_server.listen(options.port)

        #log our start message
        Log.info("Ready and listening")

        #start the server
        tornado.ioloop.IOLoop.instance().start()
Ejemplo n.º 2
0
 def main(self):
     #import tornado stuff
     import tornado.web, tornado.httpserver, tornado.ioloop, tornado.options
     from tornado.options import options
     from config import options_setup
     from whirlwind.db.mongo import Mongo
     
     #parse the app config
     tornado.options.parse_config_file(os.path.join(os.path.dirname(__file__),'config/settings.py'))
     #parse the command line args
     tornado.options.parse_command_line()
     
     #connect to our db using our options set in settings.py
     Mongo.create(host=options.db_host, port=options.db_port)
     
     #init our url routes
     url_routes = self.init_routes()
     
     #init a logger
     self.init_logging(options.log)
     
     #add in any app settings 
     settings = {
         "static_path": options.static_path,
         "cookie_secret": options.cookie_secret,
         "login_url": options.login_url,
     }
     
     #setup the controller action routes
     self.application = tornado.web.Application(url_routes,**settings)
     
     #instantiate a server instance
     http_server = tornado.httpserver.HTTPServer(self.application)
     
     #bind server to port
     http_server.listen(options.port)
     
     #log our start message
     Log.info("Ready and listening")
     
     #start the server
     tornado.ioloop.IOLoop.instance().start()
Ejemplo n.º 3
0
	def init_mongo(self):
		#connect to mongo
		Mongo.create(
			host=options.db_host, 
			port=options.db_port
		)
Ejemplo n.º 4
0
def main():

    usage = "usage: %prog [options [args]]"
    parser = optparse.OptionParser(usage)

    parser.add_option("--ca",
                      "--create-application",
                      dest="create_app",
                      metavar="FILE",
                      action="store_true",
                      default=False,
                      help="Creates an application structure")

    parser.add_option(
        "--v",
        "--version",
        dest="version",
        action="store_true",
        default=False,
        help="Print the version info for this release of WhirlWind")

    parser.add_option("--gcs",
                      "--generate-cookie-secret",
                      dest="generate_cookie_secret",
                      action="store_true",
                      default=False,
                      help="Generate a cookie secret hash")

    parser.add_option("--gmi",
                      "--generate-model-indexes",
                      dest="generate_model_indexes",
                      action="store_true",
                      default=False,
                      help="Generate mongo indexes for your models")

    options, args = parser.parse_args()

    if not options.create_app and not options.version and not options.generate_cookie_secret and not options.generate_model_indexes:
        parser.error('Must choose one -- try --ca or --v or --gcs or --gmi')

    if options.create_app:

        if len(args) != 1:
            logging.error("Error no app name given")
            return

        #generate the template dir path
        template_dir = os.path.join(whirlwind.__path__[0], 'conf',
                                    'app_template')

        #copy the template files
        copied_files = dir_util.copy_tree(template_dir, args[0])

        #check that we copied files
        if len(copied_files) > 0:
            logging.info('Created %s' % options.create_app)
        else:
            logging.info('Error copying app template')

    if options.version:
        logging.info(whirlwind.get_version())

    if options.generate_cookie_secret:
        import base64
        import uuid
        print(base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes))

    if options.generate_model_indexes:

        import sys
        import pkgutil
        from whirlwind.db.mongo import Mongo

        #insert the current dir into the path
        sys.path.insert(0, os.getcwd())

        #grab a settings path from our args if exists or assume relative default
        settings_module = args[0] if len(args) == 1 else 'config.settings'

        conf = __import__(settings_module)

        #connect to our db using our options set in settings.py
        Mongo.create(host=conf.settings.db_host, port=conf.settings.db_port)

        #import our default models package
        __import__('application.models')

        pkg_mods = sys.modules['application.models']

        #setup a prefix string
        prefix = pkg_mods.__name__ + "."

        #import all the modules in the models dir so the registration decorators fire
        for importer, modname, ispkg in pkgutil.iter_modules(
                pkg_mods.__path__, prefix):
            __import__(modname)

        #loop over the registered documents
        for doc, obj in Mongo.db.connection._registered_documents.iteritems():
            try:

                print 'Attempting to create index for ', doc
                #generate the index for this doc on the collection
                obj.generate_index(Mongo.db.connection[conf.settings.db_name][
                    obj._obj_class.__collection__])
            except Exception, e:
                #barf up an error on fail
                print 'Could not create index for %s - exception: %s' % (
                    doc, e.message)
Ejemplo n.º 5
0
def main():

    usage = "usage: %prog [options [args]]"
    parser = optparse.OptionParser(usage)

    parser.add_option("--ca", "--create-application",
                    dest="create_app",
                    metavar="FILE",
                    action="store_true",
                    default=False,
                    help="Creates an application structure")

    parser.add_option("--v", "--version", dest="version",
                    action="store_true",
                    default=False,
                    help="Print the version info for this release of WhirlWind")

    parser.add_option("--gcs", "--generate-cookie-secret", dest="generate_cookie_secret",
                    action="store_true",
                    default=False,
                    help="Generate a cookie secret hash")

    parser.add_option("--gmi", "--generate-model-indexes", dest="generate_model_indexes",
                action="store_true",
                default=False,
                help="Generate mongo indexes for your models")

    options, args = parser.parse_args()

    if not options.create_app and not options.version and not options.generate_cookie_secret and not options.generate_model_indexes:
        parser.error('Must choose one -- try --ca or --v or --gcs or --gmi')

    if options.create_app:

        if len(args) != 1:
            logging.error("Error no app name given")
            return

        #generate the template dir path
        template_dir = os.path.join(whirlwind.__path__[0], 'conf', 'app_template')

        #copy the template files
        copied_files = dir_util.copy_tree(template_dir, args[0])

        #check that we copied files
        if len(copied_files) > 0:
            logging.info('Created %s' % options.create_app)
        else:
            logging.info('Error copying app template')

    if options.version:
        logging.info(whirlwind.get_version())

    if options.generate_cookie_secret:
        import base64
        import uuid
        print(base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes))

    if options.generate_model_indexes:

        import sys
        import pkgutil
        from whirlwind.db.mongo import Mongo

        #insert the current dir into the path
        sys.path.insert(0, os.getcwd())

        #grab a settings path from our args if exists or assume relative default
        settings_module = args[0] if len(args) == 1 else 'config.settings'

        conf = __import__(settings_module)

        #connect to our db using our options set in settings.py
        Mongo.create(host=conf.settings.db_host, port=conf.settings.db_port)

        #import our default models package
        __import__('application.models')

        pkg_mods = sys.modules['application.models']

        #setup a prefix string
        prefix = pkg_mods.__name__ + "."

        #import all the modules in the models dir so the registration decorators fire
        for importer, modname, ispkg in pkgutil.iter_modules(pkg_mods.__path__, prefix):
            __import__(modname)

        #loop over the registered documents
        for doc, obj in Mongo.db.connection._registered_documents.iteritems():
            try:

                print 'Attempting to create index for ', doc
                #generate the index for this doc on the collection
                obj.generate_index(Mongo.db.connection[conf.settings.db_name][obj._obj_class.__collection__])
            except Exception, e:
                #barf up an error on fail
                print 'Could not create index for %s - exception: %s' % (doc, e.message)