def get_untranslated(lang, untranslated_file, get_all=False): """translate objects using Google API. Add you own API key for translation""" clear_cache() apps = frappe.get_all_apps(True) messages = [] untranslated = [] for app in apps: messages.extend(get_messages_for_app(app)) if get_all: print str(len(messages)) + " messages" with open(untranslated_file, "w") as f: for m in messages: f.write((m + "\n").encode("utf-8")) else: full_dict = get_full_dict(lang) for m in messages: if not full_dict.get(m): untranslated.append(m) if untranslated: print str(len(untranslated)) + " missing translations of " + str( len(messages)) with open(untranslated_file, "w") as f: for m in untranslated: f.write((m + "\n").encode("utf-8")) else: print "all translated!"
def install_app(name, verbose=False, set_as_patched=True): frappe.flags.in_install = name frappe.flags.ignore_in_install = False frappe.clear_cache() app_hooks = frappe.get_hooks(app_name=name) installed_apps = frappe.get_installed_apps() # install pre-requisites if app_hooks.required_apps: for app in app_hooks.required_apps: install_app(app, verbose=verbose) frappe.flags.in_install = name frappe.clear_cache() if name not in frappe.get_all_apps(): raise Exception("App not in apps.txt") if name in installed_apps: frappe.msgprint(_("App {0} already installed").format(name)) return print("\nInstalling {0}...".format(name)) if name != "frappe": frappe.only_for("System Manager") for before_install in app_hooks.before_install or []: out = frappe.get_attr(before_install)() if out == False: return if name != "frappe": add_module_defs(name) sync_for(name, force=True, sync_everything=True, verbose=verbose, reset_permissions=True) add_to_installed_apps(name) frappe.get_doc('Portal Settings', 'Portal Settings').sync_menu() if set_as_patched: set_all_patches_as_completed(name) for after_install in app_hooks.after_install or []: frappe.get_attr(after_install)() sync_jobs() sync_fixtures(name) sync_customizations(name) for after_sync in app_hooks.after_sync or []: frappe.get_attr(after_sync)() # frappe.flags.in_install = False
def make_asset_dirs(make_copy=False, restore=False): # don't even think of making assets_path absolute - rm -rf ahead. assets_path = join_path(frappe.local.sites_path, "assets") for dir_path in [ join_path(assets_path, 'js'), join_path(assets_path, 'css')]: if not path_exists(dir_path): os.makedirs(dir_path) for app_name in frappe.get_all_apps(True): pymodule = frappe.get_module(app_name) app_base_path = abspath(os.path.dirname(pymodule.__file__)) symlinks = [] app_public_path = join_path(app_base_path, 'public') # app/public > assets/app symlinks.append([app_public_path, join_path(assets_path, app_name)]) # app/node_modules > assets/app/node_modules if path_exists(abspath(app_public_path)): symlinks.append([join_path(app_base_path, '..', 'node_modules'), join_path(assets_path, app_name, 'node_modules')]) app_doc_path = None if isdir(join_path(app_base_path, 'docs')): app_doc_path = join_path(app_base_path, 'docs') elif isdir(join_path(app_base_path, 'www', 'docs')): app_doc_path = join_path(app_base_path, 'www', 'docs') if app_doc_path: symlinks.append([app_doc_path, join_path(assets_path, app_name + '_docs')]) for source, target in symlinks: source = abspath(source) if path_exists(source): if restore: if path_exists(target): if os.path.islink(target): os.unlink(target) else: shutil.rmtree(target) shutil.copytree(source, target) elif make_copy: if path_exists(target): warnings.warn('Target {target} already exists.'.format(target = target)) else: shutil.copytree(source, target) else: if path_exists(target): if os.path.islink(target): os.unlink(target) else: shutil.rmtree(target) try: os.symlink(source, target) except OSError: print('Cannot link {} to {}'.format(source, target)) else: # warnings.warn('Source {source} does not exist.'.format(source = source)) pass
def make_asset_dirs(make_copy=False): assets_path = os.path.join(frappe.local.sites_path, "assets") for dir_path in [ os.path.join(assets_path, 'js'), os.path.join(assets_path, 'css') ]: if not os.path.exists(dir_path): os.makedirs(dir_path) # symlink app/public > assets/app for app_name in frappe.get_all_apps(True): pymodule = frappe.get_module(app_name) app_base_path = os.path.abspath(os.path.dirname(pymodule.__file__)) symlinks = [] symlinks.append([ os.path.join(app_base_path, 'public'), os.path.join(assets_path, app_name) ]) symlinks.append([ os.path.join(app_base_path, 'docs'), os.path.join(assets_path, app_name + '_docs') ]) for source, target in symlinks: source = os.path.abspath(source) if not os.path.exists(target) and os.path.exists(source): if make_copy: shutil.copytree(source, target) else: os.symlink(source, target)
def get_version(): "Show the versions of all the installed apps" frappe.init('') for m in sorted(frappe.get_all_apps()): module = frappe.get_module(m) if hasattr(module, "__version__"): print("{0} {1}".format(m, module.__version__))
def update_translations(lang, untranslated_file, translated_file): """Update translations from a source and target file for a given language. :param lang: Language code (e.g. `en`). :param untranslated_file: File path with the messages in English. :param translated_file: File path with messages in language to be updated.""" clear_cache() full_dict = get_full_dict(lang) def restore_newlines(s): return (s.replace("|||||", "\\\n") .replace("| | | | |", "\\\n") .replace("||||", "\\n") .replace("| | | |", "\\n") .replace("|||", "\n") .replace("| | |", "\n")) translation_dict = {} for key, value in zip(frappe.get_file_items(untranslated_file, ignore_empty_lines=False), frappe.get_file_items(translated_file, ignore_empty_lines=False)): # undo hack in get_untranslated translation_dict[restore_newlines(key)] = restore_newlines(value) full_dict.update(translation_dict) for app in frappe.get_all_apps(True): write_translations_file(app, lang, full_dict)
def get_app_list(): """Get list of all apps with properties, installed, category from hooks and `frappe/data/app_listing/` if an entry exists""" out = {} installed = frappe.get_installed_apps() for app in frappe.get_all_apps(True): app_hooks = frappe.get_hooks(app_name=app) if app not in installed and app_hooks.get('hide_in_installer'): continue out[app] = {} for key in ("app_name", "app_title", "app_description", "app_icon", "app_publisher", "app_version", "app_url", "app_color"): val = app_hooks.get(key) or [] out[app][key] = val[0] if len(val) else "" if app in installed: out[app]["installed"] = 1 for app_from_list in get_app_listing().values(): if app_from_list.app_name in out: out[app_from_list.app_name].update(app_from_list) else: if not frappe.conf.disallow_app_listing: out[app_from_list.app_name] = app_from_list return out
def make_asset_dirs(make_copy=False): assets_path = os.path.join(frappe.local.sites_path, "assets") for dir_path in [ os.path.join(assets_path, 'js'), os.path.join(assets_path, 'css')]: if not os.path.exists(dir_path): os.makedirs(dir_path) # symlink app/public > assets/app for app_name in frappe.get_all_apps(True): pymodule = frappe.get_module(app_name) app_base_path = os.path.abspath(os.path.dirname(pymodule.__file__)) symlinks = [] symlinks.append([os.path.join(app_base_path, 'public'), os.path.join(assets_path, app_name)]) symlinks.append([os.path.join(app_base_path, 'docs'), os.path.join(assets_path, app_name + '_docs')]) for source, target in symlinks: source = os.path.abspath(source) if not os.path.exists(target) and os.path.exists(source): if make_copy: shutil.copytree(source, target) else: os.symlink(source, target)
def install_app(name, verbose=False, set_as_patched=True): frappe.flags.in_install_app = name frappe.clear_cache() app_hooks = frappe.get_hooks(app_name=name) installed_apps = frappe.get_installed_apps() if name not in frappe.get_all_apps(with_frappe=True): raise Exception("App not in apps.txt") if name in installed_apps: print "App Already Installed" frappe.msgprint(_("App Already Installed")) return if name != "frappe": frappe.only_for("System Manager") for before_install in app_hooks.before_install or []: frappe.get_attr(before_install)() if name != "frappe": add_module_defs(name) sync_for(name, force=True, sync_everything=True, verbose=verbose) add_to_installed_apps(name) if set_as_patched: set_all_patches_as_completed(name) for after_install in app_hooks.after_install or []: frappe.get_attr(after_install)() frappe.flags.in_install_app = False
def get_celery_app(): conf = get_site_config() app = Celery('frappe', broker=conf.celery_broker or DEFAULT_CELERY_BROKER, backend=conf.async_redis_server or DEFAULT_CELERY_BACKEND) app.autodiscover_tasks(frappe.get_all_apps(with_frappe=True, with_internal_apps=False, sites_path=SITES_PATH)) app.conf.CELERY_TASK_SERIALIZER = 'json' app.conf.CELERY_ACCEPT_CONTENT = ['json'] app.conf.CELERY_TIMEZONE = 'UTC' app.conf.CELERY_RESULT_SERIALIZER = 'json' app.conf.CELERY_TASK_RESULT_EXPIRES = timedelta(0, 3600) if conf.monitory_celery: app.conf.CELERY_SEND_EVENTS = True app.conf.CELERY_SEND_TASK_SENT_EVENT = True app.conf.CELERY_ROUTES = (SiteRouter(), AsyncTaskRouter()) app.conf.CELERYBEAT_SCHEDULE = get_beat_schedule(conf) if conf.celery_error_emails: app.conf.CELERY_SEND_TASK_ERROR_EMAILS = True for k, v in conf.celery_error_emails.iteritems(): setattr(app.conf, k, v) return app
def load_lang(lang, apps=None): """Combine all translations from `.csv` files in all `apps`. For derivative languages (es-GT), take translations from the base language (es) and then update translations from the child (es-GT)""" if lang == 'en': return {} out = frappe.cache().hget("lang_full_dict", lang, shared=True) if not out: out = {} for app in (apps or frappe.get_all_apps(True)): path = os.path.join(frappe.get_pymodule_path(app), "translations", lang + ".csv") out.update(get_translation_dict_from_file(path, lang, app) or {}) if '-' in lang: parent = lang.split('-')[0] parent_out = load_lang(parent) parent_out.update(out) out = parent_out frappe.cache().hset("lang_full_dict", lang, out, shared=True) return out or {}
def load_lang(lang, apps=None): """Combine all translations from `.csv` files in all `apps`""" out = {} for app in (apps or frappe.get_all_apps(True)): path = os.path.join(frappe.get_pymodule_path(app), "translations", lang + ".csv") if os.path.exists(path): csv_content = read_csv_file(path) cleaned = {} for item in csv_content: if len(item) == 3: # with file and line numbers cleaned[item[1]] = item[2] elif len(item) == 2: cleaned[item[0]] = item[1] else: raise Exception( "Bad translation in '{app}' for language '{lang}': {values}" .format(app=app, lang=lang, values=repr(item).encode("utf-8"))) out.update(cleaned) return out
def load_lang(lang, apps=None): """Combine all translations from `.csv` files in all `apps`""" out = {} for app in (apps or frappe.get_all_apps(True)): path = os.path.join(frappe.get_pymodule_path(app), "translations", lang + ".csv") out.update(get_translation_dict_from_file(path, lang, app)) return out
def get_build_maps(): """get all build.jsons with absolute paths""" # framework js and css files pymodules = [frappe.get_module(app) for app in frappe.get_all_apps(True)] app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules] build_maps = {} for app_path in app_paths: path = os.path.join(app_path, 'public', 'build.json') if os.path.exists(path): with open(path) as f: try: for target, sources in json.loads(f.read()).iteritems(): # update app path source_paths = [] for source in sources: if isinstance(source, list): s = frappe.get_pymodule_path(source[0], *source[1].split("/")) else: s = os.path.join(app_path, source) source_paths.append(s) build_maps[target] = source_paths except Exception, e: print path raise
def get_untranslated(lang, untranslated_file, get_all=False): """translate objects using Google API. Add you own API key for translation""" clear_cache() apps = frappe.get_all_apps(True) messages = [] untranslated = [] for app in apps: messages.extend(get_messages_for_app(app)) if get_all: print str(len(messages)) + " messages" with open(untranslated_file, "w") as f: for m in messages: f.write((m + "\n").encode("utf-8")) else: full_dict = get_full_dict(lang) for m in messages: if not full_dict.get(m): untranslated.append(m) if untranslated: print str(len(untranslated)) + " missing translations of " + str(len(messages)) with open(untranslated_file, "w") as f: for m in untranslated: f.write((m + "\n").encode("utf-8")) else: print "all translated!"
def install_app(name): """Install app, if app is not installed in local environment, install it via git url in `frappe/data/app_listing/`""" frappe.only_for("System Manager") if name not in frappe.get_all_apps(True): if not frappe.conf.disallow_app_listing: get_app(name) frappe.cache().delete_value(["app_hooks"]) # reload sys.path import site reload(site) else: # will only come via direct API frappe.throw("Listing app not allowed") app_hooks = frappe.get_hooks(app_name=name) if app_hooks.get('hide_in_installer'): frappe.throw(_("You cannot install this app")) frappe.publish_realtime("install_app_progress", {"status": _("Installing App {0}").format(name)}, user=frappe.session.user, now=True) frappe.installer.install_app(name) frappe.publish_realtime("install_app_progress", {"status": _("{0} Installed").format(name)}, user=frappe.session.user, now=True)
def get_app_list(): out = {} installed = frappe.get_installed_apps() for app in frappe.get_all_apps(True): app_hooks = frappe.get_hooks(app_name=app) if app not in installed and app_hooks.get('hide_in_installer'): continue out[app] = {} for key in ("app_name", "app_title", "app_description", "app_icon", "app_publisher", "app_version", "app_url", "app_color"): val = app_hooks.get(key) or [] out[app][key] = val[0] if len(val) else "" if app in installed: out[app]["installed"] = 1 app_listing = get_app_listing() app_listing = { app: app_listing[app] for app in app_listing if app not in installed } out.update(app_listing) return out
def get_build_maps(): """get all build.jsons with absolute paths""" # framework js and css files pymodules = [frappe.get_module(app) for app in frappe.get_all_apps(True)] app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules] build_maps = {} for app_path in app_paths: path = os.path.join(app_path, 'public', 'build.json') if os.path.exists(path): with open(path) as f: try: for target, sources in json.loads(f.read()).iteritems(): # update app path source_paths = [] for source in sources: if isinstance(source, list): s = frappe.get_pymodule_path( source[0], *source[1].split("/")) else: s = os.path.join(app_path, source) source_paths.append(s) build_maps[target] = source_paths except Exception, e: print path raise
def import_translations(lang, path): """Import translations from file in standard format""" clear_cache() full_dict = get_full_dict(lang) full_dict.update(get_translation_dict_from_file(path, lang, 'import')) for app in frappe.get_all_apps(True): write_translations_file(app, lang, full_dict)
def setup(): global app_paths pymodules = [] for app in frappe.get_all_apps(True): try: pymodules.append(frappe.get_module(app)) except ImportError: pass app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules]
def add_to_installed_apps(*apps): from frappe.installer import add_to_installed_apps frappe.connect() all_apps = frappe.get_all_apps(with_frappe=True) for each in apps: if each in all_apps: add_to_installed_apps(each, rebuild_website=False) frappe.destroy()
def load_lang(lang, apps=None): out = {} for app in (apps or frappe.get_all_apps(True)): path = os.path.join(frappe.get_pymodule_path(app), "translations", lang + ".csv") if os.path.exists(path): cleaned = dict([item for item in dict(read_csv_file(path)).iteritems() if item[1]]) out.update(cleaned) return out
def install_app(name, verbose=False, set_as_patched=True): frappe.flags.in_install = name frappe.flags.ignore_in_install = False frappe.clear_cache() app_hooks = frappe.get_hooks(app_name=name) installed_apps = frappe.get_installed_apps() # install pre-requisites if app_hooks.required_apps: for app in app_hooks.required_apps: install_app(app) frappe.flags.in_install = name frappe.clear_cache() if name not in frappe.get_all_apps(): raise Exception("App not in apps.txt") if name in installed_apps: frappe.msgprint(_("App {0} already installed").format(name)) return print("\nInstalling {0}...".format(name)) if name != "frappe": frappe.only_for("System Manager") for before_install in app_hooks.before_install or []: out = frappe.get_attr(before_install)() if out==False: return if name != "frappe": add_module_defs(name) sync_for(name, force=True, sync_everything=True, verbose=verbose, reset_permissions=True) sync_from_app(name) add_to_installed_apps(name) frappe.get_doc('Portal Settings', 'Portal Settings').sync_menu() if set_as_patched: set_all_patches_as_completed(name) for after_install in app_hooks.after_install or []: frappe.get_attr(after_install)() sync_fixtures(name) sync_customizations(name) for after_sync in app_hooks.after_sync or []: frappe.get_attr(after_sync)() # frappe.flags.in_install = False
def get_app_paths(): '''Creates a list of paths to all Python modules, based on known apps''' pymodules = [] for app in frappe.get_all_apps(True): try: pymodules.append(frappe.get_module(app)) except ImportError: pass return [os.path.dirname(pymodule.__file__) for pymodule in pymodules]
def update_translations(lang, untranslated_file, translated_file): clear_cache() full_dict = get_full_dict(lang) full_dict.update(dict(zip(frappe.get_file_items(untranslated_file), frappe.get_file_items(translated_file)))) for app in frappe.get_all_apps(True): write_translations_file(app, lang, full_dict)
def setup(): global app_paths, assets_path pymodules = [] for app in frappe.get_all_apps(True): try: pymodules.append(frappe.get_module(app)) except ImportError: pass app_paths = [os.path.dirname(pymodule.__file__) for pymodule in pymodules] assets_path = os.path.join(frappe.local.sites_path, "assets")
def install_app(name): """Install app, if app is not installed in local environment, install it via git url in `frappe/data/app_listing/`""" frappe.only_for("System Manager") if name not in frappe.get_all_apps(True): if not frappe.conf.disallow_app_listing: get_app(name) frappe.cache().delete_value(["app_hooks"]) # reload sys.path import site
def make_asset_dirs(make_copy=False, restore=False): # don't even think of making assets_path absolute - rm -rf ahead. assets_path = os.path.join(frappe.local.sites_path, "assets") for dir_path in [ os.path.join(assets_path, 'js'), os.path.join(assets_path, 'css')]: if not os.path.exists(dir_path): os.makedirs(dir_path) # symlink app/public > assets/app for app_name in frappe.get_all_apps(True): pymodule = frappe.get_module(app_name) app_base_path = os.path.abspath(os.path.dirname(pymodule.__file__)) symlinks = [] symlinks.append([os.path.join(app_base_path, 'public'), os.path.join(assets_path, app_name)]) app_doc_path = None if os.path.isdir(os.path.join(app_base_path, 'docs')): app_doc_path = os.path.join(app_base_path, 'docs') elif os.path.isdir(os.path.join(app_base_path, 'www', 'docs')): app_doc_path = os.path.join(app_base_path, 'www', 'docs') if app_doc_path: symlinks.append([app_doc_path, os.path.join(assets_path, app_name + '_docs')]) for source, target in symlinks: source = os.path.abspath(source) if os.path.exists(source): if restore: if os.path.exists(target): if os.path.islink(target): os.unlink(target) else: shutil.rmtree(target) shutil.copytree(source, target) elif make_copy: if os.path.exists(target): warnings.warn('Target {target} already exists.'.format(target = target)) else: shutil.copytree(source, target) else: if os.path.exists(target): if os.path.islink(target): os.unlink(target) else: shutil.rmtree(target) os.symlink(source, target) else: # warnings.warn('Source {source} does not exist.'.format(source = source)) pass
def install_app(name): if name not in frappe.get_all_apps(True): get_app(name) frappe.cache().delete_value(["app_hooks"]) # reload sys.path import site reload(site) app_hooks = frappe.get_hooks(app_name=name) if app_hooks.get('hide_in_installer'): frappe.throw(_("You cannot install this app")) frappe.installer.install_app(name)
def update_translations(lang, untranslated_file, translated_file): clear_cache() full_dict = get_full_dict(lang) translation_dict = {} for key, value in zip(frappe.get_file_items(untranslated_file), frappe.get_file_items(translated_file)): # undo hack in get_untranslated translation_dict[key.replace("\\n", "\n")] = value.replace("\\n", "\n") full_dict.update(translation_dict) for app in frappe.get_all_apps(True): write_translations_file(app, lang, full_dict)
def load_lang(lang, apps=None): """Combine all translations from `.csv` files in all `apps`""" out = {} for app in (apps or frappe.get_all_apps(True)): path = os.path.join(frappe.get_pymodule_path(app), "translations", lang + ".csv") if os.path.exists(path): csv_content = read_csv_file(path) try: # with file and line numbers cleaned = dict([(item[1], item[2]) for item in csv_content if item[2]]) except IndexError: cleaned = dict([(item[0], item[1]) for item in csv_content if item[1]]) out.update(cleaned) return out
def get_dict(fortype, name=None): """Returns translation dict for a type of object. :param fortype: must be one of `doctype`, `page`, `report`, `include`, `jsfile`, `boot` :param name: name of the document for which assets are to be returned. """ fortype = fortype.lower() cache = frappe.cache() asset_key = fortype + ":" + (name or "-") translation_assets = cache.hget("translation_assets", frappe.local.lang, shared=True) or {} if not asset_key in translation_assets: if fortype=="doctype": messages = get_messages_from_doctype(name) elif fortype=="page": messages = get_messages_from_page(name) elif fortype=="report": messages = get_messages_from_report(name) elif fortype=="include": messages = get_messages_from_include_files() elif fortype=="jsfile": messages = get_messages_from_file(name) elif fortype=="boot": messages = [] apps = frappe.get_all_apps(True) for app in apps: messages.extend(get_server_messages(app)) messages = deduplicate_messages(messages) messages += frappe.db.sql("""select "navbar", item_label from `tabNavbar Item` where item_label is not null""") messages = get_messages_from_include_files() messages += frappe.db.sql("select 'Print Format:', name from `tabPrint Format`") messages += frappe.db.sql("select 'DocType:', name from tabDocType") messages += frappe.db.sql("select 'Role:', name from tabRole") messages += frappe.db.sql("select 'Module:', name from `tabModule Def`") messages += frappe.db.sql("select '', format from `tabWorkspace Shortcut` where format is not null") messages += frappe.db.sql("select '', title from `tabOnboarding Step`") message_dict = make_dict_from_messages(messages, load_user_translation=False) message_dict.update(get_dict_from_hooks(fortype, name)) # remove untranslated message_dict = {k:v for k, v in iteritems(message_dict) if k!=v} translation_assets[asset_key] = message_dict cache.hset("translation_assets", frappe.local.lang, translation_assets, shared=True) translation_map = translation_assets[asset_key] translation_map.update(get_user_translations(frappe.local.lang)) return translation_map
def get_version(): "Show the versions of all the installed apps" from frappe.utils.change_log import get_app_branch frappe.init('') for m in sorted(frappe.get_all_apps()): branch_name = get_app_branch(m) module = frappe.get_module(m) app_hooks = frappe.get_module(m + ".hooks") if hasattr(app_hooks, '{0}_version'.format(branch_name)): print("{0} {1}".format(m, getattr(app_hooks, '{0}_version'.format(branch_name)))) elif hasattr(module, "__version__"): print("{0} {1}".format(m, module.__version__))
def get_app_list(): out = {} installed = frappe.get_installed_apps() for app in frappe.get_all_apps(True): out[app] = {} app_hooks = frappe.get_hooks(app_name=app) for key in ("app_name", "app_title", "app_description", "app_icon", "app_publisher", "app_version", "app_url", "app_color"): val = app_hooks.get(key) or [] out[app][key] = val[0] if len(val) else "" if app in installed: out[app]["installed"] = 1 return out
def setup_celery(app, conf): app.autodiscover_tasks(frappe.get_all_apps(with_frappe=True, with_internal_apps=False, sites_path=SITES_PATH)) app.conf.CELERY_TASK_SERIALIZER = "json" app.conf.CELERY_ACCEPT_CONTENT = ["json"] app.conf.CELERY_TIMEZONE = "UTC" if conf.celery_queue_per_site: app.conf.CELERY_ROUTES = (SiteRouter(),) app.conf.CELERYBEAT_SCHEDULE = get_beat_schedule(conf) if conf.celery_error_emails: app.conf.CELERY_SEND_TASK_ERROR_EMAILS = True for k, v in conf.celery_error_emails.iteritems(): setattr(app.conf, k, v)
def egd_load_lang(lang, apps=None): """Checks `en` too""" if is_app_for_actual_site(): import os from frappe.translate import get_translation_dict_from_file out = frappe.cache().hget("lang_full_dict", lang, shared=True) if not out: out = {} for app in (apps or frappe.get_all_apps(True)): path = os.path.join(frappe.get_pymodule_path(app), "translations", lang + ".csv") out.update(get_translation_dict_from_file(path, lang, app) or {}) frappe.cache().hset("lang_full_dict", lang, out, shared=True) return out or {} else: return frappe_load_lang(lang, apps)
def application(request): frappe.local.request = request frappe.local.is_ajax = frappe.get_request_header("X-Requested-With")=="XMLHttpRequest" response = None try: rollback = True init_site(request) #wirte comment if "api_handler" in frappe.get_all_apps() and frappe.get_hooks("api_name", app_name="api_handler"): api_name = frappe.get_hooks("api_name", app_name="api_handler")[0] if frappe.local.conf.get('maintenance_mode'): raise frappe.SessionStopped make_form_dict(request) frappe.local.http_request = frappe.auth.HTTPRequest() if frappe.local.form_dict.cmd: response = frappe.handler.handle() elif frappe.request.path.startswith("/api/"): response = frappe.api.handle() elif frappe.request.path.startswith('/backups'): response = frappe.utils.response.download_backup(request.path) elif frappe.request.path.startswith('/private/files/'): response = frappe.utils.response.download_private_file(request.path) elif frappe.local.request.method in ('GET', 'HEAD'): response = frappe.website.render.render(request.path) #write comment elif api_name and frappe.request.path.startswith("/%s/"%api_name): response = api_handler.api.handle() else: raise NotFound except HTTPException, e: return e
def install_app(name, verbose=False, set_as_patched=True): frappe.clear_cache() app_hooks = frappe.get_hooks(app_name=name) installed_apps = frappe.get_installed_apps() # install pre-requisites if app_hooks.required_apps: for app in app_hooks.required_apps: install_app(app) frappe.flags.in_install = name frappe.clear_cache() if name not in frappe.get_all_apps(with_frappe=True): raise Exception("App not in apps.txt") if name in installed_apps: frappe.msgprint("App {0} already installed".format(name)) return print "Installing {0}...".format(name) if name != "frappe": frappe.only_for("System Manager") for before_install in app_hooks.before_install or []: out = frappe.get_attr(before_install)() if out==False: return if name != "frappe": add_module_defs(name) sync_for(name, force=True, sync_everything=True, verbose=verbose) add_to_installed_apps(name) if set_as_patched: set_all_patches_as_completed(name) for after_install in app_hooks.after_install or []: frappe.get_attr(after_install)() print "Installing fixtures..." sync_fixtures(name) frappe.flags.in_install = False
def export_translations(): # ssh -p 9999 [email protected] "cd /home/frappe/frappe-bench/apps/frappe && git diff" | patch -p1 for lang in get_all_languages(): if lang!="en": print("exporting " + lang) edited = dict(frappe.db.sql("""select source, translated from `tabTranslated Message` where language=%s""", lang)) for app in frappe.get_all_apps(True): path = os.path.join(frappe.get_app_path(app, "translations", lang + ".csv")) if os.path.exists(path): # only update existing strings current = dict(read_csv_file(path)) for key in current: current[key] = edited.get(key) or current[key] write_translations_file(app, lang, current, sorted(list(current)))
def get_app_list(): out = {} installed = frappe.get_installed_apps() for app in frappe.get_all_apps(True): app_hooks = frappe.get_hooks(app_name=app) if app_hooks.get('hide_in_installer'): continue out[app] = {} for key in ("app_name", "app_title", "app_description", "app_icon", "app_publisher", "app_version", "app_url", "app_color"): val = app_hooks.get(key) or [] out[app][key] = val[0] if len(val) else "" if app in installed: out[app]["installed"] = 1 return out
def get_untranslated(lang, untranslated_file, get_all=False): """Returns all untranslated strings for a language and writes in a file :param lang: Language code. :param untranslated_file: Output file path. :param get_all: Return all strings, translated or not.""" clear_cache() apps = frappe.get_all_apps(True) messages = [] untranslated = [] for app in apps: messages.extend(get_messages_for_app(app)) messages = deduplicate_messages(messages) def escape_newlines(s): return (s.replace("\\\n", "|||||") .replace("\\n", "||||") .replace("\n", "|||")) if get_all: print str(len(messages)) + " messages" with open(untranslated_file, "w") as f: for m in messages: # replace \n with ||| so that internal linebreaks don't get split f.write((escape_newlines(m[1]) + os.linesep).encode("utf-8")) else: full_dict = get_full_dict(lang) for m in messages: if not full_dict.get(m[1]): untranslated.append(m[1]) if untranslated: print str(len(untranslated)) + " missing translations of " + str(len(messages)) with open(untranslated_file, "w") as f: for m in untranslated: # replace \n with ||| so that internal linebreaks don't get split f.write((escape_newlines(m) + os.linesep).encode("utf-8")) else: print "all translated!"
def set_filters(jenv): import frappe from frappe.utils import global_date_format, cint, cstr, flt from frappe.website.utils import get_hex_shade from markdown2 import markdown from json import dumps jenv.filters["global_date_format"] = global_date_format jenv.filters["markdown"] = markdown jenv.filters["json"] = dumps jenv.filters["get_hex_shade"] = get_hex_shade jenv.filters["len"] = len jenv.filters["int"] = cint jenv.filters["str"] = cstr jenv.filters["flt"] = flt # load jenv_filters from hooks.py for app in frappe.get_all_apps(True): for jenv_filter in (frappe.get_hooks(app_name=app).jenv_filter or []): filter_name, filter_function = jenv_filter.split(":") jenv.filters[filter_name] = frappe.get_attr(filter_function)
def get_untranslated(lang, untranslated_file, get_all=False): """translate objects using Google API. Add you own API key for translation""" clear_cache() apps = frappe.get_all_apps(True) messages = [] untranslated = [] for app in apps: messages.extend(get_messages_for_app(app)) messages = list(set(messages)) def escape_newlines(s): return (s.replace("\\\n", "|||||") .replace("\\n", "||||") .replace("\n", "|||")) if get_all: print str(len(messages)) + " messages" with open(untranslated_file, "w") as f: for m in messages: # replace \n with ||| so that internal linebreaks don't get split f.write((escape_newlines(m) + os.linesep).encode("utf-8")) else: full_dict = get_full_dict(lang) for m in messages: if not full_dict.get(m): untranslated.append(m) if untranslated: print str(len(untranslated)) + " missing translations of " + str(len(messages)) with open(untranslated_file, "w") as f: for m in untranslated: # replace \n with ||| so that internal linebreaks don't get split f.write((escape_newlines(m) + os.linesep).encode("utf-8")) else: print "all translated!"
def install_app(name): """Install app, if app is not installed in local environment, install it via git url in `frappe/data/app_listing/`""" frappe.only_for("System Manager") if name not in frappe.get_all_apps(True): if not frappe.conf.disallow_app_listing: get_app(name) frappe.cache().delete_value(["app_hooks"]) # reload sys.path import site reload_module(site) else: # will only come via direct API frappe.throw(_("Listing app not allowed")) app_hooks = frappe.get_hooks(app_name=name) if app_hooks.get('hide_in_installer'): frappe.throw(_("You cannot install this app")) enqueue('frappe.desk.page.applications.applications.start_install', name=name) frappe.msgprint(_('Queued for install'))
def update_translations(lang, untranslated_file, translated_file): clear_cache() full_dict = get_full_dict(lang) def restore_newlines(s): return (s.replace("|||||", "\\\n") .replace("| | | | |", "\\\n") .replace("||||", "\\n") .replace("| | | |", "\\n") .replace("|||", "\n") .replace("| | |", "\n")) translation_dict = {} for key, value in zip(frappe.get_file_items(untranslated_file, ignore_empty_lines=False), frappe.get_file_items(translated_file, ignore_empty_lines=False)): # undo hack in get_untranslated translation_dict[restore_newlines(key)] = restore_newlines(value) full_dict.update(translation_dict) for app in frappe.get_all_apps(True): write_translations_file(app, lang, full_dict)