Начал учить python, попутно пишу на нем графическую морду к своей проге.
#!/usr/bin/env python3
from gi.repository import Gtk
import os
import subprocess
class Parser():
def __init__(self):
self.cpu_info = []
self.os_info = []
self.kernel_info = []
self.users_info = []
def parse_cpu_info(self):
pipe = subprocess.Popen(['hardinform', '-c'], 0, None, None,subprocess.PIPE)
test = lambda x,y: x[:len(y)] == y
access_count = 0
cpu_info = self.cpu_info
cpu_info.append({})
x = 0
a = pipe.stdout.readline()
while a:
if access_count == 8:
access_count = 0
x += 1
cpu_info.append({})
if test(a, '\tCPU:'):
cpu_info[x]['CPU'] = a[len('\tCPU:'):]
print("CPU number parsed successfully")
access_count += 1
elif test(a, '\t\tModel name:'):
cpu_info[x]['Model name'] = a[len('\t\tModel name:'):]
access_count += 1
elif test(a, '\t\tCurrent processor speed in MHz:'):
cpu_info[x]['Current speed in MHz'] = a[len('\t\tCurrent processor speed in MHz:'):]
access_count += 1
elif test(a, '\t\tProcessor cache size:'):
cpu_info[x]['Cache size'] = a[len('\t\tProcessor cache size:'):]
access_count += 1
elif test(a, '\t\tProcessor speed in Bogomips:'):
cpu_info[x]['Speed in bgmps'] = a[len('\t\tProcessor speed in Bogomips:'):]
access_count += 1
elif test(a, '\t\tTemperature:'):
cpu_info[x]['Temperature'] = a[len('\t\tTemperature:'):]
access_count += 1
elif test(a, '\t\tCritical temperature:'):
cpu_info[x]['Crit temperature'] = a[len('\t\tCritical temperature:'):]
access_count += 1
a = pipe.stdout.readline()
def parse_os_info(self):
pipe = subprocess.Popen(['hardinform', '-c'])
test = lambda x,y: x[len(y):] == y
a = str()
while a == pipe.communicate()[0]:
if test.call(a, '\t\tOS version:'):
pass
elif test.call(a, '\t\tUptime:'):
pass
def parse(self):
self.parse_cpu_info()
#return self.cpu_info
class Handler():
def __init__(self, user_data):
self.data = user_data
def add_items_to_categories_tree(self, widget):
treestore = self.data['categories_tree_store']
cpu = treestore.append(None, ('CPU',))
os = treestore.append(None, ('OS',))
users = treestore.append(os, ('Users',))
kernel = treestore.append(os, ('Kernel',))
def on_status_icon_activated(self):
pass
def on_status_icon_popup_menu(self, status_icon, button, activate_time):
self.data['status_icon_context_menu'].popup(None, None , self.data['status_icon'].position_menu, self.data['status_icon'], button, activate_time)
def on_main_window_delete(self, widget, event):
widget.hide()
def cpu_item_selected(self):
pass
def os_item_selected(self):
pass
def kernel_item_selected(self):
pass
def users_item_selected(self):
pass
def on_categories_tree_row_activated(self, a, v, f):
text = self.data['categories_tree_curr_text'].props.text
if text == 'CPU':
self.cpu_item_selected()
elif text == 'OS':
self.os_item_selected()
elif text == 'Kernel':
self.kernel_item_selected()
elif text == 'Users':
self.users_item_selected()
class Main():
def __init__(self):
parser = Parser()
cpu_data = parser.cpu_info
print(cpu_data[0]['CPU']) # вот тут ошибка
user_data = {}
gtk_builder = Gtk.Builder()
gtk_builder.add_from_file("/home/inish777/hardinform/share/hardinform-gui.ui")
user_data['main_window'] = main_window = gtk_builder.get_object("main_window")
user_data['categories_tree_curr_text'] = gtk_builder.get_object('curr_categories_tree_text')
user_data['categories_tree_store'] = gtk_builder.get_object('categories_tree_store')
user_data['information_tree_store'] = gtk_builder.get_object('information_tree_store')
user_data['place_for_widgets'] = gtk_builder.get_object('place_for_widgets')
user_data['status_icon'] = gtk_builder.get_object('hardinform_status_icon')
user_data['status_icon_context_menu'] = gtk_builder.get_object('status_icon_context_menu')
user_data['main_hbox'] = gtk_builder.get_object('main_hbox')
gtk_builder.connect_signals(Handler(user_data))
main_window.show_all()
Gtk.main()
Main()
cast