1 | | to be filled in... |
| 1 | This makes it so that emacs knows the intended character encoding, BOM, |
| 2 | end-of-line markers, and standard line-width of these files. |
| 3 | |
| 4 | Also this is a form of documentation. It means that you should put only |
| 5 | utf-8-encoded things into text files, only utf-8-encoded things into source |
| 6 | code files (and actually you should write only put ASCII-encoded things except |
| 7 | possibly in comments or docstrings!), and that you should line-wrap everything |
| 8 | at 77 columns wide. |
| 9 | |
| 10 | It also specifies that text files should start with a "utf-8 BOM". (Brian |
| 11 | questions the point of this, and my answer is that it adds information and |
| 12 | doesn't hurt. Whether that information will ever be useful is an open |
| 13 | question.) |
| 14 | |
| 15 | It also specifies that text files should have unix-style ('\n') end-of-line |
| 16 | markers, not windows-style or old-macos-style. |
| 17 | |
| 18 | I generated this patch by writing and running the following script, and then |
| 19 | reading the resulting diff to make sure it was correct. I then undid the |
| 20 | changes that the script had done to the files inside the |
| 21 | "setuptools-0.6c16dev4.egg" directory before committing the patch. |
| 22 | |
| 23 | {{{ |
| 24 | import os |
| 25 | |
| 26 | magic_header_line_comment_prefix = { |
| 27 | '.py': u"# ", |
| 28 | '.rst': u".. ", |
| 29 | } |
| 30 | |
| 31 | def format(): |
| 32 | for dirpath, dirnames, filenames in os.walk('.'): |
| 33 | for filename in filenames: |
| 34 | ext = os.path.splitext(filename)[-1] |
| 35 | if ext in ('.py', '.rst'): |
| 36 | fname = os.path.join(dirpath, filename) |
| 37 | info = open(fname, 'rU') |
| 38 | formattedlines = [ line.decode('utf-8') for line in info ] |
| 39 | info.close() |
| 40 | |
| 41 | if len(formattedlines) == 0: |
| 42 | return |
| 43 | |
| 44 | outfo = open(fname, 'w') |
| 45 | outfo.write(u"\ufeff".encode('utf-8')) |
| 46 | |
| 47 | commentsign = magic_header_line_comment_prefix[ext] |
| 48 | |
| 49 | firstline = formattedlines.pop(0) |
| 50 | while firstline.startswith(u"\ufeff"): |
| 51 | firstline = firstline[len(u"\ufeff"):] |
| 52 | if firstline.startswith(u"#!"): |
| 53 | outfo.write(firstline.encode('utf-8')) |
| 54 | outfo.write(commentsign.encode('utf-8')) |
| 55 | outfo.write("-*- coding: utf-8-with-signature-unix; fill-column: 77 -*-\n".encode('utf-8')) |
| 56 | else: |
| 57 | outfo.write(commentsign.encode('utf-8')) |
| 58 | outfo.write("-*- coding: utf-8-with-signature-unix; fill-column: 77 -*-\n".encode('utf-8')) |
| 59 | if (commentsign in firstline) and ("-*-" in firstline) and ("coding:" in firstline): |
| 60 | print "warning there was already a coding line %r in %r" % (firstline, fname) |
| 61 | else: |
| 62 | outfo.write(firstline.encode('utf-8')) |
| 63 | |
| 64 | for l in formattedlines: |
| 65 | if (commentsign in l) and ("-*-" in l) and ("coding:" in l): |
| 66 | print "warning there was already a coding line %r in %r" % (l, fname) |
| 67 | else: |
| 68 | outfo.write(l.encode('utf-8')) |
| 69 | outfo.close() |
| 70 | |
| 71 | if __name__ == '__main__': |
| 72 | format() |
| 73 | }}} |