From 927a697ef6f8da258bbba1f90c5c773c4c461a41 Mon Sep 17 00:00:00 2001 From: Pratik Vyas Date: Sat, 21 Sep 2013 15:13:18 +0530 Subject: [PATCH] conf migration part 1 --- core/doctype/doctype/doctype.py | 2 +- core/doctype/page/page.py | 2 +- core/doctype/profile/profile.py | 2 +- core/page/update_manager/update_manager.py | 2 +- webnotes/__init__.py | 20 ++++++++++++++++++-- webnotes/app.py | 8 +++++--- webnotes/auth.py | 4 ++-- webnotes/boot.py | 2 +- webnotes/db.py | 2 +- webnotes/memc.py | 5 +++-- webnotes/modules/patch_handler.py | 3 ++- webnotes/utils/__init__.py | 15 +++++++++++---- webnotes/utils/backups.py | 2 ++ webnotes/utils/email_lib/bulk.py | 3 ++- webnotes/utils/email_lib/smtp.py | 2 +- webnotes/webutils.py | 2 +- 16 files changed, 53 insertions(+), 23 deletions(-) diff --git a/core/doctype/doctype/doctype.py b/core/doctype/doctype/doctype.py index 3a0446428a..ea7bb6f4e6 100644 --- a/core/doctype/doctype/doctype.py +++ b/core/doctype/doctype/doctype.py @@ -76,7 +76,7 @@ class DocType: self.change_modified_of_parent() make_module_and_roles(self.doclist) - import conf + from webnotes import conf if (not webnotes.in_import) and getattr(conf, 'developer_mode', 0): self.export_doc() self.make_controller_template() diff --git a/core/doctype/page/page.py b/core/doctype/page/page.py index 026887a7ab..844361b26c 100644 --- a/core/doctype/page/page.py +++ b/core/doctype/page/page.py @@ -33,7 +33,7 @@ class DocType: Writes the .txt for this page and if write_content is checked, it will write out a .html file """ - import conf + from webnotes import conf from core.doctype.doctype.doctype import make_module_and_roles make_module_and_roles(self.doclist, "Page Role") diff --git a/core/doctype/profile/profile.py b/core/doctype/profile/profile.py index 6e8d55cf23..9485339cdf 100644 --- a/core/doctype/profile/profile.py +++ b/core/doctype/profile/profile.py @@ -52,7 +52,7 @@ class DocType: def validate_max_users(self): """don't allow more than max users if set in conf""" - import conf + from webnotes import conf # check only when enabling a user if hasattr(conf, 'max_users') and self.doc.enabled and \ self.doc.name not in ["Administrator", "Guest"] and \ diff --git a/core/page/update_manager/update_manager.py b/core/page/update_manager/update_manager.py index 527b40fedb..8a2a6fd2ae 100644 --- a/core/page/update_manager/update_manager.py +++ b/core/page/update_manager/update_manager.py @@ -9,7 +9,7 @@ from webnotes import _ @webnotes.whitelist(allow_roles=["System Manager", "Administrator"]) def update_this_app(): - import conf + from webnotes import conf if hasattr(conf, "expires_on"): return _("This feature is only applicable to self hosted instances") diff --git a/webnotes/__init__.py b/webnotes/__init__.py index e69dd2f179..3727d09644 100644 --- a/webnotes/__init__.py +++ b/webnotes/__init__.py @@ -9,6 +9,9 @@ from __future__ import unicode_literals from werkzeug.local import Local +import os +import json + local = Local() class _dict(dict): @@ -54,6 +57,7 @@ def load_translations(module, doctype, name): # local-globals conn = local("conn") +conf = local("conf") form = form_dict = local("form_dict") request = local("request") request_method = local("request_method") @@ -68,13 +72,14 @@ message_log = local("message_log") lang = local("lang") -def init(): +def init(site=None): local.error_log = [] local.message_log = [] local.debug_log = [] local.response = _dict({}) local.lang = "en" local.request_method = request.method if request else None + local.conf = get_conf(site) _memc = None mute_emails = False @@ -515,4 +520,15 @@ def get_config(): update_config(webnotes.utils.get_path("app", "config.json")) return _config - + +def get_conf(site): + import conf + from webnotes.utils import get_storage_base_path + conf = _dict(conf.__dict__) + if conf.sites_dir and site: + with open(os.path.join(get_storage_base_path(conf.sites_dir, site), 'site_config.json'), 'r') as f: + site_config = json.load(f) + site_config.update(conf.__dict__) + return _dict(site_config) + else: + return conf diff --git a/webnotes/app.py b/webnotes/app.py index 82d4a3cc29..4bba44be6d 100644 --- a/webnotes/app.py +++ b/webnotes/app.py @@ -1,4 +1,5 @@ import sys, os +import json sys.path.insert(0, '.') sys.path.insert(0, 'app') @@ -7,6 +8,7 @@ sys.path.insert(0, 'lib') from werkzeug.wrappers import Request, Response from werkzeug.local import LocalManager from werkzeug.wsgi import SharedDataMiddleware +from webnotes import get_config import mimetypes import webnotes @@ -20,8 +22,8 @@ local_manager = LocalManager([webnotes.local]) def application(request): webnotes.local.request = request - webnotes.init() - + webnotes.init(site=request.host) + webnotes.local.form_dict = webnotes._dict({ k:v[0] if isinstance(v, (list, tuple)) else v \ for k, v in (request.form or request.args).iteritems() }) @@ -54,6 +56,6 @@ if __name__ == '__main__': if len(sys.argv) > 1: port = sys.argv[1] - run_simple('localhost', int(port), application, use_reloader=True, + run_simple('0.0.0.0', int(port), application, use_reloader=True, use_debugger=True, use_evalex=True) diff --git a/webnotes/auth.py b/webnotes/auth.py index 64bf4d6f6e..f645a3fa14 100644 --- a/webnotes/auth.py +++ b/webnotes/auth.py @@ -6,7 +6,7 @@ import webnotes import webnotes.db import webnotes.utils import webnotes.profile -import conf +from webnotes import conf from webnotes.sessions import Session @@ -251,4 +251,4 @@ def _update_password(user, password): @webnotes.whitelist() def get_logged_user(): - return webnotes.session.user \ No newline at end of file + return webnotes.session.user diff --git a/webnotes/boot.py b/webnotes/boot.py index 9793a7497d..322a3d6d91 100644 --- a/webnotes/boot.py +++ b/webnotes/boot.py @@ -69,7 +69,7 @@ def get_bootinfo(): return bootinfo def load_conf_settings(bootinfo): - import conf + from webnotes import conf for key in ['developer_mode']: if hasattr(conf, key): bootinfo[key] = getattr(conf, key) diff --git a/webnotes/db.py b/webnotes/db.py index cf81d3129d..024f792e6a 100644 --- a/webnotes/db.py +++ b/webnotes/db.py @@ -7,7 +7,7 @@ from __future__ import unicode_literals import MySQLdb import webnotes -import conf +from webnotes import conf import datetime class Database: diff --git a/webnotes/memc.py b/webnotes/memc.py index e8a382e160..bb383c2649 100644 --- a/webnotes/memc.py +++ b/webnotes/memc.py @@ -2,7 +2,8 @@ # MIT License. See license.txt from __future__ import unicode_literals -import memcache, conf +import memcache +from webnotes import conf class MClient(memcache.Client): """memcache client that will automatically prefix conf.db_name""" @@ -23,4 +24,4 @@ class MClient(memcache.Client): return val def delete_value(self, key): - self.delete(self.n(key)) \ No newline at end of file + self.delete(self.n(key)) diff --git a/webnotes/modules/patch_handler.py b/webnotes/modules/patch_handler.py index bb20bac72c..3fc5217d6d 100644 --- a/webnotes/modules/patch_handler.py +++ b/webnotes/modules/patch_handler.py @@ -31,7 +31,7 @@ def reload_doc(args): run_single(method = webnotes.modules.reload_doc, methodargs = args) def run_single(patchmodule=None, method=None, methodargs=None, force=False): - import conf + from webnotes import conf # don't write txt files conf.developer_mode = 0 @@ -77,6 +77,7 @@ def execute_patch(patchmodule, method=None, methodargs=None): def add_to_patch_log(tb): """add error log to patches/patch.log""" import conf, os + # TODO use get_storage_base_path with open(os.path.join(os.path.dirname(conf.__file__), 'app', 'patches','patch.log'),'a') as patchlog: patchlog.write('\n\n' + tb) diff --git a/webnotes/utils/__init__.py b/webnotes/utils/__init__.py index cab05665bf..bb21548b1d 100644 --- a/webnotes/utils/__init__.py +++ b/webnotes/utils/__init__.py @@ -4,8 +4,11 @@ # util __init__.py from __future__ import unicode_literals +from webnotes import conf + import webnotes + user_time_zone = None user_format = None current_date = None @@ -65,11 +68,11 @@ def validate_email_add(email_str): def get_request_site_address(full_address=False): """get app url from request""" - import os, conf + import os - if hasattr(conf, "host_name"): - host_name = conf.host_name - else: + host_name = conf.host_name + + if not host_name: if webnotes.request: protocol = 'HTTPS' in webnotes.get_request_header('SERVER_PROTOCOL', "") and 'https://' or 'http://' host_name = protocol + webnotes.request.host @@ -815,6 +818,10 @@ def get_base_path(): import conf import os return os.path.dirname(os.path.abspath(conf.__file__)) + +def get_storage_base_path(sites_dir, hostname): + import os + return os.path.join(sites_dir, hostname) def get_url(uri=None): url = get_request_site_address() diff --git a/webnotes/utils/backups.py b/webnotes/utils/backups.py index 5c2e47c5f5..9b25b3f63d 100644 --- a/webnotes/utils/backups.py +++ b/webnotes/utils/backups.py @@ -70,6 +70,7 @@ class BackupGenerator: self.backup_path_db = this_file_path def zip_files(self): + # TODO use get_storage_base_path files_path = os.path.join(os.path.dirname(os.path.abspath(conf.__file__)), 'public', 'files') cmd_string = """tar -cf %s %s""" % (self.backup_path_files, files_path) err, out = webnotes.utils.execute_in_shell(cmd_string) @@ -185,6 +186,7 @@ def get_backup_path(): global backup_path if not backup_path: import os, conf + # TODO Use get_storage_base_path backup_path = os.path.join(os.path.dirname(os.path.abspath(conf.__file__)), 'public', 'backups') return backup_path diff --git a/webnotes/utils/email_lib/bulk.py b/webnotes/utils/email_lib/bulk.py index 4756ce7eba..efba3339da 100644 --- a/webnotes/utils/email_lib/bulk.py +++ b/webnotes/utils/email_lib/bulk.py @@ -18,7 +18,8 @@ def send(recipients=None, sender=None, doctype='Profile', email_field='email', return cint(rdata.unsubscribed) def check_bulk_limit(new_mails): - import conf, startup + import startup + from webnotes import conf from webnotes.utils import nowdate this_month = webnotes.conn.sql("""select count(*) from `tabBulk Email` where month(creation)=month(%s)""" % nowdate())[0][0] diff --git a/webnotes/utils/email_lib/smtp.py b/webnotes/utils/email_lib/smtp.py index 445099481b..5900a2cf21 100644 --- a/webnotes/utils/email_lib/smtp.py +++ b/webnotes/utils/email_lib/smtp.py @@ -8,7 +8,7 @@ Allows easy adding of Attachments of "File" objects """ import webnotes -import conf +from webnotes import conf from webnotes import msgprint from webnotes.utils import cint diff --git a/webnotes/webutils.py b/webnotes/webutils.py index b638a7cf50..b8e8171a35 100644 --- a/webnotes/webutils.py +++ b/webnotes/webutils.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals -import conf +from webnotes import conf import webnotes from webnotes import _ import webnotes.utils