def from_fsdb(fs_comp, short_name=None): """gets comp and formula info from FSDB file""" from calcUtils import get_date from pathlib import Path from glob import glob from compUtils import create_comp_code, is_shortcode_unique comp = Comp() comp.comp_name = fs_comp.get('name') comp.comp_class = (fs_comp.get('discipline')).upper() comp.comp_site = fs_comp.get('location') comp.date_from = get_date(fs_comp.get('from')) comp.date_to = get_date(fs_comp.get('to')) comp.time_offset = int(float(fs_comp.get('utc_offset')) * 3600) comp.sanction = ('FAI 1' if fs_comp.get('fai_sanctioning') == '1' else 'FAI 2' if fs_comp.get('fai_sanctioning') == '2' else 'none') comp.external = True comp.locked = True '''check if we have comp_code''' if short_name and is_shortcode_unique(short_name, comp.date_from): comp.comp_code = short_name else: comp.comp_code = create_comp_code(comp.comp_name, comp.date_from) comp.create_path() '''check path does not already exist''' if Path(comp.file_path).exists(): '''create a new comp_code and comp_path''' index = len(glob(comp.file_path + '*/')) + 1 comp.comp_code = '_'.join([comp.comp_code, str(index)]) print(f"Comp short name already exists: changing to {comp.comp_code}") comp.comp_path = create_comp_path(comp.date_from, comp.comp_code) return comp
def __setattr__(self, attr, value): import datetime if attr in ('date_from', 'date_to'): if type(value) is str: value = get_date(value) elif isinstance(value, datetime.datetime): value = value.date() self.__dict__[attr] = value
def from_fsdb(pil, from_CIVL=False): """gets pilot obj. from FSDB file Input: - pil: lxml.etree: FsParticipant section - from_CIVL: BOOL: look for pilot on CIVL database""" CIVLID = None if not pil.get('CIVLID') else int(pil.get('CIVLID')) name = pil.get('name') # print(CIVLID, name) pilot = None if from_CIVL: '''check CIVL database''' if CIVLID: pilot = create_participant_from_CIVLID(CIVLID) else: pilot = create_participant_from_name(name) '''check if we have a result and name is similar''' if not (pilot and any(n in pilot.name for n in name)): '''get all pilot info from fsdb file''' if from_CIVL: print( '*** no result in CIVL database, getting data from FSDB file' ) pilot = Participant(name=name, civl_id=CIVLID) pilot.sex = 'F' if int( pil.get('female') if pil.get('female') else 0) > 0 else 'M' pilot.nat = pil.get('nat_code_3166_a3') pilot.birthdate = get_date(pil.get('birthday')) pilot.ID = int(pil.get('id')) pilot.glider = pil.get('glider') pilot.sponsor = pil.get('sponsor') """check fai is int""" if pil.get('fai_licence'): pilot.fai_valid = True pilot.fai_id = pil.get('fai_licence') else: pilot.fai_valid = False pilot.fai_id = None """check Live ID""" node = pil.find('FsCustomAttributes') if node is not None: childs = node.findall('FsCustomAttribute') live = next(el for el in childs if el.get('name') == 'Live') if live is not None: pilot.live_id = int(live.get('value')) print(pilot.live_id) return pilot