Commit e73c5e85 authored by Morten Knutsen's avatar Morten Knutsen
Browse files

Add configfile option, and example config

parent d4fbbb66
......@@ -5,6 +5,7 @@ import sys
import os.path
from pajek import read_pajek
from model import Simulation, Model
from config import Config
from plotting import PlotUI
from textwrap import TextWrapper
from termcolor import colored, colored2, colored3
......@@ -20,6 +21,8 @@ class MetricShell(Cmd):
except IOError:
pass
self.config = Config()
readline.set_completer_delims(" ")
Cmd.__init__(self)
......@@ -35,7 +38,7 @@ class MetricShell(Cmd):
print
print "Initializing model...."
if not model:
self.model = Model(nx.Graph())
self.model = Model(nx.Graph(), self.config)
self.model.refresh_from_file(self.filename)
if linkloads:
self.model.refresh_linkloads()
......@@ -204,6 +207,9 @@ class MetricShell(Cmd):
return
def do_linkloads(self, args):
if not self.config.get('use_linkloads'):
print "Link loads are not enabled"
return
if self.model.refresh_linkloads():
print "OK, loads refreshed, use 'plot with-load' to plot"
if not self.simulation.linkloads:
......@@ -402,14 +408,20 @@ class MetricShell(Cmd):
self.gui.plot(G, graphdata, edge_cmap=cmap, edge_capa=capa)
def do_areaplot(self, args):
self.gui.clear()
if not self.config.get('use_areas'):
print "IS-IS areas are not enabled"
return
graphdata = {}
G = self.model.G
areas = self.model.get_areas(G.nodes())
if not areas:
print "No IS-IS areas known"
return
self.gui.clear()
graphdata = {}
graphdata['nodegroups'] = self.model.get_node_groups()
graphdata['areagroups'] = areas
graphdata['edgegroups'] = self.model.get_edge_groups()
......@@ -575,10 +587,12 @@ class MetricShell(Cmd):
print "Effects of changes:"
for arg in subargs:
if arg not in self.simulation.get_nodes(): continue
nodechanges = self.simulation.get_effects_node(arg)
if not nodechanges.keys(): print " * No changes for %s " % (arg)
print " * Details for %s " % (arg)
print
nodechanges = self.simulation.get_effects_node(arg)
self.tw.initial_indent=''
self.tw.subsequent_indent=' '*17
......
import os.path
class Config():
def __init__(self):
self.options = {
'max_metric': 63,
'use_areas': 1,
'use_linkloads': 1,
'linkloads_host': 'drift.uninett.no',
'linkloads_url': '/nett/ip-nett/load-now'
}
localconf = os.path.join(os.environ["HOME"], ".pymetricrc")
if os.path.isfile(localconf):
self._read_localconf(localconf)
def get(self, key):
if key not in self.options: return False
return self.options[key]
def _read_localconf(self, filename):
fh = open(filename, "r")
for line in fh.readlines():
if line.startswith('#'): continue
if not line: continue
line = line.strip()
tokens = line.split()
if len(tokens) > 1:
key, value = tokens
key = key.strip()
value = value.strip()
try:
self.options[key] = int(value)
except:
self.options[key] = value
......@@ -4,8 +4,9 @@ import utils
class Model:
def __init__(self, graph, debug=False):
def __init__(self, graph, config, debug=False):
self.graph = graph
self.config = config
self.debug = debug
self.G = self._make_weighted_copy()
self._refresh_betweenness()
......@@ -16,7 +17,10 @@ class Model:
self._refresh_all_paths()
def refresh_linkloads(self):
self.linkloads = utils.read_linkloads(self.graph)
if not self.config.get('use_linkloads'): return False
self.linkloads = utils.read_linkloads(self.graph,
self.config.get('linkloads_host'),
self.config.get('linkloads_url'))
if not self.linkloads: return False
self.linkload_parts = {}
return True
......@@ -846,6 +850,7 @@ class Simulation:
return self.effects
def get_effects_node(self, node):
if not node in self.effects: return {}
return self.effects[node]
def get_effects_summary(self):
......@@ -1488,6 +1493,8 @@ class Simulation:
import time
debug = False
max_metric = self.model.config.get('max_metric')
G = self.graph
H = self.graph.copy()
I = self.graph.copy()
......@@ -1672,7 +1679,7 @@ class Simulation:
while (not bad_effect) and (not minmax):
w = w + 1
if w > 63:
if w > max_metric:
if debug: print "Reached maximum metric..."
minmax = True
continue
......@@ -1708,7 +1715,7 @@ class Simulation:
else:
w = w + 1
if w > 63:
if w > max_metric:
if debug: print "Reached maximum metric..."
continue
I.add_edge(u,v,w)
......
# PyMetric configuration file ($HOME/.pymetricrc)
# Loads
use_linkloads 1
linkloads_host drift.uninett.no
linkloads_url /nett/ip-nett/load-now
# Routing
use_areas 1
max_metric 63
......@@ -74,10 +74,10 @@ def cap2str(capacity):
if not capacity in mapping: return "Unkown"
return mapping[capacity]
def read_linkloads(G):
def read_linkloads(G, host, url):
conn = httplib.HTTPConnection("drift.uninett.no")
conn.request("GET", "/nett/ip-nett/load-now")
conn = httplib.HTTPConnection(host)
conn.request("GET", url)
r1 = conn.getresponse()
if r1.status != 200:
conn.close()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment