filecache.py

changeset 145
fde18c4d6784
parent 95
a3536e51f6bc
equal deleted inserted replaced
144:eb4c767522ac 145:fde18c4d6784
38 self.flatness = None 38 self.flatness = None
39 self.description = None 39 self.description = None
40 self.problem = None 40 self.problem = None
41 self.has_studs = None # Whether or not it contains studs 41 self.has_studs = None # Whether or not it contains studs
42 self.vertices = set() 42 self.vertices = set()
43 def __init__(self, ldraw_directories): 43 def __init__(self, context):
44 ''' 44 '''
45 Initializes a new subfile cache 45 Initializes a new subfile cache
46 ''' 46 '''
47 self.cache = dict() 47 self.cache = dict()
48 if ldraw_directories and isinstance(ldraw_directories[0], str): 48 self.context = context
49 self.ldraw_directories = [
50 Path(os.path.expanduser(directory))
51 for directory in ldraw_directories
52 ]
53 else:
54 from copy import copy
55 self.ldraw_directories = copy(ldraw_directories)
56 self.reference_stack = [] 49 self.reference_stack = []
57 def flatness_of(self, filename): 50 def flatness_of(self, filename):
58 ''' 51 '''
59 Returns the set of all directiones the specified file is flat in. 52 Returns the set of all directiones the specified file is flat in.
60 ''' 53 '''
67 self.prepare_file(filename) 60 self.prepare_file(filename)
68 return self.cache[filename].description 61 return self.cache[filename].description
69 def find_file(self, filename): 62 def find_file(self, filename):
70 return find_ldraw_file( 63 return find_ldraw_file(
71 filename = filename, 64 filename = filename,
72 libraries = self.ldraw_directories, 65 libraries = self.context.libraries,
73 ) 66 )
74 def prepare_file(self, filename): 67 def prepare_file(self, filename):
75 ''' 68 '''
76 Loads the file if not loaded yet. 69 Loads the file if not loaded yet.
77 ''' 70 '''
88 subfile = SubfileCache.Subfile() 81 subfile = SubfileCache.Subfile()
89 self.cache[filename] = subfile 82 self.cache[filename] = subfile
90 try: 83 try:
91 path = self.find_file(filename) 84 path = self.find_file(filename)
92 with path.open('rb') as file: 85 with path.open('rb') as file:
93 model = parse.read_ldraw( 86 model = parse.read_ldraw(file, context = self.context)
94 file,
95 ldraw_directories = self.ldraw_directories,
96 )
97 except (FileNotFoundError, IOError, PermissionError) as error: 87 except (FileNotFoundError, IOError, PermissionError) as error:
98 subfile.valid = False 88 subfile.valid = False
99 subfile.problem = str(error) 89 subfile.problem = str(error)
100 else: 90 else:
101 if isinstance(model.body[0], linetypes.MetaCommand): 91 if isinstance(model.body[0], linetypes.MetaCommand):
127 self.cache[subfile_reference.subfile_path].has_studs 117 self.cache[subfile_reference.subfile_path].has_studs
128 or is_logo_stud_name(subfile_reference.subfile_path) 118 or is_logo_stud_name(subfile_reference.subfile_path)
129 for subfile_reference in model.subfile_references 119 for subfile_reference in model.subfile_references
130 ) 120 )
131 self.reference_stack.pop() 121 self.reference_stack.pop()
132
133 if __name__ == '__main__':
134 cache = SubfileCache(ldraw_directories = ["~/ldraw"])

mercurial