cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
parse.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 from ec2.error import InvalidFilter
20 import re
21 
22 ##
23 # @package src.ec2.helpers.parse
24 #
25 # @copyright Copyright (c) 2012 Institute of Nuclear Physics PAS <http://www.ifj.edu.pl/>
26 # @author Łukasz Chrząszcz <l.chrzaszcz@gmail.com>
27 #
28 
29 ##
30 #
31 # Przeszukuje parametry w poszukiwaniu kluczy @prm{prefix}@val{numer}@prm{suffix}
32 # i zwraca wartości pod tymi kluczami
33 #
34 def parseSequenceArguments(parameters, prefix = '', suffix = ''):
35  arguments = []
36  counter = 1
37  while True:
38  argument = parameters.get( prefix + str( counter ) + suffix )
39 
40  if argument is None:
41  break
42  counter += 1
43 
44  arguments.append( argument )
45 
46  return arguments
47 
48  # inna wersja
49  arguments = parameters
50 
51  if prefix:
52  arguments = [argument for argument in arguments if argument.startwith( prefix )]
53  if suffix:
54  arguments = [argument for argument in arguments if argument.startwith( suffix )]
55 
56  # we now have list of matching arguments
57  result_params = [ parameters[key] for key in arguments ]
58 
59 
60 ##
61 #
62 # Parsuje filtry. Szuka argumentów zaczynających się na 'Filter.' a kończących na '.Name',
63 # wartości tych argumentów będą kluczami słownika zwracanego. Następnie funkcja przeszukuje
64 # parametry w poszukiwaniu Filter.<numer filtra>.Value. Listę takową przypisuje do odpowiedniego
65 # miejsca w słowniku.
66 #
67 # @parameter{parameters,dict} Słownik parametrów przekazanych do serwera EC2 w requeście
68 #
69 # @response{dict(Filter.Name,[list]} Lista filtrów
70 #
71 def parseFilters(parameters):
72 
73  filter_names = parseSequenceArguments(parameters, 'Filter.', '.Name')
74  filters = {}
75  filter_no = 1
76  for filter in filter_names:
77  filters[filter] = parseSequenceArguments(parameters, 'Filter.' + str(filter_no) + ".Value.")
78  if not filters[filter]:
79  raise InvalidFilter
80  filter_no += 1
81 
82  return filters
83 
84 ##
85 #
86 # Wywołuje funkcję parseSequenceArguments z takimi samymi argumentami,
87 # a następnie rzutuje ja na integer. TODO ta funkcja chyba do wywalenia :D
88 #
89 def parseSequenceIntArguments(parameters, prefix = '', suffix = ''):
90  temp_arguments = parseSequenceArguments(parameters, prefix, suffix)
91 
92  result_params = [ int(argument) for argument in temp_arguments ]
93 
94  return result_params
95 
96 ##
97 #
98 # Sprawdza czy podany entity(String) - najczęściej odebrany jako request do serwera EC2
99 # jest poprawny. Jeżeli tak to zwraca ID bez przedrostka dla kompatybilności z CC1,
100 # w przeciwnym wypadku zwraca None.
101 #
102 # @parameter{entity,string} ID wybranego zasobu
103 # @parameter{entity_type,string} Wybrana wartość z Entities
104 #
105 # @response{
106 #
107 def parseID(entity, entity_type):
108 
109  if entity.startswith( entity_type + '-' ):
110  entity = entity.replace( entity_type + '-', '')
111  return entity
112 
113  return None
114 
115 def parseIDs(entities, entity_type):
116  result = []
117  for entity in entities:
118  if entity.startswith( entity_type + '-' ):
119  result.append( entity.replace( entity_type + '-', '') )
120  else:
121  return None
122  print 'Entity:' ,entity
123  print 'Entities:',result
124  return result
125 
126 def parseClmDate(clm_date):
127  # CLM date format: 16.02.2014, 21:32:54
128  # Amazon date format: YYYY-MM-DDTHH:MM:SS.000Z
129  correctPattern = '^[0-9]{2}\.[0-9]{2}\.[0-9]{4}, [0-9]{2}:[0-9]{2}:[0-9]{2}$'
130  pattern = re.compile( correctPattern )
131  if not pattern.match(clm_date):
132  return None
133 
134  day = str(clm_date[:2])
135  month = str(clm_date[3:5])
136  year = str(clm_date[6:10])
137  time = str(clm_date[12:])
138 
139  ec2_date = year + '-' + month + '-' + day + 'T' + time
140  return ec2_date
141