-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
134 lines (108 loc) · 4.73 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
#
#------------------------------------------------------------------------------
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
#
#
# some utilities used by create_cloudless.py, dataset_reader.py,
# dataset_porocessor.py, and wcs_client.py
#
#
# Project: DeltaDREAM
# Name: util.py
# Authors: Christian Schiller <christian dot schiller at eox dot at>
#
#-------------------------------------------------------------------------------
# Copyright (C) 2014 EOX IT Services GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies of this Software or works derived from this Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#-------------------------------------------------------------------------------
#
#
import os
import sys
from xml.dom import minidom
#/************************************************************************/
#/* handle_error() */
#/************************************************************************/
def handle_error(err_msg, err_code, settings):
"""
prints out the error_msg and err_code and exit
"""
#print err_msg, err_code
err_msg = (err_code,) + err_msg
print_log(settings, err_msg)
# usage()
sys.exit(err_code)
#/************************************************************************/
#/* print_log() */
#/************************************************************************/
def print_log(settings, msg):
"""
writes log-output
"""
if msg.__class__ is str or msg.__class__ is unicode:
print >> settings['logging.log_fsock'] , msg
else:
for elem in msg:
print >> settings['logging.log_fsock'] , "%s" % elem,
print >> settings['logging.log_fsock'] ,''
settings['logging.log_fsock'].flush()
#/************************************************************************/
#/* set_logging() */
#/************************************************************************/
def set_logging(settings):
"""
set logging output according to configuration -> either to the
screen or to a files
"""
#global settings
if settings['logging.log_type'] == 'screen':
log_fsock = sys.stdout
if settings['logging.log_type'] == 'file':
if settings['logging.log_file'] is None or settings['logging.log_file'] is '':
err_msg = 'Error - there is no log-file location provided in the the config-file'
handle_error(err_msg, 10)
log_fsock = open(settings['logging.log_file'], 'a')
# also write the stderr to the logfile (in anycase)
# comment out next line if errors should go to screen and not to log-file
sys.stderr = log_fsock
#return log_fsock
settings['logging.log_fsock'] = log_fsock
#/************************************************************************/
#/* parse_xml() */
#/************************************************************************/
def parse_xml(in_xml, tag):
"""
Function to parse the request results (GetCapabilities & DescribeEOCoverageSet) for the available
DataSetSeries (EOIDs) and CoveragesIDs.
"""
# parse the xml - received as answer to the request
xmldoc = minidom.parseString(in_xml)
# find all the tags (CoverageIds or DatasetSeriesIds)
tagid_node = xmldoc.getElementsByTagName(tag)
# number of found tags
n_elem = tagid_node.length
tag_ids = []
# store the found items
for n in range(0, n_elem):
tag_ids.append(tagid_node[n].childNodes.item(0).data)
# return the found items
return tag_ids