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