Skip to content

Commit

Permalink
Fix rueckstiess#787: Replace OG Python string formatting with f-strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmd-mongo committed Apr 13, 2021
1 parent c3f4721 commit 79a3bcb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 50 deletions.
5 changes: 2 additions & 3 deletions mtools/mlaunch/mlaunch.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(self, *args, **kwargs):
kwargs.remove('serverSelectionTimeoutMS')

# Set client application name for MongoDB 3.4+ servers
kwargs['appName'] = 'mlaunch v{0}'.format(__version__)
kwargs['appName'] = f'''mlaunch v{__version__}'''

Connection.__init__(self, *args, **kwargs)

Expand Down Expand Up @@ -220,8 +220,7 @@ def run(self, arguments=None):
# to run can call different sub-commands
self.argparser = argparse.ArgumentParser()
self.argparser.add_argument('--version', action='version',
version="mtools version {0} || Python {1}".
format(__version__, sys.version))
version=f'''mtools version {__version__} || Python {sys.version}''')
self.argparser.add_argument('--no-progressbar', action='store_true',
default=False,
help='disables progress bar')
Expand Down
49 changes: 25 additions & 24 deletions mtools/mtransfer/mtransfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def run(self, arguments=None):
try:
storage_raw = open(os.path.join(self.dbpath, 'storage.bson'), 'rb').read()
except Exception as e:
sys.stderr.write('Failed to open storage.bson in "{0}": {1}\n'.format(self.dbpath, e))
sys.stderr.write(f'''Failed to open storage.bson in "{self.dbpath}": {e}\n''')
return

settings = bson.decode(storage_raw)["storage"]["options"]
Expand All @@ -66,22 +66,23 @@ def run(self, arguments=None):

if self.args['command'] == 'export':
if not os.path.exists(mtransfer_dir):
sys.stderr.write('Expected source directory "{0}" does not exist. '
'Check the database name is correct.\n'.format(mtransfer_dir))
msg = f'''Expected source directory "{mtransfer_dir}" does not exist. '''
msg = msg + 'Check the database name is correct.\n'
sys.stderr.write(msg)
return
if not self.force and os.path.exists(mtransfer_file):
sys.stderr.write('Output file "{0}" already exists\n'.format(mtransfer_file))
sys.stderr.write(f'''Output file "{mtransfer_file}" already exists\n''')
return
with open(mtransfer_file, 'wb') as outf:
self.doExport(outf)
elif self.args['command'] == 'import':
if not os.path.exists(mtransfer_dir):
sys.stderr.write('Expected target directory "{0}" does not exist. '
'Check the database name is correct.\n'.format(mtransfer_dir))
msg = f'''Expected target directory "{mtransfer_dir}" does not exist. '''
msg = msg + 'Check the database name is correct.\n'
sys.stderr.write(msg)
return
if not os.path.exists(mtransfer_file):
sys.stderr.write('Cannot import: mtransfer file "{0}" does not exist.\n'.
format(mtransfer_file))
sys.stderr.write(f'''Cannot import: mtransfer file "{mtransfer_file}" does not exist.\n''')
return
with open(mtransfer_file, 'rb') as inf:
self.doImport(inf)
Expand All @@ -97,7 +98,7 @@ def doExport(self, outf):
self.dbpath,
'log=(compressor=snappy,path=journal,recover=error),readonly=true')
except Exception as e:
sys.stderr.write('Failed to open dbpath "{0}": {1}\n'.format(self.dbpath, e))
sys.stderr.write(f'''Failed to open dbpath "{self.dbpath}": {e}\n''')
return

session = conn.open_session()
Expand Down Expand Up @@ -156,13 +157,13 @@ def doImport(self, inf):
self.dbpath,
'log=(enabled=false,compressor=snappy,path=journal,recover=error)')
except Exception as e:
sys.stderr.write('Failed to open dbpath "{0}": {1}\n'.format(self.dbpath, e))
sys.stderr.write(f'''Failed to open dbpath "{self.dbpath}": {e}\n''')
return

try:
self._doImport(conn, inf)
except Exception as e:
sys.stderr.write('Import failed: {0}'.format(e))
sys.stderr.write(f'''Import failed: {e}''')

print('Import complete')

Expand All @@ -181,7 +182,7 @@ def _doImport(self, conn, inf):
# Get the maximum file ID in the WT catalog: we will be appending
session.create('file:_mtransfer')
newfile_meta = wtMeta['file:_mtransfer']
self.message('Got new file metadata "{0}"'.format(newfile_meta))
self.message(f'''Got new file metadata "{newfile_meta}"''')
session.drop('file:_mtransfer')
file_id = int(re.search(r',id=(\d+),', newfile_meta).group(1))

Expand All @@ -193,16 +194,16 @@ def _doImport(self, conn, inf):
if not os.path.exists(
os.path.join(self.dbpath, self.database, export['filename'])):
sys.stderr.write(
'File "{0}" referenced in export missing during import'.
format(export['filename']))
f'''File "{export['filename']}" referenced in export missing during import'''
)
if not self.force:
return

if not self.force and export['version'] != __version__:
sys.stderr.write(
'Database was exported with mtools version {0}, '
'current version {1} may not be compatible'.
format(export['version'], __version__))
f'''Database was exported with mtools version {export['version']}, '''
f'''current version {__version__} may not be compatible'''
)
return

# Figure out the new namespace
Expand All @@ -219,17 +220,17 @@ def _doImport(self, conn, inf):
# the "file:" metadata with the original
app_metadata = app_metadata_re.search(idx['wtmeta_file']).group(0)
# For older style index metadata, update the namespace
app_metadata = re.sub(r'"ns" : ".*?"', '"ns" : "{0}"'.format(ns), app_metadata)
self.message('For index "{0}", app_metadata = "{1}"'.format(idxName, app_metadata))
app_metadata = re.sub(r'"ns" : ".*?"', f'''"ns" : "{ns}"''', app_metadata)
self.message(f'''For index "{idxName}", app_metadata = "{app_metadata}"''')
wtMeta[table_uri] = (app_metadata +
',colgroups=,collator=,columns=,key_format=u,value_format=u')
wtMeta[colgroup_uri] = (app_metadata +
',collator=,columns=,source="' + file_uri + '",type=file')
wtMeta[file_uri] = (idx['wtmeta_file'] + ',' + app_metadata +
(',id={0:d}'.format(file_id)))
(f''',id={file_id:d}'''))
file_id += 1
idxIdent[idxName] = ident
self.message('Adding index "{0}" with ident "{1}"'.format(idxName, ident))
self.message(f'''Adding index "{idxName}" with ident "{ident}"''')

# Figure out the WT URIs
ident = self.database + '/' + export['filename'][:-3]
Expand All @@ -240,13 +241,13 @@ def _doImport(self, conn, inf):
# Do a regular "session.create" for the table, then overwrite the
# "file:" metadata with the original
app_metadata = app_metadata_re.search(export['wtmeta_file']).group(0)
self.message('For collection "{0}", app_metadata = "{1}"'.format(ns, app_metadata))
self.message(f'''For collection "{ns}", app_metadata = "{app_metadata}"''')
wtMeta[table_uri] = (app_metadata +
',colgroups=,collator=,columns=,key_format=q,value_format=u')
wtMeta[colgroup_uri] = (app_metadata +
',collator=,columns=,source="' + file_uri + '",type=file')
wtMeta[file_uri] = (export['wtmeta_file'] + ',' + app_metadata +
(',id={0:d}'.format(file_id)))
(f''',id={file_id:d}'''))
file_id += 1

sizeStorer[ident.encode()] = bson.encode(export['sizeStorer'])
Expand All @@ -260,7 +261,7 @@ def _doImport(self, conn, inf):
for i in range(len(catalog_entry[u'md'][u'indexes'])):
catalog_entry[u'md'][u'indexes'][i][u'spec'][u'ns'] = ns
maxID += 1
self.message('Adding catalog entry {0} -> {1}'.format(maxID, catalog_entry))
self.message(f'''Adding catalog entry {maxID} -> {catalog_entry}''')
catalog[maxID] = bson.encode(catalog_entry, codec_options=codec_options)

session.commit_transaction()
Expand Down
8 changes: 5 additions & 3 deletions mtools/util/cmdlinetool.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,11 @@ def __init__(self):
"""
# define argument parser and add version argument
self.argparser = argparse.ArgumentParser()
self.argparser.add_argument('--version', action='version',
version="mtools version {0} || Python {1}".format(
__version__, sys.version))
self.argparser.add_argument(
'--version',
action='version',
version=f'''mtools version {__version__} || Python {sys.version}'''
)
self.argparser.add_argument('--no-progressbar', action='store_true',
default=False,
help='disables progress bar')
Expand Down
3 changes: 1 addition & 2 deletions mtools/util/logcodeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,5 @@ def __str__(self):
s = "%s\n" % (" <var> ".join(self.pattern))
for version in sorted(self.versions):
for filename, lineno, loglevel, trigger in self.matches[version]:
s += ("{:>10}: in {}:{}, loglevel {}, trigger {}\n"
.format(version, filename, lineno, loglevel, trigger))
s += (f'''{version:>10}: in {filename}:{lineno}, loglevel {loglevel}, trigger {trigger}\n''')
return s
12 changes: 3 additions & 9 deletions mtools/util/logevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,12 +1192,6 @@ def _parse_document(self):
yields = 'numYields:%i' % self._numYields if 'numYield' in doc else ''
duration = '%ims' % self.duration if self.duration is not None else ''

self._line_str = ("[{thread}] {operation} {namespace} {payload} "
"{scanned} {yields} locks(micros) {locks} "
"{duration}".format(datetime=self.datetime,
thread=self.thread,
operation=self.operation,
namespace=self.namespace,
payload=payload, scanned=scanned,
yields=yields, locks=locks,
duration=duration))
self._line_str = (f'''[{self.thread}] {self.operation} {self.namespace} {payload} '''
f'''{scanned} {yields} locks(micros) {locks} '''
f'''{duration}''')
20 changes: 11 additions & 9 deletions mtools/util/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,12 @@ def json2pattern(s, debug = False):
except Exception as err:
if debug:
## print some context info and return without any extracted query data..
print ("json2pattern():json.loads Exception:\n Error: {1} : {0}\n saved_s: ({2})\n s: ({3})\n".
format(err, sys.exc_info()[0], saved_s, s), file=sys.stderr)
msg = '''json2pattern():json.loads Exception:\n Error: {err} : {sys.exc_info()[0]}\n'''
msg = msg + f''' saved_s: ({saved_s})\n s: ({s})\n'''
print(msg)
return None
except:
print ("json2pattern():json.loads Unexpected error: save_s: ({0}) sys.exc_info():{1}".format(saved_s, sys.exc_info()[0]) )
print (f'''json2pattern():json.loads Unexpected error: saved_s: ({saved_s}) sys.exc_info():{sys.exc_info()[0]}''' )
raise


Expand All @@ -126,11 +127,12 @@ def json2pattern(s, debug = False):
except Exception as err:
## print some context info and return without any extracted query data..
if debug:
print ("json2pattern():json.dumps Exception:\n Error: {1} : {0}\n saved_s: ({2})\n doc: ({3})\n".
format(err, sys.exc_info()[0], saved_s, doc), file=sys.stderr)
msg = f'''json2pattern():json.dumps Exception:\n '''
msg = msg + f'''Error: {sys.exc_info()[0]} : {err}\n saved_s: ({saved_s})\n doc: ({doc})\n'''
sys.stderr.write(msg)
return None
except:
print ("json2pattern():json.dumps Unexpected error: save_s: ({0}) sys.exc_info():{1}".format(saved_s, sys.exc_info()[0]) )
print(f'''json2pattern():json.dumps Unexpected error: saved_s: ({saved_s}) sys.exc_info():{sys.exc_info()[0]}''')
raise


Expand Down Expand Up @@ -164,9 +166,9 @@ def json2pattern(s, debug = False):
r = json2pattern(k, debug)
if ( r == v ):
if debug :
print("OK...: {0}\n Expect: {1}\n Output: {2}\n\n".format(k,v,r))
print(f'''OK...: {k}\n Expect: {v}\n Output: {r}\n\n''')
else:
print("OK: {0}".format(k))
print(f'''OK: {k}''')

else:
print("\nERROR **: {0}\n Expect: {1}\n Output: {2}\n\n".format(k,v,r))
print(f'''\nERROR **: {k}\n Expect: {v}\n Output: {r}\n\n''')

0 comments on commit 79a3bcb

Please sign in to comment.