Skip to content

Commit 7c5e523

Browse files
committed
skip labels
1 parent ce98c92 commit 7c5e523

File tree

3 files changed

+101
-71
lines changed

3 files changed

+101
-71
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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: 94 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',
@@ -105,6 +103,7 @@ class Algorithm(Diffable):
105103
)
106104

107105
class Cut(Diffable):
106+
"""Diffable cut container."""
108107

109108
sorted_attributes = (
110109
'name',
@@ -116,7 +115,7 @@ class Cut(Diffable):
116115
'comment',
117116
)
118117

119-
class Menu(object):
118+
class Menu:
120119
"""Simple menu container."""
121120

122121
def __init__(self, filename):
@@ -177,7 +176,7 @@ def dump_intermediate(self, outdir=None):
177176
if not outdir:
178177
outdir = os.getcwd()
179178
filename = "{0}.txt".format(os.path.basename(self.filename))
180-
with open(os.path.join(outdir, filename), 'wb') as fp:
179+
with open(os.path.join(outdir, filename), 'w') as fp:
181180
for line in self.to_diff():
182181
fp.write(line)
183182
fp.write(os.linesep)
@@ -186,9 +185,6 @@ def report_diff(fromfile, tofile, verbose=False, ostream=sys.stdout):
186185
"""Perform simple diff on two menus in TWiki format for reports.
187186
>>> report_diff(fromfile, tofile)
188187
"""
189-
fromlist = fromfile.to_diff()
190-
tolist = tofile.to_diff()
191-
192188
from_algorithms = {}
193189
to_algorithms = {}
194190

@@ -209,7 +205,7 @@ def added_algorithms(a, b):
209205
removed = added_algorithms(fromfile, tofile)
210206
updated = []
211207

212-
for name, fromalgorithm in from_algorithms.iteritems():
208+
for name, fromalgorithm in from_algorithms.items():
213209
if name in to_algorithms:
214210
toalgorithm = to_algorithms[name]
215211
differences = []
@@ -256,38 +252,51 @@ def unified_diff(fromfile, tofile, verbose=False, ostream=sys.stdout):
256252
fromlines = fromfile.to_diff()
257253
tolines = tofile.to_diff()
258254

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
259280
for line in difflib.unified_diff(fromlines, tolines, fromfile=fromfile.filename, tofile=tofile.filename, lineterm=""):
281+
if count:
282+
ostream.write(os.linesep)
260283
# Print added lines
261284
if line.startswith('+'):
262-
if ostream.isatty():
263-
ostream.write(TTY.green)
264-
ostream.write(os.linesep)
265-
ostream.write(line)
266-
if ostream.isatty():
267-
ostream.write(TTY.clear)
285+
write_added(line)
268286
# Print removed lines
269287
elif line.startswith('-'):
270-
if ostream.isatty():
271-
ostream.write(TTY.red)
272-
ostream.write(os.linesep)
273-
ostream.write(line)
274-
if ostream.isatty():
275-
ostream.write(TTY.clear)
288+
write_removed(line)
276289
# Print diff markers
277290
elif line.startswith('@@'):
278-
if ostream.isatty():
279-
ostream.write(TTY.yellow)
280-
ostream.write(os.linesep)
281-
ostream.write(line)
282-
if ostream.isatty():
283-
ostream.write(TTY.clear)
291+
write_marker(line)
284292
# Print matching lines
285293
else:
286-
ostream.write(os.linesep)
287-
if ostream.isatty():
288-
ostream.write(TTY.clear)
289-
ostream.write(line)
290-
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)
291300

292301
def context_diff(fromfile, tofile, verbose=False, ostream=sys.stdout):
293302
"""Perform context diff on two menus.
@@ -296,46 +305,61 @@ def context_diff(fromfile, tofile, verbose=False, ostream=sys.stdout):
296305
fromlines = fromfile.to_diff()
297306
tolines = tofile.to_diff()
298307

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
299340
for line in difflib.context_diff(fromlines, tolines, fromfile=fromfile.filename, tofile=tofile.filename, lineterm=""):
341+
if count:
342+
ostream.write(os.linesep)
300343
# Print added lines
301344
if line.startswith('+ '):
302-
if ostream.isatty():
303-
ostream.write(TTY.green)
304-
ostream.write(os.linesep)
305-
ostream.write(line)
306-
if ostream.isatty():
307-
ostream.write(TTY.clear)
345+
write_added(line)
308346
# Print removed lines
309347
elif line.startswith('- '):
310-
if ostream.isatty():
311-
ostream.write(TTY.red)
312-
ostream.write(os.linesep)
313-
ostream.write(line)
314-
if ostream.isatty():
315-
ostream.write(TTY.clear)
348+
write_removed(line)
316349
# Print changed lines
317350
elif line.startswith('! '):
318-
if ostream.isatty():
319-
ostream.write(TTY.magenta)
320-
ostream.write(os.linesep)
321-
ostream.write(line)
322-
if ostream.isatty():
323-
ostream.write(TTY.clear)
351+
write_changed(line)
324352
# Print diff markers
325353
elif line.startswith('---') or line.startswith('***'):
326-
if ostream.isatty():
327-
ostream.write(TTY.yellow)
328-
ostream.write(os.linesep)
329-
ostream.write(line)
330-
if ostream.isatty():
331-
ostream.write(TTY.clear)
354+
write_marker(line)
332355
# Print matching lines
333356
else:
334-
ostream.write(os.linesep)
335-
if ostream.isatty():
336-
ostream.write(TTY.clear)
337-
ostream.write(line)
338-
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)
339363

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

0 commit comments

Comments
 (0)