Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import os 

2import shutil 

3import click 

4from dotenv import load_dotenv, find_dotenv 

5from gidappdata.utility.functions import pathmaker, create_folder, create_file, loadjson, appendwriteit 

6from gidappdata.utility.extended_dotenv import find_dotenv_everywhere 

7from gidappdata.cli.skeleton_tree import serialize_all_prebuilts, DirSkeletonReader, SkeletonInstructionItem, get_all_prebuilts 

8from pprint import pprint 

9from gidappdata.cli.tree_render import print_tree 

10from functools import partial 

11from textwrap import dedent 

12 

13 

14THIS_FILE_DIR = os.path.abspath(os.path.dirname(__file__)) 

15 

16SERIALIZED_DIR = pathmaker(DirSkeletonReader.serialized_prebuilts_folder) 

17 

18 

19def spec_appendwriteit(filepath, data): 

20 with open(filepath, 'a') as f: 

21 f.write(data + '\n') 

22 

23 

24def get_all_serialized_skeletons(): 

25 serialize_all_prebuilts() 

26 _out = {} 

27 for serialized_skeleton in os.scandir(SERIALIZED_DIR): 

28 if os.path.isfile(serialized_skeleton) and serialized_skeleton.name.endswith('.json'): 

29 _out[serialized_skeleton.name.replace('.json', '')] = loadjson(serialized_skeleton.path) 

30 return _out 

31 

32 

33def create_user_data_setup(path): 

34 user_data_setup_file = pathmaker(path, 'user_data_setup.py') 

35 with open(user_data_setup_file, 'w') as uds_file: 

36 uds_file.write(dedent("""from gidappdata import SupportKeeper 

37 from gidappdata.utility.extended_dotenv import find_dotenv_everywhere 

38 import os 

39 import dotenv 

40 from .bin_data import bin_archive_data 

41 dotenv.load_dotenv(find_dotenv_everywhere('project_meta_data.env')) 

42 

43 THIS_FILE_DIR = os.path.abspath(os.path.dirname(__file__)) 

44 DATA_DIR = os.path.join(THIS_FILE_DIR, 'data_pack') 

45 CONSTRUCTION_INFO_FILE = os.path.join(THIS_FILE_DIR, 'construction_info.env') 

46 

47 

48 if os.path.isfile(CONSTRUCTION_INFO_FILE): 

49 dotenv.load_dotenv(CONSTRUCTION_INFO_FILE) 

50 

51 if os.path.isfile('dev.trigger') is True: 

52 SupportKeeper.set_dev(True, DATA_DIR) 

53 SupportKeeper.set_archive_data(bin_archive_data) 

54 """)) 

55 

56 

57def create_dev_env_trigger(in_path): 

58 _path = pathmaker(in_path, 'dev.trigger') 

59 create_file(_path, '') 

60 

61 

62def select_skeleton(skeleton_selection, skeleton_category=None): 

63 category = 'standard' if skeleton_category is None else skeleton_category.casefold() 

64 selection = skeleton_selection.casefold() 

65 json_data = None 

66 all_serialized_skeletons = get_all_serialized_skeletons() 

67 for serialized_name in all_serialized_skeletons: 

68 if serialized_name.casefold() == f"[{category}]_{selection}": 

69 json_data = all_serialized_skeletons[serialized_name] 

70 if json_data is None: 

71 raise KeyError(f"unable to find serialized data for selection '{skeleton_selection}' and category '{category}'") 

72 return SkeletonInstructionItem.from_dict(json_data) 

73 

74 

75def build_target_skeleton(target_dir, skeleton_selection, skeleton_category, overwrite): 

76 path = pathmaker(target_dir, 'init_userdata') 

77 skeleton_tree = select_skeleton(skeleton_selection, skeleton_category) 

78 skeleton_tree.set_root_path(pathmaker(path, skeleton_tree.name)) 

79 skeleton_tree.start_build(overwrite=overwrite) 

80 create_file(pathmaker(path, '__init__.py'), '') 

81 create_dev_env_trigger(path) 

82 create_user_data_setup(path) 

83 

84 

85@click.group() 

86def cli(): 

87 pass 

88 

89 

90@cli.command(name='build') 

91@click.argument('target_dir') 

92@click.argument('skeleton_selection') 

93@click.option('-c', '--skeleton-category', default=None) 

94@click.option('--overwrite/--no-overwrite', '-o/-no', default=False) 

95def to_build_target_skeleton(target_dir, skeleton_selection, skeleton_category, overwrite): 

96 build_target_skeleton(target_dir, skeleton_selection, skeleton_category, overwrite) 

97 

98 

99@cli.command(name='list') 

100@click.option('--tree/--no-tree', '-t/-nt', default=True) 

101@click.option('-t', '--to-file', default=None) 

102def list_available(tree, to_file): 

103 all_prebuilts = get_all_prebuilts() 

104 output_func = print if to_file is None else partial(spec_appendwriteit, to_file) 

105 for category, value in all_prebuilts.items(): 

106 category = category.replace('prebuilt_', '') 

107 for name, path in value.items(): 

108 

109 output_func('\n\n#################################\n') 

110 output_func('Category: ' + category.upper() + ' --> ' + 'Name: ' + name) 

111 if tree: 

112 path = os.path.dirname(path) 

113 print_tree(path, category, output_function=output_func) 

114 output_func('\n#################################\n\n') 

115 

116 

117if __name__ == '__main__': 

118 cli()