def get_config(): """ get the configuration from config.yml, create it if not there """ data_dir = get_data_dir() try: config = yaml.load(open(get_config_file_path(), 'r'), Loader=yaml.SafeLoader) return config except (OSError, yaml.YAMLError, IOError, EnvironmentError) as e: return {'parts.lst': join(data_dir, 'ldraw', 'parts.lst')}
def try_download_generate_lib(): # Download the library and generate it, if needed config = get_config() parts_lst_path = config['parts.lst'] output_dir = os.path.dirname(parts_lst_path) if not os.path.exists(output_dir) and not os.path.exists(parts_lst_path): download(output_dir) data_dir = get_data_dir() library_path = config.get('library') if library_path is not None: generate(parts_lst_path, library_path) return library_path else: try: # try to write the library to ldraw package folder (can work if user-writeable) ldraw_path = os.path.abspath(os.path.dirname(__file__)) generate(parts_lst_path, ldraw_path) return ldraw_path except (OSError, IOError): # Failed, then write it to data_dir generate(parts_lst_path, data_dir) return data_dir
def test_config_cant_load(yaml_load_mock): yaml_load_mock.side_effect = fails expected = os.path.join(get_data_dir(), 'ldraw', 'parts.lst') assert get_config()['parts.lst'] == expected
#!/usr/bin/env python import os from ldraw.dirs import get_data_dir from ldraw import generate, download # useful for autocompletion in some IDEs output_dir = os.path.join(get_data_dir(), 'ldraw') parts_lst = os.path.join(output_dir, 'parts.lst') if not os.path.exists(output_dir): download(output_dir) generate(parts_lst, 'ldraw', force=True)