FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
book-site/fix_dunder.py at main · fluentpython/book-site · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
fluentpython
/
book-site
Public
Notifications
You must be signed in to change notification settings
Fork
19
Star
100
Code
Issues
6
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
book-site
/
fix_dunder.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
51 lines (41 loc) · 1.28 KB
Breadcrumbs
book-site
/
fix_dunder.py
Copy path
File metadata and controls
executable file
·
51 lines (41 loc) · 1.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
import
argparse
import
os
import
re
import
shutil
import
sys
SCRIPT
=
sys
.
argv
[
0
]
RE_DUNDER_CODE
=
re
.
compile
(
r'`__.+?__`'
)
RE_DUNDER_IDENT
=
re
.
compile
(
r'`(__.+?__)`'
)
def
backup
(
filename
:
str
)
->
None
:
msg
=
''
copy_name
=
filename
+
'.bkp'
try
:
shutil
.
copy2
(
filename
,
copy_name
)
except
FileNotFoundError
:
msg
=
f'File
{
filename
!r
}
not found.'
if
not
msg
and
not
os
.
path
.
exists
(
copy_name
):
msg
=
f'Unable to create
{
copy_name
!r
}
.'
if
msg
:
print
(
msg
,
file
=
sys
.
stderr
)
sys
.
exit
(
1
)
def
replace
(
filename
:
str
)
->
str
:
with
open
(
filename
)
as
fp
:
adoc
=
fp
.
read
()
for
dunder_code
in
sorted
(
set
(
RE_DUNDER_CODE
.
findall
(
adoc
))):
ident
=
RE_DUNDER_IDENT
.
match
(
dunder_code
)[
1
]
adoc
=
re
.
sub
(
dunder_code
,
f'`+
{
ident
}
+`'
,
adoc
)
print
(
ident
)
with
open
(
filename
,
'w'
)
as
fp
:
fp
.
write
(
adoc
)
def
main
()
->
None
:
parser
=
argparse
.
ArgumentParser
(
prog
=
SCRIPT
,
description
=
'Subsitui `__dunder__` por `+__dunder__+`.'
,
epilog
=
f'EXEMPLO:
\n
\t
{
SCRIPT
}
cap01.adoc'
)
parser
.
add_argument
(
'asciidoc_filename'
)
args
=
parser
.
parse_args
()
backup
(
args
.
asciidoc_filename
)
replace
(
args
.
asciidoc_filename
)
if
__name__
==
'__main__'
:
main
()
Back
|
FazBrowse Home
|
New Git URL