reformatted code to pep8 convention

This commit is contained in:
Guillaume Raffy 2018-08-27 14:54:55 +00:00
parent 197e94d320
commit e9f3c5a784
1 changed files with 253 additions and 258 deletions

View File

@ -6,16 +6,12 @@
This application takes its input from a database, currently in the form of an sql dump, but it could easily be adapted to read directly from a mysql database This application takes its input from a database, currently in the form of an sql dump, but it could easily be adapted to read directly from a mysql database
''' '''
import sqlite3
import os
import re import re
import sys
import pygraphviz import pygraphviz
from inventory import Inventory from inventory import Inventory
from SimpaDbUtil import SqlFile, SqlDatabaseReader from SimpaDbUtil import SqlFile, SqlDatabaseReader
def add_capacity_constraints(capacity1, capacity2): def add_capacity_constraints(capacity1, capacity2):
""" """
combines 2 capacity constraints (max amperes) together combines 2 capacity constraints (max amperes) together
@ -32,6 +28,7 @@ def add_capacity_constraints(capacity1, capacity2):
else: else:
return min(capacity1, capacity2) return min(capacity1, capacity2)
class Machine(object): class Machine(object):
""" """
represents a device with input and output power plugs. It could represent a power consumer such as a server (in which case, it has no output plug), or a power distrubtion unit (in which case, it has one input plug and more than one output plugs), or even something else... represents a device with input and output power plugs. It could represent a power consumer such as a server (in which case, it has no output plug), or a power distrubtion unit (in which case, it has one input plug and more than one output plugs), or even something else...
@ -45,7 +42,7 @@ class Machine(object):
self.power_consumption = 0.0 self.power_consumption = 0.0
def get_plug(self, plug_name): def get_plug(self, plug_name):
plugs = {'i': self.input_plugs, 'o':self.output_plugs}[plug_name[0]] plugs = {'i': self.input_plugs, 'o': self.output_plugs}[plug_name[0]]
if plug_name not in plugs: if plug_name not in plugs:
plugs[plug_name] = Plug(plug_name, self, self.power_config) plugs[plug_name] = Plug(plug_name, self, self.power_config)
return plugs[plug_name] return plugs[plug_name]
@ -54,7 +51,7 @@ class Machine(object):
capacity = None capacity = None
if len(self.input_plugs) > 0: if len(self.input_plugs) > 0:
capacity = self.input_plugs.values()[0].get_max_amperes() capacity = self.input_plugs.values()[0].get_max_amperes()
capacity = add_capacity_constraints (capacity, self.current_capacity_constraint) capacity = add_capacity_constraints(capacity, self.current_capacity_constraint)
return capacity return capacity
def get_outgoing_connections(self): def get_outgoing_connections(self):
@ -104,15 +101,15 @@ class Plug(object):
in_con = self.get_incoming_connection() in_con = self.get_incoming_connection()
if in_con: if in_con:
# apply incoming connection amperes limitation # apply incoming connection amperes limitation
capacity = add_capacity_constraints (capacity, in_con.get_max_amperes()) capacity = add_capacity_constraints(capacity, in_con.get_max_amperes())
# print(str(self)+ 'after incoming connection amperes limitation, capacity = ' + str(capacity)) # print(str(self)+ 'after incoming connection amperes limitation, capacity = ' + str(capacity))
else: else:
# apply the machine containing this plug's amperes limitation # apply the machine containing this plug's amperes limitation
capacity = add_capacity_constraints (capacity, self.machine.get_max_amperes()) capacity = add_capacity_constraints(capacity, self.machine.get_max_amperes())
# print(str(self)+'apply the machine containing this plug s amperes limitation, capacity = ' + str(capacity)) # print(str(self)+'apply the machine containing this plug s amperes limitation, capacity = ' + str(capacity))
# apply this plug's amperes limitation # apply this plug's amperes limitation
capacity = add_capacity_constraints (capacity, self.current_capacity_constraint) capacity = add_capacity_constraints(capacity, self.current_capacity_constraint)
# print(str(self)+'after apply this plug s amperes limitation, capacity = ' + str(capacity)) # print(str(self)+'after apply this plug s amperes limitation, capacity = ' + str(capacity))
return capacity return capacity
@ -140,7 +137,7 @@ class Connection(object):
def get_max_amperes(self): def get_max_amperes(self):
capacity = self.from_plug.get_max_amperes() capacity = self.from_plug.get_max_amperes()
capacity = add_capacity_constraints (capacity, self.to_plug.current_capacity_constraint) capacity = add_capacity_constraints(capacity, self.to_plug.current_capacity_constraint)
if self.current_capacity_constraint is not None: if self.current_capacity_constraint is not None:
capacity = min(capacity, self.current_capacity_constraint) capacity = min(capacity, self.current_capacity_constraint)
return capacity return capacity
@ -179,7 +176,7 @@ class PowerConfig(object):
rows = inventory.query("SELECT * FROM machine_to_power") rows = inventory.query("SELECT * FROM machine_to_power")
for row in rows: for row in rows:
(to_plug_as_str, from_plug_as_str, powercordid)=row (to_plug_as_str, from_plug_as_str, powercordid) = row
if to_plug_as_str != '': if to_plug_as_str != '':
conn = self._add_connection(from_plug_as_str, to_plug_as_str) conn = self._add_connection(from_plug_as_str, to_plug_as_str)
for plug in (conn.from_plug, conn.to_plug): for plug in (conn.from_plug, conn.to_plug):
@ -192,7 +189,7 @@ class PowerConfig(object):
for row in rows: for row in rows:
# print row # print row
# ('ups1_o1', 16.0), # ('ups1_o1', 16.0),
(to_plug_as_str, max_amps_as_str)=row (to_plug_as_str, max_amps_as_str) = row
to_plug = self._get_plug(to_plug_as_str) to_plug = self._get_plug(to_plug_as_str)
to_plug.set_current_capacity_constraint(float(max_amps_as_str)) to_plug.set_current_capacity_constraint(float(max_amps_as_str))
@ -211,7 +208,6 @@ class PowerConfig(object):
if power_consumption is not None: if power_consumption is not None:
machine.power_consumption = power_consumption machine.power_consumption = power_consumption
def get_connection_to(self, to_plug): def get_connection_to(self, to_plug):
for connection in self.connections: for connection in self.connections:
if connection.to_plug == to_plug: if connection.to_plug == to_plug:
@ -226,7 +222,7 @@ class PowerConfig(object):
def _get_plug(self, plug_as_str): def _get_plug(self, plug_as_str):
elements = plug_as_str.split('_') elements = plug_as_str.split('_')
plug_name = elements[-1] plug_name = elements[-1]
machine_name = plug_as_str[0:-(len(plug_name)+1)] machine_name = plug_as_str[0:-(len(plug_name) + 1)]
machine = self._get_machine(machine_name) machine = self._get_machine(machine_name)
return machine.get_plug(plug_name) return machine.get_plug(plug_name)
@ -244,56 +240,55 @@ class PowerConfig(object):
return s return s
def power_config_to_svg( power_config, svg_file_path ): def power_config_to_svg(power_config, svg_file_path):
""" """
creates a svg diagram representing the input power configuration creates a svg diagram representing the input power configuration
:param PowerConfig power_config: the input power config :param PowerConfig power_config: the input power config
""" """
graph = pygraphviz.AGraph() graph = pygraphviz.AGraph()
graph.graph_attr['overlap']='false' graph.graph_attr['overlap'] = 'false'
graph.graph_attr['splines']='true' graph.graph_attr['splines'] = 'true'
graph.edge_attr['colorscheme'] = 'rdylgn9' # 'brbg11'
graph.edge_attr['colorscheme']='rdylgn9' # 'brbg11' graph.node_attr['shape'] = 'box'
graph.node_attr['shape']='box' graph.node_attr['height'] = 0.3 # default 0.5 inches
graph.node_attr['height']=0.3 # default 0.5 inches graph.node_attr['fontname'] = 'Helvetica' # default : Times-Roman
graph.node_attr['fontname']='Helvetica' # default : Times-Roman graph.edge_attr['fontsize'] = 10 # default : 14 pt
graph.edge_attr['fontsize']=10 # default : 14 pt graph.edge_attr['len'] = 1.5 # default : 1.0
graph.edge_attr['len']=1.5 # default : 1.0
def hotness_to_hsv_color(hotness): def hotness_to_hsv_color(hotness):
""" """
:param float hotness: temperature of the wire ratio (0.0 : cold -> 1.0 : hot) :param float hotness: temperature of the wire ratio (0.0 : cold -> 1.0 : hot)
""" """
clamped_hotness = max(min(hotness, 1.0), 0.0) clamped_hotness = max(min(hotness, 1.0), 0.0)
return "%f, 1.0, 0.8" % ((1.0-clamped_hotness)*0.33) return "%f, 1.0, 0.8" % ((1.0 - clamped_hotness) * 0.33)
#graph.add_node('a') # graph.add_node('a')
#graph.add_node('b') # graph.add_node('b')
#graph.add_edge(u'a',u'b',color='blue') # graph.add_edge(u'a',u'b',color='blue')
#graph.add_edge(u'b',u'a',color='blue') # graph.add_edge(u'b',u'a',color='blue')
for con in power_config.connections: for con in power_config.connections:
# print(con.from_plug.machine.name, con.to_plug.machine.name) # print(con.from_plug.machine.name, con.to_plug.machine.name)
if not con.is_redundancy_cable(): # don't display redundancy cables, as they might overlap and hide the main one if not con.is_redundancy_cable(): # don't display redundancy cables, as they might overlap and hide the main one
power_consumption = con.get_power_consumption() power_consumption = con.get_power_consumption()
amperes = power_consumption/220.0 amperes = power_consumption / 220.0
color='green' color = 'green'
capacity = con.get_max_amperes() capacity = con.get_max_amperes()
if capacity == None: if capacity is None:
label = '?' label = '?'
else: else:
label = str(capacity) + 'A' label = str(capacity) + 'A'
if amperes/capacity > 1.0: if amperes / capacity > 1.0:
color='red' color = 'red'
elif amperes/capacity > 0.75: elif amperes / capacity > 0.75:
color='orange' color = 'orange'
else: else:
color='green' color = 'green'
label = "%.1f/%s" % (amperes, label) label = "%.1f/%s" % (amperes, label)
#color='//%d' % int(9.0-amperes/capacity*8) # color='//%d' % int(9.0-amperes/capacity*8)
color = hotness_to_hsv_color(pow(amperes/capacity, 4.0)) color = hotness_to_hsv_color(pow(amperes / capacity, 4.0))
graph.add_edge(con.from_plug.machine.name, con.to_plug.machine.name, color=color, label=label, penwidth=capacity*0.25) graph.add_edge(con.from_plug.machine.name, con.to_plug.machine.name, color=color, label=label, penwidth=capacity * 0.25)
graph.layout(prog='twopi') graph.layout(prog='twopi')
graph.draw(svg_file_path) graph.draw(svg_file_path)