@@ -25,14 +25,10 @@ class Error(Exception):
25
25
class Renderer :
26
26
""" Template renderer using mustache templates in the `templates` directory """
27
27
28
- def __init__ (self , generator : pathlib .Path , root_dir : pathlib . Path ):
28
+ def __init__ (self , generator : pathlib .Path ):
29
29
self ._r = pystache .Renderer (search_dirs = str (paths .templates_dir ), escape = lambda u : u )
30
- self ._root_dir = root_dir
31
30
self ._generator = generator
32
31
33
- def _get_path (self , file : pathlib .Path ):
34
- return file .relative_to (self ._root_dir )
35
-
36
32
def render (self , data : object , output : pathlib .Path ):
37
33
""" Render `data` to `output`.
38
34
@@ -60,7 +56,7 @@ def _do_write(self, mnemonic: str, contents: str, output: pathlib.Path):
60
56
61
57
def manage (self , generated : typing .Iterable [pathlib .Path ], stubs : typing .Iterable [pathlib .Path ],
62
58
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 )
64
60
65
61
66
62
class RenderManager (Renderer ):
@@ -85,10 +81,10 @@ class Hashes:
85
81
pre : str
86
82
post : typing .Optional [str ] = None
87
83
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 ],
89
85
stubs : typing .Iterable [pathlib .Path ],
90
86
registry : pathlib .Path , force : bool = False ):
91
- super ().__init__ (generator , root_dir )
87
+ super ().__init__ (generator )
92
88
self ._registry_path = registry
93
89
self ._force = force
94
90
self ._hashes = {}
@@ -117,10 +113,13 @@ def __exit__(self, exc_type, exc_val, exc_tb):
117
113
self ._hashes .pop (self ._get_path (f ), None )
118
114
# clean up the registry from files that do not exist any more
119
115
for f in list (self ._hashes ):
120
- if not (self ._root_dir / f ).exists ():
116
+ if not (self ._registry_path . parent / f ).exists ():
121
117
self ._hashes .pop (f )
122
118
self ._dump_registry ()
123
119
120
+ def _get_path (self , file : pathlib .Path ):
121
+ return file .relative_to (self ._registry_path .parent )
122
+
124
123
def _do_write (self , mnemonic : str , contents : str , output : pathlib .Path ):
125
124
hash = self ._hash_string (contents )
126
125
rel_output = self ._get_path (output )
@@ -186,13 +185,17 @@ def _load_registry(self):
186
185
try :
187
186
with open (self ._registry_path ) as reg :
188
187
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 )
191
191
except FileNotFoundError :
192
192
pass
193
193
194
194
def _dump_registry (self ):
195
195
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 )
197
199
for f , hashes in sorted (self ._hashes .items ()):
198
200
print (f , hashes .pre , hashes .post , file = out )
201
+ print (f"/{ f } " , "linguist-generated" , file = attrs )
0 commit comments