PK֎"KPQ[[fcomments/__init__.py"""Script for commenting in/out lines in file.""" import logging from optparse import OptionParser from .commenter import Commenter __version__ = '0.1.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ǎ"Kfcomments/dist/tmp7uu5hv2k.whlPK!Hq)(.*fcomments-0.1.1.dist-info/entry_points.txtN+I/N.,()JKM+)VUr3PK!H}0RRfcomments-0.1.1.dist-info/WHEEL1 0 RZtMDtPI{w<wUnbaKM*A1ѭ g\Ic c~PK!H6BD "fcomments-0.1.1.dist-info/METADATAV]o6}篸@8,yI lYaFc; C$LYl$R#=gkdII#`=~z ]RCߘ\}29 e3a4~{dV9Uf⅁D*lXYȸ`7W+N)t4IEI )h}>y?>!^j.qiR"#-Y>Qrʳ,ǭa?ֲ1)m,b:8[#hx@fTkp vJ<8bU{Cϝuveck06)_(8V s,e4!MU"uN}J氖o*^…64˨"Akk@ʛb]pxSw݅RYl\_ETD%I% ,XJ8v- 82q#9Sj'@!h+tز-s_0P,%MؖQHC6z%vBHȬ`1Oֶ+[bAwcHhbڰ`8!qpn5FI31&o8&Hn0=vTZ,+_n5u8n›^–]_ț|y] ' xG_ Xy7ʴjg6pl qVj~v@<_k[PlqePI U {knqˋt.zEPkdǹ6=@l| w0܍Vja]oU3N.$6>+R:IV^ǚRDBjnpCp:R@3A w(U2sQ *ࢼ$\b=]x}8{i񳼻DScЧoǾzKF;:p'9V>\'nj)W K_~fWki]ۜu^(P#qA?X"%YUY6٣9dfm_rXn +rO-Ɖ x>?0?*sך@W^`_uCn |ގwr•=b}!ni>;7#6^NF=Nm@̆od~=c?T L%פj^P.PK!Hl^r fcomments-0.1.1.dist-info/RECORD}r@}Q.@lT H"hn Wǩgcͩ:v8Sb\xR!(+KfK ǚWB@H'P:^&O3{jUn y;U,[>3Bo>X?KZ?`<8VefE L[p`'k@U}M;mivLz tg%uޫ 86=K*NeGgrD_+Xvގۧm3i/߲}Cyk4( @z$u< u#쩑7Y¾ܓ za;'Bcnor@IeQة)cΎىBM ҳF|dn9/PK֎"KPQ[[fcomments/__init__.pyPKΈ"KLCCfcomments/__main__.pyPKC"KUUfcomments/commenter.pyPKǎ"KQfcomments/dist/tmp7uu5hv2k.whlPK!Hq)(.*fcomments-0.1.1.dist-info/entry_points.txtPK!H}0RRfcomments-0.1.1.dist-info/WHEELPK!H6BD "fcomments-0.1.1.dist-info/METADATAPK!Hl^r fcomments-0.1.1.dist-info/RECORDPKY