def findT(path, language='en-us'): """ must be run by the admin app """ filename = os.path.join(path, 'languages', '%s.py' % language) sentences = read_dict(filename) mp = os.path.join(path, 'models') cp = os.path.join(path, 'controllers') vp = os.path.join(path, 'views') mop = os.path.join(path, 'modules') for file in listdir(mp, '.+\.py', 0) + listdir(cp, '.+\.py', 0)\ + listdir(vp, '.+\.html', 0) + listdir(mop, '.+\.py', 0): fp = portalocker.LockedFile(file, 'r') data = fp.read() fp.close() items = regex_translate.findall(data) for item in items: try: message = eval(item) if not message.startswith('#') and not '\n' in message: tokens = message.rsplit('##', 1) else: # this allows markmin syntax in translations tokens = [message] if len(tokens) == 2: message = tokens[0].strip() + '##' + tokens[1].strip() if message and not message in sentences: sentences[message] = message except: pass write_dict(filename, sentences)
def save_storage(storage, filename): fp = None try: fp = portalocker.LockedFile(filename, 'wb') cPickle.dump(dict(storage), fp) finally: if fp: fp.close()
def read_plural_rules_aux(filename): """retrieve plural rules from rules/*plural_rules-lang*.py file. args: filename (str): plural_rules filename returns: tuple(nplurals, get_plural_id, construct_plural_form) e.g.: (3, <function>, <function>) """ f = portalocker.LockedFile(filename, 'r') plural_py = f.read().replace('\r\n', '\n') f.close() try: exec(plural_py) nplurals = locals().get('nplurals', default_nplurals) get_plural_id = locals().get('get_plural_id', default_get_plural_id) construct_plural_form = locals().get('construct_plural_form', default_construct_plural_form) status = 'ok' except Exception, e: nplurals = default_nplurals get_plural_id = default_get_plural_id construct_plural_form = default_construct_plural_form status = 'Syntax error in %s (%s)' % (filename, e) logging.error(status)
def get_lang_info(lang, langdir): """retrieve lang information from *langdir*/*lang*.py file. Read few strings from lang.py file until keys !langname!, !langcode! or keys greater then '!*' were found args: lang (str): lang-code or 'default' langdir (str): path to 'languages' directory in web2py app dir returns: tuple(langcode, langname, langfile_mtime) e.g.: ('en', 'English', 1338549043.0) """ filename = ospath.join(langdir, lang + '.py') langcode = langname = '' f = portalocker.LockedFile(filename, 'r') try: while not (langcode and langname): line = f.readline() if not line: break match = regex_langinfo.match(line) if match: k = match.group(1) if k == '!langname!': langname = match.group(2) elif k == '!langcode!': langcode = match.group(2) elif k[0:1] > '!': break finally: f.close() if not langcode: langcode = lang if lang != 'default' else 'en' return langcode, langname or langcode, ostat(filename).st_mtime
def load_storage(filename): fp = None try: fp = portalocker.LockedFile(filename, 'rb') storage = cPickle.load(fp) finally: if fp: fp.close() return Storage(storage)
def read_dict_aux(filename): fp = portalocker.LockedFile(filename, 'r') lang_text = fp.read().replace('\r\n', '\n') fp.close() if not lang_text.strip(): return {} try: return eval(lang_text) except: logging.error('Syntax error in %s' % filename) return {'__corrupted__': True}
def write_dict(filename, contents): try: fp = portalocker.LockedFile(filename, 'w') except (IOError, OSError): if not is_gae: logging.warning('Unable to write to file %s' % filename) return fp.write('# coding: utf8\n{\n') for key in sorted(contents): fp.write('%s: %s,\n' % (utf8_repr(key), utf8_repr(contents[key]))) fp.write('}\n') fp.close()
def read_plural_dict_aux(filename): fp = portalocker.LockedFile(filename, 'r') lang_text = fp.read().replace('\r\n', '\n') fp.close() if not lang_text.strip(): return {} try: return eval(lang_text) except Exception, e: status = 'Syntax error in %s (%s)' % (filename, e) logging.error(status) return {'__corrupted__': status}
def read_dict_aux(filename): fp = portalocker.LockedFile(filename, 'r') lang_text = fp.read().replace('\r\n', '\n') fp.close() # clear cache of processed messages: clear_cache(tcache.setdefault(filename, ({}, allocate_lock()))) if not lang_text.strip(): return {} try: return eval(lang_text) except Exception, e: status = 'Syntax error in %s (%s)' % (filename, e) logging.error(status) return {'__corrupted__': status}
def write_dict(filename, contents): if '__corrupted__' in contents: return try: fp = portalocker.LockedFile(filename, 'w') except (IOError, OSError): if not settings.global_settings.web2py_runtime_gae: logging.warning('Unable to write to file %s' % filename) return fp.write('# coding: utf8\n{\n') for key in sorted( contents, lambda x, y: cmp( unicode(x, 'utf-8').lower(), unicode(y, 'utf-8').lower())): fp.write('%s: %s,\n' % (repr(Utf8(key)), repr(Utf8(contents[key])))) fp.write('}\n') fp.close()
def write_plural_dict(filename, contents): if '__corrupted__' in contents: return try: fp = portalocker.LockedFile(filename, 'w') fp.write( '#!/usr/bin/env python\n{\n# "singular form (0)": ["first plural form (1)", "second plural form (2)", ...],\n' ) # coding: utf8\n{\n') for key in sorted( contents, lambda x, y: cmp( unicode(x, 'utf-8').lower(), unicode(y, 'utf-8').lower())): forms = '[' + ','.join( [repr(Utf8(form)) for form in contents[key]]) + ']' fp.write('%s: %s,\n' % (repr(Utf8(key)), forms)) fp.write('}\n') except (IOError, OSError): if not is_gae: logging.warning('Unable to write to file %s' % filename) return finally: fp.close()