16 |
17 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/examples/test/app/test.py:
--------------------------------------------------------------------------------
1 | from fuqit import forms
2 | from fuqit.web import render
3 |
4 |
5 | def GET(web):
6 | """
7 | Demonstrates using the session and also how to then render another
8 | thing seamlessly. Just call web.app.render() and it'll do all the
9 | resolving gear again, so one method works on statics, modules, jinja2
10 | just like you accessed it from a browser.
11 | """
12 | web.form = forms.read(web, reset=False)
13 |
14 | if web.form.reset:
15 | web.session['count'] = 1
16 | else:
17 | web.session['count'] = web.session.get('count', 1) + 1
18 |
19 | return render('renderme.html', web)
20 |
--------------------------------------------------------------------------------
/setup.py:
--------------------------------------------------------------------------------
1 | try:
2 | from setuptools import setup
3 | except ImportError:
4 | from distutils.core import setup
5 |
6 | config = {
7 | 'package_data': {
8 | },
9 | 'description': 'The Fuqit Web Framework: Shit files into dir, get some web.',
10 | 'author': 'Zed A. Shaw',
11 | 'url': 'http://pypi.python.org/pypi/fuqit',
12 | 'download_url': 'http://pypi.python.org/pypi/fuqit',
13 | 'author_email': 'zedshaw@zedshaw.com',
14 | 'version': '1.0',
15 | 'scripts': ['bin/fuqit'],
16 | 'install_requires': ['python-modargs', 'python-lust'],
17 | 'packages': ['fuqit', 'fuqit.data'],
18 | 'name': 'fuqit'
19 | }
20 |
21 | setup(**config)
22 |
--------------------------------------------------------------------------------
/fuqit/forms.py:
--------------------------------------------------------------------------------
1 | from fuqit.web import RequestDict
2 |
3 | def read(web, **expected):
4 | results = web.params.copy()
5 |
6 | for key, value in expected.items():
7 | if key in results:
8 | try:
9 | if isinstance(value, int):
10 | results[key] = int(results[key])
11 | elif isinstance(value, float):
12 | results[key] = float(results[key])
13 | elif isinstance(value, bool):
14 | results[key] = bool(results[key])
15 | else:
16 | results[key] = results[key]
17 | except ValueError:
18 | # TODO: log these since they might matter
19 | results[key] = value
20 | else:
21 | results[key] = value
22 |
23 | return RequestDict(results)
24 |
--------------------------------------------------------------------------------
/bin/fuqit:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 |
3 | # Fuqit Web Framework
4 | # Copyright (C) 2013 Zed A. Shaw
5 | #
6 | # This program is free software: you can redistribute it and/or modify
7 | # it under the terms of the GNU Affero General Public License as published by
8 | # the Free Software Foundation, either version 3 of the License, or
9 | # (at your option) any later version.
10 | #
11 | # This program is distributed in the hope that it will be useful,
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 | # GNU Affero General Public License for more details.
15 | #
16 | # You should have received a copy of the GNU Affero General Public License
17 | # along with this program. If not, see .
18 |
19 | from modargs import args
20 | from fuqit import commands
21 | import sys
22 |
23 | args.parse_and_run_command(sys.argv[1:], commands, default_command="help")
24 |
25 |
26 |
--------------------------------------------------------------------------------
/examples/test/app/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 | I am a test Jinja! {{ stuff.test('HELLO') }}
8 |
9 |
10 |
Params
11 |
12 |
PARAM
VALUE
13 |
14 | {% for param, value in web.params.items() %}
15 |
{{ param }}
{{ value }}
16 | {% endfor %}
17 |
18 |
19 |
Headers
20 |
21 |
22 |
HEADER
VALUE
23 |
24 | {% for header, value in web.headers.items() %}
25 |
{{ header }}
{{ value }}
26 | {% endfor %}
27 |
28 |
29 | {% if web.params %}
30 |
31 |
Params
32 |
33 |
34 |
KEY
VALUE
35 |
36 | {% for key, value in web.params.items() %}
37 |
{{ key }}
{{ value }}
38 | {% endfor %}
39 |
40 |
41 | {% endif %}
42 |
43 |
Variables
44 |
45 |
Variable
Value
46 |
client_address
{{ web.client_address }}
47 |
method
{{ web.method }}
48 |
path
{{ web.path }}
49 |
request_version
{{ web.request_version }}
50 |
headers
{{ web.headers }}
51 |
52 |
53 |
Session
54 |
55 | {% for key, value in web.session.items() %}
56 |
{{ key }}
{{ value }}
57 | {% endfor %}
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/fuqit/sessions.py:
--------------------------------------------------------------------------------
1 | # Fuqit Web Framework
2 | # Copyright (C) 2013 Zed A. Shaw
3 | #
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU Affero General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU Affero General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU Affero General Public License
15 | # along with this program. If not, see .
16 |
17 | import datetime
18 | import re
19 | import os
20 |
21 | expires_format = "%a, %d-%b-%Y %X GMT"
22 |
23 | SESSION_PATTERN = re.compile('FuqitSession\s*=\s*([A-Fa-f0-9]+)')
24 | SESSION_TIMEOUT = 100 # days
25 | SESSION_STORE = {}
26 |
27 | def make_random_id():
28 | return os.urandom(64/8).encode('hex_codec')
29 |
30 | def get_session_id(headers):
31 | cookies = headers.get('cookie', None)
32 |
33 | if cookies:
34 | sid_match = SESSION_PATTERN.search(cookies)
35 |
36 | if sid_match:
37 | return sid_match.group(1)
38 | else:
39 | return make_random_id()
40 | else:
41 | return make_random_id()
42 |
43 | def set_session_id(headers, session_id):
44 | dt = datetime.timedelta(days=SESSION_TIMEOUT)
45 | diff = datetime.datetime.now() + dt
46 | stamp = diff.strftime(expires_format)
47 |
48 | cookies = {'Set-Cookie': 'FuqitSession=%s; version=1; path=/; expires=%s; HttpOnly' % (session_id, stamp),
49 | 'Cookie': 'FuqitSession=%s; version=1; path=/; expires=%s' % (session_id, stamp)}
50 |
51 | headers.update(cookies)
52 |
53 | def load_session(variables):
54 | session_id = get_session_id(variables['headers'])
55 | session = SESSION_STORE.get(session_id, {})
56 | variables['session'] = session
57 | variables['session_id'] = session_id
58 |
59 | def save_session(variables, response_headers):
60 | session_id = variables['session_id']
61 | set_session_id(response_headers, session_id)
62 | SESSION_STORE[session_id] = variables['session']
63 |
64 |
--------------------------------------------------------------------------------
/fuqit/server.py:
--------------------------------------------------------------------------------
1 | # Fuqit Web Framework
2 | # Copyright (C) 2013 Zed A. Shaw
3 | #
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU Affero General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU Affero General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU Affero General Public License
15 | # along with this program. If not, see .
16 |
17 | from lust import log, server
18 | from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
19 | from fuqit import web, tools
20 |
21 | DEFAULT_HEADERS = {
22 | 'Content-type': 'text/plain'
23 | }
24 |
25 | class FuqitHandler(BaseHTTPRequestHandler):
26 |
27 | def transform_request(self, request_body=None):
28 | path, params = tools.parse_request(self.path, request_body)
29 | context = tools.build_context(params, self)
30 | body, code, headers = web.process(self.command, path, params, context)
31 | self.generate_response(body, code, headers)
32 |
33 | def do_GET(self):
34 | self.transform_request()
35 |
36 | def do_POST(self):
37 | clength = int(self.headers['content-length'])
38 | request_body = self.rfile.read(clength)
39 | self.transform_request(request_body)
40 |
41 | def generate_response(self, body, code, headers):
42 | headers = headers or DEFAULT_HEADERS
43 |
44 | self.send_response(code)
45 |
46 | for header, value in headers.items():
47 | self.send_header(header, value)
48 | self.end_headers()
49 |
50 | self.wfile.write(body)
51 |
52 |
53 | def run_server(host='127.0.0.1', port=8000, config_module='config', app='app',
54 | debug=True):
55 |
56 | server_address = (host, port)
57 | web.configure(app_module=app, config_module=config_module)
58 | httpd = HTTPServer(server_address, FuqitHandler)
59 | httpd.serve_forever()
60 |
61 |
62 |
63 | class Service(server.Simple):
64 | name = 'fuqit'
65 | should_jail = False
66 |
67 | def before_drop_privs(self, args):
68 | pass
69 |
70 | def start(self, args):
71 | pass
72 |
73 |
74 | def run(args, config_file, config_name):
75 | service = Service(config_file=config_file)
76 | log.setup(service.get('log_file'))
77 | service.run(args)
78 |
79 |
--------------------------------------------------------------------------------
/fuqit/tools.py:
--------------------------------------------------------------------------------
1 | # Fuqit Web Framework
2 | # Copyright (C) 2013 Zed A. Shaw
3 | #
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU Affero General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU Affero General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU Affero General Public License
15 | # along with this program. If not, see .
16 |
17 | import importlib
18 | import mimetypes
19 | import cgi
20 | import os
21 |
22 | mimetypes.init()
23 |
24 | def module(name, app_name=None):
25 | if app_name:
26 | themodule = importlib.import_module("." + name, package=app_name)
27 | else:
28 | themodule = importlib.import_module(name)
29 |
30 | reload(themodule)
31 | return themodule
32 |
33 |
34 | def build_context(params, handler):
35 | return {'params': params,
36 | 'headers': handler.headers,
37 | 'path': handler.path,
38 | 'method': handler.command,
39 | 'client_address': handler.client_address,
40 | 'request_version': handler.request_version,
41 | }
42 |
43 | def parse_request(path, request_body):
44 | request_params = {}
45 |
46 | if '?' in path:
47 | path, params = path.split('?', 1)
48 | params = cgi.parse_qsl(params)
49 | request_params.update(params)
50 |
51 | if request_body:
52 | params = cgi.parse_qsl(request_body)
53 | request_params.update(params)
54 |
55 | return path, request_params
56 |
57 |
58 | def make_ctype(ext, default_mtype):
59 | mtype = mimetypes.types_map.get(ext, default_mtype)
60 | return {'Content-Type': mtype}
61 |
62 |
63 |
64 | def find_longest_module(app, name, variables):
65 | base = name[1:]
66 |
67 | # need to limit the max we'll try to 20 for safety
68 | for i in xrange(0, 20):
69 | # go until we hit the /
70 | if base == '/' or base == '':
71 | return None, None
72 |
73 | modname = base.replace('/', '.')
74 |
75 | try:
76 | return base, module(modname, app)
77 | except ImportError, e:
78 | # split off the next chunk to try to load
79 | print "ERROR", e
80 | base, tail = os.path.split(base)
81 |
82 | # exhausted the path limit
83 | return None, None
84 |
85 |
86 |
--------------------------------------------------------------------------------
/fuqit/commands.py:
--------------------------------------------------------------------------------
1 | # Fuqit Web Framework
2 | # Copyright (C) 2013 Zed A. Shaw
3 | #
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU Affero General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU Affero General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU Affero General Public License
15 | # along with this program. If not, see .
16 |
17 | from modargs import args
18 | import fuqit
19 | import os
20 | import sys
21 |
22 | def help_command(**options):
23 | """
24 | Prints out help for the commands.
25 |
26 | fuqit help
27 |
28 | You can get help for one command with:
29 |
30 | fuqit help -for STR
31 | """
32 |
33 | if "for" in options:
34 | help_text = args.help_for_command(fuqit.commands, options['for'])
35 |
36 | if help_text:
37 | print help_text
38 | else:
39 | args.invalid_command_message(fuqit.commands, exit_on_error=True)
40 | else:
41 | print "Available commands:\n"
42 | print ", ".join(args.available_commands(fuqit.commands))
43 | print "\nUse fuqit help -for to find out more."
44 |
45 |
46 | def init_command(into=None):
47 | """
48 | Initializes a fuqit app, default directory is 'app'.
49 |
50 | fuqit init -into myapp
51 | """
52 |
53 | if not os.path.exists(into):
54 |
55 | for newdir in ['/', '/app', '/app/static']:
56 | os.mkdir(into + newdir)
57 |
58 | open(into + '/app/__init__.py', 'w').close()
59 | with open(into + '/config.py', 'w') as config:
60 | config.write("from fuqit import data\n\ndb = data.database(dbn='sqlite', db='data.sqlite3')")
61 |
62 | with open(into + '/app/index.html', 'w') as index:
63 | index.write('Put your crap in %s/app and hit rephresh.' % into)
64 |
65 | print "Your app is ready for hackings in %s" % into
66 |
67 | else:
68 | print "The app directory already exists. Try:\n\nfuqit init -into [SOMEDIR]"
69 |
70 |
71 | def run_command(host='127.0.0.1', port=8000, config_module='config', app='app',
72 | debug=True, chroot="."):
73 | """
74 | Runs a fuqit server.
75 |
76 | fuqit run -host 127.0.0.1 -port 8000 -referer http:// -app app -debug True \
77 | -chroot .
78 |
79 | NOTE: In run mode it's meant for developers, so -chroot just does a cd
80 | to the directory. In server mode it actually chroots there. It also
81 | adds the chroot path to the python syspath.
82 |
83 | """
84 | from fuqit import server
85 |
86 | sys.path.append(os.path.realpath(chroot))
87 | os.chdir(chroot)
88 |
89 | server.run_server(host=host,
90 | port=port,
91 | config_module=config_module,
92 | app=app,
93 | debug=debug)
94 |
95 |
96 | def start_command(host='127.0.0.1', port=8000, referer='http://', app='app',
97 | debug=True, chroot="."):
98 | """
99 | Runs the fuqit server as a daemon.
100 |
101 | fuqit start -host 127.0.0.1 -port 8000 -referer http:// -app app -debug True
102 | """
103 |
104 |
105 | def stop_command():
106 | """
107 | Stops a running fuqit daemon.
108 |
109 | fuqit stop
110 | """
111 |
112 |
113 | def status_command():
114 | """
115 | Tells you if a running fuqit service is running or not.
116 |
117 | fuqit status
118 | """
119 |
120 |
121 |
122 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | The FuqIt Web Framework
2 | =======================
3 |
4 | I'm kind of tired of following the stupid rules of MVC and want to just
5 | try out making something where I shit files into a directory and they
6 | just work. This Python web framework (if you can call it that) is my
7 | first crack at something like that. I basically did this on a Saturday
8 | morning because I was bored. If you don't like it, then oh well, life
9 | goes on.
10 |
11 | How It Works
12 | ============
13 |
14 | 1. fuqit init -into test
15 | 2. ./bin/fuqit run -chroot test
16 | 3. Browser http://localhost:8000/
17 |
18 | That's it. Look in this project's test/app/ directory to see me
19 | doing stupid crap with it to see if it works.
20 |
21 | Resolution Order
22 | ================
23 |
24 | Easy, and designed to treat raw .html Jinja2 templates the same as a .py file module:
25 |
26 | 0. If it's in /static/ it's a static file.
27 | 1. If it has an extension it's a template.
28 | 2. If it ends in / it's either /index.html or a module.
29 | 3. Otherwise it's a module named after the path with / changed to .
30 | unless there is a directory with the same path, then this will produce a redirect.
31 | 4. Modules are found by trying to import longest to shortest paths.
32 |
33 | Examples:
34 |
35 | * / -> /index.html or /index.py
36 | * /the/stupid/place/stuff.txt -> jinja template same path
37 | * /the/other/place/index.html -> same thing
38 | * /mystuff/cool -> a module named app.mystuff.cool
39 | * /dir/that/exists -> redirect to /dir/that/exists/
40 | * /mymodule/the/path/after/that -> import mymodule.py give it sub_path=/the/path/after/that
41 |
42 | It uses the python mimetypes module to figure out mimetypes by extension. No, I don't
43 | know how to add new extensions to it.
44 |
45 | Sessions
46 | ========
47 |
48 | It has ephemeral sessions based on cookies, which means that they go away when you reboot the
49 | process. To use sessions you can either use them raw from fuqit/sessions.py or just do this:
50 |
51 | from fuqit.sessions import with_session
52 |
53 | @with_session
54 | def GET(variables, session):
55 | session['count'] = session.get('count', 1) + 1
56 | return "COUNT: %d" % session['count']
57 |
58 |
59 | Using It
60 | ========
61 |
62 | You can play with the example by doing this::
63 |
64 | ./bin/fuqit run -chroot examples/test -app app
65 |
66 | Then go to http://127.0.0.1:8000/ and you'll get my little demo testing app.
67 | It's in the app directory and just has some files for testing the rendering.
68 |
69 | You can get help for run with:
70 |
71 | ./bin/fuqit help -for run
72 |
73 |
74 | Writing A .html Handler
75 | =======================
76 |
77 | Here's how you do it:
78 |
79 | 1. Make a .html file in the app directory.
80 | 2. Put jinja2 syntax stuff in it.
81 | 3. You get a web variable to play with that has all the gear, including sessions, headers, response\_headers, and a module function for easily loading code.
82 | 4. Hit it with a browser. That's it.
83 |
84 |
85 | Writing A Module Handler
86 | ========================
87 |
88 | Here's how you do it:
89 |
90 | 1. Make a .py file with the name you want.
91 | 2. Put either a run, GET, or POST method in it. run handles every possible request, GET or POST handles just those.
92 | 3. Your method will get one parameter, web, which has .session, .headers, .app, and everything.
93 | 4. Return a string for the body; a body, code, headers tuple; or just call web.app.render("somefuqitpath.html", web)
94 |
95 | Remember that if you have say /myapp.py and you get a URL of /myapp/stuff/things, then this module will run and it'll get
96 | a web.sub\_path == '/stuff/things'.
97 |
98 | Static Files
99 | ============
100 |
101 | Here's how you do it:
102 |
103 | 1. mkdir app/static
104 | 2. Put the static crap in there.
105 | 3. Any URL with /static/ in front serves out of there.
106 |
107 | If you actually host it you should have your fronting web server serve them straight out of there.
108 |
109 | Databasing
110 | ==========
111 |
112 | FuqIt took the public domain web.py database module and uses that. The best docs for it is currently from
113 | the Web.py folks over at .
114 |
115 | You use a database by doing two things:
116 |
117 | 1. Edit the config.py file that fuqit makes for you. Inside there is an initial configuration that makes a db variable configured from the fuqit.data.database call.
118 | 2. Change the data.database call to use the database you want. It's currently setup to use a SQLite3 database.
119 |
120 | That's it. Once you have that configure, the web variable in templates and modules will have a web.db variable for you to do database stuff with.
121 |
122 | Web.py Database Modifications
123 | -----------------------------
124 |
125 | There are a few minor modifications to the default web.py that you need to know:
126 |
127 | 1. It's renamed data so that it can be more than just for databases and so it doesn't conflict with the db variable in config.py.
128 | 2. I added a web.db.get() function that's a reduced version of web.db.select() that is used to just get one record.
129 |
130 | As I evolve web.py's DB I'll just call it the fuqit.data API and document it differently.
131 |
132 | But That's Magic!
133 | =================
134 |
135 | I will refer you to our official Mascot:
136 |
137 | 
138 |
139 | Investor Statement
140 | ==================
141 |
142 | Do you have a load of money and are you looking for the next Meteor to waste it
143 | on? Well this project is currently looking for funding and it's already been
144 | on the top of HackerNews!
145 |
146 | 
147 |
148 | Act fast because pretty soon I'll have a spare Sunday and FuqIt will become
149 | more secure than both Meteor and Ruby On Rails and then you'll miss out on
150 | investing in the next hottest thing. You'll have to go buy some Gucci hand
151 | bags or a DVF dress to be in-fashion instead. Can't have that now.
152 |
153 |
154 |
--------------------------------------------------------------------------------
/fuqit/web.py:
--------------------------------------------------------------------------------
1 | # Fuqit Web Framework
2 | # Copyright (C) 2013 Zed A. Shaw
3 | #
4 | # This program is free software: you can redistribute it and/or modify
5 | # it under the terms of the GNU Affero General Public License as published by
6 | # the Free Software Foundation, either version 3 of the License, or
7 | # (at your option) any later version.
8 | #
9 | # This program is distributed in the hope that it will be useful,
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | # GNU Affero General Public License for more details.
13 | #
14 | # You should have received a copy of the GNU Affero General Public License
15 | # along with this program. If not, see .
16 |
17 | import os
18 | from jinja2 import Environment, PackageLoader, TemplateNotFound
19 | from fuqit import tools, sessions
20 | import re
21 | import traceback
22 | import os
23 |
24 | config = None # this gets set by calling configure below
25 |
26 | class RequestDict(dict):
27 | __getattr__ = dict.__getitem__
28 |
29 |
30 | def render_error(code, message="", variables=None):
31 | try:
32 | return render_template(config.errors_dir + '%d.html' %
33 | code, variables or {}, ext='.html')
34 | except TemplateNotFound:
35 | return message, code, {}
36 |
37 | def csrf_check(context):
38 | referer = context['headers'].get('referer', '')
39 |
40 | if referer:
41 | return config.allowed_referer.match(referer)
42 | else:
43 | return True
44 |
45 | def process(method, path, params, context):
46 | if not csrf_check(context):
47 | return render_error(404, "Not Found")
48 |
49 | try:
50 | return render(path, context)
51 | except TemplateNotFound:
52 | print "Jinja2 template missing in path: %r for context %r" % (path, context)
53 | traceback.print_exc()
54 | return render_error(404, "Not Found")
55 | except Exception as e:
56 | traceback.print_exc()
57 | return render_error(500, str(e))
58 |
59 | def render_template(path, variables, ext=None):
60 | ext = ext or os.path.splitext(path)[1]
61 | headers = tools.make_ctype(ext, config.default_mtype)
62 |
63 | if 'headers' in variables:
64 | sessions.load_session(variables)
65 |
66 | context = {'web': variables,
67 | 'module': tools.module,
68 | 'response_headers': headers,
69 | 'config': config,
70 | 'db': config.db, # it's so common
71 | }
72 |
73 | template = config.env.get_template(path)
74 | result = template.render(**context)
75 |
76 | if 'headers' in variables:
77 | sessions.save_session(variables, headers)
78 |
79 | return result, 200, headers
80 |
81 |
82 | def render_module(name, variables):
83 | base, target = tools.find_longest_module(config.app_moudle, name, variables)
84 |
85 | if not (base and target):
86 | return render_error(404, "Not Found", variables=variables)
87 |
88 | variables['base_path'] = base
89 | variables['sub_path'] = name[len(base)+1:]
90 | sessions.load_session(variables)
91 |
92 | context = RequestDict(variables)
93 |
94 | if target:
95 | try:
96 | actions = target.__dict__
97 | # TODO: need to white-list context.method
98 | func = actions.get(context.method, None) or actions['run']
99 | except KeyError:
100 | return render_error(500, 'No run method or %s method.' %
101 | context.method)
102 |
103 | result = func(context)
104 |
105 | session_headers = {}
106 | sessions.save_session(variables, session_headers)
107 |
108 | if isinstance(result, tuple):
109 | body, code, headers = result
110 | headers.update(session_headers)
111 | return body, code, headers
112 | else:
113 | session_headers['Content-type'] = config.default_mtype
114 | return result, 200, session_headers
115 | else:
116 | return render_error(404, "Not Found", variables=variables)
117 |
118 | def render_static(ext, path):
119 | # stupid inefficient, but that's what you get
120 | headers = tools.make_ctype(ext, config.default_mtype)
121 |
122 | try:
123 | return open(path).read(), 200, headers
124 | except IOError:
125 | return render_error(404, "Not Found")
126 |
127 | def render(path, variables):
128 | assert config, "You need to call fuqit.web.configure."
129 |
130 | root, ext = os.path.splitext(path)
131 | realpath = os.path.realpath(config.app_path + path)
132 |
133 | if not realpath.startswith(config.app_path) or ext == ".py":
134 | # prevent access outside the app dir by comparing path roots
135 | return render_error(404, "Not Found", variables=variables)
136 |
137 | elif realpath.startswith(config.static_dir):
138 | return render_static(ext, realpath)
139 |
140 | elif ext:
141 | # if it has an extension it's a template
142 | return render_template(path, variables, ext=ext)
143 |
144 | elif path.endswith('/'):
145 | # if it ends in /, it's a /index.html or /index.py
146 | base = os.path.join(path, 'index')
147 |
148 | #! this will be hackable if you get rid of the realpath check at top
149 | if os.path.exists(config.app_path + base + '.html'):
150 | return render_template(base + '.html', variables, ext='.html')
151 | else:
152 | return render_module(path[:-1], variables)
153 |
154 | elif os.path.isdir(realpath):
155 | return "", 301, {'Location': path + '/'}
156 |
157 | else:
158 | # otherwise it's a module, tack on .py and load or fail
159 | return render_module(path, variables)
160 |
161 | def redirect(path):
162 | """
163 | Simple redirect function for most of the interaction you need to do.
164 | """
165 | return "", 301, {'Location': path}
166 |
167 | def error(code, message):
168 | return render_error(code, message)
169 |
170 | def configure(app_module="app", config_module="config"):
171 | global config
172 |
173 | if not config:
174 | config = tools.module(config_module)
175 | config.app_module = app_module
176 | config.app_path = os.path.realpath(app_module)
177 | config.errors_dir = config.app_path + '/errors/'
178 | config.env = Environment(loader=PackageLoader(config.app_module, '.'))
179 | config.allowed_referer = re.compile(config.allowed_referer)
180 | config.static_dir = os.path.realpath(config.app_path +
181 | (config.static_dir or '/static/'))
182 |
183 |
184 |
--------------------------------------------------------------------------------
/fuqit/data/utils.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env python
2 | """
3 | General Utilities taken from web.py for use with the db.py file.
4 | """
5 |
6 | __all__ = [
7 | "storage", "storify",
8 | "iters",
9 | "safeunicode", "safestr",
10 | "iterbetter",
11 | "threadeddict",
12 | ]
13 |
14 | import itertools
15 | from threading import local as threadlocal
16 |
17 | class storage(dict):
18 | """
19 | A storage object is like a dictionary except `obj.foo` can be used
20 | in addition to `obj['foo']`.
21 |
22 | >>> o = storage(a=1)
23 | >>> o.a
24 | 1
25 | >>> o['a']
26 | 1
27 | >>> o.a = 2
28 | >>> o['a']
29 | 2
30 | >>> del o.a
31 | >>> o.a
32 | Traceback (most recent call last):
33 | ...
34 | AttributeError: 'a'
35 |
36 | """
37 | def __getattr__(self, key):
38 | try:
39 | return self[key]
40 | except KeyError, k:
41 | raise AttributeError, k
42 |
43 | def __setattr__(self, key, value):
44 | self[key] = value
45 |
46 | def __delattr__(self, key):
47 | try:
48 | del self[key]
49 | except KeyError, k:
50 | raise AttributeError, k
51 |
52 | def __repr__(self):
53 | return ''
54 |
55 | def storify(mapping, *requireds, **defaults):
56 | """
57 | Creates a `storage` object from dictionary `mapping`, raising `KeyError` if
58 | d doesn't have all of the keys in `requireds` and using the default
59 | values for keys found in `defaults`.
60 |
61 | For example, `storify({'a':1, 'c':3}, b=2, c=0)` will return the equivalent of
62 | `storage({'a':1, 'b':2, 'c':3})`.
63 |
64 | If a `storify` value is a list (e.g. multiple values in a form submission),
65 | `storify` returns the last element of the list, unless the key appears in
66 | `defaults` as a list. Thus:
67 |
68 | >>> storify({'a':[1, 2]}).a
69 | 2
70 | >>> storify({'a':[1, 2]}, a=[]).a
71 | [1, 2]
72 | >>> storify({'a':1}, a=[]).a
73 | [1]
74 | >>> storify({}, a=[]).a
75 | []
76 |
77 | Similarly, if the value has a `value` attribute, `storify will return _its_
78 | value, unless the key appears in `defaults` as a dictionary.
79 |
80 | >>> storify({'a':storage(value=1)}).a
81 | 1
82 | >>> storify({'a':storage(value=1)}, a={}).a
83 |
84 | >>> storify({}, a={}).a
85 | {}
86 |
87 | Optionally, keyword parameter `_unicode` can be passed to convert all values to unicode.
88 |
89 | >>> storify({'x': 'a'}, _unicode=True)
90 |
91 | >>> storify({'x': storage(value='a')}, x={}, _unicode=True)
92 | }>
93 | >>> storify({'x': storage(value='a')}, _unicode=True)
94 |
95 | """
96 | _unicode = defaults.pop('_unicode', False)
97 |
98 | # if _unicode is callable object, use it convert a string to unicode.
99 | to_unicode = safeunicode
100 | if _unicode is not False and hasattr(_unicode, "__call__"):
101 | to_unicode = _unicode
102 |
103 | def unicodify(s):
104 | if _unicode and isinstance(s, str): return to_unicode(s)
105 | else: return s
106 |
107 | def getvalue(x):
108 | if hasattr(x, 'file') and hasattr(x, 'value'):
109 | return x.value
110 | elif hasattr(x, 'value'):
111 | return unicodify(x.value)
112 | else:
113 | return unicodify(x)
114 |
115 | stor = storage()
116 | for key in requireds + tuple(mapping.keys()):
117 | value = mapping[key]
118 | if isinstance(value, list):
119 | if isinstance(defaults.get(key), list):
120 | value = [getvalue(x) for x in value]
121 | else:
122 | value = value[-1]
123 | if not isinstance(defaults.get(key), dict):
124 | value = getvalue(value)
125 | if isinstance(defaults.get(key), list) and not isinstance(value, list):
126 | value = [value]
127 | setattr(stor, key, value)
128 |
129 | for (key, value) in defaults.iteritems():
130 | result = value
131 | if hasattr(stor, key):
132 | result = stor[key]
133 | if value == () and not isinstance(result, tuple):
134 | result = (result,)
135 | setattr(stor, key, result)
136 |
137 | return stor
138 |
139 | iters = (list, tuple, set, frozenset)
140 |
141 | def safeunicode(obj, encoding='utf-8'):
142 | r"""
143 | Converts any given object to unicode string.
144 |
145 | >>> safeunicode('hello')
146 | u'hello'
147 | >>> safeunicode(2)
148 | u'2'
149 | >>> safeunicode('\xe1\x88\xb4')
150 | u'\u1234'
151 | """
152 | t = type(obj)
153 | if t is unicode:
154 | return obj
155 | elif t is str:
156 | return obj.decode(encoding)
157 | elif t in [int, float, bool]:
158 | return unicode(obj)
159 | elif hasattr(obj, '__unicode__') or isinstance(obj, unicode):
160 | return unicode(obj)
161 | else:
162 | return str(obj).decode(encoding)
163 |
164 | def safestr(obj, encoding='utf-8'):
165 | r"""
166 | Converts any given object to utf-8 encoded string.
167 |
168 | >>> safestr('hello')
169 | 'hello'
170 | >>> safestr(u'\u1234')
171 | '\xe1\x88\xb4'
172 | >>> safestr(2)
173 | '2'
174 | """
175 | if isinstance(obj, unicode):
176 | return obj.encode(encoding)
177 | elif isinstance(obj, str):
178 | return obj
179 | elif hasattr(obj, 'next'): # iterator
180 | return itertools.imap(safestr, obj)
181 | else:
182 | return str(obj)
183 |
184 | class iterbetter:
185 | """
186 | Returns an object that can be used as an iterator
187 | but can also be used via __getitem__ (although it
188 | cannot go backwards -- that is, you cannot request
189 | `iterbetter[0]` after requesting `iterbetter[1]`).
190 |
191 | >>> import itertools
192 | >>> c = iterbetter(itertools.count())
193 | >>> c[1]
194 | 1
195 | >>> c[5]
196 | 5
197 | >>> c[3]
198 | Traceback (most recent call last):
199 | ...
200 | IndexError: already passed 3
201 |
202 | For boolean test, iterbetter peeps at first value in the itertor without effecting the iteration.
203 |
204 | >>> c = iterbetter(iter(range(5)))
205 | >>> bool(c)
206 | True
207 | >>> list(c)
208 | [0, 1, 2, 3, 4]
209 | >>> c = iterbetter(iter([]))
210 | >>> bool(c)
211 | False
212 | >>> list(c)
213 | []
214 | """
215 | def __init__(self, iterator):
216 | self.i, self.c = iterator, 0
217 |
218 | def __iter__(self):
219 | if hasattr(self, "_head"):
220 | yield self._head
221 |
222 | while 1:
223 | yield self.i.next()
224 | self.c += 1
225 |
226 | def __getitem__(self, i):
227 | #todo: slices
228 | if i < self.c:
229 | raise IndexError, "already passed "+str(i)
230 | try:
231 | while i > self.c:
232 | self.i.next()
233 | self.c += 1
234 | # now self.c == i
235 | self.c += 1
236 | return self.i.next()
237 | except StopIteration:
238 | raise IndexError, str(i)
239 |
240 | def __nonzero__(self):
241 | if hasattr(self, "__len__"):
242 | return len(self) != 0
243 | elif hasattr(self, "_head"):
244 | return True
245 | else:
246 | try:
247 | self._head = self.i.next()
248 | except StopIteration:
249 | return False
250 | else:
251 | return True
252 |
253 | class threadeddict(threadlocal):
254 | """
255 | Thread local storage.
256 |
257 | >>> d = threadeddict()
258 | >>> d.x = 1
259 | >>> d.x
260 | 1
261 | >>> import threading
262 | >>> def f(): d.x = 2
263 | ...
264 | >>> t = threading.Thread(target=f)
265 | >>> t.start()
266 | >>> t.join()
267 | >>> d.x
268 | 1
269 | """
270 | _instances = set()
271 |
272 | def __init__(self):
273 | threadeddict._instances.add(self)
274 |
275 | def __del__(self):
276 | threadeddict._instances.remove(self)
277 |
278 | def __hash__(self):
279 | return id(self)
280 |
281 | def clear_all():
282 | """Clears all threadeddict instances.
283 | """
284 | for t in list(threadeddict._instances):
285 | t.clear()
286 | clear_all = staticmethod(clear_all)
287 |
288 | # Define all these methods to more or less fully emulate dict -- attribute access
289 | # is built into threading.local.
290 |
291 | def __getitem__(self, key):
292 | return self.__dict__[key]
293 |
294 | def __setitem__(self, key, value):
295 | self.__dict__[key] = value
296 |
297 | def __delitem__(self, key):
298 | del self.__dict__[key]
299 |
300 | def __contains__(self, key):
301 | return key in self.__dict__
302 |
303 | has_key = __contains__
304 |
305 | def clear(self):
306 | self.__dict__.clear()
307 |
308 | def copy(self):
309 | return self.__dict__.copy()
310 |
311 | def get(self, key, default=None):
312 | return self.__dict__.get(key, default)
313 |
314 | def items(self):
315 | return self.__dict__.items()
316 |
317 | def iteritems(self):
318 | return self.__dict__.iteritems()
319 |
320 | def keys(self):
321 | return self.__dict__.keys()
322 |
323 | def iterkeys(self):
324 | return self.__dict__.iterkeys()
325 |
326 | iter = iterkeys
327 |
328 | def values(self):
329 | return self.__dict__.values()
330 |
331 | def itervalues(self):
332 | return self.__dict__.itervalues()
333 |
334 | def pop(self, key, *args):
335 | return self.__dict__.pop(key, *args)
336 |
337 | def popitem(self):
338 | return self.__dict__.popitem()
339 |
340 | def setdefault(self, key, default=None):
341 | return self.__dict__.setdefault(key, default)
342 |
343 | def update(self, *args, **kwargs):
344 | self.__dict__.update(*args, **kwargs)
345 |
346 | def __repr__(self):
347 | return '' % self.__dict__
348 |
349 | __str__ = __repr__
350 |
351 |
352 | if __name__ == "__main__":
353 | import doctest
354 | doctest.testmod()
355 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU AFFERO GENERAL PUBLIC LICENSE
2 | Version 3, 19 November 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU Affero General Public License is a free, copyleft license for
11 | software and other kinds of works, specifically designed to ensure
12 | cooperation with the community in the case of network server software.
13 |
14 | The licenses for most software and other practical works are designed
15 | to take away your freedom to share and change the works. By contrast,
16 | our General Public Licenses are intended to guarantee your freedom to
17 | share and change all versions of a program--to make sure it remains free
18 | software for all its users.
19 |
20 | When we speak of free software, we are referring to freedom, not
21 | price. Our General Public Licenses are designed to make sure that you
22 | have the freedom to distribute copies of free software (and charge for
23 | them if you wish), that you receive source code or can get it if you
24 | want it, that you can change the software or use pieces of it in new
25 | free programs, and that you know you can do these things.
26 |
27 | Developers that use our General Public Licenses protect your rights
28 | with two steps: (1) assert copyright on the software, and (2) offer
29 | you this License which gives you legal permission to copy, distribute
30 | and/or modify the software.
31 |
32 | A secondary benefit of defending all users' freedom is that
33 | improvements made in alternate versions of the program, if they
34 | receive widespread use, become available for other developers to
35 | incorporate. Many developers of free software are heartened and
36 | encouraged by the resulting cooperation. However, in the case of
37 | software used on network servers, this result may fail to come about.
38 | The GNU General Public License permits making a modified version and
39 | letting the public access it on a server without ever releasing its
40 | source code to the public.
41 |
42 | The GNU Affero General Public License is designed specifically to
43 | ensure that, in such cases, the modified source code becomes available
44 | to the community. It requires the operator of a network server to
45 | provide the source code of the modified version running there to the
46 | users of that server. Therefore, public use of a modified version, on
47 | a publicly accessible server, gives the public access to the source
48 | code of the modified version.
49 |
50 | An older license, called the Affero General Public License and
51 | published by Affero, was designed to accomplish similar goals. This is
52 | a different license, not a version of the Affero GPL, but Affero has
53 | released a new version of the Affero GPL which permits relicensing under
54 | this license.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | TERMS AND CONDITIONS
60 |
61 | 0. Definitions.
62 |
63 | "This License" refers to version 3 of the GNU Affero General Public License.
64 |
65 | "Copyright" also means copyright-like laws that apply to other kinds of
66 | works, such as semiconductor masks.
67 |
68 | "The Program" refers to any copyrightable work licensed under this
69 | License. Each licensee is addressed as "you". "Licensees" and
70 | "recipients" may be individuals or organizations.
71 |
72 | To "modify" a work means to copy from or adapt all or part of the work
73 | in a fashion requiring copyright permission, other than the making of an
74 | exact copy. The resulting work is called a "modified version" of the
75 | earlier work or a work "based on" the earlier work.
76 |
77 | A "covered work" means either the unmodified Program or a work based
78 | on the Program.
79 |
80 | To "propagate" a work means to do anything with it that, without
81 | permission, would make you directly or secondarily liable for
82 | infringement under applicable copyright law, except executing it on a
83 | computer or modifying a private copy. Propagation includes copying,
84 | distribution (with or without modification), making available to the
85 | public, and in some countries other activities as well.
86 |
87 | To "convey" a work means any kind of propagation that enables other
88 | parties to make or receive copies. Mere interaction with a user through
89 | a computer network, with no transfer of a copy, is not conveying.
90 |
91 | An interactive user interface displays "Appropriate Legal Notices"
92 | to the extent that it includes a convenient and prominently visible
93 | feature that (1) displays an appropriate copyright notice, and (2)
94 | tells the user that there is no warranty for the work (except to the
95 | extent that warranties are provided), that licensees may convey the
96 | work under this License, and how to view a copy of this License. If
97 | the interface presents a list of user commands or options, such as a
98 | menu, a prominent item in the list meets this criterion.
99 |
100 | 1. Source Code.
101 |
102 | The "source code" for a work means the preferred form of the work
103 | for making modifications to it. "Object code" means any non-source
104 | form of a work.
105 |
106 | A "Standard Interface" means an interface that either is an official
107 | standard defined by a recognized standards body, or, in the case of
108 | interfaces specified for a particular programming language, one that
109 | is widely used among developers working in that language.
110 |
111 | The "System Libraries" of an executable work include anything, other
112 | than the work as a whole, that (a) is included in the normal form of
113 | packaging a Major Component, but which is not part of that Major
114 | Component, and (b) serves only to enable use of the work with that
115 | Major Component, or to implement a Standard Interface for which an
116 | implementation is available to the public in source code form. A
117 | "Major Component", in this context, means a major essential component
118 | (kernel, window system, and so on) of the specific operating system
119 | (if any) on which the executable work runs, or a compiler used to
120 | produce the work, or an object code interpreter used to run it.
121 |
122 | The "Corresponding Source" for a work in object code form means all
123 | the source code needed to generate, install, and (for an executable
124 | work) run the object code and to modify the work, including scripts to
125 | control those activities. However, it does not include the work's
126 | System Libraries, or general-purpose tools or generally available free
127 | programs which are used unmodified in performing those activities but
128 | which are not part of the work. For example, Corresponding Source
129 | includes interface definition files associated with source files for
130 | the work, and the source code for shared libraries and dynamically
131 | linked subprograms that the work is specifically designed to require,
132 | such as by intimate data communication or control flow between those
133 | subprograms and other parts of the work.
134 |
135 | The Corresponding Source need not include anything that users
136 | can regenerate automatically from other parts of the Corresponding
137 | Source.
138 |
139 | The Corresponding Source for a work in source code form is that
140 | same work.
141 |
142 | 2. Basic Permissions.
143 |
144 | All rights granted under this License are granted for the term of
145 | copyright on the Program, and are irrevocable provided the stated
146 | conditions are met. This License explicitly affirms your unlimited
147 | permission to run the unmodified Program. The output from running a
148 | covered work is covered by this License only if the output, given its
149 | content, constitutes a covered work. This License acknowledges your
150 | rights of fair use or other equivalent, as provided by copyright law.
151 |
152 | You may make, run and propagate covered works that you do not
153 | convey, without conditions so long as your license otherwise remains
154 | in force. You may convey covered works to others for the sole purpose
155 | of having them make modifications exclusively for you, or provide you
156 | with facilities for running those works, provided that you comply with
157 | the terms of this License in conveying all material for which you do
158 | not control copyright. Those thus making or running the covered works
159 | for you must do so exclusively on your behalf, under your direction
160 | and control, on terms that prohibit them from making any copies of
161 | your copyrighted material outside their relationship with you.
162 |
163 | Conveying under any other circumstances is permitted solely under
164 | the conditions stated below. Sublicensing is not allowed; section 10
165 | makes it unnecessary.
166 |
167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168 |
169 | No covered work shall be deemed part of an effective technological
170 | measure under any applicable law fulfilling obligations under article
171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
172 | similar laws prohibiting or restricting circumvention of such
173 | measures.
174 |
175 | When you convey a covered work, you waive any legal power to forbid
176 | circumvention of technological measures to the extent such circumvention
177 | is effected by exercising rights under this License with respect to
178 | the covered work, and you disclaim any intention to limit operation or
179 | modification of the work as a means of enforcing, against the work's
180 | users, your or third parties' legal rights to forbid circumvention of
181 | technological measures.
182 |
183 | 4. Conveying Verbatim Copies.
184 |
185 | You may convey verbatim copies of the Program's source code as you
186 | receive it, in any medium, provided that you conspicuously and
187 | appropriately publish on each copy an appropriate copyright notice;
188 | keep intact all notices stating that this License and any
189 | non-permissive terms added in accord with section 7 apply to the code;
190 | keep intact all notices of the absence of any warranty; and give all
191 | recipients a copy of this License along with the Program.
192 |
193 | You may charge any price or no price for each copy that you convey,
194 | and you may offer support or warranty protection for a fee.
195 |
196 | 5. Conveying Modified Source Versions.
197 |
198 | You may convey a work based on the Program, or the modifications to
199 | produce it from the Program, in the form of source code under the
200 | terms of section 4, provided that you also meet all of these conditions:
201 |
202 | a) The work must carry prominent notices stating that you modified
203 | it, and giving a relevant date.
204 |
205 | b) The work must carry prominent notices stating that it is
206 | released under this License and any conditions added under section
207 | 7. This requirement modifies the requirement in section 4 to
208 | "keep intact all notices".
209 |
210 | c) You must license the entire work, as a whole, under this
211 | License to anyone who comes into possession of a copy. This
212 | License will therefore apply, along with any applicable section 7
213 | additional terms, to the whole of the work, and all its parts,
214 | regardless of how they are packaged. This License gives no
215 | permission to license the work in any other way, but it does not
216 | invalidate such permission if you have separately received it.
217 |
218 | d) If the work has interactive user interfaces, each must display
219 | Appropriate Legal Notices; however, if the Program has interactive
220 | interfaces that do not display Appropriate Legal Notices, your
221 | work need not make them do so.
222 |
223 | A compilation of a covered work with other separate and independent
224 | works, which are not by their nature extensions of the covered work,
225 | and which are not combined with it such as to form a larger program,
226 | in or on a volume of a storage or distribution medium, is called an
227 | "aggregate" if the compilation and its resulting copyright are not
228 | used to limit the access or legal rights of the compilation's users
229 | beyond what the individual works permit. Inclusion of a covered work
230 | in an aggregate does not cause this License to apply to the other
231 | parts of the aggregate.
232 |
233 | 6. Conveying Non-Source Forms.
234 |
235 | You may convey a covered work in object code form under the terms
236 | of sections 4 and 5, provided that you also convey the
237 | machine-readable Corresponding Source under the terms of this License,
238 | in one of these ways:
239 |
240 | a) Convey the object code in, or embodied in, a physical product
241 | (including a physical distribution medium), accompanied by the
242 | Corresponding Source fixed on a durable physical medium
243 | customarily used for software interchange.
244 |
245 | b) Convey the object code in, or embodied in, a physical product
246 | (including a physical distribution medium), accompanied by a
247 | written offer, valid for at least three years and valid for as
248 | long as you offer spare parts or customer support for that product
249 | model, to give anyone who possesses the object code either (1) a
250 | copy of the Corresponding Source for all the software in the
251 | product that is covered by this License, on a durable physical
252 | medium customarily used for software interchange, for a price no
253 | more than your reasonable cost of physically performing this
254 | conveying of source, or (2) access to copy the
255 | Corresponding Source from a network server at no charge.
256 |
257 | c) Convey individual copies of the object code with a copy of the
258 | written offer to provide the Corresponding Source. This
259 | alternative is allowed only occasionally and noncommercially, and
260 | only if you received the object code with such an offer, in accord
261 | with subsection 6b.
262 |
263 | d) Convey the object code by offering access from a designated
264 | place (gratis or for a charge), and offer equivalent access to the
265 | Corresponding Source in the same way through the same place at no
266 | further charge. You need not require recipients to copy the
267 | Corresponding Source along with the object code. If the place to
268 | copy the object code is a network server, the Corresponding Source
269 | may be on a different server (operated by you or a third party)
270 | that supports equivalent copying facilities, provided you maintain
271 | clear directions next to the object code saying where to find the
272 | Corresponding Source. Regardless of what server hosts the
273 | Corresponding Source, you remain obligated to ensure that it is
274 | available for as long as needed to satisfy these requirements.
275 |
276 | e) Convey the object code using peer-to-peer transmission, provided
277 | you inform other peers where the object code and Corresponding
278 | Source of the work are being offered to the general public at no
279 | charge under subsection 6d.
280 |
281 | A separable portion of the object code, whose source code is excluded
282 | from the Corresponding Source as a System Library, need not be
283 | included in conveying the object code work.
284 |
285 | A "User Product" is either (1) a "consumer product", which means any
286 | tangible personal property which is normally used for personal, family,
287 | or household purposes, or (2) anything designed or sold for incorporation
288 | into a dwelling. In determining whether a product is a consumer product,
289 | doubtful cases shall be resolved in favor of coverage. For a particular
290 | product received by a particular user, "normally used" refers to a
291 | typical or common use of that class of product, regardless of the status
292 | of the particular user or of the way in which the particular user
293 | actually uses, or expects or is expected to use, the product. A product
294 | is a consumer product regardless of whether the product has substantial
295 | commercial, industrial or non-consumer uses, unless such uses represent
296 | the only significant mode of use of the product.
297 |
298 | "Installation Information" for a User Product means any methods,
299 | procedures, authorization keys, or other information required to install
300 | and execute modified versions of a covered work in that User Product from
301 | a modified version of its Corresponding Source. The information must
302 | suffice to ensure that the continued functioning of the modified object
303 | code is in no case prevented or interfered with solely because
304 | modification has been made.
305 |
306 | If you convey an object code work under this section in, or with, or
307 | specifically for use in, a User Product, and the conveying occurs as
308 | part of a transaction in which the right of possession and use of the
309 | User Product is transferred to the recipient in perpetuity or for a
310 | fixed term (regardless of how the transaction is characterized), the
311 | Corresponding Source conveyed under this section must be accompanied
312 | by the Installation Information. But this requirement does not apply
313 | if neither you nor any third party retains the ability to install
314 | modified object code on the User Product (for example, the work has
315 | been installed in ROM).
316 |
317 | The requirement to provide Installation Information does not include a
318 | requirement to continue to provide support service, warranty, or updates
319 | for a work that has been modified or installed by the recipient, or for
320 | the User Product in which it has been modified or installed. Access to a
321 | network may be denied when the modification itself materially and
322 | adversely affects the operation of the network or violates the rules and
323 | protocols for communication across the network.
324 |
325 | Corresponding Source conveyed, and Installation Information provided,
326 | in accord with this section must be in a format that is publicly
327 | documented (and with an implementation available to the public in
328 | source code form), and must require no special password or key for
329 | unpacking, reading or copying.
330 |
331 | 7. Additional Terms.
332 |
333 | "Additional permissions" are terms that supplement the terms of this
334 | License by making exceptions from one or more of its conditions.
335 | Additional permissions that are applicable to the entire Program shall
336 | be treated as though they were included in this License, to the extent
337 | that they are valid under applicable law. If additional permissions
338 | apply only to part of the Program, that part may be used separately
339 | under those permissions, but the entire Program remains governed by
340 | this License without regard to the additional permissions.
341 |
342 | When you convey a copy of a covered work, you may at your option
343 | remove any additional permissions from that copy, or from any part of
344 | it. (Additional permissions may be written to require their own
345 | removal in certain cases when you modify the work.) You may place
346 | additional permissions on material, added by you to a covered work,
347 | for which you have or can give appropriate copyright permission.
348 |
349 | Notwithstanding any other provision of this License, for material you
350 | add to a covered work, you may (if authorized by the copyright holders of
351 | that material) supplement the terms of this License with terms:
352 |
353 | a) Disclaiming warranty or limiting liability differently from the
354 | terms of sections 15 and 16 of this License; or
355 |
356 | b) Requiring preservation of specified reasonable legal notices or
357 | author attributions in that material or in the Appropriate Legal
358 | Notices displayed by works containing it; or
359 |
360 | c) Prohibiting misrepresentation of the origin of that material, or
361 | requiring that modified versions of such material be marked in
362 | reasonable ways as different from the original version; or
363 |
364 | d) Limiting the use for publicity purposes of names of licensors or
365 | authors of the material; or
366 |
367 | e) Declining to grant rights under trademark law for use of some
368 | trade names, trademarks, or service marks; or
369 |
370 | f) Requiring indemnification of licensors and authors of that
371 | material by anyone who conveys the material (or modified versions of
372 | it) with contractual assumptions of liability to the recipient, for
373 | any liability that these contractual assumptions directly impose on
374 | those licensors and authors.
375 |
376 | All other non-permissive additional terms are considered "further
377 | restrictions" within the meaning of section 10. If the Program as you
378 | received it, or any part of it, contains a notice stating that it is
379 | governed by this License along with a term that is a further
380 | restriction, you may remove that term. If a license document contains
381 | a further restriction but permits relicensing or conveying under this
382 | License, you may add to a covered work material governed by the terms
383 | of that license document, provided that the further restriction does
384 | not survive such relicensing or conveying.
385 |
386 | If you add terms to a covered work in accord with this section, you
387 | must place, in the relevant source files, a statement of the
388 | additional terms that apply to those files, or a notice indicating
389 | where to find the applicable terms.
390 |
391 | Additional terms, permissive or non-permissive, may be stated in the
392 | form of a separately written license, or stated as exceptions;
393 | the above requirements apply either way.
394 |
395 | 8. Termination.
396 |
397 | You may not propagate or modify a covered work except as expressly
398 | provided under this License. Any attempt otherwise to propagate or
399 | modify it is void, and will automatically terminate your rights under
400 | this License (including any patent licenses granted under the third
401 | paragraph of section 11).
402 |
403 | However, if you cease all violation of this License, then your
404 | license from a particular copyright holder is reinstated (a)
405 | provisionally, unless and until the copyright holder explicitly and
406 | finally terminates your license, and (b) permanently, if the copyright
407 | holder fails to notify you of the violation by some reasonable means
408 | prior to 60 days after the cessation.
409 |
410 | Moreover, your license from a particular copyright holder is
411 | reinstated permanently if the copyright holder notifies you of the
412 | violation by some reasonable means, this is the first time you have
413 | received notice of violation of this License (for any work) from that
414 | copyright holder, and you cure the violation prior to 30 days after
415 | your receipt of the notice.
416 |
417 | Termination of your rights under this section does not terminate the
418 | licenses of parties who have received copies or rights from you under
419 | this License. If your rights have been terminated and not permanently
420 | reinstated, you do not qualify to receive new licenses for the same
421 | material under section 10.
422 |
423 | 9. Acceptance Not Required for Having Copies.
424 |
425 | You are not required to accept this License in order to receive or
426 | run a copy of the Program. Ancillary propagation of a covered work
427 | occurring solely as a consequence of using peer-to-peer transmission
428 | to receive a copy likewise does not require acceptance. However,
429 | nothing other than this License grants you permission to propagate or
430 | modify any covered work. These actions infringe copyright if you do
431 | not accept this License. Therefore, by modifying or propagating a
432 | covered work, you indicate your acceptance of this License to do so.
433 |
434 | 10. Automatic Licensing of Downstream Recipients.
435 |
436 | Each time you convey a covered work, the recipient automatically
437 | receives a license from the original licensors, to run, modify and
438 | propagate that work, subject to this License. You are not responsible
439 | for enforcing compliance by third parties with this License.
440 |
441 | An "entity transaction" is a transaction transferring control of an
442 | organization, or substantially all assets of one, or subdividing an
443 | organization, or merging organizations. If propagation of a covered
444 | work results from an entity transaction, each party to that
445 | transaction who receives a copy of the work also receives whatever
446 | licenses to the work the party's predecessor in interest had or could
447 | give under the previous paragraph, plus a right to possession of the
448 | Corresponding Source of the work from the predecessor in interest, if
449 | the predecessor has it or can get it with reasonable efforts.
450 |
451 | You may not impose any further restrictions on the exercise of the
452 | rights granted or affirmed under this License. For example, you may
453 | not impose a license fee, royalty, or other charge for exercise of
454 | rights granted under this License, and you may not initiate litigation
455 | (including a cross-claim or counterclaim in a lawsuit) alleging that
456 | any patent claim is infringed by making, using, selling, offering for
457 | sale, or importing the Program or any portion of it.
458 |
459 | 11. Patents.
460 |
461 | A "contributor" is a copyright holder who authorizes use under this
462 | License of the Program or a work on which the Program is based. The
463 | work thus licensed is called the contributor's "contributor version".
464 |
465 | A contributor's "essential patent claims" are all patent claims
466 | owned or controlled by the contributor, whether already acquired or
467 | hereafter acquired, that would be infringed by some manner, permitted
468 | by this License, of making, using, or selling its contributor version,
469 | but do not include claims that would be infringed only as a
470 | consequence of further modification of the contributor version. For
471 | purposes of this definition, "control" includes the right to grant
472 | patent sublicenses in a manner consistent with the requirements of
473 | this License.
474 |
475 | Each contributor grants you a non-exclusive, worldwide, royalty-free
476 | patent license under the contributor's essential patent claims, to
477 | make, use, sell, offer for sale, import and otherwise run, modify and
478 | propagate the contents of its contributor version.
479 |
480 | In the following three paragraphs, a "patent license" is any express
481 | agreement or commitment, however denominated, not to enforce a patent
482 | (such as an express permission to practice a patent or covenant not to
483 | sue for patent infringement). To "grant" such a patent license to a
484 | party means to make such an agreement or commitment not to enforce a
485 | patent against the party.
486 |
487 | If you convey a covered work, knowingly relying on a patent license,
488 | and the Corresponding Source of the work is not available for anyone
489 | to copy, free of charge and under the terms of this License, through a
490 | publicly available network server or other readily accessible means,
491 | then you must either (1) cause the Corresponding Source to be so
492 | available, or (2) arrange to deprive yourself of the benefit of the
493 | patent license for this particular work, or (3) arrange, in a manner
494 | consistent with the requirements of this License, to extend the patent
495 | license to downstream recipients. "Knowingly relying" means you have
496 | actual knowledge that, but for the patent license, your conveying the
497 | covered work in a country, or your recipient's use of the covered work
498 | in a country, would infringe one or more identifiable patents in that
499 | country that you have reason to believe are valid.
500 |
501 | If, pursuant to or in connection with a single transaction or
502 | arrangement, you convey, or propagate by procuring conveyance of, a
503 | covered work, and grant a patent license to some of the parties
504 | receiving the covered work authorizing them to use, propagate, modify
505 | or convey a specific copy of the covered work, then the patent license
506 | you grant is automatically extended to all recipients of the covered
507 | work and works based on it.
508 |
509 | A patent license is "discriminatory" if it does not include within
510 | the scope of its coverage, prohibits the exercise of, or is
511 | conditioned on the non-exercise of one or more of the rights that are
512 | specifically granted under this License. You may not convey a covered
513 | work if you are a party to an arrangement with a third party that is
514 | in the business of distributing software, under which you make payment
515 | to the third party based on the extent of your activity of conveying
516 | the work, and under which the third party grants, to any of the
517 | parties who would receive the covered work from you, a discriminatory
518 | patent license (a) in connection with copies of the covered work
519 | conveyed by you (or copies made from those copies), or (b) primarily
520 | for and in connection with specific products or compilations that
521 | contain the covered work, unless you entered into that arrangement,
522 | or that patent license was granted, prior to 28 March 2007.
523 |
524 | Nothing in this License shall be construed as excluding or limiting
525 | any implied license or other defenses to infringement that may
526 | otherwise be available to you under applicable patent law.
527 |
528 | 12. No Surrender of Others' Freedom.
529 |
530 | If conditions are imposed on you (whether by court order, agreement or
531 | otherwise) that contradict the conditions of this License, they do not
532 | excuse you from the conditions of this License. If you cannot convey a
533 | covered work so as to satisfy simultaneously your obligations under this
534 | License and any other pertinent obligations, then as a consequence you may
535 | not convey it at all. For example, if you agree to terms that obligate you
536 | to collect a royalty for further conveying from those to whom you convey
537 | the Program, the only way you could satisfy both those terms and this
538 | License would be to refrain entirely from conveying the Program.
539 |
540 | 13. Remote Network Interaction; Use with the GNU General Public License.
541 |
542 | Notwithstanding any other provision of this License, if you modify the
543 | Program, your modified version must prominently offer all users
544 | interacting with it remotely through a computer network (if your version
545 | supports such interaction) an opportunity to receive the Corresponding
546 | Source of your version by providing access to the Corresponding Source
547 | from a network server at no charge, through some standard or customary
548 | means of facilitating copying of software. This Corresponding Source
549 | shall include the Corresponding Source for any work covered by version 3
550 | of the GNU General Public License that is incorporated pursuant to the
551 | following paragraph.
552 |
553 | Notwithstanding any other provision of this License, you have
554 | permission to link or combine any covered work with a work licensed
555 | under version 3 of the GNU General Public License into a single
556 | combined work, and to convey the resulting work. The terms of this
557 | License will continue to apply to the part which is the covered work,
558 | but the work with which it is combined will remain governed by version
559 | 3 of the GNU General Public License.
560 |
561 | 14. Revised Versions of this License.
562 |
563 | The Free Software Foundation may publish revised and/or new versions of
564 | the GNU Affero General Public License from time to time. Such new versions
565 | will be similar in spirit to the present version, but may differ in detail to
566 | address new problems or concerns.
567 |
568 | Each version is given a distinguishing version number. If the
569 | Program specifies that a certain numbered version of the GNU Affero General
570 | Public License "or any later version" applies to it, you have the
571 | option of following the terms and conditions either of that numbered
572 | version or of any later version published by the Free Software
573 | Foundation. If the Program does not specify a version number of the
574 | GNU Affero General Public License, you may choose any version ever published
575 | by the Free Software Foundation.
576 |
577 | If the Program specifies that a proxy can decide which future
578 | versions of the GNU Affero General Public License can be used, that proxy's
579 | public statement of acceptance of a version permanently authorizes you
580 | to choose that version for the Program.
581 |
582 | Later license versions may give you additional or different
583 | permissions. However, no additional obligations are imposed on any
584 | author or copyright holder as a result of your choosing to follow a
585 | later version.
586 |
587 | 15. Disclaimer of Warranty.
588 |
589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597 |
598 | 16. Limitation of Liability.
599 |
600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608 | SUCH DAMAGES.
609 |
610 | 17. Interpretation of Sections 15 and 16.
611 |
612 | If the disclaimer of warranty and limitation of liability provided
613 | above cannot be given local legal effect according to their terms,
614 | reviewing courts shall apply local law that most closely approximates
615 | an absolute waiver of all civil liability in connection with the
616 | Program, unless a warranty or assumption of liability accompanies a
617 | copy of the Program in return for a fee.
618 |
619 |
--------------------------------------------------------------------------------
/fuqit/data/__init__.py:
--------------------------------------------------------------------------------
1 | """
2 | Database API taken from web.py for use with fuqit.
3 | """
4 |
5 | __all__ = [
6 | "UnknownParamstyle", "UnknownDB", "TransactionError",
7 | "sqllist", "sqlors", "reparam", "sqlquote",
8 | "SQLQuery", "SQLParam", "sqlparam",
9 | "SQLLiteral", "sqlliteral",
10 | "database", 'DB',
11 | ]
12 |
13 | import time, os, urllib
14 | import datetime
15 | from utils import threadeddict, storage, iters, iterbetter, safestr, safeunicode
16 | import sys
17 | debug = sys.stderr
18 | config = storage()
19 |
20 | class UnknownDB(Exception):
21 | """raised for unsupported dbms"""
22 | pass
23 |
24 | class _ItplError(ValueError):
25 | def __init__(self, text, pos):
26 | ValueError.__init__(self)
27 | self.text = text
28 | self.pos = pos
29 | def __str__(self):
30 | return "unfinished expression in %s at char %d" % (
31 | repr(self.text), self.pos)
32 |
33 | class TransactionError(Exception): pass
34 |
35 | class UnknownParamstyle(Exception):
36 | """
37 | raised for unsupported db paramstyles
38 |
39 | (currently supported: qmark, numeric, format, pyformat)
40 | """
41 | pass
42 |
43 | class SQLParam(object):
44 | """
45 | Parameter in SQLQuery.
46 |
47 | >>> q = SQLQuery(["SELECT * FROM test WHERE name=", SQLParam("joe")])
48 | >>> q
49 |
50 | >>> q.query()
51 | 'SELECT * FROM test WHERE name=%s'
52 | >>> q.values()
53 | ['joe']
54 | """
55 | __slots__ = ["value"]
56 |
57 | def __init__(self, value):
58 | self.value = value
59 |
60 | def get_marker(self, paramstyle='pyformat'):
61 | if paramstyle == 'qmark':
62 | return '?'
63 | elif paramstyle == 'numeric':
64 | return ':1'
65 | elif paramstyle is None or paramstyle in ['format', 'pyformat']:
66 | return '%s'
67 | raise UnknownParamstyle, paramstyle
68 |
69 | def sqlquery(self):
70 | return SQLQuery([self])
71 |
72 | def __add__(self, other):
73 | return self.sqlquery() + other
74 |
75 | def __radd__(self, other):
76 | return other + self.sqlquery()
77 |
78 | def __str__(self):
79 | return str(self.value)
80 |
81 | def __repr__(self):
82 | return '' % repr(self.value)
83 |
84 | sqlparam = SQLParam
85 |
86 | class SQLQuery(object):
87 | """
88 | You can pass this sort of thing as a clause in any db function.
89 | Otherwise, you can pass a dictionary to the keyword argument `vars`
90 | and the function will call reparam for you.
91 |
92 | Internally, consists of `items`, which is a list of strings and
93 | SQLParams, which get concatenated to produce the actual query.
94 | """
95 | __slots__ = ["items"]
96 |
97 | # tested in sqlquote's docstring
98 | def __init__(self, items=None):
99 | r"""Creates a new SQLQuery.
100 |
101 | >>> SQLQuery("x")
102 |
103 | >>> q = SQLQuery(['SELECT * FROM ', 'test', ' WHERE x=', SQLParam(1)])
104 | >>> q
105 |
106 | >>> q.query(), q.values()
107 | ('SELECT * FROM test WHERE x=%s', [1])
108 | >>> SQLQuery(SQLParam(1))
109 |
110 | """
111 | if items is None:
112 | self.items = []
113 | elif isinstance(items, list):
114 | self.items = items
115 | elif isinstance(items, SQLParam):
116 | self.items = [items]
117 | elif isinstance(items, SQLQuery):
118 | self.items = list(items.items)
119 | else:
120 | self.items = [items]
121 |
122 | # Take care of SQLLiterals
123 | for i, item in enumerate(self.items):
124 | if isinstance(item, SQLParam) and isinstance(item.value, SQLLiteral):
125 | self.items[i] = item.value.v
126 |
127 | def append(self, value):
128 | self.items.append(value)
129 |
130 | def __add__(self, other):
131 | if isinstance(other, basestring):
132 | items = [other]
133 | elif isinstance(other, SQLQuery):
134 | items = other.items
135 | else:
136 | return NotImplemented
137 | return SQLQuery(self.items + items)
138 |
139 | def __radd__(self, other):
140 | if isinstance(other, basestring):
141 | items = [other]
142 | else:
143 | return NotImplemented
144 |
145 | return SQLQuery(items + self.items)
146 |
147 | def __iadd__(self, other):
148 | if isinstance(other, (basestring, SQLParam)):
149 | self.items.append(other)
150 | elif isinstance(other, SQLQuery):
151 | self.items.extend(other.items)
152 | else:
153 | return NotImplemented
154 | return self
155 |
156 | def __len__(self):
157 | return len(self.query())
158 |
159 | def query(self, paramstyle=None):
160 | """
161 | Returns the query part of the sql query.
162 | >>> q = SQLQuery(["SELECT * FROM test WHERE name=", SQLParam('joe')])
163 | >>> q.query()
164 | 'SELECT * FROM test WHERE name=%s'
165 | >>> q.query(paramstyle='qmark')
166 | 'SELECT * FROM test WHERE name=?'
167 | """
168 | s = []
169 | for x in self.items:
170 | if isinstance(x, SQLParam):
171 | x = x.get_marker(paramstyle)
172 | s.append(safestr(x))
173 | else:
174 | x = safestr(x)
175 | # automatically escape % characters in the query
176 | # For backward compatability, ignore escaping when the query looks already escaped
177 | if paramstyle in ['format', 'pyformat']:
178 | if '%' in x and '%%' not in x:
179 | x = x.replace('%', '%%')
180 | s.append(x)
181 | return "".join(s)
182 |
183 | def values(self):
184 | """
185 | Returns the values of the parameters used in the sql query.
186 | >>> q = SQLQuery(["SELECT * FROM test WHERE name=", SQLParam('joe')])
187 | >>> q.values()
188 | ['joe']
189 | """
190 | return [i.value for i in self.items if isinstance(i, SQLParam)]
191 |
192 | def join(items, sep=' ', prefix=None, suffix=None, target=None):
193 | """
194 | Joins multiple queries.
195 |
196 | >>> SQLQuery.join(['a', 'b'], ', ')
197 |
198 |
199 | Optinally, prefix and suffix arguments can be provided.
200 |
201 | >>> SQLQuery.join(['a', 'b'], ', ', prefix='(', suffix=')')
202 |
203 |
204 | If target argument is provided, the items are appended to target instead of creating a new SQLQuery.
205 | """
206 | if target is None:
207 | target = SQLQuery()
208 |
209 | target_items = target.items
210 |
211 | if prefix:
212 | target_items.append(prefix)
213 |
214 | for i, item in enumerate(items):
215 | if i != 0:
216 | target_items.append(sep)
217 | if isinstance(item, SQLQuery):
218 | target_items.extend(item.items)
219 | else:
220 | target_items.append(item)
221 |
222 | if suffix:
223 | target_items.append(suffix)
224 | return target
225 |
226 | join = staticmethod(join)
227 |
228 | def _str(self):
229 | try:
230 | return self.query() % tuple([sqlify(x) for x in self.values()])
231 | except (ValueError, TypeError):
232 | return self.query()
233 |
234 | def __str__(self):
235 | return safestr(self._str())
236 |
237 | def __unicode__(self):
238 | return safeunicode(self._str())
239 |
240 | def __repr__(self):
241 | return '' % repr(str(self))
242 |
243 | class SQLLiteral:
244 | """
245 | Protects a string from `sqlquote`.
246 |
247 | >>> sqlquote('NOW()')
248 |
249 | >>> sqlquote(SQLLiteral('NOW()'))
250 |
251 | """
252 | def __init__(self, v):
253 | self.v = v
254 |
255 | def __repr__(self):
256 | return self.v
257 |
258 | sqlliteral = SQLLiteral
259 |
260 | def _sqllist(values):
261 | """
262 | >>> _sqllist([1, 2, 3])
263 |
264 | """
265 | items = []
266 | items.append('(')
267 | for i, v in enumerate(values):
268 | if i != 0:
269 | items.append(', ')
270 | items.append(sqlparam(v))
271 | items.append(')')
272 | return SQLQuery(items)
273 |
274 | def reparam(string_, dictionary):
275 | """
276 | Takes a string and a dictionary and interpolates the string
277 | using values from the dictionary. Returns an `SQLQuery` for the result.
278 |
279 | >>> reparam("s = $s", dict(s=True))
280 |
281 | >>> reparam("s IN $s", dict(s=[1, 2]))
282 |
283 | """
284 | dictionary = dictionary.copy() # eval mucks with it
285 | vals = []
286 | result = []
287 | for live, chunk in _interpolate(string_):
288 | if live:
289 | v = eval(chunk, dictionary)
290 | result.append(sqlquote(v))
291 | else:
292 | result.append(chunk)
293 | return SQLQuery.join(result, '')
294 |
295 | def sqlify(obj):
296 | """
297 | converts `obj` to its proper SQL version
298 |
299 | >>> sqlify(None)
300 | 'NULL'
301 | >>> sqlify(True)
302 | "'t'"
303 | >>> sqlify(3)
304 | '3'
305 | """
306 | # because `1 == True and hash(1) == hash(True)`
307 | # we have to do this the hard way...
308 |
309 | if obj is None:
310 | return 'NULL'
311 | elif obj is True:
312 | return "'t'"
313 | elif obj is False:
314 | return "'f'"
315 | elif isinstance(obj, long):
316 | return str(obj)
317 | elif datetime and isinstance(obj, datetime.datetime):
318 | return repr(obj.isoformat())
319 | else:
320 | if isinstance(obj, unicode): obj = obj.encode('utf8')
321 | return repr(obj)
322 |
323 | def sqllist(lst):
324 | """
325 | Converts the arguments for use in something like a WHERE clause.
326 |
327 | >>> sqllist(['a', 'b'])
328 | 'a, b'
329 | >>> sqllist('a')
330 | 'a'
331 | >>> sqllist(u'abc')
332 | u'abc'
333 | """
334 | if isinstance(lst, basestring):
335 | return lst
336 | else:
337 | return ', '.join(lst)
338 |
339 | def sqlors(left, lst):
340 | """
341 | `left is a SQL clause like `tablename.arg = `
342 | and `lst` is a list of values. Returns a reparam-style
343 | pair featuring the SQL that ORs together the clause
344 | for each item in the lst.
345 |
346 | >>> sqlors('foo = ', [])
347 |
348 | >>> sqlors('foo = ', [1])
349 |
350 | >>> sqlors('foo = ', 1)
351 |
352 | >>> sqlors('foo = ', [1,2,3])
353 |
354 | """
355 | if isinstance(lst, iters):
356 | lst = list(lst)
357 | ln = len(lst)
358 | if ln == 0:
359 | return SQLQuery("1=2")
360 | if ln == 1:
361 | lst = lst[0]
362 |
363 | if isinstance(lst, iters):
364 | return SQLQuery(['('] +
365 | sum([[left, sqlparam(x), ' OR '] for x in lst], []) +
366 | ['1=2)']
367 | )
368 | else:
369 | return left + sqlparam(lst)
370 |
371 | def sqlwhere(dictionary, grouping=' AND '):
372 | """
373 | Converts a `dictionary` to an SQL WHERE clause `SQLQuery`.
374 |
375 | >>> sqlwhere({'cust_id': 2, 'order_id':3})
376 |
377 | >>> sqlwhere({'cust_id': 2, 'order_id':3}, grouping=', ')
378 |
379 | >>> sqlwhere({'a': 'a', 'b': 'b'}).query()
380 | 'a = %s AND b = %s'
381 | """
382 | return SQLQuery.join([k + ' = ' + sqlparam(v) for k, v in dictionary.items()], grouping)
383 |
384 | def sqlquote(a):
385 | """
386 | Ensures `a` is quoted properly for use in a SQL query.
387 |
388 | >>> 'WHERE x = ' + sqlquote(True) + ' AND y = ' + sqlquote(3)
389 |
390 | >>> 'WHERE x = ' + sqlquote(True) + ' AND y IN ' + sqlquote([2, 3])
391 |
392 | """
393 | if isinstance(a, list):
394 | return _sqllist(a)
395 | else:
396 | return sqlparam(a).sqlquery()
397 |
398 | class Transaction:
399 | """Database transaction."""
400 | def __init__(self, ctx):
401 | self.ctx = ctx
402 | self.transaction_count = transaction_count = len(ctx.transactions)
403 |
404 | class transaction_engine:
405 | """Transaction Engine used in top level transactions."""
406 | def do_transact(self):
407 | ctx.commit(unload=False)
408 |
409 | def do_commit(self):
410 | ctx.commit()
411 |
412 | def do_rollback(self):
413 | ctx.rollback()
414 |
415 | class subtransaction_engine:
416 | """Transaction Engine used in sub transactions."""
417 | def query(self, q):
418 | db_cursor = ctx.db.cursor()
419 | ctx.db_execute(db_cursor, SQLQuery(q % transaction_count))
420 |
421 | def do_transact(self):
422 | self.query('SAVEPOINT webpy_sp_%s')
423 |
424 | def do_commit(self):
425 | self.query('RELEASE SAVEPOINT webpy_sp_%s')
426 |
427 | def do_rollback(self):
428 | self.query('ROLLBACK TO SAVEPOINT webpy_sp_%s')
429 |
430 | class dummy_engine:
431 | """Transaction Engine used instead of subtransaction_engine
432 | when sub transactions are not supported."""
433 | do_transact = do_commit = do_rollback = lambda self: None
434 |
435 | if self.transaction_count:
436 | # nested transactions are not supported in some databases
437 | if self.ctx.get('ignore_nested_transactions'):
438 | self.engine = dummy_engine()
439 | else:
440 | self.engine = subtransaction_engine()
441 | else:
442 | self.engine = transaction_engine()
443 |
444 | self.engine.do_transact()
445 | self.ctx.transactions.append(self)
446 |
447 | def __enter__(self):
448 | return self
449 |
450 | def __exit__(self, exctype, excvalue, traceback):
451 | if exctype is not None:
452 | self.rollback()
453 | else:
454 | self.commit()
455 |
456 | def commit(self):
457 | if len(self.ctx.transactions) > self.transaction_count:
458 | self.engine.do_commit()
459 | self.ctx.transactions = self.ctx.transactions[:self.transaction_count]
460 |
461 | def rollback(self):
462 | if len(self.ctx.transactions) > self.transaction_count:
463 | self.engine.do_rollback()
464 | self.ctx.transactions = self.ctx.transactions[:self.transaction_count]
465 |
466 | class DB:
467 | """Database"""
468 | def __init__(self, db_module, keywords):
469 | """Creates a database.
470 | """
471 | # some DB implementaions take optional paramater `driver` to use a specific driver modue
472 | # but it should not be passed to connect
473 | keywords.pop('driver', None)
474 |
475 | self.db_module = db_module
476 | self.keywords = keywords
477 |
478 | self._ctx = threadeddict()
479 | # flag to enable/disable printing queries
480 | self.printing = config.get('debug_sql', config.get('debug', False))
481 | self.supports_multiple_insert = False
482 |
483 | try:
484 | import DBUtils
485 | # enable pooling if DBUtils module is available.
486 | self.has_pooling = True
487 | except ImportError:
488 | self.has_pooling = False
489 |
490 | # Pooling can be disabled by passing pooling=False in the keywords.
491 | self.has_pooling = self.keywords.pop('pooling', True) and self.has_pooling
492 |
493 | def _getctx(self):
494 | if not self._ctx.get('db'):
495 | self._load_context(self._ctx)
496 | return self._ctx
497 | ctx = property(_getctx)
498 |
499 | def _load_context(self, ctx):
500 | ctx.dbq_count = 0
501 | ctx.transactions = [] # stack of transactions
502 |
503 | if self.has_pooling:
504 | ctx.db = self._connect_with_pooling(self.keywords)
505 | else:
506 | ctx.db = self._connect(self.keywords)
507 | ctx.db_execute = self._db_execute
508 |
509 | if not hasattr(ctx.db, 'commit'):
510 | ctx.db.commit = lambda: None
511 |
512 | if not hasattr(ctx.db, 'rollback'):
513 | ctx.db.rollback = lambda: None
514 |
515 | def commit(unload=True):
516 | # do db commit and release the connection if pooling is enabled.
517 | ctx.db.commit()
518 | if unload and self.has_pooling:
519 | self._unload_context(self._ctx)
520 |
521 | def rollback():
522 | # do db rollback and release the connection if pooling is enabled.
523 | ctx.db.rollback()
524 | if self.has_pooling:
525 | self._unload_context(self._ctx)
526 |
527 | ctx.commit = commit
528 | ctx.rollback = rollback
529 |
530 | def _unload_context(self, ctx):
531 | del ctx.db
532 |
533 | def _connect(self, keywords):
534 | return self.db_module.connect(**keywords)
535 |
536 | def _connect_with_pooling(self, keywords):
537 | def get_pooled_db():
538 | from DBUtils import PooledDB
539 |
540 | # In DBUtils 0.9.3, `dbapi` argument is renamed as `creator`
541 | # see Bug#122112
542 |
543 | if PooledDB.__version__.split('.') < '0.9.3'.split('.'):
544 | return PooledDB.PooledDB(dbapi=self.db_module, **keywords)
545 | else:
546 | return PooledDB.PooledDB(creator=self.db_module, **keywords)
547 |
548 | if getattr(self, '_pooleddb', None) is None:
549 | self._pooleddb = get_pooled_db()
550 |
551 | return self._pooleddb.connection()
552 |
553 | def _db_cursor(self):
554 | return self.ctx.db.cursor()
555 |
556 | def _param_marker(self):
557 | """Returns parameter marker based on paramstyle attribute if this database."""
558 | style = getattr(self, 'paramstyle', 'pyformat')
559 |
560 | if style == 'qmark':
561 | return '?'
562 | elif style == 'numeric':
563 | return ':1'
564 | elif style in ['format', 'pyformat']:
565 | return '%s'
566 | raise UnknownParamstyle, style
567 |
568 | def _db_execute(self, cur, sql_query):
569 | """executes an sql query"""
570 | self.ctx.dbq_count += 1
571 |
572 | try:
573 | a = time.time()
574 | query, params = self._process_query(sql_query)
575 | out = cur.execute(query, params)
576 | b = time.time()
577 | except:
578 | if self.printing:
579 | print >> debug, 'ERR:', str(sql_query)
580 | if self.ctx.transactions:
581 | self.ctx.transactions[-1].rollback()
582 | else:
583 | self.ctx.rollback()
584 | raise
585 |
586 | if self.printing:
587 | print >> debug, '%s (%s): %s' % (round(b-a, 2), self.ctx.dbq_count, str(sql_query))
588 | return out
589 |
590 | def _process_query(self, sql_query):
591 | """Takes the SQLQuery object and returns query string and parameters.
592 | """
593 | paramstyle = getattr(self, 'paramstyle', 'pyformat')
594 | query = sql_query.query(paramstyle)
595 | params = sql_query.values()
596 | return query, params
597 |
598 | def _where(self, where, vars):
599 | if isinstance(where, (int, long)):
600 | where = "id = " + sqlparam(where)
601 | #@@@ for backward-compatibility
602 | elif isinstance(where, (list, tuple)) and len(where) == 2:
603 | where = SQLQuery(where[0], where[1])
604 | elif isinstance(where, SQLQuery):
605 | pass
606 | else:
607 | where = reparam(where, vars)
608 | return where
609 |
610 | def query(self, sql_query, vars=None, processed=False, _test=False):
611 | """
612 | Execute SQL query `sql_query` using dictionary `vars` to interpolate it.
613 | If `processed=True`, `vars` is a `reparam`-style list to use
614 | instead of interpolating.
615 |
616 | >>> db = DB(None, {})
617 | >>> db.query("SELECT * FROM foo", _test=True)
618 |
619 | >>> db.query("SELECT * FROM foo WHERE x = $x", vars=dict(x='f'), _test=True)
620 |
621 | >>> db.query("SELECT * FROM foo WHERE x = " + sqlquote('f'), _test=True)
622 |
623 | """
624 | if vars is None: vars = {}
625 |
626 | if not processed and not isinstance(sql_query, SQLQuery):
627 | sql_query = reparam(sql_query, vars)
628 |
629 | if _test: return sql_query
630 |
631 | db_cursor = self._db_cursor()
632 | self._db_execute(db_cursor, sql_query)
633 |
634 | if db_cursor.description:
635 | names = [x[0] for x in db_cursor.description]
636 | def iterwrapper():
637 | row = db_cursor.fetchone()
638 | while row:
639 | yield storage(dict(zip(names, row)))
640 | row = db_cursor.fetchone()
641 | out = iterbetter(iterwrapper())
642 | out.__len__ = lambda: int(db_cursor.rowcount)
643 | out.list = lambda: [storage(dict(zip(names, x))) \
644 | for x in db_cursor.fetchall()]
645 | else:
646 | out = db_cursor.rowcount
647 |
648 | if not self.ctx.transactions:
649 | self.ctx.commit()
650 | return out
651 |
652 | def get(self, tables, by_id=None, vars=None, what='*', where=None, _test=False):
653 | if by_id:
654 | assert not (vars and where), "If by_id you can't give vars and where."
655 | results = self.select(tables, vars={'id': by_id}, what=what,
656 | where='id = $id', limit=1, _test=_test)
657 | else:
658 | results = self.select(tables, vars=vars, what=what, where=where, limit=1, _test=_test)
659 |
660 | if not results:
661 | return None
662 | else:
663 | return list(results)[0]
664 |
665 | def select(self, tables, vars=None, what='*', where=None, order=None, group=None,
666 | limit=None, offset=None, _test=False):
667 | """
668 | Selects `what` from `tables` with clauses `where`, `order`,
669 | `group`, `limit`, and `offset`. Uses vars to interpolate.
670 | Otherwise, each clause can be a SQLQuery.
671 |
672 | >>> db = DB(None, {})
673 | >>> db.select('foo', _test=True)
674 |
675 | >>> db.select(['foo', 'bar'], where="foo.bar_id = bar.id", limit=5, _test=True)
676 |
677 | """
678 | if vars is None: vars = {}
679 | sql_clauses = self.sql_clauses(what, tables, where, group, order, limit, offset)
680 | clauses = [self.gen_clause(sql, val, vars) for sql, val in sql_clauses if val is not None]
681 | qout = SQLQuery.join(clauses)
682 | if _test: return qout
683 | return self.query(qout, processed=True)
684 |
685 | def where(self, table, what='*', order=None, group=None, limit=None,
686 | offset=None, _test=False, **kwargs):
687 | """
688 | Selects from `table` where keys are equal to values in `kwargs`.
689 |
690 | >>> db = DB(None, {})
691 | >>> db.where('foo', bar_id=3, _test=True)
692 |
693 | >>> db.where('foo', source=2, crust='dewey', _test=True)
694 |
695 | >>> db.where('foo', _test=True)
696 |
697 | """
698 | where_clauses = []
699 | for k, v in kwargs.iteritems():
700 | where_clauses.append(k + ' = ' + sqlquote(v))
701 |
702 | if where_clauses:
703 | where = SQLQuery.join(where_clauses, " AND ")
704 | else:
705 | where = None
706 |
707 | return self.select(table, what=what, order=order,
708 | group=group, limit=limit, offset=offset, _test=_test,
709 | where=where)
710 |
711 | def sql_clauses(self, what, tables, where, group, order, limit, offset):
712 | return (
713 | ('SELECT', what),
714 | ('FROM', sqllist(tables)),
715 | ('WHERE', where),
716 | ('GROUP BY', group),
717 | ('ORDER BY', order),
718 | ('LIMIT', limit),
719 | ('OFFSET', offset))
720 |
721 | def gen_clause(self, sql, val, vars):
722 | if isinstance(val, (int, long)):
723 | if sql == 'WHERE':
724 | nout = 'id = ' + sqlquote(val)
725 | else:
726 | nout = SQLQuery(val)
727 | #@@@
728 | elif isinstance(val, (list, tuple)) and len(val) == 2:
729 | nout = SQLQuery(val[0], val[1]) # backwards-compatibility
730 | elif isinstance(val, SQLQuery):
731 | nout = val
732 | else:
733 | nout = reparam(val, vars)
734 |
735 | def xjoin(a, b):
736 | if a and b: return a + ' ' + b
737 | else: return a or b
738 |
739 | return xjoin(sql, nout)
740 |
741 | def insert(self, tablename, seqname=None, _test=False, **values):
742 | """
743 | Inserts `values` into `tablename`. Returns current sequence ID.
744 | Set `seqname` to the ID if it's not the default, or to `False`
745 | if there isn't one.
746 |
747 | >>> db = DB(None, {})
748 | >>> q = db.insert('foo', name='bob', age=2, created=SQLLiteral('NOW()'), _test=True)
749 | >>> q
750 |
751 | >>> q.query()
752 | 'INSERT INTO foo (age, name, created) VALUES (%s, %s, NOW())'
753 | >>> q.values()
754 | [2, 'bob']
755 | """
756 | def q(x): return "(" + x + ")"
757 |
758 | if values:
759 | _keys = SQLQuery.join(values.keys(), ', ')
760 | _values = SQLQuery.join([sqlparam(v) for v in values.values()], ', ')
761 | sql_query = "INSERT INTO %s " % tablename + q(_keys) + ' VALUES ' + q(_values)
762 | else:
763 | sql_query = SQLQuery(self._get_insert_default_values_query(tablename))
764 |
765 | if _test: return sql_query
766 |
767 | db_cursor = self._db_cursor()
768 | if seqname is not False:
769 | sql_query = self._process_insert_query(sql_query, tablename, seqname)
770 |
771 | if isinstance(sql_query, tuple):
772 | # for some databases, a separate query has to be made to find
773 | # the id of the inserted row.
774 | q1, q2 = sql_query
775 | self._db_execute(db_cursor, q1)
776 | self._db_execute(db_cursor, q2)
777 | else:
778 | self._db_execute(db_cursor, sql_query)
779 |
780 | try:
781 | out = db_cursor.fetchone()[0]
782 | except Exception:
783 | out = None
784 |
785 | if not self.ctx.transactions:
786 | self.ctx.commit()
787 | return out
788 |
789 | def _get_insert_default_values_query(self, table):
790 | return "INSERT INTO %s DEFAULT VALUES" % table
791 |
792 | def multiple_insert(self, tablename, values, seqname=None, _test=False):
793 | """
794 | Inserts multiple rows into `tablename`. The `values` must be a list of dictioanries,
795 | one for each row to be inserted, each with the same set of keys.
796 | Returns the list of ids of the inserted rows.
797 | Set `seqname` to the ID if it's not the default, or to `False`
798 | if there isn't one.
799 |
800 | >>> db = DB(None, {})
801 | >>> db.supports_multiple_insert = True
802 | >>> values = [{"name": "foo", "email": "foo@example.com"}, {"name": "bar", "email": "bar@example.com"}]
803 | >>> db.multiple_insert('person', values=values, _test=True)
804 |
805 | """
806 | if not values:
807 | return []
808 |
809 | if not self.supports_multiple_insert:
810 | out = [self.insert(tablename, seqname=seqname, _test=_test, **v) for v in values]
811 | if seqname is False:
812 | return None
813 | else:
814 | return out
815 |
816 | keys = values[0].keys()
817 | #@@ make sure all keys are valid
818 |
819 | for v in values:
820 | if v.keys() != keys:
821 | raise ValueError, 'Not all rows have the same keys'
822 |
823 | sql_query = SQLQuery('INSERT INTO %s (%s) VALUES ' % (tablename, ', '.join(keys)))
824 |
825 | for i, row in enumerate(values):
826 | if i != 0:
827 | sql_query.append(", ")
828 | SQLQuery.join([SQLParam(row[k]) for k in keys], sep=", ", target=sql_query, prefix="(", suffix=")")
829 |
830 | if _test: return sql_query
831 |
832 | db_cursor = self._db_cursor()
833 | if seqname is not False:
834 | sql_query = self._process_insert_query(sql_query, tablename, seqname)
835 |
836 | if isinstance(sql_query, tuple):
837 | # for some databases, a separate query has to be made to find
838 | # the id of the inserted row.
839 | q1, q2 = sql_query
840 | self._db_execute(db_cursor, q1)
841 | self._db_execute(db_cursor, q2)
842 | else:
843 | self._db_execute(db_cursor, sql_query)
844 |
845 | try:
846 | out = db_cursor.fetchone()[0]
847 | out = range(out-len(values)+1, out+1)
848 | except Exception:
849 | out = None
850 |
851 | if not self.ctx.transactions:
852 | self.ctx.commit()
853 | return out
854 |
855 |
856 | def update(self, tables, where, vars=None, _test=False, **values):
857 | """
858 | Update `tables` with clause `where` (interpolated using `vars`)
859 | and setting `values`.
860 |
861 | >>> db = DB(None, {})
862 | >>> name = 'Joseph'
863 | >>> q = db.update('foo', where='name = $name', name='bob', age=2,
864 | ... created=SQLLiteral('NOW()'), vars=locals(), _test=True)
865 | >>> q
866 |
867 | >>> q.query()
868 | 'UPDATE foo SET age = %s, name = %s, created = NOW() WHERE name = %s'
869 | >>> q.values()
870 | [2, 'bob', 'Joseph']
871 | """
872 | if vars is None: vars = {}
873 | where = self._where(where, vars)
874 |
875 | query = (
876 | "UPDATE " + sqllist(tables) +
877 | " SET " + sqlwhere(values, ', ') +
878 | " WHERE " + where)
879 |
880 | if _test: return query
881 |
882 | db_cursor = self._db_cursor()
883 | self._db_execute(db_cursor, query)
884 | if not self.ctx.transactions:
885 | self.ctx.commit()
886 | return db_cursor.rowcount
887 |
888 | def delete(self, table, where, using=None, vars=None, _test=False):
889 | """
890 | Deletes from `table` with clauses `where` and `using`.
891 |
892 | >>> db = DB(None, {})
893 | >>> name = 'Joe'
894 | >>> db.delete('foo', where='name = $name', vars=locals(), _test=True)
895 |
896 | """
897 | if vars is None: vars = {}
898 | where = self._where(where, vars)
899 |
900 | q = 'DELETE FROM ' + table
901 | if using: q += ' USING ' + sqllist(using)
902 | if where: q += ' WHERE ' + where
903 |
904 | if _test: return q
905 |
906 | db_cursor = self._db_cursor()
907 | self._db_execute(db_cursor, q)
908 | if not self.ctx.transactions:
909 | self.ctx.commit()
910 | return db_cursor.rowcount
911 |
912 | def _process_insert_query(self, query, tablename, seqname):
913 | return query
914 |
915 | def transaction(self):
916 | """Start a transaction."""
917 | return Transaction(self.ctx)
918 |
919 | class PostgresDB(DB):
920 | """Postgres driver."""
921 | def __init__(self, **keywords):
922 | if 'pw' in keywords:
923 | keywords['password'] = keywords.pop('pw')
924 |
925 | db_module = import_driver(["psycopg2", "psycopg", "pgdb"], preferred=keywords.pop('driver', None))
926 | if db_module.__name__ == "psycopg2":
927 | import psycopg2.extensions
928 | psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
929 | if db_module.__name__ == "pgdb" and 'port' in keywords:
930 | keywords["host"] += ":" + str(keywords.pop('port'))
931 |
932 | # if db is not provided postgres driver will take it from PGDATABASE environment variable
933 | if 'db' in keywords:
934 | keywords['database'] = keywords.pop('db')
935 |
936 | self.dbname = "postgres"
937 | self.paramstyle = db_module.paramstyle
938 | DB.__init__(self, db_module, keywords)
939 | self.supports_multiple_insert = True
940 | self._sequences = None
941 |
942 | def _process_insert_query(self, query, tablename, seqname):
943 | if seqname is None:
944 | # when seqname is not provided guess the seqname and make sure it exists
945 | seqname = tablename + "_id_seq"
946 | if seqname not in self._get_all_sequences():
947 | seqname = None
948 |
949 | if seqname:
950 | query += "; SELECT currval('%s')" % seqname
951 |
952 | return query
953 |
954 | def _get_all_sequences(self):
955 | """Query postgres to find names of all sequences used in this database."""
956 | if self._sequences is None:
957 | q = "SELECT c.relname FROM pg_class c WHERE c.relkind = 'S'"
958 | self._sequences = set([c.relname for c in self.query(q)])
959 | return self._sequences
960 |
961 | def _connect(self, keywords):
962 | conn = DB._connect(self, keywords)
963 | try:
964 | conn.set_client_encoding('UTF8')
965 | except AttributeError:
966 | # fallback for pgdb driver
967 | conn.cursor().execute("set client_encoding to 'UTF-8'")
968 | return conn
969 |
970 | def _connect_with_pooling(self, keywords):
971 | conn = DB._connect_with_pooling(self, keywords)
972 | conn._con._con.set_client_encoding('UTF8')
973 | return conn
974 |
975 | class MySQLDB(DB):
976 | def __init__(self, **keywords):
977 | import MySQLdb as db
978 | if 'pw' in keywords:
979 | keywords['passwd'] = keywords['pw']
980 | del keywords['pw']
981 |
982 | if 'charset' not in keywords:
983 | keywords['charset'] = 'utf8'
984 | elif keywords['charset'] is None:
985 | del keywords['charset']
986 |
987 | self.paramstyle = db.paramstyle = 'pyformat' # it's both, like psycopg
988 | self.dbname = "mysql"
989 | DB.__init__(self, db, keywords)
990 | self.supports_multiple_insert = True
991 |
992 | def _process_insert_query(self, query, tablename, seqname):
993 | return query, SQLQuery('SELECT last_insert_id();')
994 |
995 | def _get_insert_default_values_query(self, table):
996 | return "INSERT INTO %s () VALUES()" % table
997 |
998 | def import_driver(drivers, preferred=None):
999 | """Import the first available driver or preferred driver.
1000 | """
1001 | if preferred:
1002 | drivers = [preferred]
1003 |
1004 | for d in drivers:
1005 | try:
1006 | return __import__(d, None, None, ['x'])
1007 | except ImportError:
1008 | pass
1009 | raise ImportError("Unable to import " + " or ".join(drivers))
1010 |
1011 | class SqliteDB(DB):
1012 | def __init__(self, **keywords):
1013 | db = import_driver(["sqlite3", "pysqlite2.dbapi2", "sqlite"], preferred=keywords.pop('driver', None))
1014 |
1015 | if db.__name__ in ["sqlite3", "pysqlite2.dbapi2"]:
1016 | db.paramstyle = 'qmark'
1017 |
1018 | # sqlite driver doesn't create datatime objects for timestamp columns unless `detect_types` option is passed.
1019 | # It seems to be supported in sqlite3 and pysqlite2 drivers, not surte about sqlite.
1020 | keywords.setdefault('detect_types', db.PARSE_DECLTYPES)
1021 |
1022 | self.paramstyle = db.paramstyle
1023 | keywords['database'] = keywords.pop('db')
1024 | keywords['pooling'] = False # sqlite don't allows connections to be shared by threads
1025 | self.dbname = "sqlite"
1026 | DB.__init__(self, db, keywords)
1027 |
1028 | def _process_insert_query(self, query, tablename, seqname):
1029 | return query, SQLQuery('SELECT last_insert_rowid();')
1030 |
1031 | def query(self, *a, **kw):
1032 | out = DB.query(self, *a, **kw)
1033 | if isinstance(out, iterbetter):
1034 | del out.__len__
1035 | return out
1036 |
1037 | class FirebirdDB(DB):
1038 | """Firebird Database.
1039 | """
1040 | def __init__(self, **keywords):
1041 | try:
1042 | import kinterbasdb as db
1043 | except Exception:
1044 | db = None
1045 | pass
1046 | if 'pw' in keywords:
1047 | keywords['password'] = keywords.pop('pw')
1048 | keywords['database'] = keywords.pop('db')
1049 |
1050 | self.paramstyle = db.paramstyle
1051 |
1052 | DB.__init__(self, db, keywords)
1053 |
1054 | def delete(self, table, where=None, using=None, vars=None, _test=False):
1055 | # firebird doesn't support using clause
1056 | using=None
1057 | return DB.delete(self, table, where, using, vars, _test)
1058 |
1059 | def sql_clauses(self, what, tables, where, group, order, limit, offset):
1060 | return (
1061 | ('SELECT', ''),
1062 | ('FIRST', limit),
1063 | ('SKIP', offset),
1064 | ('', what),
1065 | ('FROM', sqllist(tables)),
1066 | ('WHERE', where),
1067 | ('GROUP BY', group),
1068 | ('ORDER BY', order)
1069 | )
1070 |
1071 | class MSSQLDB(DB):
1072 | def __init__(self, **keywords):
1073 | import pymssql as db
1074 | if 'pw' in keywords:
1075 | keywords['password'] = keywords.pop('pw')
1076 | keywords['database'] = keywords.pop('db')
1077 | self.dbname = "mssql"
1078 | DB.__init__(self, db, keywords)
1079 |
1080 | def _process_query(self, sql_query):
1081 | """Takes the SQLQuery object and returns query string and parameters.
1082 | """
1083 | # MSSQLDB expects params to be a tuple.
1084 | # Overwriting the default implementation to convert params to tuple.
1085 | paramstyle = getattr(self, 'paramstyle', 'pyformat')
1086 | query = sql_query.query(paramstyle)
1087 | params = sql_query.values()
1088 | return query, tuple(params)
1089 |
1090 | def sql_clauses(self, what, tables, where, group, order, limit, offset):
1091 | return (
1092 | ('SELECT', what),
1093 | ('TOP', limit),
1094 | ('FROM', sqllist(tables)),
1095 | ('WHERE', where),
1096 | ('GROUP BY', group),
1097 | ('ORDER BY', order),
1098 | ('OFFSET', offset))
1099 |
1100 | def _test(self):
1101 | """Test LIMIT.
1102 |
1103 | Fake presence of pymssql module for running tests.
1104 | >>> import sys
1105 | >>> sys.modules['pymssql'] = sys.modules['sys']
1106 |
1107 | MSSQL has TOP clause instead of LIMIT clause.
1108 | >>> db = MSSQLDB(db='test', user='joe', pw='secret')
1109 | >>> db.select('foo', limit=4, _test=True)
1110 |
1111 | """
1112 | pass
1113 |
1114 | class OracleDB(DB):
1115 | def __init__(self, **keywords):
1116 | import cx_Oracle as db
1117 | if 'pw' in keywords:
1118 | keywords['password'] = keywords.pop('pw')
1119 |
1120 | #@@ TODO: use db.makedsn if host, port is specified
1121 | keywords['dsn'] = keywords.pop('db')
1122 | self.dbname = 'oracle'
1123 | db.paramstyle = 'numeric'
1124 | self.paramstyle = db.paramstyle
1125 |
1126 | # oracle doesn't support pooling
1127 | keywords.pop('pooling', None)
1128 | DB.__init__(self, db, keywords)
1129 |
1130 | def _process_insert_query(self, query, tablename, seqname):
1131 | if seqname is None:
1132 | # It is not possible to get seq name from table name in Oracle
1133 | return query
1134 | else:
1135 | return query + "; SELECT %s.currval FROM dual" % seqname
1136 |
1137 | def dburl2dict(url):
1138 | """
1139 | Takes a URL to a database and parses it into an equivalent dictionary.
1140 |
1141 | >>> dburl2dict('postgres://james:day@serverfarm.example.net:5432/mygreatdb')
1142 | {'pw': 'day', 'dbn': 'postgres', 'db': 'mygreatdb', 'host': 'serverfarm.example.net', 'user': 'james', 'port': '5432'}
1143 | >>> dburl2dict('postgres://james:day@serverfarm.example.net/mygreatdb')
1144 | {'user': 'james', 'host': 'serverfarm.example.net', 'db': 'mygreatdb', 'pw': 'day', 'dbn': 'postgres'}
1145 | >>> dburl2dict('postgres://james:d%40y@serverfarm.example.net/mygreatdb')
1146 | {'user': 'james', 'host': 'serverfarm.example.net', 'db': 'mygreatdb', 'pw': 'd@y', 'dbn': 'postgres'}
1147 | """
1148 | dbn, rest = url.split('://', 1)
1149 | user, rest = rest.split(':', 1)
1150 | pw, rest = rest.split('@', 1)
1151 | if ':' in rest:
1152 | host, rest = rest.split(':', 1)
1153 | port, rest = rest.split('/', 1)
1154 | else:
1155 | host, rest = rest.split('/', 1)
1156 | port = None
1157 | db = rest
1158 |
1159 | uq = urllib.unquote
1160 | out = dict(dbn=dbn, user=uq(user), pw=uq(pw), host=uq(host), db=uq(db))
1161 | if port: out['port'] = port
1162 | return out
1163 |
1164 | _databases = {}
1165 | def database(dburl=None, **params):
1166 | """Creates appropriate database using params.
1167 |
1168 | Pooling will be enabled if DBUtils module is available.
1169 | Pooling can be disabled by passing pooling=False in params.
1170 | """
1171 | if not dburl and not params:
1172 | dburl = os.environ['DATABASE_URL']
1173 | if dburl:
1174 | params = dburl2dict(dburl)
1175 | dbn = params.pop('dbn')
1176 | if dbn in _databases:
1177 | return _databases[dbn](**params)
1178 | else:
1179 | raise UnknownDB, dbn
1180 |
1181 | def register_database(name, clazz):
1182 | """
1183 | Register a database.
1184 |
1185 | >>> class LegacyDB(DB):
1186 | ... def __init__(self, **params):
1187 | ... pass
1188 | ...
1189 | >>> register_database('legacy', LegacyDB)
1190 | >>> db = database(dbn='legacy', db='test', user='joe', passwd='secret')
1191 | """
1192 | _databases[name] = clazz
1193 |
1194 | register_database('mysql', MySQLDB)
1195 | register_database('postgres', PostgresDB)
1196 | register_database('sqlite', SqliteDB)
1197 | register_database('firebird', FirebirdDB)
1198 | register_database('mssql', MSSQLDB)
1199 | register_database('oracle', OracleDB)
1200 |
1201 | def _interpolate(format):
1202 | """
1203 | Takes a format string and returns a list of 2-tuples of the form
1204 | (boolean, string) where boolean says whether string should be evaled
1205 | or not.
1206 |
1207 | from (public domain, Ka-Ping Yee)
1208 | """
1209 | from tokenize import tokenprog
1210 |
1211 | def matchorfail(text, pos):
1212 | match = tokenprog.match(text, pos)
1213 | if match is None:
1214 | raise _ItplError(text, pos)
1215 | return match, match.end()
1216 |
1217 | namechars = "abcdefghijklmnopqrstuvwxyz" \
1218 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
1219 | chunks = []
1220 | pos = 0
1221 |
1222 | while 1:
1223 | dollar = format.find("$", pos)
1224 | if dollar < 0:
1225 | break
1226 | nextchar = format[dollar + 1]
1227 |
1228 | if nextchar == "{":
1229 | chunks.append((0, format[pos:dollar]))
1230 | pos, level = dollar + 2, 1
1231 | while level:
1232 | match, pos = matchorfail(format, pos)
1233 | tstart, tend = match.regs[3]
1234 | token = format[tstart:tend]
1235 | if token == "{":
1236 | level = level + 1
1237 | elif token == "}":
1238 | level = level - 1
1239 | chunks.append((1, format[dollar + 2:pos - 1]))
1240 |
1241 | elif nextchar in namechars:
1242 | chunks.append((0, format[pos:dollar]))
1243 | match, pos = matchorfail(format, dollar + 1)
1244 | while pos < len(format):
1245 | if format[pos] == "." and \
1246 | pos + 1 < len(format) and format[pos + 1] in namechars:
1247 | match, pos = matchorfail(format, pos + 1)
1248 | elif format[pos] in "([":
1249 | pos, level = pos + 1, 1
1250 | while level:
1251 | match, pos = matchorfail(format, pos)
1252 | tstart, tend = match.regs[3]
1253 | token = format[tstart:tend]
1254 | if token[0] in "([":
1255 | level = level + 1
1256 | elif token[0] in ")]":
1257 | level = level - 1
1258 | else:
1259 | break
1260 | chunks.append((1, format[dollar + 1:pos]))
1261 | else:
1262 | chunks.append((0, format[pos:dollar + 1]))
1263 | pos = dollar + 1 + (nextchar == "$")
1264 |
1265 | if pos < len(format):
1266 | chunks.append((0, format[pos:]))
1267 | return chunks
1268 |
1269 | if __name__ == "__main__":
1270 | import doctest
1271 | doctest.testmod()
1272 |
--------------------------------------------------------------------------------