def generate(parts_lst, output_dir, force=False): """ main function for the library generation """ library_path = os.path.join(output_dir, 'library') ensure_exists(library_path) hash_path = os.path.join(library_path, '__hash__') md5_parts_lst = hashlib.md5(open(parts_lst, 'rb').read()).hexdigest() if os.path.exists(hash_path): md5 = open(hash_path, 'r').read() if md5 == md5_parts_lst and not force: return parts = Parts(parts_lst) library__init__ = os.path.join(library_path, '__init__.py') with open(library__init__, 'w') as library__init__: library__init__.write(LIBRARY_INIT) #shutil.copy('ldraw-license.txt', os.path.join(library_path, 'license.txt')) gen_colours(parts, output_dir) gen_parts(parts, output_dir) open(hash_path, 'w').write(md5_parts_lst)
def write_section_file(parts_dir, list_of_parts, mod_path): """ Writes a single section files""" list_of_parts.sort(key=lambda o: o['description']) part_str = pystache.render(PARTS_TEMPLATE, context={'parts': list_of_parts}) parts_py = os.path.join(parts_dir, mod_path) ensure_exists(os.path.dirname(parts_py)) with codecs.open(parts_py, 'w', encoding='utf-8') as generated_file: generated_file.write(part_str)
def generate_parts__init__(library_path, modules, package_name): """ generate the appropriate __init__.py to make submodules in ldraw.library.parts """ parts__init__str = pystache.render(PARTS__INIT__TEMPLATE, context={'sections': modules}) if package_name == '': parts__init__ = os.path.join(library_path, 'parts', '__init__.py') else: parts__init__ = os.path.join(library_path, 'parts', package_name, '__init__.py') ensure_exists(os.path.dirname(parts__init__)) with codecs.open(parts__init__, 'w', encoding='utf-8') as parts__init__file: parts__init__file.write(parts__init__str)
def gen_parts(parts, output_dir): """ Generates the ldraw.library.parts namespace :param parts: Parts object :param output_dir: where to output the library :return: """ print('generate ldraw.library.parts, this might take a long time...') library_path = os.path.join(output_dir, 'library') parts_dir = ensure_exists(os.path.join(library_path, 'parts')) sections = _get_sections(parts) packages = _get_packages(sections) for package_name, modules in packages.items(): generate_parts__init__(library_path, modules, package_name) for section_name, section_parts in sections.items(): generate_section(parts, parts_dir, section_name, section_parts)
def download(output_dir): """ download complete.zip, mklist, main function""" tmp_ldraw = get_cache_dir() parts_lst_path = os.path.join(output_dir, 'parts.lst') retrieved = os.path.join(tmp_ldraw, "complete.zip") print('retrieve the complete.zip from ldraw.org ...') urlretrieve(LDRAW_URL, filename=retrieved) print('unzipping the complete.zip ...') zip_ref = zipfile.ZipFile(retrieved, 'r') zip_ref.extractall(tmp_ldraw) zip_ref.close() output_dir = ensure_exists(output_dir) copy_tree(os.path.join(tmp_ldraw, 'ldraw'), os.path.join(output_dir)) print('mklist...') generate_parts_lst('description', os.path.join(output_dir, 'parts'), parts_lst_path)
def get_data_dir(): """ get the directory where to put some data """ return ensure_exists(appdirs.user_data_dir(PYLDRAW))
def get_cache_dir(): return ensure_exists(appdirs.user_cache_dir(PYLDRAW))
def get_config_dir(): """ get the directory where the config is """ return ensure_exists(appdirs.user_config_dir(PYLDRAW))