def __init__(self, element_id, date_formats, start_year=None): """Create a new MultiLocaleDateTimeModelElement object. @param date_formats this parameter is a list of tuples, each tuple containing information about one date format to support. The tuple structure is (format_string, format_locale, format_timezone). The format_string may contain the same elements as supported by strptime from datetime.datetime. The format_locale defines the locale for the string content, e.g. de_DE for german, but also the data IO encoding, e.g. ISO-8859-1. The locale information has to be available, e.g. using "locale-gen" on Debian systems. The format_timezone can be used to define the timezone of the timestamp parsed. When None, UTC is used. The timezone support may only be sufficient for very simple usecases, e.g. all data from one source configured to create timestamps in that timezone. This may still fail, e.g. when daylight savings changes make timestamps ambiguous during a short window of time. In all those cases, timezone should be left empty here and a separate filtering component should be used to apply timestamp corrections afterwards. See the FIXME-Filter component for that. Also having the same format_string for two different timezones will result in an error as correct timezone to apply cannot be distinguished just from format. @param start_year when given, parsing will use this year value for semiqualified timestamps to add correct year information. This is especially relevant for historic datasets as otherwise leap year handling may fail. The startYear parameter will only take effect when the first timestamp to be parsed by this object is also semiqualified. Otherwise the year information is extracted from this record. When empty and first parsing invocation involves a semiqualified date, the current year in UTC timezone is used.""" self.element_id = element_id self.start_year = start_year # The latest parsed timestamp value. self.latest_parsed_timestamp = None self.total_seconds_start_time = datetime.datetime(1970, 1, 1) self.date_formats = DateFormatComponent(-1, None, -1, None, None) default_locale = locale.getlocale() # Build a decision tree for all format variants describing how to analyze a given timestamp. The tree is created containing # nodes of form (separator, digitsOnlyFlag, length) for format_string, format_locale, format_timezone in date_formats: self.date_formats.add_format(format_string, format_locale, format_timezone) # Restore previous locale settings. There seems to be no way in python to get back to the exact same state. Hence perform the # reset only when locale has changed. This would also change the locale from (None, None) to some system-dependent locale. if locale.getlocale() != default_locale: locale.resetlocale()
def __init__(self, movie, trailerFilters, *args): QWidget.__init__(self, *args) self.movie = movie self.filters = trailerFilters self.titlebox = QHBoxLayout() releaseDate = None if hasattr(movie, "releasedate"): locale.setlocale(locale.LC_ALL, "C") releaseDate = dparser.parse(movie.releasedate) locale.resetlocale() releaseDate = releaseDate.strftime("%x") else: releaseDate = self.tr("Unknown") titleLabel = QLabel( "<h2>%s</h2> (%s %s)" % (movie.title, self.tr("Release date:"), releaseDate), self) self.setFrameStyle(QFrame.Panel | QFrame.Sunken) self.titlebox.addWidget(titleLabel) self.titlebox.addStretch(1) middleArea = QHBoxLayout() self.posterLabel = QLabel("<img src=" "/>", self) self.posterLabel.setMinimumSize(QSize(134, 193)) self.posterLabel.setMaximumSize(QSize(134, 193)) mainArea = QVBoxLayout() self.mainArea = mainArea genre = QLabel("<b>%s</b>%s" % (self.tr("Genre(s): "), self.__get_movie_info(movie, "genre", True))) mainArea.addWidget(genre) studio = QLabel( "<b>%s</b>%s" % (self.tr("Studio: "), self.__get_movie_info(movie, "studio"))) mainArea.addWidget(studio) directors = QLabel("<b>%s</b>%s" % (self.tr("Director(s): "), self.__get_movie_info(movie, "directors"))) mainArea.addWidget(directors) actors = QLabel("<b>%s</b>%s" % (self.tr("Actors: "), self.__get_movie_info(movie, "actors", True))) mainArea.addWidget(actors) actors.setWordWrap(True) mainArea.addStretch(1) middleArea.addWidget(self.posterLabel) middleArea.addLayout(mainArea) topLevelLayout = QVBoxLayout() topLevelLayout.addLayout(self.titlebox) topLevelLayout.addLayout(middleArea) self.setMinimumSize(400, 150) self.setLayout(topLevelLayout)
def test_float_accepts_comma_for_comma_locale(): locale.setlocale(locale.LC_ALL, "de_DE") IMPORTSTRUCT = {"RFCFLOAT": "1,2"} output = client.call("STFC_STRUCTURE", IMPORTSTRUCT=IMPORTSTRUCT)["ECHOSTRUCT"] assert output["RFCFLOAT"] == 1.2 locale.resetlocale(locale.LC_ALL)
def _resetLocale(): """Reseting the process time settings""" try: resetlocale(LC_TIME) except Error: setlocale(LC_TIME, "C")
def test_expires_header_future(self): """Testing the cache with the Expires header in the future""" # We generate the future date in the C locale so that it is properly # formatted. locale.setlocale(locale.LC_TIME, str('C')) future_date = datetime.datetime.utcnow() + datetime.timedelta(days=1) future_date = future_date.strftime(APICache.EXPIRES_FORMAT) + 'UTC' locale.resetlocale(locale.LC_TIME) self.urlopener.endpoints['http://expires_future'] = { 'hit_count': 0, 'headers': { 'Expires': future_date, }, } request = Request('http://expires_future', method='GET') first_resp = self.cache.make_request(request) second_resp = self.cache.make_request(request) self.assertEqual(self.urlopener.get_hit_count('http://expires_future'), 1) self.assertFalse(isinstance(first_resp, CachedHTTPResponse)) self.assertTrue(isinstance(second_resp, CachedHTTPResponse))
def main(): """From: http://stackoverflow.com/questions/20801034/how-to-measure-download-speed-and-progress-using-requests""" # Prepare. if os.name == 'nt': locale.setlocale(locale.LC_ALL, 'english-us') else: locale.resetlocale() response = requests.get(OPTIONS['<url>'], stream=True) content_length = None if OPTIONS['--ignore-length'] else int(response.headers.get('Content-Length')) progress_bar = ProgressBarWget(content_length, eta_every=4) thread = DownloadThread(response) print_every_seconds = 0.25 # Download. thread.start() while True: progress_bar.numerator = thread.bytes_downloaded print(progress_bar, end='\r') sys.stdout.flush() # For undefined downloads (no content-length), check if thread has stopped. Loop only checks defined downloads. if not thread.isAlive(): progress_bar.force_done = True break if progress_bar.done: break time.sleep(print_every_seconds) print(progress_bar) # Always print one last time.
def func(): locale.setlocale(locale.LC_ALL, 'en_US') rule = '%B %d, %Y' format_time = datetime.datetime.strptime("October 18, 2019", rule) # format_time = datetime.datetime.strptime("1 set 2020", rule) print(format_time.strftime('%Y-%m-%d')) locale.resetlocale()
def tearDown(self): locale.resetlocale() if self.original_timezone is None: del os.environ['TZ'] else: os.environ['TZ'] = self.original_timezone time.tzset()
def set_nikola_test_locales(): try: out = subprocess.check_output(["locale", "-a"]) out = out.decode("utf-8") locales = [] languages = set() for line in out.splitlines(): if line.endswith(".utf8") and "_" in line: lang = line.split("_")[0] if lang not in languages: try: locale.setlocale(locale.LC_ALL, line) except: continue languages.add(lang) locales.append((lang, line)) if len(locales) == 2: break if len(locales) != 2: return False # task failed else: os.environ["NIKOLA_LOCALE_DEFAULT"] = ",".join(locales[0]) os.environ["NIKOLA_LOCALE_OTHER"] = ",".join(locales[1]) finally: # restore to default locale locale.resetlocale()
def test_build_notification(self): # force locale to compare 'message' value locale.setlocale(locale.LC_ALL, ('en_US', 'UTF-8')) # task_info['id'], method, params, self.session, self.options task_id = 999 fn = os.path.join(os.path.dirname(__file__), 'data/calls', 'build_notif_1', 'params.json') kwargs = koji.load_json(fn) self.session = MyClientSession('https://koji.example.com/kojihub') self.session.load_calls('build_notif_1') self.options.from_addr = "*****@*****.**" server = mock.MagicMock() self.SMTP.return_value = server # run it handler = kojid.BuildNotificationTask(task_id, 'buildNotification', koji.encode_args(**kwargs), self.session, self.options) ret = handler.run() self.assertEqual( ret, "sent notification of build 612609 to: [email protected]") # check sendmail args from_addr, recipients, message = server.sendmail.call_args[0] self.assertEqual(from_addr, "*****@*****.**") self.assertEqual(recipients, ["*****@*****.**"]) fn = os.path.join(os.path.dirname(__file__), 'data/calls', 'build_notif_1', 'message.txt') with open(fn, 'rb') as fp: msg_expect = fp.read() if six.PY2: msg_expect = msg_expect.decode() self.assertMultiLineEqual(message.decode(), msg_expect.decode()) locale.resetlocale()
def set_nikola_test_locales(): try: out = subprocess.check_output(['locale', '-a']) locales = [] languages = set() for line in out.splitlines(): if line.endswith('.utf8') and '_' in line: lang = line.split('_')[0] if lang not in languages: try: locale.setlocale(locale.LC_ALL, line) except: continue languages.add(lang) locales.append((lang, line)) if len(locales) == 2: break if len(locales) != 2: return False # task failed else: os.environ['NIKOLA_LOCALE_DEFAULT'] = ','.join(locales[0]) os.environ['NIKOLA_LOCALE_OTHER'] = ','.join(locales[1]) finally: # restore to default locale locale.resetlocale()
def save(self, type, lesson, path): class EvalPseudoSandbox(pyratemp.EvalPseudoSandbox): def __init__(self2, *args, **kwargs): pyratemp.EvalPseudoSandbox.__init__(self2, *args, **kwargs) self2.register("compose", self._compose) templatePath = self._mm.resourcePath("template.xml") t = pyratemp.Template( open(templatePath).read(), eval_class=EvalPseudoSandbox ) #set to C so strftime is English locale.setlocale(locale.LC_ALL, "C") try: dt = lesson.list["items"][0]["created"] except (IndexError, KeyError): dt = datetime.datetime.now() data = { "list": lesson.list, #+0100 since our datetime objects are naive and most WRTS #users live in the Netherlands, so this is for them at max #one hour off due to DST. "created": dt.strftime("%a, %d %b %Y %H:%M:%S +0100"), "now": datetime.datetime.now().strftime("%a, %d %b %Y %H:%M:%S +0100"), "appname": self._metadata["name"], "appversion": self._metadata["version"], } #reset locale so other modules don't get unexpected behaviour. locale.resetlocale() content = t(**data) with open(path, "w") as f: f.write(content.encode("UTF-8")) lesson.path = None
def test_l10n_sane_cookie_date(self): import locale from datetime import datetime try: locale.setlocale(locale.LC_ALL, ('nb_NO', 'UTF-8')) now = datetime(2009, 11, 8, 16, 15, 22) self._setNowTesting(now) plugin = self._makeOne('secret') environ = {'HTTP_HOST': 'example.com'} tkt = self._makeTicket(userid='chris', userdata='') result = plugin.remember(environ, {'repoze.who.userid': 'chris', 'max_age': '500'}) name, value = result.pop(0) self.assertEqual('Set-Cookie', name) self.failUnless( value.endswith('; Expires=Sun, 08 Nov 2009 16:23:42 GMT')) finally: locale.resetlocale(locale.LC_ALL)
def close(self): """ This function closes the spss data file.""" self.closeSavFile(self.fh, self.mode) try: locale.resetlocale() # fails on Windows except: locale.setlocale(locale.LC_ALL, "")
def main(): """From: http://stackoverflow.com/questions/20801034/how-to-measure-download-speed-and-progress-using-requests""" # Prepare. if os.name == 'nt': locale.setlocale(locale.LC_ALL, 'english-us') else: locale.resetlocale() response = requests.get(OPTIONS['<url>'], stream=True) content_length = None if OPTIONS['--ignore-length'] else int( response.headers.get('Content-Length')) progress_bar = ProgressBarWget(content_length, eta_every=4) thread = DownloadThread(response) print_every_seconds = 0.25 # Download. thread.start() while True: progress_bar.numerator = thread.bytes_downloaded print(progress_bar, end='\r') sys.stdout.flush() # For undefined downloads (no content-length), check if thread has stopped. Loop only checks defined downloads. if not thread.isAlive(): progress_bar.force_done = True break if progress_bar.done: break time.sleep(print_every_seconds) print(progress_bar) # Always print one last time.
def site_statistics(cls): # first check the cache stats = Cache.get_site_statistics() if stats is not None: return stats # we didn't get anything from the cache, so we need to generate and # cache a new set # prep the query and result objects q = JournalArticleQuery() stats = { "articles" : 0, "journals" : 0, "countries" : 0, "searchable" : 0 } # do the query res = cls.query(q=q.site_statistics()) # pull the journal and article facets out terms = res.get("facets", {}).get("type", {}).get("terms", []) # can't use the Python , option when formatting numbers since we # need to be compatible with Python 2.6 # otherwise we would be able to do "{0:,}".format(t.get("count", 0)) if sys.version_info[0] == 2 and sys.version_info[1] < 7: locale.setlocale(locale.LC_ALL, 'en_US') for t in terms: if t.get("term") == "journal": stats["journals"] = locale.format("%d", t.get("count", 0), grouping=True) if t.get("term") == "article": stats["articles"] = locale.format("%d", t.get("count", 0), grouping=True) # count the size of the countries facet stats["countries"] = locale.format("%d", len(res.get("facets", {}).get("countries", {}).get("terms", [])), grouping=True) # count the size of the journals facet (which tells us how many journals have articles) stats["searchable"] = locale.format("%d", len(res.get("facets", {}).get("journals", {}).get("terms", [])), grouping=True) locale.resetlocale() else: for t in terms: if t.get("term") == "journal": stats["journals"] = "{0:,}".format(t.get("count", 0)) if t.get("term") == "article": stats["articles"] = "{0:,}".format(t.get("count", 0)) # count the size of the countries facet stats["countries"] = "{0:,}".format(len(res.get("facets", {}).get("countries", {}).get("terms", []))) # count the size of the journals facet (which tells us how many journals have articles) stats["searchable"] = "{0:,}".format(len(res.get("facets", {}).get("journals", {}).get("terms", []))) # now cache and return Cache.cache_site_statistics(stats) return stats
def save(self, type, lesson, path): class EvalPseudoSandbox(pyratemp.EvalPseudoSandbox): def __init__(self2, *args, **kwargs): pyratemp.EvalPseudoSandbox.__init__(self2, *args, **kwargs) self2.register("compose", self._compose) templatePath = self._mm.resourcePath("template.xml") t = pyratemp.Template(open(templatePath).read(), eval_class=EvalPseudoSandbox) #set to C so strftime is English locale.setlocale(locale.LC_ALL, "C") try: dt = lesson.list["items"][0]["created"] except (IndexError, KeyError): dt = datetime.datetime.now() data = { "list": lesson.list, #+0100 since our datetime objects are naive and most WRTS #users live in the Netherlands, so this is for them at max #one hour off due to DST. "created": dt.strftime("%a, %d %b %Y %H:%M:%S +0100"), "now": datetime.datetime.now().strftime("%a, %d %b %Y %H:%M:%S +0100"), "appname": self._metadata["name"], "appversion": self._metadata["version"], } #reset locale so other modules don't get unexpected behaviour. locale.resetlocale() content = t(**data) with open(path, "w") as f: f.write(content.encode("UTF-8")) lesson.path = None
def main(): """From: http://stackoverflow.com/questions/20801034/how-to-measure-download-speed-and-progress-using-requests""" # Prepare. if os.name == 'nt': locale.setlocale(locale.LC_ALL, 'english-us') else: locale.resetlocale() if len(sys.argv) < 2: error('Path to manifest is missing!') manifest = sys.argv[1] os.chdir(os.path.dirname(manifest)) with open(os.path.basename(manifest), 'r') as hdl: meta = json.load(hdl) chk_file = os.path.basename(manifest) + '.chk' last_chk = None if os.path.isfile(chk_file): with open(chk_file, 'r') as stream: last_chk = stream.read().strip() if last_chk == '#'.join(meta['checksum']): return print('%s has changed or has not been downloaded, yet. Downloading...' % manifest) response = requests.get(meta['url'], stream=True) content_length = None if meta.get('ignore_length', False) else int(response.headers.get('Content-Length')) progress_bar = ProgressBarWget(content_length, eta_every=4) thread = DownloadThread(response, meta['checksum']) print_every_seconds = 0.25 # Download. thread.start() while True: progress_bar.numerator = thread.bytes_downloaded print(progress_bar, end='\r') sys.stdout.flush() # For undefined downloads (no content-length), check if thread has stopped. Loop only checks defined downloads. if not thread.isAlive(): progress_bar.force_done = True break if progress_bar.done: break time.sleep(print_every_seconds) print(progress_bar) # Always print one last time. if thread.failed: error('The download failed because the download was incomplete or corrupted!') thread.join() with open(chk_file, 'w') as stream: stream.write('#'.join(meta['checksum']))
def main(): """From: http://stackoverflow.com/questions/20801034/how-to-measure-download-speed-and-progress-using-requests""" # Prepare. if os.name == 'nt': locale.setlocale(locale.LC_ALL, 'english-us') else: locale.resetlocale() if len(sys.argv) < 2: error('Path to manifest is missing!') manifest = sys.argv[1] os.chdir(os.path.dirname(manifest)) with open(os.path.basename(manifest), 'r') as hdl: meta = json.load(hdl) chk_file = os.path.basename(manifest) + '.chk' last_chk = None if os.path.isfile(chk_file): with open(chk_file, 'r') as stream: last_chk = stream.read().strip() if last_chk == '#'.join(meta['checksum']): return print('%s has changed or has not been downloaded, yet. Downloading...' % manifest) response = requests.get(meta['url'], stream=True) content_length = None if meta.get('ignore_length', False) else int(response.headers.get('Content-Length', 0)) progress_bar = ProgressBarWget(content_length, eta_every=4) thread = DownloadThread(response, meta['checksum']) print_every_seconds = 0.25 # Download. thread.start() while True: progress_bar.numerator = thread.bytes_downloaded print(progress_bar, end='\r') sys.stdout.flush() # For undefined downloads (no content-length), check if thread has stopped. Loop only checks defined downloads. if not thread.isAlive(): progress_bar.force_done = True break if progress_bar.done: break time.sleep(print_every_seconds) print(progress_bar) # Always print one last time. if thread.failed: error('The download failed because the download was incomplete or corrupted!') thread.join() with open(chk_file, 'w') as stream: stream.write('#'.join(meta['checksum']))
def parse(lines): """Parse Firebird server log and yield named tuples describing individual log entries/events. Args: lines: Iterable of lines from Firebird server log. Raises: fdb.ParseError: When any problem is found in input stream. """ line_no = 0 locale = getlocale() # (LC_ALL) if sys.platform == 'win32': setlocale(LC_ALL, 'English_United States') else: setlocale(LC_ALL, 'en_US') try: clean = (line.strip() for line in lines) entry_lines = [] timestamp = None source_id = 'UNKNOWN' for line in clean: line_no += 1 if line == '': continue items = line.split() if len(items) > 5: # It's potentially new entry try: new_timestamp = datetime.strptime( ' '.join(items[len(items) - 5:]), '%a %b %d %H:%M:%S %Y') except ValueError: new_timestamp = None if new_timestamp is not None: if entry_lines: yield LogEntry(source_id=source_id, timestamp=timestamp, message='\n'.join(entry_lines)) entry_lines = [] # Init new entry timestamp = new_timestamp source_id = ' '.join(items[:len(items) - 5]) else: entry_lines.append(line) else: entry_lines.append(line) if entry_lines: yield LogEntry(source_id=source_id, timestamp=timestamp, message='\n'.join(entry_lines)) except Exception as e: raise ParseError("Can't parse line %d\n%s" % (line_no, e.message)) finally: if locale[0] is None: if sys.platform == 'win32': setlocale(LC_ALL, '') else: resetlocale(LC_ALL) else: setlocale(LC_ALL, locale)
def close(self): """This function closes the spss data file and does some cleaning.""" if not segfaults: self.closeSavFile(self.fh, mode=b"rb") try: locale.resetlocale() # fails on Windows except: locale.setlocale(locale.LC_ALL, "")
def test_date_formatter(self): date_last_update = crs_to_date( 'Aggiornato a\xa0mercoledì 03 agosto 2016\xa0alle\xa012:50') locale.setlocale(locale.LC_TIME, "it_IT.UTF-8") date_expected = pytz.timezone('Europe/Rome').localize( datetime.strptime('03 agosto 2016 12:50', '%d %B %Y %H:%M')) locale.resetlocale(locale.LC_TIME) self.assertEqual(date_last_update, date_expected)
def __init__(self): """ init the buttonbar """ plugin.DaemonPlugin.__init__(self) plugin.register(self, 'buttonbar') self.visible = True self.bar = None self.barfile = '' self.surface = None self.colors = ['red', 'green', 'yellow', 'blue'] self.actions = [None, None, None, None] if not hasattr(config, 'BUTTONBAR_TVGUIDE_ACTIONS'): self.tvguide_actions = [ MenuItem('-1 Day', action=self.advance_tv_guide, arg=-24), MenuItem('-6 Hours', action=self.advance_tv_guide, arg=-6), MenuItem('+6 Hours', action=self.advance_tv_guide, arg=6), MenuItem('+1 Day', action=self.advance_tv_guide, arg=24) ] else: # Process TV Guide buttons self.tvguide_actions = [None, None, None, None] for index in range(0, len(self.colors)): if config.BUTTONBAR_TVGUIDE_ACTIONS.has_key( self.colors[index]): actionstr = config.BUTTONBAR_TVGUIDE_ACTIONS[ self.colors[index]] if actionstr == 'record': self.tvguide_actions[index] = MenuItem( _('Record'), action=self.send_event_to_menu, arg=event.TV_START_RECORDING) elif actionstr == 'info': self.tvguide_actions[index] = MenuItem( _('Full Description'), action=self.show_program_info) elif actionstr == 'now': self.tvguide_actions[index] = MenuItem( _('Now'), action=self.jump_to_now) elif actionstr.startswith('adv:'): hours = eval(actionstr[4:]) self.tvguide_actions[index] = MenuItem( 'Advance %d hours' % hours, action=self.advance_tv_guide, arg=hours) else: msgtext = _('WARNING: ') msgtext += _( '"%s" is not a valid argument for the button bar. ' ) % actionstr _debug_(msgtext, DERROR) # Getting current LOCALE try: locale.resetlocale() except: pass
def get_time_from_locale_format(time_str: str, time_format, time_locale): try: locale.setlocale(locale.LC_ALL, time_locale) format_time = datetime.datetime.strptime(time_str, time_format) locale.resetlocale() return format_time except: return None
def open_data(): client = pymongo.MongoClient(os.environ['MONGO_CASES_URI']) db = client.va_circuit_court_cases locale.resetlocale() case_number_count = locale.format("%d", db.case_numbers.count(), grouping=True) data = {'case_number_count': case_number_count} return render_template('open_data.html', data=data)
def crs_to_date(date): my_tz = pytz.timezone('Europe/Rome') locale.setlocale(locale.LC_TIME, "it_IT.UTF-8") clean1 = date.replace('Aggiornato a\xa0', '').replace('\xa0alle\xa0', ' ').split(' ') clean2 = ' '.join(clean1[1:]) date = my_tz.localize(datetime.strptime(clean2, "%d %B %Y %H:%M")) locale.resetlocale(locale.LC_TIME) return date
def open_data(): client = pymongo.MongoClient(os.environ['MONGO_CASES_URI']) db = client.va_circuit_court_cases locale.resetlocale() case_number_count = locale.format("%d", db.case_numbers.count(), grouping=True) data = { 'case_number_count': case_number_count } return render_template('open_data.html', data=data)
def load(self, path): with open(path) as f: root = ElementTree.parse(f).getroot() #dutch: lijst = list listTree = root.find("lijst") wordList = { "items": list(), } #dutch: titel = title wordList["title"] = listTree.findtext("titel") or u"" #dutch: taal = language wordList["questionLanguage"] = listTree.findtext("taal/a") or u"" wordList["answerLanguage"] = listTree.findtext("taal/b") or u"" #change locale temporary; so strptime can do it's work the way #it should. locale.setlocale(locale.LC_ALL, "C") try: created = datetime.datetime.strptime( listTree.findtext("created").rsplit(" ", 1)[0], #strip tz info "%a, %d %b %Y %H:%M:%S" #since our datetime objects are naive ) except (ValueError, AttributeError): created = None #set locale back to make sure conflicts don't arise with other #modules depending on the locale. locale.resetlocale() #counter is used as word id counter = 1 #dutch: woord = word for wordTree in listTree.findall("woord"): word = { "id": int(), "questions": list(), "answers": list(), "comment": unicode() } word["id"] = counter if created: word["created"] = created wsp = self._modules.default("active", type="wordsStringParser") word["questions"] = wsp.parse(wordTree.findtext("a") or u"") word["answers"] = wsp.parse(wordTree.findtext("b") or u"") wordList["items"].append(word) counter += 1 return { "resources": {}, "list": wordList, }
def debug_show_image(title, image, config): image_hide_key = re.sub('[^0-9a-z_]+', '_', 'hide_image_' + title.lower()) if image_hide_key[-1] == '_': image_hide_key = image_hide_key[:-1] if config.get('general', 'debug', 'boolean') and \ not config.get('debug', 'hide_images', 'boolean'): if not config.get('debug', image_hide_key, 'boolean', allow_missing=True): cv2.imshow(title, image) locale.resetlocale()
def __init__(self, movie, trailerFilters, *args): QWidget.__init__(self, *args) self.movie = movie self.filters = trailerFilters self.titlebox = QHBoxLayout() releaseDate=None if hasattr(movie, "releasedate"): locale.setlocale(locale.LC_ALL, "C") releaseDate = dparser.parse(movie.releasedate) locale.resetlocale() releaseDate = releaseDate.strftime("%x") else: releaseDate = self.tr("Unknown") titleLabel = QLabel("<h2>%s</h2> (%s %s)" % (movie.title, self.tr("Release date:"), releaseDate), self) self.setFrameStyle(QFrame.Panel | QFrame.Sunken); self.titlebox.addWidget(titleLabel) self.titlebox.addStretch(1) middleArea = QHBoxLayout() self.posterLabel = QLabel("<img src=""/>", self) self.posterLabel.setMinimumSize(QSize(134,193)) self.posterLabel.setMaximumSize(QSize(134,193)) mainArea = QVBoxLayout() self.mainArea = mainArea genre = QLabel("<b>%s</b>%s" % (self.tr("Genre(s): "), self.__get_movie_info(movie, "genre", True))) mainArea.addWidget(genre) studio = QLabel("<b>%s</b>%s" % (self.tr("Studio: "), self.__get_movie_info(movie, "studio"))) mainArea.addWidget(studio) directors = QLabel("<b>%s</b>%s" % (self.tr("Director(s): "), self.__get_movie_info(movie, "directors"))) mainArea.addWidget(directors) actors = QLabel("<b>%s</b>%s" % (self.tr("Actors: "), self.__get_movie_info(movie, "actors", True))) mainArea.addWidget(actors) actors.setWordWrap(True) mainArea.addStretch(1) middleArea.addWidget(self.posterLabel) middleArea.addLayout(mainArea) topLevelLayout = QVBoxLayout() topLevelLayout.addLayout(self.titlebox) topLevelLayout.addLayout(middleArea) self.setMinimumSize(400,150) self.setLayout(topLevelLayout)
def __init__(self, row, lang): # parse value as float according to locale value = row.pop('INDHOLD') locale.setlocale(locale.LC_NUMERIC, config.LOCALES[lang]) # empty strings are handled as nulled values self.value = None if not value else locale.atof(value) locale.resetlocale() # parse time as timestamp timestring = row.pop('TID').split(' ', 1)[0] self.time = time.parse(timestring) self.variables = {k.lower(): Value(dict(zip(['id', 'text'], v.split(' ', 1)))) for k, v in row.items()}
def main(argv): dump_only = False if len(argv) > 1: if argv.pop() == '--dump': dump_only = True else: print('Error: Unrecognized option.', file=sys.stderr) sys.exit(1) wik = pwb.Site(code='bg', fam='wikipedia') params = { 'action': 'query', 'format': 'json', 'list': 'abusefilters', 'formatversion': '2', 'abfstartid': '12', 'abfendid': '12', 'abfprop': 'pattern', } pattern = pwb.data.api.Request( site=wik, parameters=params).submit()['query']['abusefilters'][0]['pattern'] site_list = [ _[5:][:-4].replace('\\.', '.') for _ in pattern.splitlines() if _[2:5] == "'\\b" ] site_list.sort() if dump_only: for site in site_list: print('* {}'.format(site)) else: list_page_name = 'Уикипедия:Патрульори/СФИН' list_page = pwb.Page(wik, list_page_name) lnum_page = pwb.Page(wik, list_page_name + '/N') lupd_page = pwb.Page(wik, list_page_name + '/U') list_page.text = '{{' + list_page_name + '/H}}\n' site_index = '' for site in site_list: if site[0] != site_index: list_page.text += '\n<h3> {} </h3>\n'.format( site[0].capitalize()) site_index = site[0] list_page.text += '* {}\n'.format(site) list_page.text += '\n{{' + list_page_name + '/F}}' lnum_page.text = str(len(site_list)) locale.setlocale(locale.LC_TIME, 'bg_BG.UTF-8') lupd_page.text = dt.now().strftime('%H:%M на %e %B %Y').lower() locale.resetlocale(locale.LC_TIME) list_page.save(summary='Бот: актуализация', quiet=True) lnum_page.save(summary='Бот: актуализация', quiet=True) lupd_page.save(summary='Бот: актуализация', quiet=True)
def get_latest_trailer_date(self): """Returns date (unix timestamp) of latest trailer for this movie """ tsMax = 0 for trailer in self.trailers: locale.setlocale(locale.LC_ALL, "C") pdate = dparser.parse(trailer['postdate']) locale.resetlocale() ts = mktime(pdate.timetuple()) if ts > tsMax: tsMax = ts return tsMax
def runTest(self): unicode_str1 = u"ááááááááá" unicode_str2 = u"áááááá" # pylint: disable=environment-modify # Make sure the locale is reset so that the traceback could happen del os.environ["LANG"] locale.resetlocale() # parser should parse string including non-ascii characters self.parser.readKickstartFromString(self.ks) # str(handler) should not cause traceback and should contain the # original non-ascii strings as utf-8 encoded byte strings and # str(self.handler) should not fail -- i.e. self.handler.__str__() # should return byte string not unicode string self.assertIn(self.get_encoded_str(unicode_str1), str(self.handler)) self.assertIn(self.get_encoded_str(unicode_str2), str(self.handler)) # set root password to unicode string self.handler.rootpw.password = unicode_str1 # str(handler) should not cause traceback and should contain the # original unicode string as utf-8 encoded byte string self.assertIn(self.get_encoded_str(unicode_str1), str(self.handler)) # set root password to encoded string self.handler.rootpw.password = self.get_encoded_str(unicode_str1, force_encode=True) # str(handler) should not cause traceback and should contain the # original unicode string as utf-8 encoded byte string self.assertIn(str(self.get_encoded_str(unicode_str1, force_encode=True)), str(self.handler)) (fd, name) = tempfile.mkstemp(prefix="ks-", suffix=".cfg", dir="/tmp", text=True) if six.PY3: buf = self.ks.encode("utf-8") else: buf = self.ks os.write(fd, buf) os.close(fd) # This should not traceback with a UnicodeError try: self.parser.readKickstart(name) # str(handler) should not cause traceback and should contain the # original non-ascii strings as utf-8 encoded byte strings and # str(self.handler) should not fail -- i.e. self.handler.__str__() # should return byte string not unicode string self.assertIn(self.get_encoded_str(unicode_str1), str(self.handler)) self.assertIn(self.get_encoded_str(unicode_str2), str(self.handler)) finally: os.unlink(name)
def _parseDate(self, date): #change locale temporary; so strptime can do it's work the way #it should. locale.setlocale(locale.LC_ALL, "C") #do conversion dt = datetime.datetime.strptime(date, "%d-%b-%Y %H:%M:%S") #set locale back to make sure conflicts don't arise with other #modules depending on the locale. locale.resetlocale() return dt
def set_locale(): if os.name == 'nt': locale.setlocale(locale.LC_ALL, 'english-us') return for l in ('en_US.utf8', 'english-us', 'english_us', 'en_us'): try: locale.setlocale(locale.LC_ALL, l) except locale.Error: continue return locale.resetlocale()
def changing_locale(loc): """ Temporarily change locale.LC_ALL. """ try: try: locale.setlocale(locale.LC_ALL, loc) except Exception: raise RuntimeError(f'error while setting locale to: {loc}') yield finally: # Change back. locale.resetlocale()
def init(): # initialize locale - for number formatting resetlocale() # find config files near script location, build WORKSPACES from them rel = partial(join, sys.path[0]) for fn in glob(rel("*.conf")) + glob(rel("../*.conf")) + glob( rel("conf/*")) + glob(rel("../conf/*")): if not fn.lower().endswith(".template"): try: cfg = get_config(fn, False) except Exception: continue WORKSPACES.update({k: v[0] for k, v in cfg['workspace'].items()})
def reader_status(status_fd, update): # try to get nicer number formating try: locale.resetlocale(locale.LC_NUMERIC) except Exception: pass count = 0 while True: update('{0:n} lines read'.format(count)) data = os.read(status_fd, 8) if not data: break count = struct.unpack("=Q", data)[0]
def en_locale(): """ A context maanger that temporarily sets the LC_TIME locale to en_US.UTF-8 and then resets it. """ try: locale.setlocale(locale.LC_TIME, "en_US.UTF-8") except locale.Error: yield else: try: yield finally: locale.resetlocale(locale.LC_TIME)
def test_float_rejects_point_for_comma_locale(): locale.setlocale(locale.LC_ALL, "de_DE") IMPORTSTRUCT = {"RFCFLOAT": "1.2"} try: output = client.call("STFC_STRUCTURE", IMPORTSTRUCT=IMPORTSTRUCT) except Exception as ex: assert type(ex) is ExternalRuntimeError assert ex.code == 22 assert ex.key == "RFC_CONVERSION_FAILURE" assert ( ex.message == "Cannot convert string value 1.2 at position 1 for the field RFCFLOAT to type RFCTYPE_FLOAT" ) locale.resetlocale(locale.LC_ALL)
def test_at_loc_boundary(self): try: self.void_locale() opcodes1 = self.encode_literal("bla") \ + [OPCODES["at"], ATCODES["at_loc_boundary"], OPCODES["success"]] opcodes2 = self.encode_literal("bla") \ + [OPCODES["at"], ATCODES["at_loc_non_boundary"], OPCODES["success"]] self.assertMatch(opcodes1, "bla\xFC") self.assertNoMatch(opcodes2, "bla\xFC") locale.setlocale(locale.LC_ALL, "de_DE") self.assertNoMatch(opcodes1, "bla\xFC") self.assertMatch(opcodes2, "bla\xFC") locale.resetlocale() # is this the right way to rest the locale? except locale.Error: # skip test pass
def main(): if os.name == 'nt': locale.setlocale(locale.LC_ALL, 'english-us') else: locale.resetlocale() if OPTIONS['progress_bar']: progress_bar() elif OPTIONS['progress_bar_bits']: progress_bar_bits() elif OPTIONS['progress_bar_bytes']: progress_bar_bytes() elif OPTIONS['progress_bar_wget']: progress_bar_wget() elif OPTIONS['progress_bar_yum']: progress_bar_yum() else: raise RuntimeError
def test_at_loc_boundary(self): import locale try: s.void_locale() opcodes1 = s.encode_literal("bla") \ + [s.OPCODES["at"], s.ATCODES["at_loc_boundary"], s.OPCODES["success"]] opcodes2 = s.encode_literal("bla") \ + [s.OPCODES["at"], s.ATCODES["at_loc_non_boundary"], s.OPCODES["success"]] s.assert_match(opcodes1, "bla\xFC") s.assert_no_match(opcodes2, "bla\xFC") locale.setlocale(locale.LC_ALL, "de_DE") s.assert_no_match(opcodes1, "bla\xFC") s.assert_match(opcodes2, "bla\xFC") locale.resetlocale() # is this the right way to rest the locale? except locale.Error: # skip test skip("locale error")
def main(): Windows.enable() # Does nothing if not on Windows. # Prepare. if os.name == 'nt': locale.setlocale(locale.LC_ALL, 'english-us') else: locale.resetlocale() progress_bar = ProgressBar(5 if OPTIONS['--fast'] else 100) progress_bar.bar.CHAR_FULL = Color('{autoyellow}#{/autoyellow}') progress_bar.bar.CHAR_LEADING = Color('{autoyellow}#{/autoyellow}') progress_bar.bar.CHAR_LEFT_BORDER = Color('{autoblue}[{/autoblue}') progress_bar.bar.CHAR_RIGHT_BORDER = Color('{autoblue}]{/autoblue}') # Run. for i in range(6 if OPTIONS['--fast'] else 101): progress_bar.numerator = i print(progress_bar, end='\r') sys.stdout.flush() time.sleep(0.25) print(progress_bar) # Always print one last time.
def get_revision_metadata(changeset, metadata_property_map=None, repository_uri=None, encoding="utf-8"): """ Return dictionary of metadatas defined in metadata_property_map. Uses slow solution (git log query per property) to avoid "delimiter inside result" problem. """ # it looks like git is displaying time in en_US locale even if, i.e. cs_CZ # is set, which is bad when using with %a or %b...so hack it setlocale(LC_ALL, USED_GIT_PARSING_LOCALE) metadata = {"repository_uri": repository_uri or get_repository_uri()} metadata_property_map = metadata_property_map or { "%h": {"name": "hash_abbrev"}, "%H": {"name": "hash"}, "%aN": {"name": "author_name"}, "%ae": {"name": "author_email"}, "%ad": {"name": "author_date", "filter": filter_parse_date}, "%cN": {"name": "commiter_name"}, "%ce": {"name": "commiter_email"}, "%cd": {"name": "commiter_date", "filter": filter_parse_date}, "%s": {"name": "subject"}, } for property in metadata_property_map: if "filter" in metadata_property_map[property]: filter = metadata_property_map[property]["filter"] else: filter = None try: metadata[metadata_property_map[property]["name"]] = get_revision_metadata_property( changeset, property, filter ) except (CalledProcessError, ValueError): metadata[metadata_property_map[property]["name"]] = "[failed to retrieve]" log.error("Error when parsing metadata: %s" % traceback.format_exc()) resetlocale() return metadata
def test_locale(): orig_locale = locale.getlocale(locale.LC_NUMERIC)[0] try: locale.setlocale(locale.LC_NUMERIC, 'fr_FR') except locale.Error: pytest.xfail( "Can't set to 'fr_FR' locale, perhaps because it is not installed " "on this system") try: header = get_pkg_data_contents('data/locale.hdr', encoding='binary') w = _wcs.Wcsprm(header) assert re.search("[0-9]+,[0-9]*", w.to_header()) is None finally: if orig_locale is None: # reset to the default setting locale.resetlocale(locale.LC_NUMERIC) else: # restore to whatever the previous value had been set to for # whatever reason locale.setlocale(locale.LC_NUMERIC, orig_locale)
def __init__(self): """ initialise an instance of the PluginInterface """ logger.log( 9, 'PluginInterface.__init__()') plugin.DaemonPlugin.__init__(self) self.poll_interval = 30 self.poll_menu_only = False self.plugins = None plugin.register(self, 'idlebar') self.visible = True self.bar = None self.rect = None self.barfile = '' self.free_space = -1 self.used_space = 0 # Getting current LOCALE try: locale.resetlocale() except: pass
def filter_parse_date(stdout, used_locale=None): """ Construct a datetime object from local date string returned by git show """ # originally, we just passed this to strptime, but it turned out it's not exactly # working for some cases, such as single-digit days setlocale(LC_ALL, used_locale or USED_GIT_PARSING_LOCALE) # use regexp to sniff what we want, skipping things like TZ info match = re.match( r"^(?P<dow>\w+)\ {1}(?P<month>\w+)\ {1}(?P<day>\d{1,2})\ {1}(?P<hour>\d{1,2})\:{1}(?P<minute>\d{1,2})\:{1}(?P<second>\d{1,2})\ {1}(?P<year>\d+).*$", stdout, re.UNICODE, ) if not match: raise ValueError("Date '%s' is not matching our format, please report bug" % str(stdout)) data = match.groupdict() # to be able to provide zero-padded values, non-string values must be integers data.update(dict([(key, int(value)) for key, value in data.items() if key not in ["dow", "month"]])) date = datetime.strptime( "%(dow)s %(month)s %(day)02d %(hour)02d:%(minute)02d:%(second)02d %(year)04d" % data, "%a %b %d %H:%M:%S %Y" ) resetlocale() return date
def posix_locale(): locale.setlocale(locale.LC_ALL, "POSIX") yield locale.resetlocale(locale.LC_ALL)
from argparse import ArgumentParser import argparse #from contexo import ctx_rspec from contexo import ctx_view from contexo import ctx_cfg #from contexo import ctx_cmod from contexo.ctx_envswitch import assureList, EnvironmentLayout, switchEnvironment from contexo import ctx_common from contexo.ctx_common import setInfoMessageVerboseLevel, infoMessage, userErrorExit, warningMessage, ctxAssert from contexo.ctx_comp import ctx_log, COMPFile from contexo import ctx_sysinfo import locale try: #this fails on windows, but it doesn't matter much locale.resetlocale() # locale.LC_ALL, 'en_US.UTF-8') except: pass msgSender = 'ctx.py' #logging.basicConfig(format = '%(asctime)s %(levelname)-8s %(filename)s %(lineno)s %(message)s', logging.basicConfig(format = '%(asctime)s %(levelname)-8s %(message)s', datefmt='%H:%M:%S', level = logging.DEBUG); #logger = logging.getLogegr() #logger. #logging.debug('Starting...') # # Get configuration.
def tearDownModule(): locale.resetlocale()
# go to dir # watch dir # get a dir list # run for each file a sniper # react on dir changes # IN_CLOSE_WRITE: restart # IN_MOVED_FROM: stop # IN_MOVED_TO: restart # IN_DELETE: stop import sys, os, signal, re, subprocess, locale, argparse # for output/log file encoding (does not work as _we_ don't write in that file) # maybe for filefilter re checks (but I think just plain "UNICODE" is better) # to convert file names to unicode objects I don't need to set the locale, but anyway locale.resetlocale() # same as locale.setlocale(locale.LC_ALL, '') or not? encoding = locale.getpreferredencoding(False) # here we get _always_ a good guess def debug(msg): print >> sys.stderr, msg def unicod(str): return unicode(str, encoding, 'replace') def filefilter(name, bad = re.compile(ur"\W", re.UNICODE)): return not bad.search(unicod(name)) class Snipers(object): def __init__(self): self.proc = {}
def __init__(self): """ init the buttonbar """ plugin.DaemonPlugin.__init__(self) plugin.register(self, 'buttonbar') self.visible = True self.bar = None self.barfile = '' self.surface = None self.colors = config.BUTTONBAR_ORDER self.events = [] for index in self.colors: eventname = 'BUTTONBAR_' + index.upper() self.events.append(eventname) self.actions = [None, None, None, None] if not hasattr(config, 'BUTTONBAR_TVGUIDE_ACTIONS'): self.tvguide_actions = [MenuItem('-1 Day', action= self.advance_tv_guide, arg= -24), MenuItem('-6 Hours', action= self.advance_tv_guide, arg= -6), MenuItem('+6 Hours', action= self.advance_tv_guide, arg= 6), MenuItem('+1 Day', action= self.advance_tv_guide, arg= 24)] else: # Process TV Guide buttons self.tvguide_actions = [None, None, None, None] for index in range(0, len(self.colors)): actionstr = '' if config.BUTTONBAR_TVGUIDE_ACTIONS.has_key('button' + str(index+1)): actionstr = config.BUTTONBAR_TVGUIDE_ACTIONS['button' + str(index+1)] if config.BUTTONBAR_TVGUIDE_ACTIONS.has_key(self.colors[index]): actionstr = config.BUTTONBAR_TVGUIDE_ACTIONS[self.colors[index]] if actionstr == 'record': self.tvguide_actions[index] = MenuItem(_('Record'), action=self.send_event_to_menu, arg= event.TV_START_RECORDING) elif actionstr == 'enter': self.tvguide_actions[index] = MenuItem(_('More options'), action=self.show_options, arg=None) #action=self.send_event_to_menu, #arg= event.MENU_SUBMENU) elif actionstr == 'info': self.tvguide_actions[index] = MenuItem(_('Full Description'), action=self.show_program_info) elif actionstr == 'now': self.tvguide_actions[index] = MenuItem(_('Now'), action=self.jump_to_now) elif actionstr.startswith('adv:'): hours = eval(actionstr[4:]) self.tvguide_actions[index] = MenuItem('Advance %d hours' % hours, action= self.advance_tv_guide, arg= hours) else: msgtext = _('WARNING: ') msgtext+= _('"%s" is not a valid argument for the button bar. ') % actionstr logger.error(msgtext) # Getting current LOCALE try: locale.resetlocale() except: pass
def tearDownModule(): if sys.platform == 'win32': locale.setlocale(locale.LC_ALL, "") else: locale.resetlocale()