Browse Source

Strip encoding lines.

tags/v1.0^2
Ben Kurtovic 10 years ago
parent
commit
5a83720617
2 changed files with 23 additions and 2 deletions
  1. +1
    -1
      bitshift/crawler/crawl.py
  2. +22
    -1
      bitshift/parser/python.py

+ 1
- 1
bitshift/crawler/crawl.py View File

@@ -45,7 +45,7 @@ def _configure_logging():
logging.getLogger("urllib3").setLevel(logging.WARNING)

formatter = logging.Formatter(
fmt=("%(asctime)s %(levelname)s %(name)s %(funcName)s"
fmt=("%(asctime)s %(levelname)s %(name)s:%(funcName)s"
" %(message)s"), datefmt="%y-%m-%d %H:%M:%S")

handler = logging.handlers.TimedRotatingFileHandler(


+ 22
- 1
bitshift/parser/python.py View File

@@ -1,4 +1,7 @@
import ast
import re

encoding_re = re.compile(r"^\s*#.*coding[:=]\s*([-\w.]+)", re.UNICODE)

class _CachedWalker(ast.NodeVisitor):
"""
@@ -154,7 +157,25 @@ def parse_py(codelet):
:type code: Codelet
"""

tree = ast.parse(codelet.code)
def strip_encoding(lines):
"""Strips the encoding line from a file, which breaks the parser."""
try:
first = next(lines)
if not encoding_re.match(first):
yield first
second = next(lines)
if not encoding_re.match(second):
yield second
except StopIteration:
return
for line in lines:
yield line

try:
tree = ast.parse("\n".join(strip_encoding(codelet.code.splitlines())))
except SyntaxError:
## TODO: add some logging here?
return
cutter = _CachedWalker()
cutter.visit(tree)
codelet.symbols = cutter.accum

Loading…
Cancel
Save