"""Utility functions for the statemodify package."""importglobimportosfromtypingimportUnionimportpkg_resourcesimportyamlos.environ["PYGAME_HIDE_SUPPORT_PROMPT"]="hide"importpygamefrompygame.localsimportKEYDOWN,QUIT
[docs]defyaml_to_dict(yaml_file:str)->dict:"""Read in a YAML file and convert to a typed dictionary. NOTE: code can be executed from the YAML file due to the use of UnsafeLoader. :param yaml_file: Full path with file name and extension to the input YAML file. :type yaml_file: str :return: Dictionary of typed elements of the YAML file. :rtype: dict """withopen(yaml_file)asyml:returnyaml.load(yml,Loader=yaml.UnsafeLoader)
[docs]defselect_template_file(basin_name:str,template_file:Union[None,str],extension:Union[None,str]=None)->str:"""Select either the default template file or a user provided one. :param basin_name: Name of basin for either: Upper_Colorado Yampa San_Juan Gunnison White :type basin_name: str :param template_file: If a full path to a template file is provided it will be used. Otherwise the default template in this package will be used. :type template_file: Union[None, str] :param extension: Extension of the target template file with no dot. :type extension: Union[None, str] :return: Template file path :rtype: str """iftemplate_fileisNone:# get basin abbreviation from basin namespec_file=pkg_resources.resource_filename("statemodify","data/basin_specification.yml")basin_spec=yaml_to_dict(spec_file)basin_abbrev=basin_spec[basin_name]["abbrev"]data_dir=pkg_resources.resource_filename("statemodify","data")returnglob.glob(os.path.join(data_dir,f"{basin_abbrev}*.{extension}"))[0]else:returntemplate_file
[docs]defselect_data_specification_file(yaml_file:Union[None,str],extension:Union[None,str]=None)->str:"""Select either the default template file or a user provided one. :param yaml_file: If a full path to a YAML file is provided it will be used. Otherwise the default file in this package will be used. :type yaml_file: Union[None, str] :param extension: Extension of the target template file with no dot. :type extension: Union[None, str] :return: Template file path :rtype: str """ifyaml_fileisNone:returnpkg_resources.resource_filename("statemodify",f"data/{extension}_data_specification.yml")else:returnyaml_file
defcredits():"""Run credit reel."""credit_list=["statemodify"," ","A gift to you","and yours from:"," ","Rohini S. Gupta","and","Chris R. Vernon",]pygame.init()pygame.display.set_caption("End credits")screen=pygame.display.set_mode((800,600))screen_r=screen.get_rect()font=pygame.font.Font(pkg_resources.resource_filename("statemodify","data/EightBit-Atari-Regular.ttf"),26,)music_file=pkg_resources.resource_filename("statemodify","data/corrina.midi")clock=pygame.time.Clock()pygame.mixer.music.load(music_file)pygame.mixer.music.play()background_image=pygame.image.load(pkg_resources.resource_filename("statemodify","data/background.png"))content=[]forindex,lineinenumerate(credit_list):ft=font.render(line,1,(255,255,255))# white fontplacement=ft.get_rect(centerx=screen_r.centerx,y=screen_r.bottom+index*45)content.append((placement,ft))whileTrue:foreinpygame.event.get():ife.type==QUITore.type==KEYDOWNande.key==pygame.K_ESCAPE:breakscreen.blit(background_image,(0,0))forr,sincontent:r.move_ip(0,-1)screen.blit(s,r)ifnotscreen_r.collidelistall([rfor(r,_)incontent]):breakpygame.display.flip()# fps designationclock.tick(25)pygame.mixer.music.stop()pygame.mixer.quit()pygame.quit()returnNone