source: mauRepo/HPFos/trunk/hpfos/HPFos/osImpl/commons.py @ 8717

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/HPFos/trunk/hpfos/HPFos/osImpl/commons.py@8717
Revision 8717, 6.0 KB checked in by mnagni, 6 years ago (diff)

Incomplete - # 22576: Filtering on Files in a particular Result returns all Files rather than a subset.
 http://team.ceda.ac.uk/trac/ceda/ticket/22576

Should fix all the remaining bugs

Line 
1'''
2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29Created on 16 Aug 2012
30
31@author: mnagni
32'''
33
34from xml.etree.ElementTree import XML
35import urllib2
36from ea_model.iso_19108_2006_temporal_schema.temporal_objects.tm_instant import TM_Instant
37from ea_model.iso_19108_2006_temporal_schema.temporal_objects.tm_period import TM_Period
38import re
39from datetime import tzinfo, timedelta, datetime
40from urllib2 import HTTPError
41from hpfos.HPFos.exception import FatcatException
42
43import logging
44
45# Get an instance of a logger
46LOGGER = logging.getLogger(__name__)
47
48class OS_tz(tzinfo):
49    def __init__(self, minutes):
50        self.__offset = timedelta(minutes = minutes)       
51   
52    def utcoffset(self, dt):
53        return self.__offset
54    def dst(self, dt):
55        return timedelta(0)
56    def tzname(self,dt):
57        return "Not Defined"
58
59DATE_FORMAT = '%Y-%m-%d'
60DATETIME_FORMAT = DATE_FORMAT + 'T%H:%M:%S'
61
62def string_to_datetime(datestr):
63    datetimeformat_withtz = r'\d{4}\-([0]{1}\d{1}|[1]{1}[0-2]{1})\-([0-2]{1}\d{1}|[3]{1}[0-1]{1})T([0-1]{1}\d{1}|[2]{1}[0-3]{1})\:([0-5]{1}\d{1})\:([0-5]{1}\d{1})[\-\+]([0-1]{1}\d{1}|[2]{1}[0-3]{1})\:([0-5]{1}\d{1})'
64    datetimeformat_withnotz  = r'\d{4}\-([0]{1}\d{1}|[1]{1}[0-2]{1})\-([0-2]{1}\d{1}|[3]{1}[0-1]{1})T([0-1]{1}\d{1}|[2]{1}[0-3]{1})\:([0-5]{1}\d{1})\:([0-5]{1}\d{1})'
65    datetimeformat_withUTC  = r'\d{4}\-([0]{1}\d{1}|[1]{1}[0-2]{1})\-([0-2]{1}\d{1}|[3]{1}[0-1]{1})T([0-1]{1}\d{1}|[2]{1}[0-3]{1})\:([0-5]{1}\d{1})\:([0-5]{1}\d{1})Z'
66    dateformat  = r'\d{4}\-([0]{1}\d{1}|[1]{1}[0-2]{1})\-([0-2]{1}\d{1}|[3]{1}[0-1]{1})'   
67   
68    datetime_withtz = re.match(datetimeformat_withtz, datestr)
69    datetime_withnotz = re.match(datetimeformat_withnotz, datestr)
70    datetime_withUTC = re.match(datetimeformat_withUTC, datestr)
71
72    tzmins = None
73    if datetime_withtz is not None:
74        mytz = datestr.replace(datetime_withnotz.group(0),'')
75        hours, mins = mytz[1:].split(':')
76        tzmins = int(hours)*60 + int(mins)
77        if mytz.startswith('-'):
78            tzmins = -1*tzmins
79
80    ret_datetime = None
81    if datetime_withUTC is not None:
82        ret_datetime = \
83            datetime.strptime(datetime_withUTC.group(0)[:-1], DATETIME_FORMAT)           
84    elif datetime_withnotz is not None:
85        ret_datetime = \
86            datetime.strptime(datetime_withnotz.group(0), DATETIME_FORMAT)
87           
88    if ret_datetime is not None:
89        if tzmins is not None:             
90            ret_datetime = ret_datetime.replace(tzinfo = OS_tz(tzmins))
91        return ret_datetime
92       
93    datetime_simple = re.match(dateformat, datestr)       
94    if datetime_simple is not None:
95        return datetime.strptime(datetime_simple.group(0), DATE_FORMAT)
96
97def tm_InstantToDatetime(tm_instant):
98    idate = tm_instant.position.date8601
99    return datetime.date(int(idate.year), int(idate.month), int(idate.day))
100
101def from_pt_to_string(phenomenonTime):
102    """
103        Transforms a TM_Instant instance in a date string like '2002-07-22' (startDate) or   
104        a TM_Period instance in a string like '2002-07-22/2011-08-06' (start/endDate)
105        @param phenomenonTime: a aTM_Instace or a TM_Period instance
106        @return a pair startDate, endDate. If endDate does not exists return startDate,
107        None
108    """   
109    if phenomenonTime is None:
110        return None
111    startDate =None
112    endDate = None
113    if isinstance(phenomenonTime, TM_Instant):
114        startDate = _tm_InstantToString(phenomenonTime)
115    elif isinstance(phenomenonTime, TM_Period):
116        startDate = _tm_InstantToString(phenomenonTime.begin)       
117        endDate = _tm_InstantToString(phenomenonTime.end)
118    return startDate, endDate
119
120def _tm_InstantToString(tm_instant):
121    idate = tm_instant.position.date8601
122    return datetime(int(idate.year), 
123                    int(idate.month), 
124                    int(idate.day), 0, 0, 0).isoformat()
125
126def return_not_none_text(element):
127    if element is None:
128        return None
129    return element.text
130
131def get_document(host, path, port = 80, proxy = None):
132    if proxy is not None:
133        proxy = urllib2.ProxyHandler({'http': proxy})
134        opener = urllib2.build_opener(proxy)
135        urllib2.install_opener(opener)
136    connect_to = 'http://%s:%s%s' % (host, port, path)
137    try:
138        response = urllib2.urlopen(connect_to)
139        return  response.read()
140    except HTTPError as e:
141        msg = "Fatcat returned HTTP 500 for the following query: %s" % path
142        raise FatcatException(msg)
143        LOGGER.error(msg)
144
145def get_xml_document(source):
146    return XML(source)
Note: See TracBrowser for help on using the repository browser.