def _init(): from os import path, makedirs from pytsite import tpl, lang, reg, cleanup from . import _eh # Resources tpl.register_package(__name__) lang.register_package(__name__) # Create directory to store session data session_storage_path = reg.get('paths.session') if not path.exists(session_storage_path): makedirs(session_storage_path, 0o755, True) # Lang globals lang.register_global('base_url', lambda language, args: base_url(language)) # Tpl globals tpl.register_global('url', url) tpl.register_global('is_rule_defined', has_rule) tpl.register_global('rule_url', rule_url) tpl.register_global('current_url', current_url) tpl.register_global('current_path', current_path) tpl.register_global('base_url', base_url) tpl.register_global('is_base_url', is_base_url) tpl.register_global('is_main_host', is_main_host) tpl.register_global('session_messages', lambda x: session().get_messages(x) if session() else ()) # Clear flash messages from all sessions s_store = get_session_store() for sid in s_store.list(): s_store.save_if_modified(s_store.get(sid).clear_messages()) # Events handlers cleanup.on_cleanup(_eh.on_cleanup)
def plugin_load_uwsgi(): from pytsite import lang from plugins import content_export from . import _driver lang.register_package(__name__) content_export.register_driver(_driver.Driver())
def plugin_load(): from pytsite import lang from plugins import assetman lang.register_package(__name__) assetman.register_package(__name__) assetman.t_less(__name__) assetman.t_js(__name__)
def plugin_load(): from pytsite import lang from plugins import assetman from . import _eh, _settings_form, _controllers # Resources lang.register_package(__name__) assetman.register_package(__name__) assetman.t_copy_static(__name__)
def _init(): from pytsite import lang, router, tpl lang.register_package(__name__) router.on_dispatch(reset, -999, '*') router.on_xhr_dispatch(reset, -999, '*') router.on_exception(lambda args: reset(args.get('title')), -999) tpl.register_global('metatag', dump) tpl.register_global('metatags', dump_all) tpl.register_global('metatag_get', get)
def plugin_load_uwsgi(): from pytsite import lang, events from plugins import content_export from . import _driver, _eh # Resources lang.register_package(__name__) # Register content export driver content_export.register_driver(_driver.Driver()) events.listen('[email protected]_save.content_export', _eh.odm_entity_pre_save_content_export)
def _init(): from pytsite import lang, console, update from . import _cc, _eh # Resource packages lang.register_package(__name__) # Console commands console.register_command(_cc.Install()) console.register_command(_cc.Uninstall()) # Events handlers update.on_update_stage_2(_eh.update_stage_2)
def _init(): from os import mkdir from pytsite import reg, lang, tpl, console, update, on_pytsite_load from . import _cc, _eh # Resources lang.register_package(__name__) tpl.register_global('plugins', _PluginsTplGlobal()) # Create 'plugins' package if it doesn't exist plugins_dir = plugins_dir_path() if not _path.exists(plugins_dir): mkdir(plugins_dir, 0o755) with open(_path.join(plugins_dir, '__init__.py'), 'wt') as f: f.write('"""Pytsite Plugins\n"""\n') # Register console commands console.register_command(_cc.Install()) console.register_command(_cc.Uninstall()) # Enable imports checking _meta_path.insert(0, _MetaPathHook()) # Load installed plugins if reg.get('plugman.autoload', True): maint_was_enabled = _maintenance.is_enabled() disabled_plugins = reg.get('plugman.disabled_plugins', []) try: _maintenance.enable(True) for p_name in local_plugins_info(): if p_name.startswith('_') or p_name in disabled_plugins: continue try: if not is_loaded(p_name): load(p_name) except (error.PluginLoadError, error.PluginNotInstalled) as e: console.print_warning(e) finally: if not maint_was_enabled: _maintenance.disable(True) # Event handlers on_pytsite_load(_eh.on_pytsite_load) update.on_update_stage_2(_eh.on_pytsite_update_stage_2)
def plugin_load_uwsgi(): from pytsite import lang, router from plugins import settings from . import _settings_form, _eh # Resources lang.register_package(__name__) # Lang globals lang.register_global('tumblr_admin_settings_url', lambda language, args: settings.form_url('tumblr')) # Settings settings.define('tumblr', _settings_form.Form, 'tumblr@tumblr', 'fa fa-tumblr', 'dev') # Event handlers router.on_dispatch(_eh.router_dispatch)
def plugin_load(): from pytsite import lang, events from plugins import permissions, odm from . import _model, _api, _driver, _eh # Resources lang.register_package(__name__) # Permissions permissions.define_group('content_import', 'content_import@content_import') # ODM models odm.register_model('content_import', _model.ContentImport) # Event handlers events.listen('[email protected]_fields', _eh.odm_model_setup_fields) events.listen('[email protected]_indexes', _eh.odm_model_setup_indexes) # RSS import driver _api.register_driver(_driver.RSS())
def _init(): from pytsite import console, lang from . import _console_command lang.register_package(__name__) console.register_command(_console_command.Maintenance())
"""Pytsite Database Module. """ __author__ = 'Oleksandr Shepetko' __email__ = '*****@*****.**' __license__ = 'MIT' # Public API from pymongo import ASCENDING as I_ASC, DESCENDING as I_DESC, TEXT as I_TEXT, GEO2D as I_GEO2D from ._api import get_client, get_collection, get_collection_names, get_config, get_database from pytsite import console as _console, lang as _lang from ._console_command import Db _lang.register_package(__name__) _console.register_command(Db())
def load(self): """Load the theme """ from plugins import assetman # Check for requirements try: package_info.check_requirements(self._package_name) except package_info.error.Error as e: raise RuntimeError('Error while loading theme {}: {}'.format(self._package_name, e)) # Create translations directory lang_dir = path.join(self._path, 'res', 'lang') if not path.exists(lang_dir): makedirs(lang_dir, 0o755, True) # Create translation stub files for lng in lang.langs(): lng_f_path = path.join(lang_dir, '{}.yml'.format(lng)) if not path.exists(lng_f_path): with open(lng_f_path, 'wt'): pass # Register translation resources lang.register_package(self._package_name) # Register template resources tpl_path = path.join(self._path, 'res', 'tpl') if not path.exists(tpl_path): makedirs(tpl_path, 0o755, True) tpl.register_package(self._package_name) # Register assetman resources assets_path = path.join(self._path, 'res', 'assets') if not path.exists(assets_path): makedirs(assets_path, 0o755, True) assetman.register_package(self._package_name) # Load required plugins for pn, pv in self._requires['plugins'].items(): plugman.load(pn, VersionRange(pv)) # Load theme's module try: self._module = import_module(self._package_name) if hasattr(self._module, 'theme_load') and callable(self._module.theme_load): self._module.theme_load() # theme_load_{env.type}() hook env_type = reg.get('env.type') hook_names = ['theme_load_{}'.format(env_type)] if env_type == 'wsgi': hook_names.append('theme_load_uwsgi') for hook_name in hook_names: if hasattr(self._module, hook_name): getattr(self._module, hook_name)() logger.debug("Theme '{}' successfully loaded".format(self._package_name)) except Exception as e: raise _error.ThemeLoadError("Error while loading theme package '{}': {}".format(self._package_name, e)) # Compile assets if not reg.get('theme.compiled'): assetman.setup() assetman.build(self._package_name) reg.put('theme.compiled', True) self._is_loaded = True return self
"""PytSite Blog Init. """ from pytsite import lang, content, permissions, settings, router, widget, tpl from . import model, settings_form # Resources lang.register_package('app', 'lang') # ODM models content.register_model('article', model.Article, 'app@articles') content.register_model('page', model.Page, 'app@pages') # Permissions permissions.define_permission('app.settings.manage', 'app@manage_app_settings', 'app') # Settings settings.define('app', settings_form.Form, __name__ + '@application', 'fa fa-cube', 'app.settings.manage') # Index by section route router.add_rule('/section/<string:term_alias>', 'index_by_section', 'pytsite.content@index', { 'model': 'article', 'term_field': 'section', }) # Index by tag route router.add_rule('/tag/<string:term_alias>', 'article_index_by_tag', 'pytsite.content@index', { 'model': 'article', 'term_field': 'tags', }) # Prepare language related tpl globals
def _init(): from pytsite import lang lang.register_package(__name__)
def _init(): """Init wrapper """ import sys from importlib import import_module from sys import argv, exit from os import path, environ from getpass import getuser from socket import gethostname from . import reg, package_info, semver # Load regisrty memory driver reg.set_driver(reg.driver.Memory()) # Environment type and name reg.put('env.name', getuser() + '@' + gethostname()) if len(argv) > 1 and argv[1] == 'test': reg.put('env.type', 'testing') else: reg.put('env.type', 'wsgi' if 'UWSGI_ORIGINAL_PROC_NAME' in environ else 'console') # Detect application's root directory path cur_dir = path.abspath(path.dirname(__file__)) while True: if path.exists(path.join(cur_dir, 'app', 'app.json')): root_path = cur_dir break elif cur_dir != '/': cur_dir = path.abspath(path.join(cur_dir, path.pardir)) else: raise RuntimeError('Cannot determine root directory of application') # It is important for correct importing of packages inside 'themes', 'plugins', etc sys.path.append(root_path) # Base filesystem paths env_path = environ.get('VIRTUAL_ENV', path.join(root_path, 'env')) app_path = path.join(root_path, 'app') reg.put('paths.root', root_path) reg.put('paths.env', env_path) reg.put('paths.app', app_path) for n in ['config', 'log', 'static', 'storage', 'tmp']: reg.put('paths.' + n, path.join(root_path, n)) # PytSite path reg.put('paths.pytsite', path.join(root_path, path.dirname(__file__))) # uWSGI does not export virtualenv paths, do it by ourselves if 'VIRTUAL_ENV' not in environ: environ['VIRTUAL_ENV'] = env_path environ['PATH'] = path.join(env_path, 'bin') + ':' + environ['PATH'] # Additional filesystem paths reg.put('paths.session', path.join(reg.get('paths.storage'), 'session')) # Debug is disabled by default reg.put('debug', False) # Check for 'app' package if not path.exists(app_path): raise FileNotFoundError("Directory '{}' is not found".format(app_path)) # Switch registry to the file driver file_driver = reg.driver.File(reg.get('paths.config'), reg.get('env.name'), reg.get_driver()) reg.set_driver(file_driver) # Default output parameters reg.put('output', { 'minify': not reg.get('debug'), }) # Initialize logger from . import logger logger.info('') logger.info('---===[ PytSite-{} Started ]===---'.format(package_info.version('pytsite'))) # Initialize rest of the system from pytsite import console, util try: # Initialize cache with default driver from pytsite import cache cache.set_driver(cache.driver.File()) # Load required core packages, order is important for pkg_name in ('cron', 'stats', 'reload', 'update', 'plugman', 'testing'): import_module('pytsite.' + pkg_name) # Register app's resources if path.exists(path.join(app_path, 'res', 'lang')): from pytsite import lang lang.register_package('app') if path.exists(path.join(app_path, 'res', 'tpl')): from pytsite import tpl tpl.register_package('app') # Load app package from pytsite import plugman, events try: import app package_info.check_requirements('app') # app_load() hook if hasattr(app, 'app_load'): app.app_load() # app_load_{env.type}() hook hook_name = 'app_load_{}'.format(reg.get('env.type')) if hasattr(app, hook_name): getattr(app, hook_name)() events.fire('pytsite.app_load') logger.debug('Application loaded') except Exception as e: logger.error(e) console.print_warning('Application load error: {}'.format(e)) finally: events.fire('pytsite.load') logger.debug('PytSite initialized and ready to work') except Warning as e: console.print_warning(e) except Exception as e: console.print_error(e) if reg.get('debug'): raise e exit(1)
def _init(): from pytsite import lang, console from . import _console_command lang.register_package(__name__) console.register_command(_console_command.Reload())