Exemplo n.º 1
0
    def _init_theme_renderer(self):
        """
Set up theme framework if renderer has not already been initialized.

:since: v1.0.0
        """

        if (self._theme_renderer is None):
            if (self._log_handler is not None): self._log_handler.debug("#echo(__FILEPATH__)# -{0!r}._init_theme_renderer()- (#echo(__LINE__)#)", self, context = "pas_http_core")

            Settings.read_file("{0}/settings/pas_http_theme.json".format(Settings.get("path_data")))

            if (self._theme is None):
                theme = AbstractHttpRequest.get_instance().get_parameter("theme")
                if (theme is not None): self._theme = theme
            #

            theme = (Hook.call("dNG.pas.http.Theme.checkCandidates", theme = self._theme)
                     if (Settings.get("pas_http_theme_plugins_supported", True)) else
                     None
                    )

            if (theme is not None): theme = re.sub("\\W", "", theme)

            self._theme_renderer = NamedLoader.get_instance("dNG.data.xhtml.theme.Renderer")
            self._theme_renderer.theme = (self._theme if (theme is None) else theme)

            self._theme_active = self._theme_renderer.theme

            Settings.set("x_pas_http_theme", self.theme)

            self.theme_active_base_path = path.join(Settings.get("path_data"),
                                                    "assets",
                                                    "themes",
                                                    self.theme_active
                                                   )

            self._theme_renderer.log_handler = self._log_handler
            self._theme_renderer.theme = self.theme_active
            self._theme_renderer.theme_subtype = self.theme_subtype

            if (self._description is not None): self._theme_renderer.description = self._description
            if (self._canonical_url is not None): self._theme_renderer.canonical_url = self.canonical_url

            for css_file in self.css_files_cache: self.add_css_file(css_file)
            self.css_files_cache = [ ]

            for js_file in self.js_files_cache: self.add_js_file(js_file)
            self.js_files_cache = [ ]

            for theme_css_file in self.theme_css_files_cache: self.add_theme_css_file(theme_css_file)
            self.theme_css_files_cache = [ ]

            for webc_file in self.webc_files_cache: self.add_webc_file(webc_file)
            self.webc_files_cache = [ ]
Exemplo n.º 2
0
    def oset(self, oset):
        """
Sets the OSet to use.

:param oset: OSet name

:since: v1.0.0
        """

        self._oset = oset
        Settings.set("x_pas_http_oset", self._oset)
Exemplo n.º 3
0
    def _set_theme(self, theme):
        """
Sets the theme to use. This function does not change CSS and JavaScript
files already set. Use with care.

:param theme: Output theme

:since: v1.0.0
        """

        if (self._log_handler is not None): self._log_handler.debug("#echo(__FILEPATH__)# -{0!r}._set_theme({1})- (#echo(__LINE__)#)", self, theme, context = "pas_http_core")
        self.theme = re.sub("\\W", "", theme)

        if (self._theme_renderer is not None and self.theme_renderer.is_supported(theme)):
            self.theme_active_base_path = path.join(Settings.get("path_data"),
                                                    "assets",
                                                    "themes",
                                                    theme
                                                   )

            self.theme_renderer.theme = theme
            Settings.set("x_pas_http_theme", theme)
Exemplo n.º 4
0
    def _configure(self):
        """
Configures the server

:since: v1.0.0
        """

        site_version = Settings.get("pas_http_site_version", "")

        if (site_version == ""):
            site_version = "#echo(pasHttpCoreIVersion)#"
            Settings.set("pas_http_site_version", site_version)
        #

        if (Link.is_preferred_defined()): Settings.set("x_pas_http_base_url", Link.get_preferred().build_url(Link.TYPE_ABSOLUTE_URL | Link.TYPE_BASE_PATH))
        else: Settings.set("x_pas_http_base_url", None)

        Settings.set("x_pas_http_session_uuid", "")
        Settings.set("x_pas_http_path_assets_versioned", "/data/assets/{0}".format(site_version))

        VirtualRoute.set("/data/assets/{0}/".format(site_version), { "s": "cache", "path_parameter_key": "dfile" })
        VirtualRoute.set("/data/assets/", { "s": "cache", "path_parameter_key": "dfile" })

        Hook.call("dNG.pas.http.Server.onConfigured", server = self)
Exemplo n.º 5
0
    def _ensure_settings():
        """
Check and read settings if needed.

:since: v1.0.0
        """

        if (not Connection._settings_initialized):
            with Connection._serialized_lock:
                # Thread safety
                if (not Connection._settings_initialized):
                    Settings.read_file("{0}/settings/pas_database.json".format(Settings.get("path_data")), True)

                    if (not Settings.is_defined("pas_database_url")): raise ValueException("Minimum database configuration missing")
                    url = Settings.get("pas_database_url").replace("__path_base__", path.abspath(Settings.get("path_base")))

                    if (not Settings.is_defined("pas_database_table_prefix")): Settings.set("pas_database_table_prefix", "pas")
                    Connection._serialized = (not Settings.get("pas_database_threaded", True))

                    if (Connection._serialized):
                        LogLine.debug("pas.database access is serialized", context = "pas_database")
                        Connection._serialized_lock.timeout = Settings.get("pas_database_lock_timeout", 30)
                    #

                    url_elements = urlsplit(url)

                    Settings.set("x_pas_database_backend_name", url_elements.scheme.split("+")[0])

                    if (url_elements.username is None
                        and url_elements.password is None
                        and Settings.is_defined("pas_database_user")
                        and Settings.is_defined("pas_database_password")
                       ):
                        url = "{0}://{1}:{2}@{3}{4}".format(url_elements.scheme,
                                                            Settings.get("pas_database_user"),
                                                            Settings.get("pas_database_password"),
                                                            url_elements.hostname,
                                                            url_elements.path
                                                           )

                        if (url_elements.query != ""): url += "?{0}".format(url_elements.query)
                        if (url_elements.fragment != ""): url += "#{0}".format(url_elements.fragment)
                    #

                    Settings.set("pas_database_sqlalchemy_url", url)

                    Connection._settings_initialized = True
Exemplo n.º 6
0
    def start(self, params = None, last_return = None):
        """
Starts all UPnP listeners and announces itself.

:param params: Parameter specified
:param last_return: The return value from the last hook called.

:since: v0.2.00
        """

        # pylint: disable=broad-except

        if (self.log_handler is not None): self.log_handler.debug("#echo(__FILEPATH__)# -{0!r}.start()- (#echo(__LINE__)#)", self, context = "pas_upnp")

        # Set bootid and configid to a value between 0 and 16777215
        startupid = int(time()) % 16777216

        self.bootid = startupid
        self.configid = startupid

        preferred_host = Settings.get("pas_upnp_server_preferred_host")
        preferred_port = Settings.get("pas_upnp_server_preferred_port")

        upnp_http_host = (Hook.call("dNG.pas.http.Server.getHost")
                          if (preferred_host is None) else
                          preferred_host
                         )

        if (Settings.get("pas_upnp_server_bind_host_to_ipv4", False)):
            try: upnp_http_host = socket.gethostbyname(upnp_http_host)
            except socket.error as handled_exception:
                if (self.log_handler is not None): self.log_handler.debug(handled_exception)
            #
        #

        self.http_host = upnp_http_host

        self.http_port = (Hook.call("dNG.pas.http.Server.getPort")
                          if (preferred_port is None) else
                          preferred_port
                         )

        if (self.http_host is None or self.http_port is None): raise ValueException("HTTP server must provide the hostname and port for the UPnP ControlPoint")

        if (not Settings.is_defined("pas_http_site_preferred_url_base_upnp")):
            Settings.set("pas_http_site_preferred_url_base_upnp",
                         "http://{0}:{1:d}".format(self.http_host, self.http_port)
                        )
        #

        Hook.load("upnp")
        is_ipv6_supported = getattr(socket, "has_ipv6", False)

        with self.lock:
            self.bootid += 1

            ip_addresses = Settings.get("pas_upnp_bind_network_addresses", [ ])
            if (type(ip_addresses) is not list): ip_addresses = [ ]

            listener_addresses = 0

            if (len(ip_addresses) < 1 and Settings.get("pas_upnp_bind_network_addresses_detect", True)):
                ip_address_list = socket.getaddrinfo(None, self.listener_port, socket.AF_UNSPEC, 0, socket.IPPROTO_UDP)
                ip_address_list += socket.getaddrinfo(self.http_host, self.http_port, socket.AF_UNSPEC, 0, socket.IPPROTO_TCP)

                for ip_address_data in ip_address_list:
                    if ((ip_address_data[0] == socket.AF_INET
                         or (is_ipv6_supported and ip_address_data[0] == socket.AF_INET6)
                        )
                        and ip_address_data[4][0] not in ip_addresses
                       ): ip_addresses.append(ip_address_data[4][0])
                #

                if (self.log_handler is not None and len(ip_addresses) < 1): self.log_handler.warning("pas.upnp.ControlPoint was unable to find available networks", context = "pas_upnp")
            #

            for ip_address in ip_addresses:
                if (ip_address[:4] != "127." and ip_address != "::1"):
                    # Accept user defined or discovered list of IP addresses to listen on to fail on startup
                    try:
                        self._activate_multicast_listener(ip_address)
                        listener_addresses += 1
                    except Exception: pass
                #
            #

            if (listener_addresses < 1):
                if (self.log_handler is not None): self.log_handler.debug("{0!r} will bind to all interfaces", self, context = "pas_upnp")

                self._activate_multicast_listener("0.0.0.0")
                if (is_ipv6_supported): self._activate_multicast_listener("::0")
            #
        #

        AbstractTimed.start(self)
        Hook.call("dNG.pas.upnp.ControlPoint.onStartup")
        if (self.log_handler is not None): self.log_handler.info("pas.upnp.ControlPoint starts with bootId '{0:d}' and configId '{1:d}'", self.bootid, self.configid, context = "pas_upnp")

        return last_return