[fix] some problems in o2m
[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 import config
29
30 def get_plain_ftp(timeout=10.0):
31     ftp = FTP()
32     host = config.get('ftp_server_host', '127.0.0.1')
33     port = config.get('ftp_server_port', '8021')
34     ftp.connect(host, port,timeout)
35     return ftp
36
37 def get_ftp_login(cr, uid, ormobj):
38     ftp = get_plain_ftp()
39     user = ormobj.pool.get('res.users').browse(cr, uid, uid)
40     passwd = user.password or ''
41     if passwd.startswith("$1$"):
42         # md5 by base crypt. We cannot decode, wild guess
43         # that passwd = login
44         passwd = user.login
45     ftp.login(user.login, passwd)
46     ftp.cwd("/" + cr.dbname)
47     return ftp
48
49 def get_ftp_anonymous(cr):
50     ftp = get_plain_ftp()
51     ftp.login('anonymous', 'the-test')
52     ftp.cwd("/")
53     return ftp
54
55 def get_ftp_folder(cr, uid, ormobj, foldername):
56     ftp = get_ftp_login(cr, uid, ormobj)
57     ftp.cwd("/" + cr.dbname+"/"+foldername)
58     return ftp
59
60 def get_ftp_fulldata(ftp, fname, limit=8192):
61     from functools import partial
62     data = []
63     def ffp(data, ndata):
64         if len(data)+ len(ndata) > limit:
65             raise IndexError('Data over the limit')
66         data.append(ndata)
67     ftp.retrbinary('RETR %s' % fname, partial(ffp,data))
68     return ''.join(data)
69
70 #eof
71
72 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: