Skip to content

Custom collectors #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions djlsp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def main():
default=False,
help="Use cache for Django data collection (optional provide a filepath)",
)
parser.add_argument(
"--custom-collector-module",
action="store",
type=str,
help="Provide a python path to a function that is used to collect custom data",
)

args = parser.parse_args()

Expand All @@ -55,6 +61,7 @@ def main():
"docker_compose_service": args.docker_compose_service,
"django_settings_module": args.django_settings_module,
"cache": args.cache,
"custom_collector_module": args.custom_collector_module,
}

if args.collect:
Expand Down
6 changes: 6 additions & 0 deletions djlsp/scripts/django-collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ def get_default_django_settings_module():
)
parser.add_argument("--django-settings-module", action="store", type=str)
parser.add_argument("--project-src", action="store", type=str)
parser.add_argument("--custom-collector-module", action="store", type=str)
args = parser.parse_args()

project_src_path = args.project_src if args.project_src else os.getcwd()
Expand Down Expand Up @@ -635,4 +636,9 @@ def get_default_django_settings_module():
collector = DjangoIndexCollector(project_src_path)
collector.collect()

if args.custom_collector_module:
module, func = args.custom_collector_module.rsplit(".", 1)
custom_collector = importlib.import_module(module)
getattr(custom_collector, func)(collector)

print(collector.to_json(), file=stdout)
17 changes: 16 additions & 1 deletion djlsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def __init__(self, *args, **kwargs):
self.docker_compose_service = "django"
self.django_settings_module = ""
self.cache = False
self.custom_collector_module = None
self.workspace_index = WorkspaceIndex()
self.workspace_index.update(FALLBACK_DJANGO_DATA)
self.jedi_project = jedi.Project(".")
Expand Down Expand Up @@ -100,6 +101,9 @@ def set_initialization_options(self, options: dict):
"django_settings_module", self.django_settings_module
)
self.cache = options.get("cache", self.cache)
self.custom_collector_module = options.get(
"custom_collector_module", self.custom_collector_module
)

def check_version(self):
try:
Expand Down Expand Up @@ -242,12 +246,13 @@ def _get_cache_file_hash(self, django_data):
files = set(
f
for p in django_data["file_watcher_globs"]
for f in glob.glob(p, recursive=True)
for f in glob.glob(p, root_dir=self.workspace.root_path, recursive=True)
)
files.add(DJANGO_COLLECTOR_SCRIPT_PATH)

h = hashlib.md5()
for f in sorted(files):
f = os.path.join(self.workspace.root_path, f)
if os.path.isfile(f):
h.update(f"{os.stat(f).st_mtime}".encode())

Expand Down Expand Up @@ -276,6 +281,11 @@ def _get_django_data_from_python_path(self, python_path):
else None
),
f"--project-src={self.project_src_path}",
(
f"--custom-collector-module={self.custom_collector_module}" # noqa: E501
if self.custom_collector_module
else None
),
],
)
)
Expand Down Expand Up @@ -333,6 +343,11 @@ def _get_django_data_from_docker(self):
else None
),
"--project-src=/src",
(
f"--custom-collector-module={self.custom_collector_module}" # noqa: E501
if self.custom_collector_module
else None
),
],
)
)
Expand Down