types.py 2.1 KB
Newer Older
Jerome Mariette's avatar
Jerome Mariette committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#
# Copyright (C) 2012 INRA
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

Jerome Mariette's avatar
Jerome Mariette committed
18
19
import os
import argparse
Jerome Mariette's avatar
Jerome Mariette committed
20
import datetime
Jerome Mariette's avatar
Jerome Mariette committed
21
22
23
import urllib2 
import tempfile
import logging
Jerome Mariette's avatar
Jerome Mariette committed
24

Jerome Mariette's avatar
Jerome Mariette committed
25
26
from urlparse import urlparse
from jflow.config_reader import JFlowConfigReader
Jerome Mariette's avatar
Jerome Mariette committed
27

Jerome Mariette's avatar
Jerome Mariette committed
28
29
# custom date type
def date(datestr):
Jerome Mariette's avatar
Jerome Mariette committed
30
31
32
33
34
35
    return datetime.datetime.strptime(datestr, '%d/%m/%Y')

def localfile(file):
    if os.path.isfile(file):
        return os.path.abspath(file)
    else:
Jerome Mariette's avatar
Jerome Mariette committed
36
37
        raise argparse.ArgumentTypeError("Local file '" + file + "' does not exists! Please provide a valid file path!")
    
Jerome Mariette's avatar
Jerome Mariette committed
38
39
40
41
42
43
44
45
46
def inputfile(ifile):
    # test the format
    uri_object = urlparse(ifile)
    # check the file
    if uri_object.scheme == '':
        if os.path.isfile(ifile):
            return os.path.abspath(ifile)
        else:
            raise argparse.ArgumentTypeError("Local file '" + ifile + "' does not exists! Please provide a valid file path!")
Jerome Mariette's avatar
Jerome Mariette committed
47
    else:
Jerome Mariette's avatar
Jerome Mariette committed
48
49
50
51
52
53
54
55
56
57
58
59
60
        try:
            opener = urllib2.urlopen(ifile)
        except:
            raise argparse.ArgumentTypeError("URL '" + ifile + "' is invalid!")
        file_name = os.path.basename(uri_object.path)
        if file_name is not None and file_name != "":
            metadata = opener.info()
            file_size = int(metadata.getheaders("Content-Length")[0])
            if file_size == 0:
                raise argparse.ArgumentTypeError("The URL file '" + ifile + "' is empty!")
            return ifile
        else:
            raise argparse.ArgumentTypeError("URL '" + ifile + "' does not contain any file name!")
Jerome Mariette's avatar
Jerome Mariette committed
61