File: notes.py

package info (click to toggle)
python-django-extensions 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,820 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (74 lines) | stat: -rw-r--r-- 3,024 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
import os
import re

from django.conf import settings
from django.core.management.base import BaseCommand

from django_extensions.compat import get_template_setting
from django_extensions.management.utils import signalcommand

ANNOTATION_RE = re.compile(
    r"\{?#[\s]*?(TODO|FIXME|BUG|HACK|WARNING|NOTE|XXX)[\s:]?(.+)"
)
ANNOTATION_END_RE = re.compile(r"(.*)#\}(.*)")


class Command(BaseCommand):
    help = "Show all annotations like TODO, FIXME, BUG, HACK, WARNING, NOTE or XXX "
    "in your py and HTML files."
    label = "annotation tag (TODO, FIXME, BUG, HACK, WARNING, NOTE, XXX)"

    def add_arguments(self, parser):
        super().add_arguments(parser)
        parser.add_argument(
            "--tag", dest="tag", help="Search for specific tags only", action="append"
        )

    @signalcommand
    def handle(self, *args, **options):
        # don't add django internal code
        apps = [
            app.replace(".", "/")
            for app in filter(
                lambda app: not app.startswith("django.contrib"),
                settings.INSTALLED_APPS,
            )
        ]
        template_dirs = get_template_setting("DIRS", [])
        base_dir = getattr(settings, "BASE_DIR")
        if template_dirs:
            apps += template_dirs
        for app_dir in apps:
            if base_dir:
                app_dir = os.path.join(base_dir, app_dir)
            for top, dirs, files in os.walk(app_dir):
                for fn in files:
                    if os.path.splitext(fn)[1] in (".py", ".html"):
                        fpath = os.path.join(top, fn)
                        annotation_lines = []
                        with open(fpath, "r") as fd:
                            i = 0
                            for line in fd.readlines():
                                i += 1
                                if ANNOTATION_RE.search(line):
                                    tag, msg = ANNOTATION_RE.findall(line)[0]
                                    if options["tag"]:
                                        if tag not in map(
                                            str.upper, map(str, options["tag"])
                                        ):
                                            break

                                    if ANNOTATION_END_RE.search(msg.strip()):
                                        msg = ANNOTATION_END_RE.findall(msg.strip())[0][
                                            0
                                        ]

                                    annotation_lines.append(
                                        "[%3s] %-5s %s" % (i, tag, msg.strip())
                                    )
                            if annotation_lines:
                                self.stdout.write("%s:" % fpath)
                                for annotation in annotation_lines:
                                    self.stdout.write("  * %s" % annotation)
                                self.stdout.write("")