Skip to content

Commit

Permalink
standardized where logger is instantiated
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellen Zhong committed Jun 22, 2014
1 parent 8311ffe commit 63e5ef8
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
3 changes: 1 addition & 2 deletions testing/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
N_FORMATS = 3

# Make a global logging object.
logger = logging.getLogger('InterMolLog')
if __name__ == '__main__':
logger = logging.getLogger('InterMolLog')
logger.setLevel(logging.DEBUG) # specifies lowest severity log messages to handle
h = logging.StreamHandler()
h.setLevel(logging.INFO) # ignores DEBUG level for now
Expand Down Expand Up @@ -87,7 +87,6 @@ def main(args=''):
else: # flexibility to call main() from other scripts
args = parser.parse_args(args)

logger = logging.getLogger('InterMolLog')
if args.verbose:
h.setLevel(logging.DEBUG)
System._sys = System('InterMol')
Expand Down
5 changes: 2 additions & 3 deletions testing/desmond_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import logging
from intermol.desmond_extension.desmond_parser import DesmondParser

logger = logging.getLogger('InterMolLog')

def readFile(infile):
logger = logging.getLogger('InterMolLog')
logger.info('Reading DESMOND file {0}'.format(infile))
parser = DesmondParser()
parser.read_file(infile)
logger.info('Structure loaded')

def writeFile(outfile):
logger = logging.getLogger('InterMolLog')
logger.info('Writing DESMOND file {0}'.format(outfile))
parser = DesmondParser()
parser.write_file(outfile)
Expand Down Expand Up @@ -95,7 +95,6 @@ def desmond_energies(cms, cfg, despath):
despath = path to DESMOND binaries
"""
logger = logging.getLogger('InterMolLog')
logger.info('Evaluating energy of {0}'.format(cms))

cms = os.path.abspath(cms)
Expand Down
5 changes: 2 additions & 3 deletions testing/gromacs_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from intermol.gromacs_extension.gromacs_topology_parser import GromacsTopologyParser
import intermol.gromacs_extension.gromacs_structure_parser as GromacsStructureParser

logger = logging.getLogger('InterMolLog')

def readFile(top_in, gro_in, gropath):
logger = logging.getLogger('InterMolLog')
# ensure .gro and .top are a valid match
gromacs_energies(top_in, gro_in,
'inputs/Gromacs/grompp.mdp', gropath, '',
Expand All @@ -23,7 +24,6 @@ def readFile(top_in, gro_in, gropath):
logger.info('Structure loaded')

def writeFile(outtop, outgro):
logger = logging.getLogger('InterMolLog')
logger.info('Writing GROMACS file {0}'.format(outgro))
GromacsStructureParser.writeStructure(outgro)
logger.info('Writing GROMACS file {0}'.format(outtop))
Expand All @@ -37,7 +37,6 @@ def gromacs_energies(top=None, gro=None, mdp=None, gropath='',grosuff='', grompp
gropath = path to gromacs binaries
grosuff = suffix of gromacs binaries, usually '' or '_d'
"""
logger = logging.getLogger('InterMolLog')
if not grompp_check:
logger.info('Evaluating energy of {0}'.format(gro))

Expand Down
5 changes: 2 additions & 3 deletions testing/lammps_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import logging
from intermol.lammps_extension.lammps_parser import LammpsParser

logger = logging.getLogger('InterMolLog')

def readFile(infile):
logger = logging.getLogger('InterMolLog')
logger.info('Reading LAMMPS files {0}'.format(infile))
parser = LammpsParser()
parser.read_system(infile)
logger.info('Structure loaded')

def writeFile(outfile):
logger = logging.getLogger('InterMolLog')
logger.info('Writing LAMMPS file {0}'.format(outfile))
parser = LammpsParser()
parser.write(outfile)
Expand All @@ -26,7 +26,6 @@ def lammps_energies(input_file, lmppath='lmp_openmpi'):
input_file = path to input file (expects data file in same folder)
lmppath = path to LAMMPS binaries
"""
logger = logging.getLogger('InterMolLog')
logger.info('Evaluating energy of {0}'.format(input_file))

directory, input_file = os.path.split(input_file)
Expand Down
17 changes: 12 additions & 5 deletions testing/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,17 @@ def add_flags(args, flags):
return flags

def add_handler(dir):
logger = logging.getLogger('InterMolLog')
'''
Adds two FileHandlers to the global logger object
args:
dir: path to output directory of the conversion
return:
h1, h2 -- FileHandlers so that they can be removed later
h1: logs all >=INFO-level messages to dir/UnitTestInfo.log
h2: logs all >=DEBUG-level messages to dir/UnitTestDebug.log
also includes function and line number for each message
'''
h1 = logging.FileHandler('%s/UnitTestInfo.log' % dir, mode='w') # don't append to existing file
f1 = logging.Formatter("%(levelname)s %(asctime)s %(message)s", "%Y-%m-%d %H:%M:%S")
h1.setFormatter(f1)
Expand All @@ -88,12 +98,11 @@ def add_handler(dir):
return h1, h2

def remove_handler(h1, h2):
logger = logging.getLogger('InterMolLog')
'''Removes the filehandlers h1, h2 so that messages do not continue to be logged to their respective files'''
logger.removeHandler(h1)
logger.removeHandler(h2)

def test_desmond(args):
logger = logging.getLogger('InterMolLog')
files = sorted(glob.glob('%s/*/*.cms' % DES_IN)) # return list of files that match the string
files = [x for x in files if not x.endswith('-out.cms')]
results = []
Expand Down Expand Up @@ -122,7 +131,6 @@ def test_desmond(args):
return files, results

def test_gromacs(args):
logger = logging.getLogger('InterMolLog')
gro_files = sorted(glob.glob('%s/*/*.gro' % GRO_IN)) # return list of files that match the string
gro_files = [x for x in gro_files if not x.endswith('out.gro')]
top_files = sorted(glob.glob('%s/*/*.top' % GRO_IN)) # return list of files that match the string
Expand Down Expand Up @@ -153,7 +161,6 @@ def test_gromacs(args):
return gro_files, results

def test_lammps(args):
logger = logging.getLogger('InterMolLog')
files = sorted(glob.glob('%s/*/*.lmp' % LMP_IN)) # return list of files that match the string
results = []

Expand Down

0 comments on commit 63e5ef8

Please sign in to comment.