Document ftp: have timeout at tests, skip if server is not available.
[odoo/odoo.git] / addons / document_ftp / test_easyftp.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #    
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.     
19 #
20 ##############################################################################
21
22 """ This is a testing module, which exports some functions for the YAML tests.
23     Instead of repeating the same FTP code all over, we prefer to have
24     it in this file
25 """
26
27 from ftplib import FTP
28 from tools.misc import detect_ip_addr
29 from tools import config
30
31 def get_plain_ftp(timeout=10.0):
32     ftp = FTP()
33     host = config.get('ftp_server_host', '127.0.0.1')
34     port = config.get('ftp_server_port','8021')
35     ftp.connect(host,port, timeout=timeout)
36     return ftp
37
38 def get_ftp_login(cr, uid, ormobj):
39     ftp = get_plain_ftp()
40     user = ormobj.pool.get('res.users').read(cr, uid, uid)
41     ftp.login(user.get('login',''),user.get('password',''))
42     ftp.cwd("/" + cr.dbname)
43     return ftp
44
45 def get_ftp_anonymous(cr):
46     ftp = get_plain_ftp()
47     ftp.login('anonymous', 'the-test')
48     ftp.cwd("/")
49     return ftp
50
51 def get_ftp_folder(cr, uid, ormobj, foldername):
52     ftp = get_ftp_login(cr, uid, ormobj)
53     ftp.cwd("/" + cr.dbname+"/"+foldername)
54     return ftp
55
56 def get_ftp_fulldata(ftp, fname, limit=8192):
57     from functools import partial
58     data = []
59     def ffp(data, ndata):
60         if len(data)+ len(ndata) > limit:
61             raise IndexError('Data over the limit')
62         data.append(ndata)
63     ftp.retrbinary('RETR %s' % fname, partial(ffp,data))
64     return ''.join(data)
65
66 #eof