Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed formatted strings to fstrings in add_collaborators.py #24

Open
wants to merge 8 commits into
base: BoatswainFormatToFString
Choose a base branch
from
18 changes: 7 additions & 11 deletions add_collaborators.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,15 @@ def add_collaborators_deco(parser):

def add_collaborators(opt):
g = Github(opt.githubToken())
repo_full_name = '{}/{}'.format(opt.owner, opt.repo)
repo_full_name = f'{opt.owner}/{opt.repo}'
repo = g.get_repo(repo_full_name)

if not opt.promptYes(('Are you sure you would like to add users from {} '
'as collaborators to {} with {} permissions?')
.format(opt.users.name, repo_full_name,
opt.permission), True):
if not opt.promptYes((f'Are you sure you would like to add users from {opt.users.name} '
f'as collaborators to {repo_full_name} with {opt.permission} permissions?'), True):
opt.warn('Aborting')
return

opt.info('Proceeding to add {} as collaborators to {} with {} permissions'
.format(opt.users.name, repo_full_name, opt.permission))
opt.info(f'Proceeding to add {opt.users.name} as collaborators to {repo_full_name} with {opt.permission} permissions'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a closing parentheses


do_add = opt.begin is None
begin, added, total = 0, 0, 0
Expand All @@ -71,15 +68,14 @@ def add_collaborators(opt):
do_add_collaborator(repo, user, opt.permission, opt)
added = added + 1
else:
opt.info('Skipped {}'.format(user))
opt.info(f'Skipped {user}')

except Exception as e:
opt.error(e)
opt.error('{} failed on {} ({})'.format(CMD_NAME, user, total))
opt.error(f'{CMD_NAME} failed on {user} ({total})')
return

opt.info('Added {} users of {} total users, starting from index {}'
.format(added, total, begin))
opt.info(f'Added {added} users of {total} total users, starting from index {begin}')


def main(args=None, config_path=None):
Expand Down
14 changes: 6 additions & 8 deletions add_team_members.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def add_team_members_deco(parser):


def do_create_team(org, team_name, opt):
opt.info('Creating secret team {} in org {}'.format(team_name, org.name))
opt.info(f'Creating secret team {team_name} in org {org.name}')

if opt.noop:
opt.log('--noop option specified; not creating team')
Expand All @@ -58,22 +58,21 @@ def do_create_team(org, team_name, opt):

team = org.create_team(team_name, privacy='secret')

opt.info('Team {} successfully created'.format(team.name))
opt.info(f'Team {team.name} successfully created')

return team


def do_add_team_member(team, user, role, opt):
opt.info('Adding {} as team member to {} with {} permissions'
.format(user.login, team.name, role))
opt.info(f'Adding {user.login} as team member to {team.name} with {role} permissions')

if opt.noop:
opt.log('--noop option specified; not adding team member')
return

team.add_membership(user, role=role)

opt.info('User {} successfully added'.format(user.login))
opt.info(f'User {user.login} successfully added')


def add_team_members(opt):
Expand All @@ -96,12 +95,11 @@ def add_team_members(opt):
break

if team is None:
opt.error('Team {} not found in org {}'.format(opt.team, opt.org))
opt.error(f'Team {opt.team} not found in org {opt.org}')
opt.error('You may try passing the --create flag to create it')
return

opt.info('Adding users from {} as team members to {}/{} with {} permissions'
.format(opt.users.name, org.name, team.name, opt.role))
opt.info(f'Adding users from {opt.users.name} as team members to {org.name}/{team.name} with {opt.role} permissions')

if not opt.promptYes('Are you sure you would like to proceed?', True):
opt.warn('Aborting')
Expand Down
16 changes: 8 additions & 8 deletions boatswain_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def log(self, fmt, *args, tag=None):
if len(args) > 0:
fmt = fmt.format(*args)
if tag is not None:
fmt = '=={}==\t{}'.format(tag, fmt)
fmt = f'=={tag}==\t{fmt}'

# TODO: logging to file
print(fmt)
Expand Down Expand Up @@ -183,9 +183,9 @@ def ParseOption(
)
except configparser.NoOptionError:
print('[WARN]: You appear to be missing a Canvas token in your '
+ 'Boatswain configuration ({}). Please add this information '
+ f'Boatswain configuration ({config_path}). Please add this information '
+ 'to your config file under section [canvas] with key "token".'
+ '\n'.format(config_path))
+ '\n')
Comment on lines +186 to +188
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need the + - Python strings are implicitly concatenated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for the rest of the changes in this file

parser.add_argument('canvas_token',
type=str,
help='Canvas LMS auth token',
Expand All @@ -202,9 +202,9 @@ def ParseOption(
)
except configparser.NoOptionError:
print('[WARN]: You appear to be missing the Canvas URL in your '
+ 'Boatswain configuration ({}). Please add this information '
+ f'Boatswain configuration ({config_path}). Please add this information '
+ 'to your config file under section [canvas] with key "url".'
+ '\n'.format(config_path))
+ '\n')
parser.add_argument('canvas_url',
type=str,
help='Canvas LMS URL',
Expand All @@ -222,9 +222,9 @@ def ParseOption(
)
except configparser.NoOptionError:
print('[WARN]: You appear to be missing a GitHub token in your '
+ 'Boatswain configuration ({}). Please add this information '
+ f'Boatswain configuration ({config_path}). Please add this information '
+ 'to your config file under section [github] with key "token".'
+ '\n'.format(config_path))
+ '\n')
parser.add_argument('github_token',
type=str,
help='GitHub auth token',
Expand Down Expand Up @@ -299,7 +299,7 @@ def createConfigInteractive():
config = newPopulatedConfigInteractive()
config.write(open(path, 'w+'))

itv.output('Boatswain config file created at {}'.format(path))
itv.output(f'Boatswain config file created at {path}')

except EOFError:
itv.output()
Expand Down
12 changes: 6 additions & 6 deletions canvas-wrangler.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def retrieve_index(header_row, target):
for i, cell in enumerate(header_row):
if cell == target:
return i
raise LookupError('{} index not found'.format(target))
raise LookupError(f'{target} index not found')


def retrieve_indices(header, opt):
Expand Down Expand Up @@ -103,18 +103,18 @@ def build_grade_data(grades, student_i, grade_i, comment_i, opt):
else:
comment = None

opt.info('{} ({}): {}'.format(user_id, grade, comment))
grade_data['sis_user_id:{}'.format(user_id)] = grade_entry
opt.info(f'{user_id} ({grade}): {comment}')
grade_data[f'sis_user_id:{user_id}'] = grade_entry

return grade_data


def log_dump_wrangler(grade_data, course, assignment, opt):
opt.info('Course: {}'.format(course))
opt.info('Assignment: {}'.format(assignment))
opt.info(f'Course: {course}')
opt.info(f'Assignment: {assignment}')
opt.info('Grade Data: {')
for k in grade_data:
opt.info('{} : {}'.format(k, grade_data[k]))
opt.info(f'{k} : {grade_data[k]}')
opt.info('}')
opt.info('')

Expand Down
10 changes: 5 additions & 5 deletions interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def output(*args):

def promptInput(prompt, fmt='', default=''):
if fmt != '':
promptf = '{} [{}]'.format(prompt, fmt)
promptf = f'{prompt} [{fmt}]'
elif default != '':
promptf = '{} [{}]'.format(prompt, default)
promptf = f'{prompt} [{default}]'
else:
promptf = prompt

promptf = '{}: '.format(promptf)
promptf = '{promptf}: '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a starting f


while True:
inp = input(promptf)
Expand All @@ -31,7 +31,7 @@ def promptValidate(prompt, validator, fmt='', default=''):
v = validator(inp)
if v == '':
return inp
output('Invalid input (case-insensitive): {}'.format(v))
output(f'Invalid input (case-insensitive): {v}')


def selectorValidator(options, default=''):
Expand Down Expand Up @@ -70,7 +70,7 @@ def newFileValidator():
def validator(inp):
try:
if os.path.exists(inp):
return '{} already exists'.format(inp)
return f'{inp} already exists'
else:
open(inp, 'w').close()
os.unlink(inp)
Expand Down
17 changes: 6 additions & 11 deletions mk_group_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,12 @@ def mk_group_repos(opt):
org = g.get_organization(opt.org)

if not opt.promptYes(('Are you sure you would like to create group repos '
'for users in {} under org {} with name {}')
.format(opt.groups.name, opt.org,
fmt_hyphen(opt.prefix, '<group>')),
f'for users in {opt.groups.name} under org {opt.org} with name {fmt_hyphen(opt.prefix, '<group>')}'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would split this into multiple lines. Also, I'm not sure the quotes surrounding '<group>' are going to work. Quotes in the format part of f-strings can get tricky - I would test that and see if it potentially works with double quotes. The cleanest solution may just be to turn the fmt_hyphen section into a variable and use that instead

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for line 57

True):
opt.warn('Aborting')
return

opt.info('Creating repos under {} for groups in {}, with name {}'
.format(opt.groups.name, opt.org,
fmt_hyphen(opt.prefix, '<group>')))
opt.info(f'Creating repos under {opt.groups.name} for groups in {opt.org}, with name {fmt_hyphen(opt.prefix, '<group>')}')

for g in csv.reader(opt.groups):
group, members = g[0], [m.strip() for m in g[1:] if m != '']
Expand All @@ -69,19 +65,18 @@ def mk_group_repos(opt):
action = 'creating'
else:
action = 'looking up'
opt.info('{} {}/{} and adding members {}'
.format(action, opt.org, repo_name, members))
opt.info(f'{action} {opt.org}/{repo_name} and adding members {members}')

if opt.create:
repo = do_mk_repo(org, repo_name, opt)
else:
repo = org.get_repo(repo_name)
opt.info('Looked up repo {}/{}'.format(org.name, repo_name))
opt.info(f'Looked up repo {org.name}/{repo_name}')

if opt.permission == 'none':
opt.info('Not adding {} to {}'.format(members, repo_name))
opt.info(f'Not adding {members} to {repo_name}')
else:
opt.info('Adding {} to {}'.format(members, repo_name))
opt.info(f'Adding {members} to {repo_name}')
for member in members:
do_add_collaborator(repo, member, opt.permission, opt)

Expand Down
15 changes: 6 additions & 9 deletions mk_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ def mk_repo_deco(parser):

def fmt_hyphen(prefix, name):
if prefix[-1] == '-':
return '{}{}'.format(prefix, name)
return f'{prefix}{name}'
else:
return '{}-{}'.format(prefix, name)
return f'{prefix}-{name}'


def do_mk_repo(org, repo_name, opt):
opt.info('Creating private repo {} under organization {}'
.format(repo_name, org))
opt.info(f'Creating private repo {repo_name} under organization {org}')

if opt.noop:
opt.log('--noop option specified; not creating repo')
Expand All @@ -49,7 +48,7 @@ def do_mk_repo(org, repo_name, opt):
repo = org.create_repo(repo_name, private=True)

# funky output so easily greppable
opt.log('@CREATED_REPO: {} {}'.format(repo_name, repo.git_url))
opt.log(f'@CREATED_REPO: {repo_name} {repo.git_url}')

return repo

Expand All @@ -61,14 +60,12 @@ def mk_repo(opt):
repo_name = fmt_hyphen(opt.prefix, opt.repo)

if not opt.promptYes(('Are you sure you would like to create a private '
'repo under organization {} named {}?')
.format(opt.org, repo_name),
f'repo under organization {opt.org} named {repo_name}?'),
True):
opt.warn('Aborting')
return

opt.info('Proceeding to create private repo {}/{}'
.format(opt.org, repo_name))
opt.info(f'Proceeding to create private repo {opt.org}/{repo_name}')

do_mk_repo(org, repo_name, opt)

Expand Down
12 changes: 5 additions & 7 deletions scrub-deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ def scrub_deco(parser):

def do_scrub(repo, opt):
if opt.noop:
opt.log('--noop option specified; not deleting {}'.format(repo.name))
opt.log(f'--noop option specified; not deleting {repo.name}')
return

opt.warn('Deleting {}...'.format(repo.name))
opt.warn(f'Deleting {repo.name}...')
repo.delete()


def scrub_org(opt):
g = Github(opt.githubToken())
org = g.get_organization(opt.org_name)

opt.warn('Scrubbing Github Org: {} ({})'.format(org.login, org.url))
opt.warn(f'Scrubbing Github Org: {org.login} ({org.url})')

if not opt.promptYes('Are you sure you want to scrub this org?', False):
# deliberately disallow --yes to work
Expand All @@ -58,13 +58,11 @@ def scrub_org(opt):
repo_name = repo.full_name.split('/')[-1]
if opt.invert:
if opt.filter.fullmatch(repo_name):
opt.info('Skipping {}; matched with inverted filter: {}'
.format(repo_name, opt.filter))
opt.info(f'Skipping {repo_name}; matched with inverted filter: {opt.filter}')
continue
else:
if not opt.filter.fullmatch(repo_name):
opt.info('Skipping {}; did not match with filter: {}'
.format(repo_name, opt.filter))
opt.info(f'Skipping {repo_name}; did not match with filter: {opt.filter}')
continue

do_scrub(repo, opt)
Expand Down