22import os
33import shutil
44import sys
5- import time
65from unittest import TestCase , expectedFailure
76
87from colosseum .constants import BLOCK , HTML4 , MEDIUM , THICK , THIN
@@ -133,31 +132,60 @@ def clean_layout(layout):
133132def output_layout (layout , depth = 1 ):
134133 if 'tag' in layout :
135134 return (' ' * depth
136- + '* {tag}{n[content][size][0]}x{n[content][size][1]}'
137- ' @ ({n[content][position][0]}, {n[content][position][1]})'
138- '\n ' .format (
139- n = layout ,
140- tag = ('<' + layout ['tag' ] + '> ' ) if 'tag' in layout else '' ,
141- # text=(": '" + layout['text'] + "'") if 'text' in layout else ''
142- )
143- # + ' ' * depth
144- # + ' padding: {n[padding_box][size][0]}x{n[padding_box][size][1]}'
145- # ' @ ({n[padding_box][position][0]}, {n[padding_box][position][1]})'
146- # '\n'.format(n=layout)
147- # + ' ' * depth
148- # + ' border: {n[border_box][size][0]}x{n[border_box][size][1]}'
149- # ' @ ({n[border_box][position][0]}, {n[border_box][position][1]})'
150- # '\n'.format(n=layout)
151- + '' .join (
152- output_layout (child , depth = depth + 1 )
153- for child in layout .get ('children' , [])
154- ) if layout else ''
155- + ('\n ' if layout and layout .get ('children' , None ) and depth > 1 else '' )
156- )
135+ + '* {tag}{n[content][size][0]}x{n[content][size][1]}'
136+ ' @ ({n[content][position][0]}, {n[content][position][1]})'
137+ '\n ' .format (
138+ n = layout ,
139+ tag = ('<' + layout ['tag' ] + '> ' ) if 'tag' in layout else '' ,
140+ # text=(": '" + layout['text'] + "'") if 'text' in layout else ''
141+ )
142+ # + ' ' * depth
143+ # + ' padding: {n[padding_box][size][0]}x{n[padding_box][size][1]}'
144+ # ' @ ({n[padding_box][position][0]}, {n[padding_box][position][1]})'
145+ # '\n'.format(n=layout)
146+ # + ' ' * depth
147+ # + ' border: {n[border_box][size][0]}x{n[border_box][size][1]}'
148+ # ' @ ({n[border_box][position][0]}, {n[border_box][position][1]})'
149+ # '\n'.format(n=layout)
150+ + '' .join (
151+ output_layout (child , depth = depth + 1 )
152+ for child in layout .get ('children' , [])
153+ ) if layout else ''
154+ + ('\n ' if layout and layout .get ('children' , None ) and depth > 1 else '' ))
157155 else :
158- return (' ' * depth
159- + "* '{text}'\n " .format (text = layout ['text' ].strip ())
160- )
156+ return (' ' * depth + "* '{text}'\n " .format (text = layout ['text' ].strip ()))
157+
158+
159+ def fonts_path (system = False ):
160+ """Return the path for cross platform user fonts."""
161+ if os .name == 'nt' :
162+ import winreg
163+ fonts_dir = os .path .join (winreg .ExpandEnvironmentStrings ('%windir%' ), 'fonts' )
164+ elif sys .platform == 'darwin' :
165+ if system :
166+ fonts_dir = os .path .expanduser ('/Library/Fonts' )
167+ else :
168+ fonts_dir = os .path .expanduser ('~/Library/Fonts' )
169+ elif sys .platform .startswith ('linux' ):
170+ fonts_dir = os .path .expanduser ('~/.local/share/fonts/' )
171+ else :
172+ raise NotImplementedError ('System not supported!' )
173+
174+ return fonts_dir
175+
176+
177+ def copy_fonts (system = False ):
178+ """Copy needed files for running tests."""
179+ fonts_folder = fonts_path (system = system )
180+ if not os .path .isdir (fonts_folder ):
181+ os .makedirs (fonts_folder )
182+ fonts_data_path = os .path .join (HERE , 'data' , 'fonts' )
183+ font_files = sorted ([item for item in os .listdir (fonts_data_path ) if item .endswith ('.ttf' )])
184+ for font_file in font_files :
185+ font_file_data_path = os .path .join (fonts_data_path , font_file )
186+ font_file_path = os .path .join (fonts_folder , font_file )
187+ if not os .path .isfile (font_file_path ):
188+ shutil .copyfile (font_file_data_path , font_file_path )
161189
162190
163191class ColosseumTestCase (TestCase ):
@@ -167,30 +195,8 @@ def __init__(self, *args, **kwargs):
167195 super ().__init__ (* args , ** kwargs )
168196 self .copy_fonts ()
169197
170- def fonts_path (self ):
171- """Return the path for cross platform user fonts."""
172- if os .name == 'nt' :
173- import winreg
174- fonts_dir = os .path .join (winreg .ExpandEnvironmentStrings ('%windir%' ), 'fonts' )
175- elif sys .platform == 'darwin' :
176- fonts_dir = os .path .expanduser ('~/Library/Fonts' )
177- elif sys .platform .startswith ('linux' ):
178- fonts_dir = os .path .expanduser ('~/.local/share/fonts/' )
179- else :
180- raise NotImplementedError ('System not supported!' )
181-
182- return fonts_dir
183-
184198 def copy_fonts (self ):
185- """Copy needed files for running tests."""
186- fonts_path = self .fonts_path ()
187- fonts_data_path = os .path .join (HERE , 'data' , 'fonts' )
188- font_files = sorted ([item for item in os .listdir (fonts_data_path ) if item .endswith ('.ttf' )])
189- for font_file in font_files :
190- font_file_data_path = os .path .join (fonts_data_path , font_file )
191- font_file_path = os .path .join (fonts_path , font_file )
192- if not os .path .isfile (font_file_path ):
193- shutil .copyfile (font_file_data_path , font_file_path )
199+ copy_fonts ()
194200
195201 try :
196202 FontDatabase .validate_font_family ('Ahem' )
@@ -442,3 +448,10 @@ def test_method(self):
442448 tests [test_name ] = test_method
443449
444450 return tests
451+
452+
453+ if __name__ == '__main__' :
454+ print ('Copying test fonts...' )
455+ print (fonts_path (system = True ))
456+ copy_fonts (system = True )
457+ print (list (sorted (os .listdir (fonts_path ()))))
0 commit comments