@@ 392-428 (lines=37) @@ | ||
389 | cls._cache[code_name] = regiment |
|
390 | return regiment |
|
391 | ||
392 | raise ValueError("Regiment with code name '{0}' is unknown" |
|
393 | .format(code_name)) |
|
394 | ||
395 | @classmethod |
|
396 | def filter_by_air_force(cls, air_force): |
|
397 | result = [] |
|
398 | ||
399 | flight_prefixes = AirForces.get_flight_prefixes() |
|
400 | found = False |
|
401 | ||
402 | file_path = _get_data_file_path(cls._file_name) |
|
403 | with open(file_path, mode='r', encoding='cp1251') as f: |
|
404 | for line in f: |
|
405 | line = line.strip() |
|
406 | ||
407 | if not line: |
|
408 | continue |
|
409 | ||
410 | if line == air_force.default_flight_prefix: |
|
411 | # Flag that proper section was found. |
|
412 | found = True |
|
413 | continue |
|
414 | ||
415 | if found: |
|
416 | if ( |
|
417 | line in flight_prefixes |
|
418 | or (line.startswith('[') and line.endswith(']')) |
|
419 | ): |
|
420 | # Next section was found. Fullstop. |
|
421 | break |
|
422 | ||
423 | if line in cls._cache: |
|
424 | regiment = cls._cache[line] |
|
425 | else: |
|
426 | regiment = Regiment(air_force, line) |
|
427 | cls._cache[line] = regiment |
|
428 | ||
429 | result.append(regiment) |
|
430 | ||
431 | return result |
|
@@ 361-390 (lines=30) @@ | ||
358 | _cache = {} |
|
359 | _file_name = 'regiments.ini' |
|
360 | ||
361 | def __new__(cls): |
|
362 | raise TypeError("'{0}' may not be instantiated".format(cls.__name__)) |
|
363 | ||
364 | @classmethod |
|
365 | def get_by_code_name(cls, code_name): |
|
366 | if code_name in cls._cache: |
|
367 | return cls._cache[code_name] |
|
368 | ||
369 | flight_prefixes = AirForces.get_flight_prefixes() |
|
370 | last_flight_prefix = None |
|
371 | found = False |
|
372 | ||
373 | file_path = _get_data_file_path(cls._file_name) |
|
374 | with open(file_path, mode='r', encoding='cp1251') as f: |
|
375 | for line in f: |
|
376 | line = line.strip() |
|
377 | if not line: |
|
378 | continue |
|
379 | if line in flight_prefixes: |
|
380 | last_flight_prefix = line |
|
381 | continue |
|
382 | if line == code_name: |
|
383 | found = True |
|
384 | break |
|
385 | ||
386 | if found and last_flight_prefix: |
|
387 | air_force = AirForces.get_by_flight_prefix(last_flight_prefix) |
|
388 | regiment = Regiment(air_force, code_name) |
|
389 | cls._cache[code_name] = regiment |
|
390 | return regiment |
|
391 | ||
392 | raise ValueError("Regiment with code name '{0}' is unknown" |
|
393 | .format(code_name)) |