Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/scripts/wrap/util.py @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 ''' | |
| 2 Utility fns. | |
| 3 ''' | |
| 4 | |
| 5 import os | |
| 6 | |
| 7 import jlib | |
| 8 | |
| 9 def clip( text, prefixes, suffixes=''): | |
| 10 ''' | |
| 11 Returns <text> with prefix(s) and suffix(s) removed if present. | |
| 12 ''' | |
| 13 if isinstance( prefixes, str): | |
| 14 prefixes = prefixes, | |
| 15 if isinstance( suffixes, str): | |
| 16 suffixes = suffixes, | |
| 17 while 1: | |
| 18 for prefix in prefixes: | |
| 19 if text.startswith( prefix): | |
| 20 text = text[ len( prefix):] | |
| 21 break | |
| 22 else: | |
| 23 break | |
| 24 while 1: | |
| 25 for suffix in suffixes: | |
| 26 if suffix and text.endswith( suffix): | |
| 27 text = text[ :-len( suffix)] | |
| 28 break | |
| 29 else: | |
| 30 break | |
| 31 return text | |
| 32 | |
| 33 | |
| 34 def update_file_regress( text, filename, check_regression): | |
| 35 ''' | |
| 36 Behaves like jlib.fs_update(), but if check_regression is true and | |
| 37 <filename> already exists with different content from <text>, we show a | |
| 38 diff and return an exception. | |
| 39 ''' | |
| 40 text_old = jlib.fs_update( text, filename, check_regression) | |
| 41 if check_regression: | |
| 42 if text_old is not None: | |
| 43 # Existing content differs and <check_regression> is true. | |
| 44 with open( f'{filename}-2', 'w') as f: | |
| 45 f.write( text) | |
| 46 message = f'File would have changed: {os.path.relpath(filename)}' | |
| 47 jlib.log( message) | |
| 48 jlib.system( f'diff -u {filename} {filename}-2', verbose=True, raise_errors=False, prefix=f'diff {os.path.relpath(filename)}: ', out='log') | |
| 49 return Exception( message) | |
| 50 return text_old |
