PKN edarken/__init__.py""" Apply black only on mdified files """ __version__ = '0.0.3' import black import argparse import sys from itertools import chain def parse_range(rng): parts = rng.split('-') if 1 > len(parts) > 2: raise ValueError("Bad range: '%s'" % (rng,)) parts = [int(i) for i in parts] start = parts[0] end = start if len(parts) == 1 else parts[1] if start > end: end, start = start, end return range(start, end + 1) def parse_range_list(rngs): return sorted(set(chain(*[parse_range(rng) for rng in rngs.split(',')]))) def insert_marks(lines, ranges): activated = False yield '# bm12' yield "# fmt: off" for lnum, line in enumerate(lines, start=1): if lnum in ranges and not activated: activated = True yield '# bm12' yield "# fmt: on" elif lnum not in ranges and activated: activated = False yield '# bm12' yield "# fmt: off" yield line def filtermark(gen): try: for l in gen: if l.strip().endswith('# bm12'): next(gen) continue yield l except StopIteration: return def darken_file(filename, ranges, dry_run): with open(filename, 'r') as f: s = f.read() s = '\n'.join(insert_marks(s.splitlines(), ranges)) res = s res = black.format_str(s, mode=black.FileMode()) lines = res.splitlines() re_lines = list(filtermark(iter(lines))) if not dry_run: with open(filename, 'w') as f: f.write('\n'.join(re_lines)) else: for i,l in enumerate(re_lines, start=1): print(f"{i:02}", l) def main(argv=None): if argv is None: argv = sys.argv[1:] parser = argparse.ArgumentParser(prog='darken', description='Apply black only to some specific lines of a file.') parser.add_argument('--ranges', help='which lines to apply darken to') parser.add_argument('--since', help='comitish') parser.add_argument('--dry-run', help='', action='store_true') parser.add_argument('filename', nargs='?', help='filename to process') parsed = parser.parse_args(argv) print(parsed) res = parsed.ranges if res: ranges = parse_range_list(res) if parsed.since: from collections import defaultdict dct = defaultdict(lambda:[]) print('trying to extract changes since', parsed.since) import subprocess sub = subprocess.run(['git','diff', '-U0', parsed.since], stdout=subprocess.PIPE) current_file = None for line in sub.stdout.decode().splitlines(): if line.startswith('+++'): current_file = line[5:] continue if line.startswith('@@'): res,add = line.split('@@')[1][1:-1].split(' ') from_,lenght,*_ = add.split(',')+['1'] print(current_file, int(from_), int(from_)+int(lenght) ) dct[current_file].append((int(from_), int(from_)+int(lenght))) else: continue for k,v in dct.items(): if k == 'dev/null': continue ranges = set(chain(*[range(start,stop+1) for start,stop in v])) print(k,ranges) try: darken_file(k[1:], ranges, parsed.dry_run) except: pass sys.exit() filename = parsed.filename darken_file(filename, ranges, parsed.dry_run) if __name__ == '__main__': main() PK!H$#&'darken-0.0.3.dist-info/entry_points.txtN+I/N.,()JI,NͳPVy\\PK,nN>>darken-0.0.3.dist-info/LICENSEThe MIT License (MIT) Copyright (c) 2019 Matthias Bussonnier Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HMuSadarken-0.0.3.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,szd&Y)r$[)T&UD"PK!H8Ddarken-0.0.3.dist-info/METADATAmT[8)IKfeęLn~MK-D $5{=%'jN!ꪢ8ZG>gKz?Szjwli,?_i?@^W;rPpM9nl-PEkb7m ťT|6ۀ??]|V)vΗtcn Yk2Kڞ6cVvzϡf!t60Qo,u-\,v%zw~ZV_{c9J}\PIS"r#w:*lTR,[1]@,&toU`D螒ЩL es 2s3myev;5! M Ҷt SӹR$ V^Gs@ﴯ1B5naQ?bY(0U"j\?L|fkY@x^C[GGϔM`W`y8Ƨ:!|>^_ғS g?ˊ@δVz}™Z2I{z<?;k&z =LÄ"% 0Ɯl'&tEML)9U'l&QB0ﴍǔO#BMI%#sXAb+D7RB"yɖOź#(Mf P"Ph6 }oBC;<$$͆SV9ĶA,F0 [ >Y jWu09VIX0q2EYŷt<{hٝb. ū3PK!H-darken-0.0.3.dist-info/RECORDuI@@ѽg,A@+(s!X2W%xYڿz$e]$aډ_G g6C78xOўD eEaS4`1Y96I?%mSd`Y|k}egюy̑yf]keTSv"gl"Uxgm6_qO,78#dR; PUkA]Mos/XTԎ!yb|l9DȾޒ>darken-0.0.3.dist-info/LICENSEPK!HMuSadarken-0.0.3.dist-info/WHEELPK!H8Ddarken-0.0.3.dist-info/METADATAPK!H-#darken-0.0.3.dist-info/RECORDPK