metripy 0.3.2__py3-none-any.whl → 0.3.7__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of metripy might be problematic. Click here for more details.

@@ -16,7 +16,7 @@ class ConfigFileReaderInterface(ABC):
16
16
  pass
17
17
 
18
18
  def resolve_path(self, path: str) -> str:
19
- return os.path.join(os.path.dirname(self.filename), path)
19
+ return path
20
20
 
21
21
  def parse_data(self, data: dict, config: Config) -> None:
22
22
  # configs
@@ -1,17 +1,42 @@
1
+ from importlib.metadata import version, metadata
1
2
  import toml
2
3
 
3
4
 
4
5
  class Info:
5
6
  def __init__(self):
6
- data = self._get_data()
7
- self.version = data["project"]["version"]
8
- self.url = data["project"]["urls"]["Homepage"]
7
+ self.version = self._get_version()
8
+ self.url = self._get_homepage_url()
9
9
 
10
- def _get_data(self) -> dict:
10
+ def _get_pyproject_data(self) -> dict:
11
11
  with open("pyproject.toml", "r") as file:
12
12
  data = toml.load(file)
13
13
  return data
14
14
 
15
+ def _get_version(self) -> str:
16
+ """Get version from installed package metadata"""
17
+ try:
18
+ return version("metripy")
19
+ except Exception:
20
+ # Fallback for development if not installed
21
+ return self._get_pyproject_data()["project"]["version"]
22
+
23
+ def _get_homepage_url(self) -> str:
24
+ """Get homepage URL from installed package metadata"""
25
+ try:
26
+ meta = metadata("metripy")
27
+ # Try to get Home-Page from metadata
28
+ homepage = meta.get("Home-Page")
29
+ if not homepage:
30
+ # Try Project-URL field
31
+ for line in meta.get_all("Project-URL") or []:
32
+ if line.startswith("Homepage"):
33
+ homepage = line.split(",", 1)[1].strip()
34
+ break
35
+ return homepage or "no homepage found"
36
+ except Exception:
37
+ # Fallback
38
+ return self._get_pyproject_data()["project"]["urls"]["Homepage"]
39
+
15
40
  def get_version(self) -> str:
16
41
  return self.version
17
42
 
@@ -1,7 +1,10 @@
1
1
  import math
2
+ import os
2
3
  from pathlib import Path
3
4
 
4
5
  import lizard
6
+ import tempfile
7
+ import re
5
8
 
6
9
  from metripy.Component.Output.ProgressBar import ProgressBar
7
10
  from metripy.LangAnalyzer.AbstractLangAnalyzer import AbstractLangAnalyzer
@@ -46,10 +49,29 @@ class PhpAnalyzer(AbstractLangAnalyzer):
46
49
  return f"{filename}:{item_name}"
47
50
  return f"{filename}:{class_name}:{item_name}"
48
51
 
52
+ def _create_lizard_analyzable_file(self, filename: str) -> str:
53
+ """
54
+ Because of a bug in lizard it cannot correctly analyze traits.
55
+ See https://github.com/terryyin/lizard/issues/441
56
+ """
57
+ tmp_file = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.php')
58
+ with open(filename, "r") as f:
59
+ code = f.readlines()
60
+ code = [re.sub(r'^(\s*)trait\s+', r'\1class ', line) for line in code]
61
+ tmp_file.writelines(code)
62
+ tmp_file.close()
63
+ return tmp_file.name
64
+
49
65
  def analyze(self, code: str, filename: str) -> None:
50
66
  file_stem = Path(filename).stem
51
67
  structure = PhpBasicAstParser.parse_php_structure(code)
52
68
 
69
+
70
+ is_tmp_file = False
71
+ if "trait" in filename.lower():
72
+ filename = self._create_lizard_analyzable_file(filename)
73
+ is_tmp_file = True
74
+
53
75
  lizard_result = lizard.analyze_file(filename)
54
76
  complexity_data = {
55
77
  func.name: {
@@ -60,6 +82,9 @@ class PhpAnalyzer(AbstractLangAnalyzer):
60
82
  for func in lizard_result.function_list
61
83
  }
62
84
 
85
+ if is_tmp_file:
86
+ os.unlink(filename)
87
+
63
88
  classes: dict[str, ClassNode] = {}
64
89
  functions: dict[str, FunctionNode] = {}
65
90
  for obj in structure:
@@ -1,7 +1,7 @@
1
1
  from metripy.Dependency.Dependency import Dependency
2
2
  from metripy.Metric.ProjectMetrics import ProjectMetrics
3
3
  from metripy.Report.Html.PageRenderer import PageRenderer
4
-
4
+ import json
5
5
 
6
6
  class DependencyPageRenderer(PageRenderer):
7
7
  def __init__(self, template_dir: str, output_dir: str, project_name: str):
@@ -16,6 +16,6 @@ class DependencyPageRenderer(PageRenderer):
16
16
  {
17
17
  "has_dependencies_data": bool(metrics.dependencies),
18
18
  "dependencies": [d.to_dict() for d in dependencies],
19
- "license_distribution": license_by_type,
19
+ "license_distribution_json": json.dumps(license_by_type, indent=2),
20
20
  },
21
21
  )
@@ -3,6 +3,8 @@ import json
3
3
  from metripy.Metric.ProjectMetrics import ProjectMetrics
4
4
  from metripy.Report.Html.PageRenderer import PageRenderer
5
5
 
6
+ from metripy.Dependency.Dependency import Dependency
7
+
6
8
 
7
9
  class IndexPageRenderer(PageRenderer):
8
10
  def __init__(self, template_dir: str, output_dir: str, project_name: str):
@@ -13,10 +15,15 @@ class IndexPageRenderer(PageRenderer):
13
15
  if metrics.git_metrics:
14
16
  git_stats_data = metrics.git_metrics.get_commit_stats_per_month()
15
17
 
18
+
19
+ dependencies = metrics.dependencies if metrics.dependencies is not None else []
20
+ license_by_type = Dependency.get_lisence_distribution(dependencies)
21
+
16
22
  self.render_template(
17
23
  "index.html",
18
24
  {
19
25
  "git_stats_data": json.dumps(git_stats_data, indent=4),
26
+ "license_distribution_json": json.dumps(license_by_type, indent=2),
20
27
  "total_code_metrics": metrics.total_code_metrics.to_dict(),
21
28
  "has_total_code_metrics_trend": metrics.total_code_metrics.trend
22
29
  is not None,
@@ -1,6 +1,8 @@
1
1
  import os
2
2
  import shutil
3
+ import sys
3
4
  from datetime import datetime
5
+ from pathlib import Path
4
6
 
5
7
 
6
8
  from metripy.Application.Config.ReportConfig import ReportConfig
@@ -16,7 +18,16 @@ class Reporter(ReporterInterface):
16
18
  ):
17
19
  self.config: ReportConfig = config
18
20
  self.output = output
19
- self.template_dir = os.path.join(os.getcwd(), "templates/html_report")
21
+
22
+ # Find templates directory - works both in development and when installed
23
+ template_dir = self._find_template_dir()
24
+ if not template_dir.exists():
25
+ raise FileNotFoundError(
26
+ f"Could not find templates directory. Searched in: "
27
+ f"{template_dir}"
28
+ )
29
+
30
+ self.template_dir = str(template_dir)
20
31
  self.project_name = project_name
21
32
 
22
33
  self.page_renderer_factory = PageRendererFactory(
@@ -27,6 +38,28 @@ class Reporter(ReporterInterface):
27
38
  "project_name": project_name,
28
39
  "last_updated": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
29
40
  }
41
+
42
+ def _find_template_dir(self) -> Path:
43
+ """Find the templates directory, checking multiple possible locations"""
44
+ package_dir = Path(__file__).parent.parent.parent # metripy package root
45
+
46
+ # List of possible locations to check
47
+ possible_locations = [
48
+ # Development: templates at project root
49
+ package_dir.parent / "templates" / "html_report",
50
+ # Alternative: templates inside metripy package
51
+ package_dir / "templates" / "html_report",
52
+ # System install location
53
+ Path(sys.prefix) / "share" / "metripy" / "templates" / "html_report",
54
+ # Fallback to cwd (for development)
55
+ Path.cwd() / "metripy" / "templates" / "html_report",
56
+ ]
57
+
58
+ for location in possible_locations:
59
+ if location.exists() and (location / "index.html").exists():
60
+ return location
61
+
62
+ return possible_locations[0]
30
63
 
31
64
  def generate(self, metrics: ProjectMetrics):
32
65
 
@@ -63,12 +96,6 @@ class Reporter(ReporterInterface):
63
96
  )
64
97
  # shutil.copytree(os.path.join(self.template_dir, "fonts"), os.path.join(self.config.path, "fonts"), dirs_exist_ok=True)
65
98
 
66
- # copy logo, lies 2 down from the templates directory
67
- shutil.copy(
68
- os.path.join(self.template_dir, "../..", "logo.svg"),
69
- os.path.join(self.config.path, "images", "logo.svg"),
70
- )
71
-
72
99
  # Render main pages
73
100
  self.render_index_page(metrics)
74
101
  self.render_files_page(metrics)
@@ -22,6 +22,29 @@ class TrendsPageRenderer(PageRenderer):
22
22
  }
23
23
 
24
24
  def render(self, metrics: ProjectMetrics):
25
+ if metrics.total_code_metrics.trend is None:
26
+ self.render_template(
27
+ "trends.html",
28
+ {
29
+ "has_trend_data": False,
30
+ "trend_data": {
31
+ "top_improved_complexity": [],
32
+ "top_improved_maintainability": [],
33
+ "top_worsened_complexity": [],
34
+ "top_worsened_maintainability": [],
35
+ "loc_segments_current": {},
36
+ "loc_segments_prev": {},
37
+ "complexity_segments_current": {},
38
+ "complexity_segments_prev": {},
39
+ "maintainability_segments_current": {},
40
+ "maintainability_segments_prev": {},
41
+ "method_size_segments_current": {},
42
+ "method_size_segments_prev": {},
43
+ },
44
+ },
45
+ )
46
+ return
47
+
25
48
  # Top improved complexity (complexity went down - negative delta)
26
49
  top_improved_complexity = [
27
50
  x