PK#Koqfcomments/.DS_StoreBud1 cache_ __pycache__lg1Scomp __pycache__moDDdutc __pycache__modDdutc __pycache__ph1ScompPdistlg1ScompdistmoDDdutcЄdistmodDdutcЄdistph1Scomp  @ @ @ @ E DSDB ` @ @ @PK#Kn_ssfcomments/__init__.py"""Script for commenting in/out lines in file.""" import logging from optparse import OptionParser from .commenter import Commenter __version__ = '0.1.3' log = logging.getLogger(__name__) def create_parser(): 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 lines %prog -au path/to/file ---> uncomment all lines %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: '#']") return parser def main(): """Script for commenting in/out lines in file.""" parser = create_parser() 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') try: commenter = Commenter(path=args[0]) commenter.comment_file(**options.__dict__) except Exception as e: parser.error(e) PK#KLCCfcomments/__main__.pyfrom __future__ import absolute_import from . import main main() PK#K N//fcomments/commenter.pyimport re class Commenter: """Comment or uncomment lines in file.""" def __init__(self, **kwargs): self.result = '' self.path = None self.output = None self.all_lines = False self.uncomment = False self.comment = False self.reverse = False self.in_pattern = None self.out_pattern = None self.lines = None self.comment_symbol = '#' for key in kwargs: setattr(self, key, kwargs[key]) with open(self.path, "r") as f: self.original = [l for l in f] self._update_reverse() if self.all_lines: self.lines = [i for i in range(len(self.original) + 1)] def comment_file(self, commit=True, **kwargs): all_lines = kwargs.get('all_lines', None) original = kwargs.get('original', self.original) if all_lines: lines = [i for i in range(len(original) + 1)] kwargs.update({'lines': lines}) # self._check(**kwargs) if kwargs.get('lines', None): self.line_based(**kwargs) else: # raise NotImplementedError('pattern based: {}'.format(kwargs)) self.pattern_based(**kwargs) if commit: self.save_file() def _update_reverse(self): if not (self.comment or self.uncomment): self.reverse = True # @staticmethod # def _check(self, **kwargs): # if self.comment and self.uncomment: # raise RuntimeError('both -cu') # if not (self.in_pattern or # self.out_pattern or # self.lines or # self.all_lines): # print('no pattern specified') # raise RuntimeError('no pattern specified') @staticmethod def _get_updates(): return {'output': None, 'comment_symbol': '#', 'lines': None, 'comment': False, 'all_lines': False, 'uncomment': False, 'in_pattern': None, 'out_pattern': None, 'reverse': True} def handle_line(self, line, comment, reverse=True): is_comment = re.match('\s*' + self.comment_symbol, line) is not None if 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, lines=None, original=None, comment=False, reverse=True, update=False, **kwargs): res_file = [] if not original: original = self.original assert lines for i, line in enumerate(original): if i + 1 in lines: line = self.handle_line(line, comment, reverse) res_file.append(line) # print('line based:', ''.join(res_file)) self.result = ''.join(res_file) if update: pass def pattern_based(self, in_pattern=None, out_pattern=None, original=None, comment=False, reverse=True, update=False, **kwargs): start_pattern = False end_pattern = False res_file = [] if not original: original = self.original for line in original: if start_pattern and re.match(out_pattern, line): end_pattern = True start_pattern = False if start_pattern: line = self.handle_line(line, comment, reverse) res_file.append(line) if end_pattern: continue if re.match(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) with open(self.path, "r") as f: self.original = [l for l in f] self.result = '' PKǎ"Kfcomments/dist/tmp7uu5hv2k.whlPK!Hq)(.*fcomments-0.1.3.dist-info/entry_points.txtN+I/N.,()JKM+)VUr3PK!H}0RRfcomments-0.1.3.dist-info/WHEEL1 0 RZtMDtPI{w<wUnbaKM*A1ѭ g\Ic c~PK!H@PT"fcomments-0.1.3.dist-info/METADATAX_o68$mhu,6Ά!Z-6S#)Y'i ɻ9eNL*.v]ҧ `$aVdA$T.gBBA#r 1O Ly|H T̸򱏬Ԃ : YG|`;!'*(Ls E/[^ly,<`jL *9h:Iq?flЃ,b&f~ ^Y:A|YZTa ڡxhbd %!SJlڜү _hs͙"|qރs{);ģJ"sIqT-!/W+E FKBx4cxAʳLHd. oU \e/䬃B]Xf4"$A3(kOrBlJXØEt1T Pw\sӐ0fD5)PH(0wPl-_1;c%Ɲ`J1,t3AB>]Bt9ӬZrW1:L.QP8¼0'*ԒbC4\yHֲB-8.jG CU>SxU:^x]|~޿v!.,2n|w.BȬnPZ/ ;:3:j|V2 CoY y )x Iʐ)ZUB"GV,"ҝL(,&(+=0-Nu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