Пример #1
0
    def top_heading(self, title):
        if type(self.req.user) == str:
            login_text = "<b>%s</b> (%s" % (config.user_id, "+".join(config.user_role_ids))
            if config.debug:
                if config.get_language():
                    login_text += "/%s" % config.get_language()
            login_text += ')'
        else:
            login_text = _("not logged in")
        self.write('<table class=header><tr><td width="*" class=heading>')
        self.write('<a href="#" onfocus="if (this.blur) this.blur();" '
                   'onclick="this.innerHTML=\'%s\'; document.location.reload();">%s</a></td>' %
                   (_("Reloading..."), title))
        self.write('<td style="min-width:240px" class=right><span id=headinfo></span>%s &nbsp; <b id=headertime></b>' %
                   login_text)
        self.write("<script language=\"javascript\" type=\"text/javascript\">updateHeaderTime()</script>")
        try:
            self.help_visible = config.load_user_file("help", False)  # cache for later usage
        except:
            self.help_visible = False

        cssclass = self.help_visible and "active" or "passive"
        self.write('<a id=helpbutton class=%s href="#" onclick="help_toggle();" style="display: none"></a>' %
            cssclass)
        self.write("%s</td></tr></table>" %
                   _("<a href=\"http://mathias-kettner.de\"><img src=\"images/mk_logo_small.gif\"/></a>"))
        self.write("<hr class=header>\n")
        if config.debug:
            self.write("<div class=urldebug>%s</div>" % self.makeuri([]))
Пример #2
0
def create_crash_dump_info_file(tar):
    exc_type, exc_value, exc_traceback = sys.exc_info()

    crash_info = {
        "crash_type"    : "gui",
        "time"          : time.time(),
        "os"            : get_os_info(),
        "version"       : defaults.check_mk_version,
        "exc_type"      : exc_type.__name__,
        "exc_value"     : "%s" % exc_value,
        "exc_traceback" : traceback.extract_tb(exc_traceback),
        "details"    : {
            "page"           : html.myfile+".py",
            "vars"           : html.vars,
            "username"       : html.user,
            "user_agent"     : html.get_user_agent(),
            "is_mobile"      : html.is_mobile(),
            "is_ssl_request" : html.is_ssl_request(),
            "language"       : config.get_language(),
        },
    }

    content = cStringIO.StringIO()
    content.write(json.dumps(crash_info))
    content.seek(0)

    tarinfo = tarfile.TarInfo(name="crash.info")
    content.seek(0, os.SEEK_END)
    tarinfo.size = content.tell()
    content.seek(0)
    tar.addfile(tarinfo=tarinfo, fileobj=content)
Пример #3
0
def create_crash_dump_info_file(tar):
    exc_type, exc_value, exc_traceback = sys.exc_info()

    crash_info = {
        "crash_type": "gui",
        "time": time.time(),
        "os": get_os_info(),
        "version": defaults.check_mk_version,
        "exc_type": exc_type.__name__,
        "exc_value": "%s" % exc_value,
        "exc_traceback": traceback.extract_tb(exc_traceback),
        "details": {
            "page": html.myfile + ".py",
            "vars": html.vars,
            "username": html.user,
            "user_agent": html.get_user_agent(),
            "is_mobile": html.is_mobile(),
            "is_ssl_request": html.is_ssl_request(),
            "language": config.get_language(),
        },
    }

    content = cStringIO.StringIO()
    content.write(json.dumps(crash_info))
    content.seek(0)

    tarinfo = tarfile.TarInfo(name="crash.info")
    content.seek(0, os.SEEK_END)
    tarinfo.size = content.tell()
    content.seek(0)
    tar.addfile(tarinfo=tarinfo, fileobj=content)
Пример #4
0
def create_crash_dump_info_file(tar):
    exc_type, exc_value, exc_traceback = sys.exc_info()

    crash_info = {
        "crash_type": "gui",
        "time": time.time(),
        "os": get_os_info(),
        "version": defaults.check_mk_version,
        "python_version": sys.version,
        "exc_type": exc_type.__name__,
        "exc_value": "%s" % exc_value,
        "exc_traceback": traceback.extract_tb(exc_traceback),
        "local_vars": get_local_vars_of_last_exception(),
        "details": {
            "page": html.myfile + ".py",
            "vars": html.vars,
            "username": html.user,
            "user_agent": html.get_user_agent(),
            "referer": html.get_referer(),
            "is_mobile": html.is_mobile(),
            "is_ssl_request": html.is_ssl_request(),
            "language": config.get_language(),
            "request_method": html.request_method(),
        },
    }

    content = cStringIO.StringIO()
    content.write(json.dumps(crash_info, cls=RobustJSONEncoder))
    content.seek(0)

    tarinfo = tarfile.TarInfo(name="crash.info")
    content.seek(0, 2)
    tarinfo.size = content.tell()
    content.seek(0)
    tar.addfile(tarinfo=tarinfo, fileobj=content)
Пример #5
0
    def top_heading(self, title):
        if type(self.user) == str:
            login_text = "<b>%s</b> (%s" % (config.user_id, "+".join(config.user_role_ids))
            if self.enable_debug:
                if config.get_language():
                    login_text += "/%s" % config.get_language()
            login_text += ')'
        else:
            login_text = _("not logged in")
        self.top_heading_left(title)

        self.write('<td style="min-width:240px" class=right><span id=headinfo></span>%s &nbsp; ' % login_text)
        if config.pagetitle_date_format:
            self.write(' &nbsp; <b id=headerdate format="%s"></b>' % config.pagetitle_date_format)
        self.write(' <b id=headertime></b>')
        self.write("<script language=\"javascript\" type=\"text/javascript\">updateHeaderTime()</script>")
        self.top_heading_right()
Пример #6
0
    def top_heading(self, title):
        if type(self.user) == str:
            login_text = "<b>%s</b> (%s" % (config.user_id, "+".join(config.user_role_ids))
            if self.enable_debug:
                if config.get_language():
                    login_text += "/%s" % config.get_language()
            login_text += ')'
        else:
            login_text = _("not logged in")
        self.top_heading_left(title)

        self.write('<td style="min-width:240px" class=right><span id=headinfo></span>%s &nbsp; ' % login_text)
        if config.pagetitle_date_format:
            self.write(' &nbsp; <b id=headerdate format="%s"></b>' % config.pagetitle_date_format)
        self.write(' <b id=headertime></b>')
        self.write("<script language=\"javascript\" type=\"text/javascript\">updateHeaderTime()</script>")
        self.top_heading_right()
Пример #7
0
def start(ctx,problem,language,no_open=False):
    if language is None:
        language=Config.get_language()
    if language is None:
        ctx.exit("Please set default lang or provide as argument")
    if Config.get_extension(language) is None:
        ctx.exit("Please set extension for lang "+language)
    if Config.get_cmp_cmd(language) is None:
        ctx.exit("Please set cmp_cmd for lang "+language)
    if Config.get_run_cmd(language) is None:
        ctx.exit("Please set run_cmd for lang "+language)
    spoj=Spoj(problem,language,problem+"."+Config.get_extension(language))
    spoj.start(no_open)
Пример #8
0
def page_login(no_html_output = False):
    # Initialize the i18n for the login dialog. This might be overridden
    # later after user login
    i18n.localize(html.var("lang", config.get_language()))

    result = do_login()
    if type(result) == tuple:
        return result # Successful login
    elif no_html_output:
        raise MKAuthException(_("Invalid login credentials."))

    if html.mobile:
        import mobile
        return mobile.page_login()

    else:
        return normal_login_page()
Пример #9
0
def page_login(no_html_output = False):
    # Initialize the i18n for the login dialog. This might be overridden
    # later after user login
    i18n.localize(html.var("lang", config.get_language()))

    result = do_login()
    if type(result) == tuple:
        return result # Successful login
    elif no_html_output:
        raise MKAuthException(_("Invalid login credentials."))

    if html.mobile:
        import mobile
        return mobile.page_login()

    else:
        return normal_login_page()
Пример #10
0
def submit(ctx, filename, problem, language):
    if problem is None:
        name = filename.name
        name = name.split('/')[-1]
        try:
            name = name.split('.')[-2]
        except Exception:
            name = name.split('.')[-1]
        problem = name.upper()
    problem = problem.upper()
    if language is None:
        language = Config.get_language()
    if language is None:
        language = raw_input('Language (Integer): ')
        if language not in map(str, [lan[0] for lan in lang.LANG]):
            ctx.exit('Language not supported. Please check `spoj language`.')
    spoj = Spoj(problem, language, filename)
    submit_status, message = spoj.submit()
    if submit_status:
        print message
        spoj.fetch_status()
    else:
        print message
Пример #11
0
 def __init__(self):
     self.lang = get_language()
     xlog.debug("lang: %s", self.lang)
     self.base_po_dict = {}
Пример #12
0
        unreadable += 1

import numpy

temperatures.sort()
print temperatures
print 'Interpreted temperatures', len(temperatures)
print 'Unreadable', unreadable
print 'Average', average(temperatures)
print '  Single', len(singles), average(singles), numpy.std(singles)
print '  Range', len(ranges), average(ranges), numpy.std(ranges)
print '  Milk', milks
print '  Body', bodies
print '  Other', other

LANG = config.get_language()
label = {'en': 'Pitch temperatures', 'no': 'Tilsetningstemperaturer'}

from matplotlib import pyplot

pyplot.style.use(config.get_plot_style())

(n, bins, patches) = pyplot.hist(temperatures,
                                 BINS,
                                 alpha=0.5,
                                 label=label[LANG])
if LANG == 'en':
    pyplot.title('Pitch temperatures')
    pyplot.xlabel('Degrees C')
    pyplot.ylabel('Number of accounts')
elif LANG == 'no':
Пример #13
0
 def change_language(self):
     language = RUSSIAN_LANGUAGE if get_language(
     ) == UKRAINIAN_LANGUAGE else UKRAINIAN_LANGUAGE
     set_language(language)
     self.destroy_and_rebuild()
     self.update_table_data()
Пример #14
0
def handler(req, fields = None, is_profiling = False):
    # Create an object that contains all data about the request and
    # helper functions for creating valid HTML. Parse URI and
    # store results in the request object for later usage.
    __builtin__.html = html_mod_python(req, fields)

    response_code = apache.OK
    try:
        config.load_config() # load multisite.mk etc.
        init_profiling(is_profiling)
        html.init_modes()

        # Make sure all plugins are avaiable as early as possible. At least
        # we need the plugins (i.e. the permissions declared in these) at the
        # time before the first login for generating auth.php.
        modules.load_all_plugins()

        # Get page handler.
        handler = modules.get_handler(html.myfile, page_not_found)

        # Some pages do skip authentication. This is done by adding
        # noauth: to the page hander, e.g. "noauth:run_cron" : ...
        if handler == page_not_found:
            handler = modules.get_handler("noauth:" + html.myfile, page_not_found)
            if handler != page_not_found:
                try:
                    handler()
                except Exception, e:
                    html.write("%s" % e)
                    if config.debug:
                        html.write(html.attrencode(format_exception()))
                raise FinalizeRequest()

        # Is the user set by the webserver? otherwise use the cookie based auth
        if not html.is_logged_in():
            config.auth_type = 'cookie'
            # When not authed tell the browser to ask for the password
            html.login(login.check_auth())
            if not html.is_logged_in():
                if fail_silently():
                    # While api call don't show the login dialog
                    raise MKUnauthenticatedException(_('You are not authenticated.'))

                # Redirect to the login-dialog with the current url as original target
                # Never render the login form directly when accessing urls like "index.py"
                # or "dashboard.py". This results in strange problems.
                if html.myfile != 'login':
                    html.http_redirect(defaults.url_prefix + 'check_mk/login.py?_origtarget=%s' %
                                                html.urlencode(html.makeuri([])))

                # Initialize the i18n for the login dialog. This might be overridden
                # later after user login
                i18n.localize(html.var("lang", config.get_language()))

                # This either displays the login page or validates the information submitted
                # to the login form. After successful login a http redirect to the originally
                # requested page is performed.
                login.page_login(plain_error())
                raise FinalizeRequest()
        else:
            # In case of basic auth the user is already known, but we still need to decide
            # whether or not the user is an automation user (which is allowed to use transid=-1)
            if html.var("_secret"):
                login.check_auth_automation()

        # Set all permissions, read site config, and similar stuff
        config.login(html.user)
        html.load_help_visible()

        # Initialize the multiste i18n. This will be replaced by
        # language settings stored in the user profile after the user
        # has been initialized
        previous_language = current_language
        i18n.localize(html.var("lang", config.get_language()))

        # All plugins might have to be reloaded due to a language change. Only trigger
        # a second plugin loading when the user is really using a custom localized GUI.
        # Otherwise the load_all_plugins() at the beginning of the request is sufficient.
        if current_language != previous_language:
            modules.load_all_plugins()

        # User allowed to login at all?
        if not config.may("general.use"):
            reason = _("You are not authorized to use Check_MK Multisite. Sorry. "
                       "You are logged in as <b>%s</b>.") % config.user_id
            if len(config.user_role_ids):
                reason += _("Your roles are <b>%s</b>. " % ", ".join(config.user_role_ids))
            else:
                reason += _("<b>You do not have any roles.</b> ")
            reason += _("If you think this is an error, "
                        "please ask your administrator to check the permissions configuration.")

            if config.auth_type == 'cookie':
                reason += _('<p>You have been logged out. Please reload the page to re-authenticate.</p>')
                login.del_auth_cookie()

            raise MKAuthException(reason)

        handler()
Пример #15
0
def handler(req, fields = None, profiling = True):
    req.content_type = "text/html; charset=UTF-8"
    req.header_sent = False

    # Create an object that contains all data about the request and
    # helper functions for creating valid HTML. Parse URI and
    # store results in the request object for later usage.
    html = html_mod_python(req, fields)
    html.enable_debug = config.debug
    html.id = {} # create unique ID for this request
    __builtin__.html = html
    response_code = apache.OK

    try:

        # Ajax-Functions want no HTML output in case of an error but
        # just a plain server result code of 500
        fail_silently = html.has_var("_ajaxid")

        # Webservice functions may decide to get a normal result code
        # but a text with an error message in case of an error
        plain_error = html.has_var("_plain_error")

        config.load_config() # load multisite.mk
        if html.var("debug"): # Debug flag may be set via URL
            config.debug = True

        if html.var("screenshotmode") or config.screenshotmode: # Omit fancy background, make it white
            html.screenshotmode = True

        html.enable_debug = config.debug
        html.set_buffering(config.buffered_http_stream)

        # profiling can be enabled in multisite.mk
        if profiling and config.profile:
            import cProfile # , pstats, sys, StringIO, tempfile
            # the profiler looses the memory about all modules. We need to hand over
            # the request object in the apache module.
            # Ubuntu: install python-profiler when using this feature
            profilefile = defaults.var_dir + "/web/multisite.profile"
            retcode = cProfile.runctx(
                "import index; "
                "index.handler(profile_req, profile_fields, False)",
                {'profile_req': req, 'profile_fields': html.fields}, {}, profilefile)
            file(profilefile + ".py", "w").write(
                "#!/usr/bin/python\n"
                "import pstats\n"
                "stats = pstats.Stats(%r)\n"
                "stats.sort_stats('time').print_stats()\n" % profilefile)
            os.chmod(profilefile + ".py", 0755)
            release_all_locks()
            return apache.OK

        # Make sure all plugins are avaiable as early as possible. At least
        # we need the plugins (i.e. the permissions declared in these) at the
        # time before the first login for generating auth.php.
        load_all_plugins()

        # Detect mobile devices
        if html.has_var("mobile"):
            html.mobile = not not html.var("mobile")
        else:
            user_agent = html.req.headers_in.get('User-Agent', '')
            html.mobile = mobile.is_mobile(user_agent)

        # Redirect to mobile GUI if we are a mobile device and
        # the URL is /
        if html.myfile == "index" and html.mobile:
            html.myfile = "mobile"

        # Get page handler.
        handler = pagehandlers.get(html.myfile, page_not_found)

        # First initialization of the default permissions. Needs to be done before the auth_file
        # (auth.php) ist written (it's done during showing the login page for the first time).
        # Must be loaded before the "automation" call to have the general.* permissions available
        # during automation action processing (e.g. hooks triggered by restart)
        default_permissions.load()

        # Special handling for automation.py. Sorry, this must be hardcoded
        # here. Automation calls bybass the normal authentication stuff
        if html.myfile in [ "automation", "run_cron" ]:
            try:
                handler()
            except Exception, e:
                html.write(str(e))
                if config.debug:
                    html.write(html.attrencode(format_exception()))
            release_all_locks()
            return apache.OK

        # Prepare output format
        output_format = html.var("output_format", "html")
        html.set_output_format(output_format)

        # Is the user set by the webserver? otherwise use the cookie based auth
        if not html.user or type(html.user) != str:
            config.auth_type = 'cookie'
            # When not authed tell the browser to ask for the password
            html.user = login.check_auth()
            if html.user == '':
                if fail_silently:
                    # While api call don't show the login dialog
                    raise MKUnauthenticatedException(_('You are not authenticated.'))

                # Redirect to the login-dialog with the current url as original target
                # Never render the login form directly when accessing urls like "index.py"
                # or "dashboard.py". This results in strange problems.
                if html.myfile != 'login':
                    html.http_redirect(defaults.url_prefix + 'check_mk/login.py?_origtarget=%s' %
                                                html.urlencode(html.makeuri([])))

                # Initialize the i18n for the login dialog. This might be overridden
                # later after user login
                load_language(html.var("lang", config.get_language()))

                # This either displays the login page or validates the information submitted
                # to the login form. After successful login a http redirect to the originally
                # requested page is performed.
                login.page_login(plain_error)
                release_all_locks()
                return apache.OK

        # Call userdb page hooks which are executed on a regular base to e.g. syncronize
        # information withough explicit user triggered actions
        userdb.hook_page()

        # Set all permissions, read site config, and similar stuff
        config.login(html.user)
        html.load_help_visible()

        # Initialize the multiste i18n. This will be replaced by
        # language settings stored in the user profile after the user
        # has been initialized
        load_language(html.var("lang", config.get_language()))

        # All plugins might have to be reloaded due to a language change
        load_all_plugins()

        # Reload default permissions (maybe reload due to language change)
        default_permissions.load()

        # User allowed to login at all?
        if not config.may("general.use"):
            reason = _("You are not authorized to use Check_MK Multisite. Sorry. "
                       "You are logged in as <b>%s</b>.") % config.user_id
            if len(config.user_role_ids):
                reason += _("Your roles are <b>%s</b>. " % ", ".join(config.user_role_ids))
            else:
                reason += _("<b>You do not have any roles.</b> ")
            reason += _("If you think this is an error, "
                        "please ask your administrator to check the permissions configuration.")

            if config.auth_type == 'cookie':
                reason += _('<p>You have been logged out. Please reload the page to re-authenticate.</p>')
                login.del_auth_cookie()

            raise MKAuthException(reason)

        handler()
Пример #16
0
def handler(req, fields = None, profiling = True):
    req.content_type = "text/html; charset=UTF-8"
    req.header_sent = False

    # Create an object that contains all data about the request and
    # helper functions for creating valid HTML. Parse URI and
    # store results in the request object for later usage.
    html = html_mod_python(req, fields)
    html.enable_debug = config.debug
    html.id = {} # create unique ID for this request
    __builtin__.html = html
    response_code = apache.OK

    try:

        # Ajax-Functions want no HTML output in case of an error but
        # just a plain server result code of 500
        fail_silently = html.has_var("_ajaxid")

        # Webservice functions may decide to get a normal result code
        # but a text with an error message in case of an error
        plain_error = html.has_var("_plain_error")

        config.load_config() # load multisite.mk
        if html.var("debug"): # Debug flag may be set via URL
            config.debug = True

        if html.var("screenshotmode") or config.screenshotmode: # Omit fancy background, make it white
            html.screenshotmode = True

        html.enable_debug = config.debug
        html.set_buffering(config.buffered_http_stream)

        # profiling can be enabled in multisite.mk
        if profiling and config.profile:
            import cProfile # , pstats, sys, StringIO, tempfile
            # the profiler loses the memory about all modules. We need to hand over
            # the request object in the apache module.
            # Ubuntu: install python-profiler when using this feature
            profilefile = defaults.var_dir + "/web/multisite.profile"
            retcode = cProfile.runctx(
                "import index; "
                "index.handler(profile_req, profile_fields, False)",
                {'profile_req': req, 'profile_fields': html.fields}, {}, profilefile)
            file(profilefile + ".py", "w").write(
                "#!/usr/bin/python\n"
                "import pstats\n"
                "stats = pstats.Stats(%r)\n"
                "stats.sort_stats('time').print_stats()\n" % profilefile)
            os.chmod(profilefile + ".py", 0755)
            release_all_locks()
            return apache.OK

        # Make sure all plugins are avaiable as early as possible. At least
        # we need the plugins (i.e. the permissions declared in these) at the
        # time before the first login for generating auth.php.
        load_all_plugins()

        # Detect mobile devices
        if html.has_var("mobile"):
            html.mobile = not not html.var("mobile")
        else:
            user_agent = html.req.headers_in.get('User-Agent', '')
            html.mobile = mobile.is_mobile(user_agent)

        # Redirect to mobile GUI if we are a mobile device and
        # the URL is /
        if html.myfile == "index" and html.mobile:
            html.myfile = "mobile"

        # Get page handler.
        handler = pagehandlers.get(html.myfile, page_not_found)

        # First initialization of the default permissions. Needs to be done before the auth_file
        # (auth.php) ist written (it's done during showing the login page for the first time).
        # Must be loaded before the "automation" call to have the general.* permissions available
        # during automation action processing (e.g. hooks triggered by restart)
        default_permissions.load()

        # Some pages do skip authentication. This is done by adding
        # noauth: to the page hander, e.g. "noauth:run_cron" : ...
        if handler == page_not_found:
            handler = pagehandlers.get("noauth:" + html.myfile, page_not_found)
            if handler != page_not_found:
                try:
                    # Call userdb page hooks which are executed on a regular base to e.g. syncronize
                    # information withough explicit user triggered actions
                    userdb.hook_page()

                    handler()
                except Exception, e:
                    html.write(str(e))
                    if config.debug:
                        html.write(html.attrencode(format_exception()))
                release_all_locks()
                return apache.OK

        # Prepare output format
        output_format = html.var("output_format", "html")
        html.set_output_format(output_format)

        # Is the user set by the webserver? otherwise use the cookie based auth
        if not html.user or type(html.user) != str:
            config.auth_type = 'cookie'
            # When not authed tell the browser to ask for the password
            html.user = login.check_auth()
            if html.user == '':
                if fail_silently:
                    # While api call don't show the login dialog
                    raise MKUnauthenticatedException(_('You are not authenticated.'))

                # Redirect to the login-dialog with the current url as original target
                # Never render the login form directly when accessing urls like "index.py"
                # or "dashboard.py". This results in strange problems.
                if html.myfile != 'login':
                    html.http_redirect(defaults.url_prefix + 'check_mk/login.py?_origtarget=%s' %
                                                html.urlencode(html.makeuri([])))

                # Initialize the i18n for the login dialog. This might be overridden
                # later after user login
                load_language(html.var("lang", config.get_language()))

                # This either displays the login page or validates the information submitted
                # to the login form. After successful login a http redirect to the originally
                # requested page is performed.
                login.page_login(plain_error)
                release_all_locks()
                return apache.OK
        else:
            # In case of basic auth the user is already known, but we still need to decide
            # whether or not the user is an automation user (which is allowed to use transid=-1)
            if html.var("_secret"):
                login.check_auth_automation()

        # Call userdb page hooks which are executed on a regular base to e.g. syncronize
        # information withough explicit user triggered actions
        userdb.hook_page()

        # Set all permissions, read site config, and similar stuff
        config.login(html.user)
        html.load_help_visible()

        # Initialize the multiste i18n. This will be replaced by
        # language settings stored in the user profile after the user
        # has been initialized
        load_language(html.var("lang", config.get_language()))

        # All plugins might have to be reloaded due to a language change
        load_all_plugins()

        # Reload default permissions (maybe reload due to language change)
        default_permissions.load()

        # User allowed to login at all?
        if not config.may("general.use"):
            reason = _("You are not authorized to use Check_MK Multisite. Sorry. "
                       "You are logged in as <b>%s</b>.") % config.user_id
            if len(config.user_role_ids):
                reason += _("Your roles are <b>%s</b>. " % ", ".join(config.user_role_ids))
            else:
                reason += _("<b>You do not have any roles.</b> ")
            reason += _("If you think this is an error, "
                        "please ask your administrator to check the permissions configuration.")

            if config.auth_type == 'cookie':
                reason += _('<p>You have been logged out. Please reload the page to re-authenticate.</p>')
                login.del_auth_cookie()

            raise MKAuthException(reason)

        handler()
Пример #17
0
def handler(req, profiling = True):
    req.content_type = "text/html; charset=UTF-8"
    req.header_sent = False

    # All URIs end in .py. We strip away the .py and get the
    # name of the page.
    req.myfile = req.uri.split("/")[-1][:-3]

    # Create an object that contains all data about the request and
    # helper functions for creating valid HTML. Parse URI and
    # store results in the request object for later usage.
    html = htmllib.html(req)
    html.id = {} # create unique ID for this request
    __builtin__.html = html
    req.uriinfo = htmllib.uriinfo(req)

    response_code = apache.OK
    try:
        # Do not parse variables again if profiling (and second run is done)
        if profiling:
            read_get_vars(req)
            read_cookies(req)

        # Ajax-Functions want no HTML output in case of an error but
        # just a plain server result code of 500
        fail_silently = html.has_var("_ajaxid")

        # Webservice functions may decide to get a normal result code
        # but a text with an error message in case of an error
        plain_error = html.has_var("_plain_error")

        config.load_config() # load multisite.mk
        if html.var("debug"): # Debug flag may be set via URL
            config.debug = True
        html.set_buffering(config.buffered_http_stream)

        # profiling can be enabled in multisite.mk
        if profiling and config.profile:
            import cProfile # , pstats, sys, StringIO, tempfile
            # the profiler looses the memory about all modules. We need to park
            # the request object in the apache module. This seems to be persistent.
            # Ubuntu: install python-profiler when using this feature
            apache._profiling_req = req
            profilefile = defaults.var_dir + "/web/multisite.profile"
            retcode = cProfile.run("import index; from mod_python import apache; index.handler(apache._profiling_req, False)", profilefile)
            file(profilefile + ".py", "w").write("#!/usr/bin/python\nimport pstats\nstats = pstats.Stats(%r)\nstats.sort_stats('time').print_stats()\n" % profilefile)
            os.chmod(profilefile + ".py", 0755)
            release_all_locks()
            return apache.OK

        # Make sure all plugins are avaiable as early as possible. At least
        # we need the plugins (i.e. the permissions declared in these) at the
        # time before the first login for generating auth.php.
        load_all_plugins()

        # Detect mobile devices
        if html.has_var("mobile"):
            html.mobile = not not html.var("mobile")
        else:
            user_agent = html.req.headers_in.get('User-Agent', '')
            html.mobile = mobile.is_mobile(user_agent)

        # Redirect to mobile GUI if we are a mobile device and
        # the URL is /
        if req.myfile == "index" and html.mobile:
            req.myfile = "mobile"

        # Get page handler
        handler = pagehandlers.get(req.myfile, page_not_found)

        # First initialization of the default permissions. Needs to be done before the auth_file
        # (auth.php) ist written (it's done during showing the login page for the first time).
        # Must be loaded before the "automation" call to have the general.* permissions available
        # during automation action processing (e.g. hooks triggered by restart)
        default_permissions.load()

        # Special handling for automation.py. Sorry, this must be hardcoded
        # here. Automation calls bybass the normal authentication stuff
        if req.myfile == "automation":
            try:
                handler()
            except Exception, e:
                html.write(str(e))
            release_all_locks()
            return apache.OK

        # Prepare output format
        output_format = html.var("output_format", "html")
        html.set_output_format(output_format)

        # Is the user set by the webserver? otherwise use the cookie based auth
        if not req.user or type(req.user) != str:
            config.auth_type = 'cookie'
            # When not authed tell the browser to ask for the password
            req.user = login.check_auth()
            if req.user == '':
                if fail_silently:
                    # While api call don't show the login dialog
                    raise MKUnauthenticatedException(_('You are not authenticated.'))

                # Initialize the i18n for the login dialog. This might be overridden
                # later after user login
                load_language(html.var("lang", config.get_language()))

                # After auth check the regular page can be shown
                result = login.page_login(plain_error)
                if type(result) == tuple:
                    # This is the redirect to the requested page directly after successful login
                    req.user = result[0]
                    req.uri  = result[1]
                    req.myfile = req.uri.split("/")[-1][:-3]
                    handler = pagehandlers.get(req.myfile, page_not_found)
                else:
                    release_all_locks()
                    return result

        # Call userdb page hooks which are executed on a regular base to e.g. syncronize
        # information withough explicit user triggered actions
        userdb.hook_page()

        # Set all permissions, read site config, and similar stuff
        config.login(html.req.user)

        # Initialize the multiste i18n. This will be replaced by
        # language settings stored in the user profile after the user
        # has been initialized
        load_language(html.var("lang", config.get_language()))

        # All plugins might have to be reloaded due to a language change
        load_all_plugins()

        # Reload default permissions (maybe reload due to language change)
        default_permissions.load()

        # User allowed to login at all?
        if not config.may("general.use"):
            reason = _("You are not authorized to use Check_MK Multisite. Sorry. "
                       "You are logged in as <b>%s</b>.") % config.user_id
            if len(config.user_role_ids):
                reason += _("Your roles are <b>%s</b>. " % ", ".join(config.user_role_ids))
            else:
                reason += _("<b>You do not have any roles.</b> ")
            reason += _("If you think this is an error, "
                        "please ask your administrator to check the permissions configuration.")

            if config.auth_type == 'cookie':
                reason += _('<p>You have been logged out. Please reload the page to re-authenticate.</p>')
                login.del_auth_cookie()

            raise MKAuthException(reason)

        # General access allowed. Now connect to livestatus
        connect_to_livestatus(html)

        handler()
Пример #18
0
def pick_name(name_or_names):
    if type(name_or_names) == type({}):
        return name_or_names[config.get_language()]
    else:
        return name_or_names
Пример #19
0
# -*- coding:utf-8 -*-
"""
train_model and deal crops
"""
import config
config.set_language('en')
lan = config.get_language()
import time
import pickle
import random
import numpy as np
from lucene import initVM
initVM()
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.cross_validation import cross_val_score
from supervised_keywords_server import SupervisedKeyowrds
from model_evaluation import cal_precision_and_recall

SAMPLE_FILE = 'data/en/train_data_0207.pkl'  #us
#SAMPLE_FILE = 'data/id/train_data_id_4715_0213.pkl'#0124
bxz_all = 0
bxz_select = 0


def deal_crops(train, weight=1):
    global bxz_all, bxz_select
    features = train['features']
    all_candidates = train['all_candidates']
    golde_set_keywords = train['golden_set_keywords']
Пример #20
0
def get_language(ctx):
    click.echo(Config.get_language())
Пример #21
0
 def __init__(self):
     self.lang = get_language()
     xlog.debug("lang: %s", self.lang)
Пример #22
0
def handler(req, fields = None, is_profiling = False):
    # Create an object that contains all data about the request and
    # helper functions for creating valid HTML. Parse URI and
    # store results in the request object for later usage.
    __builtin__.html = html_mod_python(req, fields)

    response_code = apache.OK
    try:
        config.load_config() # load multisite.mk etc.
        html.init_modes()
        init_profiling(is_profiling)

        # Make sure all plugins are avaiable as early as possible. At least
        # we need the plugins (i.e. the permissions declared in these) at the
        # time before the first login for generating auth.php.
        modules.load_all_plugins()

        # Get page handler.
        handler = modules.get_handler(html.myfile, page_not_found)

        # Some pages do skip authentication. This is done by adding
        # noauth: to the page hander, e.g. "noauth:run_cron" : ...
        if handler == page_not_found:
            handler = modules.get_handler("noauth:" + html.myfile, page_not_found)
            if handler != page_not_found:
                try:
                    # Call userdb page hooks which are executed on a regular base to e.g. syncronize
                    # information withough explicit user triggered actions
                    userdb.hook_page()

                    handler()
                except Exception, e:
                    html.write(str(e))
                    if config.debug:
                        html.write(html.attrencode(format_exception()))
                raise FinalizeRequest()

        # Is the user set by the webserver? otherwise use the cookie based auth
        if not html.is_logged_in():
            config.auth_type = 'cookie'
            # When not authed tell the browser to ask for the password
            html.login(login.check_auth())
            if not html.is_logged_in():
                if fail_silently():
                    # While api call don't show the login dialog
                    raise MKUnauthenticatedException(_('You are not authenticated.'))

                # Redirect to the login-dialog with the current url as original target
                # Never render the login form directly when accessing urls like "index.py"
                # or "dashboard.py". This results in strange problems.
                if html.myfile != 'login':
                    html.http_redirect(defaults.url_prefix + 'check_mk/login.py?_origtarget=%s' %
                                                html.urlencode(html.makeuri([])))

                # Initialize the i18n for the login dialog. This might be overridden
                # later after user login
                i18n.localize(html.var("lang", config.get_language()))

                # This either displays the login page or validates the information submitted
                # to the login form. After successful login a http redirect to the originally
                # requested page is performed.
                login.page_login(plain_error())
                raise FinalizeRequest()
        else:
            # In case of basic auth the user is already known, but we still need to decide
            # whether or not the user is an automation user (which is allowed to use transid=-1)
            if html.var("_secret"):
                login.check_auth_automation()

        # Call userdb page hooks which are executed on a regular base to e.g. syncronize
        # information withough explicit user triggered actions
        userdb.hook_page()

        # Set all permissions, read site config, and similar stuff
        config.login(html.user)
        html.load_help_visible()

        # Initialize the multiste i18n. This will be replaced by
        # language settings stored in the user profile after the user
        # has been initialized
        i18n.localize(html.var("lang", config.get_language()))

        # All plugins might have to be reloaded due to a language change
        modules.load_all_plugins()

        # User allowed to login at all?
        if not config.may("general.use"):
            reason = _("You are not authorized to use Check_MK Multisite. Sorry. "
                       "You are logged in as <b>%s</b>.") % config.user_id
            if len(config.user_role_ids):
                reason += _("Your roles are <b>%s</b>. " % ", ".join(config.user_role_ids))
            else:
                reason += _("<b>You do not have any roles.</b> ")
            reason += _("If you think this is an error, "
                        "please ask your administrator to check the permissions configuration.")

            if config.auth_type == 'cookie':
                reason += _('<p>You have been logged out. Please reload the page to re-authenticate.</p>')
                login.del_auth_cookie()

            raise MKAuthException(reason)

        handler()
Пример #23
0
def get_lang_info(ctx,language):
    if language == None:
        language=Config.get_language()
    click.echo("Compile cmd : "+str(Config.get_cmp_cmd(language)))
    click.echo("Run cmd : "+str(Config.get_run_cmd(language)))
    click.echo("Extension : "+str(Config.get_extension(language)))
Пример #24
0
def set_lang_info(ctx,language,cmpile_cmd,run_cmd,extension):
    if language == None:
        language=Config.get_language()
    Config.set_cmp_cmd(language,cmpile_cmd)
    Config.set_run_cmd(language,run_cmd)
    Config.set_extension(language,extension)
Пример #25
0
def handler(req, profiling=True):
    req.content_type = "text/html; charset=UTF-8"
    req.header_sent = False

    # All URIs end in .py. We strip away the .py and get the
    # name of the page.
    req.myfile = req.uri.split("/")[-1][:-3]

    # Create an object that contains all data about the request and
    # helper functions for creating valid HTML. Parse URI and
    # store results in the request object for later usage.
    html = htmllib.html(req)
    html.id = {}  # create unique ID for this request
    __builtin__.html = html
    req.uriinfo = htmllib.uriinfo(req)

    response_code = apache.OK
    try:
        # Do not parse variables again if profiling (and second run is done)
        if profiling:
            read_get_vars(req)
            read_cookies(req)

        # Ajax-Functions want no HTML output in case of an error but
        # just a plain server result code of 500
        fail_silently = html.has_var("_ajaxid")

        # Webservice functions may decide to get a normal result code
        # but a text with an error message in case of an error
        plain_error = html.has_var("_plain_error")

        config.load_config()  # load multisite.mk
        if html.var("debug"):  # Debug flag may be set via URL
            config.debug = True
        html.set_buffering(config.buffered_http_stream)

        # profiling can be enabled in multisite.mk
        if profiling and config.profile:
            import cProfile  # , pstats, sys, StringIO, tempfile
            # the profiler looses the memory about all modules. We need to park
            # the request object in the apache module. This seems to be persistent.
            # Ubuntu: install python-profiler when using this feature
            apache._profiling_req = req
            profilefile = defaults.var_dir + "/web/multisite.profile"
            retcode = cProfile.run(
                "import index; from mod_python import apache; index.handler(apache._profiling_req, False)",
                profilefile)
            file(profilefile + ".py", "w").write(
                "#!/usr/bin/python\nimport pstats\nstats = pstats.Stats(%r)\nstats.sort_stats('time').print_stats()\n"
                % profilefile)
            os.chmod(profilefile + ".py", 0755)
            return apache.OK

        # Make sure all plugins are avaiable as early as possible. At least
        # we need the plugins (i.e. the permissions declared in these) at the
        # time before the first login for generating auth.php.
        load_all_plugins()

        # Detect mobile devices
        if html.has_var("mobile"):
            html.mobile = not not html.var("mobile")
        else:
            user_agent = html.req.headers_in.get('User-Agent', '')
            html.mobile = mobile.is_mobile(user_agent)

        # Redirect to mobile GUI if we are a mobile device and
        # the URL is /
        if req.myfile == "index" and html.mobile:
            req.myfile = "mobile"

        # Get page handler
        handler = pagehandlers.get(req.myfile, page_not_found)

        # Special handling for automation.py. Sorry, this must be hardcoded
        # here. Automation calls bybass the normal authentication stuff
        if req.myfile == "automation":
            try:
                handler()
            except Exception, e:
                html.write(str(e))
            return apache.OK

        # Prepare output format
        output_format = html.var("output_format", "html")
        html.set_output_format(output_format)

        # Is the user set by the webserver? otherwise use the cookie based auth
        if not req.user or type(req.user) != str:
            config.auth_type = 'cookie'
            # When not authed tell the browser to ask for the password
            req.user = login.check_auth()
            if req.user == '':
                if fail_silently:
                    # While api call don't show the login dialog
                    raise MKUnauthenticatedException(
                        _('You are not authenticated.'))

                # Initialize the i18n for the login dialog. This might be overridden
                # later after user login
                load_language(html.var("lang", config.get_language()))

                # After auth check the regular page can be shown
                result = login.page_login(plain_error)
                if type(result) == tuple:
                    # This is the redirect to the requested page directly after successful login
                    req.user = result[0]
                    req.uri = result[1]
                    req.myfile = req.uri.split("/")[-1][:-3]
                    handler = pagehandlers.get(req.myfile, page_not_found)
                else:
                    return result

        # Set all permissions, read site config, and similar stuff
        config.login(html.req.user)

        # Initialize the multiste i18n. This will be replaced by
        # language settings stored in the user profile after the user
        # has been initialized
        load_language(html.var("lang", config.get_language()))

        # All plugins might have to be reloaded due to a language change
        load_all_plugins()

        # Initialize default permissions (maybe reload due to language change)
        import default_permissions
        default_permissions.load()

        # User allowed to login at all?
        if not config.may("general.use"):
            reason = _(
                "You are not authorized to use Check_MK Multisite. Sorry. "
                "You are logged in as <b>%s</b>.") % config.user_id
            if len(config.user_role_ids):
                reason += _("Your roles are <b>%s</b>. " %
                            ", ".join(config.user_role_ids))
            else:
                reason += _("<b>You do not have any roles.</b> ")
            reason += _(
                "If you think this is an error, "
                "please ask your administrator to check the permissions configuration."
            )

            if config.auth_type == 'cookie':
                reason += _(
                    '<p>You have been logged out. Please reload the page to re-authenticate.</p>'
                )
                login.del_auth_cookie()

            raise MKAuthException(reason)

        # General access allowed. Now connect to livestatus
        connect_to_livestatus(html)

        handler()
Пример #26
0
def translate_(s):
    if get_language() == UKRAINIAN_LANGUAGE:
        return UKRAINIAN[s]
    if get_language() == RUSSIAN_LANGUAGE:
        return RUSSIAN[s]