def call_local(self): ''' sort out and return local_file This comes from the URL and local_dir and ends .store ''' if self.indb(): if callable(self.local): sys.msg(f"**unexpected method for self.local {self.local}") else: return self.local kwargs = fdict(self.__dict__.copy()) if 'local_dir' in kwargs and \ (kwargs['local_dir'] is not None) and \ len(kwargs['local_dir']) > 0: self.local_dir = list_resolve(kwargs['local_dir']) if (self.local_dir is None) or (len(self.local_dir) == 0): self.local_dir = list_resolve(self.db_dir) self.local_file = Path(self.local_dir[0],self.as_posix().split("://")[1]) #self.local_file = Path(self.local_dir[-1],str(self.with_scheme(''))[2:]).absolute() # replace ' ' self.local_file = Path(str(self.local_file).replace(' ','_')) suffix = self.local_file.suffix self.local_file = self.local_file.with_suffix(suffix + '.store') self.check_path(self.local_file.parent) self.local_file.parent.mkdir(parents=True,exist_ok=True) return self.local_file
def call_db(self): '''deal with call to access db and setup if needed''' if len(self.db_dir) == 0: self.db_dir = list_resolve(Path.home() / '.url_db') self.msg(f'setting db_dir {self.db_dir}') if not self.db_file: self.db_file = name_resolve([f / '.db.yml' for f in self.db_dir]) self.msg(f'setting db_file {self.db_file}') self.set_db({}) return self.db_file
def call_local(self): ''' sort out and return local_file This comes from the URL and local_dir and ends .store ''' kwargs = fdict(self.__dict__.copy()) if 'local_dir' in kwargs and \ (kwargs['local_dir'] is not None) and \ len(kwargs['local_dir']) > 0: self.local_dir = list_resolve(kwargs['local_dir']) if (self.local_dir is None) or (len(self.local_dir) == 0): self.local_dir = list_resolve(self.db_dir) self.local_file = Path(self.local_dir[-1], str(self.with_scheme(''))[2:]).absolute() # replace ' ' self.local_file = Path(str(self.local_file).replace(' ', '_')) suffix = self.local_file.suffix self.local_file = self.local_file.with_suffix(suffix + '.store') self.check_path(self.local_file.parent) self.local_file.parent.mkdir(parents=True, exist_ok=True) return self.local_file
def __init__(self, args, **kwargs): ''' kwargs setup and organisation of local_dir and db_dir args are database files ''' defaults = {\ 'verbose' : False,\ 'db_dir' : list_resolve(['~/.url_db']),\ 'db_file' : None,\ 'log' : None,\ 'database' : None,\ 'stderr' : sys.stderr,\ } defaults.update(kwargs) old_db = defaults['database'] self.__dict__.update(defaults) if ('database' in self.__dict__) and (type(self.database) is Database): try: print("WARNING: shouldnt be here ... ") this = self.database.__dict__ # in case database object passed self.__dict__.update(fdict(this)) if type(old_db) is dict: self.database.update(old_db) except: pass if self.log is not None: try: self.stderr = Path(self.log).open("a") if self.verbose: try: #msg = f"database: log file {self.log}" self.store_msg.append(msg) print(msg, file=sys.stderr) except: pass except: self.stderr = sys.stderr self.msg(f"WARNING: failure to open log file {self.log}") if type(self.db_file) is str: self.db_file = [self.db_file] # database files if (self.db_file is None): self.db_file = args #else: # if type(self.db_file) is not list: # self.db_file = [self.db_file] # self.db_file.append(args) if (self.db_file is not None) and type(self.db_file) is not list: self.db_file = [self.db_file] if (self.db_dir is not None) and type(self.db_dir) is not list: self.db_dir = [self.db_dir] # may be a cache if 'CACHE_FILE' in os.environ and os.environ['CACHE_FILE'] is not None: db_file = [str(l) for l in list_resolve(os.environ['CACHE_FILE'])] self.msg(f'using cache {db_file}') if (self.db_file is None): self.db_file = db_file else: self.db_file = list_resolve(self.db_file + db_file) # in case still none if (self.db_file is None) or \ ((type(self.db_file) is list) and len(self.db_file) == 0): # in case self.db_dir is none if (self.db_dir is None) or \ ((type(self.db_dir) is list) and len(self.db_dir) == 0): self.db_dir = list_resolve([Path('~', '.url_db')]) self.db_file = [Path(d, '.db.yml') for d in self.db_dir] if type(self.db_file) is str: self.db_file = [self.db_file] self.db_file = list_resolve([Path(f) for f in self.db_file]) self.db_dir = [Path(d).parent for d in self.db_file] if self.database and (len(self.database.keys())): self.msg('getting database from command line') else: self.database = self.set_db(dict(self.get_db())) self.init_database = self.database.copy()
def __init__(self, args, **kwargs): ''' kwargs setup and organisation of local_dir and db_dir args are database files ''' defaults = {\ 'verbose' : False,\ 'db_dir' : None,\ 'db_file' : None,\ 'log' : None,\ 'database' : None,\ 'stderr' : sys.stderr,\ } # try to read from ~/.url_db/.init initfile = Path('~/.url_db/init.yml').expanduser().absolute() if initfile.exists(): #self.msg(f'reading init file {initfile.as_posix()}') with initfile.open('r') as f: info = yaml.safe_load(f) else: info = {} defaults.update(info) defaults.update(kwargs) old_db = defaults['database'] self.__dict__.update(defaults) if ('database' in self.__dict__) and (type(self.database) is Database): try: print("WARNING: shouldnt be here ... ") this = self.database.__dict__ # in case database object passed self.__dict__.update(fdict(this)) if type(old_db) is dict: self.database.update(old_db) except: pass if self.log is not None: try: self.stderr = Path(self.log).open("a") if self.verbose: try: #msg = f"database: log file {self.log}" self.store_msg.append(msg) print(msg, file=sys.stderr) except: pass except: self.stderr = sys.stderr self.msg(f"WARNING: failure to open log file {self.log}") if type(self.db_file) is str: self.db_file = [self.db_file] # database files if (self.db_file is None): self.db_file = args #else: # if type(self.db_file) is not list: # self.db_file = [self.db_file] # self.db_file.append(args) if (self.db_file is not None) and type(self.db_file) is not list: self.db_file = [self.db_file] if (self.db_dir is not None) and type(self.db_dir) is not list: self.db_dir = [self.db_dir] # may be a cache #cache=Path("/shared/groups/jrole001/geog0111/work/database.db") #if cache.exists(): # cache = cache.as_posix() # self.msg(f'using cache {cache}') # if "db_file" not in self.__dict__: # self.db_file = cache # # if (self.db_file is None): # self.db_file = cache # else: # self.db_file = list_resolve([cache] + self.db_file) if info == {} and 'CACHE_FILE' in os.environ and os.environ[ 'CACHE_FILE'] is not None: db_file = [str(l) for l in list_resolve(os.environ['CACHE_FILE'])] #self.msg(f'using cache {db_file}') if (self.db_file is None): self.db_file = db_file else: self.db_file = list_resolve(self.db_file + db_file) if ((type(self.db_dir) is list) and len(self.db_dir) == 0): self.db_dir = None if ((type(self.db_file) is list) and len(self.db_file) == 0): self.db_file = None if type(self.db_file) is str: self.db_file = [self.db_file] if type(self.db_dir) is str: self.db_dir = [self.db_dir] # writeable db_files if (self.db_file is not None): # ie we apparently have something can_write = False for d in self.db_file: try: Path(d).touch() can_write = True except: pass # in case still none or no writeable if (not can_write) or (self.db_file is None): # in case self.db_dir is none if (self.db_dir is None): self.db_dir = list_resolve([Path('~', '.url_db')]) if (self.db_file is None): self.db_file = [Path(d, '.db.yml') for d in self.db_dir] else: self.db_file.extend([Path(d, '.db.yml') for d in self.db_dir]) self.db_file = list_resolve([Path(f) for f in self.db_file]) self.db_dir = [Path(d).parent for d in self.db_file] if self.database and (len(self.database.keys())): self.msg('getting database from command line') else: self.database = self.set_db(dict(self.get_db())) self.init_database = self.database.copy()