1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
/*
* Copyright (C) 2010-2012 Jeremy Lainé
* Contact: http://code.google.com/p/qdjango/
*
* This file is part of the QDjango Library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include <cstdlib>
#include <QDebug>
#include <QMetaProperty>
#include <QStringList>
#include <QCoreApplication>
#include <QFileInfo>
#include <QScriptEngine>
#include <QSettings>
#include <QSqlDatabase>
#include <QTextStream>
#include <QtPlugin>
#include "QDjango.h"
#include "QDjangoScript.h"
#include "../../tests/auth-models.h"
static bool wantsToQuit;
Q_DECLARE_METATYPE(QDjangoQuerySet<User>)
Q_DECLARE_METATYPE(QDjangoQuerySet<Group>)
Q_DECLARE_METATYPE(QDjangoQuerySet<Message>)
static QScriptValue qtscript_dir(QScriptContext *ctx, QScriptEngine *eng)
{
QObject *obj = ctx->argument(0).toQObject();
if (obj)
{
const QMetaObject* meta = obj->metaObject();
for(int i = meta->propertyOffset(); i < meta->propertyCount(); ++i)
qDebug() << meta->property(i).name();
}
return eng->undefinedValue();
}
static QScriptValue qtscript_load(QScriptContext *ctx, QScriptEngine *eng)
{
QString name = ctx->argument(0).toString();
eng->importExtension(name);
return eng->undefinedValue();
}
static QScriptValue qtscript_quit(QScriptContext *ctx, QScriptEngine *eng)
{
Q_UNUSED(ctx);
wantsToQuit = true;
return eng->undefinedValue();
}
static QScriptValue qtscript_syncdb(QScriptContext *ctx, QScriptEngine *eng)
{
Q_UNUSED(ctx);
QDjango::createTables();
return eng->undefinedValue();
}
static void interactive(QScriptEngine *eng)
{
QScriptValue global = eng->globalObject();
if (!global.property(QLatin1String("dir")).isValid())
global.setProperty(QLatin1String("dir"), eng->newFunction(qtscript_dir));
if (!global.property(QLatin1String("load")).isValid())
global.setProperty(QLatin1String("load"), eng->newFunction(qtscript_load));
if (!global.property(QLatin1String("quit")).isValid())
global.setProperty(QLatin1String("quit"), eng->newFunction(qtscript_quit));
if (!global.property(QLatin1String("syncdb")).isValid())
global.setProperty(QLatin1String("syncdb"), eng->newFunction(qtscript_syncdb));
wantsToQuit = false;
QTextStream qin(stdin, QIODevice::ReadOnly);
const char *qscript_prompt = "qdjango> ";
const char *dot_prompt = ".... ";
const char *prompt = qscript_prompt;
QString code;
printf("Commands:\n"
"\tdir(obj) : print the object's properties\n"
"\tload() : loads a QtScript extension\n"
"\tquit() : exits console\n"
"\tsyncdb() : creates database tables\n");
forever {
QString line;
printf("%s", prompt);
fflush(stdout);
line = qin.readLine();
if (line.isNull())
break;
code += line;
code += QLatin1Char('\n');
if (line.trimmed().isEmpty()) {
continue;
} else if (! eng->canEvaluate(code)) {
prompt = dot_prompt;
} else {
QScriptValue result = eng->evaluate(code, QLatin1String("typein"));
code.clear();
prompt = qscript_prompt;
if (! result.isUndefined())
fprintf(stderr, "%s\n", qPrintable(result.toString()));
if (wantsToQuit)
break;
}
}
}
void usage()
{
fprintf(stderr, "Usage: qdjango-console [options]\n\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, "-d <database> use <database>\n");
fprintf(stderr, "-p <plugins> add <plugins> to plugins search path\n");
}
int main(int argc, char *argv[])
{
QString databaseName(":memory:");
/* Create application */
QCoreApplication app(argc, argv);
/* Parse command line arguments */
for (int i = 1; i < argc; i++)
{
if (!strcmp(argv[i], "-h"))
{
usage();
return EXIT_SUCCESS;
} else if (!strcmp(argv[i], "-d")) {
if (i == argc - 1 || !strlen(argv[i+1]) || argv[i+1][0] == '-')
{
fprintf(stderr, "Option -d requires an argument\n");
usage();
return EXIT_FAILURE;
}
databaseName = QString::fromLocal8Bit(argv[++i]);
} else if (!strcmp(argv[i], "-p")) {
if (i == argc - 1 || !strlen(argv[i+1]) || argv[i+1][0] == '-')
{
fprintf(stderr, "Option -p requires an argument\n");
usage();
return EXIT_FAILURE;
}
app.setLibraryPaths(app.libraryPaths() << QString::fromLocal8Bit(argv[++i]));
}
}
/* Open database */
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(databaseName);
if (!db.open())
{
fprintf(stderr, "Could not access database '%s'\n", databaseName.toLocal8Bit().constData());
return EXIT_FAILURE;
}
QDjango::setDatabase(db);
/* Run interactive shell */
QScriptEngine *engine = new QScriptEngine();
QDjangoScript::registerWhere(engine);
QDjangoScript::registerModel<User>(engine);
QDjangoScript::registerModel<Group>(engine);
QDjangoScript::registerModel<Message>(engine);
qDebug() << "Available extensions: " << engine->availableExtensions();
interactive(engine);
return EXIT_SUCCESS;
}
|