Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 2 | |
| 3 | # Released to the public domain, by Tim Peters, 03 October 2000. |
| 4 | |
Skip Montanaro | 9a29e7a | 2002-03-26 11:39:26 +0000 | [diff] [blame] | 5 | """reindent [-d][-r][-v] [ path ... ] |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 6 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 7 | -d (--dryrun) Dry run. Analyze, but don't make any changes to, files. |
| 8 | -r (--recurse) Recurse. Search for all .py files in subdirectories too. |
| 9 | -n (--nobackup) No backup. Does not make a ".bak" file before reindenting. |
| 10 | -v (--verbose) Verbose. Print informative msgs; else no output. |
Jason R. Coombs | 76eec3d | 2011-07-26 11:38:04 -0400 | [diff] [blame] | 11 | (--newline) Newline. Specify the newline character to use (CRLF, LF). |
| 12 | Default is the same as the original file. |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 13 | -h (--help) Help. Print this usage information and exit. |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 14 | |
| 15 | Change Python (.py) files to use 4-space indents and no hard tab characters. |
Tim Peters | ba001a0 | 2001-10-04 19:44:10 +0000 | [diff] [blame] | 16 | Also trim excess spaces and tabs from ends of lines, and remove empty lines |
| 17 | at the end of files. Also ensure the last line ends with a newline. |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 18 | |
Skip Montanaro | 9a29e7a | 2002-03-26 11:39:26 +0000 | [diff] [blame] | 19 | If no paths are given on the command line, reindent operates as a filter, |
| 20 | reading a single source file from standard input and writing the transformed |
| 21 | source to standard output. In this case, the -d, -r and -v flags are |
| 22 | ignored. |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 23 | |
Skip Montanaro | 9a29e7a | 2002-03-26 11:39:26 +0000 | [diff] [blame] | 24 | You can pass one or more file and/or directory paths. When a directory |
| 25 | path, all .py files within the directory will be examined, and, if the -r |
| 26 | option is given, likewise recursively for subdirectories. |
| 27 | |
| 28 | If output is not to standard output, reindent overwrites files in place, |
| 29 | renaming the originals with a .bak extension. If it finds nothing to |
| 30 | change, the file is left alone. If reindent does change a file, the changed |
| 31 | file is a fixed-point for future runs (i.e., running reindent on the |
| 32 | resulting .py file won't change it again). |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 33 | |
| 34 | The hard part of reindenting is figuring out what to do with comment |
| 35 | lines. So long as the input files get a clean bill of health from |
| 36 | tabnanny.py, reindent should do a good job. |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 37 | |
| 38 | The backup file is a copy of the one that is being reindented. The ".bak" |
| 39 | file is generated with shutil.copy(), but some corner cases regarding |
Jason R. Coombs | 76748b7 | 2011-07-26 11:18:40 -0400 | [diff] [blame] | 40 | user/group and permissions could leave the backup file more readable than |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 41 | you'd prefer. You can always use the --nobackup option to prevent this. |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 42 | """ |
| 43 | |
| 44 | __version__ = "1" |
| 45 | |
| 46 | import tokenize |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 47 | import os |
| 48 | import shutil |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 49 | import sys |
| 50 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 51 | verbose = False |
| 52 | recurse = False |
| 53 | dryrun = False |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 54 | makebackup = True |
Jason R. Coombs | 4789104 | 2011-07-29 09:31:56 -0400 | [diff] [blame] | 55 | "A specified newline to be used in the output (set by --newline option)" |
| 56 | spec_newline = None |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 57 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 58 | |
Skip Montanaro | 165163f | 2004-03-27 18:43:56 +0000 | [diff] [blame] | 59 | def usage(msg=None): |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 60 | if msg is None: |
| 61 | msg = __doc__ |
| 62 | print(msg, file=sys.stderr) |
| 63 | |
Skip Montanaro | 165163f | 2004-03-27 18:43:56 +0000 | [diff] [blame] | 64 | |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 65 | def errprint(*args): |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 66 | sys.stderr.write(" ".join(str(arg) for arg in args)) |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 67 | sys.stderr.write("\n") |
| 68 | |
| 69 | def main(): |
| 70 | import getopt |
Jason R. Coombs | 76eec3d | 2011-07-26 11:38:04 -0400 | [diff] [blame] | 71 | global verbose, recurse, dryrun, makebackup, spec_newline |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 72 | try: |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 73 | opts, args = getopt.getopt(sys.argv[1:], "drnvh", |
Jason R. Coombs | 76eec3d | 2011-07-26 11:38:04 -0400 | [diff] [blame] | 74 | ["dryrun", "recurse", "nobackup", "verbose", "newline=", "help"]) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 75 | except getopt.error as msg: |
Skip Montanaro | 165163f | 2004-03-27 18:43:56 +0000 | [diff] [blame] | 76 | usage(msg) |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 77 | return |
| 78 | for o, a in opts: |
Skip Montanaro | 165163f | 2004-03-27 18:43:56 +0000 | [diff] [blame] | 79 | if o in ('-d', '--dryrun'): |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 80 | dryrun = True |
Skip Montanaro | 165163f | 2004-03-27 18:43:56 +0000 | [diff] [blame] | 81 | elif o in ('-r', '--recurse'): |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 82 | recurse = True |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 83 | elif o in ('-n', '--nobackup'): |
| 84 | makebackup = False |
Skip Montanaro | 165163f | 2004-03-27 18:43:56 +0000 | [diff] [blame] | 85 | elif o in ('-v', '--verbose'): |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 86 | verbose = True |
Jason R. Coombs | 76eec3d | 2011-07-26 11:38:04 -0400 | [diff] [blame] | 87 | elif o in ('--newline',): |
| 88 | if not a.upper() in ('CRLF', 'LF'): |
| 89 | usage() |
| 90 | return |
| 91 | spec_newline = dict(CRLF='\r\n', LF='\n')[a.upper()] |
Skip Montanaro | 165163f | 2004-03-27 18:43:56 +0000 | [diff] [blame] | 92 | elif o in ('-h', '--help'): |
| 93 | usage() |
| 94 | return |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 95 | if not args: |
Skip Montanaro | 9a29e7a | 2002-03-26 11:39:26 +0000 | [diff] [blame] | 96 | r = Reindenter(sys.stdin) |
| 97 | r.run() |
| 98 | r.write(sys.stdout) |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 99 | return |
| 100 | for arg in args: |
| 101 | check(arg) |
| 102 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 103 | |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 104 | def check(file): |
| 105 | if os.path.isdir(file) and not os.path.islink(file): |
| 106 | if verbose: |
Guido van Rossum | 6247fdb | 2007-04-27 19:48:23 +0000 | [diff] [blame] | 107 | print("listing directory", file) |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 108 | names = os.listdir(file) |
| 109 | for name in names: |
| 110 | fullname = os.path.join(file, name) |
| 111 | if ((recurse and os.path.isdir(fullname) and |
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 112 | not os.path.islink(fullname) and |
| 113 | not os.path.split(fullname)[1].startswith(".")) |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 114 | or name.lower().endswith(".py")): |
| 115 | check(fullname) |
| 116 | return |
| 117 | |
| 118 | if verbose: |
Guido van Rossum | 6247fdb | 2007-04-27 19:48:23 +0000 | [diff] [blame] | 119 | print("checking", file, "...", end=' ') |
Jason R. Coombs | 76748b7 | 2011-07-26 11:18:40 -0400 | [diff] [blame] | 120 | with open(file, 'rb') as f: |
Alexander Belopolsky | 4a98e3b | 2010-10-18 14:43:38 +0000 | [diff] [blame] | 121 | encoding, _ = tokenize.detect_encoding(f.readline) |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 122 | try: |
Alexander Belopolsky | 4a98e3b | 2010-10-18 14:43:38 +0000 | [diff] [blame] | 123 | with open(file, encoding=encoding) as f: |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 124 | r = Reindenter(f) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 125 | except IOError as msg: |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 126 | errprint("%s: I/O Error: %s" % (file, str(msg))) |
| 127 | return |
| 128 | |
Jason R. Coombs | 76eec3d | 2011-07-26 11:38:04 -0400 | [diff] [blame] | 129 | newline = spec_newline if spec_newline else r.newlines |
Jason R. Coombs | 76748b7 | 2011-07-26 11:18:40 -0400 | [diff] [blame] | 130 | if isinstance(newline, tuple): |
Jason R. Coombs | 76eec3d | 2011-07-26 11:38:04 -0400 | [diff] [blame] | 131 | errprint("%s: mixed newlines detected; cannot continue without --newline" % file) |
Jason R. Coombs | 76748b7 | 2011-07-26 11:18:40 -0400 | [diff] [blame] | 132 | return |
| 133 | |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 134 | if r.run(): |
| 135 | if verbose: |
Guido van Rossum | 6247fdb | 2007-04-27 19:48:23 +0000 | [diff] [blame] | 136 | print("changed.") |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 137 | if dryrun: |
Guido van Rossum | 6247fdb | 2007-04-27 19:48:23 +0000 | [diff] [blame] | 138 | print("But this is a dry run, so leaving it alone.") |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 139 | if not dryrun: |
| 140 | bak = file + ".bak" |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 141 | if makebackup: |
| 142 | shutil.copyfile(file, bak) |
| 143 | if verbose: |
| 144 | print("backed up", file, "to", bak) |
Jason R. Coombs | 76748b7 | 2011-07-26 11:18:40 -0400 | [diff] [blame] | 145 | with open(file, "w", encoding=encoding, newline=newline) as f: |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 146 | r.write(f) |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 147 | if verbose: |
Guido van Rossum | 6247fdb | 2007-04-27 19:48:23 +0000 | [diff] [blame] | 148 | print("wrote new", file) |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 149 | return True |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 150 | else: |
| 151 | if verbose: |
Guido van Rossum | 6247fdb | 2007-04-27 19:48:23 +0000 | [diff] [blame] | 152 | print("unchanged.") |
Christian Heimes | ada8c3b | 2008-03-18 18:26:33 +0000 | [diff] [blame] | 153 | return False |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 154 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 155 | |
Tim Peters | ba001a0 | 2001-10-04 19:44:10 +0000 | [diff] [blame] | 156 | def _rstrip(line, JUNK='\n \t'): |
| 157 | """Return line stripped of trailing spaces, tabs, newlines. |
| 158 | |
| 159 | Note that line.rstrip() instead also strips sundry control characters, |
| 160 | but at least one known Emacs user expects to keep junk like that, not |
| 161 | mentioning Barry by name or anything <wink>. |
| 162 | """ |
| 163 | |
| 164 | i = len(line) |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 165 | while i > 0 and line[i - 1] in JUNK: |
Tim Peters | ba001a0 | 2001-10-04 19:44:10 +0000 | [diff] [blame] | 166 | i -= 1 |
| 167 | return line[:i] |
| 168 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 169 | |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 170 | class Reindenter: |
| 171 | |
| 172 | def __init__(self, f): |
| 173 | self.find_stmt = 1 # next token begins a fresh stmt? |
| 174 | self.level = 0 # current indent level |
| 175 | |
| 176 | # Raw file lines. |
| 177 | self.raw = f.readlines() |
| 178 | |
| 179 | # File lines, rstripped & tab-expanded. Dummy at start is so |
| 180 | # that we can use tokenize's 1-based line numbering easily. |
| 181 | # Note that a line is all-blank iff it's "\n". |
Tim Peters | ba001a0 | 2001-10-04 19:44:10 +0000 | [diff] [blame] | 182 | self.lines = [_rstrip(line).expandtabs() + "\n" |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 183 | for line in self.raw] |
| 184 | self.lines.insert(0, None) |
| 185 | self.index = 1 # index into self.lines of next line |
| 186 | |
| 187 | # List of (lineno, indentlevel) pairs, one for each stmt and |
| 188 | # comment line. indentlevel is -1 for comment lines, as a |
| 189 | # signal that tokenize doesn't know what to do about them; |
| 190 | # indeed, they're our headache! |
| 191 | self.stats = [] |
| 192 | |
Jason R. Coombs | 76748b7 | 2011-07-26 11:18:40 -0400 | [diff] [blame] | 193 | # Save the newlines found in the file so they can be used to |
| 194 | # create output without mutating the newlines. |
| 195 | self.newlines = f.newlines |
| 196 | |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 197 | def run(self): |
Trent Nelson | 428de65 | 2008-03-18 22:41:35 +0000 | [diff] [blame] | 198 | tokens = tokenize.generate_tokens(self.getline) |
| 199 | for _token in tokens: |
| 200 | self.tokeneater(*_token) |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 201 | # Remove trailing empty lines. |
| 202 | lines = self.lines |
| 203 | while lines and lines[-1] == "\n": |
| 204 | lines.pop() |
| 205 | # Sentinel. |
| 206 | stats = self.stats |
| 207 | stats.append((len(lines), 0)) |
| 208 | # Map count of leading spaces to # we want. |
| 209 | have2want = {} |
| 210 | # Program after transformation. |
| 211 | after = self.after = [] |
Tim Peters | 54e5b89 | 2002-02-17 07:03:05 +0000 | [diff] [blame] | 212 | # Copy over initial empty lines -- there's nothing to do until |
| 213 | # we see a line with *something* on it. |
| 214 | i = stats[0][0] |
| 215 | after.extend(lines[1:i]) |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 216 | for i in range(len(stats) - 1): |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 217 | thisstmt, thislevel = stats[i] |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 218 | nextstmt = stats[i + 1][0] |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 219 | have = getlspace(lines[thisstmt]) |
| 220 | want = thislevel * 4 |
| 221 | if want < 0: |
| 222 | # A comment line. |
| 223 | if have: |
| 224 | # An indented comment line. If we saw the same |
| 225 | # indentation before, reuse what it most recently |
| 226 | # mapped to. |
| 227 | want = have2want.get(have, -1) |
| 228 | if want < 0: |
| 229 | # Then it probably belongs to the next real stmt. |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 230 | for j in range(i + 1, len(stats) - 1): |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 231 | jline, jlevel = stats[j] |
| 232 | if jlevel >= 0: |
| 233 | if have == getlspace(lines[jline]): |
| 234 | want = jlevel * 4 |
| 235 | break |
| 236 | if want < 0: # Maybe it's a hanging |
| 237 | # comment like this one, |
| 238 | # in which case we should shift it like its base |
| 239 | # line got shifted. |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 240 | for j in range(i - 1, -1, -1): |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 241 | jline, jlevel = stats[j] |
| 242 | if jlevel >= 0: |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 243 | want = have + (getlspace(after[jline - 1]) - |
| 244 | getlspace(lines[jline])) |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 245 | break |
| 246 | if want < 0: |
| 247 | # Still no luck -- leave it alone. |
| 248 | want = have |
| 249 | else: |
| 250 | want = 0 |
| 251 | assert want >= 0 |
| 252 | have2want[have] = want |
| 253 | diff = want - have |
| 254 | if diff == 0 or have == 0: |
| 255 | after.extend(lines[thisstmt:nextstmt]) |
| 256 | else: |
| 257 | for line in lines[thisstmt:nextstmt]: |
| 258 | if diff > 0: |
| 259 | if line == "\n": |
| 260 | after.append(line) |
| 261 | else: |
| 262 | after.append(" " * diff + line) |
| 263 | else: |
| 264 | remove = min(getlspace(line), -diff) |
| 265 | after.append(line[remove:]) |
| 266 | return self.raw != self.after |
| 267 | |
| 268 | def write(self, f): |
| 269 | f.writelines(self.after) |
| 270 | |
| 271 | # Line-getter for tokenize. |
| 272 | def getline(self): |
| 273 | if self.index >= len(self.lines): |
| 274 | line = "" |
| 275 | else: |
| 276 | line = self.lines[self.index] |
| 277 | self.index += 1 |
| 278 | return line |
| 279 | |
| 280 | # Line-eater for tokenize. |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 281 | def tokeneater(self, type, token, slinecol, end, line, |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 282 | INDENT=tokenize.INDENT, |
| 283 | DEDENT=tokenize.DEDENT, |
| 284 | NEWLINE=tokenize.NEWLINE, |
| 285 | COMMENT=tokenize.COMMENT, |
| 286 | NL=tokenize.NL): |
| 287 | |
| 288 | if type == NEWLINE: |
| 289 | # A program statement, or ENDMARKER, will eventually follow, |
| 290 | # after some (possibly empty) run of tokens of the form |
| 291 | # (NL | COMMENT)* (INDENT | DEDENT+)? |
| 292 | self.find_stmt = 1 |
| 293 | |
| 294 | elif type == INDENT: |
| 295 | self.find_stmt = 1 |
| 296 | self.level += 1 |
| 297 | |
| 298 | elif type == DEDENT: |
| 299 | self.find_stmt = 1 |
| 300 | self.level -= 1 |
| 301 | |
| 302 | elif type == COMMENT: |
| 303 | if self.find_stmt: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 304 | self.stats.append((slinecol[0], -1)) |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 305 | # but we're still looking for a new stmt, so leave |
| 306 | # find_stmt alone |
| 307 | |
| 308 | elif type == NL: |
| 309 | pass |
| 310 | |
| 311 | elif self.find_stmt: |
| 312 | # This is the first "real token" following a NEWLINE, so it |
| 313 | # must be the first token of the next program statement, or an |
| 314 | # ENDMARKER. |
| 315 | self.find_stmt = 0 |
| 316 | if line: # not endmarker |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 317 | self.stats.append((slinecol[0], self.level)) |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 318 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 319 | |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 320 | # Count number of leading blanks. |
| 321 | def getlspace(line): |
| 322 | i, n = 0, len(line) |
| 323 | while i < n and line[i] == " ": |
| 324 | i += 1 |
| 325 | return i |
| 326 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame] | 327 | |
Tim Peters | ad14720 | 2000-10-05 03:48:38 +0000 | [diff] [blame] | 328 | if __name__ == '__main__': |
| 329 | main() |