Changes between Initial Version and Version 2 of Ticket #2138


Ignore:
Timestamp:
2013-12-17T22:58:30Z (11 years ago)
Author:
zooko
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #2138 – Description

    initial v2  
    1 to be filled in...
     1This makes it so that emacs knows the intended character encoding, BOM,
     2end-of-line markers, and standard line-width of these files.
     3
     4Also this is a form of documentation. It means that you should put only
     5utf-8-encoded things into text files, only utf-8-encoded things into source
     6code files (and actually you should write only put ASCII-encoded things except
     7possibly in comments or docstrings!), and that you should line-wrap everything
     8at 77 columns wide.
     9
     10It also specifies that text files should start with a "utf-8 BOM". (Brian
     11questions the point of this, and my answer is that it adds information and
     12doesn't hurt. Whether that information will ever be useful is an open
     13question.)
     14
     15It also specifies that text files should have unix-style ('\n') end-of-line
     16markers, not windows-style or old-macos-style.
     17
     18I generated this patch by writing and running the following script, and then
     19reading the resulting diff to make sure it was correct. I then undid the
     20changes that the script had done to the files inside the
     21"setuptools-0.6c16dev4.egg" directory before committing the patch.
     22
     23{{{
     24import os
     25
     26magic_header_line_comment_prefix = {
     27    '.py': u"# ",
     28    '.rst': u".. ",
     29    }
     30
     31def 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
     71if __name__ == '__main__':
     72    format()
     73}}}