1
- import shlex
2
1
import shutil
3
2
from pathlib import Path
4
3
from textwrap import indent
@@ -22,20 +21,34 @@ def paragraph_ctx(txt):
22
21
file .write (f"\n # -- End { txt } --" )
23
22
return paragraph_ctx
24
23
24
+ PRIVATE_PATHS = [(str (Path .home ()), "~" )]
25
+ if Path .cwd () != Path ("/" ): # don't strip unix root
26
+ PRIVATE_PATHS += [(str (Path .cwd ()), "." )]
27
+ # sort descending by length to avoid interference
28
+ PRIVATE_PATHS .sort (key = lambda x : len (x [0 ]), reverse = True )
29
+
30
+ def txtpath (s ):
31
+ # Returns a path string suitable for embedding into the output, with private paths stripped
32
+ s = str (s )
33
+ for p , x in PRIVATE_PATHS :
34
+ if s .startswith (p ):
35
+ return x + s [len (p ):]
36
+ return s
37
+
25
38
26
39
# Important: Concerning newlines handling, please read docs/dev_comments.md
27
40
28
41
class WrapperPrinter :
29
42
30
- def __init__ (self , outpath , opts , data , argv ):
43
+ def __init__ (self , outpath , opts , data , cmd_str ):
31
44
32
45
self .opts = opts
33
46
34
47
with outpath .open ("w" , encoding = "utf-8" ) as self .file :
35
48
36
49
self .paragraph_ctx = ParagraphCtxFactory (self .file )
37
50
38
- self .print_info (argv )
51
+ self .print_info (cmd_str )
39
52
self .file .write (
40
53
"\n \n import ctypes"
41
54
"\n from ctypes import *"
@@ -65,26 +78,11 @@ def __init__(self, outpath, opts, data, argv):
65
78
66
79
for fp in opts .inserted_files :
67
80
self .file .write ("\n \n \n " )
68
- self ._embed_file (fp , f"inserted file '{ self . _txtpath (fp )} '" )
81
+ self ._embed_file (fp , f"inserted file '{ txtpath (fp )} '" )
69
82
70
83
self .file .write ("\n " )
71
84
72
85
73
- PRIVATE_PATHS_TABLE = [(str (Path .home ()), "~" )]
74
- if Path .cwd () != Path ("/" ): # don't strip unix root
75
- PRIVATE_PATHS_TABLE += [(str (Path .cwd ()), "." )]
76
- # sort descending by length to avoid interference
77
- PRIVATE_PATHS_TABLE .sort (key = lambda x : len (x [0 ]), reverse = True )
78
-
79
- @classmethod
80
- def _txtpath (cls , s ):
81
- # Returns a path string suitable for embedding into the output, with private paths stripped
82
- s = str (s )
83
- for p , x in cls .PRIVATE_PATHS_TABLE :
84
- if s .startswith (p ):
85
- return x + s [len (p ):]
86
- return s
87
-
88
86
def _embed_file (self , fp , desc ):
89
87
with self .paragraph_ctx (desc ), open (fp , "r" ) as src_fh :
90
88
self .file .write ("\n \n " )
@@ -99,13 +97,11 @@ def _srcinfo(self, src):
99
97
if fp in ("<built-in>" , "<command line>" ):
100
98
self .file .write (f"# { fp } \n " )
101
99
else :
102
- self .file .write (f"# { self . _txtpath (fp )} : { lineno } \n " )
100
+ self .file .write (f"# { txtpath (fp )} : { lineno } \n " )
103
101
104
102
105
- def print_info (self , argv ):
106
- argv = [self ._txtpath (a ) for a in argv ]
107
- argv_str = ' ' .join ([shlex .quote (a ) for a in argv ])
108
- self .file .write (f'R"""\n Auto-generated by:\n ctypesgen { argv_str } \n """' )
103
+ def print_info (self , cmd_str ):
104
+ self .file .write (f'R"""\n Auto-generated by:\n { cmd_str } \n """' )
109
105
110
106
111
107
def print_loader (self , opts ):
0 commit comments