pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

pastebin private pastebin - collaborative debugging tool What's a private pastebin?

Posted by frymaster on Fri 20th Nov 15:06
download | new post

  1. #!/usr/bin/python
  2. #"useless" means "use less, not nano"
  3. #it does not mean "it has no use"
  4. #:P
  5. #This will turn on the appropriate colour highlighting for nano (based on the colourmap dict in the getCol() function
  6. #It will also use less instead of nano if you don't have write permissions on the file (as a subtle hint you should get your sudo on)
  7. import os,sys
  8. from os import path
  9.  
  10. def getCol(line):
  11.         if line[:2]=="#!":
  12.                 line = line[2:].strip()
  13.                 foo=line.rfind("/")
  14.                 if foo >= 0:
  15.                         line = line[foo+1:].strip()
  16.                 if line[:3]=="env":
  17.                         line = line[3:].strip()
  18.                 foo=line.find(" ")
  19.                 if foo > 0:
  20.                         line = line[:foo].strip()
  21.                 colourmap = {"bash":"sh","sh":"sh","python":"python","perl":"perl"}
  22.                 if colourmap.has_key(line):
  23.                         return colourmap[line]
  24.  
  25. useless = False
  26. lesslist = []
  27. overrideColours = False
  28. for arg in sys.argv[1:]:
  29.         if (os.access(arg,os.F_OK)):
  30.                 if (os.path.isdir(arg)):
  31.                         sys.stderr.write("Error: Can't edit a directory!\nYou twazzock\n")
  32.                         sys.exit(1)
  33.                 if (os.access(arg,os.W_OK) != True):
  34.                         useless = True
  35.                 lesslist.append(arg)
  36.         else:
  37.                 if arg[:9] == "--syntax=":
  38.                         overrideColours=True
  39. args = sys.argv
  40. args.pop(0)
  41. args.insert(0,"nano")
  42. if (useless):
  43.         lesslist.insert(0,"less")
  44.         os.execv("/usr/bin/less",lesslist)
  45. else:
  46.         if (len(lesslist) == 1 and path.split(lesslist[0])[1].find(".") == -1 and not overrideColours ):
  47.                 f = open(lesslist[0])
  48.                 first = f.readline()
  49.                 f.close()
  50.                 colval = getCol(first)
  51.                 if colval:
  52.                         args.append("--syntax=" + colval)
  53.                         os.execv("/usr/bin/nano",args)
  54.  
  55. os.execv("/usr/bin/nano",args)

Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Use syntax highlighting

To highlight particular lines, prefix each line with @@


Remember my settings