Skip to content

Commit 42f2e72

Browse files
committed
[CHANGE] Python code refactored/optmized
1 parent 029249f commit 42f2e72

15 files changed

+2166
-618
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ Section Order:
3535
### Security
3636
-->
3737

38+
### Added
39+
40+
- More tests
41+
3842
### Changed
3943

40-
- JavaScript refactored/optmized
44+
- Python code refactored/optmized
45+
- JavaScript code refactored/optmized
4146
- HTML markup for the result tables is now unified
4247

4348
## [2.6.1] - 2025-03-07

aa_intel_tool/helper/eve_character.py

+24-36
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
logger = LoggerAddTag(my_logger=get_extension_logger(name=__name__), prefix=__title__)
2626

2727

28-
def _create_alliance(alliance_ids: Iterable[int] = None) -> None:
28+
def _create_alliance(alliance_ids: Iterable[int]) -> None:
2929
"""
3030
Bulk creation of EveAllianceInfo objects
3131
@@ -36,31 +36,28 @@ def _create_alliance(alliance_ids: Iterable[int] = None) -> None:
3636
"""
3737

3838
alliance_ids = set(alliance_ids)
39-
4039
existing_alliance_ids = set(
4140
EveAllianceInfo.objects.filter(alliance_id__in=alliance_ids).values_list(
4241
"alliance_id", flat=True
4342
)
4443
)
4544

4645
alliances_to_fetch = alliance_ids - existing_alliance_ids
47-
count_alliances_to_fetch = len(alliances_to_fetch)
4846

4947
if alliances_to_fetch:
5048
logger.debug(
51-
f"{count_alliances_to_fetch} EveAllianceInfo object(s) need to be created …"
49+
f"{len(alliances_to_fetch)} EveAllianceInfo object(s) need to be created …"
5250
)
5351

54-
for loop_count, alliance_id in enumerate(alliances_to_fetch):
52+
for loop_count, alliance_id in enumerate(alliances_to_fetch, start=1):
5553
alliance = EveAllianceInfo.objects.create_alliance(alliance_id=alliance_id)
56-
5754
logger.debug(
58-
f"({loop_count + 1}/{count_alliances_to_fetch}) "
55+
f"({loop_count}/{len(alliances_to_fetch)}) "
5956
f"EveAllianceInfo object created for: {alliance.alliance_name}"
6057
)
6158

6259

63-
def _create_corporation(corporation_ids: Iterable[int] = None) -> None:
60+
def _create_corporation(corporation_ids: Iterable[int]) -> None:
6461
"""
6562
Bulk creation of EveCorporationInfo objects
6663
@@ -71,34 +68,32 @@ def _create_corporation(corporation_ids: Iterable[int] = None) -> None:
7168
"""
7269

7370
corporation_ids = set(corporation_ids)
74-
7571
existing_corporation_ids = set(
7672
EveCorporationInfo.objects.filter(
7773
corporation_id__in=corporation_ids
7874
).values_list("corporation_id", flat=True)
7975
)
8076

8177
corporations_to_fetch = corporation_ids - existing_corporation_ids
82-
count_corporations_to_fetch = len(corporations_to_fetch)
8378

8479
if corporations_to_fetch:
8580
logger.debug(
86-
f"{count_corporations_to_fetch} EveCorporationInfo object(s) need to be created …" # pylint: disable=line-too-long
81+
f"{len(corporations_to_fetch)} EveCorporationInfo object(s) need to be created …"
8782
)
8883

89-
for loop_count, corporation_id in enumerate(corporations_to_fetch):
84+
for loop_count, corporation_id in enumerate(corporations_to_fetch, start=1):
9085
corporation = EveCorporationInfo.objects.create_corporation(
9186
corp_id=corporation_id
9287
)
9388

9489
logger.debug(
95-
f"({loop_count + 1}/{count_corporations_to_fetch}) "
90+
f"({loop_count}/{len(corporations_to_fetch)}) "
9691
f"EveCorporationInfo object created for: {corporation.corporation_name}"
9792
)
9893

9994

10095
def _create_character(
101-
character_ids: Iterable[int] = None, with_affiliation: bool = True
96+
character_ids: Iterable[int], with_affiliation: bool = True
10297
) -> None:
10398
"""
10499
Bulk creation of EveCharacter objects
@@ -112,35 +107,30 @@ def _create_character(
112107
"""
113108

114109
character_ids = set(character_ids)
110+
tmp_affiliation_ids = {"alliance": set(), "corporation": set()}
115111

116-
count_characters_to_fetch = len(character_ids)
117-
118-
logger.debug(
119-
f"{count_characters_to_fetch} EveCharacter object(s) need to be created …"
120-
)
121-
122-
tmp_character_ids = {"corporation": [], "alliance": []}
112+
logger.info(f"{len(character_ids)} EveCharacter object(s) need to be created …")
123113

124-
for loop_count, character_id in enumerate(character_ids):
114+
for loop_count, character_id in enumerate(character_ids, start=1):
125115
# Create character
126116
character = EveCharacter.objects.create_character(character_id=character_id)
127117

128118
logger.debug(
129-
f"({loop_count + 1}/{count_characters_to_fetch}) "
119+
f"({loop_count}/{len(character_ids)}) "
130120
f"EveCharacter object created for: {character.character_name}"
131121
)
132122

133-
if character.alliance_id is not None:
134-
tmp_character_ids["alliance"].append(character.alliance_id)
135-
else:
136-
tmp_character_ids["corporation"].append(character.corporation_id)
123+
affiliation_key = "alliance" if character.alliance_id else "corporation"
124+
tmp_affiliation_ids[affiliation_key].add(
125+
character.alliance_id or character.corporation_id
126+
)
137127

138-
if with_affiliation is True:
139-
if len(tmp_character_ids["alliance"]) > 0:
140-
_create_alliance(tmp_character_ids["alliance"])
128+
if with_affiliation:
129+
if tmp_affiliation_ids["alliance"]:
130+
_create_alliance(tmp_affiliation_ids["alliance"])
141131

142-
if len(tmp_character_ids["corporation"]) > 0:
143-
_create_corporation(tmp_character_ids["corporation"])
132+
if tmp_affiliation_ids["corporation"]:
133+
_create_corporation(tmp_affiliation_ids["corporation"])
144134

145135

146136
def get_or_create_character(
@@ -157,7 +147,7 @@ def get_or_create_character(
157147
:rtype:
158148
"""
159149

160-
character_ids = set(character_ids)
150+
character_ids = set(character_ids or [])
161151

162152
logger.debug(
163153
msg=f"Getting information for {len(character_ids)} character(s) from AA …"
@@ -174,6 +164,4 @@ def get_or_create_character(
174164
if character_ids_to_fetch:
175165
_create_character(character_ids=character_ids_to_fetch, with_affiliation=True)
176166

177-
characters = EveCharacter.objects.filter(character_id__in=character_ids)
178-
179-
return characters
167+
return EveCharacter.objects.filter(character_id__in=character_ids)

aa_intel_tool/helper/static_files.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ def calculate_integrity_hash(relative_file_path: str) -> str:
3434
:rtype: str
3535
"""
3636

37-
file_path = os.path.join(APP_STATIC_DIR, relative_file_path)
3837
integrity_hash = calculate_integrity(
39-
path=Path(file_path), algorithm=Algorithm.SHA512
38+
path=Path(os.path.join(APP_STATIC_DIR, relative_file_path)),
39+
algorithm=Algorithm.SHA512,
4040
)
4141

4242
return integrity_hash

aa_intel_tool/parser/general.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,19 @@ def parse_intel(form_data: str) -> str:
5757
:return:
5858
:rtype:
5959
"""
60-
6160
scan_data = form_data.splitlines()
6261

63-
if len(scan_data) > 0:
64-
try:
65-
intel_type = check_intel_type(scan_data=scan_data)
66-
except ParserError as exc:
67-
raise ParserError(message=exc.message) from exc
68-
69-
try:
70-
new_scan = SUPPORTED_INTEL_TYPES[intel_type]["parser"](scan_data=scan_data)
71-
except ParserError as exc:
72-
# Re-raise the Exception
73-
raise ParserError(message=exc.message) from exc
62+
if not scan_data:
63+
raise ParserError(message=_("No data to parse …"))
7464

75-
new_scan.raw_data = form_data
76-
new_scan.save()
65+
try:
66+
intel_type = check_intel_type(scan_data=scan_data)
67+
new_scan = SUPPORTED_INTEL_TYPES[intel_type]["parser"](scan_data=scan_data)
68+
except ParserError as exc:
69+
# Re-raise the Exception
70+
raise ParserError(message=exc.message) from exc
7771

78-
return new_scan.hash
72+
new_scan.raw_data = form_data
73+
new_scan.save()
7974

80-
raise ParserError(message=_("No data to parse …"))
75+
return new_scan.hash

aa_intel_tool/parser/helper/db.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,20 @@ def safe_scan_to_db(scan_type: Scan.Type, parsed_data: dict) -> Scan:
1818
:rtype:
1919
"""
2020

21-
# Creating a new Scan object
22-
new_scan = Scan(scan_type=scan_type)
23-
new_scan.save()
21+
# Creating a new Scan object and saving it
22+
new_scan = Scan.objects.create(scan_type=scan_type)
2423

25-
# Creating the associated ScanData objects
26-
scan_data_objects = []
27-
for scan_data in parsed_data.values():
28-
scan_data_objects.append(
24+
# Creating and saving the associated ScanData objects
25+
ScanData.objects.bulk_create(
26+
[
2927
ScanData(
3028
scan=new_scan,
3129
section=scan_data["section"],
3230
processed_data=scan_data["data"],
3331
)
34-
)
35-
36-
# Saving ScanData objects
37-
ScanData.objects.bulk_create(scan_data_objects)
32+
for scan_data in parsed_data.values()
33+
]
34+
)
3835

3936
# Return the Scan object
4037
return new_scan

0 commit comments

Comments
 (0)