コード例 #1
0
ファイル: locale_help.py プロジェクト: dpetzold/natsort
def locale_convert(val, func, group):
    """\
    Attempt to convert a string to a number, first converting
    the decimal place character if needed. Then, if the conversion
    was not possible (i.e. it is not a number), run it through
    strxfrm to make the work sorting as requested, possibly grouping first.
    """

    # Format the number so that the conversion function can interpret it.
    radix = localeconv()['decimal_point']
    s = val.replace(radix, '.') if radix != '.' else val

    # Perform the conversion
    t = func[0](s)

    # Return the number or transformed string.
    # If the input is identical to the output, then no conversion happened.
    # In this case, we don't want to return the function output because it
    # may have had characters modified from the above 'replace' call,
    # so we return the input.
    if group:
        if use_pyicu:
            xfrm = get_pyicu_transform(getlocale())
            return xfrm(groupletters(val)) if not func[1](t) else t
        else:
            return strxfrm(groupletters(val)) if not func[1](t) else t
    else:
        if use_pyicu:
            xfrm = get_pyicu_transform(getlocale())
            return xfrm(val) if not func[1](t) else t
        else:
            return strxfrm(val) if not func[1](t) else t
コード例 #2
0
ファイル: testing.py プロジェクト: jniznan/pandas
def set_locale(new_locale, lc_var=locale.LC_ALL):
    """Context manager for temporarily setting a locale.

    Parameters
    ----------
    new_locale : str or tuple
        A string of the form <language_country>.<encoding>. For example to set
        the current locale to US English with a UTF8 encoding, you would pass
        "en_US.UTF-8".

    Notes
    -----
    This is useful when you want to run a particular block of code under a
    particular locale, without globally setting the locale. This probably isn't
    thread-safe.
    """
    current_locale = locale.getlocale()

    try:
        locale.setlocale(lc_var, new_locale)

        try:
            normalized_locale = locale.getlocale()
        except ValueError:
            yield new_locale
        else:
            if all(lc is not None for lc in normalized_locale):
                yield '.'.join(normalized_locale)
            else:
                yield new_locale
    finally:
        locale.setlocale(lc_var, current_locale)
コード例 #3
0
ファイル: gmI18N.py プロジェクト: ncqgm/gnumed
def activate_locale():
	"""Get system locale from environment."""
	global system_locale

	__log_locale_settings('unmodified startup locale settings (should be [C])')

	# activate user-preferred locale
	loc, enc = None, None
	try:
		# check whether already set
		loc, loc_enc = locale.getlocale()
		if loc is None:
			loc = locale.setlocale(locale.LC_ALL, '')
			_log.debug("activating user-default locale with <locale.setlocale(locale.LC_ALL, '')> returns: [%s]" % loc)
		else:
			_log.info('user-default locale already activated')
		loc, loc_enc = locale.getlocale()
	except AttributeError:
		_log.exception('Windows does not support locale.LC_ALL')
	except:
		_log.exception('error activating user-default locale')

	__log_locale_settings('locale settings after activating user-default locale')

	# did we find any locale setting ? assume en_EN if not
	if loc in [None, 'C']:
		_log.error('the current system locale is still [None] or [C], assuming [en_EN]')
		system_locale = "en_EN"
	else:
		system_locale = loc

	# generate system locale levels
	__split_locale_into_levels()

	return True
コード例 #4
0
ファイル: mm1.py プロジェクト: AtomLaw/Airtime
def configure_locale():
    logger.debug("Before %s", locale.nl_langinfo(locale.CODESET))
    current_locale = locale.getlocale()

    if current_locale[1] is None:
        logger.debug("No locale currently set. Attempting to get default locale.")
        default_locale = locale.getdefaultlocale()

        if default_locale[1] is None:
            logger.debug("No default locale exists. Let's try loading from /etc/default/locale")
            if os.path.exists("/etc/default/locale"):
                config = ConfigObj('/etc/default/locale')
                lang = config.get('LANG')
                new_locale = lang
            else:
                logger.error("/etc/default/locale could not be found! Please run 'sudo update-locale' from command-line.")
                sys.exit(1)
        else:
            new_locale = default_locale

        logger.info("New locale set to: %s", locale.setlocale(locale.LC_ALL, new_locale))



    reload(sys)
    sys.setdefaultencoding("UTF-8")
    current_locale_encoding = locale.getlocale()[1].lower()
    logger.debug("sys default encoding %s", sys.getdefaultencoding())
    logger.debug("After %s", locale.nl_langinfo(locale.CODESET))

    if current_locale_encoding not in ['utf-8', 'utf8']:
        logger.error("Need a UTF-8 locale. Currently '%s'. Exiting..." % current_locale_encoding)
        sys.exit(1)
コード例 #5
0
def generate(target='uimodel', **options):
    globals()['target'] = target
    print("// Build for target " + target)
    import locale
    print("//locale %s" % repr(locale.getlocale()))
    locale_info = repr(locale.getlocale())
    uifield_list = []
    comments = []
    last_model_decl = None
    for line in sys.stdin:
        line = line.strip()
        if line:
            if isinstance(line, str):
                line = line.decode(encoding='utf-8')
            comments.append("// " + line)
            if line.startswith('-'):
                last_model_decl = parse_model_name(line)
                continue
            uifields = parse_line(line)
            if uifields:
                uifield_list.extend(uifields)
    from .helper import jinja2_env
    template = jinja2_env.get_template('bx%s_tpl.html' % target)
    has_textfield = len([field for field in uifield_list if field.ftype == 'f']) > 0
    locale_info.encode(encoding='utf-8')
    text = template.render(model=last_model_decl, uifields=uifield_list, has_textfield=has_textfield, comments=comments)
    return text.encode('utf-8')
コード例 #6
0
ファイル: dashboard.py プロジェクト: cc-archive/pootle
def server_stats_more(request):
    result = cache.get("server_stats_more")
    if result is None:
        result = {}
        unit_query = Unit.objects.filter(state__gte=TRANSLATED).exclude(
            store__translation_project__project__code__in=('pootle', 'tutorial', 'terminology')).exclude(
            store__translation_project__language__code='templates').order_by()
        result['store_count'] = unit_query.values('store').distinct().count()
        result['project_count'] = unit_query.values('store__translation_project__project').distinct().count()
        result['language_count'] = unit_query.values('store__translation_project__language').distinct().count()
        sums = sum_column(unit_query, ('source_wordcount',), count=True)
        result['string_count'] = sums['count']
        result['word_count'] = sums['source_wordcount'] or 0
        result['user_active_count'] = (PootleProfile.objects.exclude(submission=None) |\
                                       PootleProfile.objects.exclude(suggestion=None) |\
                                       PootleProfile.objects.exclude(suggester=None)).order_by().count()
        _format_numbers(result)
        cache.set("server_stats_more", result, 86400)
    stat_strings = {'store_count': _('Files'),
                    'project_count': _('Active projects'),
                    'language_count': _('Active languages'),
                    'string_count': _('Translated strings'),
                    'word_count': _('Translated words'),
                    'user_active_count': _('Active users')}
    response = []
    for k in result.keys():
        response.append((stat_strings[k], result[k]))
    print response
    print locale.getlocale()
    response = simplejson.dumps(response)
    return HttpResponse(response, mimetype="application/json")
コード例 #7
0
 def test_locale_set(self):
     oldloc = locale.getlocale(locale.LC_TIME)
     locale.setlocale(locale.LC_TIME, "C")
     self.assertEquals(locale.getlocale(locale.LC_TIME)[0], None)
     with _fix_locale("de_DE"):
         self.assertEquals(locale.getlocale(locale.LC_TIME)[0], "de_DE")
     self.assertEquals(locale.getlocale(locale.LC_TIME)[0], None)
コード例 #8
0
ファイル: format.py プロジェクト: qwang2505/ccrawler
def parse_http_date(http_date):
    '''
    Parse an HTTP date into a datetime object.

    @param http_date: The text of the HTTP Date.
    '''
    if not http_date:
        return None
    import locale
    locale_cache = None
    # Ensure we use en_US locale to make abbreviated weekday, month correct.
    if locale.getlocale(locale.LC_TIME)[0] != HTTP_DATE_LOCALE[0]:
        locale_cache = locale.getlocale(locale.LC_ALL)
        locale.setlocale(locale.LC_TIME, HTTP_DATE_LOCALE)
    date = None
    for formatter in HTTP_DATE_FORMATS:
        try:
            date = datetime.datetime.strptime(http_date, formatter)
            break
        except ValueError:
            pass
    if not date:
        raise ValueError("'%s' isn't a valid HTTP date." % http_date)
    if locale_cache:
        locale.setlocale(locale.LC_TIME, locale_cache)
    return date
コード例 #9
0
ファイル: string_utilities.py プロジェクト: gdever/ultimix
def			ProcessLog( LogFile ):
	if( ( sys.version_info[ 0 ] == 2 and sys.version_info[ 1 ] >= 2 ) or ( sys.version_info[ 0 ] >= 3 ) ):
		if( conf.console_encoding != None ):
			d = codecs.getdecoder( conf.console_encoding );
		else:
			if( locale.getlocale()[ 1 ] == None ):
				d = codecs.getdecoder( 'cp1251' );
			else:
				d = codecs.getdecoder( locale.getlocale()[ 1 ] );
	else:
		if( conf.console_encoding != None ):
			d = codecs.lookup( conf.console_encoding )[ 1 ];
		else:
			if( sys.version_info[ 0 ] > 1 ):
				d = codecs.lookup( locale.getlocale()[ 1 ] )[ 1 ];
			else:
				d = None;
		
	if( sys.version_info[ 0 ] == 2 and sys.version_info[ 1 ] in [ 3 , 4 , 5 ,6 ] ):
		LogFile = d( LogFile )[ 0 ].replace( "\r" , "" );
	elif( sys.version_info[ 0 ] >= 3 ):
		if( type( LogFile ) == type( "" ) ):
			LogFile = LogFile.replace( "\r" , "" );
		else:
			LogFile = d( LogFile )[ 0 ].replace( "\r" , "" );
	else:
		if( d != None ):
			LogFile = d( LogFile )[ 0 ].replace( "\r" , "" );
		else:
			LogFile = 'ERROR (Log file is hidden but it can be found in %test_project_pah%/logs directory)';
	
	return( LogFile );
コード例 #10
0
ファイル: _locale.py プロジェクト: rabbitvcs/rabbitvcs
def set_locale(language, encoding):
    sane_default = locale.getlocale(locale.LC_MESSAGES)
    loc = language
    if not encoding:
        if not language:
            return sane_default
        encoding = sane_default[1]
    if not loc:
        loc = sane_default[0]

    try:
        locale.setlocale(locale.LC_ALL, (loc, encoding))
    except locale.Error:
        # If the user's environment does not specify an encoding, Python will
        # pick a default which might not be available. It seems to pick
        # ISO8859-1 (latin1), but UTF8 is a better idea on GNU/Linux.
        log.warning("Could not set locale (%s, %s)" % (loc, encoding))

        # We should only try this if we have a region to set as well.
        if language and encoding != "UTF-8":
            try:
                locale.setlocale(locale.LC_ALL, (language, "UTF-8"))
                log.warning("Manually set encoding to UTF-8")
            except locale.Error:
                # Nope, no UTF-8 either.
                log.warning("Could not set user's locale to UTF-8")

    loc = locale.getlocale(locale.LC_MESSAGES)
    langs = []
    if loc[0]:
        langs.append(loc[0])
    gettext.set_language(langs)
    return loc
コード例 #11
0
ファイル: test_localemock.py プロジェクト: gordonzola/srmlf
def test_new_locale_with_multiple_categories():
    old_locale = locale.getlocale(locale.LC_MESSAGES)
    lm = LocaleMock(('en_US', 'UTF-8'), [locale.LC_TIME, locale.LC_MONETARY])
    lm.__enter__()
    assert locale.getlocale(locale.LC_TIME) == ('en_US', 'UTF-8')
    assert locale.getlocale(locale.LC_MONETARY) == ('en_US', 'UTF-8')
    assert locale.getlocale(locale.LC_MESSAGES) == old_locale
コード例 #12
0
ファイル: gtcache.py プロジェクト: nxmirrors/miro
def init():
    global _gtcache
    global codeset
    _gtcache = {}
    if not miro.plat.utils.locale_initialized():
        raise Exception, "locale not initialized"

    language = app.config.get(prefs.LANGUAGE)

    if language != "system":
        os.environ["LANGUAGE"] = language

    # try to set the locale to the platform default, but if that fails
    # log a message and set it to C.
    try:
        locale.setlocale(locale.LC_ALL, "")
    except locale.Error:
        print "gtcache.init: setlocale failed.  setting locale to 'C'"
        locale.setlocale(locale.LC_ALL, "C")

    # try to get the locale which might fail despite the above.  see bug #11783.
    try:
        codeset = locale.getlocale()[1]
    except ValueError:
        print "gtcache.init: getlocale failed.  setting locale to 'C'"
        locale.setlocale(locale.LC_ALL, "C")
        codeset = locale.getlocale()[1]

    if codeset is not None:
        codeset = codeset.lower()

    _gt.bindtextdomain("miro", app.config.get(prefs.GETTEXT_PATHNAME))
    _gt.textdomain("miro")
    _gt.bind_textdomain_codeset("miro", "UTF-8")
コード例 #13
0
 def _is_fa_locale(self):
     if FA_LOCALE in _locale.getlocale():
         return True
     if None not in _locale.getlocale():
         return False
     if FA_LOCALE in _locale.getdefaultlocale():
         return True
     return False
コード例 #14
0
ファイル: pyurl.py プロジェクト: federicoculloca/pyurl
def language_set():
    """read from UNIX or windows locale informations and set language"""
    ita = gettext.translation('pyurl', localedir='locale', languages=['it'])
    eng = gettext.translation('pyurl', localedir='locale', languages=['en'])
    if locale.getlocale()[0] == 'it_IT' or locale.getlocale()[0] == 'ita':
        ita.install()
    else:
        eng.install()
コード例 #15
0
ファイル: test_stdmodules.py プロジェクト: 89sos98/main
def test_get_set_locale():
    import locale
    locale.setlocale(locale.LC_ALL, 'en-US')
    loc = locale.getlocale(locale.LC_ALL)
    AreEqual(loc, ('en_US','ISO8859-1'))
    
    locale.setlocale(locale.LC_ALL, 'C')
    loc = locale.getlocale(locale.LC_ALL)
    AreEqual(loc, (None,None))
コード例 #16
0
ファイル: i18n.py プロジェクト: cs2c/AppStream
def init_locale():
    try:
        locale.setlocale(locale.LC_ALL, "")
        # we need this for bug #846038, with en_NG setlocale() is fine
        # but the next getlocale() will crash (fun!)
        locale.getlocale()
    except:
        LOG.exception("setlocale failed, resetting to C")
        locale.setlocale(locale.LC_ALL, "C")
コード例 #17
0
ファイル: test_processors.py プロジェクト: Big-Data/scrapylib
    def test_localized_to_date(self):
        current_locale = locale.getlocale(locale.LC_ALL)

        self.assertEquals(
            to_date('11 janvier 2011', '%d %B %Y', locale='fr_FR.UTF-8'),
            datetime.date(2011, 1, 11)
        )

        self.assertEquals(current_locale, locale.getlocale(locale.LC_ALL))
コード例 #18
0
ファイル: functions.py プロジェクト: princessnadia/Bootleg
def format_variables(): # formats a few variables to make sure they're correct
    if var.MOD_LOCATION:
        mod_loc = var.MOD_LOCATION.split(";")
        moloc = []
        for semicolon in mod_loc:
            if semicolon == "":
                continue
            semicolon = semicolon.replace("/", "\\")
            if not semicolon[-1:] == "\\":
                semicolon = semicolon + "\\"
            moloc.append(semicolon)
        if moloc:
            var.MOD_LOCATION = moloc
    else:
        var.MOD_LOCATION = [os.getcwd()]
    if var.SYS_FOLDER is None:
        var.SYS_FOLDER = os.getcwd()
    if var.FFVII_PATH is None:
        var.FFVII_PATH = os.getcwd() + "/Final Fantasy VII"
    var.FFVII_PATH = var.FFVII_PATH.replace("/", "\\")
    if not var.FFVII_PATH[-1:] == "\\":
        var.FFVII_PATH = var.FFVII_PATH + "\\"
    if var.BOOTLEG_TEMP is None:
        var.BOOTLEG_TEMP = tempfile.gettempdir() + "\\"
    if not var.BOOTLEG_TEMP[-1:] == "\\":
        var.BOOTLEG_TEMP += "\\"
    _mkdir(var.BOOTLEG_TEMP)
    var.BOOTLEG_TEMP += make_random_() + "\\"
    log.logger(var.BOOTLEG_TEMP, display=False, type="temp")
    os.mkdir(var.BOOTLEG_TEMP) # no integrity check, there's too small a chance that the folder already exists. and if it does, I want an error to occur
    if var.FFVII_IMAGE is not None:
        if not var.FFVII_IMAGE[-4:].lower() == ".zip":
            var.FFVII_IMAGE = None
    if var.LANGUAGE is not None:
        var.LANGUAGE = var.LANGUAGE[0].upper() + var.LANGUAGE[1:].lower()
        if var.LANGUAGE in ("Default", "Current", "System"):
            syslng = locale.getdefaultlocale()[0]
            if locale.getlocale()[0]:
                syslng = locale.getlocale()[0]
            var.LANGUAGE = syslng[:2]
        for lang, lng in con.LANGUAGES.items():
            if var.LANGUAGE.lower() == lng:
                var.LANGUAGE = lang
                break
        if var.LANGUAGE == "None":
            var.LANGUAGE = None
        if var.LANGUAGE not in con.LANGUAGES.keys():
            var.LANGUAGE = None
    if var.LANGUAGE is None:
        var.LANGUAGE = "English"
    for lang in con.LANGUAGES.keys():
        if hasattr(con, lang.upper() + "_TRANSLATORS"):
            lng = getattr(con, lang.upper() + "_TRANSLATORS")
            for trnl in lng:
                if trnl not in con.TRANSLATORS:
                    con.TRANSLATORS.append(trnl)
コード例 #19
0
ファイル: localization.py プロジェクト: coiel/crochet-cad
    def __call__(self, to_translate):
        """ Return translation for to_translate.

        Checks to ensure the locale hasn't changed  (and re-loads translations
        if it has) and returns suitable translation.
        """
        if (self.lang, self.charset) != locale.getlocale():
            self.lang, self.charset = locale.getlocale()
            self._lookup_translations()
        return self.translator(to_translate)
コード例 #20
0
ファイル: Installer.py プロジェクト: Kodextor/arkos-install
def init_internationalization():
	locale.setlocale(locale.LC_ALL, '')
	loc = locale.getlocale()
	filename = os.path.join(os.path.dirname(__file__), "translations/%s.mo" % locale.getlocale()[0][0:2])
	try:
		translation = gettext.GNUTranslations(open(filename, "rb"))
	except IOError:
		translation = gettext.NullTranslations()

	translation.install(True)
コード例 #21
0
    def get_background_items(self, window, current_folder):
        SlobelMenuItem = Nautilus.MenuItem(
            name = "Slobel::CreateFile",
            label = self.TITLE[getlocale()[0]],
            tip = self.TITLE[getlocale()[0]]
        )

        SlobelMenuItem.connect('activate', self.create_new_file, current_folder)

        return [SlobelMenuItem]
コード例 #22
0
 def setUp(self):
     # save current locale
     self.save_locale = locale.getlocale(locale.LC_ALL)
     # set locale
     locale.setlocale(locale.LC_ALL, "")
     # if not in UTF-8 locale, fallback to en_US.UTF-8
     enc = locale.getlocale(locale.LC_CTYPE)[1]
     if enc is None or enc.upper() != "UTF-8" and enc.upper() != "UTF8":
         print "This test script uses hard-coded Unicode characters and "
         print "needs to run in a UTF-8 locale. Fallback to en_US.UTF-8."
         locale.setlocale(locale.LC_ALL, "en_US.UTF-8")
コード例 #23
0
ファイル: Locale.py プロジェクト: Alberto-Beralix/Beralix
    def display (self):
        self.answers = {}
        (messages, encoding) = locale.getlocale (locale.LC_MESSAGES)
        (ctype, encoding) = locale.getlocale (locale.LC_CTYPE)
        self.answers['user_locale_messages'] = messages
        self.answers['user_locale_ctype'] = ctype

        try:
            i18n = file ("/etc/sysconfig/i18n").readlines ()
            for line in i18n:
                if line.startswith ("LANG="):
                    system_lang = line[5:].strip ('\n"')
                    dot = system_lang.find ('.')
                    if dot != -1:
                        system_lang = system_lang[:dot]
        except:
            system_lang = None

        self.answers['system_locale_lang'] = system_lang

        printer_page_size = None
        try:
            ppd_defs = self.troubleshooter.answers['cups_printer_ppd_defaults']
            for group, options in ppd_defs.iteritems ():
                if options.has_key ("PageSize"):
                    printer_page_size = options["PageSize"]
                    break

        except KeyError:
            try:
                attrs = self.troubleshooter.answers['remote_cups_queue_attributes']
                printer_page_size = attrs["media-default"]
            except KeyError:
                pass

        try:
            job_status = self.troubleshooter.answers["test_page_job_status"]
        except KeyError:
            job_status = []

        self.answers['printer_page_size'] = printer_page_size
        if printer_page_size != None:
            job_page_size = None
            for (test, jobid, printer, doc, status, attrs) in job_status:
                if test:
                    if attrs.has_key ("PageSize"):
                        job_page_size = attrs["PageSize"]
                        self.answers['job_page_size'] = job_page_size
                        if job_page_size != printer_page_size:
                            self.printer_page_size.set_text (printer_page_size)
                            self.job_page_size.set_text (job_page_size)
                            return True

        return False
コード例 #24
0
ファイル: chessboard.py プロジェクト: Xbroom/chessboard
    def initLocale(self):
        locale.setlocale(locale.LC_ALL, "")
        loc = locale.getlocale()

        try:
            filename = "locale/%s.mo" % locale.getlocale()[0][0:2]
            trans = gettext.GNUTranslations(open(filename, "rb"))
        except IOError:
            trans = gettext.NullTranslations()

        trans.install()
コード例 #25
0
ファイル: base.py プロジェクト: raphaelm/logmylife
	def __init__(self):
		locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
		# take first two characters of country code
		loc = locale.getlocale()
		filename = "res/messages_%s.mo" % locale.getlocale()[0][0:2]
		
		try:
			trans = gettext.GNUTranslations(open( filename, "rb" ) )
		except IOError:
			trans = gettext.NullTranslations()

		trans.install(True)
コード例 #26
0
ファイル: localemock.py プロジェクト: gordonzola/srmlf
    def __init__(self, new_locale, categories=None):
        self.old_locales = {}
        self.new_locale = new_locale
        self.categories = categories if categories is not None else\
            [locale.LC_CTYPE]
        for category in self.categories:
            self.old_locales[category] = locale.getlocale(category)

        # testing given locale
        old_locales = locale.getlocale(locale.LC_CTYPE)
        locale.setlocale(locale.LC_CTYPE, self.new_locale)
        locale.setlocale(locale.LC_CTYPE, old_locales)
コード例 #27
0
ファイル: lens.py プロジェクト: vincepii/remember-the-lens
def init_localization():
    locale.setlocale(locale.LC_ALL, '')
    # take first two characters of country code
    loc = locale.getlocale()
    filename = "../locale/%s.mo" % locale.getlocale()[0][0:2]
    try:
        print "Opening message file {} for locale {}".format(filename, loc[0])
        trans = gettext.GNUTranslations(open(filename, "rb"))
    except IOError:
        print "Using default messages (English)"
        trans = gettext.NullTranslations()
    trans.install()
コード例 #28
0
 def test_getsetlocale_issue1813(self):
     # Issue #1813: setting and getting the locale under a Turkish locale
     oldlocale = locale.getlocale()
     self.addCleanup(locale.setlocale, locale.LC_CTYPE, oldlocale)
     try:
         locale.setlocale(locale.LC_CTYPE, 'tr_TR')
     except locale.Error:
         # Unsupported locale on this system
         self.skipTest('test needs Turkish locale')
     loc = locale.getlocale()
     locale.setlocale(locale.LC_CTYPE, loc)
     self.assertEqual(loc, locale.getlocale())
コード例 #29
0
ファイル: utils.py プロジェクト: vladsaveliev/Utils
def set_locale():
    import locale
    try:
        if 'UTF-8' not in locale.getlocale(locale.LC_ALL):
            try:
                locale.setlocale(locale.LC_ALL, 'en_AU.UTF-8')
            except locale.Error:
                locale.setlocale(locale.LC_ALL, 'C.UTF-8')
            var = '.'.join(locale.getlocale(locale.LC_ALL))
            environ['LC_ALL'] = environ['LANG'] = var
    except TypeError:
        pass
コード例 #30
0
ファイル: test_util.py プロジェクト: andy-z/urwid
    def test_locale(self):
        initial = locale.getlocale()

        locale.setlocale(locale.LC_ALL, (None, None))
        util.detect_encoding()
        self.assertEqual(locale.getlocale(), (None, None))

        locale.setlocale(locale.LC_ALL, ('en_US', 'UTF-8'))
        util.detect_encoding()
        self.assertEqual(locale.getlocale(), ('en_US', 'UTF-8'))

        locale.setlocale(locale.LC_ALL, initial)
コード例 #31
0
    logger.exception('Could not set locale settings')

_pref_enc = locale.getpreferredencoding()
if _pref_enc in ('ascii', 'us-ascii', 'ANSI_X3.4-1968'):
    logger.warn(
        'Your system encoding is set to %s, if you want support for special characters\n'
        'or see errors due to encoding, please ensure to configure your system to use "UTF-8"'
        % _pref_enc)
    # NOTE: tried doing this automatically, but failed because locale at
    # python run time initialization seems to define how most libraries
    # handle encoding. Specifically the value of "getpreferredencoding()"
    # does not change as result of calling "setlocale()"

## Initialize gettext  (maybe make this optional later for module use ?)

_lang, _enc = locale.getlocale()
if os.name == "nt" and not os.environ.get('LANG') and _lang not in (None, 'C'):
    # Set locale config for gettext (other platforms have this by default)
    # Using LANG because it is lowest prio - do not override other params
    os.environ['LANG'] = _lang + '.' + _enc if _enc else _lang

_localedir = os.path.join(os.path.dirname(ZIM_EXECUTABLE), 'locale')

try:
    if os.path.isdir(_localedir):
        # We are running from a source dir - use the locale data included there
        gettext.install('zim', _localedir, names=('_', 'gettext', 'ngettext'))
    else:
        # Hope the system knows where to find the data
        gettext.install('zim', None, names=('_', 'gettext', 'ngettext'))
except:
コード例 #32
0
    def __init__(self, panora, type, size=(854, 480)):
        self.panora = panora

        self.activePage = None

        # Create Qt application and the QDeclarative view
        class ModifiedQDeclarativeView(QDeclarativeView):
            def __init__(self, gui):
                QDeclarativeView.__init__(self)
                self.gui = gui

            def closeEvent(self, event):
                print("shutting down")
                self.gui.panora.destroy()

        self.app = QApplication(sys.argv)
        self.view = ModifiedQDeclarativeView(self)
        # try OpenGl acceleration
        glw = QtOpenGL.QGLWidget()
        self.view.setViewport(glw)
        self.window = QMainWindow()
        self.window.resize(*size)
        self.window.setCentralWidget(self.view)
        self.view.setResizeMode(QDeclarativeView.SizeRootObjectToView)

        # add image providers
        #    self.pageProvider = MangaPageImageProvider(self)
        self.iconProvider = IconImageProvider()
        #    self.view.engine().addImageProvider("page",self.pageProvider)
        self.view.engine().addImageProvider("icons", self.iconProvider)
        rc = self.view.rootContext()
        # make the main context accessible from QML
        rc.setContextProperty("panora", Panora(self))
        # make options accessible from QML
        options = Options(self.panora)
        rc.setContextProperty("options", options)
        # make platform module accessible from QML
        platform = Platform(self.panora)
        rc.setContextProperty("platform", platform)

        # activate translation
        translator = QtCore.QTranslator(self.app)
        if self.panora.args.locale is not None:
            localeId = self.panora.args.locale
        else:
            localeId = locale.getlocale()[0]
        translator.load("gui/qml/i18n/qml_" + localeId)
        self.app.installTranslator(translator)

        # Set the QML file and show
        self.view.setSource(QUrl('gui/qml/main.qml'))
        self.view.show()
        self.window.closeEvent = self._qtWindowClosed

        self.rootObject = self.view.rootObject()

        self.toggleFullscreen()

        # check if first start dialog has to be shown
        if self.panora.get("QMLShowFirstStartDialog", True):
            self.rootObject.openFirstStartDialog()
コード例 #33
0
ファイル: weather.py プロジェクト: mrizyver/linux-config
temperature = round((weather['currently']['temperature'] - 32) * 5 / 9)

# Get current weather condition:
condition = weather['currently']['summary']

# Translate city when approriate:
cdict = dict({
    'Boston': {
        'ga': 'Bostún'
    },
    'Dublin': {
        'ga': 'Baile Átha Cliath'
    }
})

language = locale.getlocale()[0]
if language == 'ga_IE':
    try:
        city = cdict[city]['ga']
    except KeyError:
        city = city
elif language == 'de_DE':
    try:
        city = cdict[city]['de']
    except KeyError:
        city = city

# Get weather translation and icon:
df = pd.read_csv(os.path.expanduser('~/') + '.config/i3/scripts/weather.csv')
df = df[df.en == condition]
コード例 #34
0
ファイル: tkui.py プロジェクト: rascul/lyntin-source
Lyntin, but largely re-coded in various areas.
"""

from Tkinter import *
from ScrolledText import ScrolledText
import os, tkFont, types, Queue
import locale
import sys
from lyntin import ansi, event, engine, exported, utils, constants, config
from lyntin.ui import base, message


if locale.__dict__.has_key('getpreferredencoding'):
  UNICODE_ENCODING = locale.getpreferredencoding() # python2.3 and later
else:  
  UNICODE_ENCODING = locale.getlocale()[1]

try:
  ''.decode(UNICODE_ENCODING).encode(UNICODE_ENCODING)
except:
  try:
    UNICODE_ENCODING = 'CP' + UNICODE_ENCODING # Kludge for 2.2
    ''.decode(UNICODE_ENCODING).encode(UNICODE_ENCODING)
  except:  
    UNICODE_ENCODING = 'latin-1'


def _decode(text):
    """
    Decodes the text to the unicode representation.
    Tries its best to replace illegal characters.
コード例 #35
0
ファイル: __init__.py プロジェクト: thezedwards/pybrowscap
def load_file(browscap_file_path):
    """
    Loading browscap csv data file, parsing in into accessible python
    form and returning a new Browscap class instance with all appropriate data.

    :param browscap_file_path: location of browcap file on filesystem
    :type browscap_file_path: string
    :returns: Browscap instance filled with data
    :rtype: pybrowscap.loader.Browscap

    """
    def replace_defaults(line, defaults):
        """Replaces 'default' values for a line with parent line value and converting it into native python value.

        :param line: original line from browscap file
        :type line: dict
        :param defaults: default values for current line
        :type defaults: dict
        :returns: dictionary with replaced default values
        :rtype: dict
        :raises: IOError

        """
        new_line = {}
        try:
            items = line.iteritems()
        except AttributeError:
            items = line.items()
        for feature, value in items:
            feature = feature.lower()
            if value == 'default' or value == '':
                value = defaults.get(feature, value)
            if value == 'true':
                value = True
            if value == 'false':
                value = False
            if feature == 'minorver' and value == '0':
                value = defaults.get(feature, value)
            if feature == 'majorver' or feature == 'minorver':
                try:
                    value = int(value)
                except (ValueError, OverflowError):
                    value = 0
            if (feature == 'version'
                    or feature == 'renderingengine_version') and value == '0':
                value = defaults.get(feature, value)
            if (feature == 'cssversion' or feature == 'aolversion'
                    or feature == 'version'
                    or feature == 'renderingengine_version'
                    or feature == 'platform_version'):
                try:
                    value = float(value)
                except (ValueError, OverflowError):
                    value = float(0)
            new_line[feature.lower()] = value
        return new_line

    try:
        with open(browscap_file_path, 'r') as csvfile:
            # in py3
            # mode='rb', read return bytes
            # mode='r' , read return str
            log.info('Reading browscap source file %s', browscap_file_path)
            dialect = csv.Sniffer().sniff(csvfile.read(4096))
            csvfile.seek(0)
            log.info('Getting file version and release date')
            csvfile.readline()
            reader = csv.reader(StringIO(csvfile.readline()))
            for line in reader:  # read version and date to line
                break
            log.info('Getting browcap file version')
            try:
                version = int(line[0])
            except ValueError:
                log.exception('Error while getting browscap file version')
                version = None
            log.info("Browscap Version: {}".format(version))
            log.info('Getting browscap file release date')
            try:
                old_locale = locale.getlocale()
                locale.setlocale(locale.LC_TIME,
                                 locale.normalize('en_US.utf8'))
                release_date = datetime.strptime(line[1][:-6],
                                                 '%a, %d %b %Y %H:%M:%S')
            except (ValueError, locale.Error):
                log.exception('Error while getting browscap file release date')
                release_date = None
            finally:
                locale.setlocale(locale.LC_TIME, old_locale)
            log.info("Browscap Release Date: {}".format(release_date))

            log.info('Reading browscap user-agent data')
            reader = csv.DictReader(csvfile, dialect=dialect)
            defaults = {}
            browscap_data = {}
            regex_cache = []

            for line in reader:
                if line['PropertyName'] == 'DefaultProperties':
                    for key in line:
                        defaults[key.lower()] = line[
                            key]  # turn defaults key to lower
                    break
            for line in reader:
                line = replace_defaults(line, defaults)
                try:
                    ua_regex = '^{0}$'.format(re.escape(line['propertyname']))
                    ua_regex = ua_regex.replace('\\?',
                                                '.').replace('\\*', '.*?')
                    browscap_data[ua_regex] = line
                    log.debug('Compiling user agent regex: %s', ua_regex)
                    regex_cache.append(re.compile(ua_regex, re.IGNORECASE))
                except sre_constants.error:
                    continue
        return Browscap(browscap_data, regex_cache, browscap_file_path,
                        TYPE_CSV, version, release_date)
    except IOError:
        log.exception('Error while reading browscap source file %s',
                      browscap_file_path)
        raise
コード例 #36
0
ファイル: main.py プロジェクト: hayate891/exaile
    def __init(self):
        """
            Initializes Exaile
        """
        # pylint: disable-msg=W0201
        logger.info("Loading Exaile %s on Python %s..." % (__version__, platform.python_version()))

        logger.info("Loading settings...")
        try:
            from xl import settings
        except common.VersionError:
            logger.exception("Error loading settings")
            sys.exit(1)
            
        logger.debug("Settings loaded from %s" % settings.location)
        
        # display locale information if available
        try:
            import locale
            lc, enc = locale.getlocale()
            if enc is not None:
                logger.info("Using %s %s locale" % (lc, enc))
            else:
                logger.info("Using unknown locale")
        except Exception:
            pass

        splash = None

        if self.options.StartGui:
            if settings.get_option('gui/use_splash', True):
                from xlgui.widgets.info import Splash

                splash = Splash()
                splash.show()

        firstrun = settings.get_option("general/first_run", True)

        if not self.options.NoImport and \
                (firstrun or self.options.ForceImport):
            try:
                sys.path.insert(0, xdg.get_data_path("migrations"))
                import migration_200907100931 as migrator
                del sys.path[0]
                migrator.migrate(force=self.options.ForceImport)
                del migrator
            except Exception:
                logger.exception("Failed to migrate from 0.2.14")

        # Migrate old rating options
        from xl.migrations.settings import rating
        rating.migrate()

        # Migrate builtin OSD to plugin
        from xl.migrations.settings import osd
        osd.migrate()
        
        # Migrate engines
        from xl.migrations.settings import engine
        engine.migrate()
        
        # TODO: enable audio plugins separately from normal
        #       plugins? What about plugins that use the player?
        
        # Gstreamer doesn't initialize itself automatically, and fails
        # miserably when you try to inherit from something and GST hasn't
        # been initialized yet. So this is here.
        from gi.repository import Gst
        Gst.init(None)

        # Initialize plugin manager
        from xl import plugins
        self.plugins = plugins.PluginsManager(self)
        
        if not self.options.SafeMode:
            logger.info("Loading plugins...")
            self.plugins.load_enabled()
        else:
            logger.info("Safe mode enabled, not loading plugins.")

        # Initialize the collection
        logger.info("Loading collection...")
        from xl import collection
        try:
            self.collection = collection.Collection("Collection",
                    location=os.path.join(xdg.get_data_dir(), 'music.db'))
        except common.VersionError:
            logger.exception("VersionError loading collection")
            sys.exit(1)

        from xl import event
        # Set up the player and playback queue
        from xl import player
        event.log_event("player_loaded", player.PLAYER, None)

        # Initalize playlist manager
        from xl import playlist
        self.playlists = playlist.PlaylistManager()
        self.smart_playlists = playlist.SmartPlaylistManager('smart_playlists',
            collection=self.collection)
        if firstrun:
            self._add_default_playlists()
        event.log_event("playlists_loaded", self, None)

        # Initialize dynamic playlist support
        from xl import dynamic
        dynamic.MANAGER.collection = self.collection

        # Initalize device manager
        logger.info("Loading devices...")
        from xl import devices
        self.devices = devices.DeviceManager()
        event.log_event("device_manager_ready", self, None)

        # Initialize dynamic device discovery interface
        # -> if initialized and connected, then the object is not None
        
        self.udisks2 = None
        self.udisks = None
        self.hal = None
        
        if self.options.Hal:
            from xl import hal
                
            udisks2 = hal.UDisks2(self.devices)
            if udisks2.connect():
                self.udisks2 = udisks2
            else:
                udisks = hal.UDisks(self.devices)
                if udisks.connect():
                    self.udisks = udisks
                else:
                    self.hal = hal.HAL(self.devices)
                    self.hal.connect()
        else:
            self.hal = None

        # Radio Manager
        from xl import radio
        self.stations = playlist.PlaylistManager('radio_stations')
        self.radio = radio.RadioManager()

        self.gui = None
        # Setup GUI
        if self.options.StartGui:
            logger.info("Loading interface...")

            import xlgui
            self.gui = xlgui.Main(self)
            self.gui.main.window.show_all()
            event.log_event("gui_loaded", self, None)

            if splash is not None:
                splash.destroy()

        if firstrun:
            settings.set_option("general/first_run", False)

        self.loading = False
        Exaile._exaile = self
        event.log_event("exaile_loaded", self, None)

        restore = True

        if self.gui:
            # Find out if the user just passed in a list of songs
            # TODO: find a better place to put this
            
            songs = [ Gio.File.new_for_path(arg).get_uri() for arg in self.options.locs ]
            if len(songs) > 0:
                restore = False
                self.gui.open_uri(songs[0], play=True)
                for arg in songs[1:]:
                    self.gui.open_uri(arg)
            
            # kick off autoscan of libraries
            # -> don't do it in command line mode, since that isn't expected
            self.gui.rescan_collection_with_progress(True)

        if restore:
            player.QUEUE._restore_player_state(
                    os.path.join(xdg.get_data_dir(), 'player.state'))
コード例 #37
0
# file: share/when-wizard/modules/power-hibernate.py
# -*- coding: utf-8 -*-
#
# Task plugin to enter hibernation
# Copyright (c) 2015-2018 Francesco Garosi
# Released under the BSD License (see LICENSE file)

import locale
from plugin import TaskPlugin, PLUGIN_CONST, plugin_name

# setup i18n for both applet text and dialogs
locale.setlocale(locale.LC_ALL, locale.getlocale())
locale.bindtextdomain(APP_NAME, APP_LOCALE_FOLDER)
locale.textdomain(APP_NAME)
_ = locale.gettext

HELP = _("""\
This action hibernates your Workstation, saving its current state and entering
a very low-consumption mode: depending on your OS release and your settings
the system will resume operations by either providing input or pressing the
power button. In some cases network activity could cause a wakeup.
""")

HIBERNATE_COMMAND = """\
sleep 2 && \
gdbus call -y -d org.freedesktop.UPower \
              -o /org/freedesktop/UPower \
              -m org.freedesktop.UPower.Suspend
"""

コード例 #38
0
    def run(self):
        """Run the bot."""
        while True:
            welcomed_count = 0
            for users in self.parseNewUserLog():
                if users.isBlocked():
                    showStatus(3)
                    pywikibot.output('%s has been blocked!' % users.username)
                    continue
                if 'bot' in users.groups():
                    showStatus(3)
                    pywikibot.output('%s is a bot!' % users.username)
                    continue
                if 'bot' in users.username.lower():
                    showStatus(3)
                    pywikibot.output(u'%s might be a global bot!'
                                     % users.username)
                    continue
                if users.editCount() >= globalvar.attachEditCount:
                    showStatus(2)
                    pywikibot.output(u'%s has enough edits to be welcomed.'
                                     % users.username)
                    ustp = users.getUserTalkPage()
                    if ustp.exists():
                        showStatus(3)
                        pywikibot.output(u'%s has been already welcomed.'
                                         % users.username)
                        continue
                    else:
                        if self.badNameFilter(users.username):
                            self.reportBadAccount(users.username)
                            continue
                        welcome_text = self.welcome_text
                        if globalvar.randomSign:
                            if self.site.family.name != 'wikinews':
                                welcome_text = (welcome_text
                                                % choice(self.defineSign()))
                            if self.site.family.name == 'wiktionary' and \
                               self.site.code == 'it':
                                pass
                            else:
                                welcome_text += timeselected
                        elif (self.site.family.name != 'wikinews' and
                              self.site.code != 'it'):
                            welcome_text = (welcome_text
                                            % globalvar.defaultSign)
                        final_text = i18n.translate(
                            self.site, final_new_text_additions)
                        if final_text:
                            welcome_text += final_text
                        welcome_comment = i18n.twtranslate(self.site,
                                                           'welcome-welcome')
                        try:
                            # append welcomed, welcome_count++
                            ustp.put(welcome_text, welcome_comment,
                                     minorEdit=False)
                            welcomed_count += 1
                            self._totallyCount += 1
                            self.welcomed_users.append(users)
                        except pywikibot.EditConflict:
                            showStatus(4)
                            pywikibot.output(u'An edit conflict has occurred, '
                                             u'skipping this user.')

                    if globalvar.makeWelcomeLog and \
                       i18n.translate(self.site, logbook):
                        showStatus(5)
                        if welcomed_count == 1:
                            pywikibot.output(u'One user has been welcomed.')
                        elif welcomed_count == 0:
                            pywikibot.output(u'No users have been welcomed.')
                        else:
                            pywikibot.output(u'%s users have been welcomed.'
                                             % welcomed_count)
                        if welcomed_count >= globalvar.dumpToLog:
                            if self.makelogpage(self.welcomed_users):
                                self.welcomed_users = []
                                welcomed_count = 0
                            else:
                                continue
                    # If we haven't to report, do nothing.
                else:
                    if users.editCount() == 0:
                        if not globalvar.quiet:
                            showStatus(1)
                            pywikibot.output(u'%s has no contributions.'
                                             % users.username)
                    else:
                        showStatus(1)
                        pywikibot.output(u'%s has only %d contributions.'
                                         % (users.username, users.editCount()))
                    # That user mustn't be welcomed.
                    continue
            if globalvar.makeWelcomeLog and i18n.translate(
                    self.site, logbook) and welcomed_count > 0:
                showStatus()
                if welcomed_count == 1:
                    pywikibot.output(u'Putting the log of the latest user...')
                else:
                    pywikibot.output(
                        u'Putting the log of the latest %d users...'
                        % welcomed_count)
                if not self.makelogpage(self.welcomed_users):
                    continue
                self.welcomed_users = []
            if hasattr(self, '_BAQueue'):
                showStatus()
                pywikibot.output("Putting bad name to report page....")
                self.reportBadAccount(None, final=True)
            try:
                if globalvar.recursive:
                    showStatus()
                    if locale.getlocale()[1]:
                        strfstr = time.strftime(
                            '%d %b %Y %H:%M:%S (UTC)', time.gmtime())
                        # py2-py3 compatibility
                        if not isinstance(strfstr, UnicodeType):
                            strfstr = strfstr.decode(locale.getlocale()[1])
                    else:
                        strfstr = time.strftime(
                            u"%d %b %Y %H:%M:%S (UTC)", time.gmtime())
                    pywikibot.output(u'Sleeping %d seconds before rerun. %s'
                                     % (globalvar.timeRecur, strfstr))
                    pywikibot.stopme()
                    time.sleep(globalvar.timeRecur)
                else:
                    raise KeyboardInterrupt
            except KeyboardInterrupt:
                break
コード例 #39
0
    def _setSystemInfo(self):
        """System info
        """
        # system encoding
        osEncoding = sys.getfilesystemencoding()

        # machine name
        self['systemHostName'] = platform.node()

        self['systemMemTotalRAM'], self['systemMemFreeRAM'] = getRAM()

        # locale information:
        # (None, None) -> str
        loc = '.'.join([str(x) for x in locale.getlocale()])
        if loc == 'None.None':
            loc = locale.setlocale(locale.LC_ALL, '')
        # == the locale in use, from OS or user-pref
        self['systemLocale'] = loc

        # platform name, etc
        if sys.platform in ['darwin']:
            OSXver, _junk, architecture = platform.mac_ver()
            platInfo = 'darwin ' + OSXver + ' ' + architecture
            # powerSource = ...
        elif sys.platform.startswith('linux'):
            platInfo = 'linux ' + platform.release()
            # powerSource = ...
        elif sys.platform in ['win32']:
            platInfo = 'windowsversion=' + repr(sys.getwindowsversion())
            # powerSource = ...
        else:
            platInfo = ' [?]'
            # powerSource = ...
        self['systemPlatform'] = platInfo
        # self['systemPowerSource'] = powerSource

        # count all unique people (user IDs logged in), and find current user
        # name & UID
        self['systemUser'], self['systemUserID'] = _getUserNameUID()
        try:
            users = shellCall("who -q").splitlines()[0].split()
            self['systemUsersCount'] = len(set(users))
        except Exception:
            self['systemUsersCount'] = False

        # when last rebooted?
        try:
            lastboot = shellCall("who -b").split()
            self['systemRebooted'] = ' '.join(lastboot[2:])
        except Exception:  # windows
            sysInfo = shellCall('systeminfo').splitlines()
            lastboot = [
                line for line in sysInfo if line.startswith("System Up Time")
                or line.startswith("System Boot Time")
            ]
            lastboot += ['[?]']  # put something in the list just in case
            self['systemRebooted'] = lastboot[0].strip()

        # R (and r2py) for stats:
        try:
            Rver = shellCall(["R", "--version"])
            Rversion = Rver.splitlines()[0]
            if Rversion.startswith('R version'):
                self['systemRavailable'] = Rversion.strip()
            try:
                import rpy2
                self['systemRpy2'] = rpy2.__version__
            except ImportError:
                pass
        except Exception:
            pass

        # encryption / security tools:
        try:
            vers, se = shellCall('openssl version', stderr=True)
            if se:
                vers = str(vers) + se.replace('\n', ' ')[:80]
            if vers.strip():
                self['systemSec.OpenSSLVersion'] = vers
        except Exception:
            pass
        try:
            so = shellCall(['gpg', '--version'])
            if so.find('GnuPG') > -1:
                self['systemSec.GPGVersion'] = so.splitlines()[0]
                _home = [
                    line.replace('Home:', '').lstrip()
                    for line in so.splitlines() if line.startswith('Home:')
                ]
                self['systemSec.GPGHome'] = ''.join(_home)
        except Exception:
            pass
        try:
            import ssl
            self['systemSec.pythonSSL'] = True
        except ImportError:
            self['systemSec.pythonSSL'] = False

        # pyo for sound:
        try:
            travis = bool(str(os.environ.get('TRAVIS')).lower() == 'true')
            assert not travis  # skip sound-related stuff on travis-ci.org

            import pyo
            self['systemPyoVersion'] = '%i.%i.%i' % pyo.getVersion()
            try:
                # requires pyo svn r1024 or higher:
                inp, out = pyo.pa_get_devices_infos()
                for devList in [inp, out]:
                    for key in devList.keys():
                        if isinstance(devList[key]['name'], str):
                            devList[key]['name'] = devList[key]['name'].decode(
                                osEncoding)
                self['systemPyo.InputDevices'] = inp
                self['systemPyo.OutputDevices'] = out
            except AttributeError:
                pass
        except (AssertionError, ImportError):
            pass

        # flac (free lossless audio codec) for google-speech:
        flacv = ''
        if sys.platform == 'win32':
            flacexe = 'C:\\Program Files\\FLAC\\flac.exe'
            if os.path.exists(flacexe):
                flacv = core.shellCall(flacexe + ' --version')
        else:
            flac, se = core.shellCall('which flac', stderr=True)
            if not se and flac and not flac.find('Command not found') > -1:
                flacv = core.shellCall('flac --version')
        if flacv:
            self['systemFlacVersion'] = flacv

        # detect internet access or fail quickly:
        # web.setupProxy() & web.testProxy(web.proxies)  # can be slow
        # to fail if there's no connection
        self['systemHaveInternetAccess'] = web.haveInternetAccess()
        if not self['systemHaveInternetAccess']:
            self['systemHaveInternetAccess'] = 'False (proxies not attempted)'
コード例 #40
0
def parse_msa_string(string, filename=None):
    """Parse an EMSA/MSA file content.

    Parameters
    ----------
    string: string or file object
        It must complain with the EMSA/MSA standard.
    filename: string or None
        The filename.

    Returns:
    --------
    file_data_list: list
        The list containts a dictionary that contains the parsed
        information. It can be used to create a `:class:BaseSignal`
        using `:func:hyperspy.io.dict2signal`.

    """
    if not hasattr(string, "readlines"):
        string = string.splitlines()
    parameters = {}
    mapped = DictionaryTreeBrowser({})
    y = []
    # Read the keywords
    data_section = False
    for line in string:
        if data_section is False:
            if line[0] == "#":
                try:
                    key, value = line.split(': ')
                    value = value.strip()
                except ValueError:
                    key = line
                    value = None
                key = key.strip('#').strip()

                if key != 'SPECTRUM':
                    parameters[key] = value
                else:
                    data_section = True
        else:
            # Read the data
            if line[0] != "#" and line.strip():
                if parameters['DATATYPE'] == 'XY':
                    xy = line.replace(',', ' ').strip().split()
                    y.append(float(xy[1]))
                elif parameters['DATATYPE'] == 'Y':
                    data = [
                        float(i)
                        for i in line.replace(',', ' ').strip().split()
                    ]
                    y.extend(data)
    # We rewrite the format value to be sure that it complies with the
    # standard, because it will be used by the writer routine
    parameters['FORMAT'] = "EMSA/MAS Spectral Data File"

    # Convert the parameters to the right type and map some
    # TODO: the msa format seems to support specifying the units of some
    # parametes. We should add this feature here
    for parameter, value in parameters.items():
        # Some parameters names can contain the units information
        # e.g. #AZIMANGLE-dg: 90.
        if '-' in parameter:
            clean_par, units = parameter.split('-')
            clean_par, units = clean_par.strip(), units.strip()
        else:
            clean_par, units = parameter, None
        if clean_par in keywords:
            try:
                parameters[parameter] = keywords[clean_par]['dtype'](value)
            except:
                # Normally the offending mispelling is a space in the scientic
                # notation, e.g. 2.0 E-06, so we try to correct for it
                try:
                    parameters[parameter] = keywords[clean_par]['dtype'](
                        value.replace(' ', ''))
                except:
                    _logger.exception(
                        "The %s keyword value, %s could not be converted to "
                        "the right type", parameter, value)

            if keywords[clean_par]['mapped_to'] is not None:
                mapped.set_item(keywords[clean_par]['mapped_to'],
                                parameters[parameter])
                if units is not None:
                    mapped.set_item(
                        keywords[clean_par]['mapped_to'] + '_units', units)

    # The data parameter needs some extra care
    # It is necessary to change the locale to US english to read the date
    # keyword
    loc = locale.getlocale(locale.LC_TIME)
    # Setting locale can raise an exception because
    # their name depends on library versions, platform etc.
    try:
        if os_name == 'posix':
            locale.setlocale(locale.LC_TIME, ('en_US', 'utf8'))
        elif os_name == 'windows':
            locale.setlocale(locale.LC_TIME, 'english')
        try:
            H, M = time.strptime(parameters['TIME'], "%H:%M")[3:5]
            mapped.set_item('General.time', datetime.time(H, M))
        except:
            if 'TIME' in parameters and parameters['TIME']:
                _logger.warn('The time information could not be retrieved')
        try:
            Y, M, D = time.strptime(parameters['DATE'], "%d-%b-%Y")[0:3]
            mapped.set_item('General.date', datetime.date(Y, M, D))
        except:
            if 'DATE' in parameters and parameters['DATE']:
                _logger.warn('The date information could not be retrieved')
    except:
        warnings.warn("I couldn't write the date information due to"
                      "an unexpected error. Please report this error to "
                      "the developers")
    locale.setlocale(locale.LC_TIME, loc)  # restore saved locale

    axes = [{
        'size': len(y),
        'index_in_array': 0,
        'name': parameters['XLABEL'] if 'XLABEL' in parameters else '',
        'scale': parameters['XPERCHAN'] if 'XPERCHAN' in parameters else 1,
        'offset': parameters['OFFSET'] if 'OFFSET' in parameters else 0,
        'units': parameters['XUNITS'] if 'XUNITS' in parameters else '',
    }]
    if filename is not None:
        mapped.set_item('General.original_filename',
                        os.path.split(filename)[1])
    mapped.set_item('Signal.record_by', 'spectrum')
    if mapped.has_item('Signal.signal_type'):
        if mapped.Signal.signal_type == 'ELS':
            mapped.Signal.signal_type = 'EELS'
    else:
        # Defaulting to EELS looks reasonable
        mapped.set_item('Signal.signal_type', 'EELS')

    dictionary = {
        'data': np.array(y),
        'axes': axes,
        'metadata': mapped.as_dictionary(),
        'original_metadata': parameters
    }
    file_data_list = [
        dictionary,
    ]
    return file_data_list
コード例 #41
0
def main(argv=None):
    """
    Main function : set options, set logging system and launch GUI
    """

    # parse command line options and parameters
    progName, dirProject, baseDirPath = parseParameters(argv, __doc__)

    # Read configuration properties
    configApp = readConfigurationFile(dirProject)

    # Set local : country, region and encoding
    setLocaleCalcal(configApp, dirProject)

    # Get user workdir for loging system
    homeUser = os.path.expanduser("~")
    if platform.system() == 'Darwin':  # On Mac
        homeUser = os.path.join(homeUser, "Documents")
    homeCalcAl = os.path.join(homeUser, configApp.get('Resources',
                                                      'AppDataDir'))
    if not os.path.exists(homeCalcAl):
        os.mkdir(homeCalcAl)
        print(_("Directory created") + " :", homeCalcAl)

    # Start logging message system
    try:
        initLogging(configApp, homeCalcAl)
    except Exception as exc:
        fileError = os.path.join(homeCalcAl, "ErrStartLogCalcal.txt")
        with open(fileError, mode='w') as hError:
            hError.write(str(exc))
    logger = logging.getLogger(configApp.get('Log', 'LoggerName'))

    # Welcome and configuration messages
    idProg = configApp.get('Version', 'AppName') + ' - version ' + \
             configApp.get('Version', 'Number') + ' du ' + \
             configApp.get('Version', 'Date')
    logger.info(_("Starting") + " " + progName + " : " + idProg)
    welcomeMsg = _("Thanks to you") + " " + getpass.getuser() + " " + \
                 _("for using this software") + " !"
    logger.info(welcomeMsg)
    emails = configApp.get('Version', 'EmailSupport1') + ", " +\
             configApp.get('Version', 'EmailSupport2')
    logger.info(_("To contact authors") + " : " + emails)
    logger.info(
        _("On") + " : " + platform.system() + ", " + platform.release() +
        ", " + "Python : " + platform.python_version() + ", Tk : " +
        str(TkVersion))
    logger.info(
        _("Detected language and encoding on this computer") + " = " +
        str(locale.getlocale()))

    # Init user databases
    if baseDirPath is None:
        baseDirPath = os.path.join(homeCalcAl,
                                   configApp.get('Resources', 'DatabaseDir'))
    installUserDatabases(configApp, logger, baseDirPath, dirProject)

    # Init database manager
    databaseManager = DatabaseManager.DatabaseManager(configApp, dirProject,
                                                      baseDirPath)

    # Launch GUI
    CalcAlGUI.CalcAlGUI(configApp, dirProject, databaseManager).mainloop()

    logger.info(_("End of") + " " + progName + " : " + idProg)
    logging.shutdown()  # Terminaison of logging system
コード例 #42
0
import locale
loc = locale.getlocale()  # get current locale
#locale.getdefaultlocale()

print loc
コード例 #43
0
    def _informationWidget(self):
        group = QGroupBox()
        group.setTitle(catalog.i18nc("@title:groupbox", "System information"))
        layout = QVBoxLayout()
        label = QLabel()

        try:
            from UM.Application import Application
            self.cura_version = Application.getInstance().getVersion()
            self.cura_locale = Application.getInstance().getPreferences(
            ).getValue("general/language")
        except:
            self.cura_version = catalog.i18nc("@label unknown version of Cura",
                                              "Unknown")
            self.cura_locale = "??_??"

        self.data["cura_version"] = self.cura_version
        self.data["os"] = {
            "type": platform.system(),
            "version": platform.version()
        }
        self.data["qt_version"] = QT_VERSION_STR
        self.data["pyqt_version"] = PYQT_VERSION_STR
        self.data["locale_os"] = locale.getlocale(locale.LC_MESSAGES)[0] if hasattr(locale, "LC_MESSAGES") else \
        locale.getdefaultlocale()[0]
        self.data["locale_cura"] = self.cura_locale

        crash_info = "<b>" + catalog.i18nc("@label Cura version number",
                                           "Cura version") + ":</b> " + str(
                                               self.cura_version) + "<br/>"
        crash_info += "<b>" + catalog.i18nc("@label",
                                            "Cura language") + ":</b> " + str(
                                                self.cura_locale) + "<br/>"
        crash_info += "<b>" + catalog.i18nc(
            "@label", "OS language") + ":</b> " + str(
                self.data["locale_os"]) + "<br/>"
        crash_info += "<b>" + catalog.i18nc("@label Type of platform",
                                            "Platform") + ":</b> " + str(
                                                platform.platform()) + "<br/>"
        crash_info += "<b>" + catalog.i18nc(
            "@label", "Qt version") + ":</b> " + str(QT_VERSION_STR) + "<br/>"
        crash_info += "<b>" + catalog.i18nc(
            "@label",
            "PyQt version") + ":</b> " + str(PYQT_VERSION_STR) + "<br/>"
        crash_info += "<b>" + catalog.i18nc(
            "@label OpenGL version", "OpenGL") + ":</b> " + str(
                self._getOpenGLInfo()) + "<br/>"

        label.setText(crash_info)

        layout.addWidget(label)
        group.setLayout(layout)

        if with_sentry_sdk:
            with configure_scope() as scope:
                scope.set_tag("qt_version", QT_VERSION_STR)
                scope.set_tag("pyqt_version", PYQT_VERSION_STR)
                scope.set_tag("os", platform.system())
                scope.set_tag("os_version", platform.version())
                scope.set_tag("locale_os", self.data["locale_os"])
                scope.set_tag("locale_cura", self.cura_locale)
                scope.set_tag("is_enterprise",
                              ApplicationMetadata.IsEnterpriseVersion)

                scope.set_user({"id": str(uuid.getnode())})

        return group
コード例 #44
0
import locale
import getpass
import requests
# from hyper.contrib import HTTP20Adapter

from .. import models

[LOCALE, _] = locale.getlocale()

# API_SCHEME_AND_DOMAIN = "https://api.tidalhifi.com"
API_ENDPOINT = "https://api.tidalhifi.com/v1"
API_TOKEN = "pl4Vc0hemlAXD0mN"
USER_AGENT = "TIDAL_ANDROID/879 okhttp/3.10.0"
CLIENT_VERSION = "2.10.1"
DEVICE_TYPE = "TABLET"
MAXIMUM_LIMIT = 50
CLIENT_UNIQUE_KEY = "kjadsfjkhsadkjhasdkjh"  # apparently, this string is arbitrary:
# https://github.com/lucaslg26/TidalAPI/issues/22

COVER_ART_SIZE = 1280


class TidalClient(models.Client):
    name = "TIDAL"

    def __init__(self, config):
        username = config["tidal-username"] or input("Tidal username: "******"tidal-password"] or getpass.getpass(
            "Tidal password: "******"search-count"]
        self._quality = config["tidal-quality"]
コード例 #45
0
ファイル: _test_decorators.py プロジェクト: huaxz1986/pandas
def _skip_if_not_us_locale():
    lang, _ = locale.getlocale()
    if lang != "en_US":
        return True
コード例 #46
0
ファイル: utils.py プロジェクト: Egniel/parser-template
def set_locale(locale_):
    initial_locale = '.'.join(locale.getlocale())
    # TODO make better.
    locale_ = locale.normalize(locale_ + '.utf8')
    yield locale.setlocale(locale.LC_ALL, locale_)
    locale.setlocale(locale.LC_ALL, initial_locale)
コード例 #47
0
ファイル: _test_decorators.py プロジェクト: huaxz1986/pandas
def _skip_if_has_locale():
    lang, _ = locale.getlocale()
    if lang is not None:
        return True
コード例 #48
0
ファイル: util.py プロジェクト: lucchouina/Rim
move_prev_char = str_util.move_prev_char
within_double_byte = str_util.within_double_byte

try:
    enumerate
except:
    enumerate = lambda x: zip(range(len(x)), x)  # old python

# Try to determine if using a supported double-byte encoding
import locale
try:
    try:
        locale.setlocale(locale.LC_ALL, "")
    except locale.Error:
        pass
    detected_encoding = locale.getlocale()[1]
    if not detected_encoding:
        detected_encoding = ""
except ValueError, e:
    # with invalid LANG value python will throw ValueError
    if e.args and e.args[0].startswith("unknown locale"):
        detected_encoding = ""
    else:
        raise

_target_encoding = None
_use_dec_special = True


def set_encoding(encoding):
    """
コード例 #49
0
 def __get_locale_currency(self):
     if locale.getlocale() == (None, None):
         locale.setlocale(locale.LC_ALL, '')
     mnemonic = locale.localeconv()['int_curr_symbol'].strip() or "EUR"
     return self.book.currencies(mnemonic=mnemonic)
コード例 #50
0
 def __enter__(self):
     self.oldlocale = _locale.getlocale(_locale.LC_TIME)
     _locale.setlocale(_locale.LC_TIME, self.locale)
     return _locale.getlocale(_locale.LC_TIME)[1]
コード例 #51
0
ファイル: ExtensionCore.py プロジェクト: linuxmint/cinnamon
SHOW_INACTIVE = 2

SETTING_TYPE_NONE = 0
SETTING_TYPE_INTERNAL = 1
SETTING_TYPE_EXTERNAL = 2

ROW_SIZE = 32

UNSAFE_ITEMS = [
    'spawn_sync', 'spawn_command_line_sync', 'GTop',
    'get_file_contents_utf8_sync'
]

LANGUAGE_CODE = "C"
try:
    LANGUAGE_CODE = locale.getlocale()[0].split("_")[0]
except:
    pass


@lru_cache(maxsize=None)  # fetch only once
def get_cinnamon_version():
    version_str = subprocess.check_output(['cinnamon', '--version'],
                                          encoding="utf-8").split()[1]
    return [int(part) for part in version_str.split(".")]


def find_extension_subdir(directory):
    largest = [0]
    curr_a = get_cinnamon_version()
コード例 #52
0
 def test_basic(self):
     self.assertEqual(_strptime._getlang(),
                      locale.getlocale(locale.LC_TIME))
コード例 #53
0
ファイル: cocos.py プロジェクト: cocos2d-cpp/mini
        ret = False

    if not ret:
        print(
            MultiLanguage.get_string('COCOS_PYTHON_VERSION_TIP_FMT') %
            (major_ver, minor_ver))

    return ret


# gettext
language = None
encoding = None
try:
    locale.setlocale(locale.LC_ALL, '')  # use user's preferred locale
    language, encoding = locale.getlocale()
except:
    pass

if language is not None:
    filename = "language_%s.mo" % language[0:2]
    try:
        trans = gettext.GNUTranslations(open(filename, "rb"))
    except IOError:
        trans = gettext.NullTranslations()
    trans.install()
    _ = trans.gettext
else:
    _ = MultiLanguage.get_string

if __name__ == "__main__":
コード例 #54
0
def file_writer(filename,
                signal,
                format=None,
                separator=', ',
                encoding='latin-1'):
    loc_kwds = {}
    FORMAT = "EMSA/MAS Spectral Data File"
    if hasattr(signal.original_metadata, 'FORMAT') and \
            signal.original_metadata.FORMAT == FORMAT:
        loc_kwds = signal.original_metadata.as_dictionary()
        if format is not None:
            loc_kwds['DATATYPE'] = format
        else:
            if 'DATATYPE' in loc_kwds:
                format = loc_kwds['DATATYPE']
    else:
        if format is None:
            format = 'Y'
        if signal.metadata.has_item("General.date"):
            # Setting locale can raise an exception because
            # their name depends on library versions, platform etc.
            try:
                loc = locale.getlocale(locale.LC_TIME)
                if os_name == 'posix':
                    locale.setlocale(locale.LC_TIME, ('en_US', 'latin-1'))
                elif os_name == 'windows':
                    locale.setlocale(locale.LC_TIME, 'english')
                loc_kwds['DATE'] = signal.metadata.data.strftime("%d-%b-%Y")
                locale.setlocale(locale.LC_TIME, loc)  # restore saved locale
            except:
                warnings.warn(
                    "I couldn't write the date information due to"
                    "an unexpected error. Please report this error to "
                    "the developers")
    keys_from_signal = {
        # Required parameters
        'FORMAT': FORMAT,
        'VERSION': '1.0',
        # 'TITLE' : signal.title[:64] if hasattr(signal, "title") else '',
        'DATE': '',
        'TIME': '',
        'OWNER': '',
        'NPOINTS': signal.axes_manager._axes[0].size,
        'NCOLUMNS': 1,
        'DATATYPE': format,
        'SIGNALTYPE': signal.metadata.Signal.signal_type,
        'XPERCHAN': signal.axes_manager._axes[0].scale,
        'OFFSET': signal.axes_manager._axes[0].offset,
        # Signal1D characteristics
        'XLABEL': signal.axes_manager._axes[0].name,
        #        'YLABEL' : '',
        'XUNITS': signal.axes_manager._axes[0].units,
        #        'YUNITS' : '',
        'COMMENT': 'File created by HyperSpy version %s' % Release.version,
        # Microscope
        #        'BEAMKV' : ,
        #        'EMISSION' : ,
        #        'PROBECUR' : ,
        #        'BEAMDIAM' : ,
        #        'MAGCAM' : ,
        #        'OPERMODE' : ,
        #        'CONVANGLE' : ,
        # Specimen
        #        'THICKNESS' : ,
        #        'XTILTSTGE' : ,
        #        'YTILTSTGE' : ,
        #        'XPOSITION' : ,
        #        'YPOSITION' : ,
        #        'ZPOSITION' : ,
        #
        # EELS
        # 'INTEGTIME' : , # in ms
        # 'DWELLTIME' : , # in ms
        #        'COLLANGLE' : ,
        #        'ELSDET' :  ,
    }

    # Update the loc_kwds with the information retrieved from the signal class
    for key, value in keys_from_signal.items():
        if key not in loc_kwds or value != '':
            loc_kwds[key] = value

    for key, dic in keywords.items():

        if dic['mapped_to'] is not None:
            if 'SEM' in signal.metadata.Signal.signal_type:
                dic['mapped_to'] = dic['mapped_to'].replace('TEM', 'SEM')
            if signal.metadata.has_item(dic['mapped_to']):
                loc_kwds[key] = eval('signal.metadata.%s' % dic['mapped_to'])

    with codecs.open(filename, 'w', encoding=encoding, errors='ignore') as f:
        # Remove the following keys from loc_kwds if they are in
        # (although they shouldn't)
        for key in ['SPECTRUM', 'ENDOFDATA']:
            if key in loc_kwds:
                del (loc_kwds[key])

        f.write('#%-12s: %s\u000D\u000A' % ('FORMAT', loc_kwds.pop('FORMAT')))
        f.write('#%-12s: %s\u000D\u000A' %
                ('VERSION', loc_kwds.pop('VERSION')))
        for keyword, value in loc_kwds.items():
            f.write('#%-12s: %s\u000D\u000A' % (keyword, value))

        f.write('#%-12s: Spectral Data Starts Here\u000D\u000A' % 'SPECTRUM')

        if format == 'XY':
            for x, y in zip(signal.axes_manager._axes[0].axis, signal.data):
                f.write("%g%s%g" % (x, separator, y))
                f.write('\u000D\u000A')
        elif format == 'Y':
            for y in signal.data:
                f.write('%f%s' % (y, separator))
                f.write('\u000D\u000A')
        else:
            raise ValueError('format must be one of: None, \'XY\' or \'Y\'')

        f.write('#%-12s: End Of Data and File' % 'ENDOFDATA')
コード例 #55
0
ファイル: comps.py プロジェクト: xuchongchong/dnf
 def _dotted_locale_str():
     lcl = locale.getlocale(locale.LC_MESSAGES)
     if lcl == (None, None):
         return 'C'
     return'.'.join(lcl)
コード例 #56
0
ファイル: analytics_bot.py プロジェクト: maxgld/analytics_bot
def main():
    is_loaded_config = False
    logger = None
    logger_name = 'analytics_bot'

    config_path = 'analytics_bot.yaml'
    env_value = os.getenv('AB_CFG', None)
    if env_value:
        config_path = env_value
    if os.path.exists(config_path):
        with open(config_path, 'rt', encoding='utf8') as f:
            try:
                config = yaml.safe_load(f.read())
                logging.config.dictConfig(config['logging'])
                logger = logging.getLogger(logger_name)
                is_loaded_config = True
            except Exception as e:
                print(e)
                print('Error in Logging Configuration. Using default configs')

    if not is_loaded_config:
        print('Failed to load configuration file. Using default configs')
        logger = logging.getLogger(logger_name)
        logger.setLevel(logging.DEBUG)

        formatter = logging.Formatter(
            '%(asctime)s %(filename)-20s %(lineno)-7d %(levelname)-8s %(message)s'
        )

        console = logging.StreamHandler()
        console.setLevel(logging.DEBUG)
        console.setFormatter(formatter)

        if sys.platform.startswith('linux'):
            file_handler = logging.handlers.WatchedFileHandler(
                '/opt/www/log/analytics_bot.log')
        elif sys.platform.startswith('darwin'):
            file_handler = logging.handlers.WatchedFileHandler(
                'analytics_bot.log')
        elif sys.platform.startswith('win32'):
            file_handler = logging.handlers.WatchedFileHandler(
                'analytics_bot.log')
        else:
            file_handler = logging.handlers.WatchedFileHandler(
                'analytics_bot.log')

        file_handler.setLevel(logging.DEBUG)
        file_handler.setFormatter(formatter)

        logger.addHandler(console)
        logger.addHandler(file_handler)

    logger.info(
        '******************************************************************')
    logger.info(
        '*****                New analytic_bot started                *****')
    logger.info(
        '******************************************************************')

    db = None
    db_analytics = None
    if is_loaded_config:
        db = DBworker(
            logger,
            dbname=config['database']['catalog_ratengoods']['dbname'],
            user=config['database']['catalog_ratengoods']['user'],
            password=config['database']['catalog_ratengoods']['password'],
            host=config['database']['catalog_ratengoods']['host'],
            port=config['database']['catalog_ratengoods']['port'],
            connect_timeout=config['database']['catalog_ratengoods']
            ['connect_timeout'],
            read_only=config['database']['catalog_ratengoods']['read_only'])
        db_analytics = DBworker(
            logger,
            dbname=config['database']['analytics']['dbname'],
            user=config['database']['analytics']['user'],
            password=config['database']['analytics']['password'],
            host=config['database']['analytics']['host'],
            port=config['database']['analytics']['port'],
            connect_timeout=config['database']['analytics']['connect_timeout'],
            read_only=config['database']['analytics']['read_only'])
    else:
        db = DBworker(logger,
                      dbname='catalog',
                      user='******',
                      password='******',
                      host='XXXXXX',
                      port=5432,
                      connect_timeout=15,
                      read_only=True)
        db_analytics = DBworker(logger,
                                dbname='analytics',
                                user='******',
                                password='******',
                                host='XXXXXX',
                                port=5432,
                                connect_timeout=15,
                                read_only=False)

    # current_date at time zone 'gmt'
    start_week_date = db.execute_sql_datetime(
        "select date_trunc('week', localtimestamp - interval '7' day)")
    logger.info('start_week_date = %s', start_week_date)
    finish_week_date = db.execute_sql_datetime(
        "select date_trunc('week', localtimestamp)")
    logger.info('finish_week_date = %s', finish_week_date)
    start_3months_date = db.execute_sql_datetime(
        "select date_trunc('month', localtimestamp - interval '3' month)")
    logger.info('start_3months_date = %s', start_3months_date)
    finish_3months_date = db.execute_sql_datetime(
        "select date_trunc('month', localtimestamp)")
    logger.info('finish_3months_date = %s', finish_3months_date)

    analytics_bot_id = db_analytics.write_time_periods(start_week_date,
                                                       finish_week_date,
                                                       start_3months_date,
                                                       finish_3months_date)

    # Новых пользователей: A(A1% количество новых запусков по сравнению с прошлыми 3 месяцами)
    a = db.execute_sql_int(f"""
        select count(*) from users_user
        where date_joined >= '{start_week_date}' and date_joined < '{finish_week_date}';
        """)

    a2 = db.execute_sql_int(f"""
        select count(*) from users_user
        where date_joined >= '{start_3months_date}' and date_joined < '{finish_3months_date}'
        """)

    a1 = db.calculate('a', a, a2, start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'a', a, a2, a1)

    # Регистраций: B(B1% количество регистраций по сравнению с прошлыми 3 месяцами)
    b = db.execute_sql_int(f"""
        select count(*) from catalog_userprofile
        where date_registered >= '{start_week_date}' and date_registered < '{finish_week_date}';
        """)

    b2 = db.execute_sql_int(f"""
        select count(*) from catalog_userprofile
        where date_registered >= '{start_3months_date}' and date_registered < '{finish_3months_date}';
        """)

    b1 = db.calculate('b', b, b2, start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'b', b, b2, b1)

    # Сканирований: C(C1% количество сканов  по сравнению с прошлыми 3 месяцами. По catalog_historyitem.)
    c = db.execute_sql_int(f"""
        select count(*) from catalog_historyitem
        where timestamp >= '{start_week_date}' and timestamp < '{finish_week_date}' and scanned = true;
        """)

    c2 = db.execute_sql_int(f"""
        select count(*) from catalog_historyitem
        where timestamp >= '{start_3months_date}' and timestamp < '{finish_3months_date}' and scanned = true;
        """)

    c1 = db.calculate('c', c, c2, start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'c', c, c2, c1)

    # Сканирований: C'(C'1% количество сканов  по сравнению с прошлыми 3 месяцами без учета Web.
    # По catalog_historyitem.)
    c_minus_web = db.execute_sql_int(f"""
        select count(*) from catalog_historyitem
        where timestamp >= '{start_week_date}' and timestamp < '{finish_week_date}' and scanned = true
        and user_id <> 1200021;
        """)

    c_minus_web2 = db.execute_sql_int(f"""
        select count(*) from catalog_historyitem
        where timestamp >= '{start_3months_date}' and timestamp < '{finish_3months_date}' and scanned = true
        and user_id <> 1200021;
        """)

    c_minus_web1 = db.calculate('c_minus_web', c_minus_web, c_minus_web2,
                                start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'c_minus_web', c_minus_web,
                                 c_minus_web2, c_minus_web1)

    # Сканирований: C''(C''1% количество сканов  по сравнению с прошлыми 3 месяцами. По catalog_userevent.)
    # ProductScan = 15
    cc = db.execute_sql_int(f"""
        select count(*) from catalog_userevent
        where created >= '{start_week_date}' and created < '{finish_week_date}' and category = 15;
        """)

    cc2 = db.execute_sql_int(f"""
        select count(*) from catalog_userevent
        where created >= '{start_3months_date}' and created < '{finish_3months_date}' and category = 15;
        """)

    cc1 = db.calculate('cc', cc, cc2, start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'cc', cc, cc2, cc1)

    # Сканирований: C'''(C'''1% количество сканов  по сравнению с прошлыми 3 месяцами без учета Web.
    # По catalog_userevent.)
    # ProductScan = 15
    cc_minus_web = db.execute_sql_int(f"""
        select count(*) from catalog_userevent
        where created >= '{start_week_date}' and created < '{finish_week_date}' and category = 15
        and user_id <> 1200021;
        """)

    cc_minus_web2 = db.execute_sql_int(f"""
        select count(*) from catalog_userevent
        where created >= '{start_3months_date}' and created < '{finish_3months_date}' and category = 15
        and user_id <> 1200021;
        """)

    cc_minus_web1 = db.calculate('cc_minus_web', cc_minus_web, cc_minus_web2,
                                 start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'cc_minus_web',
                                 cc_minus_web, cc_minus_web2, cc_minus_web1)

    # Отзывов: D(D1% количество отзывов по сравнению с прошлыми 3 месяцами. По catalog_productreview.)
    d = db.execute_sql_int(f"""
        select count(*) from catalog_productreview
        where created >= '{start_week_date}' and created < '{finish_week_date}' and status = 1;
        """)

    d2 = db.execute_sql_int(f"""
        select count(*) from catalog_productreview
        where created >= '{start_3months_date}' and created < '{finish_3months_date}' and status = 1;
        """)

    d1 = db.calculate('d', d, d2, start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'd', d, d2, d1)

    # Отзывов: D'(D'1% количество отзывов по сравнению с прошлыми 3 месяцами. По catalog_userevent.)
    # ProductReviewAdd = 10
    dd = db.execute_sql_int(f"""
        select count(*) from catalog_userevent
        where created >= '{start_week_date}' and created < '{finish_week_date}' and category = 10;
        """)

    dd2 = db.execute_sql_int(f"""
        select count(*) from catalog_userevent
        where created >= '{start_3months_date}' and created < '{finish_3months_date}' and category = 10;
        """)

    dd1 = db.calculate('dd', dd, dd2, start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'dd', dd, dd2, dd1)

    # Фото: E(E1% количество одобренных фото по сравнению с прошлыми 3 месяцами. По catalog_productimage.)
    e = db.execute_sql_int(f"""
        select count(*) from catalog_productimage
        where created >='{start_week_date}' and created < '{finish_week_date}' and status = 1;
        """)

    e2 = db.execute_sql_int(f"""
        select count(*) from catalog_productimage
        where created >= '{start_3months_date}' and created < '{finish_3months_date}' and status = 1;
        """)

    e1 = db.calculate('e', e, e2, start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'e', e, e2, e1)

    # Фото: E'(E'1% количество одобренных фото по сравнению с прошлыми 3 месяцами. По catalog_userevent.)
    # ProductImageAdd = 7
    # ProductImageApprove = 8
    ee = db.execute_sql_int(f"""
        select count(*) from catalog_userevent
        where created >= '{start_week_date}' and created < '{finish_week_date}' and category in (7, 8);
        """)

    ee2 = db.execute_sql_int(f"""
        select count(*) from catalog_userevent
        where created >= '{start_3months_date}' and created < '{finish_3months_date}' and category in (7, 8);
        """)

    ee1 = db.calculate('ee', ee, ee2, start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'ee', ee, ee2, ee1)

    # Цен: F(F1% количество добавленных цен по сравнению с прошлыми 3 месяцами. По catalog_price.)
    f = db.execute_sql_int(f"""
        select count(*) from catalog_price
        where created >= '{start_week_date}' and created < '{finish_week_date}' and status = 1;
        """)

    f2 = db.execute_sql_int(f"""
        select count(*) from catalog_price
        where created >= '{start_3months_date}' and created < '{finish_3months_date}' and status = 1;
        """)

    f1 = db.calculate('f', f, f2, start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'f', f, f2, f1)

    # Цен: F'(F'1% количество добавленных цен по сравнению с прошлыми 3 месяцами. По catalog_userevent.)
    # PriceAdd = 4
    # PriceApprove = 5
    ff = db.execute_sql_int(f"""
        select count(*) from catalog_userevent
        where created >= '{start_week_date}' and created < '{finish_week_date}' and category in (4, 5);
        """)

    ff2 = db.execute_sql_int(f"""
        select count(*) from catalog_userevent
        where created >= '{start_3months_date}' and created < '{finish_3months_date}' and category in (4, 5);
        """)

    ff1 = db.calculate('ff', ff, ff2, start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'ff', ff, ff2, ff1)

    # Активных пользователей: G(G1% активные пользователи за месяц по любому действию
    #                          (сколько людей, зарегистрированных в текущем месяце, сделали хотя бы одно действие))
    # Без учета: ProfileBirthday = 17, LegacyRating = 1.
    g = db.execute_sql_int(f"""
        select count(*) from users_user
        where date_joined >= '{start_week_date}' and date_joined < '{finish_week_date}'
        and id in (select user_id from catalog_userevent
        where created >= '{start_week_date}' and created < '{finish_week_date}'
        and category in (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27));
        """)

    g2 = db.execute_sql_int(f"""
        select count(*) from users_user
        where date_joined >= '{start_3months_date}' and date_joined < '{finish_3months_date}'
        and id in (select user_id from catalog_userevent
        where created >= '{start_3months_date}' and created < '{finish_3months_date}'
        and category in (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27));
        """)

    g1 = db.calculate('g', g, g2, start_3months_date, finish_3months_date)

    db_analytics.write_parameter(analytics_bot_id, 'g', g, g2, g1)

    db.close()
    db_analytics.close()

    webhook_url = 'XXXXXX'

    one_day = datetime.timedelta(days=1)

    # Если будут проблемы с локалями, то
    # https://stackoverflow.com/questions/14547631/python-locale-error-unsupported-locale-setting
    # 'ru' and 'pl' for Windows, 'ru_RU' and 'pl_PL' for Mac OS
    loc_num = locale.getlocale(locale.LC_NUMERIC)

    if sys.platform.startswith('linux'):
        locale.setlocale(locale.LC_NUMERIC, 'pl_PL')
    elif sys.platform.startswith('darwin'):
        locale.setlocale(locale.LC_NUMERIC, 'pl_PL')
    elif sys.platform.startswith('win32'):
        locale.setlocale(locale.LC_NUMERIC, 'pl')
    else:
        locale.setlocale(locale.LC_NUMERIC, 'pl_PL')

    slack_text = """
*==============================*

*Cтатистика за прошлую неделю в сравнении с показателями предыдущих трех месяцев:*

Данные формируются за прошлую неделю и сравниваются со средним значением за прошлые 3 месяца по формуле:
_*x1*_ = (_*x*_ / _*7*_ - _*x2*_ / _*90*_) / (_*x2*_ / _*90*_) * _*100*_,
где
_*x*_ - количество пользователей/действий за предыдущую неделю,
_*x2*_ - количество пользователей/действий за предыдущие 3 месяца,
_*x1*_ - процент изменения пользователей/действий в среднем за один день предыдущей недели к среднему за один день 3-х предыдущих месяцев,
_*7*_ - количество дней в неделе,
_*90*_ - количество дней в предыдущих трёх месяцах - может быть равно 90, 91, 92 в зависимости от количества дней в месяцах.

Данные выводятся в формате:  _*x*_ (_*x1*_ %)

*==============================*

Прошлая неделя: _*{start_week_date}*_ - _*{finish_week_date}*_.
Предыдущие три месяца: _*{start_3months_date}*_ - _*{finish_3months_date}*_.

*==============================*

Новых пользователей:   _*{A}*_  (_*{A1}* %_)

Регистраций:   _*{B}*_  (_*{B1}*_ %)

Сканирований:   _*{CC_MINUS_WEB}*_  (_*{CC_MINUS_WEB1}*_ %)

Отзывов:   _*{DD}*_  (_*{DD1}*_ %)

Фотографий:   _*{EE}*_  (_*{EE1}*_ %)

Цен:   _*{F}*_  (_*{F1}*_ %)

Активных пользователей:   _*{G}*_  (_*{G1}*_ %)

*==============================*
""".format(start_week_date=start_week_date.strftime('%d.%m.%Y'),
           finish_week_date=(finish_week_date - one_day).strftime('%d.%m.%Y'),
           start_3months_date=start_3months_date.strftime('%d.%m.%Y'),
           finish_3months_date=(finish_3months_date -
                                one_day).strftime('%d.%m.%Y'),
           A=format(a, 'n'),
           A1=format(a1, 'n'),
           B=format(b, 'n'),
           B1=format(b1, 'n'),
           C=format(c, 'n'),
           C1=format(c1, 'n'),
           C_MINUS_WEB=format(c_minus_web, 'n'),
           C_MINUS_WEB1=format(c_minus_web1, 'n'),
           CC=format(cc, 'n'),
           CC1=format(cc1, 'n'),
           CC_MINUS_WEB=format(cc_minus_web, 'n'),
           CC_MINUS_WEB1=format(cc_minus_web1, 'n'),
           D=format(d, 'n'),
           D1=format(d1, 'n'),
           DD=format(dd, 'n'),
           DD1=format(dd1, 'n'),
           E=format(e, 'n'),
           E1=format(e1, 'n'),
           EE=format(ee, 'n'),
           EE1=format(ee1, 'n'),
           F=format(f, 'n'),
           F1=format(f1, 'n'),
           FF=format(ff, 'n'),
           FF1=format(ff1, 'n'),
           G=format(g, 'n'),
           G1=format(g1, 'n'))

    locale.setlocale(locale.LC_NUMERIC, loc_num)

    # """
    # Сканирований:   _*{C}*_  (_*{C1}*_ %)  _по таблице catalog_historyitem_.
    # Сканирований:   _*{C_MINUS_WEB}*_  (_*{C_MINUS_WEB1}*_ %)  _по таблице catalog_historyitem без учета сканирований через Web_.
    # Сканирований:   _*{CC}*_  (_*{CC1}*_ %)  _по таблице catalog_userevent_.
    # _*Сканирований:*_   _*{CC_MINUS_WEB}*_  (_*{CC_MINUS_WEB1}*_ %)  *_по таблице catalog_userevent без учета сканирований через Web_. Выбор редакции.*
    #
    # Отзывов:   _*{D}*_  (_*{D1}*_ %) _по таблице catalog_productreview_.
    # _*Отзывов:*_   _*{DD}*_  (_*{DD1}*_ %) *_по таблице catalog_userevent_. Выбор редакции.*
    #
    # Фотографий:   _*{E}*_  (_*{E1}*_ %) _по таблице catalog_productimage_.
    # _*Фотографий:*_   _*{EE}*_  (_*{EE1}*_ %) *_по таблице catalog_userevent_. Выбор редакции.*
    #
    # _*Цен:*_   _*{F}*_  (_*{F1}*_ %) *_по таблице catalog_price_. Выбор редакции.*
    # Цен:   _*{FF}*_  (_*{FF1}*_ %) _по таблице catalog_userevent_.
    # """

    slack_data = {
        'text': slack_text,
        'username': '******'s bot',
        'icon_url': 'XXXXXX'  #,
        #'channel': '#marketing'
    }

    response = requests.post(url=webhook_url,
                             data=json.dumps(slack_data),
                             headers={'Content-Type': 'application/json'})
    logger.info("Request is sent. Return code: %s. Return text: %s.",
                response.status_code, response.text)

    logger.info(
        '******************************************************************')
    logger.info(
        '*****                 analytic_bot finished                  *****')
    logger.info(
        '******************************************************************')
    """
       Ссылка на объект в фрейме стека при возбуждении исключения (трассировка стека, хранимая в sys.exc_info()[2]
       (3.0 sys.exc_traceback) продлевает жизнь фрейма) — исправляется путём освобождения ссылки на объект трассировки,
       когда в нём более нет надобности (3.0 sys.exc_traceback = None)
    """
    sys.exc_traceback = None
    """
       В интерактивном режиме ссылка на объект в фрейме стека, где возбуждено необработанное исключение
       (трассировка стека, хранимая в sys.last_traceback продлевает жизнь фрейма) —
       исправляется путём sys.last_traceback = None.
    """
    sys.last_traceback = None
コード例 #57
0
def main():
    """initialize objects and run the filemanager"""
    import locale
    import os.path
    import ranger
    from ranger.core.shared import (EnvironmentAware, FileManagerAware,
                                    SettingsAware)
    from ranger.core.fm import FM

    try:
        locale.setlocale(locale.LC_ALL, '')
    except:
        print("Warning: Unable to set locale.  Expect encoding problems.")

    if not 'SHELL' in os.environ:
        os.environ['SHELL'] = 'bash'

    ranger.arg = arg = parse_arguments()
    if arg.copy_config is not None:
        fm = FM()
        fm.copy_config_files(arg.copy_config)
        return 1 if arg.fail_unless_cd else 0

    SettingsAware._setup(clean=arg.clean)

    targets = arg.targets or ['.']
    target = targets[0]
    if arg.targets:
        if target.startswith('file://'):
            target = target[7:]
        if not os.access(target, os.F_OK):
            print("File or directory doesn't exist: %s" % target)
            return 1
        elif os.path.isfile(target):

            def print_function(string):
                print(string)

            from ranger.core.runner import Runner
            from ranger.fsobject import File
            runner = Runner(logfunc=print_function)
            load_apps(runner, arg.clean)
            runner(files=[File(target)], mode=arg.mode, flags=arg.flags)
            return 1 if arg.fail_unless_cd else 0

    crash_traceback = None
    try:
        # Initialize objects
        from ranger.core.environment import Environment
        fm = FM()
        FileManagerAware.fm = fm
        EnvironmentAware.env = Environment(target)
        fm.tabs = dict((n+1, os.path.abspath(path)) for n, path \
          in enumerate(targets[:9]))
        load_settings(fm, arg.clean)
        if fm.env.username == 'root':
            fm.settings.preview_files = False
            fm.settings.use_preview_script = False
        if not arg.debug:
            from ranger.ext import curses_interrupt_handler
            curses_interrupt_handler.install_interrupt_handler()

        # Run the file manager
        fm.initialize()
        fm.ui.initialize()
        fm.loop()
    except Exception:
        import traceback
        crash_traceback = traceback.format_exc()
    except SystemExit as error:
        return error.args[0]
    finally:
        if crash_traceback:
            filepath = fm.env.cf.path if fm.env.cf else "None"
        try:
            fm.ui.destroy()
        except (AttributeError, NameError):
            pass
        if crash_traceback:
            print("Ranger version: %s, executed with python %s" %
                  (ranger.__version__, sys.version.split()[0]))
            print("Locale: %s" % '.'.join(str(s) for s in locale.getlocale()))
            print("Current file: %s" % filepath)
            print(crash_traceback)
            print("Ranger crashed.  " \
             "Please report this traceback at:")
            print("http://savannah.nongnu.org/bugs/?group=ranger&func=additem")
            return 1
        return 0
コード例 #58
0
ファイル: declinator.py プロジェクト: Arusekk/declinator
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Declinator.  If not, see <http://www.gnu.org/licenses/>.

__all__ = ["findgender", "declmod"]

from . import duplidict

import collections
import locale
import os.path
import re

default_locale, _ = locale.getlocale()
if default_locale is None:
  default_locale = 'en_US'
settings_all = duplidict.FSDict(os.path.join(os.path.dirname(__file__),"rules"))
settings = settings_all[default_locale]

DETECTOR_NAME = 'detection.pcre'

detector = settings[DETECTOR_NAME]

letters = re.compile(r'[^\W\d_]+')

def findgender(word):
  '''
      Try to guess the gender of a name.
コード例 #59
0
__all__ = [
    'bot',
    'config',
    'db',
    'formatting',
    'irc',
    'loader',
    'logger',
    'module',  # deprecated in 7.1, removed in 9.0
    'plugin',
    'tools',
    'trigger',
    'version_info',
]

loc = locale.getlocale()
if not loc[1] or ('UTF-8' not in loc[1] and 'utf8' not in loc[1]):
    print('WARNING!!! You are running with a non-UTF8 locale environment '
          'variable (e.g. LC_ALL is set to "C"), which makes Python 3 do '
          'stupid things. If you get strange errors, please set it to '
          'something like "en_US.UTF-8".', file=sys.stderr)


__version__ = pkg_resources.get_distribution('sopel').version


def _version_info(version=__version__):
    regex = re.compile(r'(\d+)\.(\d+)\.(\d+)(?:[\-\.]?(a|b|rc)(\d+))?.*')
    version_groups = regex.match(version).groups()
    major, minor, micro = (int(piece) for piece in version_groups[0:3])
    level = version_groups[3]
コード例 #60
0

im_tcp_tunneler.send_xmpp_message = send_xmpp_message


def get_client_jid():
    return client.stream.my_jid.as_unicode()


im_tcp_tunneler.get_client_jid = get_client_jid

if __name__ == '__main__':
    # XMPP protocol is Unicode-based to properly display data received
    # _must_ convert it to local encoding or UnicodeException may be raised
    locale.setlocale(locale.LC_CTYPE, "")
    encoding = locale.getlocale()[1]
    if not encoding:
        encoding = "us-ascii"
    sys.stdout = codecs.getwriter(encoding)(sys.stdout, errors="replace")
    sys.stderr = codecs.getwriter(encoding)(sys.stderr, errors="replace")

    # PyXMPP uses `logging` module for its debug output
    # applications should set it up as needed
    logger = logging.getLogger()
    logger.addHandler(logging.StreamHandler())
    logger.setLevel(logging.INFO)  # change to DEBUG for higher verbosity

    if len(sys.argv) < 3:
        print u"Usage:"
        print "\t%s JID password 'tls_noverify'|cacert_file tunnelconf_file" % (
            sys.argv[0], )