Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
sbws
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
juga
sbws
Commits
b15392f4
Commit
b15392f4
authored
7 years ago
by
Matt Traudt
Browse files
Options
Downloads
Patches
Plain Diff
Prompt before overwriting config.ini
GH: closes #86
parent
430fe9be
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
sbws/core/init.py
+12
-3
12 additions, 3 deletions
sbws/core/init.py
sbws/globals.py
+1
-1
1 addition, 1 deletion
sbws/globals.py
sbws/util/userquery.py
+30
-0
30 additions, 0 deletions
sbws/util/userquery.py
with
43 additions
and
4 deletions
sbws/core/init.py
+
12
−
3
View file @
b15392f4
from
sbws.globals
import
(
is_initted
,
fail_hard
,
touch_file
)
from
sbws.util.config
import
get_user_example_config
from
sbws.util.userquery
import
query_yes_no
from
argparse
import
ArgumentDefaultsHelpFormatter
import
os
import
logging
...
...
@@ -23,10 +24,18 @@ def main(args, conf):
log
.
info
(
'
Creating %s
'
,
args
.
directory
)
os
.
makedirs
(
args
.
directory
,
exist_ok
=
False
)
# Create config.log.ini ####
touch_file
(
os
.
path
.
join
(
args
.
directory
,
'
config.log.ini
'
))
config_fname
=
os
.
path
.
join
(
args
.
directory
,
'
config.ini
'
)
# Create config.ini ####
fname
=
os
.
path
.
join
(
args
.
directory
,
'
config.ini
'
)
if
os
.
path
.
exists
(
fname
)
and
not
os
.
path
.
isfile
(
fname
):
fail_hard
(
'
Don
\'
t know how to handle %s existing as a non-file
'
,
fname
)
if
os
.
path
.
isfile
(
fname
)
and
not
query_yes_no
(
'
Is it okay to overwrite {}?
'
.
format
(
fname
),
default
=
None
):
fail_hard
(
'
Cannot continue
'
)
c
=
get_user_example_config
()
c
[
'
paths
'
][
'
sbws_home
'
]
=
args
.
directory
log
.
info
(
'
Creating %s based on example config
'
,
config_
fname
)
with
open
(
config_
fname
,
'
wt
'
)
as
fd
:
log
.
info
(
'
Creating %s based on example config
'
,
fname
)
with
open
(
fname
,
'
wt
'
)
as
fd
:
c
.
write
(
fd
)
This diff is collapsed.
Click to expand it.
sbws/globals.py
+
1
−
1
View file @
b15392f4
...
...
@@ -29,7 +29,7 @@ def is_initted(d):
conf_fnames
=
[
os
.
path
.
join
(
d
,
'
config.ini
'
),
os
.
path
.
join
(
d
,
'
config.log.ini
'
)]
for
fname
in
conf_fnames
:
if
not
os
.
path
.
exists
(
fname
):
if
not
os
.
path
.
isfile
(
fname
):
return
False
return
True
...
...
This diff is collapsed.
Click to expand it.
sbws/util/userquery.py
0 → 100644
+
30
−
0
View file @
b15392f4
# Based on https://stackoverflow.com/a/3041990
def
query_yes_no
(
question
,
default
=
'
yes
'
):
'''
Ask a yes/no question via input() and return the user
'
s answer.
:param str question: Prompt given to the user.
:param str default: The assumed answer if th user just hits **Enter**. It
must be ``
'
yes
'
`` (the default if no default is given), ``
'
no
'
``, or
``None`` (meaning an answer is required from the user).
:returns: ``True`` if we ended up with a
'
yes
'
answer, otherwise
``False``.
'''
valid
=
{
'
yes
'
:
True
,
'
y
'
:
True
,
'
ye
'
:
True
,
'
no
'
:
False
,
'
n
'
:
False
}
if
default
is
None
:
prompt
=
'
[y/n]
'
elif
default
==
'
yes
'
:
prompt
=
'
[Y/n]
'
elif
default
==
'
no
'
:
prompt
=
'
[y/N]
'
else
:
raise
ValueError
(
'
invalid default answer:
"
%s
"'
%
default
)
while
True
:
print
(
question
+
prompt
,
end
=
''
)
choice
=
input
().
lower
()
if
default
is
not
None
and
choice
==
''
:
return
valid
[
default
]
elif
choice
in
valid
:
return
valid
[
choice
]
else
:
print
(
'
Please respond with
"
yes
"
or
"
no
"
(or
"
y
"
or
"
n
"
).
\n
'
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment