Skip to content

Commit f7e723c

Browse files
authored
0.7.0
Merge pull request #4 from arnobaer/devel-0.7.x
2 parents 5b42aaa + 7c5e523 commit f7e723c

File tree

6 files changed

+108
-76
lines changed

6 files changed

+108
-76
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Install using pip (>= 19.0).
77

88
```bash
99
pip install --upgrade pip
10-
pip install git+https://github.com/cms-l1-globaltrigger/tm-diff.git@0.6.3
10+
pip install git+https://github.com/cms-l1-globaltrigger/tm-diff.git@0.7.0
1111
```
1212

1313
## Basic usage
@@ -43,6 +43,7 @@ Use flag `-s|--skip <mode>` to ignore certain attributes. Options are
4343

4444
* `module` to skip implementation details (attributes `uuid_firmware`, `n_modules`, `module_id`, `module_index`)
4545
* `comment` to skip comments (attribute `comment`)
46+
* `labels` to skip algorithm labels (attribute `labels`)
4647

4748
**Example:**
4849

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
tm-python @ git+https://github.com/cms-l1-globaltrigger/tm-python@0.7.4
1+
tm-python @ git+https://github.com/cms-l1-globaltrigger/tm-python@0.8.0

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
setup(
66
name="tm-diff",
7-
version='0.6.3',
7+
version='0.7.0',
88
url="https://github.com/cms-l1-globaltrigger/tm-diff",
99
author="Bernhard Arnold",
1010
author_email="bernhard.arnold@cern.ch",
1111
description="Compare the content of two XML trigger menus.",
1212
long_description=long_description,
1313
packages=['tmDiff'],
1414
install_requires=[
15-
'tm-python @ git+https://github.com/cms-l1-globaltrigger/tm-python@0.7.4',
15+
'tm-python @ git+https://github.com/cms-l1-globaltrigger/tm-python@0.8.0',
1616
],
1717
entry_points={
1818
'console_scripts': [

tmDiff/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.6.3'
1+
__version__ = '0.7.0'

tmDiff/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
SKIP_MODULE = 'module'
1515
SKIP_COMMENT = 'comment'
16-
SKIP_CHOICES = [SKIP_MODULE, SKIP_COMMENT]
16+
SKIP_LABELS = 'labels'
17+
SKIP_CHOICES = [SKIP_MODULE, SKIP_COMMENT, SKIP_LABELS]
1718

1819
SORT_INDEX = 'index'
1920
SORT_NAME = 'name'
@@ -93,6 +94,10 @@ def main():
9394
if SKIP_COMMENT in args.skip:
9495
skip.append('comment')
9596

97+
# Skip comments
98+
if SKIP_LABELS in args.skip:
99+
skip.append('labels')
100+
96101
# Extract information from XMLs
97102
from_menu = menudiff.Menu(from_file)
98103
from_menu.skip = skip

tmDiff/menudiff.py

Lines changed: 96 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212
name: <name>
1313
expression: <expression>
1414
comment: <comment>
15+
labels: <labels>
1516
```
1617
1718
The printed line information refers to the extracted content (can be dumped
1819
with flag -d
1920
2021
"""
2122

22-
import tmTable
23-
2423
import datetime
2524
import difflib
26-
import logging
27-
import sys, os
25+
import os
26+
import sys
27+
28+
import tmTable
2829

2930
from . import __version__
3031

@@ -37,7 +38,7 @@ class TTY:
3738
blue = "\033[34m"
3839
magenta = "\033[35m"
3940

40-
class Diffable(object):
41+
class Diffable:
4142
"""Implements a diffabel object. To be inherited by classes defining class
4243
attribute `sorted_attribuites`.
4344
"""
@@ -57,19 +58,18 @@ def fmt_attr(self, attr):
5758
"""
5859
return "{0}: {1}".format(attr, getattr(self, attr))
5960

60-
def to_diff(self, skip=[]):
61+
def to_diff(self, skip=None):
6162
"""Returns diff-able list of attributes for unified diff.
6263
>>> o.to_diff()
6364
['foo: 42', 'bar: baz']
6465
>>> o.to_diff(skip=['bar']) # skip attributes
6566
['foo: 42']
6667
"""
68+
skip = skip or []
6769
return [self.fmt_attr(attr) for attr in self.sorted_attributes if attr not in skip]
6870

6971
class Meta(Diffable):
70-
"""Simple menu metadata container.
71-
>>> meta = Meta(**row)
72-
"""
72+
"""Diffable menu metadata container."""
7373

7474
sorted_attributes = (
7575
'name',
@@ -83,9 +83,7 @@ class Meta(Diffable):
8383
)
8484

8585
class Algorithm(Diffable):
86-
"""Simple algorithm container.
87-
>>> algorithm = Algorithm(**row)
88-
"""
86+
"""Diffable algorithm container."""
8987

9088
sorted_attributes = (
9189
'index',
@@ -94,15 +92,18 @@ class Algorithm(Diffable):
9492
'name',
9593
'expression',
9694
'comment',
95+
'labels',
9796
)
9897

9998
report_attributes = (
10099
'index',
101100
'name',
102101
'expression',
102+
'labels',
103103
)
104104

105105
class Cut(Diffable):
106+
"""Diffable cut container."""
106107

107108
sorted_attributes = (
108109
'name',
@@ -114,7 +115,7 @@ class Cut(Diffable):
114115
'comment',
115116
)
116117

117-
class Menu(object):
118+
class Menu:
118119
"""Simple menu container."""
119120

120121
def __init__(self, filename):
@@ -175,7 +176,7 @@ def dump_intermediate(self, outdir=None):
175176
if not outdir:
176177
outdir = os.getcwd()
177178
filename = "{0}.txt".format(os.path.basename(self.filename))
178-
with open(os.path.join(outdir, filename), 'wb') as fp:
179+
with open(os.path.join(outdir, filename), 'w') as fp:
179180
for line in self.to_diff():
180181
fp.write(line)
181182
fp.write(os.linesep)
@@ -184,9 +185,6 @@ def report_diff(fromfile, tofile, verbose=False, ostream=sys.stdout):
184185
"""Perform simple diff on two menus in TWiki format for reports.
185186
>>> report_diff(fromfile, tofile)
186187
"""
187-
fromlist = fromfile.to_diff()
188-
tolist = tofile.to_diff()
189-
190188
from_algorithms = {}
191189
to_algorithms = {}
192190

@@ -207,7 +205,7 @@ def added_algorithms(a, b):
207205
removed = added_algorithms(fromfile, tofile)
208206
updated = []
209207

210-
for name, fromalgorithm in from_algorithms.iteritems():
208+
for name, fromalgorithm in from_algorithms.items():
211209
if name in to_algorithms:
212210
toalgorithm = to_algorithms[name]
213211
differences = []
@@ -254,38 +252,51 @@ def unified_diff(fromfile, tofile, verbose=False, ostream=sys.stdout):
254252
fromlines = fromfile.to_diff()
255253
tolines = tofile.to_diff()
256254

255+
def write_added(line):
256+
if ostream.isatty():
257+
ostream.write(TTY.green)
258+
ostream.write(line)
259+
if ostream.isatty():
260+
ostream.write(TTY.clear)
261+
262+
def write_removed(line):
263+
if ostream.isatty():
264+
ostream.write(TTY.red)
265+
ostream.write(line)
266+
if ostream.isatty():
267+
ostream.write(TTY.clear)
268+
269+
def write_marker(line):
270+
if ostream.isatty():
271+
ostream.write(TTY.yellow)
272+
ostream.write(line)
273+
if ostream.isatty():
274+
ostream.write(TTY.clear)
275+
276+
def write_match(line):
277+
ostream.write(line)
278+
279+
count = 0
257280
for line in difflib.unified_diff(fromlines, tolines, fromfile=fromfile.filename, tofile=tofile.filename, lineterm=""):
281+
if count:
282+
ostream.write(os.linesep)
258283
# Print added lines
259284
if line.startswith('+'):
260-
if ostream.isatty():
261-
ostream.write(TTY.green)
262-
ostream.write(os.linesep)
263-
ostream.write(line)
264-
if ostream.isatty():
265-
ostream.write(TTY.clear)
285+
write_added(line)
266286
# Print removed lines
267287
elif line.startswith('-'):
268-
if ostream.isatty():
269-
ostream.write(TTY.red)
270-
ostream.write(os.linesep)
271-
ostream.write(line)
272-
if ostream.isatty():
273-
ostream.write(TTY.clear)
288+
write_removed(line)
274289
# Print diff markers
275290
elif line.startswith('@@'):
276-
if ostream.isatty():
277-
ostream.write(TTY.yellow)
278-
ostream.write(os.linesep)
279-
ostream.write(line)
280-
if ostream.isatty():
281-
ostream.write(TTY.clear)
291+
write_marker(line)
282292
# Print matching lines
283293
else:
284-
ostream.write(os.linesep)
285-
if ostream.isatty():
286-
ostream.write(TTY.clear)
287-
ostream.write(line)
288-
ostream.write(os.linesep)
294+
write_match(line)
295+
count += 1
296+
297+
# Omit newline if nothing was written.
298+
if count:
299+
ostream.write(os.linesep)
289300

290301
def context_diff(fromfile, tofile, verbose=False, ostream=sys.stdout):
291302
"""Perform context diff on two menus.
@@ -294,46 +305,61 @@ def context_diff(fromfile, tofile, verbose=False, ostream=sys.stdout):
294305
fromlines = fromfile.to_diff()
295306
tolines = tofile.to_diff()
296307

308+
def write_added(line):
309+
if ostream.isatty():
310+
ostream.write(TTY.green)
311+
ostream.write(line)
312+
if ostream.isatty():
313+
ostream.write(TTY.clear)
314+
315+
def write_removed(line):
316+
if ostream.isatty():
317+
ostream.write(TTY.red)
318+
ostream.write(line)
319+
if ostream.isatty():
320+
ostream.write(TTY.clear)
321+
322+
def write_changed(line):
323+
if ostream.isatty():
324+
ostream.write(TTY.magenta)
325+
ostream.write(line)
326+
if ostream.isatty():
327+
ostream.write(TTY.clear)
328+
329+
def write_marker(line):
330+
if ostream.isatty():
331+
ostream.write(TTY.yellow)
332+
ostream.write(line)
333+
if ostream.isatty():
334+
ostream.write(TTY.clear)
335+
336+
def write_match(line):
337+
ostream.write(line)
338+
339+
count = 0
297340
for line in difflib.context_diff(fromlines, tolines, fromfile=fromfile.filename, tofile=tofile.filename, lineterm=""):
341+
if count:
342+
ostream.write(os.linesep)
298343
# Print added lines
299344
if line.startswith('+ '):
300-
if ostream.isatty():
301-
ostream.write(TTY.green)
302-
ostream.write(os.linesep)
303-
ostream.write(line)
304-
if ostream.isatty():
305-
ostream.write(TTY.clear)
345+
write_added(line)
306346
# Print removed lines
307347
elif line.startswith('- '):
308-
if ostream.isatty():
309-
ostream.write(TTY.red)
310-
ostream.write(os.linesep)
311-
ostream.write(line)
312-
if ostream.isatty():
313-
ostream.write(TTY.clear)
348+
write_removed(line)
314349
# Print changed lines
315350
elif line.startswith('! '):
316-
if ostream.isatty():
317-
ostream.write(TTY.magenta)
318-
ostream.write(os.linesep)
319-
ostream.write(line)
320-
if ostream.isatty():
321-
ostream.write(TTY.clear)
351+
write_changed(line)
322352
# Print diff markers
323353
elif line.startswith('---') or line.startswith('***'):
324-
if ostream.isatty():
325-
ostream.write(TTY.yellow)
326-
ostream.write(os.linesep)
327-
ostream.write(line)
328-
if ostream.isatty():
329-
ostream.write(TTY.clear)
354+
write_marker(line)
330355
# Print matching lines
331356
else:
332-
ostream.write(os.linesep)
333-
if ostream.isatty():
334-
ostream.write(TTY.clear)
335-
ostream.write(line)
336-
ostream.write(os.linesep)
357+
write_match(line)
358+
count += 1
359+
360+
# Omit newline if nothing was written.
361+
if count:
362+
ostream.write(os.linesep)
337363

338364
def html_diff(fromfile, tofile, verbose=False, ostream=sys.stdout):
339365
"""Perform diff on two menus and writes results to HTML table.

0 commit comments

Comments
 (0)