@@ -25,14 +25,10 @@ class Error(Exception):
2525class Renderer :
2626 """ Template renderer using mustache templates in the `templates` directory """
2727
28- def __init__ (self , generator : pathlib .Path , root_dir : pathlib . Path ):
28+ def __init__ (self , generator : pathlib .Path ):
2929 self ._r = pystache .Renderer (search_dirs = str (paths .templates_dir ), escape = lambda u : u )
30- self ._root_dir = root_dir
3130 self ._generator = generator
3231
33- def _get_path (self , file : pathlib .Path ):
34- return file .relative_to (self ._root_dir )
35-
3632 def render (self , data : object , output : pathlib .Path ):
3733 """ Render `data` to `output`.
3834
@@ -60,7 +56,7 @@ def _do_write(self, mnemonic: str, contents: str, output: pathlib.Path):
6056
6157 def manage (self , generated : typing .Iterable [pathlib .Path ], stubs : typing .Iterable [pathlib .Path ],
6258 registry : pathlib .Path , force : bool = False ) -> "RenderManager" :
63- return RenderManager (self ._generator , self . _root_dir , generated , stubs , registry , force )
59+ return RenderManager (self ._generator , generated , stubs , registry , force )
6460
6561
6662class RenderManager (Renderer ):
@@ -85,10 +81,10 @@ class Hashes:
8581 pre : str
8682 post : typing .Optional [str ] = None
8783
88- def __init__ (self , generator : pathlib .Path , root_dir : pathlib . Path , generated : typing .Iterable [pathlib .Path ],
84+ def __init__ (self , generator : pathlib .Path , generated : typing .Iterable [pathlib .Path ],
8985 stubs : typing .Iterable [pathlib .Path ],
9086 registry : pathlib .Path , force : bool = False ):
91- super ().__init__ (generator , root_dir )
87+ super ().__init__ (generator )
9288 self ._registry_path = registry
9389 self ._force = force
9490 self ._hashes = {}
@@ -117,10 +113,13 @@ def __exit__(self, exc_type, exc_val, exc_tb):
117113 self ._hashes .pop (self ._get_path (f ), None )
118114 # clean up the registry from files that do not exist any more
119115 for f in list (self ._hashes ):
120- if not (self ._root_dir / f ).exists ():
116+ if not (self ._registry_path . parent / f ).exists ():
121117 self ._hashes .pop (f )
122118 self ._dump_registry ()
123119
120+ def _get_path (self , file : pathlib .Path ):
121+ return file .relative_to (self ._registry_path .parent )
122+
124123 def _do_write (self , mnemonic : str , contents : str , output : pathlib .Path ):
125124 hash = self ._hash_string (contents )
126125 rel_output = self ._get_path (output )
@@ -186,13 +185,17 @@ def _load_registry(self):
186185 try :
187186 with open (self ._registry_path ) as reg :
188187 for line in reg :
189- filename , prehash , posthash = line .split ()
190- self ._hashes [pathlib .Path (filename )] = self .Hashes (prehash , posthash )
188+ if line .strip ():
189+ filename , prehash , posthash = line .split ()
190+ self ._hashes [pathlib .Path (filename )] = self .Hashes (prehash , posthash )
191191 except FileNotFoundError :
192192 pass
193193
194194 def _dump_registry (self ):
195195 self ._registry_path .parent .mkdir (parents = True , exist_ok = True )
196- with open (self ._registry_path , 'w' ) as out :
196+ with open (self ._registry_path , 'w' ) as out , open (self ._registry_path .parent / ".gitattributes" , "w" ) as attrs :
197+ print (f"/{ self ._registry_path .name } " , "linguist-generated" , file = attrs )
198+ print ("/.gitattributes" , "linguist-generated" , file = attrs )
197199 for f , hashes in sorted (self ._hashes .items ()):
198200 print (f , hashes .pre , hashes .post , file = out )
201+ print (f"/{ f } " , "linguist-generated" , file = attrs )
0 commit comments