def handle(self, options, global_options, *args): from uliweb.utils.common import pkg from uliweb import functions from uliweb.core.template import template_file from uliweb.orm import true import time self.get_application(global_options) Recorder = functions.get_model('uliwebrecorder') if args: if os.path.exists(args[0]): message = "Ths file %s is already exists, do you want to overwrite it?" % args[ 0] ans = 'Y' if global_options.yes else get_answer(message) if ans != 'Y': return out = open(args[0], 'w') relpath = os.path.normpath( os.path.relpath(os.path.dirname(args[0]) or './', '.')).replace('\\', '/') else: out = sys.stdout relpath = '.' condition = true() if options.begin_time: condition = (Recorder.c.begin_datetime >= options.begin_time) & condition if options.id: condition = (Recorder.c.id >= int(options.id)) & condition path = pkg.resource_filename('uliweb.contrib.recorder', 'template_files') tplfile = os.path.join(path, options.template).replace('\\', '/') row_tplfile = os.path.join(path, options.template_row).replace('\\', '/') out.write('#coding=utf8\n') if global_options.verbose: print('#recorder template is "%s"' % tplfile) print('#recorder row template is "%s"' % row_tplfile) begin = time.time() rows = [] for row in Recorder.filter(condition): rows.append(template_file(row_tplfile, {'row': row}).rstrip()) out.write( template_file(tplfile, { 'project_dir': relpath, 'rows': rows })) out.write('\n#total %d records output, time used %ds\n' % (len(rows), time.time() - begin))
def handle(self, options, global_options, *args): from uliweb.utils.common import pkg from uliweb.utils.pyini import Ini from uliweb.core.commands import get_input from uliweb.core.template import template_file import glob _types = [] config_files = {} app_dirs = [os.path.join(SimpleFrame.get_app_dir(appname), 'template_files/config') for appname in self.get_apps(global_options)] for path in [pkg.resource_filename('uliweb', 'template_files/config/')] + app_dirs: if os.path.exists(path): files = glob.glob(os.path.join(path, '*.conf')) if files: for f in files: _name = os.path.splitext(os.path.basename(f))[0] _types.append(_name) config_files[_name] = f support_type = args[0] if args else '' while not support_type in _types and support_type != 'quit': print 'Supported types:\n' print ' ' + '\n '.join(sorted(_types)) print support_type = raw_input('Please enter support type[quit to exit]:') if support_type != 'quit': conf_file = config_files[support_type] conf_ini = conf_file[:-5] + '.ini' if not os.path.exists(conf_file): log.error("%s config can't be found" % support_type) sys.exit(1) data = {} data['project_dir'] = os.path.abspath(os.getcwd()) data['project'] = os.path.basename(data['project_dir']) if os.path.exists(conf_ini): x = Ini(conf_ini) for k, v in x.INPUT.items(): if isinstance(v, (tuple, list)): if len(v) == 2: prompt, default = v else: prompt = v[0] default = '' else: prompt, default = v or '', '' if not prompt.strip(): prompt = 'Please input %s[%s]:' % (k, default) r = get_input(prompt, default=default) data[k] = r data.update(x.get('DEFAULT', {})) print print template_file(conf_file, data)
def handle(self, options, global_options, *args): from uliweb.utils.common import pkg from uliweb.utils.pyini import Ini from uliweb.core.commands import get_input from uliweb.core.template import template_file import glob _types = [] config_files = {} app_dirs = [ os.path.join(SimpleFrame.get_app_dir(appname), "template_files/config") for appname in self.get_apps(global_options) ] for path in [pkg.resource_filename("uliweb", "template_files/config/")] + app_dirs: if os.path.exists(path): files = glob.glob(os.path.join(path, "*.conf")) if files: for f in files: _name = os.path.splitext(os.path.basename(f))[0] _types.append(_name) config_files[_name] = f support_type = args[0] if args else "" while not support_type in _types and support_type != "quit": print "Supported types:\n" print " " + "\n ".join(sorted(_types)) print support_type = raw_input("Please enter support type[quit to exit]:") if support_type != "quit": conf_file = config_files[support_type] conf_ini = conf_file[:-5] + ".ini" if not os.path.exists(conf_file): log.error("%s config can't be found" % support_type) sys.exit(1) data = {} data["project_dir"] = os.path.abspath(os.getcwd()) data["project"] = os.path.basename(data["project_dir"]) if os.path.exists(conf_ini): x = Ini(conf_ini) for k, v in x.INPUT.items(): if isinstance(v, (tuple, list)): prompt, default = v else: prompt, default = v or "", "" if not prompt.strip(): prompt = "Please input %s[%s]:" % (k, default) r = get_input(prompt, default=default) data[k] = r data.update(x.get("DEFAULT", {})) print print template_file(conf_file, data)
def handle(self, options, global_options, *args): from uliweb.utils.common import extract_dirs from uliweb.core.template import template_file if not args: project_name = '' while not project_name: project_name = raw_input('Please enter project name:') else: project_name = args[0] ans = '-1' if os.path.exists(project_name): if options.force: ans = 'y' while ans not in ('y', 'n'): ans = raw_input('The project directory has been existed, do you want to overwrite it?(y/n)[n]') if not ans: ans = 'n' else: ans = 'y' if ans == 'y': extract_dirs('uliweb', 'template_files/project', project_name, verbose=global_options.verbose) #template setup.py setup_file = os.path.join(project_name, 'setup.py') text = template_file(setup_file, {'project_name':project_name}) with open(setup_file, 'w') as f: f.write(text) #rename .gitignore.template to .gitignore os.rename(os.path.join(project_name, '.gitignore.template'), os.path.join(project_name, '.gitignore'))
def handle(self, options, global_options, *args): from uliweb.utils.common import extract_dirs from uliweb.core.template import template_file from uliweb.manage import make_simple_application from uliweb import settings from sqlalchemy import create_engine, MetaData, Table from shutil import rmtree from uliweb.orm import get_connection, engine_manager alembic_path = os.path.join(global_options.project, 'alembic', options.engine).replace('\\', '/') #delete alembic path if os.path.exists(alembic_path): rmtree(alembic_path, True) extract_dirs('uliweb.contrib.orm', 'templates/alembic', alembic_path, verbose=global_options.verbose, replace=True) make_simple_application(project_dir=global_options.project, settings_file=global_options.settings, local_settings_file=global_options.local_settings) ini_file = os.path.join(alembic_path, 'alembic.ini') text = template_file(ini_file, {'connection':engine_manager[options.engine].options.connection_string, 'engine_name':options.engine, 'script_location':alembic_path}) with open(ini_file, 'w') as f: f.write(text) #drop old alembic_version table db = get_connection(options.engine) metadata = MetaData(db) if db.dialect.has_table(db.connect(), 'alembic_version'): version = Table('alembic_version', metadata, autoload=True) version.drop()
def handle(self, options, global_options, *args): from uliweb.utils.common import extract_dirs from uliweb.core.template import template_file from uliweb.manage import make_simple_application from uliweb import settings from sqlalchemy import create_engine, MetaData, Table from shutil import rmtree from uliweb.orm import get_connection, engine_manager alembic_path = os.path.join(global_options.project, 'alembic', options.engine).replace('\\', '/') #delete alembic path if os.path.exists(alembic_path): rmtree(alembic_path, True) extract_dirs('uliweb.contrib.orm', 'templates/alembic', alembic_path, verbose=global_options.verbose, replace=True) make_simple_application(project_dir=global_options.project, settings_file=global_options.settings, local_settings_file=global_options.local_settings) ini_file = os.path.join(alembic_path, 'alembic.ini') text = template_file(ini_file, {'connection':engine_manager[options.engine].options.connection_string, 'engine_name':options.engine, 'script_location':alembic_path}) with open(ini_file, 'w') as f: f.write(text) #drop old alembic_version table db = get_connection(engine_name=options.engine) metadata = MetaData(db) if db.dialect.has_table(db.connect(), 'alembic_version'): version = Table('alembic_version', metadata, autoload=True) version.drop()
def handle(self, options, global_options, *args): from uliweb.utils.common import extract_dirs from uliweb.core.template import template_file if not args: project_name = "" while not project_name: project_name = raw_input("Please enter project name:") else: project_name = args[0] ans = "-1" if os.path.exists(project_name): if options.force: ans = "y" while ans not in ("y", "n"): ans = raw_input("The project directory has been existed, do you want to overwrite it?(y/n)[n]") if not ans: ans = "n" else: ans = "y" if ans == "y": extract_dirs("uliweb", "template_files/project", project_name, verbose=global_options.verbose) # template setup.py setup_file = os.path.join(project_name, "setup.py") text = template_file(setup_file, {"project_name": project_name}) with open(setup_file, "w") as f: f.write(text) # rename .gitignore.template to .gitignore os.rename(os.path.join(project_name, ".gitignore.template"), os.path.join(project_name, ".gitignore"))
def handle(self, options, global_options, *args): from uliweb.utils.common import pkg from uliweb import functions from uliweb.core.template import template_file from uliweb.orm import true import time self.get_application(global_options) Recorder = functions.get_model('uliwebrecorder') if args: if os.path.exists(args[0]): message = "Ths file %s is already exists, do you want to overwrite it?" % args[0] ans = 'Y' if global_options.yes else get_answer(message) if ans != 'Y': return out = open(args[0], 'w') relpath = os.path.normpath(os.path.relpath(os.path.dirname(args[0]) or './', '.')).replace('\\', '/') else: out = sys.stdout relpath = '.' condition = true() if options.begin_time: condition = (Recorder.c.begin_datetime >= options.begin_time) & condition if options.id: condition = (Recorder.c.id >= int(options.id)) & condition path = pkg.resource_filename('uliweb.contrib.recorder', 'template_files') tplfile = os.path.join(path, options.template).replace('\\', '/') row_tplfile = os.path.join(path, options.template_row).replace('\\', '/') out.write('#coding=utf8\n') if global_options.verbose: print '#recorder template is "%s"' % tplfile print '#recorder row template is "%s"' % row_tplfile begin = time.time() rows = [] for row in Recorder.filter(condition): rows.append(template_file(row_tplfile, {'row':row}).rstrip()) out.write(template_file(tplfile, {'project_dir':relpath, 'rows':rows})) out.write('\n#total %d records output, time used %ds\n' % (len(rows), time.time()-begin))
def handle(self, options, global_options, *args): from uliweb.utils.common import extract_dirs, pkg from uliweb.core.template import template_file extract_dirs('uliweb.contrib.orm', 'templates/alembic', '.', verbose=global_options.verbose, replace=False) engine_string = get_engine(options, global_options) ini_file = os.path.join(pkg.resource_filename('uliweb.contrib.orm', 'templates/alembic/alembic.ini')) text = template_file(ini_file, {'CONNECTION':engine_string}) with open(os.path.join(global_options.project, 'alembic.ini'), 'w') as f: f.write(text)
def copy_view(self, viewfile_template, data, viewfile_dst, replace): text = template_file(viewfile_template, data).replace('\r\n', '\n') if replace or not os.path.exists(viewfile_dst): text = '#coding=utf8\n' + text f = open(viewfile_dst, 'w') else: if os.path.getsize(viewfile_dst) == 0: text = '#coding=utf8\n' + text f = open(viewfile_dst, 'a') f.write(text) f.close()
def generate_html(tables, apps, **kwargs): from uliweb import orm from os.path import dirname, join from uliweb.core.template import template_file from uliweb.orm import ReferenceProperty from uliweb.utils.textconvert import text2html from sqlalchemy.schema import CreateIndex menus = [] for app in apps: section = {'name': '%s' % app, 'items': []} t = get_model_tables(tables, app) if not t: continue for tablename in t: item = { 'app_name': app.replace('.', '_'), 'name': tablename, 'caption': tablename, } try: M = orm.get_model(tablename) except: continue item['label'] = getattr(M, '__verbose_name__', tablename) if tablename != M.tablename: item['caption'] += ' - ' + M.tablename section['items'].append(item) menus.append(section) all_tables = [] for name, t in sorted(tables.iteritems()): model = { 'name': name, 'fields': [], 'relations': [], 'choices': [], 'indexes': [], } if hasattr(t, '__appname__'): model['appname'] = text2html(t.__appname__) else: model['appname'] = None M = None try: M = orm.get_model(name) except: pass if getattr(M, '__verbose_name__', None): model['label'] = "%s(%s)" % (name, getattr(M, '__verbose_name__', None)) else: model['label'] = name if name != getattr(M, 'tablename', name): model['label'] += ' - ' + M.tablename #Add docstring for Model if M.__doc__: model['desc'] = M.__doc__ else: model['desc'] = '' #process indexes for x in t.indexes: model['indexes'].append(CreateIndex(x)) star_index = 0 for tablefield in sorted(t.c, key=lambda x: (x.name)): field = { 'name': tablefield.name, 'type': tablefield.type, 'nullable': tablefield.nullable, 'primary_key': tablefield.primary_key } field['reftable'] = None field['star'] = False field['label'] = "%s" % tablefield.name if M: ppp = M.properties[tablefield.name] if getattr(ppp, 'verbose_name', None): field['label'] = "%s" % (getattr(ppp, 'verbose_name', None)) if getattr(ppp, 'choices', None): choices_list = getattr(ppp, 'choices', None) if callable(choices_list): choices_list = choices_list() if choices_list: star_index = star_index + 1 model['choices'].append({ 'index': star_index, 'fieldlabel': field['label'], 'fieldname': field['name'], 'list': choices_list }) field['star'] = star_index if ppp and ppp.__class__ is ReferenceProperty: field['reftable'] = "%s" % ppp.reference_class.tablename model['fields'].append(field) all_tables.append(model) database = {} database["menus"] = menus database["tables"] = all_tables return template_file(join(dirname(__file__), "templates/docindex.html"), database)
def render(vars, name=name): return template_file(join(dirname(__file__), 'shared', name), vars)
def generate_html(tables, apps, **kwargs): from uliweb import orm from os.path import dirname, join from uliweb.core.template import template_file from uliweb.orm import ReferenceProperty from uliweb.utils.textconvert import text2html from sqlalchemy.schema import CreateIndex menus = [] for app in apps: section = { 'name': '%s' % app, 'items': [] } t = get_model_tables(tables, app) if not t: continue for tablename in t: item = { 'app_name': app.replace('.', '_'), 'name': tablename, 'caption': tablename, } try: M = orm.get_model(tablename) except: continue item['label'] = getattr(M, '__verbose_name__', tablename) if tablename != M.tablename: item['caption'] += ' - ' + M.tablename section['items'].append(item) menus.append(section) all_tables = [] for name, t in sorted(tables.iteritems()): model = { 'name': name, 'fields': [], 'relations': [], 'choices': [], 'indexes': [], } if hasattr(t, '__appname__') : model['appname'] = text2html(t.__appname__) else : model['appname'] = None M = None try: M = orm.get_model(name) except: pass if getattr(M, '__verbose_name__', None): model['label'] = "%s(%s)" % (name, getattr(M, '__verbose_name__', None)) else: model['label'] = name if name != getattr(M, 'tablename', name): model['label'] += ' - ' + M.tablename #Add docstring for Model if M.__doc__: model['desc'] = M.__doc__ else: model['desc'] = '' #process indexes for x in t.indexes: model['indexes'].append(CreateIndex(x)) star_index = 0 for tablefield in sorted(t.c, key=lambda x:(x.name)): field = { 'name': tablefield.name, 'type': tablefield.type, 'nullable': tablefield.nullable, 'primary_key': tablefield.primary_key } field['reftable'] = None field['star'] = False field['label'] = "%s" % tablefield.name if M: ppp = M.properties[tablefield.name] if getattr(ppp, 'verbose_name', None): field['label'] = "%s" % (getattr(ppp, 'verbose_name', None)) if getattr(ppp, 'choices', None): choices_list = getattr(ppp, 'choices', None) if callable(choices_list) : choices_list = choices_list() if choices_list : star_index = star_index + 1 model['choices'].append({ 'index': star_index , 'fieldlabel':field['label'], 'fieldname':field['name'], 'list':choices_list}) field['star'] = star_index if ppp and ppp.__class__ is ReferenceProperty: field['reftable'] = "%s" % ppp.reference_class.tablename model['fields'].append(field) all_tables.append(model) database = {} database["menus"] = menus; database["tables"] = all_tables; return template_file(join(dirname(__file__), "templates/docindex.html"), database)
def copy_template(self, src_template, data, dst_template): text = template_file(src_template, data).replace('\r\n', '\n') f = open(dst_template, 'w') f.write(text) f.close()
def render(fpath, dst, df): text = template_file(fpath, data).replace('\r\n', '\n') open(df, 'w').write(text) return True