glenn-jocher commited on
Commit
cd810c8
1 Parent(s): 22ee6fb

Centralize `user_config_dir()` decision making (#4755)

Browse files
Files changed (2) hide show
  1. utils/general.py +9 -5
  2. utils/plots.py +1 -2
utils/general.py CHANGED
@@ -103,11 +103,15 @@ def get_latest_run(search_dir='.'):
103
  return max(last_list, key=os.path.getctime) if last_list else ''
104
 
105
 
106
- def user_config_dir(dir='Ultralytics'):
107
- # Return path of user configuration directory (make if necessary)
108
- cfg = {'Windows': 'AppData/Roaming', 'Linux': '.config', 'Darwin': 'Library/Application Support'} # 3 config dirs
109
- path = Path.home() / cfg.get(platform.system(), '') # OS-specific config dir
110
- path = (path if is_writeable(path) else Path('/tmp')) / dir # GCP and AWS lambda fix, only /tmp is writeable
 
 
 
 
111
  path.mkdir(exist_ok=True) # make if required
112
  return path
113
 
 
103
  return max(last_list, key=os.path.getctime) if last_list else ''
104
 
105
 
106
+ def user_config_dir(dir='Ultralytics', env_var='YOLOV5_CONFIG_DIR'):
107
+ # Return path of user configuration directory. Prefer environment variable if exists. Make dir if required.
108
+ env = os.getenv(env_var)
109
+ if env:
110
+ path = Path(env) # use environment variable
111
+ else:
112
+ cfg = {'Windows': 'AppData/Roaming', 'Linux': '.config', 'Darwin': 'Library/Application Support'} # 3 OS dirs
113
+ path = Path.home() / cfg.get(platform.system(), '') # OS-specific config dir
114
+ path = (path if is_writeable(path) else Path('/tmp')) / dir # GCP and AWS lambda fix, only /tmp is writeable
115
  path.mkdir(exist_ok=True) # make if required
116
  return path
117
 
utils/plots.py CHANGED
@@ -4,7 +4,6 @@ Plotting utils
4
  """
5
 
6
  import math
7
- import os
8
  from copy import copy
9
  from pathlib import Path
10
 
@@ -21,7 +20,7 @@ from utils.general import user_config_dir, is_ascii, xywh2xyxy, xyxy2xywh
21
  from utils.metrics import fitness
22
 
23
  # Settings
24
- CONFIG_DIR = Path(os.getenv('YOLOV5_CONFIG_DIR') or user_config_dir()) # Ultralytics settings dir
25
  matplotlib.rc('font', **{'size': 11})
26
  matplotlib.use('Agg') # for writing to files only
27
 
 
4
  """
5
 
6
  import math
 
7
  from copy import copy
8
  from pathlib import Path
9
 
 
20
  from utils.metrics import fitness
21
 
22
  # Settings
23
+ CONFIG_DIR = user_config_dir() # Ultralytics settings dir
24
  matplotlib.rc('font', **{'size': 11})
25
  matplotlib.use('Agg') # for writing to files only
26