Cache to automagically download .hrb files
This commit is contained in:
		
							parent
							
								
									910b678ffe
								
							
						
					
					
						commit
						dfb285d239
					
				| 
						 | 
					@ -0,0 +1,39 @@
 | 
				
			||||||
 | 
					from os import listdir, mkdir, path
 | 
				
			||||||
 | 
					import re
 | 
				
			||||||
 | 
					import fetch
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					NAME_REGEX = r'(.*\..*)(_)'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Cache:
 | 
				
			||||||
 | 
					    def __init__(self, dir: str = "AutoDatabaseGenerator/Resources"):
 | 
				
			||||||
 | 
					        self.dir = dir
 | 
				
			||||||
 | 
					        self._cache = dict()
 | 
				
			||||||
 | 
					        self._scan_cache()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get(self, name: str):
 | 
				
			||||||
 | 
					        if name in self._cache:
 | 
				
			||||||
 | 
					            return path.abspath(self.dir + self._cache[name])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            driver = fetch.get_driver_by_exact_name(name)
 | 
				
			||||||
 | 
					            file = fetch.download_driver(driver, self.dir)
 | 
				
			||||||
 | 
					        except Exception as e:
 | 
				
			||||||
 | 
					            raise Exception("Could not fetch driver", e)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self._cache[name] = file
 | 
				
			||||||
 | 
					        return path.abspath(self.dir + self._cache[name])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _scan_cache(self):
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            files = listdir(self.dir)
 | 
				
			||||||
 | 
					            print("files found: ", files)
 | 
				
			||||||
 | 
					        except FileNotFoundError:
 | 
				
			||||||
 | 
					            mkdir(self.dir)
 | 
				
			||||||
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        filtered = [file for file in files if file.endswith('.hrb')]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for file in filtered:
 | 
				
			||||||
 | 
					            name = re.match(NAME_REGEX, file).group(1)
 | 
				
			||||||
 | 
					            self._cache[name] = file
 | 
				
			||||||
		Loading…
	
		Reference in New Issue