PKN"KjYYfcomments/__init__.py"""Script for commenting in/out lines in file.""" import logging from optparse import OptionParser from .commenter import Commenter __version__ = '0.1' log = logging.getLogger(__name__) def main(reverse=False): """Script for commenting in/out lines in file.""" def line_callback(option, opt, value, parser): if '-' in value: lin = [int(i) for i in value.split('-')] assert(len(lin) == 2) lin[1] += 1 lines = [i for i in range(*lin)] else: lines = [int(n) for n in value.split(',')] if value else None parser.values.lines = lines usage = """usage: %prog [options] Examples: %prog -h ---> see help %prog -cl1,2 path/to/file ---> comment out lines 1 and 2 %prog -ul3-6 path/to/file ---> uncomment lines 3 to 6 (inclusive) %prog -ac path/to/file ---> comment out all file %prog --start-pattern='\s+operations\s?=\s?\[' --end-pattern='\s+\]' path/to/file ---> comment out everything inside the `operations` list: 1| class Migration(...): 2| 3| operations = [ 4| # migrations( 5| # ... 6| # ), 7| ] """ description = """ Comment or uncomment lines in a file. Default behavior: do the oposite i.e. if a line is commented - uncomment it, and vice versa. To make sure that the matched lines will be [un]commented out - run with -[u]c option. If you don't specify an --output, the original file () will be overwritten. """ parser = OptionParser(usage=usage, description=description) parser.add_option("-c", "--comment", action="store_true", dest="comment", default=False, help="comment lines [default: False]") parser.add_option("-u", "--uncomment", action="store_true", dest="uncomment", default=False, help="uncomment lines [default: False]") parser.add_option("-a", "--all", action="store_true", dest="all_lines", default=False, help="apply to all lines in file; suppresses -l option [default: False]") parser.add_option("-l", "--lines", action="callback", type="string", dest="lines", default=None, callback=line_callback, help="comma separeted string of line numbers [default: None]") parser.add_option("-s", "--start-pattern", action="store", type="string", dest="in_pattern", default=None, help="pattern to match against the line before commented section [default: None]") parser.add_option("-e", "--end-pattern", action="store", type="string", dest="out_pattern", default=None, help="pattern to match against the first line after commented section [default: None]") parser.add_option("-o", "--output", action="store", type="string", dest="output", default=None, help="specify a path to output file [default: None]") parser.add_option("--symbol", action="store", type="string", dest="comment_symbol", default='#', help="specify a string to use as comment [default: '#']") options, args = parser.parse_args() if len(args) != 1: parser.error("specify a path to file") if options.comment and options.uncomment: parser.error('cannot run with both -c and -u set to True') if not (options.in_pattern or options.out_pattern or options.lines or options.all_lines): parser.error('specify patterns or lines; see --help') if not (options.comment or options.uncomment): reverse = True try: commenter = Commenter(path=args[0], reverse=reverse, **options.__dict__) commenter.comment_file() except Exception as e: parser.error(e) PKΈ"KLCCfcomments/__main__.pyfrom __future__ import absolute_import from . import main main() PKC"KUUfcomments/commenter.pyimport re class Commenter: """Comment or uncomment lines in file.""" def __init__(self, **kwargs): for key in kwargs: setattr(self, key, kwargs[key]) with open(self.path, "r") as f: self.original = [l for l in f] self.result = '' if self.all_lines: self.lines = [i for i in range(len(self.original) + 1)] def comment_file(self): self.line_based() if self.lines else self.pattern_based() self.save_file() def handle_line(self, line, comment): is_comment = re.match('\s*' + self.comment_symbol, line) is not None if self.reverse: comment = False if is_comment else True elif not (comment or is_comment): return line if comment: if is_comment: print('Warning: line already commented') return line else: return self.comment_symbol + line else: assert is_comment return line[1:] def line_based(self): res_file = [] assert self.lines for i, line in enumerate(self.original): if i + 1 in self.lines: line = self.handle_line(line, self.comment) res_file.append(line) self.result = ''.join(res_file) def pattern_based(self): start_pattern = False end_pattern = False res_file = [] for line in self.original: if start_pattern and re.match(self.out_pattern, line): end_pattern = True start_pattern = False if start_pattern: line = self.handle_line(line, self.comment) res_file.append(line) if end_pattern: continue if re.match(self.in_pattern, line): start_pattern = True self.result = ''.join(res_file) def save_file(self): path = self.output if self.output else self.path with open(path, "w") as f: f.write(self.result) PK!Hq)(.(fcomments-0.1.dist-info/entry_points.txtN+I/N.,()JKM+)VUr3PK!H}0RRfcomments-0.1.dist-info/WHEEL1 0 RZtMDtPI{w<wUnbaKM*A1ѭ g\Ic c~PK!HXb2 fcomments-0.1.dist-info/METADATAVn6}W NPK6 u4MQc} N1viidH5~־u(Z٢ 33i0TKB?8&,H9 ۠ mWO}@#BYq4zFlEԶ[=hLprNJQ9S0# 2Wć|m29NĠs*oO.:HR]vZ1x)*Ew-y`RtwmB V)ApN,*=›c^C룶]-?kݶ譋`PϲMS9F}23”s;xbY% A xBF%5nUԲ@kPBtl`LSX66>Rܤ/e-&%,:BF

, CvVW=w!(O0Ԟݴ K#i D@g;q0OUe^Ypzt`ߘqg{{mxŰC)t&e?׉j- A)f 7PKN"KjYYfcomments/__init__.pyPKΈ"KLCCfcomments/__main__.pyPKC"KUUfcomments/commenter.pyPK!Hq)(.(Ofcomments-0.1.dist-info/entry_points.txtPK!H}0RRfcomments-0.1.dist-info/WHEELPK!HXb2 Jfcomments-0.1.dist-info/METADATAPK!H4[fcomments-0.1.dist-info/RECORDPK0