1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- encoding: utf-8 -*- |
3
|
|
|
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8: |
4
|
|
|
# Author: Binux<[email protected]> |
5
|
|
|
# http://binux.me |
6
|
|
|
# Created on 2014-02-09 11:28:52 |
7
|
|
|
|
8
|
|
|
import re |
9
|
|
|
|
10
|
|
|
# NOTE: When get/get_all/check_update from database with default fields, |
11
|
|
|
# all following fields should be included in output dict. |
12
|
|
|
{ |
13
|
|
|
'project': { |
14
|
|
|
'name': str, |
15
|
|
|
'group': str, |
16
|
|
|
'status': str, |
17
|
|
|
'script': str, |
18
|
|
|
# 'config': str, |
19
|
|
|
'comments': str, |
20
|
|
|
# 'priority': int, |
21
|
|
|
'rate': int, |
22
|
|
|
'burst': int, |
23
|
|
|
'updatetime': int, |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
class ProjectDB(object): |
29
|
|
|
status_str = [ |
30
|
|
|
'TODO', |
31
|
|
|
'STOP', |
32
|
|
|
'CHECKING', |
33
|
|
|
'DEBUG', |
34
|
|
|
'RUNNING', |
35
|
|
|
] |
36
|
|
|
|
37
|
|
|
def insert(self, name, obj={}): |
38
|
|
|
raise NotImplementedError |
39
|
|
|
|
40
|
|
|
def update(self, name, obj={}, **kwargs): |
41
|
|
|
raise NotImplementedError |
42
|
|
|
|
43
|
|
|
def get_all(self, fields=None): |
44
|
|
|
raise NotImplementedError |
45
|
|
|
|
46
|
|
|
def get(self, name, fields): |
47
|
|
|
raise NotImplementedError |
48
|
|
|
|
49
|
|
|
def drop(self, name): |
50
|
|
|
raise NotImplementedError |
51
|
|
|
|
52
|
|
|
def check_update(self, timestamp, fields=None): |
53
|
|
|
raise NotImplementedError |
54
|
|
|
|
55
|
|
|
def split_group(self, group, lower=True): |
56
|
|
|
return re.split("\W+", (group or '').lower()) |
57
|
|
|
|
58
|
|
|
def verify_project_name(self, name): |
59
|
|
|
if len(name) > 64: |
60
|
|
|
return False |
61
|
|
|
if re.search(r"[^\w]", name): |
62
|
|
|
return False |
63
|
|
|
return True |
64
|
|
|
|
65
|
|
|
def copy(self): |
66
|
|
|
''' |
67
|
|
|
database should be able to copy itself to create new connection |
68
|
|
|
|
69
|
|
|
it's implemented automatically by pyspider.database.connect_database |
70
|
|
|
if you are not create database connection via connect_database method, |
71
|
|
|
you should implement this |
72
|
|
|
''' |
73
|
|
|
raise NotImplementedError |
74
|
|
|
|