예제 #1
0
    def setup(self):
        if self.client is not None and self.db is not None:
            raise AlreadySetUpError("MongoDB has already been configured.")

        if isinstance(self.config, str):
            log("Using Mongo URI from environment: {0}".format(self.config))

            self.uri = self.config
            client = pymongo.MongoClient(self.uri)
            db = client[self.uri.rsplit("/", 1)[-1]]
        elif self.config.get("default", True):
            client = pymongo.MongoClient()
            db = client.archivesmc
            self.uri = "mongodb://localhost/statik"
        else:
            auth = self.config.get("authentication", None)

            if auth is None:
                self.uri = "mongodb://{0}:{1}/{2}".format(
                    self.config["host"], self.config["port"],
                    self.config["db"])
            else:
                self.uri = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
                    auth["username"], auth["password"], self.config["host"],
                    self.config["port"], self.config["db"])

            client = pymongo.MongoClient(self.uri)
            db = client[self.config["db"]]

        self.client = client
        self.db = db
예제 #2
0
    def __init__(self, app, manager):
        self.app = app
        self.manager = manager

        self._reload()

        app.route("/blog", "GET", self.blog)
        app.route("/blog/", "GET", self.blog)

        app.route("/blog/page", "GET", self.blog_redirect)
        app.route("/blog/page/<page>", "GET", self.blog_page)
        app.route("/blog/page/<page>/", "GET", self.blog_page)

        app.route("/blog/entry", "GET", self.blog_redirect)
        app.route("/blog/entry/<entry>", "GET", self.blog_entry)
        app.route("/blog/entry/<entry>/", "GET", self.blog_entry)

        app.route("/blog/reload/<password>", "GET", self.reload)

        self.watcher = Observer()
        handler = EventHandler(self.watcher_callback)
        self.watcher.schedule(handler, "blog", recursive=True)
        self.watcher.start()

        log("Blog routes set up.")
예제 #3
0
    def __init__(self, app, manager):
        self.app = app
        self.manager = manager

        app.route("/static/<path:path>", ["GET", "POST"], self.static)
        app.route("/css/<path:path>", ["GET", "POST"], self.static_blog_css)
        app.route("/js/<path:path>", ["GET", "POST"], self.static_blog_js)
        app.route("/static/", ["GET", "POST"], self.static_403)
        app.route("/static", ["GET", "POST"], self.static_403)
        app.route("/.well-known/keybase.txt", ["GET", "POST"],
                  self.static_keybase_txt)

        log("Static routes set up.")
예제 #4
0
 def start(self):
     try:
         config = yaml.load(open("development.yml", "r"))
         host = config.get("host", "127.0.0.1")
         port = config.get("port", 8080)
         server = config.get("server", "cherrypy")
     except Exception as e:
         log("Unable to load development config: %s" % e, logging.INFO)
         log("Continuing using the defaults.", logging.INFO)
         host = "127.0.0.1"
         port = 8080
         server = "cherrypy"
     run(host=host, port=port, server=server)
예제 #5
0
 def start(self):
     try:
         config = yaml.load(open("development.yml", "r"))
         host = config.get("host", "127.0.0.1")
         port = config.get("port", 8080)
         server = config.get("server", "cherrypy")
     except Exception as e:
         log("Unable to load development config: %s" % e, logging.INFO)
         log("Continuing using the defaults.", logging.INFO)
         host = "127.0.0.1"
         port = 8080
         server = "cherrypy"
     run(host=host, port=port, server=server)
예제 #6
0
    def __init__(self, app, manager):
        self.app = app
        self.manager = manager

        app.route("/static/<path:path>", ["GET", "POST"], self.static)
        app.route("/css/<path:path>", ["GET", "POST"], self.static_blog_css)
        app.route("/js/<path:path>", ["GET", "POST"], self.static_blog_js)
        app.route("/static/", ["GET", "POST"], self.static_403)
        app.route("/static", ["GET", "POST"], self.static_403)
        app.route("/.well-known/keybase.txt", ["GET", "POST"],
                  self.static_keybase_txt)

        log("Static routes set up.")
예제 #7
0
    def start(self):
        def log_all():
            log_request(request, "{0} {1} ".format(request.method,
                                                   request.fullpath))

        hook('after_request')(log_all)

        try:
            config = yaml.load(open("config/development.yml", "r"))
            host = config.get("host", "127.0.0.1")
            port = config.get("port", 8080)
            server = config.get("server", "cherrypy")
        except Exception as e:
            log("Unable to load development config: {0}".format(e))
            log("Continuing using the defaults.")
            host = "127.0.0.1"
            port = 8080
            server = "cherrypy"

        run(app=self.get_app(), host=host, port=port, server=server)
예제 #8
0
    def __init__(self):
        self.app = default_app()

        @hook('after_request')
        def log_all():
            log_request(request, "%s %s " % (request.method, request.fullpath),
                        logging.INFO)

        self.routes = {}

        files = os.listdir("routes")
        files.remove("__init__.py")

        for _file in files:
            if _file.endswith(".py"):
                module = _file.rsplit(".", 1)[0]
                try:
                    log("Loading routes module '%s'..." % module, logging.INFO)
                    mod = __import__("routes.%s" % module, fromlist=["Routes"])
                    self.routes[module] = mod.Routes(self.app, self)
                except Exception as e:
                    log("Error loading routes module '%s': %s" % (module, e),
                        logging.INFO)

        log("Finished loading routes modules.", logging.INFO)
예제 #9
0
    def reload_entry(self, entry, recount_latest=True):
        base_url = "/blog/entry/%s"

        if not os.path.exists("blog/entries/%s.md" % entry):
            if entry in self.parsed_entries:
                log("Entry deleted: %s" % entry)
                del self.parsed_entries[entry]
                return

        if entry in self.config["entries"]:
            try:
                markdown = parse_markdown("blog/entries/%s.md" % entry)
            except Exception as e:
                log("Unable to parse '%s': %s" % (entry, e), logging.WARN)
            else:
                self.parsed_entries[entry] = markdown
                log("Parsed entry: %s" % entry, logging.INFO)

        if recount_latest:
            self.latest_entries = []
            for i in range(0, 9):
                try:
                    entry = self.config["reversed_entries"][i]
                    markdown = self.parsed_entries[entry]
                    title = markdown.Meta["title"][0]
                    self.latest_entries.append({"title": title,
                                                "url": base_url % entry})
                except IndexError:
                    break  # No more entries
예제 #10
0
    def setup_routes(self):
        files = os.listdir("routes")
        files.remove("__init__.py")

        for _file in files:
            if _file.endswith(".py"):
                module = _file.rsplit(".", 1)[0]
                try:
                    log(
                        "Loading routes module '{0}'".format(module),
                        logging.INFO
                    )
                    mod = __import__(
                        "routes.{0}".format(module),
                        fromlist=["Routes"]
                    )
                    self.routes[module] = mod.Routes(self.app, self)
                except Exception as e:
                    log(
                        "Error loading routes module '{0}': {1}"
                        .format(module, e)
                    )
                    raise

        log("{0} routes set up.".format(len(self.app.routes)))
예제 #11
0
    def __init__(self):
        self.app = default_app()

        self.mongo_conf = os.environ.get("MONGOHQ_URL", None)

        if not self.mongo_conf:
            self.db = yaml.load(open("config/database.yml", "r"))
            self.mongo_conf = self.db["mongo"]

        self.setup_mongo()

        self.routes = {}
        self.api_routes = {}

        files = os.listdir("routes")
        files.remove("__init__.py")

        for _file in files:
            if _file.endswith(".py"):
                module = _file.rsplit(".", 1)[0]
                try:
                    log("Loading routes module '{0}'".format(module),
                        logging.INFO)
                    mod = __import__("routes.{0}".format(module),
                                     fromlist=["Routes"])
                    self.routes[module] = mod.Routes(self.app, self)
                except Exception as e:
                    log("Error loading routes module '{0}': {1}".format(
                        module, e))

        log("{0} routes set up.".format(len(self.app.routes)))
예제 #12
0
    def __init__(self):
        self.app = default_app()

        @hook('after_request')
        def log_all():
            log_request(request, "%s %s " % (request.method, request.fullpath),
                        logging.INFO)

        self.routes = {}

        files = os.listdir("routes")
        files.remove("__init__.py")

        for _file in files:
            if _file.endswith(".py"):
                module = _file.rsplit(".", 1)[0]
                try:
                    log("Loading routes module '%s'..." % module, logging.INFO)
                    mod = __import__("routes.%s" % module, fromlist=["Routes"])
                    self.routes[module] = mod.Routes(self.app, self)
                except Exception as e:
                    log("Error loading routes module '%s': %s" % (module, e),
                        logging.INFO)

        log("Finished loading routes modules.", logging.INFO)
예제 #13
0
    def setup(self):
        if self.client is not None and self.db is not None:
            raise AlreadySetUpError("MongoDB has already been configured.")

        if isinstance(self.config, str):
            log("Using Mongo URI from environment: {0}".format(self.config))

            self.uri = self.config
            client = pymongo.MongoClient(self.uri)
            db = client[self.uri.rsplit("/", 1)[-1]]

            data = os.environ.get("STATIK_COLLECTION_DATA", None)
            ts = os.environ.get("STATIK_COLLECTION_TIMESTAMPS", None)

            if not data:
                log("Unable to find STATIK_COLLECTION_DATA environment "
                    "variable!")
            else:
                self.aliases["data"] = data

            if not ts:
                log("Unable to find STATIK_COLLECTION_TIMESTAMPS environment "
                    "variable!")
            else:
                self.aliases["timestamps"] = ts
        elif self.config.get("default", True):
            client = pymongo.MongoClient()
            db = client.archivesmc
            self.uri = "mongodb://localhost/statik"

            self.aliases = {
                "data": "data_collection",
                "timestamps": "timestamps"
            }
        else:
            auth = self.config.get("authentication", None)

            if auth is None:
                self.uri = "mongodb://{0}:{1}/{2}".format(
                    self.config["host"],
                    self.config["port"],
                    self.config["db"]
                )
            else:
                self.uri = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
                    auth["username"],
                    auth["password"],
                    self.config["host"],
                    self.config["port"],
                    self.config["db"]
                )

            client = pymongo.MongoClient(self.uri)
            db = client[self.config["db"]]

            self.aliases = self.config.get("collections", {})

        self.client = client
        self.db = db
예제 #14
0
    def start(self):
        def log_all():
            log_request(
                request,
                "{0} {1} ".format(request.method, request.fullpath)
            )

        hook('after_request')(log_all)

        try:
            config = yaml.load(open("config/development.yml", "r"))
            host = config.get("host", "127.0.0.1")
            port = config.get("port", 8080)
            server = config.get("server", "cherrypy")
        except Exception as e:
            log("Unable to load development config: {0}".format(e))
            log("Continuing using the defaults.")
            host = "127.0.0.1"
            port = 8080
            server = "cherrypy"

        run(app=self.get_app(), host=host, port=port, server=server)
예제 #15
0
    def watcher_callback(self, event):

        if not event.src_path:
            # Why would that happen?
            log("No source path: %s" % event, logging.WARN)
            return

        if event.is_directory or event.src_path.endswith("~"):
            # The latter is for Vim support
            return

        if event.event_type != "moved":
            if event.src_path.endswith(("___jb_bak___", "___jb_old___")):
                # IntelliJ IDE support
                return
        else:
            if event.dest_path.endswith(("___jb_bak___", "___jb_old___")):
                # IntelliJ IDE support
                return

        path = os.path.relpath(event.src_path).replace("\\", "/")
        split_path = path.split("/")
        split_path.pop(0)  # blog

        if split_path[-1].startswith(".") or not "." in split_path[-1]:
            # Vim and dot-files support
            return

        if event.event_type == "modified":
            log("Modified: %s" % path, logging.INFO)

            if split_path[0] == "config.yml":
                self.reload_config()
            elif split_path[0] == "entries":
                self.reload_entry(split_path[1].rstrip(".md"))
            elif split_path[0] == "pages":
                self.reload_page(split_path[1])
예제 #16
0
파일: db.py 프로젝트: Ribesg/Statik-API
    def setup(self):
        if self.client is not None and self.db is not None:
            raise AlreadySetUpError("MongoDB has already been configured.")

        if isinstance(self.config, str):
            log("Using Mongo URI from environment: {0}".format(self.config))

            self.uri = self.config
            client = pymongo.MongoClient(self.uri)
            db = client[self.uri.rsplit("/", 1)[-1]]
        elif self.config.get("default", True):
            client = pymongo.MongoClient()
            db = client.archivesmc
            self.uri = "mongodb://localhost/statik"
        else:
            auth = self.config.get("authentication", None)

            if auth is None:
                self.uri = "mongodb://{0}:{1}/{2}".format(
                    self.config["host"],
                    self.config["port"],
                    self.config["db"]
                )
            else:
                self.uri = "mongodb://{0}:{1}@{2}:{3}/{4}".format(
                    auth["username"],
                    auth["password"],
                    self.config["host"],
                    self.config["port"],
                    self.config["db"]
                )

            client = pymongo.MongoClient(self.uri)
            db = client[self.config["db"]]

        self.client = client
        self.db = db
예제 #17
0
    def setup_mongo(self):
        try:
            self.mongo = Db(self.mongo_conf)
            self.mongo.setup()

            for key in schemas.keys():
                log("Adding schema for collection: {0}".format(key))
                self.mongo.add_schema(key, schemas[key])

            self.mongo.client.admin.command("ping")
            log("Set up Mongo successfully.")
        except Exception as e:
            log("Unable to set up Mongo: {0}".format(e), logging.ERROR)
예제 #18
0
    def setup_mongo(self):
        try:
            self.mongo = Db(self.mongo_conf)
            self.mongo.setup()

            for key in schemas.keys():
                log("Adding schema for collection: {0}".format(key))
                self.mongo.add_schema(key, schemas[key])

            self.mongo.client.admin.command("ping")
            log("Set up Mongo successfully.")
        except Exception as e:
            log("Unable to set up Mongo: {0}".format(e), logging.ERROR)
예제 #19
0
 def reload_page(self, filename):
     if filename.endswith(".md"):
         name = filename.rsplit(".", 1)[0]
         friendly = name.replace("_", " ").title()
         if not os.path.exists("blog/pages/%s" % filename):
             if friendly in self.all_pages:
                 del self.all_pages[friendly]
                 log("Page deleted: %s" % friendly, logging.INFO)
                 return
         try:
             markdown = parse_markdown("blog/pages/%s" % filename)
         except Exception as e:
             log("Unable to parse '%s': %s" % (name, e), logging.WARN)
         else:
             self.all_pages[friendly] = "/blog/page/%s" % name
             self.parsed_pages[name] = markdown
             log("Parsed page: %s" % name, logging.INFO)
예제 #20
0
    def __init__(self):
        self.app = default_app()

        self.mongo_conf = os.environ.get("MONGOHQ_URL", None)

        if not self.mongo_conf:
            self.db = yaml.load(open("config/database.yml", "r"))
            self.mongo_conf = self.db["mongo"]

        self.setup_mongo()

        self.routes = {}
        self.api_routes = {}

        files = os.listdir("routes")
        files.remove("__init__.py")

        for _file in files:
            if _file.endswith(".py"):
                module = _file.rsplit(".", 1)[0]
                try:
                    log(
                        "Loading routes module '{0}'".format(module),
                        logging.INFO
                    )
                    mod = __import__(
                        "routes.{0}".format(module),
                        fromlist=["Routes"]
                    )
                    self.routes[module] = mod.Routes(self.app, self)
                except Exception as e:
                    log(
                        "Error loading routes module '{0}': {1}"
                        .format(module, e)
                    )

        log("{0} routes set up.".format(len(self.app.routes)))
예제 #21
0
    def __init__(self):
        self.app = default_app()

        self.db = yaml.load(open("config/database.yml", "r"))
        self.mongo_conf = self.db["mongo"]
        self.db = self.db["beaker"]
        self.main_conf = yaml.load(open("config/config.yml", "r"))

        self.db_uri = "%s://%s:%s@%s/%s" % (
            self.db["adapter"], self.db["username"], self.db["password"],
            self.db["host"], self.db["database"]
        )

        if "socket" in self.db and self.db["socket"]:
            log("Using unix socket for DB connection: %s" % self.db["socket"],
                logging.INFO)
            self.db_uri += "?unix_socket=%s" % self.db["socket"]

        session_opts = {
            "session.cookie_expires": True,
            "session.type": "ext:database",
            "session.url": self.db_uri,
            "session.lock_dir": "sessions/lock/",
            # "auto": True,
            "secret": self.main_conf["secret"]
        }

        def setup_request():
            request.session = request.environ['beaker.session']

        def log_all():
            log_request(request, "%s %s " % (request.method, request.fullpath),
                        logging.INFO)

        hook('before_request')(setup_request)
        hook('after_request')(log_all)

        self.wrapped = SessionMiddleware(self.app, session_opts)

        self.routes = {}
        self.api_routes = []

        files = os.listdir("routes")
        files.remove("__init__.py")

        for _file in files:
            if _file.endswith(".py"):
                module = _file.rsplit(".", 1)[0]
                if module in self.main_conf.get("disabled-routes", []):
                    log("Routes module '%s' is disabled - not loading."
                        % module, logging.INFO)
                    continue
                try:
                    log("Loading routes module '%s'..." % module, logging.INFO)
                    mod = __import__("routes.%s" % module, fromlist=["Routes"])
                    self.routes[module] = mod.Routes(self.wrapped, self)
                except Exception as e:
                    log("Error loading routes module '%s': %s" % (module, e),
                        logging.INFO)

        log("%s routes set up." % len(self.app.routes), logging.INFO)