示例#1
0
    def open_file(self, filename, force_date = None):
        self._file_list = list()
        ds = None
        if os.linesep in filename:
            # already list of files (date range)
            return filename.split(os.linesep)

        mtype = mimetypes.guess_type(filename)[0]
        if mtype is None or 'xml' not in mtype:
            # assuming text file containing list of VFR files
            try:
                f = open(filename)
                i = 0
                lines = f.read().splitlines()
                for line in lines:
                    if len(line) < 1 or line.startswith('#'):
                        continue # skip empty or commented lines 

                    if not line.startswith('http://') and \
                            not line.startswith('20'):
                        # determine date if missing
                        if not force_date:
                            if line.startswith('ST_Z'):
                                date = yesterday()
                            else:
                                date = last_day_of_month()
                        else:
                            date = force_date
                        line = date + '_' + line

                    if not line.endswith('.xml.gz'):
                        # add extension if missing
                        line += '.xml.gz'

                    if not os.path.exists(line):
                        if not line.startswith('http://'):
                            line = 'http://vdp.cuzk.cz/vymenny_format/soucasna/' + line
                        line = download_vfr(line)

                    self._file_list.append(line)
                    i += 1
                VfrLogger.msg("%d VFR files will be processed..." % len(self._file_list))
            except IOError:
                raise VfrError("Unable to read '%s'" % filename)
            f.close()    
        else:
            # single VFR file
            self._file_list.append(filename)

        return self._file_list
示例#2
0
def parse_cmd(argv, flags, params, optdir):
    get_opt(argv, flags, params, optdir)

    if optdir['list']:
        if not optdir['dbname']:
            raise getopt.GetoptError("--dbname required")
        return 0
    
    filename = optdir.get('filename', None)
    date = optdir.get('date', None)
    ftype = optdir.get('ftype', None)
    
    # check required options
    if not filename and not ftype:
        raise getopt.GetoptError("--file or --type required")
    if filename and ftype:
        raise getopt.GetoptError("--file and --type are mutually exclusive")

    if ftype and not date:
        if ftype.startswith('ST_Z'):
            date_list = [yesterday()]
        else:
            date_list = [last_day_of_month()]
    elif ftype and date and ':' in date:
            if ftype.startswith('ST_Z'):
                date_list = get_date_interval(date)
            else:
                raise getopt.GetoptError("Date interval is valid only for '--type ST_ZXXX'")
    else:
        date_list = [date]
    
    if optdir['overwrite'] and optdir.get('append', False):
        raise getopt.GetoptError("--append and --overwrite are mutually exclusive")
    
    if optdir['layer']:
        optdir['layer'] = optdir['layer'].split(',')
    
    if filename:
        # is file a valid VFR file
        filename = check_file(filename)
    else: # --date & --type
        flist = []
        base_url = "http://vdp.cuzk.cz/vymenny_format/"
        if ftype != 'ST_UVOH':
            base_url += "soucasna/"
        else:
            base_url += "specialni/"
        for d in date_list:
            fname = "%s_%s.xml.gz" % (d, ftype)
            url = base_url + fname
            if os.path.exists(fname):
                # use existing file
                flist.append(fname)
            else:
                # download file
                flist.append(download_vfr(url))
            
        if not flist:
            raise getopt.GetoptError("Empty date range")
        
        filename = os.linesep.join(flist)
        
    if not filename:
        raise getopt.GetoptError("Invalid input file")

    return filename