Attachment 'un_commentLines.py'

Download

   1 #!/usr/bin/python
   2 # 
   3 # v2.0 fixed error with perl scripts; added applescript support
   4 # v2.1 fixed error if no selection; now just inserts a comment marker in the text
   5 # v2.2 Added commments and started added html support
   6 #
   7 # -- PB User Script Info --
   8 # %%%{PBXName=Un/Commenter Python}%%%
   9 # %%%{PBXInput=AllText}%%%
  10 # %%%{PBXOutput=ReplaceSelection}%%%
  11 # %%%{PBXKeyEquivalent=@/}%%%
  12 
  13 import os, re, sys
  14 
  15 def main():
  16     start = %%%{PBXSelectionStart}%%%
  17     finish = %%%{PBXSelectionEnd}%%%
  18 #    sys.stderr.write("start: %s\nfinish: %s\n" % (start, finish))
  19     # stdin has AllText
  20     # need to get this even if no selection otherwise XCode quits!
  21     inputLines = sys.stdin.readlines()
  22     firstLine = inputLines[0]
  23     text = "".join(inputLines)
  24     selection = text[start:finish]
  25    
  26     # Commment styles
  27     # Applescript comment
  28     aComment = r'--'
  29     # Python and Perl comment
  30     pComment = r'#'
  31     # C comment
  32     cComment = r'//'
  33     # HTML comment: not implemented
  34     hOpenCommment = r'<!--'
  35     hCloseCommment = r'-->'
  36     commentString = cComment
  37     # test using filename suffix
  38     filename = r"%%%{PBXFilePath}%%%"
  39     fileNameRegex = re.compile(r".*(?P<suffix>py|pl|sh|applescript)$", re.VERBOSE | re.IGNORECASE)
  40     match = fileNameRegex.search(filename)
  41     if match:
  42         # will match shell to default
  43         suffix = match.group('suffix')
  44         if suffix == "py" or suffix == "pl":
  45             commentString = pComment
  46         elif suffix == "applescript":
  47             commentString = aComment
  48     else:
  49         # determine the type of file we have by looking for the #! line at the top
  50         # careful--it might already be commented out!
  51         # test first line
  52         allTextRegex = re.compile(r"""
  53          ^([#]|//)*                 # it may be commented out
  54          \s*.*                      # whitespace
  55          (?P<script>python|perl|sh) # look for shell
  56          .*                         # following characters
  57          """, re.VERBOSE) # | re.IGNORECASE)
  58         match = allTextRegex.search(firstLine)
  59         if match:
  60             script = match.group('script')
  61             if script == "python" or script == "perl":
  62                 commentString = pComment
  63 
  64     lines = selection.splitlines()
  65     if lines:
  66         # add or remove comment markers depending on the state of the first line of the selection
  67         # if it is uncommented, comment all lines.  If it is commented, remove comment markers, if present
  68         commentRegex = re.compile(r"^[" + commentString + r"]+(?P<text>.*)$")
  69         commentStringPresent = commentRegex.match(lines[0])
  70         
  71         for i in range(len(lines)):
  72             line = lines[i]
  73             if commentStringPresent:
  74                 # remove comment markers
  75                 m = commentRegex.match(line)
  76                 if m:
  77                     if commentStringPresent:
  78                         lines[i] = m.group('text')
  79                     else:
  80                         lines[i] = commentString + line
  81             else:
  82                 # prefix with comment
  83                 lines[i] = commentString + line
  84         replacement = os.linesep.join(lines)
  85     else:
  86         replacement = commentString
  87     output = "%s%s%s" % (r"%%%{PBXSelection}%%%", replacement, r"%%%{PBXSelection}%%%")
  88     if selection and (selection[-1] == os.linesep):
  89         output = output + os.linesep
  90     sys.stdout.write(output)
  91 
  92 # main area
  93 if __name__ == "__main__":
  94     main()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2007-06-21 00:31:39, 0.0 KB) [[attachment:50-un_commentLines.py]]
  • [get | view] (2007-05-18 16:50:49, 0.0 KB) [[attachment:StartupScript]]
  • [get | view] (2009-08-17 10:37:11, 0.0 KB) [[attachment:attachment_StartupScript]]
  • [get | view] (2008-03-11 04:09:03, 3.4 KB) [[attachment:un_commentLines.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

Unable to edit the page? See the FrontPage for instructions.