diff options
author | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-02-10 17:53:25 +0100 |
---|---|---|
committer | RaNaN <Mast3rRaNaN@hotmail.de> | 2011-02-10 17:53:25 +0100 |
commit | 6750a9481f44c55252d72b3c791f5efbcaeae71c (patch) | |
tree | 90011b409c2e1f2103c7b505a013600a2ddd9840 | |
parent | captcha trader fix (diff) | |
download | pyload-6750a9481f44c55252d72b3c791f5efbcaeae71c.tar.xz |
cleanup
33 files changed, 62 insertions, 2628 deletions
diff --git a/module/PluginThread.py b/module/PluginThread.py index 90ad9ec26..34cf46bc5 100644 --- a/module/PluginThread.py +++ b/module/PluginThread.py @@ -32,11 +32,11 @@ from os.path import exists from pycurl import error from utils import save_join +from module import PyFile from module.plugins.Plugin import Abort from module.plugins.Plugin import Fail from module.plugins.Plugin import Reconnect from module.plugins.Plugin import Retry -from module.FileDatabase import PyFile ######################################################################## class PluginThread(Thread): diff --git a/module/DatabaseBackend.py b/module/database/DatabaseBackend.py index 1c40f270b..2bc6c0bb2 100644 --- a/module/DatabaseBackend.py +++ b/module/database/DatabaseBackend.py @@ -27,7 +27,7 @@ from shutil import move from Queue import Queue from traceback import print_exc -from utils import chmod +from module.utils import chmod try: from pysqlite2 import dbapi2 as sqlite3 diff --git a/module/FileDatabase.py b/module/database/FileDatabase.py index 20548c136..46f23855e 100644 --- a/module/FileDatabase.py +++ b/module/database/FileDatabase.py @@ -17,12 +17,9 @@ @author: mkaay """ -from Queue import Queue -from threading import Lock + from threading import RLock -from threading import Thread from time import time -import traceback from module.PullEvents import InsertEvent from module.PullEvents import ReloadAllEvent @@ -33,8 +30,8 @@ from module.PyPackage import PyPackage from module.PyFile import PyFile from module.PyFile import formatSize -from module.DatabaseBackend import style -from module.DatabaseBackend import DatabaseBackend +from module.database import style +from module.database import DatabaseBackend try: from pysqlite2 import dbapi2 as sqlite3 diff --git a/module/StorageDatabase.py b/module/database/StorageDatabase.py index 9d3587340..dc3135c30 100644 --- a/module/StorageDatabase.py +++ b/module/database/StorageDatabase.py @@ -16,8 +16,8 @@ @author: mkaay """ -from module.DatabaseBackend import style -from module.DatabaseBackend import DatabaseBackend +from module.database import style +from module.database import DatabaseBackend class StorageMethods(): @style.queue diff --git a/module/UserDatabase.py b/module/database/UserDatabase.py index a69dfff0e..a69dfff0e 100644 --- a/module/UserDatabase.py +++ b/module/database/UserDatabase.py diff --git a/module/lib/thrift/protocol/fastbinary.c b/module/lib/thrift/protocol/fastbinary.c deleted file mode 100644 index 67b215a83..000000000 --- a/module/lib/thrift/protocol/fastbinary.c +++ /dev/null @@ -1,1203 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include <Python.h> -#include "cStringIO.h" -#include <stdbool.h> -#include <stdint.h> -#include <netinet/in.h> - -/* Fix endianness issues on Solaris */ -#if defined (__SVR4) && defined (__sun) - #if defined(__i386) && !defined(__i386__) - #define __i386__ - #endif - - #ifndef BIG_ENDIAN - #define BIG_ENDIAN (4321) - #endif - #ifndef LITTLE_ENDIAN - #define LITTLE_ENDIAN (1234) - #endif - - /* I386 is LE, even on Solaris */ - #if !defined(BYTE_ORDER) && defined(__i386__) - #define BYTE_ORDER LITTLE_ENDIAN - #endif -#endif - -// TODO(dreiss): defval appears to be unused. Look into removing it. -// TODO(dreiss): Make parse_spec_args recursive, and cache the output -// permanently in the object. (Malloc and orphan.) -// TODO(dreiss): Why do we need cStringIO for reading, why not just char*? -// Can cStringIO let us work with a BufferedTransport? -// TODO(dreiss): Don't ignore the rv from cwrite (maybe). - -/* ====== BEGIN UTILITIES ====== */ - -#define INIT_OUTBUF_SIZE 128 - -// Stolen out of TProtocol.h. -// It would be a huge pain to have both get this from one place. -typedef enum TType { - T_STOP = 0, - T_VOID = 1, - T_BOOL = 2, - T_BYTE = 3, - T_I08 = 3, - T_I16 = 6, - T_I32 = 8, - T_U64 = 9, - T_I64 = 10, - T_DOUBLE = 4, - T_STRING = 11, - T_UTF7 = 11, - T_STRUCT = 12, - T_MAP = 13, - T_SET = 14, - T_LIST = 15, - T_UTF8 = 16, - T_UTF16 = 17 -} TType; - -#ifndef __BYTE_ORDER -# if defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) -# define __BYTE_ORDER BYTE_ORDER -# define __LITTLE_ENDIAN LITTLE_ENDIAN -# define __BIG_ENDIAN BIG_ENDIAN -# else -# error "Cannot determine endianness" -# endif -#endif - -// Same comment as the enum. Sorry. -#if __BYTE_ORDER == __BIG_ENDIAN -# define ntohll(n) (n) -# define htonll(n) (n) -#elif __BYTE_ORDER == __LITTLE_ENDIAN -# if defined(__GNUC__) && defined(__GLIBC__) -# include <byteswap.h> -# define ntohll(n) bswap_64(n) -# define htonll(n) bswap_64(n) -# else /* GNUC & GLIBC */ -# define ntohll(n) ( (((unsigned long long)ntohl(n)) << 32) + ntohl(n >> 32) ) -# define htonll(n) ( (((unsigned long long)htonl(n)) << 32) + htonl(n >> 32) ) -# endif /* GNUC & GLIBC */ -#else /* __BYTE_ORDER */ -# error "Can't define htonll or ntohll!" -#endif - -// Doing a benchmark shows that interning actually makes a difference, amazingly. -#define INTERN_STRING(value) _intern_ ## value - -#define INT_CONV_ERROR_OCCURRED(v) ( ((v) == -1) && PyErr_Occurred() ) -#define CHECK_RANGE(v, min, max) ( ((v) <= (max)) && ((v) >= (min)) ) - -// Py_ssize_t was not defined before Python 2.5 -#if (PY_VERSION_HEX < 0x02050000) -typedef int Py_ssize_t; -#endif - -/** - * A cache of the spec_args for a set or list, - * so we don't have to keep calling PyTuple_GET_ITEM. - */ -typedef struct { - TType element_type; - PyObject* typeargs; -} SetListTypeArgs; - -/** - * A cache of the spec_args for a map, - * so we don't have to keep calling PyTuple_GET_ITEM. - */ -typedef struct { - TType ktag; - TType vtag; - PyObject* ktypeargs; - PyObject* vtypeargs; -} MapTypeArgs; - -/** - * A cache of the spec_args for a struct, - * so we don't have to keep calling PyTuple_GET_ITEM. - */ -typedef struct { - PyObject* klass; - PyObject* spec; -} StructTypeArgs; - -/** - * A cache of the item spec from a struct specification, - * so we don't have to keep calling PyTuple_GET_ITEM. - */ -typedef struct { - int tag; - TType type; - PyObject* attrname; - PyObject* typeargs; - PyObject* defval; -} StructItemSpec; - -/** - * A cache of the two key attributes of a CReadableTransport, - * so we don't have to keep calling PyObject_GetAttr. - */ -typedef struct { - PyObject* stringiobuf; - PyObject* refill_callable; -} DecodeBuffer; - -/** Pointer to interned string to speed up attribute lookup. */ -static PyObject* INTERN_STRING(cstringio_buf); -/** Pointer to interned string to speed up attribute lookup. */ -static PyObject* INTERN_STRING(cstringio_refill); - -static inline bool -check_ssize_t_32(Py_ssize_t len) { - // error from getting the int - if (INT_CONV_ERROR_OCCURRED(len)) { - return false; - } - if (!CHECK_RANGE(len, 0, INT32_MAX)) { - PyErr_SetString(PyExc_OverflowError, "string size out of range"); - return false; - } - return true; -} - -static inline bool -parse_pyint(PyObject* o, int32_t* ret, int32_t min, int32_t max) { - long val = PyInt_AsLong(o); - - if (INT_CONV_ERROR_OCCURRED(val)) { - return false; - } - if (!CHECK_RANGE(val, min, max)) { - PyErr_SetString(PyExc_OverflowError, "int out of range"); - return false; - } - - *ret = (int32_t) val; - return true; -} - - -/* --- FUNCTIONS TO PARSE STRUCT SPECIFICATOINS --- */ - -static bool -parse_set_list_args(SetListTypeArgs* dest, PyObject* typeargs) { - if (PyTuple_Size(typeargs) != 2) { - PyErr_SetString(PyExc_TypeError, "expecting tuple of size 2 for list/set type args"); - return false; - } - - dest->element_type = PyInt_AsLong(PyTuple_GET_ITEM(typeargs, 0)); - if (INT_CONV_ERROR_OCCURRED(dest->element_type)) { - return false; - } - - dest->typeargs = PyTuple_GET_ITEM(typeargs, 1); - - return true; -} - -static bool -parse_map_args(MapTypeArgs* dest, PyObject* typeargs) { - if (PyTuple_Size(typeargs) != 4) { - PyErr_SetString(PyExc_TypeError, "expecting 4 arguments for typeargs to map"); - return false; - } - - dest->ktag = PyInt_AsLong(PyTuple_GET_ITEM(typeargs, 0)); - if (INT_CONV_ERROR_OCCURRED(dest->ktag)) { - return false; - } - - dest->vtag = PyInt_AsLong(PyTuple_GET_ITEM(typeargs, 2)); - if (INT_CONV_ERROR_OCCURRED(dest->vtag)) { - return false; - } - - dest->ktypeargs = PyTuple_GET_ITEM(typeargs, 1); - dest->vtypeargs = PyTuple_GET_ITEM(typeargs, 3); - - return true; -} - -static bool -parse_struct_args(StructTypeArgs* dest, PyObject* typeargs) { - if (PyTuple_Size(typeargs) != 2) { - PyErr_SetString(PyExc_TypeError, "expecting tuple of size 2 for struct args"); - return false; - } - - dest->klass = PyTuple_GET_ITEM(typeargs, 0); - dest->spec = PyTuple_GET_ITEM(typeargs, 1); - - return true; -} - -static int -parse_struct_item_spec(StructItemSpec* dest, PyObject* spec_tuple) { - - // i'd like to use ParseArgs here, but it seems to be a bottleneck. - if (PyTuple_Size(spec_tuple) != 5) { - PyErr_SetString(PyExc_TypeError, "expecting 5 arguments for spec tuple"); - return false; - } - - dest->tag = PyInt_AsLong(PyTuple_GET_ITEM(spec_tuple, 0)); - if (INT_CONV_ERROR_OCCURRED(dest->tag)) { - return false; - } - - dest->type = PyInt_AsLong(PyTuple_GET_ITEM(spec_tuple, 1)); - if (INT_CONV_ERROR_OCCURRED(dest->type)) { - return false; - } - - dest->attrname = PyTuple_GET_ITEM(spec_tuple, 2); - dest->typeargs = PyTuple_GET_ITEM(spec_tuple, 3); - dest->defval = PyTuple_GET_ITEM(spec_tuple, 4); - return true; -} - -/* ====== END UTILITIES ====== */ - - -/* ====== BEGIN WRITING FUNCTIONS ====== */ - -/* --- LOW-LEVEL WRITING FUNCTIONS --- */ - -static void writeByte(PyObject* outbuf, int8_t val) { - int8_t net = val; - PycStringIO->cwrite(outbuf, (char*)&net, sizeof(int8_t)); -} - -static void writeI16(PyObject* outbuf, int16_t val) { - int16_t net = (int16_t)htons(val); - PycStringIO->cwrite(outbuf, (char*)&net, sizeof(int16_t)); -} - -static void writeI32(PyObject* outbuf, int32_t val) { - int32_t net = (int32_t)htonl(val); - PycStringIO->cwrite(outbuf, (char*)&net, sizeof(int32_t)); -} - -static void writeI64(PyObject* outbuf, int64_t val) { - int64_t net = (int64_t)htonll(val); - PycStringIO->cwrite(outbuf, (char*)&net, sizeof(int64_t)); -} - -static void writeDouble(PyObject* outbuf, double dub) { - // Unfortunately, bitwise_cast doesn't work in C. Bad C! - union { - double f; - int64_t t; - } transfer; - transfer.f = dub; - writeI64(outbuf, transfer.t); -} - - -/* --- MAIN RECURSIVE OUTPUT FUCNTION -- */ - -static int -output_val(PyObject* output, PyObject* value, TType type, PyObject* typeargs) { - /* - * Refcounting Strategy: - * - * We assume that elements of the thrift_spec tuple are not going to be - * mutated, so we don't ref count those at all. Other than that, we try to - * keep a reference to all the user-created objects while we work with them. - * output_val assumes that a reference is already held. The *caller* is - * responsible for handling references - */ - - switch (type) { - - case T_BOOL: { - int v = PyObject_IsTrue(value); - if (v == -1) { - return false; - } - - writeByte(output, (int8_t) v); - break; - } - case T_I08: { - int32_t val; - - if (!parse_pyint(value, &val, INT8_MIN, INT8_MAX)) { - return false; - } - - writeByte(output, (int8_t) val); - break; - } - case T_I16: { - int32_t val; - - if (!parse_pyint(value, &val, INT16_MIN, INT16_MAX)) { - return false; - } - - writeI16(output, (int16_t) val); - break; - } - case T_I32: { - int32_t val; - - if (!parse_pyint(value, &val, INT32_MIN, INT32_MAX)) { - return false; - } - - writeI32(output, val); - break; - } - case T_I64: { - int64_t nval = PyLong_AsLongLong(value); - - if (INT_CONV_ERROR_OCCURRED(nval)) { - return false; - } - - if (!CHECK_RANGE(nval, INT64_MIN, INT64_MAX)) { - PyErr_SetString(PyExc_OverflowError, "int out of range"); - return false; - } - - writeI64(output, nval); - break; - } - - case T_DOUBLE: { - double nval = PyFloat_AsDouble(value); - if (nval == -1.0 && PyErr_Occurred()) { - return false; - } - - writeDouble(output, nval); - break; - } - - case T_STRING: { - Py_ssize_t len = PyString_Size(value); - - if (!check_ssize_t_32(len)) { - return false; - } - - writeI32(output, (int32_t) len); - PycStringIO->cwrite(output, PyString_AsString(value), (int32_t) len); - break; - } - - case T_LIST: - case T_SET: { - Py_ssize_t len; - SetListTypeArgs parsedargs; - PyObject *item; - PyObject *iterator; - - if (!parse_set_list_args(&parsedargs, typeargs)) { - return false; - } - - len = PyObject_Length(value); - - if (!check_ssize_t_32(len)) { - return false; - } - - writeByte(output, parsedargs.element_type); - writeI32(output, (int32_t) len); - - iterator = PyObject_GetIter(value); - if (iterator == NULL) { - return false; - } - - while ((item = PyIter_Next(iterator))) { - if (!output_val(output, item, parsedargs.element_type, parsedargs.typeargs)) { - Py_DECREF(item); - Py_DECREF(iterator); - return false; - } - Py_DECREF(item); - } - - Py_DECREF(iterator); - - if (PyErr_Occurred()) { - return false; - } - - break; - } - - case T_MAP: { - PyObject *k, *v; - Py_ssize_t pos = 0; - Py_ssize_t len; - - MapTypeArgs parsedargs; - - len = PyDict_Size(value); - if (!check_ssize_t_32(len)) { - return false; - } - - if (!parse_map_args(&parsedargs, typeargs)) { - return false; - } - - writeByte(output, parsedargs.ktag); - writeByte(output, parsedargs.vtag); - writeI32(output, len); - - // TODO(bmaurer): should support any mapping, not just dicts - while (PyDict_Next(value, &pos, &k, &v)) { - // TODO(dreiss): Think hard about whether these INCREFs actually - // turn any unsafe scenarios into safe scenarios. - Py_INCREF(k); - Py_INCREF(v); - - if (!output_val(output, k, parsedargs.ktag, parsedargs.ktypeargs) - || !output_val(output, v, parsedargs.vtag, parsedargs.vtypeargs)) { - Py_DECREF(k); - Py_DECREF(v); - return false; - } - Py_DECREF(k); - Py_DECREF(v); - } - break; - } - - // TODO(dreiss): Consider breaking this out as a function - // the way we did for decode_struct. - case T_STRUCT: { - StructTypeArgs parsedargs; - Py_ssize_t nspec; - Py_ssize_t i; - - if (!parse_struct_args(&parsedargs, typeargs)) { - return false; - } - - nspec = PyTuple_Size(parsedargs.spec); - - if (nspec == -1) { - return false; - } - - for (i = 0; i < nspec; i++) { - StructItemSpec parsedspec; - PyObject* spec_tuple; - PyObject* instval = NULL; - - spec_tuple = PyTuple_GET_ITEM(parsedargs.spec, i); - if (spec_tuple == Py_None) { - continue; - } - - if (!parse_struct_item_spec (&parsedspec, spec_tuple)) { - return false; - } - - instval = PyObject_GetAttr(value, parsedspec.attrname); - - if (!instval) { - return false; - } - - if (instval == Py_None) { - Py_DECREF(instval); - continue; - } - - writeByte(output, (int8_t) parsedspec.type); - writeI16(output, parsedspec.tag); - - if (!output_val(output, instval, parsedspec.type, parsedspec.typeargs)) { - Py_DECREF(instval); - return false; - } - - Py_DECREF(instval); - } - - writeByte(output, (int8_t)T_STOP); - break; - } - - case T_STOP: - case T_VOID: - case T_UTF16: - case T_UTF8: - case T_U64: - default: - PyErr_SetString(PyExc_TypeError, "Unexpected TType"); - return false; - - } - - return true; -} - - -/* --- TOP-LEVEL WRAPPER FOR OUTPUT -- */ - -static PyObject * -encode_binary(PyObject *self, PyObject *args) { - PyObject* enc_obj; - PyObject* type_args; - PyObject* buf; - PyObject* ret = NULL; - - if (!PyArg_ParseTuple(args, "OO", &enc_obj, &type_args)) { - return NULL; - } - - buf = PycStringIO->NewOutput(INIT_OUTBUF_SIZE); - if (output_val(buf, enc_obj, T_STRUCT, type_args)) { - ret = PycStringIO->cgetvalue(buf); - } - - Py_DECREF(buf); - return ret; -} - -/* ====== END WRITING FUNCTIONS ====== */ - - -/* ====== BEGIN READING FUNCTIONS ====== */ - -/* --- LOW-LEVEL READING FUNCTIONS --- */ - -static void -free_decodebuf(DecodeBuffer* d) { - Py_XDECREF(d->stringiobuf); - Py_XDECREF(d->refill_callable); -} - -static bool -decode_buffer_from_obj(DecodeBuffer* dest, PyObject* obj) { - dest->stringiobuf = PyObject_GetAttr(obj, INTERN_STRING(cstringio_buf)); - if (!dest->stringiobuf) { - return false; - } - - if (!PycStringIO_InputCheck(dest->stringiobuf)) { - free_decodebuf(dest); - PyErr_SetString(PyExc_TypeError, "expecting stringio input"); - return false; - } - - dest->refill_callable = PyObject_GetAttr(obj, INTERN_STRING(cstringio_refill)); - - if(!dest->refill_callable) { - free_decodebuf(dest); - return false; - } - - if (!PyCallable_Check(dest->refill_callable)) { - free_decodebuf(dest); - PyErr_SetString(PyExc_TypeError, "expecting callable"); - return false; - } - - return true; -} - -static bool readBytes(DecodeBuffer* input, char** output, int len) { - int read; - - // TODO(dreiss): Don't fear the malloc. Think about taking a copy of - // the partial read instead of forcing the transport - // to prepend it to its buffer. - - read = PycStringIO->cread(input->stringiobuf, output, len); - - if (read == len) { - return true; - } else if (read == -1) { - return false; - } else { - PyObject* newiobuf; - - // using building functions as this is a rare codepath - newiobuf = PyObject_CallFunction( - input->refill_callable, "s#i", *output, read, len, NULL); - if (newiobuf == NULL) { - return false; - } - - // must do this *AFTER* the call so that we don't deref the io buffer - Py_CLEAR(input->stringiobuf); - input->stringiobuf = newiobuf; - - read = PycStringIO->cread(input->stringiobuf, output, len); - - if (read == len) { - return true; - } else if (read == -1) { - return false; - } else { - // TODO(dreiss): This could be a valid code path for big binary blobs. - PyErr_SetString(PyExc_TypeError, - "refill claimed to have refilled the buffer, but didn't!!"); - return false; - } - } -} - -static int8_t readByte(DecodeBuffer* input) { - char* buf; - if (!readBytes(input, &buf, sizeof(int8_t))) { - return -1; - } - - return *(int8_t*) buf; -} - -static int16_t readI16(DecodeBuffer* input) { - char* buf; - if (!readBytes(input, &buf, sizeof(int16_t))) { - return -1; - } - - return (int16_t) ntohs(*(int16_t*) buf); -} - -static int32_t readI32(DecodeBuffer* input) { - char* buf; - if (!readBytes(input, &buf, sizeof(int32_t))) { - return -1; - } - return (int32_t) ntohl(*(int32_t*) buf); -} - - -static int64_t readI64(DecodeBuffer* input) { - char* buf; - if (!readBytes(input, &buf, sizeof(int64_t))) { - return -1; - } - - return (int64_t) ntohll(*(int64_t*) buf); -} - -static double readDouble(DecodeBuffer* input) { - union { - int64_t f; - double t; - } transfer; - - transfer.f = readI64(input); - if (transfer.f == -1) { - return -1; - } - return transfer.t; -} - -static bool -checkTypeByte(DecodeBuffer* input, TType expected) { - TType got = readByte(input); - if (INT_CONV_ERROR_OCCURRED(got)) { - return false; - } - - if (expected != got) { - PyErr_SetString(PyExc_TypeError, "got wrong ttype while reading field"); - return false; - } - return true; -} - -static bool -skip(DecodeBuffer* input, TType type) { -#define SKIPBYTES(n) \ - do { \ - if (!readBytes(input, &dummy_buf, (n))) { \ - return false; \ - } \ - } while(0) - - char* dummy_buf; - - switch (type) { - - case T_BOOL: - case T_I08: SKIPBYTES(1); break; - case T_I16: SKIPBYTES(2); break; - case T_I32: SKIPBYTES(4); break; - case T_I64: - case T_DOUBLE: SKIPBYTES(8); break; - - case T_STRING: { - // TODO(dreiss): Find out if these check_ssize_t32s are really necessary. - int len = readI32(input); - if (!check_ssize_t_32(len)) { - return false; - } - SKIPBYTES(len); - break; - } - - case T_LIST: - case T_SET: { - TType etype; - int len, i; - - etype = readByte(input); - if (etype == -1) { - return false; - } - - len = readI32(input); - if (!check_ssize_t_32(len)) { - return false; - } - - for (i = 0; i < len; i++) { - if (!skip(input, etype)) { - return false; - } - } - break; - } - - case T_MAP: { - TType ktype, vtype; - int len, i; - - ktype = readByte(input); - if (ktype == -1) { - return false; - } - - vtype = readByte(input); - if (vtype == -1) { - return false; - } - - len = readI32(input); - if (!check_ssize_t_32(len)) { - return false; - } - - for (i = 0; i < len; i++) { - if (!(skip(input, ktype) && skip(input, vtype))) { - return false; - } - } - break; - } - - case T_STRUCT: { - while (true) { - TType type; - - type = readByte(input); - if (type == -1) { - return false; - } - - if (type == T_STOP) - break; - - SKIPBYTES(2); // tag - if (!skip(input, type)) { - return false; - } - } - break; - } - - case T_STOP: - case T_VOID: - case T_UTF16: - case T_UTF8: - case T_U64: - default: - PyErr_SetString(PyExc_TypeError, "Unexpected TType"); - return false; - - } - - return true; - -#undef SKIPBYTES -} - - -/* --- HELPER FUNCTION FOR DECODE_VAL --- */ - -static PyObject* -decode_val(DecodeBuffer* input, TType type, PyObject* typeargs); - -static bool -decode_struct(DecodeBuffer* input, PyObject* output, PyObject* spec_seq) { - int spec_seq_len = PyTuple_Size(spec_seq); - if (spec_seq_len == -1) { - return false; - } - - while (true) { - TType type; - int16_t tag; - PyObject* item_spec; - PyObject* fieldval = NULL; - StructItemSpec parsedspec; - - type = readByte(input); - if (type == -1) { - return false; - } - if (type == T_STOP) { - break; - } - tag = readI16(input); - if (INT_CONV_ERROR_OCCURRED(tag)) { - return false; - } - if (tag >= 0 && tag < spec_seq_len) { - item_spec = PyTuple_GET_ITEM(spec_seq, tag); - } else { - item_spec = Py_None; - } - - if (item_spec == Py_None) { - if (!skip(input, type)) { - return false; - } else { - continue; - } - } - - if (!parse_struct_item_spec(&parsedspec, item_spec)) { - return false; - } - if (parsedspec.type != type) { - if (!skip(input, type)) { - PyErr_SetString(PyExc_TypeError, "struct field had wrong type while reading and can't be skipped"); - return false; - } else { - continue; - } - } - - fieldval = decode_val(input, parsedspec.type, parsedspec.typeargs); - if (fieldval == NULL) { - return false; - } - - if (PyObject_SetAttr(output, parsedspec.attrname, fieldval) == -1) { - Py_DECREF(fieldval); - return false; - } - Py_DECREF(fieldval); - } - return true; -} - - -/* --- MAIN RECURSIVE INPUT FUCNTION --- */ - -// Returns a new reference. -static PyObject* -decode_val(DecodeBuffer* input, TType type, PyObject* typeargs) { - switch (type) { - - case T_BOOL: { - int8_t v = readByte(input); - if (INT_CONV_ERROR_OCCURRED(v)) { - return NULL; - } - - switch (v) { - case 0: Py_RETURN_FALSE; - case 1: Py_RETURN_TRUE; - // Don't laugh. This is a potentially serious issue. - default: PyErr_SetString(PyExc_TypeError, "boolean out of range"); return NULL; - } - break; - } - case T_I08: { - int8_t v = readByte(input); - if (INT_CONV_ERROR_OCCURRED(v)) { - return NULL; - } - - return PyInt_FromLong(v); - } - case T_I16: { - int16_t v = readI16(input); - if (INT_CONV_ERROR_OCCURRED(v)) { - return NULL; - } - return PyInt_FromLong(v); - } - case T_I32: { - int32_t v = readI32(input); - if (INT_CONV_ERROR_OCCURRED(v)) { - return NULL; - } - return PyInt_FromLong(v); - } - - case T_I64: { - int64_t v = readI64(input); - if (INT_CONV_ERROR_OCCURRED(v)) { - return NULL; - } - // TODO(dreiss): Find out if we can take this fastpath always when - // sizeof(long) == sizeof(long long). - if (CHECK_RANGE(v, LONG_MIN, LONG_MAX)) { - return PyInt_FromLong((long) v); - } - - return PyLong_FromLongLong(v); - } - - case T_DOUBLE: { - double v = readDouble(input); - if (v == -1.0 && PyErr_Occurred()) { - return false; - } - return PyFloat_FromDouble(v); - } - - case T_STRING: { - Py_ssize_t len = readI32(input); - char* buf; - if (!readBytes(input, &buf, len)) { - return NULL; - } - - return PyString_FromStringAndSize(buf, len); - } - - case T_LIST: - case T_SET: { - SetListTypeArgs parsedargs; - int32_t len; - PyObject* ret = NULL; - int i; - - if (!parse_set_list_args(&parsedargs, typeargs)) { - return NULL; - } - - if (!checkTypeByte(input, parsedargs.element_type)) { - return NULL; - } - - len = readI32(input); - if (!check_ssize_t_32(len)) { - return NULL; - } - - ret = PyList_New(len); - if (!ret) { - return NULL; - } - - for (i = 0; i < len; i++) { - PyObject* item = decode_val(input, parsedargs.element_type, parsedargs.typeargs); - if (!item) { - Py_DECREF(ret); - return NULL; - } - PyList_SET_ITEM(ret, i, item); - } - - // TODO(dreiss): Consider biting the bullet and making two separate cases - // for list and set, avoiding this post facto conversion. - if (type == T_SET) { - PyObject* setret; -#if (PY_VERSION_HEX < 0x02050000) - // hack needed for older versions - setret = PyObject_CallFunctionObjArgs((PyObject*)&PySet_Type, ret, NULL); -#else - // official version - setret = PySet_New(ret); -#endif - Py_DECREF(ret); - return setret; - } - return ret; - } - - case T_MAP: { - int32_t len; - int i; - MapTypeArgs parsedargs; - PyObject* ret = NULL; - - if (!parse_map_args(&parsedargs, typeargs)) { - return NULL; - } - - if (!checkTypeByte(input, parsedargs.ktag)) { - return NULL; - } - if (!checkTypeByte(input, parsedargs.vtag)) { - return NULL; - } - - len = readI32(input); - if (!check_ssize_t_32(len)) { - return false; - } - - ret = PyDict_New(); - if (!ret) { - goto error; - } - - for (i = 0; i < len; i++) { - PyObject* k = NULL; - PyObject* v = NULL; - k = decode_val(input, parsedargs.ktag, parsedargs.ktypeargs); - if (k == NULL) { - goto loop_error; - } - v = decode_val(input, parsedargs.vtag, parsedargs.vtypeargs); - if (v == NULL) { - goto loop_error; - } - if (PyDict_SetItem(ret, k, v) == -1) { - goto loop_error; - } - - Py_DECREF(k); - Py_DECREF(v); - continue; - - // Yuck! Destructors, anyone? - loop_error: - Py_XDECREF(k); - Py_XDECREF(v); - goto error; - } - - return ret; - - error: - Py_XDECREF(ret); - return NULL; - } - - case T_STRUCT: { - StructTypeArgs parsedargs; - if (!parse_struct_args(&parsedargs, typeargs)) { - return NULL; - } - - PyObject* ret = PyObject_CallObject(parsedargs.klass, NULL); - if (!ret) { - return NULL; - } - - if (!decode_struct(input, ret, parsedargs.spec)) { - Py_DECREF(ret); - return NULL; - } - - return ret; - } - - case T_STOP: - case T_VOID: - case T_UTF16: - case T_UTF8: - case T_U64: - default: - PyErr_SetString(PyExc_TypeError, "Unexpected TType"); - return NULL; - } -} - - -/* --- TOP-LEVEL WRAPPER FOR INPUT -- */ - -static PyObject* -decode_binary(PyObject *self, PyObject *args) { - PyObject* output_obj = NULL; - PyObject* transport = NULL; - PyObject* typeargs = NULL; - StructTypeArgs parsedargs; - DecodeBuffer input = {}; - - if (!PyArg_ParseTuple(args, "OOO", &output_obj, &transport, &typeargs)) { - return NULL; - } - - if (!parse_struct_args(&parsedargs, typeargs)) { - return NULL; - } - - if (!decode_buffer_from_obj(&input, transport)) { - return NULL; - } - - if (!decode_struct(&input, output_obj, parsedargs.spec)) { - free_decodebuf(&input); - return NULL; - } - - free_decodebuf(&input); - - Py_RETURN_NONE; -} - -/* ====== END READING FUNCTIONS ====== */ - - -/* -- PYTHON MODULE SETUP STUFF --- */ - -static PyMethodDef ThriftFastBinaryMethods[] = { - - {"encode_binary", encode_binary, METH_VARARGS, ""}, - {"decode_binary", decode_binary, METH_VARARGS, ""}, - - {NULL, NULL, 0, NULL} /* Sentinel */ -}; - -PyMODINIT_FUNC -initfastbinary(void) { -#define INIT_INTERN_STRING(value) \ - do { \ - INTERN_STRING(value) = PyString_InternFromString(#value); \ - if(!INTERN_STRING(value)) return; \ - } while(0) - - INIT_INTERN_STRING(cstringio_buf); - INIT_INTERN_STRING(cstringio_refill); -#undef INIT_INTERN_STRING - - PycString_IMPORT; - if (PycStringIO == NULL) return; - - (void) Py_InitModule("thrift.protocol.fastbinary", ThriftFastBinaryMethods); -} diff --git a/module/remote/pyload.thrift b/module/remote/pyload.thrift index 413a841d7..4fb054cd1 100644 --- a/module/remote/pyload.thrift +++ b/module/remote/pyload.thrift @@ -28,26 +28,6 @@ enum Destination { Collector } -enum CaptchaStatus { - Init, - Waiting, - User, - SharedUser, - Done -} - -enum ConfigItemType { - String, - Password, - Choice, - Bool, - Integer, - IP, - File, - Folder, - Time -} - enum ElementType { Package, File @@ -95,7 +75,7 @@ struct ConfigItem { 1: string name, 2: string description, 3: string value, - 4: ConfigItemType type, + 4: string type, 5: optional set<string> choice } @@ -133,7 +113,7 @@ struct PackageData { } struct CaptchaTask { - 1: TaskID tid, + 1: i16 tid, 2: binary data, 3: string type } @@ -222,7 +202,7 @@ service Pyload { //captcha bool isCaptchaWaiting(), CaptchaTask getCaptchaTask(1: bool exclusive), - CaptchaStatus getCaptchaTaskStatus(1: TaskID tid), + string getCaptchaTaskStatus(1: TaskID tid), void setCaptchaResult(1: TaskID tid, 2: string result), //events diff --git a/module/remote/thriftgen/pyload/Pyload-remote b/module/remote/thriftgen/pyload/Pyload-remote index 67edb2b71..cd8a1d703 100755 --- a/module/remote/thriftgen/pyload/Pyload-remote +++ b/module/remote/thriftgen/pyload/Pyload-remote @@ -66,7 +66,7 @@ if len(sys.argv) <= 1 or sys.argv[1] == '--help': print ' getFileOrder(PackageID pid)' print ' bool isCaptchaWaiting()' print ' CaptchaTask getCaptchaTask(bool exclusive)' - print ' CaptchaStatus getCaptchaTaskStatus(TaskID tid)' + print ' string getCaptchaTaskStatus(TaskID tid)' print ' void setCaptchaResult(TaskID tid, string result)' print ' getEvents()' print ' getAccounts()' diff --git a/module/remote/thriftgen/pyload/Pyload.py b/module/remote/thriftgen/pyload/Pyload.py index b4912a580..032c1baaa 100644 --- a/module/remote/thriftgen/pyload/Pyload.py +++ b/module/remote/thriftgen/pyload/Pyload.py @@ -7344,7 +7344,7 @@ class getCaptchaTaskStatus_result: """ thrift_spec = ( - (0, TType.I32, 'success', None, None, ), # 0 + (0, TType.STRING, 'success', None, None, ), # 0 ) def __init__(self, success=None,): @@ -7360,8 +7360,8 @@ class getCaptchaTaskStatus_result: if ftype == TType.STOP: break if fid == 0: - if ftype == TType.I32: - self.success = iprot.readI32(); + if ftype == TType.STRING: + self.success = iprot.readString(); else: iprot.skip(ftype) else: @@ -7375,8 +7375,8 @@ class getCaptchaTaskStatus_result: return oprot.writeStructBegin('getCaptchaTaskStatus_result') if self.success != None: - oprot.writeFieldBegin('success', TType.I32, 0) - oprot.writeI32(self.success) + oprot.writeFieldBegin('success', TType.STRING, 0) + oprot.writeString(self.success) oprot.writeFieldEnd() oprot.writeFieldStop() oprot.writeStructEnd() diff --git a/module/remote/thriftgen/pyload/ttypes.py b/module/remote/thriftgen/pyload/ttypes.py index 96d2fd7aa..6c5642e60 100644 --- a/module/remote/thriftgen/pyload/ttypes.py +++ b/module/remote/thriftgen/pyload/ttypes.py @@ -81,64 +81,6 @@ class Destination: "Collector": 1, } -class CaptchaStatus: - Init = 0 - Waiting = 1 - User = 2 - SharedUser = 3 - Done = 4 - - _VALUES_TO_NAMES = { - 0: "Init", - 1: "Waiting", - 2: "User", - 3: "SharedUser", - 4: "Done", - } - - _NAMES_TO_VALUES = { - "Init": 0, - "Waiting": 1, - "User": 2, - "SharedUser": 3, - "Done": 4, - } - -class ConfigItemType: - String = 0 - Password = 1 - Choice = 2 - Bool = 3 - Integer = 4 - IP = 5 - File = 6 - Folder = 7 - Time = 8 - - _VALUES_TO_NAMES = { - 0: "String", - 1: "Password", - 2: "Choice", - 3: "Bool", - 4: "Integer", - 5: "IP", - 6: "File", - 7: "Folder", - 8: "Time", - } - - _NAMES_TO_VALUES = { - "String": 0, - "Password": 1, - "Choice": 2, - "Bool": 3, - "Integer": 4, - "IP": 5, - "File": 6, - "Folder": 7, - "Time": 8, - } - class ElementType: Package = 0 File = 1 @@ -556,7 +498,7 @@ class ConfigItem: (1, TType.STRING, 'name', None, None, ), # 1 (2, TType.STRING, 'description', None, None, ), # 2 (3, TType.STRING, 'value', None, None, ), # 3 - (4, TType.I32, 'type', None, None, ), # 4 + (4, TType.STRING, 'type', None, None, ), # 4 (5, TType.SET, 'choice', (TType.STRING,None), None, ), # 5 ) @@ -592,8 +534,8 @@ class ConfigItem: else: iprot.skip(ftype) elif fid == 4: - if ftype == TType.I32: - self.type = iprot.readI32(); + if ftype == TType.STRING: + self.type = iprot.readString(); else: iprot.skip(ftype) elif fid == 5: @@ -629,8 +571,8 @@ class ConfigItem: oprot.writeString(self.value) oprot.writeFieldEnd() if self.type != None: - oprot.writeFieldBegin('type', TType.I32, 4) - oprot.writeI32(self.type) + oprot.writeFieldBegin('type', TType.STRING, 4) + oprot.writeString(self.type) oprot.writeFieldEnd() if self.choice != None: oprot.writeFieldBegin('choice', TType.SET, 5) @@ -1113,7 +1055,7 @@ class CaptchaTask: thrift_spec = ( None, # 0 - (1, TType.I32, 'tid', None, None, ), # 1 + (1, TType.I16, 'tid', None, None, ), # 1 (2, TType.STRING, 'data', None, None, ), # 2 (3, TType.STRING, 'type', None, None, ), # 3 ) @@ -1133,8 +1075,8 @@ class CaptchaTask: if ftype == TType.STOP: break if fid == 1: - if ftype == TType.I32: - self.tid = iprot.readI32(); + if ftype == TType.I16: + self.tid = iprot.readI16(); else: iprot.skip(ftype) elif fid == 2: @@ -1158,8 +1100,8 @@ class CaptchaTask: return oprot.writeStructBegin('CaptchaTask') if self.tid != None: - oprot.writeFieldBegin('tid', TType.I32, 1) - oprot.writeI32(self.tid) + oprot.writeFieldBegin('tid', TType.I16, 1) + oprot.writeI16(self.tid) oprot.writeFieldEnd() if self.data != None: oprot.writeFieldBegin('data', TType.STRING, 2) diff --git a/module/web/ajax/__init__.py b/module/web/ajax/__init__.py deleted file mode 100644 index e69de29bb..000000000 --- a/module/web/ajax/__init__.py +++ /dev/null diff --git a/module/web/ajax/models.py b/module/web/ajax/models.py deleted file mode 100644 index 35e0d6486..000000000 --- a/module/web/ajax/models.py +++ /dev/null @@ -1,2 +0,0 @@ - -# Create your models here. diff --git a/module/web/ajax/tests.py b/module/web/ajax/tests.py deleted file mode 100644 index 2247054b3..000000000 --- a/module/web/ajax/tests.py +++ /dev/null @@ -1,23 +0,0 @@ -""" -This file demonstrates two different styles of tests (one doctest and one -unittest). These will both pass when you run "manage.py test". - -Replace these with more appropriate tests for your application. -""" - -from django.test import TestCase - -class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.failUnlessEqual(1 + 1, 2) - -__test__ = {"doctest": """ -Another way to test that 1 + 1 is equal to 2. - ->>> 1 + 1 == 2 -True -"""} - diff --git a/module/web/ajax/urls.py b/module/web/ajax/urls.py deleted file mode 100644 index ebbf401af..000000000 --- a/module/web/ajax/urls.py +++ /dev/null @@ -1,36 +0,0 @@ -# -*- coding: utf-8 -*- -from django.conf.urls.defaults import * - - -urlpatterns = patterns('ajax', - # Example: - # (r'^pyload/', include('pyload.foo.urls')), - - # Uncomment the admin/doc line below and add 'django.contrib.admindocs' - # to INSTALLED_APPS to enable admin documentation: - # (r'^admin/doc/', include('django.contrib.admindocs.urls')), - - # Uncomment the next line to enable the admin: - (r'^add_package$', 'views.add_package'), - (r'^abort_link/(\d+)$', 'views.abort_link'), - (r'^status$', 'views.status'), - (r'^links$', 'views.links'), #currently active links - (r'^queue$', 'views.queue'), - (r'^pause$', 'views.pause'), - (r'^unpause$', 'views.unpause'), - (r'^cancel$', 'views.cancel'), - (r'^packages$', 'views.packages'), - (r'^package/(\d+)$', 'views.package'), - (r'^link/(\d+)$', 'views.link'), - (r'^remove_package/(\d+)$', 'views.remove_package'), - (r'^restart_package/(\d+)$', 'views.restart_package'), - (r'^remove_link/(\d+)$', 'views.remove_link'), - (r'^restart_link/(\d+)$', 'views.restart_link'), - (r'^move_package/(\d+)/(\d+)$', 'views.move_package'), - (r'^set_captcha$', 'views.set_captcha'), - (r'^package_order/([0-9|]+)$', 'views.package_order'), - (r'^link_order/([0-9|]+)$', 'views.link_order'), - (r'edit_package$', 'views.edit_package'), - (r'restart_failed$', 'views.restart_failed'), - (r'delete_finished$', 'views.delete_finished'), - )
\ No newline at end of file diff --git a/module/web/ajax/views.py b/module/web/ajax/views.py deleted file mode 100644 index acceac585..000000000 --- a/module/web/ajax/views.py +++ /dev/null @@ -1,321 +0,0 @@ -# Create your views here. -from os.path import join -import time - -from django.conf import settings -from django.core.serializers import json -from django.http import HttpResponse -from django.http import HttpResponseForbidden -from django.http import HttpResponseServerError -from django.utils import simplejson -from django.utils.translation import ugettext as _ -import base64 - -from traceback import print_exc - -def format_time(seconds): - seconds = int(seconds) - - hours, seconds = divmod(seconds, 3600) - minutes, seconds = divmod(seconds, 60) - return "%.2i:%.2i:%.2i" % (hours, minutes, seconds) - -def get_sort_key(item): - return item["order"] - -def permission(perm): - def _dec(view_func): - def _view(request, * args, ** kwargs): - if request.user.has_perm(perm) and request.user.is_authenticated(): - return view_func(request, * args, ** kwargs) - else: - return HttpResponseForbidden() - - _view.__name__ = view_func.__name__ - _view.__dict__ = view_func.__dict__ - _view.__doc__ = view_func.__doc__ - - return _view - - return _dec - -class JsonResponse(HttpResponse): - def __init__(self, object): - content = simplejson.dumps( - object, indent=2, cls=json.DjangoJSONEncoder, - ensure_ascii=False) - super(JsonResponse, self).__init__( - content)#, content_type='application/json') #@TODO uncomment - self['Cache-Control'] = 'no-cache, must-revalidate' - - -@permission('pyload.can_add') -def add_package(request): - - name = request.POST['add_name'] - queue = int(request.POST['add_dest']) - links = request.POST['add_links'].split("\n") - pw = request.POST.get("add_password", "").strip("\n\r") - - try: - f = request.FILES['add_file'] - - if name is None or name == "": - name = f.name - - fpath = join(settings.PYLOAD.get_conf_val("general","download_folder"), "tmp_"+ f.name) - destination = open(fpath, 'wb') - for chunk in f.chunks(): - destination.write(chunk) - destination.close() - links.insert(0, fpath) - except: - pass - - if name is None or name == "": - return HttpResponseServerError() - - links = map(lambda x: x.strip(), links) - links = filter(lambda x: x != "", links) - - pack = settings.PYLOAD.add_package(name, links, queue) - if pw: - data = {"password": pw} - settings.PYLOAD.set_package_data(pack, data) - - return JsonResponse("success") - -@permission('pyload.can_delete') -def remove_link(request, id): - try: - settings.PYLOAD.del_links([int(id)]) - return JsonResponse("sucess") - except Exception, e: - return HttpResponseServerError() - -@permission('pyload.can_see_dl') -def status(request): - try: - status = settings.PYLOAD.status_server() - status['captcha'] = settings.PYLOAD.is_captcha_waiting() - return JsonResponse(status) - except: - return HttpResponseServerError() - -@permission('pyload.can_see_dl') -def links(request): - try: - links = settings.PYLOAD.status_downloads() - ids = [] - for link in links: - ids.append(link['id']) - - if link['status'] == 12: - link['info'] = "%s @ %s kb/s" % (link['format_eta'], round(link['speed'], 2)) - elif link['status'] == 5: - link['percent'] = 0 - link['size'] = 0 - link['kbleft'] = 0 - link['info'] = _("waiting %s") % link['format_wait'] - else: - link['info'] = "" - - - data = {} - data['links'] = links - data['ids'] = ids - return JsonResponse(data) - except Exception, e: - return HttpResponseServerError() - -@permission('pyload.can_see_dl') -def queue(request): - try: - return JsonResponse(settings.PYLOAD.get_queue()) - - except: - return HttpResponseServerError() - - -@permission('pyload.can_change_satus') -def pause(request): - try: - return JsonResponse(settings.PYLOAD.pause_server()) - - except: - return HttpResponseServerError() - - -@permission('pyload.can_change_status') -def unpause(request): - try: - return JsonResponse(settings.PYLOAD.unpause_server()) - - except: - return HttpResponseServerError() - - -@permission('pyload.can_change_status') -def cancel(request): - try: - return JsonResponse(settings.PYLOAD.stop_downloads()) - except: - return HttpResponseServerError() - -@permission('pyload.can_see_dl') -def packages(request): - try: - data = settings.PYLOAD.get_queue() - - for package in data: - package['links'] = [] - for file in settings.PYLOAD.get_package_files(package['id']): - package['links'].append(settings.PYLOAD.get_file_info(file)) - - return JsonResponse(data) - - except: - return HttpResponseServerError() - -@permission('pyload.can_see_dl') -def package(request, id): - try: - data = settings.PYLOAD.get_package_data(int(id)) - - for pyfile in data["links"].itervalues(): - if pyfile["status"] == 0: - pyfile["icon"] = "status_finished.png" - elif pyfile["status"] in (2,3): - pyfile["icon"] = "status_queue.png" - elif pyfile["status"] in (9,1): - pyfile["icon"] = "status_offline.png" - elif pyfile["status"] == 5: - pyfile["icon"] = "status_waiting.png" - elif pyfile["status"] == 8: - pyfile["icon"] = "status_failed.png" - elif pyfile["status"] in (11,13): - pyfile["icon"] = "status_proc.png" - else: - pyfile["icon"] = "status_downloading.png" - - tmp = data["links"].values() - tmp.sort(key=get_sort_key) - data["links"] = tmp - - return JsonResponse(data) - - except: - print_exc() - return HttpResponseServerError() - -@permission('pyload.can_add') -def package_order(request, ids): - try: - pid, pos = ids.split("|") - settings.PYLOAD.order_package(int(pid), int(pos)) - return JsonResponse("success") - except: - print_exc() - return HttpResponseServerError() - -@permission('pyload.can_add') -def link_order(request, ids): - try: - pid, pos = ids.split("|") - settings.PYLOAD.order_file(int(pid), int(pos)) - return JsonResponse("success") - except: - print_exc() - return HttpResponseServerError() - -@permission('pyload.can_see_dl') -def link(request, id): - try: - data = settings.PYLOAD.get_file_info(int(id)) - return JsonResponse(data) - - except: - return HttpResponseServerError() - -@permission('pyload.can_delete') -def remove_package(request, id): - try: - settings.PYLOAD.del_packages([int(id)]) - return JsonResponse("success") - except Exception, e: - print_exc() - return HttpResponseServerError() - -@permission('pyload.can_add') -def restart_package(request, id): - try: - settings.PYLOAD.restart_package(int(id)) - return JsonResponse("success") - except Exception: - return HttpResponseServerError() - -@permission('pyload.can_add') -def restart_link(request, id): - try: - settings.PYLOAD.restart_file(int(id)) - return JsonResponse("success") - except Exception: - return HttpResponseServerError() - -@permission('pyload.can_delete') -def abort_link(request, id): - try: - settings.PYLOAD.stop_download("link", int(id)) - return JsonResponse("success") - except: - return HttpResponseServerError() - -@permission('pyload.can_add') -def move_package(request, dest, id): - try: - settings.PYLOAD.move_package(int(dest), int(id)) - return JsonResponse("success") - except: - return HttpResponseServerError() - -@permission('pyload.can_add') -def edit_package(request): - try: - id = int(request.POST.get("pack_id")) - data = {"name": request.POST.get("pack_name"), - "folder": request.POST.get("pack_folder"), - "priority": request.POST.get("pack_prio"), - "password": request.POST.get("pack_pws")} - - settings.PYLOAD.set_package_data(id, data) - return JsonResponse("success") - - except: - return HttpResponseServerError() - -@permission('pyload.can_add') -def set_captcha(request): - if request.META['REQUEST_METHOD'] == "POST": - try: - settings.PYLOAD.set_captcha_result(request.POST["cap_id"], request.POST["cap_text"]) - except: - pass - - id, binary, typ = settings.PYLOAD.get_captcha_task() - - if id: - binary = base64.standard_b64encode(str(binary)) - src = "data:image/%s;base64,%s" % (typ, binary) - - return JsonResponse({'captcha': True, 'src': src, 'id': id}) - else: - return JsonResponse({'captcha': False}) - - -@permission('pyload.can_delete') -def delete_finished(request): - return JsonResponse({"del":settings.PYLOAD.delete_finished()}) - -@permission('pyload.can_delete') -def restart_failed(request): - return JsonResponse(settings.PYLOAD.restart_failed())
\ No newline at end of file diff --git a/module/web/cnl/__init__.py b/module/web/cnl/__init__.py deleted file mode 100644 index e69de29bb..000000000 --- a/module/web/cnl/__init__.py +++ /dev/null diff --git a/module/web/cnl/models.py b/module/web/cnl/models.py deleted file mode 100644 index 71a836239..000000000 --- a/module/web/cnl/models.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.db import models - -# Create your models here. diff --git a/module/web/cnl/tests.py b/module/web/cnl/tests.py deleted file mode 100644 index 2247054b3..000000000 --- a/module/web/cnl/tests.py +++ /dev/null @@ -1,23 +0,0 @@ -""" -This file demonstrates two different styles of tests (one doctest and one -unittest). These will both pass when you run "manage.py test". - -Replace these with more appropriate tests for your application. -""" - -from django.test import TestCase - -class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.failUnlessEqual(1 + 1, 2) - -__test__ = {"doctest": """ -Another way to test that 1 + 1 is equal to 2. - ->>> 1 + 1 == 2 -True -"""} - diff --git a/module/web/cnl/urls.py b/module/web/cnl/urls.py deleted file mode 100644 index 635e751ca..000000000 --- a/module/web/cnl/urls.py +++ /dev/null @@ -1,21 +0,0 @@ -# -*- coding: utf-8 -*- -from django.conf.urls.defaults import * - - -urlpatterns = patterns('cnl', - # Example: - # (r'^pyload/', include('pyload.foo.urls')), - - # Uncomment the admin/doc line below and add 'django.contrib.admindocs' - # to INSTALLED_APPS to enable admin documentation: - # (r'^admin/doc/', include('django.contrib.admindocs.urls')), - - # Uncomment the next line to enable the admin: - (r'^add$', 'views.add'), - (r'^addcrypted$', 'views.addcrypted'), - (r'^addcrypted2$', 'views.addcrypted2'), - (r'^crossdomain\.xml', 'views.crossdomain'), - (r'^jdcheck\.js', 'views.jdcheck'), - (r'^checkSupportForUrl', 'views.checksupport'), - (r'', 'views.flash') - ) diff --git a/module/web/cnl/views.py b/module/web/cnl/views.py deleted file mode 100644 index 8b43bdccc..000000000 --- a/module/web/cnl/views.py +++ /dev/null @@ -1,166 +0,0 @@ -# Create your views here. - - -from os.path import join -import re -from urllib import unquote -from base64 import standard_b64decode -from binascii import unhexlify - -from django.conf import settings -from django.http import HttpResponse -from django.http import HttpResponseServerError - -from django.core.serializers import json -from django.utils import simplejson - -try: - from Crypto.Cipher import AES -except: - pass - -def local_check(function): - def _dec(view_func): - def _view(request, * args, ** kwargs): - if request.META.get('REMOTE_ADDR', "0") in ('127.0.0.1','localhost') or request.META.get('HTTP_HOST','0') == '127.0.0.1:9666': - return view_func(request, * args, ** kwargs) - else: - return HttpResponseServerError() - - _view.__name__ = view_func.__name__ - _view.__dict__ = view_func.__dict__ - _view.__doc__ = view_func.__doc__ - - return _view - - if function is None: - return _dec - else: - return _dec(function) - -class JsonResponse(HttpResponse): - def __init__(self, obj, request): - cb = request.GET.get("callback") - if cb: - obj = {"content": obj} - content = simplejson.dumps(obj, indent=2, cls=json.DjangoJSONEncoder, ensure_ascii=False) - content = "%s(%s)\r\n" % (cb, content) - HttpResponse.__init__(self, content, content_type="application/json") - else: - content = "%s\r\n" % obj - HttpResponse.__init__(self, content, content_type="text/html") - self["Cache-Control"] = "no-cache, must-revalidate" - -@local_check -def flash(request): - return HttpResponse("JDownloader") - -@local_check -def add(request): - package = request.POST.get('referer', 'ClickAndLoad Package') - urls = filter(lambda x: x != "", request.POST['urls'].split("\n")) - - settings.PYLOAD.add_package(package, urls, False) - - return HttpResponse() - -@local_check -def addcrypted(request): - - package = request.POST.get('referer', 'ClickAndLoad Package') - dlc = request.POST['crypted'].replace(" ", "+") - - dlc_path = join(settings.DL_ROOT, package.replace("/", "").replace("\\", "").replace(":", "") + ".dlc") - dlc_file = file(dlc_path, "wb") - dlc_file.write(dlc) - dlc_file.close() - - try: - settings.PYLOAD.add_package(package, [dlc_path], False) - except: - return JsonResponse("", request) - else: - return JsonResponse("success", request) - -@local_check -def addcrypted2(request): - - package = request.POST.get("source", "ClickAndLoad Package") - crypted = request.POST["crypted"] - jk = request.POST["jk"] - - crypted = standard_b64decode(unquote(crypted.replace(" ", "+"))) - if settings.JS: - jk = "%s f()" % jk - jk = settings.JS.eval(jk) - - else: - try: - jk = re.findall(r"return ('|\")(.+)('|\")", jk)[0][1] - except: - ## Test for some known js functions to decode - if jk.find("dec") > -1 and jk.find("org") > -1: - org = re.findall(r"var org = ('|\")([^\"']+)", jk)[0][1] - jk = list(org) - jk.reverse() - jk = "".join(jk) - else: - print "Could not decrypt key, please install py-spidermonkey or ossp-js" - - try: - Key = unhexlify(jk) - except: - print "Could not decrypt key, please install py-spidermonkey or ossp-js" - return JsonResponse("failed", request) - - IV = Key - - obj = AES.new(Key, AES.MODE_CBC, IV) - result = obj.decrypt(crypted).replace("\x00", "").replace("\r","").split("\n") - - result = filter(lambda x: x != "", result) - - try: - settings.PYLOAD.add_package(package, result, False) - except: - return JsonResponse("failed can't add", request) - else: - return JsonResponse("success", request) - -@local_check -def flashgot(request): - if request.META['HTTP_REFERER'] != "http://localhost:9666/flashgot" and request.META['HTTP_REFERER'] != "http://127.0.0.1:9666/flashgot": - return HttpResponseServerError() - - autostart = int(request.POST.get('autostart', 0)) - package = request.POST.get('package', "FlashGot") - urls = filter(lambda x: x != "", request.POST['urls'].split("\n")) - folder = request.POST.get('dir', None) - - settings.PYLOAD.add_package(package, urls, autostart) - - return HttpResponse("") - -@local_check -def crossdomain(request): - rep = "<?xml version=\"1.0\"?>\n" - rep += "<!DOCTYPE cross-domain-policy SYSTEM \"http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd\">\n" - rep += "<cross-domain-policy>\n" - rep += "<allow-access-from domain=\"*\" />\n" - rep += "</cross-domain-policy>" - return HttpResponse(rep) - -@local_check -def checksupport(request): - - url = request.GET.get("url") - res = settings.PYLOAD.checkURLs([url]) - supported = (not res[0][1] is None) - - return JsonResponse(str(supported).lower(), request) - -@local_check -def jdcheck(request): - rep = "jdownloader=true;\n" - rep += "var version='10629';\n" - return HttpResponse(rep) diff --git a/module/web/pyload/__init__.py b/module/web/pyload/__init__.py deleted file mode 100644 index e69de29bb..000000000 --- a/module/web/pyload/__init__.py +++ /dev/null diff --git a/module/web/pyload/admin.py b/module/web/pyload/admin.py deleted file mode 100644 index 99cb28836..000000000 --- a/module/web/pyload/admin.py +++ /dev/null @@ -1,15 +0,0 @@ -# -*- coding: utf-8 -*- -from django.contrib import admin -from models import Prefs -from django.contrib.auth.models import User -from django.contrib.auth.admin import UserAdmin as RealUserAdmin - - -class UserProfileInline(admin.StackedInline): - model = Prefs - -class UserAdmin(RealUserAdmin): - inlines = [ UserProfileInline ] - -admin.site.unregister(User) -admin.site.register(User, UserAdmin)
\ No newline at end of file diff --git a/module/web/pyload/models.py b/module/web/pyload/models.py deleted file mode 100644 index 86962f23c..000000000 --- a/module/web/pyload/models.py +++ /dev/null @@ -1,31 +0,0 @@ -# -*- coding: utf-8 -*- -from django.db import models -from django.contrib.auth.models import User -# Create your models here. - -class Prefs(models.Model): - """ Permissions setting """ - - user = models.ForeignKey(User, unique=True) - template = models.CharField(max_length=30, default='default', null=False, blank=False) #@TODO: currently unused - - class Meta: - permissions = ( - ('can_see_dl', 'User can see Downloads'), - ('can_change_status', 'User can change Status'), - ('can_download', 'User can download'), - ('can_add', 'User can add Links'), - ('can_delete', 'User can delete Links'), - ('can_see_logs', 'User can see Logs'), - ) - verbose_name = "Preferences" - verbose_name_plural = "Preferences" - - def __unicode__(self): - return "Preferences for %s" % self.user - - -def user_post_save(sender, instance, **kwargs): - profile, new = Prefs.objects.get_or_create(user=instance) - -models.signals.post_save.connect(user_post_save, User)
\ No newline at end of file diff --git a/module/web/pyload/templatetags/__init__.py b/module/web/pyload/templatetags/__init__.py deleted file mode 100644 index e69de29bb..000000000 --- a/module/web/pyload/templatetags/__init__.py +++ /dev/null diff --git a/module/web/pyload/templatetags/contains.py b/module/web/pyload/templatetags/contains.py deleted file mode 100644 index ed6225a95..000000000 --- a/module/web/pyload/templatetags/contains.py +++ /dev/null @@ -1,14 +0,0 @@ -from django import template -register = template.Library() - -@register.filter() -def contains(value, arg): - """ - Usage: - {% if text|contains:" http://" %} - This is a link. - {% else %} - Not a link. - {% endif %} - """ - return arg in value diff --git a/module/web/pyload/templatetags/quotepath.py b/module/web/pyload/templatetags/quotepath.py deleted file mode 100644 index 8483a1553..000000000 --- a/module/web/pyload/templatetags/quotepath.py +++ /dev/null @@ -1,68 +0,0 @@ -import os - -from django.template.defaultfilters import stringfilter -from django import template - -try: - from os.path import relpath -except: - from posixpath import curdir, sep, pardir, join - def relpath(path, start=curdir): - """Return a relative version of a path""" - if not path: - raise ValueError("no path specified") - start_list = os.path.abspath(start).split(sep) - path_list = os.path.abspath(path).split(sep) - # Work out how much of the filepath is shared by start and path. - i = len(os.path.commonprefix([start_list, path_list])) - rel_list = [pardir] * (len(start_list)-i) + path_list[i:] - if not rel_list: - return curdir - return join(*rel_list) - -register = template.Library() - -quotechar = "::/" - -@stringfilter -def quotepath(path): - try: - return path.replace("../", quotechar) - except AttributeError: - return path - except: - return "" - - -register.filter(quotepath) - -@stringfilter -def unquotepath(path): - try: - return path.replace(quotechar, "../") - except AttributeError: - return path - except: - return "" - -register.filter(unquotepath) - -def path_make_absolute(path): - p = os.path.abspath(path) - if p[-1] == os.path.sep: - return p - else: - return p + os.path.sep - - -register.filter(path_make_absolute) - -def path_make_relative(path): - p = relpath(path) - if p[-1] == os.path.sep: - return p - else: - return p + os.path.sep - -register.filter(path_make_relative) - diff --git a/module/web/pyload/templatetags/token.py b/module/web/pyload/templatetags/token.py deleted file mode 100644 index e6117b839..000000000 --- a/module/web/pyload/templatetags/token.py +++ /dev/null @@ -1,17 +0,0 @@ - -from django import VERSION -from django import template -register = template.Library() - -if VERSION[:3] < (1,1,2): - - class TokenNode(template.Node): - def render(self, content): - return "" - - @register.tag() - def csrf_token(parser, token): - """ - Return nothing, since csrf is deactivated in django 1.1 - """ - return TokenNode() diff --git a/module/web/pyload/templatetags/truncate.py b/module/web/pyload/templatetags/truncate.py deleted file mode 100644 index ed35ea998..000000000 --- a/module/web/pyload/templatetags/truncate.py +++ /dev/null @@ -1,13 +0,0 @@ -from django.template.defaultfilters import stringfilter -from django import template - -register = template.Library() - - -@stringfilter -def truncate(value, n): - if (n - len(value)) < 3: - return value[:n]+"..." - return value - -register.filter(truncate) diff --git a/module/web/pyload/tests.py b/module/web/pyload/tests.py deleted file mode 100644 index 2247054b3..000000000 --- a/module/web/pyload/tests.py +++ /dev/null @@ -1,23 +0,0 @@ -""" -This file demonstrates two different styles of tests (one doctest and one -unittest). These will both pass when you run "manage.py test". - -Replace these with more appropriate tests for your application. -""" - -from django.test import TestCase - -class SimpleTest(TestCase): - def test_basic_addition(self): - """ - Tests that 1 + 1 always equals 2. - """ - self.failUnlessEqual(1 + 1, 2) - -__test__ = {"doctest": """ -Another way to test that 1 + 1 is equal to 2. - ->>> 1 + 1 == 2 -True -"""} - diff --git a/module/web/pyload/urls.py b/module/web/pyload/urls.py deleted file mode 100644 index fe85c1096..000000000 --- a/module/web/pyload/urls.py +++ /dev/null @@ -1,31 +0,0 @@ -# -*- coding: utf-8 -*- - -from os.path import join - -from django.conf import settings -from django.conf.urls.defaults import * - - -urlpatterns = patterns('pyload', - (r'^home/$', 'views.home'), - (r'^downloads/$', 'views.downloads',{},'downloads'), - (r'^download/(?P<path>[a-zA-z\.0-9\-/_% "\\]+)$', 'views.download',{},'download'), - (r'^queue/$', 'views.queue',{}, 'queue'), - (r'^collector/$', 'views.collector',{}, 'collector'), - (r'^settings/$', 'views.config',{}, 'config'), - (r'^logs/$', 'views.logs',{}, 'logs'), - (r'^logs/(?P<item>\d+)$', 'views.logs',{}, 'logs'), - (r'^package_ui.js$', 'views.package_ui', {}, 'package_ui'), - (r'^$', 'views.home',{}, 'home'), - url(r'^pathchooser/(?P<path>.*)', 'views.path', {'type':'folder'}, name='path'), - url(r'^pathchooser/$', 'views.root', {'type':'folder'}, name='pathroot'), - url(r'^filechooser/(?P<path>.*)', 'views.path', {'type':'file'}, name='file'), - url(r'^filechooser/$', 'views.root', {'type':'file'}, name='fileroot'), - ) - -urlpatterns += patterns('django.contrib.auth', - (r'^login/$', 'views.login', {'template_name': join(settings.TEMPLATE, 'login.html')}), - (r'^logout/$', 'views.logout', {'template_name': join(settings.TEMPLATE, 'logout.html')}, 'logout'), -) - - diff --git a/module/web/pyload/views.py b/module/web/pyload/views.py deleted file mode 100644 index 8e72402a4..000000000 --- a/module/web/pyload/views.py +++ /dev/null @@ -1,492 +0,0 @@ -# -*- coding: utf-8 -*- - -# Create your views here. -import mimetypes -import os -from os import listdir -from os import stat -from os.path import isdir -from os.path import isfile -from os.path import join -from sys import getfilesystemencoding -from urllib import unquote -from itertools import chain -from datetime import datetime -from time import localtime, strftime -from copy import deepcopy -from operator import itemgetter -from pyload.templatetags import quotepath - -try: - from os.path import relpath -except: - from posixpath import curdir, sep, pardir - def relpath(path, start=curdir): - """Return a relative version of a path""" - if not path: - raise ValueError("no path specified") - start_list = os.path.abspath(start).split(sep) - path_list = os.path.abspath(path).split(sep) - # Work out how much of the filepath is shared by start and path. - i = len(os.path.commonprefix([start_list, path_list])) - rel_list = [pardir] * (len(start_list)-i) + path_list[i:] - if not rel_list: - return curdir - return join(*rel_list) - -from django.conf import settings -from django.contrib.auth.decorators import login_required -from django.http import HttpResponse -from django.http import HttpResponseNotFound -from django.http import HttpResponseRedirect -from django.shortcuts import render_to_response -from django.template import RequestContext -from django.utils.translation import ugettext as _ -from django.core.urlresolvers import reverse - - -def get_sort_key(item): - return item[1]["order"] - -def formatSize(size): - """formats size of bytes""" - size = int(size) - steps = 0 - sizes = ["KB", "MB", "GB", "TB"] - - while size > 1000: - size /= 1024.0 - steps += 1 - - return "%.2f %s" % (size, sizes[steps]) - -def check_server(function): - def _dec(view_func): - def _view(request, * args, ** kwargs): - try: - version = settings.PYLOAD.get_server_version() - except Exception, e: - return base(request, messages=[_('Can\'t connect to pyLoad. Please check your configuration and make sure pyLoad is running.'), str(e)]) - return view_func(request, * args, ** kwargs) - - _view.__name__ = view_func.__name__ - _view.__dict__ = view_func.__dict__ - _view.__doc__ = view_func.__doc__ - - return _view - - if function is None: - return _dec - else: - return _dec(function) - - -def permission(perm): - def _dec(view_func): - def _view(request, * args, ** kwargs): - if request.user.has_perm(perm) and request.user.is_authenticated(): - return view_func(request, * args, ** kwargs) - else: - return base(request, messages=[_('You don\'t have permission to view this page.')]) - - _view.__name__ = view_func.__name__ - _view.__dict__ = view_func.__dict__ - _view.__doc__ = view_func.__doc__ - - return _view - - return _dec - - - -def status_proc(request): - return {'status': settings.PYLOAD.status_server(), 'captcha': settings.PYLOAD.is_captcha_waiting()} - - -def base(request, messages): - return render_to_response(join(settings.TEMPLATE, 'base.html'), {'messages': messages}, RequestContext(request)) - -@login_required -@permission('pyload.can_see_dl') -@check_server -def home(request): - res = settings.PYLOAD.status_downloads() - - for link in res: - if link["status"] == 12: - link["information"] = "%s kB @ %s kB/s" % (link["size"] - link["kbleft"], link["speed"]) - - return render_to_response(join(settings.TEMPLATE, 'home.html'), RequestContext(request, {'content': res}, [status_proc])) - - -@login_required -@permission('pyload.can_see_dl') -@check_server -def queue(request): - queue = settings.PYLOAD.get_queue_info() - - data = zip(queue.keys(), queue.values()) - data.sort(key=get_sort_key) - - return render_to_response(join(settings.TEMPLATE, 'queue.html'), RequestContext(request, {'content': data}, [status_proc])) - - -@login_required -@permission('pyload.can_download') -@check_server -def downloads(request): - - root = settings.PYLOAD.get_conf_val("general", "download_folder") - - if not isdir(root): - return base(request, [_('Download directory not found.')]) - data = { - 'folder': [], - 'files': [] - } - - for item in sorted(listdir(root)): - if isdir(join(root, item)): - folder = { - 'name': item, - 'path': item, - 'files': [] - } - for file in sorted(listdir(join(root, item))): - try: - if isfile(join(root, item, file)): - folder['files'].append(file) - except: - pass - - data['folder'].append(folder) - elif isfile(join(root, item)): - data['files'].append(item) - - - return render_to_response(join(settings.TEMPLATE, 'downloads.html'), RequestContext(request, {'files': data}, [status_proc])) - -@login_required -@permission('pyload.can_download') -@check_server -def download(request, path): - path = unquote(path) - path = path.split("/") - - root = settings.PYLOAD.get_conf_val("general", "download_folder") - - dir = join(root, path[1].replace('..', '')) - if isdir(dir) or isfile(dir): - if isdir(dir): filepath = join(dir, path[2]) - elif isfile(dir): filepath = dir - - if isfile(filepath): - try: - type, encoding = mimetypes.guess_type(filepath) - if type is None: - type = 'application/octet-stream' - - response = HttpResponse(mimetype=type) - response['Content-Length'] = str(stat(filepath).st_size) - - if encoding is not None: - response['Content-Encoding'] = encoding - - response.write(file(filepath, "rb").read()) - return response - - except Exception, e: - return HttpResponseNotFound("File not Found. %s" % str(e)) - - return HttpResponseNotFound("File not Found.") - -@login_required -@permission('pyload.can_see_logs') -@check_server -def logs(request, item=-1): - - perpage = request.session.get('perpage', 34) - reversed = request.session.get('reversed', False) - - warning = "" - conf = settings.PYLOAD.get_config() - if not conf['log']['file_log']['value']: - warning = "Warning: File log is disabled, see settings page." - - perpage_p = ((20,20), (34, 34), (40, 40), (100, 100), (0,'all')) - fro = None - - if request.method == 'POST': - try: - fro = datetime.strptime(request.POST['from'], '%d.%m.%Y %H:%M:%S') - except: - pass - try: - perpage = int(request.POST['perpage']) - request.session['perpage'] = perpage - - reversed = bool(request.POST.get('reversed', False)) - request.session['reversed'] = reversed - except: - pass - - try: - item = int(item) - except: - pass - - log = settings.PYLOAD.get_log() - if perpage == 0: - item = 0 - - if item < 1 or type(item) is not int: - item = 1 if len(log) - perpage + 1 < 1 else len(log) - perpage + 1 - - if type(fro) is datetime: # we will search for datetime - item = -1 - - data = [] - counter = 0 - perpagecheck = 0 - for l in log: - counter = counter+1 - - if counter >= item: - try: - date,time,level,message = l.split(" ", 3) - dtime = datetime.strptime(date+' '+time, '%d.%m.%Y %H:%M:%S') - except: - dtime = None - date = '?' - time = ' ' - level = '?' - message = l - if item == -1 and dtime is not None and fro <= dtime: - item = counter #found our datetime - if item >= 0: - data.append({'line': counter, 'date': date+" "+time, 'level':level, 'message': message}) - perpagecheck += 1 - if fro is None and dtime is not None: #if fro not set set it to first showed line - fro = dtime - if perpagecheck >= perpage > 0: - break - - if fro is None: #still not set, empty log? - fro = datetime.now() - if reversed: - data.reverse() - return render_to_response(join(settings.TEMPLATE, 'logs.html'), RequestContext(request, {'warning': warning, 'log': data, 'from': fro.strftime('%d.%m.%Y %H:%M:%S'), 'reversed': reversed, 'perpage':perpage, 'perpage_p':sorted(perpage_p), 'iprev': 1 if item - perpage < 1 else item - perpage, 'inext': (item + perpage) if item+perpage < len(log) else item}, [status_proc])) - -@login_required -@permission('pyload.can_add_dl') -@check_server -def collector(request): - queue = settings.PYLOAD.get_collector_info() - - data = zip(queue.keys(), queue.values()) - data.sort(key=get_sort_key) - - return render_to_response(join(settings.TEMPLATE, 'collector.html'), RequestContext(request, {'content': data}, [status_proc])) - - -@login_required -@permission('pyload.can_change_status') -@check_server -def config(request): - conf = settings.PYLOAD.get_config() - plugin = settings.PYLOAD.get_plugin_config() - accs = settings.PYLOAD.get_accounts(False, False) - messages = [] - - for section in chain(conf.itervalues(), plugin.itervalues()): - for key, option in section.iteritems(): - if key == "desc": continue - - if ";" in option["type"]: - option["list"] = option["type"].split(";") - - if request.META.get('REQUEST_METHOD', "GET") == "POST": - - errors = [] - - for key, value in request.POST.iteritems(): - if not "|" in key: continue - sec, skey, okey = key.split("|")[:] - - if sec == "General": - - if conf.has_key(skey): - if conf[skey].has_key(okey): - try: - if str(conf[skey][okey]['value']) != value: - settings.PYLOAD.set_conf_val(skey, okey, value) - except Exception, e: - errors.append("%s | %s : %s" % (skey, okey, e)) - else: - continue - else: - continue - - elif sec == "Plugin": - if plugin.has_key(skey): - if plugin[skey].has_key(okey): - try: - if str(plugin[skey][okey]['value']) != value: - settings.PYLOAD.set_conf_val(skey, okey, value, "plugin") - except Exception, e: - errors.append("%s | %s : %s" % (skey, okey, e)) - else: - continue - else: - continue - elif sec == "Accounts": - if ";" in okey: - action, name = okey.split(";") - if action == "delete": - settings.PYLOAD.remove_account(skey, name) - - if okey == "newacc" and value: - # add account - - pw = request.POST.get("Accounts|%s|newpw" % skey) - - settings.PYLOAD.update_account(skey, value, pw) - - for pluginname, accdata in accs.iteritems(): - for data in accdata: - newpw = request.POST.get("Accounts|%s|password;%s" % (pluginname, data["login"]), "").strip() - time = request.POST.get("Accounts|%s|time;%s" % (pluginname, data["login"]), "").strip() - - if newpw or (time and (not data["options"].has_key("time") or [time] != data["options"]["time"])): - settings.PYLOAD.update_account(pluginname, data["login"], newpw, {"time": [time]}) - - - if errors: - messages.append(_("Error occured when setting the following options:")) - messages.append("") - messages += errors - else: - messages.append(_("All options were set correctly.")) - - accs = deepcopy(settings.PYLOAD.get_accounts(False, False)) - for accounts in accs.itervalues(): - for data in accounts: - if data["trafficleft"] == -1: - data["trafficleft"] = _("unlimited") - elif not data["trafficleft"]: - data["trafficleft"] = _("not available") - else: - data["trafficleft"] = formatSize(data["trafficleft"]) - - if data["validuntil"] == -1: - data["validuntil"] = _("unlimited") - elif not data["validuntil"]: - data["validuntil"] = _("not available") - else: - t = localtime(data["validuntil"]) - data["validuntil"] = strftime("%d.%m.%Y",t) - - if data["options"].has_key("time"): - try: - data["time"] = data["options"]["time"][0] - except: - data["time"] = "invalid" - - return render_to_response(join(settings.TEMPLATE, 'settings.html'), RequestContext(request, {'conf': {'Plugin':plugin, 'General':conf, 'Accounts': accs}, 'errors': messages}, [status_proc])) - -@login_required -@permission('pyload.can_change_status') -@check_server -def package_ui(request): - return render_to_response(join(settings.TEMPLATE, 'package_ui.js'), RequestContext(request, {}, )) - - -@login_required -@permission('pyload.can_change_status') -@check_server -def root(request, type): - cwd = os.getcwd() - return HttpResponseRedirect(reverse('path', args=[cwd[1:], type])) - -@login_required -@permission('pyload.can_change_status') -@check_server -def path(request, path, type): - - path = os.path.normpath(quotepath.unquotepath(path)) - - if os.path.isfile(path): - oldfile = path - path = os.path.dirname(path) - else: - oldfile = '' - - abs = False - - if os.path.isdir(path): - if os.path.isabs(path): - cwd = os.path.abspath(path) - abs = True - else: - cwd = relpath(path) - else: - cwd = os.getcwd() - - try: - cwd = cwd.encode("utf8") - except: - pass - - cwd = os.path.normpath(os.path.abspath(cwd)) - parentdir = os.path.dirname(cwd) - if not abs: - if os.path.abspath(cwd) == "/": - cwd = relpath(cwd) - else: - cwd = relpath(cwd) + os.path.sep - parentdir = relpath(parentdir) + os.path.sep - - if os.path.abspath(cwd) == "/": - parentdir = "" - - try: - folders = os.listdir(cwd) - except: - folders = [] - - files = [] - - for f in folders: - try: - f = f.decode(getfilesystemencoding()) - data = {} - data['name'] = f - data['fullpath'] = os.path.join(cwd, f) - data['sort'] = data['fullpath'].lower() - data['modified'] = datetime.fromtimestamp(int(os.path.getmtime(os.path.join(cwd, f)))) - data['ext'] = os.path.splitext(f)[1] - except: - continue - - if os.path.isdir(os.path.join(cwd, f)): - data['type'] = 'dir' - else: - data['type'] = 'file' - - if os.path.isfile(os.path.join(cwd, f)): - data['size'] = os.path.getsize(os.path.join(cwd, f)) - - power = 0 - while (data['size']/1024) > 0.3: - power += 1 - data['size'] = data['size'] / 1024. - units = ('', 'K','M','G','T') - data['unit'] = units[power]+'Byte' - else: - data['size'] = '' - - files.append(data) - - files = sorted(files, key=itemgetter('type', 'sort')) - - return render_to_response(join(settings.TEMPLATE, 'pathchooser.html'), {'cwd': cwd, 'files': files, 'parentdir': parentdir, 'type': type, 'oldfile': oldfile, 'absolute': abs}, RequestContext(request))
\ No newline at end of file diff --git a/module/web/pyload_app.py b/module/web/pyload_app.py index 0f8dd859c..d240564ab 100644 --- a/module/web/pyload_app.py +++ b/module/web/pyload_app.py @@ -31,7 +31,7 @@ from sys import getfilesystemencoding from hashlib import sha1 from urllib import unquote -from bottle import route, static_file, request, response, redirect, HTTPError +from bottle import route, static_file, request, response, redirect, HTTPError, error from webinterface import PYLOAD, PROJECT_DIR @@ -65,10 +65,16 @@ def base(messages): ## Views +@error(500) +def error500(error): + return base(["An Error occured, please enable debug mode to get more details.", error, + error.traceback.replace("\n", "<br>")]) + @route('/media/:path#.+#') def server_static(path): - response.headers['Expires'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time() + 60 * 60 * 24 * 7)) + response.headers['Expires'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", + time.gmtime(time.time() + 60 * 60 * 24 * 7)) response.headers['Cache-control'] = "public" return static_file(path, root=join(PROJECT_DIR, "media")) @@ -258,7 +264,8 @@ def config(): newpw = request.POST.get("Accounts|%s|password;%s" % (pluginname, data["login"]), "").strip() new_time = request.POST.get("Accounts|%s|time;%s" % (pluginname, data["login"]), "").strip() - if newpw or (new_time and (not data["options"].has_key("time") or [new_time] != data["options"]["time"])): + if newpw or ( + new_time and (not data["options"].has_key("time") or [new_time] != data["options"]["time"])): PYLOAD.update_account(pluginname, data["login"], newpw, {"time": [new_time]}) if errors: @@ -292,7 +299,6 @@ def config(): except: data["time"] = "invalid" - return render_to_response('settings.html', {'conf': {'Plugin': plugin, 'General': conf, 'Accounts': accs}, 'errors': messages}, [pre_processor]) @@ -300,7 +306,8 @@ def config(): @route("/package_ui.js") @login_required('can_see_dl') def package_ui(): - response.headers['Expires'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(time.time() + 60 * 60 * 24 * 7)) + response.headers['Expires'] = time.strftime("%a, %d %b %Y %H:%M:%S GMT", + time.gmtime(time.time() + 60 * 60 * 24 * 7)) response.headers['Cache-control'] = "public" return render_to_response('package_ui.js') @@ -380,11 +387,11 @@ def path(file="", path=""): data['size'] = os.path.getsize(join(cwd, f)) power = 0 - while (data['size']/1024) > 0.3: + while (data['size'] / 1024) > 0.3: power += 1 data['size'] /= 1024. - units = ('', 'K','M','G','T') - data['unit'] = units[power]+'Byte' + units = ('', 'K', 'M', 'G', 'T') + data['unit'] = units[power] + 'Byte' else: data['size'] = '' @@ -392,7 +399,9 @@ def path(file="", path=""): files = sorted(files, key=itemgetter('type', 'sort')) - return render_to_response('pathchooser.html', {'cwd': cwd, 'files': files, 'parentdir': parentdir, 'type': type, 'oldfile': oldfile, 'absolute': abs}, []) + return render_to_response('pathchooser.html', + {'cwd': cwd, 'files': files, 'parentdir': parentdir, 'type': type, 'oldfile': oldfile, + 'absolute': abs}, []) @route("/logs") @route("/logs", method="POST") @@ -410,7 +419,7 @@ def logs(item=-1): if not conf['log']['file_log']['value']: warning = "Warning: File log is disabled, see settings page." - perpage_p = ((20,20), (34, 34), (40, 40), (100, 100), (0,'all')) + perpage_p = ((20, 20), (34, 34), (40, 40), (100, 100), (0, 'all')) fro = None if request.environ.get('REQUEST_METHOD', "GET") == "POST": @@ -439,7 +448,7 @@ def logs(item=-1): item = 0 if item < 1 or type(item) is not int: - item = 1 if len(log) - perpage + 1 < 1 else len(log) - perpage + 1 + item = 1 if len(log) - perpage + 1 < 1 else len(log) - perpage + 1 if type(fro) is datetime: # we will search for datetime item = -1 @@ -452,8 +461,8 @@ def logs(item=-1): if counter >= item: try: - date,time,level,message = l.split(" ", 3) - dtime = datetime.strptime(date+' '+time, '%d.%m.%Y %H:%M:%S') + date, time, level, message = l.split(" ", 3) + dtime = datetime.strptime(date + ' ' + time, '%d.%m.%Y %H:%M:%S') except: dtime = None date = '?' @@ -463,7 +472,7 @@ def logs(item=-1): if item == -1 and dtime is not None and fro <= dtime: item = counter #found our datetime if item >= 0: - data.append({'line': counter, 'date': date+" "+time, 'level':level, 'message': message}) + data.append({'line': counter, 'date': date + " " + time, 'level': level, 'message': message}) perpagecheck += 1 if fro is None and dtime is not None: #if fro not set set it to first showed line fro = dtime @@ -474,4 +483,12 @@ def logs(item=-1): fro = datetime.now() if reversed: data.reverse() - return render_to_response('logs.html', {'warning': warning, 'log': data, 'from': fro.strftime('%d.%m.%Y %H:%M:%S'), 'reversed': reversed, 'perpage':perpage, 'perpage_p':sorted(perpage_p), 'iprev': 1 if item - perpage < 1 else item - perpage, 'inext': (item + perpage) if item+perpage < len(log) else item}, [pre_processor]) + return render_to_response('logs.html', {'warning': warning, 'log': data, 'from': fro.strftime('%d.%m.%Y %H:%M:%S'), + 'reversed': reversed, 'perpage': perpage, 'perpage_p': sorted(perpage_p), + 'iprev': 1 if item - perpage < 1 else item - perpage, + 'inext': (item + perpage) if item + perpage < len(log) else item}, + [pre_processor]) + +@route("/admin") +def admin(): + return base([])
\ No newline at end of file diff --git a/pyLoadCore.py b/pyLoadCore.py index 6ad45c875..9e3e1053a 100755 --- a/pyLoadCore.py +++ b/pyLoadCore.py @@ -61,12 +61,12 @@ from module.PyFile import PyFile from module.Scheduler import Scheduler from module.JsEngine import JsEngine from module.remote.RemoteManager import RemoteManager -from module.DatabaseBackend import DatabaseBackend +from module.database import DatabaseBackend #registering db methods -from module.FileDatabase import FileHandler -from module.UserDatabase import UserMethods -from module.StorageDatabase import StorageMethods +from module.database import FileHandler +from module.database import UserMethods +from module.database import StorageMethods from codecs import getwriter if os.name == "nt": |