brainrat-private  5.1.2
python_helpers.h
Go to the documentation of this file.
1 
2 /* Copyright (C) 2000-2013 CEA
3  *
4  * This software and supporting documentation were developed by
5  * bioPICSEL
6  * CEA/DSV/I²BM/MIRCen/LMN, Batiment 61,
7  * 18, route du Panorama
8  * 92265 Fontenay-aux-Roses
9  * France
10  */
11 #ifndef BIOPROCESSING_IO_PYTHON_HELPERS_H
12 #define BIOPROCESSING_IO_PYTHON_HELPERS_H
13 
14 #include <string>
15 #include <vector>
16 
17 #include <cartobase/object/property.h>
18 #include <cartobase/object/object.h>
19 #include <cartobase/object/pythonwriter.h>
20 #include <cartobase/object/pythonreader.h>
21 
22 template <typename T >
23 carto::GenericObject* listHelper( carto::GenericObject* object, const std::string & semantic, soma::PythonReader & r )
24 {
25  bool end = false;
26  std::string id, type;
27  soma::DataSource & ds = *r.dataSource();
28 
29  if( !ds.isOpen() )
30  return 0;
31  r.skipWhile( " \t\n\\\r" );
32  if( r.eof() )
33  carto::io_error::launchErrnoExcept( ds.url() );
34  char c = ds.getch(), mark;
35  if( ds.eof() )
36  carto::io_error::launchErrnoExcept( ds.url() );
37  if( c == '(' )
38  mark = ')';
39  else if( c == '[' )
40  mark = ']';
41  else
42  {
43  throw std::runtime_error( std::string( "PythonReader: Not a list/tuple: " )
44  + r.name() + ", line " + r.lineString() );
45  }
46 
47  std::string synt = "", contsem = "";
48  carto::GenericObject *obj = new carto::ValueObject<T >;
49 
50  // Try to find syntaxed object for the type
51  bool genfallback = false;
52  soma::PythonReader::HelperSet::const_iterator h = r.helpers().end();
53  carto::SyntaxSet::const_iterator syntax = r.syntaxes().find( obj->type() );
54  if( syntax == r.syntaxes().end() && !genfallback )
55  {
56  syntax = r.syntaxes().find( "__generic__" );
57  genfallback = true;
58  }
59 
60  if( syntax != r.syntaxes().end() )
61  {
62  synt = syntax->first; // update according to real syntax
63  carto::Syntax::const_iterator property = syntax->second.find( "" );
64  if( property == syntax->second.end() && !genfallback )
65  {
66  genfallback = true;
67  carto::SyntaxSet::const_iterator syntax2 = r.syntaxes().find( "__generic__" );
68  if( syntax2 != r.syntaxes().end() )
69  {
70  syntax = syntax2;
71  property = syntax->second.find( "" );
72  }
73  }
74  if( property == syntax->second.end() )
75  {
76  property = syntax->second.find( "__fallback__" );
77  }
78  if( property != syntax->second.end() )
79  {
80  contsem = property->second.type;
81  h = r.helpers().find( contsem );
82  }
83  }
84 
85 
86  try
87  {
88  do
89  {
90  r.skipWhile( " \t\n\\\r" );
91  if( r.eof() )
92  carto::io_error::launchErrnoExcept( ds.url() );
93  c = ds.getch();
94  if( ds.eof() )
95  carto::io_error::launchErrnoExcept( ds.url() );
96  if( c == ',' ) // separator
97  {
98  r.skipWhile( " \t\n\\\r" );
99  if( r.eof() )
100  carto::io_error::launchErrnoExcept( ds.url() );
101  c = ds.getch();
102  if( ds.eof() )
103  carto::io_error::launchErrnoExcept( ds.url() );
104  }
105  if( c == mark )
106  {
107  end = true;
108  break; // end
109  }
110  ds.ungetch( c );
111 
112  carto::GenericObject* o1;
113  if( h != r.helpers().end() )
114  {
115  // call the associated I/O function
116  o1 = (h->second)( obj, contsem, r );
117  }
118  else
119  {
120  o1 = r.read( obj, contsem );
121  }
122 
123  obj->GenericObject::value<T >().push_back( o1->GenericObject::value<typename T::value_type >() );
124  }
125  while( ds.isOpen() );
126 
127  return( obj );
128  }
129  catch( std::exception & e )
130  {
131  delete obj;
132  throw;
133  }
134 }
135 
136 template <typename T>
137 carto::GenericObject* propertySetHelper(carto::GenericObject *, const std::string &,
138  soma::PythonReader & r)
139 {
140  std::string synt;
141  soma::DataSource & ds = *r.dataSource();
142  char c;
143 
144  r.skipWhile( " \t\n\\\r" );
145  if( r.eof() )
146  carto::io_error::launchErrnoExcept( ds.url() );
147  c = ds.getch();
148  if( ds.eof() )
149  carto::io_error::launchErrnoExcept( ds.url() );
150  if ( c != '{' ) {
151  std::cerr << "Attempt to read a dictionary not starting with '{'" << std::endl;
152  return 0;
153  }
154  r.skipWhile( " \t\n\\\r" );
155  if( r.eof() )
156  carto::io_error::launchErrnoExcept( ds.url() );
157  c = ds.getch();
158  if( ds.eof() )
159  carto::io_error::launchErrnoExcept( ds.url() );
160  if( c == 'u' )
161  {
162  c = ds.getch();
163  if( ds.eof() )
164  carto::io_error::launchErrnoExcept( ds.url() );
165  }
166 
167  // expect attribute name
168  switch( c )
169  {
170  case '0':
171  case '1':
172  case '2':
173  case '3':
174  case '4':
175  case '5':
176  case '6':
177  case '7':
178  case '8':
179  case '9':
180  case '.':
181  case '-':
182  case '+':
183  case '\'': // string identifier
184  case '"':
185  {
186  ds.ungetch( c );
187  carto::GenericObject *obj;
188 
189  obj = new carto::ValueObject<T>;
190 
191  try
192  {
193  r.readDictionary2( *obj );
194  }
195  catch( std::exception & e )
196  {
197  delete obj;
198  throw;
199  }
200 
201  return( obj );
202  }
203  break;
204  case '}':
205  return new carto::ValueObject<T>;
206  break;
207  }
208  throw carto::parse_error( "unrecognized tag start", std::string() + c,
209  ds.url(), r.line() );
210  return 0;
211 }
212 
213 #endif
carto::GenericObject * listHelper(carto::GenericObject *object, const std::string &semantic, soma::PythonReader &r)
carto::GenericObject * propertySetHelper(carto::GenericObject *, const std::string &, soma::PythonReader &r)