def __init__(self, directory): self.logger = logging.getLogger(__name__) self.config_cd: CachedDictionary self.config_cd = None self.directory: str = directory self.cache_dir = GeoUtil.get_cache_directory(self.directory)
def get_directory_locations(self): home_path = str(Path.home()) self.directory = Path( os.path.join(home_path, str(GeoUtil.get_directory_name()))) self.ini_handler = IniHandler.IniHandler(base_path=home_path, ini_name='geofinder.ini') self.directory = self.ini_handler.get_directory_from_ini( "GeoFinder", GeoUtil.get_directory_name()) self.cache_dir = GeoUtil.get_cache_directory(self.directory) self.logger.info(f'Cache directory {self.cache_dir}')
def __init__(self, directory: str, filename: str, progress_bar, prefix, geo_build: GeodataBuild, lang_list): """ Read in geonames alternate names file and add to geodata database in alt_names table # Args: directory: base directory for alternate names file filename: filename of geonames alternate_namesV2.txt file progress_bar: tkhelper progress bar or None geo_files: GeodataFiles instance lang_list: List of ISO languages we want to support, e.g. ['fr', 'es'] """ super().__init__(directory, filename, progress_bar, prefix=prefix) self.sub_dir = GeoUtil.get_cache_directory(directory) self.geo_build: GeodataBuild.GeodataBuild = geo_build self.lang_list = lang_list self.place = Loc.Loc() self.search = None
def __init__(self): self.logger = logging.getLogger(__name__) fmt = "%(levelname)s %(asctime)s %(name)s.%(funcName)s %(lineno)d: %(message)s" logging.basicConfig(level=logging.DEBUG, format=fmt) self.logger.info('Configuration') self.directory: str = os.path.join(str(Path.home()), GeoUtil.get_directory_name()) self.cache_dir = GeoUtil.get_cache_directory(self.directory) # Get configuration settings stored in config pickle file self.cfg: CachedDictionary.CachedDictionary = CachedDictionary.CachedDictionary( self.cache_dir, "config.pkl") if not os.path.exists(self.directory): self.logger.info(f'Creating main folder {self.directory}') os.makedirs(self.directory) if not os.path.exists(self.cache_dir): self.logger.info(f'Creating cache folder {self.cache_dir}') os.makedirs(self.cache_dir) self.cfg.read() # Verify config - test to see if gedcom file accessible self.get_config() # Create App window self.root = Tk() self.root["padx"] = 30 self.root["pady"] = 30 self.root.title('GeoUtil') UtilLayout.UtilLayout(root=self.root, directory=self.directory, cache_dir=self.cache_dir)
def __init__(self, directory: str, display_progress, show_message: bool, exit_on_error: bool, languages_list_dct: {}, feature_code_list_dct: {}, supported_countries_dct: {}, volume=''): """ Read in datafiles needed for geodata, filter them and create a sql db. Filter dictionary examples: languages_list_dct={'fr','de'} feature_code_list_dct={'PPL', 'ADM1', 'CSTL'} supported_countries_dct = {'us','gb','at'} # Args: directory: base directory display_progress: None or Handler called with percent_done:int, msg:str show_message: True to show message boxes to user on errors exit_on_error: True to exit on serious errors languages_list_dct: dictionary containing the ISO-2 languages to load from alternateNames feature_code_list_dct: dictionary containing the Geonames.org feature codes to load supported_countries_dct: dictionary containing the ISO-2 countries to load volume: disk volume to use - e.g. C: for Windows or /Volumes/xyz for OSX, /media/xyz for linux """ self.logger = logging.getLogger(__name__) self.geodb: [GeoDB.GeoDB, None] = None self.show_message = show_message self.geoid_main_dict = {} # Key is GEOID, Value is DB ID for entry self.geoid_admin_dict = {} # Key is GEOID, Value is DB ID for entry # TODO fix volume handling self.volume = volume self.collate = 'COLLATE NOCASE' self.exit_on_error = exit_on_error self.required_db_version = 4 # Message to user upgrading from earlier DB version self.db_upgrade_text = 'Renamed column to Feature' self.directory: str = directory self.progress_bar = display_progress self.line_num = 0 self.cache_changed: bool = False sub_dir = GeoUtil.get_cache_directory(self.directory) self.country = None self.languages_list_dct = languages_list_dct self.feature_code_list_dct = feature_code_list_dct self.supported_countries_dct = supported_countries_dct self.lang_list = [] self.norm = Normalize.Normalize() for item in self.languages_list_dct: self.lang_list.append(item) if volume != '': os.chdir(volume) if not os.path.exists(sub_dir): self.logger.warning(f'Directory] {sub_dir} NOT FOUND') if self.show_message: messagebox.showwarning( 'Folder not found', f'Directory\n\n {sub_dir}\n\n NOT FOUND') if exit_on_error: sys.exit() # Read in Text Replacement dictionary pickle - this has output text replacements self.output_replace_cd = CachedDictionary.CachedDictionary( sub_dir, "output_list.pkl") self.output_replace_cd.read() self.output_replace_dct: Dict[str, str] = self.output_replace_cd.dict self.output_replace_list = [] for item in self.output_replace_dct: self.output_replace_list.append(item) self.entry_place = Loc.Loc() # Support for Geonames AlternateNames file. Adds alternate names for entries self.alternate_names = AlternateNames.AlternateNames( directory=self.directory, geo_build=self, progress_bar=self.progress_bar, prefix="Step 3 of 4) ", filename='alternateNamesV2.txt', lang_list=self.lang_list)
def open_geodb(self, repair_database: bool, query_limit: int) -> bool: """ Open Geoname DB file - this is the db of geoname.org city files and is stored in cache directory under geonames_data. The db only contains important fields and only for supported countries. If the db doesn't exist and repair flag is True, read the geonames.org files and build DB. The DB has a version table for the schema version. If the schema changes, the version should be updated. This will check DB schema version and rebuild DB if version is out of date. # Args: repair_database: If True, rebuild database if error or missing Returns: True if error """ # Use db if it exists and has data and is correct version cache_dir = GeoUtil.get_cache_directory(self.directory) db_path = os.path.join(cache_dir, 'geodata.db') self.logger.debug(f'path for geodata.db: {db_path}') err_msg = '' # Validate Database setup if os.path.exists(db_path): # DB was Found self.logger.debug(f'DB found at {db_path}') self.geodb = GeoDB.GeoDB(db_path=db_path, show_message=self.show_message, exit_on_error=self.exit_on_error, set_speed_pragmas=True, db_limit=query_limit) # Make sure DB is correct version ver = self.geodb.get_db_version() if ver != self.required_db_version: # Bad DB version if ver == DB_REBUILDING: # DB didn't complete rebuild err_msg = f'Database only partially built. Deleting and will rebuild on next startup' else: # DB is out of date err_msg = f'Database version will be upgraded:\n\n{self.db_upgrade_text}\n\n' \ f'Upgrading database from V {ver} to V {self.required_db_version}.' self.geodb.close() os.remove(db_path) self.logger.info(err_msg) if self.show_message: messagebox.showinfo( 'Database Deleted. Will rebuild on start up', err_msg) sys.exit() else: err_msg = f'Database not found at\n\n{db_path}.\n\nBuilding DB' self.logger.debug(f'{err_msg}') if err_msg == '': pass # No DB errors detected #count = self.geodb.get_row_count() #self.logger.info(f'Geoname database has {count:,} entries\n' # f'------------------------------------------------------------\n') else: # DB error detected - rebuild database if flag set if self.show_message: messagebox.showinfo('Database Error', err_msg) self.logger.debug(err_msg) if repair_database: if os.path.exists(db_path): self.geodb.close() os.remove(db_path) self.logger.info('Database deleted') if self.show_message: messagebox.showinfo( 'Database Deleted. Will rebuild on start up', err_msg) self.geodb = GeoDB.GeoDB(db_path=db_path, show_message=self.show_message, exit_on_error=self.exit_on_error, set_speed_pragmas=True, db_limit=query_limit) return self.create_geonames_database() return False
def __init__(self, frame, title, dir_name, cache_filename, error): self.logger = logging.getLogger(__name__) self.file_error = True self.title = title self.frame = frame self.separator = ":" self.dirty_flag = False # Flag to track if data was modified self.error = error # Load in list from cache file self.directory = dir_name self.cache_dir = GeoUtil.get_cache_directory(dir_name) self.logger.debug( f'SetupStatusFrame dir={dir_name} sub_dir={self.cache_dir} file={cache_filename}' ) self.cache = CachedDictionary.CachedDictionary(self.cache_dir, cache_filename) self.cache.read() self.error_dict = {} # Keep a dictionary of errors self.supported_countries_cd = CachedDictionary.CachedDictionary( self.cache_dir, "country_list.pkl") self.supported_countries_cd.read() self.supported_countries_dct: Dict[ str, str] = self.supported_countries_cd.dict self.logger.debug( f'country list len={len(self.supported_countries_dct)}') self.grd = { "title_label": [0, 0, 5, 5, "W"], "scrollbar": [1, 2, 0, 5, "WNS"], "status": [0, 1, 5, 5, "W"], "add_button": [2, 4, 5, 5, "W"], "listbox": [0, 2, 5, 5, "E"], "unused": [2, 3, 5, 5, "W"], "add_entry": [0, 4, 5, 5, "W"], "load_button": [2, 1, 5, 5, "W"], "geoname_button": [2, 1, 5, 5, "E"], "add_label": [0, 3, 5, 5, "EW"] } self.title_label = Widge.CLabel(frame, text=self.title, width=80, style='Info.TLabel') self.status = Widge.CLabel(frame, text=" ", width=80, style='Highlight.TLabel') self.scrollbar = Scrollbar(frame) self.listbox = Listbox(frame, width=80, height=20, bg=AppStyle.LT_GRAY, selectmode=MULTIPLE, yscrollcommand=self.scrollbar.set) self.add_button = ttk.Button(frame, text="geonames.org", command=self.web_handler, width=12) # Configure buttons and widgets self.configure_widgets() #self.frame.columnconfigure(0, weight=5) #self.frame.columnconfigure(2, weight=2) #self.frame.rowconfigure(0, weight=2) #self.frame.rowconfigure(1, weight=2) # Display data self.load_handler()