Skip to content
This repository
Browse code

additional template fields for readmes so they can look nicer even wi…

…th varying-width IPs
  • Loading branch information...
commit 675c581fa18b57b801782079ed33f7b095898c7f 1 parent c203f63
David Underhill authored

Showing 1 changed file with 25 additions and 8 deletions. Show diff stats Hide diff stats

  1. +25 8 web/vnswww/models.py
33 web/vnswww/models.py
@@ -144,11 +144,14 @@ class TopologyTemplate(Model):
144 144 help_text='The organization this template belongs to.')
145 145 visibility = IntegerField(choices=VISIBILITY_CHOICES,
146 146 help_text='Who may see and use this template.')
147   - readme = TextField(help_text='Template of the readme which will explain ' + \
148   - 'the topology to the user. Template tags start with $ ' + \
149   - 'and include the following: $topo.id, $sim.gatewayip, ' + \
150   - "and $node.port.ip where node is a node's name and " + \
151   - "port is a port's name (e.g., $Server1.eth0.ip).")
  147 + readme = TextField(help_text='''Template of the readme which will explain \
  148 +the topology to the user. Template tags start with $: \
  149 +$topo.id, $topo.gatewayip[15], and $node.port.ip[15[R]|_SameSzXY] where node is \
  150 +a node's name and port is a port's name and X is one of L (left) or R (right) \
  151 +and Y is one of D (dash) or S (space) (e.g., $Server1.eth0.ip or \
  152 +$Server1.eth0.ip_SameSzLD [the latter will be replaced with the IP plus one \
  153 +space and enough dashes on the left so that the replacement is the same size \
  154 +as the source text]).''')
152 155 rtable = TextField(help_text='Template of the rtable for the topology, if any.')
153 156
154 157 def get_root_port(self):
@@ -169,7 +172,7 @@ def get_root_port(self):
169 172 except IndexError:
170 173 return None # no ports in this topology
171 174
172   - TEMPLATE_TEXT_REGEXP = re.compile(r'[$][a-zA-Z0-9.]+')
  175 + TEMPLATE_TEXT_REGEXP = re.compile(r'[$][a-zA-Z0-9._]+')
173 176 def render_template_text(self, sim, topo, txt):
174 177 # build the dictionary of all valid substitutions
175 178 values = {}
@@ -177,8 +180,22 @@ def render_template_text(self, sim, topo, txt):
177 180 values['$topo.gatewayip15'] = ('%-15s' % sim.gatewayIP)
178 181 values['$topo.id'] = topo.id
179 182 for ipa in IPAssignment.objects.filter(port__node__template=self, topology=topo):
180   - values['$%s.%s.ip' % (ipa.port.node.name, ipa.port.name)] = ipa.ip
181   - values['$%s.%s.ip15' % (ipa.port.node.name, ipa.port.name)] = ('%-15s' % ipa.ip)
  183 + t = (ipa.port.node.name, ipa.port.name)
  184 + values['$%s.%s.ip' % t] = ipa.ip
  185 + values['$%s.%s.ip15' % t] = ('%-15s' % ipa.ip)
  186 + values['$%s.%s.ip15R' % t] = ('%15s' % ipa.ip)
  187 +
  188 + more_pad_len = 0
  189 + space_pad = ''
  190 + src_len = len('$%s.%s.ip_SameSzXY' % t) # min size is 16 even if %s are both 1 char ... intentional: matches min size of an IPv4 addr + 1
  191 + ip_len = len(str(ipa.ip))
  192 + if ip_len < src_len:
  193 + space_pad = ' '
  194 + more_pad_len = max(src_len - ip_len - 1, 0)
  195 + values['$%s.%s.ip_SameSzLS' % t] = '%s%s%s' % (' ' * more_pad_len, space_pad, ipa.ip)
  196 + values['$%s.%s.ip_SameSzLD' % t] = '%s%s%s' % ('-' * more_pad_len, space_pad, ipa.ip)
  197 + values['$%s.%s.ip_SameSzRS' % t] = '%s%s%s' % (ipa.ip, space_pad, ' ' * more_pad_len)
  198 + values['$%s.%s.ip_SameSzRD' % t] = '%s%s%s' % (ipa.ip, space_pad, '-' * more_pad_len)
182 199
183 200 # define a function for substituting the appropriate value for an exp
184 201 def repl(m):

0 comments on commit 675c581

Please sign in to comment.
Something went wrong with that request. Please try again.