def get_composite_formation(self, formation_name): if not self.formations: raise ScoutCardMakerException('Formation Library Empty') formation_words = formation_name.strip().upper().split() #determine if going lt or rt direction = None if 'LT' in formation_words and 'RT' not in formation_words: direction = 'LT' elif 'LT' not in formation_words and 'RT' in formation_words: direction = 'RT' else: raise ScoutCardMakerException('Can\'t determine if going Lt or Rt from formation name: ' + formation_name) sub_formation_names = [word + ' ' + direction for word in formation_words if word != 'LT' and word != 'RT'] #check that all sub_formations exist for sub_formation_name in sub_formation_names: if sub_formation_name not in self.formations: raise ScoutCardMakerException(sub_formation_name + ' doesn\'t exist in library. Create it.') formation = Formation() #copy all formations affects one at a time for sub_formation_name in sub_formation_names: formation.override_formation(self.formations[sub_formation_name]) return formation
def get_script_from_excel_file(root, file_name): try: work_book = xl.open_workbook(file_name) if work_book.nsheets == 1: sheet = work_book.sheet_by_index(0) else: sheet_name = ChooseExcelSheetDialog(root, work_book.sheet_names()).show() sheet = work_book.sheet_by_name(sheet_name) plays = [] for index in range(1, sheet.nrows): row_values = sheet.row_values(index) play_info = {} play_info['Number'] = int(row_values[0]) play_info['Hash'] = row_values[1] play_info['Dnd'] = row_values[2] play_info['Formation'] = row_values[3] play_info['Play'] = row_values[4] play_info['Defense'] = row_values[5] play_info['Note'] = row_values[6] play_info['Card Maker Formation'] = row_values[7].strip().upper() play_info['Card Maker Defense'] = row_values[8].strip().upper() plays.append(play_info) except IOError as e: raise ScoutCardMakerException(str(e)) except Exception: traceback.print_exc() raise ScoutCardMakerException('Excel sheet incorrectly formatted.') return plays
def load_library(self, filename): try: with open(filename, 'rb') as file_object: defenses = pickle.load(file_object) self.defenses = defenses except IOError as e: raise ScoutCardMakerException(str(e))
def get_formation(self, formation_name): if not self.formations: raise ScoutCardMakerException('Formation Library Empty') formation = Formation() formation.copy_formation_from_formation(self.formations[formation_name]) return formation
def add_defense_to_library(self, defense_name, defense): if len(defense_name.strip().split()) != 1: raise ScoutCardMakerException("Defensive name must be one word.") defense_name_upper = defense_name.strip().upper() #create defense to save and add it to the library defense_to_save = get_default_defense() defense_to_save.copy_defense_from_defense(defense) self.defenses[defense_name_upper] = defense_to_save
def delete_formation_from_library(self, formation_name): formation_words = formation_name.strip().upper().split() formation_to_delete = formation_name opposite_formation_to_delete = None if formation_words[-1] == 'LT': opposite_formation_to_delete = ' '.join([formation_words[0], 'RT']) elif formation_words[-1] == 'RT': opposite_formation_to_delete = ' '.join([formation_words[0], 'LT']) else: raise ScoutCardMakerException('Can\'t delete formation that doesn\'t end in LT or RT') try: del self.formations[formation_to_delete] except KeyError: raise ScoutCardMakerException(f'{formation_to_delete} not in formations.') try: del self.formations[opposite_formation_to_delete] except KeyError: raise ScoutCardMakerException(f'{opposite_formation_to_delete} not in formations.')
def write_missing_formations_and_defenses_to_text_file(presentation_filename, plays, formation_library, defense_library): formations_listed_in_script = [ play['Card Maker Formation'] for play in plays ] parsed_formations = [] for composite_formation in formations_listed_in_script: split_formations = composite_formation.split() for formation in split_formations: if formation != 'LT' and formation != 'RT' and formation not in parsed_formations: parsed_formations.append(formation) missing_formations = [] for parsed_formation in parsed_formations: if not formation_library.does_formation_exist(parsed_formation + ' RT'): missing_formations.append(parsed_formation) defenses_listed_in_script = [play['Card Maker Defense'] for play in plays] parsed_defenses = [] for composite_defense in defenses_listed_in_script: split_defenses = composite_defense.split() for defense in split_defenses: if defense not in parsed_formations: parsed_defenses.append(defense) missing_defenses = [] for parsed_defense in parsed_defenses: if not defense_library.does_defense_exist(parsed_defense): missing_defenses.append(parsed_defense) if missing_formations or missing_defenses: try: presentation_directory = os.path.dirname(presentation_filename) missing_stuff_filename = os.path.join( presentation_directory, 'missingFormationsandDefense.txt') with open(missing_stuff_filename, 'w') as missing_stuff_file: if missing_formations: missing_stuff_file.write( 'Missing Formations\n-------------------\n') missing_stuff_file.write('\n'.join(missing_formations)) missing_stuff_file.write('\n\n') if missing_defenses: missing_stuff_file.write( 'Missing Defenses\n-------------------\n') missing_stuff_file.write('\n'.join(missing_defenses)) missing_stuff_file.write('\n\n') except IOError as e: raise ScoutCardMakerException( 'Couldn\'t write missing formations/defenses to text file')
def add_formation_to_library(self, formation_name, formation): formation_words = formation_name.strip().upper().split() #automatically make direction right if they they don't specify it if len(formation_words) == 1: formation_words.append('RT') #Check that formation name has only two words if len(formation_words) != 2: raise ScoutCardMakerException("Formation names must be made up one word and a direction.") #check that the last word is either lt or rt if formation_words[1] != 'LT' and formation_words[1] != 'RT': raise ScoutCardMakerException('Name must end in LT or RT') #check that no other words are LT or RT if formation_words[0] == 'LT' or formation_words[0] == 'RT': raise ScoutCardMakerException('Name can\'t contain LT or RT except to specify direction') #construct formation name from the words modified_formation_name = ' '.join(formation_words) #construct a flipped formation name as well if formation_words[-1] == 'LT': formation_words[-1] = 'RT' else: formation_words[-1] = 'LT' flipped_modified_formation_name = ' '.join(formation_words) #create two formations and add them to the library formation_to_save = Formation() formation_to_save.copy_formation_from_formation(formation) flipped_formation_to_save = Formation() flipped_formation_to_save.copy_formation_from_formation(formation) flipped_formation_to_save.flip_formation() self.formations[modified_formation_name] = formation_to_save self.formations[flipped_modified_formation_name] = flipped_formation_to_save
def get_composite_defense(self, defense_name): defense_words = defense_name.strip().upper().split() #check that all defenses exist for defense_word in defense_words: if defense_word not in self.defenses: raise ScoutCardMakerException( defense_word + ' doesn\'t exist in library. Create it.') defense = get_default_defense() #copy all defense affects one at a time for defense_word in defense_words: defense.override_defense(self.defenses[defense_word]) return defense
def save_library(self, filename): try: with open(filename, 'wb') as file_object: pickle.dump(self.defenses, file_object) except IOError as e: raise ScoutCardMakerException(str(e))
def delete_defense_from_library(self, defense_to_delete): try: del self.defenses[defense_to_delete] except KeyError: raise ScoutCardMakerException( f'{defense_to_delete} not in defenses.')