3 This file contains the base DataSource class, and all sub classes that implement their own methods for parsing data.
22 new_node =
node(node_data)
23 self._child_nodes.append(new_node)
41 return "<node data='%s' children=%s>" % (self.
data(), str(self.
children()))
63 def to_sql(self, model_name=None, root=None):
69 return model +
"." + root.data().strip()
71 return root.data().strip()
73 child_nodes = [self.
to_sql(model, child_node)
for child_node
in root.children()]
74 return "(%s)" % ((
" " + root.data().
upper() +
" ").
join(child_nodes))
85 class data_source(node):
94 return "<data_source>"
102 _data, _sub_data, _file_name =
None,
None,
None
108 contents = "".
join(handle.readlines())
109 data = json.loads(contents)
112 except IOError
as io:
116 return json_data_node.make(self.
_data)
125 _data, _sub_data, _file_name =
None,
None,
None
132 cursor = connection.cursor()
133 if query_object ==
None:
135 tables = cursor.execute(
"select name from sqlite_master where type = 'table'")
138 table_to_columns = {}
139 for table
in tables.fetchall():
140 table_to_columns[table[0]] = []
142 columns = cursor.execute(
"pragma table_info(%s)" % table[0])
143 for column
in columns.fetchall():
144 table_to_columns[table[0]].
append(str(column[1]))
148 for table
in table_to_columns:
150 column_string =
",".
join(table_to_columns[table])
151 sql_query =
"select %s from %s" % (column_string, table)
152 results = cursor.execute(sql_query).fetchall()
153 for n
in range(0, len(results)):
154 results[n] =
dict(zip(table_to_columns[table], map(str, results[n])))
155 table_to_data[str(table)] = results
156 self.
_data = json_data_node.make(table_to_data)
158 sql_query = query_object.to_sql()
160 except IOError
as io:
179 if type(data) == list:
181 elif type(data) == dict:
193 current_json_node = self
195 data_to_use = current_json_node.data()[args[0]]
196 return json_data_node.make(data_to_use)
198 current_json_node = current_json_node.get(key)
199 return current_json_node
209 if type(self.
_data) == type_name:
210 lists.append(self.
_data)
211 if type(self.
_data) == list:
212 for item
in self.
_data:
213 lists += json_data_node.make(item).
find(type_name)
214 elif type(self.
_data) == dict:
215 for key
in self.
_data:
216 lists += json_data_node.make(self.
_data[key]).
find(type_name)
220 return "<json_data_node data='%s'>" % str(self.
_data)
224 iterator_index =
None
227 self.
_data = data
if data !=
None else []
235 data = self.
get(len(self.
data())-1)
239 if data.__class__.__name__
in [
"json_list",
"json_dict",
"json_basic"]:
241 self._data.append(data)
262 for index
in indices:
265 final_list.append(self.
get(index).
data())
268 return json_data_node.make(final_list)
274 if len(self.
data()) == 0:
275 print(
"\nNo data to convert to dictionaries.\n")
278 if self.
get(0).
data().__class__.__name__
in [
"GlobalTag",
"GlobalTagMap",
"GlobalTagMapRequest",
"Tag",
"IOV",
"Payload"]:
280 new_data = map(
lambda item : item.as_dicts(), [item
for item
in self.
data()])
283 print(
"Data in json_list was not the correct type.")
290 def as_table(self, fit=[], columns=None, hide=None, col_width=None, row_nums=False):
292 if len(self.
data()) == 0:
293 print(
"\nNo data to draw table with.\n")
301 if self.
get(0).
data().__class__.__name__
in [
"GlobalTag",
"GlobalTagMap",
"GlobalTagMapRequest",
"Tag",
"IOV",
"Payload"]:
303 from data_formats
import _objects_to_dicts
306 from querying
import connection
307 table_name = connection.class_name_to_column(self.
get(0).
data().__class__).
upper()
311 headers = [header
for header
in models_dict[self.
get(0).
data().__class__.__name__.lower()].headers]
316 headers = data[0].
keys()
322 headers = [
"row"] + headers
325 for i, item
in enumerate(data):
326 data[i][
"row"] = str(i)
331 if col_width ==
None:
333 table_width = int(0.95*int(subprocess.check_output([
"stty",
"size"]).
split(
" ")[1]))
334 col_width = int(table_width/len(headers))
337 for n
in range(0, len(hide)):
338 del headers[headers.index(hide[n])]
340 def max_width_of_column(column, data):
341 max_width_found = len(str(data[0][column]))
343 current_width = len(str(item[column]))
344 if current_width > max_width_found:
345 max_width_found = current_width
346 if max_width_found > len(column):
347 return max_width_found
351 def cell(content, header, col_width, fit):
353 col_width_with_padding = col_width+2
354 col_width_substring = len(str(content))
356 col_width_with_padding = col_width-2
if col_width-2 > 0
else 1
357 col_width_substring = col_width-5
if col_width-7 > 0
else 1
358 return (
"| {:<%s} " % (col_width_with_padding)).
format(str(content)[0:col_width_substring].
replace(
"\n",
"")\
359 + (
"..." if not(fit)
and col_width_substring < len(str(content))
else ""))
369 if not(column
in headers):
370 print(
"'%s' is not a valid column." % column)
373 column_to_width[column] = max_width_of_column(column, data)
374 surplus_width += column_to_width[column]-col_width
376 if len(
set(headers)-
set(fit)) != 0:
377 non_fited_width_surplus = surplus_width/len(
set(headers)-
set(fit))
379 non_fited_width_surplus = 0
381 for column
in headers:
382 if not(column
in fit):
383 column_to_width[column] = col_width - non_fited_width_surplus
385 for column
in headers:
386 column_to_width[column] = max_width_of_column(column, data)
388 ascii_string =
"\n%s\n\n" % table_name
if table_name !=
None else "\n"
389 for header
in headers:
390 ascii_string += cell(header.upper(), header, column_to_width[header], header
in fit)
392 horizontal_border =
"\n"
393 ascii_string += horizontal_border
395 for n
in range(0, len(headers)):
396 entry = item[headers[n]]
397 ascii_string += cell(entry, headers[n], column_to_width[headers[n]], headers[n]
in fit)
400 ascii_string += horizontal_border
406 self.
_data = data
if data !=
None else {}
409 if data.__class__.__name__
in [
"json_list",
"json_dict",
"json_basic"]:
411 self.
_data[key] = data
417 self.
_data = data
if data !=
None else ""
std::string print(const Track &, edm::Verbosity=edm::Concise)
Track print utility.
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
static std::string join(char **cmd)