From 84fb8fdead26743dce1c7755f1fe1283286c46be Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Mon, 17 Jun 2013 11:55:29 +0530 Subject: [PATCH] [build] rewrite to build translations on demand (wip) --- webnotes/{utils/bundlejs.py => build.py} | 68 ++++++++++++++---------- webnotes/translate.py | 47 ++++++++++++++++ wnf.py | 38 ++++--------- 3 files changed, 95 insertions(+), 58 deletions(-) rename webnotes/{utils/bundlejs.py => build.py} (97%) diff --git a/webnotes/utils/bundlejs.py b/webnotes/build.py similarity index 97% rename from webnotes/utils/bundlejs.py rename to webnotes/build.py index 3a1af7b48e..71956ae9ed 100644 --- a/webnotes/utils/bundlejs.py +++ b/webnotes/build.py @@ -23,8 +23,47 @@ from __future__ import unicode_literals from webnotes.utils.minify import JavascriptMinify +""" +Build the `public` folders and setup languages +""" + import os, sys, webnotes + +def bundle(no_compress, cms_make=True): + """concat / minify js files""" + # build js files + check_public() + check_lang() + bundle = Bundle() + bundle.no_compress = no_compress + bundle.make() + + if cms_make: + # build index.html and app.html + from website.helpers.make_web_include_files import make + make() + +def watch(no_compress): + """watch and rebuild if necessary""" + import time + bundle = Bundle() + bundle.no_compress = no_compress + + while True: + if bundle.dirty(): + bundle.make() + + time.sleep(3) + +def check_public(): + from webnotes.install_lib.setup_public_folder import make + make() + +def check_lang(): + from webnotes.tranlate import update_translations + update_translations() + class Bundle: """ Concatenate, compress and mix (if required) js+css files from build.json @@ -184,32 +223,3 @@ class Bundle: self.appfiles = appfiles self.bdata = bdata -def check_public(): - from webnotes.install_lib.setup_public_folder import make - make() - -def bundle(no_compress, cms_make=True): - """concat / minify js files""" - # build js files - check_public() - bundle = Bundle() - bundle.no_compress = no_compress - bundle.make() - - if cms_make: - # build index.html and app.html - from website.helpers.make_web_include_files import make - make() - -def watch(no_compress): - """watch and rebuild if necessary""" - import time - bundle = Bundle() - bundle.no_compress = no_compress - - while True: - if bundle.dirty(): - bundle.make() - - time.sleep(3) - diff --git a/webnotes/translate.py b/webnotes/translate.py index a0c7019ee5..4f39660ee6 100644 --- a/webnotes/translate.py +++ b/webnotes/translate.py @@ -37,6 +37,53 @@ import re messages = {} +def translate(lang=None): + languages = [lang] + if lang=="all" or lang==None: + languages = get_all_languages() + + print "Extracting messages..." + build_message_files() + + export_messages(lang, '_lang_tmp.csv') + + for lang in languages: + if lang != "en": + filename = 'app/translations/'+lang+'.csv' + print "For " + lang + ":" + print "Compiling messages in one file..." + print "Translating via Google Translate..." + google_translate(lang, '_lang_tmp.csv', filename) + print "Updating language files..." + import_messages(lang, filename) + print "Deleting temp files..." + + os.remove('_lang_tmp.csv') + +def get_all_languages(): + return [f[:-4] for f in os.listdir("app/translations") if f.endswith(".csv")] + +def update_translations(): + """ + compare language file timestamps with last updated timestamps in `.wnf-lang-status` + if timestamps are missing / changed, build new `.json` files in the `lang folders` + """ + langstatus = {} + languages = get_all_languages() + if os.path.exists(".wnf-lang-status"): + with open(".erpnext-lang-status", "r") as langstatusfile: + langstatus = eval(langstatusfile.read()) + + for lang in languages: + filename = 'app/translations/'+lang+'.csv' + if langstatus.get(lang, None)!=os.path.getmtime(filename): + print "Setting up lang files for " + lang + "..." + import_messages(lang, filename) + langstatus[lang] = os.path.getmtime(filename) + + with open(".wnf-lang-status", "w") as langstatusfile: + langstatus = langstatusfile.write(str(langstatus)) + def build_message_files(): """build from doctypes, pages, database and framework""" build_for_pages('lib/core') diff --git a/wnf.py b/wnf.py index 0377f5eae1..dcebb72340 100755 --- a/wnf.py +++ b/wnf.py @@ -60,8 +60,8 @@ def pull(remote, branch, build=False): def rebuild(): # build js / css - from webnotes.utils import bundlejs - bundlejs.bundle(False) + from webnotes import build + build.bundle(False) def apply_latest_patches(): import webnotes.modules.patch_handler @@ -277,17 +277,17 @@ def run(): # build if options.build: - from webnotes.utils import bundlejs + from webnotes import build if options.no_cms: cms_make = False else: cms_make = True - bundlejs.bundle(False, cms_make) + build.bundle(False, cms_make) return elif options.watch: - from webnotes.utils import bundlejs - bundlejs.watch(True) + from webnotes import build + build.watch(True) return # code replace @@ -477,29 +477,9 @@ def run(): google_translate(*options.google_translate) elif options.translate: - from webnotes.translate import build_message_files, \ - export_messages, google_translate, import_messages - - languages = [options.translate] - if options.translate=="all": - from startup import lang_list - languages = lang_list - - print "Extracting messages..." - build_message_files() - for lang in languages: - if lang != "en": - filename = 'app/translations/'+lang+'.csv' - print "For " + lang + ":" - print "Compiling messages in one file..." - export_messages(lang, '_lang_tmp.csv') - print "Translating via Google Translate..." - google_translate(lang, '_lang_tmp.csv', filename) - print "Updating language files..." - import_messages(lang, filename) - print "Deleting temp files..." - os.remove('_lang_tmp.csv') - + from webnotes.translate import translate + translate(options.translate) + elif options.reset_perms: for d in webnotes.conn.sql_list("""select name from `tabDocType` where ifnull(istable, 0)=0 and ifnull(custom, 0)=0"""):