示例#1
0
    def scan_source_files(self):
        source_files = []
        kid_files = []
        genshi_files = []
        js_files = []
        srcdir = self.options.source_dir or get_package_name().split(".", 1)[0]
        print "Scanning source directory", srcdir
        for root, dirs, files in os.walk(srcdir):
            if os.path.basename(root).lower() in ("cvs", ".svn"):
                continue
            for fname in files:
                name, ext = os.path.splitext(fname)
                srcfile = os.path.join(root, fname)
                if ext == ".py":
                    source_files.append(srcfile)
                elif ext == ".kid":
                    kid_files.append(srcfile)
                elif ext == ".html":
                    genshi_files.append(srcfile)
                elif ext == ".js":
                    js_files.append(srcfile)
                else:
                    pass  # do nothing
        tmp_handle, tmp_potfile = tempfile.mkstemp(".pot", "tmp", self.locale_dir)
        os.close(tmp_handle)
        potbasename = os.path.basename(tmp_potfile)[:-4]
        pygettext_options = ["-v", "-d", potbasename, "-p", os.path.dirname(tmp_potfile)]
        if self.options.ascii_output:
            pygettext_options.insert(0, "-E")
        pygettext.sys.argv = [""] + pygettext_options + source_files
        pygettext.main()
        if not os.path.exists(tmp_potfile):
            raise ProgramError("pygettext failed")
        atexit.register(silent_os_remove, tmp_potfile)

        if kid_files and self.options.kid_support:
            if not kid:
                print "Kid not installed, no support for Kid templates."
            else:
                self.scan_kid_files(tmp_potfile, kid_files)

        if genshi_files and self.options.genshi_support:
            try:
                self.scan_genshi_files(tmp_potfile, genshi_files)
            except ImportError:
                print "Genshi not installed, no support for Genshi templates."

        if js_files and self.options.js_support:
            self.scan_js_files(tmp_potfile, js_files)

        potfile = self.get_potfile_path()
        if os.path.isfile(potfile):
            bakfile = potfile.replace(".pot", ".bak")
            silent_os_remove(bakfile)
            os.rename(potfile, bakfile)
            print "Backup existing file to", bakfile
        os.rename(tmp_potfile, potfile)
        print "Message templates written to", potfile
示例#2
0
    def collect_string_for_files(self, files):
        if not files:
            return

        params = ['', '-v']
        for file in files:
            params.append(file)

        pygettext.sys.argv = params
        pygettext.main()

        pot = os.path.join(self.currentProject, 'messages.pot')
        if not os.path.exists(pot):
            return

        locales = self.locales_directory()
        filename = os.path.join(locales, 'messages.pot')
        try:
            shutil.copyfile(pot, filename)
        except IOError, e:
            print e