##// END OF EJS Templates
Make the directory name be the default relpath
Jessica B. Hamrick -
Show More
@@ -142,9 +142,10 b' class Exporter(LoggingConfigurable):'
142 resources = ResourcesDict()
142 resources = ResourcesDict()
143 if not 'metadata' in resources or resources['metadata'] == '':
143 if not 'metadata' in resources or resources['metadata'] == '':
144 resources['metadata'] = ResourcesDict()
144 resources['metadata'] = ResourcesDict()
145 basename = os.path.basename(filename)
145 path, basename = os.path.split(filename)
146 notebook_name = basename[:basename.rfind('.')]
146 notebook_name = basename[:basename.rfind('.')]
147 resources['metadata']['name'] = notebook_name
147 resources['metadata']['name'] = notebook_name
148 resources['metadata']['path'] = path
148
149
149 modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
150 modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
150 resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)
151 resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)
@@ -29,7 +29,9 b' class FilesWriter(WriterBase):'
29 "", config=True,
29 "", config=True,
30 help="""When copying files that the notebook depends on, copy them in
30 help="""When copying files that the notebook depends on, copy them in
31 relation to this path, such that the destination filename will be
31 relation to this path, such that the destination filename will be
32 os.path.relpath(filename, relpath).""")
32 os.path.relpath(filename, relpath). If FilesWriter is operating on a
33 notebook that already exists elsewhere on disk, then the default will be
34 the directory containing that notebook.""")
33
35
34
36
35 # Make sure that the output directory exists.
37 # Make sure that the output directory exists.
@@ -65,6 +67,12 b' class FilesWriter(WriterBase):'
65 # Pull the extension and subdir from the resources dict.
67 # Pull the extension and subdir from the resources dict.
66 output_extension = resources.get('output_extension', None)
68 output_extension = resources.get('output_extension', None)
67
69
70 # Get the relative path for copying files
71 if self.relpath == '':
72 relpath = resources.get('metadata', {}).get('path', '')
73 else:
74 relpath = self.relpath
75
68 # Write all of the extracted resources to the destination directory.
76 # Write all of the extracted resources to the destination directory.
69 # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG
77 # NOTE: WE WRITE EVERYTHING AS-IF IT'S BINARY. THE EXTRACT FIG
70 # PREPROCESSOR SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS...
78 # PREPROCESSOR SHOULD HANDLE UNIX/WINDOWS LINE ENDINGS...
@@ -88,8 +96,8 b' class FilesWriter(WriterBase):'
88 for matching_filename in glob.glob(filename):
96 for matching_filename in glob.glob(filename):
89
97
90 # compute the relative path for the filename
98 # compute the relative path for the filename
91 if self.relpath:
99 if relpath != '':
92 dest_filename = os.path.relpath(matching_filename, self.relpath)
100 dest_filename = os.path.relpath(matching_filename, relpath)
93 else:
101 else:
94 dest_filename = matching_filename
102 dest_filename = matching_filename
95
103
@@ -236,3 +236,72 b' class Testfiles(TestsBase):'
236 with open(dest, 'r') as f:
236 with open(dest, 'r') as f:
237 output = f.read()
237 output = f.read()
238 self.assertEqual(output, 'd')
238 self.assertEqual(output, 'd')
239
240 def test_relpath_default(self):
241 """Is the FilesWriter default relative path correct?"""
242
243 # Work in a temporary directory.
244 with self.create_temp_cwd():
245
246 # Create test file
247 os.mkdir('sub')
248 with open(os.path.join('sub', 'c'), 'w') as f:
249 f.write('d')
250
251 # Create the resoruces dictionary
252 res = dict(metadata=dict(path="sub"))
253
254 # Create files writer, test output
255 writer = FilesWriter()
256 writer.files = [os.path.join('sub', 'c')]
257 writer.build_directory = u'build'
258 writer.write(u'y', res, notebook_name="z")
259
260 # Check the output of the file
261 assert os.path.isdir(writer.build_directory)
262 dest = os.path.join(writer.build_directory, 'z')
263 with open(dest, 'r') as f:
264 output = f.read()
265 self.assertEqual(output, u'y')
266
267 # Check to make sure the linked file was copied
268 dest = os.path.join(writer.build_directory, 'c')
269 assert os.path.isfile(dest)
270 with open(dest, 'r') as f:
271 output = f.read()
272 self.assertEqual(output, 'd')
273
274 def test_relpath_default(self):
275 """Does the FilesWriter relpath option take precedence over the path?"""
276
277 # Work in a temporary directory.
278 with self.create_temp_cwd():
279
280 # Create test file
281 os.mkdir('sub')
282 with open(os.path.join('sub', 'c'), 'w') as f:
283 f.write('d')
284
285 # Create the resoruces dictionary
286 res = dict(metadata=dict(path="other_sub"))
287
288 # Create files writer, test output
289 writer = FilesWriter()
290 writer.files = [os.path.join('sub', 'c')]
291 writer.build_directory = u'build'
292 writer.relpath = 'sub'
293 writer.write(u'y', res, notebook_name="z")
294
295 # Check the output of the file
296 assert os.path.isdir(writer.build_directory)
297 dest = os.path.join(writer.build_directory, 'z')
298 with open(dest, 'r') as f:
299 output = f.read()
300 self.assertEqual(output, u'y')
301
302 # Check to make sure the linked file was copied
303 dest = os.path.join(writer.build_directory, 'c')
304 assert os.path.isfile(dest)
305 with open(dest, 'r') as f:
306 output = f.read()
307 self.assertEqual(output, 'd')
General Comments 0
You need to be logged in to leave comments. Login now