3 This file contains the base DataSource class, and all sub classes that implement their own methods for parsing data. 6 from __future__
import print_function
7 from __future__
import absolute_import
24 new_node =
node(node_data)
25 self._child_nodes.append(new_node)
43 return "<node data='%s' children=%s>" % (self.
data(),
str(self.
children()))
54 return "<data_source>" 62 _data, _sub_data, _file_name =
None,
None,
None 67 contents = "".
join(handle.readlines())
68 data = json.loads(contents)
73 return json_data_node.make(self.
_data)
82 _data, _sub_data, _file_name =
None,
None,
None 88 cursor = connection.cursor()
89 if query_object ==
None:
91 tables = cursor.execute(
"select name from sqlite_master where type = 'table'")
95 for table
in tables.fetchall():
96 table_to_columns[table[0]] = []
98 columns = cursor.execute(
"pragma table_info(%s)" % table[0])
99 for column
in columns.fetchall():
100 table_to_columns[table[0]].
append(
str(column[1]))
104 for table
in table_to_columns:
106 column_string =
",".
join(table_to_columns[table])
107 sql_query =
"select %s from %s" % (column_string, table)
108 results = cursor.execute(sql_query).fetchall()
109 for n
in range(0, len(results)):
110 results[n] =
dict(
zip(table_to_columns[table],
map(str, results[n])))
111 table_to_data[
str(table)] = results
112 self.
_data = json_data_node.make(table_to_data)
114 sql_query = query_object.to_sql()
132 if isinstance(data, list):
134 elif isinstance(data, dict):
146 current_json_node = self
148 data_to_use = current_json_node.data()[args[0]]
149 return json_data_node.make(data_to_use)
151 current_json_node = current_json_node.get(key)
152 return current_json_node
162 if isinstance(self.
_data, type_name):
163 lists.append(self.
_data)
164 if isinstance(self.
_data, list):
165 for item
in self.
_data:
166 lists += json_data_node.make(item).
find(type_name)
167 elif isinstance(self.
_data, dict):
168 for key
in self.
_data:
169 lists += json_data_node.make(self.
_data[key]).
find(type_name)
173 return "<json_data_node data='%s'>" %
str(self.
_data)
177 iterator_index =
None 180 self.
_data = data
if data !=
None else []
188 data = self.
get(len(self.
data())-1)
192 if data.__class__.__name__
in [
"json_list",
"json_dict",
"json_basic"]:
194 self._data.append(data)
216 for index
in indices:
220 final_list.append(self.
get(index).
data())
226 return json_data_node.make(final_list)
230 if not(type(member_name)
in [str, unicode]):
231 raise TypeError(
"Value given for member name must be a string.")
232 type_of_first_item = self.
data()[0].__class__
233 for item
in self.
data():
234 if item.__class__ != type_of_first_item:
236 return json_data_node.make(
map(
lambda item : getattr(item, member_name), self.
data()))
242 if len(self.
data()) == 0:
243 print(
"\nNo data to convert to dictionaries.\n")
246 if self.
get(0).
data().__class__.__name__
in [
"GlobalTag",
"GlobalTagMap",
"Tag",
"IOV",
"Payload"]:
248 new_data =
map(
lambda item : item.as_dicts(convert_timestamps=convert_timestamps), [item
for item
in self.
data()])
251 print(
"Data in json_list was not the correct type.")
258 def as_table(self, fit=["all"], columns=None, hide=None, col_width=None, row_nums=False):
260 if len(self.
data()) == 0:
261 print(
"\nNo data to draw table with.\n")
269 if self.
get(0).
data().__class__.__name__
in [
"GlobalTag",
"GlobalTagMap",
"GlobalTagMapRequest",
"Tag",
"IOV",
"Payload"]:
271 from .data_formats
import _objects_to_dicts
274 from .querying
import connection
279 headers = [header
for header
in models_dict[self.
get(0).
data().__class__.__name__.lower()].headers]
284 headers = data[0].
keys()
290 headers = [
"row"] + headers
293 for i, item
in enumerate(data):
294 data[i][
"row"] =
str(i)
299 if col_width ==
None:
301 table_width =
int(0.95*
int(subprocess.check_output([
"stty",
"size"]).
split(
" ")[1]))
302 col_width =
int(table_width/len(headers))
305 for n
in range(0, len(hide)):
306 del headers[headers.index(hide[n])]
308 def max_width_of_column(column, data):
309 max_width_found = len(
str(data[0][column]))
311 current_width = len(
str(item[column]))
312 if current_width > max_width_found:
313 max_width_found = current_width
314 if max_width_found > len(column):
315 return max_width_found
319 def cell(content, header, col_width, fit):
321 col_width_with_padding = col_width+2
322 col_width_substring = len(
str(content))
324 col_width_with_padding = col_width-2
if col_width-2 > 0
else 1
325 col_width_substring = col_width-5
if col_width-7 > 0
else 1
326 return (
"| {:<%s} " % (col_width_with_padding)).
format(
str(content)[0:col_width_substring].
replace(
"\n",
"")\
327 + (
"..." if not(fit)
and col_width_substring < len(
str(content))
else ""))
337 if not(column
in headers):
338 print(
"'%s' is not a valid column." % column)
341 column_to_width[column] = max_width_of_column(column, data)
342 surplus_width += column_to_width[column]-col_width
344 if len(
set(headers)-
set(fit)) != 0:
345 non_fited_width_surplus = surplus_width/len(
set(headers)-
set(fit))
347 non_fited_width_surplus = 0
349 for column
in headers:
350 if not(column
in fit):
351 column_to_width[column] = col_width - non_fited_width_surplus
353 for column
in headers:
354 column_to_width[column] = max_width_of_column(column, data)
356 ascii_string =
"\n%s\n\n" % table_name
if table_name !=
None else "\n" 357 for header
in headers:
358 ascii_string += cell(header, header, column_to_width[header], header
in fit)
360 horizontal_border =
"\n" 361 ascii_string += horizontal_border
363 for n
in range(0, len(headers)):
364 entry = item[headers[n]]
365 ascii_string += cell(entry, headers[n], column_to_width[headers[n]], headers[n]
in fit)
368 ascii_string += horizontal_border
369 ascii_string +=
"Showing %d rows\n\n" % len(data)
375 self.
_data = data
if data !=
None else {}
378 if data.__class__.__name__
in [
"json_list",
"json_dict",
"json_basic"]:
380 self.
_data[key] = data
386 self.
_data = data
if data !=
None else "" def add_child(self, node_data)
def __init__(self, data=None)
def get_members(self, member_name)
def replace(string, replacements)
def add_key(self, data, key)
def as_dicts(self, convert_timestamps=False)
S & print(S &os, JobReport::InputFile const &f)
def as_table(self, fit=["all"], columns=None, hide=None, col_width=None, row_nums=False)
def generate(map_blobs=False, class_name=None)
def class_name_to_column(cls)
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def __init__(self, data=None)
def find(self, type_name)
def __init__(self, json_file_name)
def __init__(self, data=None)
def __init__(self, data=None)
static std::string join(char **cmd)
def __init__(self, data=None)
def __init__(self, sqlite_file_name)
def indices(self, indices)
def add_child(self, data)