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()))
52 return "<data_source>" 60 _data, _sub_data, _file_name =
None,
None,
None 65 contents = "".
join(handle.readlines())
66 data = json.loads(contents)
71 return json_data_node.make(self.
_data)
80 _data, _sub_data, _file_name =
None,
None,
None 86 cursor = connection.cursor()
87 if query_object ==
None:
89 tables = cursor.execute(
"select name from sqlite_master where type = 'table'")
93 for table
in tables.fetchall():
94 table_to_columns[table[0]] = []
96 columns = cursor.execute(
"pragma table_info(%s)" % table[0])
97 for column
in columns.fetchall():
98 table_to_columns[table[0]].
append(
str(column[1]))
102 for table
in table_to_columns:
104 column_string =
",".
join(table_to_columns[table])
105 sql_query =
"select %s from %s" % (column_string, table)
106 results = cursor.execute(sql_query).fetchall()
107 for n
in range(0, len(results)):
108 results[n] =
dict(
zip(table_to_columns[table],
map(str, results[n])))
109 table_to_data[
str(table)] = results
110 self.
_data = json_data_node.make(table_to_data)
112 sql_query = query_object.to_sql()
130 if isinstance(data, list):
132 elif isinstance(data, dict):
144 current_json_node = self
146 data_to_use = current_json_node.data()[args[0]]
147 return json_data_node.make(data_to_use)
149 current_json_node = current_json_node.get(key)
150 return current_json_node
160 if isinstance(self.
_data, type_name):
161 lists.append(self.
_data)
162 if isinstance(self.
_data, list):
163 for item
in self.
_data:
164 lists += json_data_node.make(item).
find(type_name)
165 elif isinstance(self.
_data, dict):
166 for key
in self.
_data:
167 lists += json_data_node.make(self.
_data[key]).
find(type_name)
171 return "<json_data_node data='%s'>" %
str(self.
_data)
175 iterator_index =
None 178 self.
_data = data
if data !=
None else []
186 data = self.
get(len(self.
data())-1)
190 if data.__class__.__name__
in [
"json_list",
"json_dict",
"json_basic"]:
192 self._data.append(data)
214 for index
in indices:
218 final_list.append(self.
get(index).
data())
224 return json_data_node.make(final_list)
228 if not(type(member_name)
in [str, unicode]):
229 raise TypeError(
"Value given for member name must be a string.")
230 type_of_first_item = self.
data()[0].__class__
231 for item
in self.
data():
232 if item.__class__ != type_of_first_item:
234 return json_data_node.make(
map(
lambda item : getattr(item, member_name), self.
data()))
240 if len(self.
data()) == 0:
241 print(
"\nNo data to convert to dictionaries.\n")
244 if self.
get(0).
data().__class__.__name__
in [
"GlobalTag",
"GlobalTagMap",
"Tag",
"IOV",
"Payload"]:
246 new_data =
map(
lambda item : item.as_dicts(convert_timestamps=convert_timestamps), [item
for item
in self.
data()])
249 print(
"Data in json_list was not the correct type.")
256 def as_table(self, fit=["all"], columns=None, hide=None, col_width=None, row_nums=False):
258 if len(self.
data()) == 0:
259 print(
"\nNo data to draw table with.\n")
267 if self.
get(0).
data().__class__.__name__
in [
"GlobalTag",
"GlobalTagMap",
"GlobalTagMapRequest",
"Tag",
"IOV",
"Payload"]:
269 from data_formats
import _objects_to_dicts
272 from querying
import connection
277 headers = [header
for header
in models_dict[self.
get(0).
data().__class__.__name__.lower()].headers]
282 headers = data[0].
keys()
288 headers = [
"row"] + headers
291 for i, item
in enumerate(data):
292 data[i][
"row"] =
str(i)
297 if col_width ==
None:
299 table_width =
int(0.95*
int(subprocess.check_output([
"stty",
"size"]).
split(
" ")[1]))
300 col_width =
int(table_width/len(headers))
303 for n
in range(0, len(hide)):
304 del headers[headers.index(hide[n])]
306 def max_width_of_column(column, data):
307 max_width_found = len(
str(data[0][column]))
309 current_width = len(
str(item[column]))
310 if current_width > max_width_found:
311 max_width_found = current_width
312 if max_width_found > len(column):
313 return max_width_found
317 def cell(content, header, col_width, fit):
319 col_width_with_padding = col_width+2
320 col_width_substring = len(
str(content))
322 col_width_with_padding = col_width-2
if col_width-2 > 0
else 1
323 col_width_substring = col_width-5
if col_width-7 > 0
else 1
324 return (
"| {:<%s} " % (col_width_with_padding)).
format(
str(content)[0:col_width_substring].
replace(
"\n",
"")\
325 + (
"..." if not(fit)
and col_width_substring < len(
str(content))
else ""))
335 if not(column
in headers):
336 print(
"'%s' is not a valid column." % column)
339 column_to_width[column] = max_width_of_column(column, data)
340 surplus_width += column_to_width[column]-col_width
342 if len(
set(headers)-
set(fit)) != 0:
343 non_fited_width_surplus = surplus_width/len(
set(headers)-
set(fit))
345 non_fited_width_surplus = 0
347 for column
in headers:
348 if not(column
in fit):
349 column_to_width[column] = col_width - non_fited_width_surplus
351 for column
in headers:
352 column_to_width[column] = max_width_of_column(column, data)
354 ascii_string =
"\n%s\n\n" % table_name
if table_name !=
None else "\n" 355 for header
in headers:
356 ascii_string += cell(header, header, column_to_width[header], header
in fit)
358 horizontal_border =
"\n" 359 ascii_string += horizontal_border
361 for n
in range(0, len(headers)):
362 entry = item[headers[n]]
363 ascii_string += cell(entry, headers[n], column_to_width[headers[n]], headers[n]
in fit)
366 ascii_string += horizontal_border
367 ascii_string +=
"Showing %d rows\n\n" % len(data)
373 self.
_data = data
if data !=
None else {}
376 if data.__class__.__name__
in [
"json_list",
"json_dict",
"json_basic"]:
378 self.
_data[key] = data
384 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)