Beispiel #1
0
def load_translations(directory, domain="cyclone"):
    """Loads translations from gettext's locale tree

    Locale tree is similar to system's /usr/share/locale, like:

    {directory}/{lang}/LC_MESSAGES/{domain}.mo

    Three steps are required to have you app translated:

    1. Generate POT translation file
        xgettext --language=Python --keyword=_:1,2 -d cyclone file1.py file2.html etc

    2. Merge against existing POT file:
        msgmerge old.po cyclone.po > new.po

    3. Compile:
        msgfmt cyclone.po -o {directory}/pt_BR/LC_MESSAGES/cyclone.mo

    """
    global _translations
    global _supported_locales
    _translations = {}

    for lang in os.listdir(directory):
        try:
            os.stat(os.path.join(directory, lang, "LC_MESSAGES", domain+".mo"))
            _translations[lang] = gettext.translation(domain, directory, languages=[lang])
        except Exception, e:
            log.err("Cannot load translation for '%s': %s" % (lang, str(e)))
            continue
Beispiel #2
0
 def __init__(self, template_string, name="<string>", loader=None,
              compress_whitespace=None):
     self.name = name
     if compress_whitespace is None:
         compress_whitespace = name.endswith(".html") or \
             name.endswith(".js")
     reader = _TemplateReader(name, template_string)
     self.file = _File(_parse(reader))
     self.code = self._generate_python(loader, compress_whitespace)
     try:
         self.compiled = compile(self.code, self.name, "exec")
     except:
         formatted_code = _format_code(self.code).rstrip()
         log.err("%s code:\n%s" % (self.name, formatted_code))
         raise
Beispiel #3
0
 def generate(self, **kwargs):
     """Generate this template with the given arguments."""
     namespace = {
         "escape": escape.xhtml_escape,
         "url_escape": escape.url_escape,
         "json_encode": escape.json_encode,
         "squeeze": escape.squeeze,
         "datetime": datetime,
     }
     namespace.update(kwargs)
     exec self.compiled in namespace
     execute = namespace["_execute"]
     try:
         return execute()
     except:
         formatted_code = _format_code(self.code).rstrip()
         log.err("%s code:\n%s" % (self.name, formatted_code))
         raise
Beispiel #4
0
 def _parse_mime_body(self, boundary, data):
     if data.endswith("\r\n"):
         footer_length = len(boundary) + 6
     else:
         footer_length = len(boundary) + 4
     parts = data[:-footer_length].split("--" + boundary + "\r\n")
     for part in parts:
         if not part: continue
         eoh = part.find("\r\n\r\n")
         if eoh == -1:
             log.err("multipart/form-data missing headers")
             continue
         headers = HTTPHeaders.parse(part[:eoh])
         name_header = headers.get("Content-Disposition", "")
         if not name_header.startswith("form-data;") or \
            not part.endswith("\r\n"):
             log.err("Invalid multipart/form-data")
             continue
         value = part[eoh + 4:-2]
         name_values = {}
         for name_part in name_header[10:].split(";"):
             name, name_value = name_part.strip().split("=", 1)
             name_values[name] = name_value.strip('"').decode("utf-8")
         if not name_values.get("name"):
             log.err("multipart/form-data value missing name")
             continue
         name = name_values["name"]
         if name_values.get("filename"):
             ctype = headers.get("Content-Type", "application/unknown")
             self._request.files.setdefault(name, []).append(dict(
                 filename=name_values["filename"], body=value,
                 content_type=ctype))
         else:
             self._request.arguments.setdefault(name, []).append(value)