fix paths with data-dir

This commit is contained in:
Vladimir Mandic
2023-09-13 16:10:29 -04:00
parent 287d46748e
commit af672d10eb
2 changed files with 17 additions and 14 deletions
+5 -3
View File
@@ -62,10 +62,12 @@ def create_paths(opts, log=None):
def fix_path(folder):
tgt = opts.data.get(folder, None) or opts.data_labels[folder].default
if tgt is None or tgt == '':
return
if os.path.isabs(tgt) or (len(data_path) > 0 and tgt.startswith(data_path)) and not tgt.startswith(script_path):
return
return
if len(data_path) > 0 and tgt.startswith(data_path): # path is already relative to data_path
return tgt
fullpath = os.path.join(data_path, tgt)
if len(data_path) > 0 and os.path.isabs(data_path):
return fullpath
relpath = os.path.relpath(fullpath, script_path)
opts.data[folder] = relpath
return relpath
+12 -11
View File
@@ -41,22 +41,19 @@ class StyleDatabase:
def __init__(self, opts):
self.no_style = Style("None")
self.styles = {}
self.path = opts.styles_dir
if os.path.isfile(opts.styles_dir):
if os.path.isfile(opts.styles_dir) or opts.styles_dir.endswith(".csv"):
legacy_file = opts.styles_dir
self.load_csv(legacy_file)
opts.styles_dir = os.path.join(paths.models_path, "styles")
self.path = opts.styles_dir
self.mkdir()
os.makedirs(opts.styles_dir, exist_ok=True)
self.save_styles(opts.styles_dir, verbose=True)
log.debug(f'Migrated styles: file={legacy_file} folder={self.path}')
log.debug(f'Migrated styles: file={legacy_file} folder={opts.styles_dir}')
self.reload()
self.mkdir()
def mkdir(self):
if not os.path.isdir(self.path):
os.makedirs(self.path, exist_ok=True)
log.debug(f'Created styles: folder={self.path}')
if not os.path.isdir(opts.styles_dir):
opts.styles_dir = os.path.join(paths.models_path, "styles")
self.path = opts.styles_dir
os.makedirs(opts.styles_dir, exist_ok=True)
def reload(self):
self.styles.clear()
@@ -108,9 +105,13 @@ class StyleDatabase:
log.debug(f'Saved style: name={name} file={fn}')
except Exception as e:
log.error(f'Failed to save style: name={name} file={path} error={e}')
log.debug(f'Saved styles: {path} {len(self.styles.keys())}')
count = len(list(self.styles))
if count > 0:
log.debug(f'Saved styles: {path} {count}')
def load_csv(self, legacy_file):
if not os.path.isfile(legacy_file):
return
with open(legacy_file, "r", encoding="utf-8-sig", newline='') as file:
reader = csv.DictReader(file, skipinitialspace=True)
for row in reader: