Source code for pyspex.log

#!/usr/bin/env python

import os
from . import pyspex_f2py


[docs]class Log: """Class to save and execute commands and output from the terminal. :ivar do_output: Parameter indicating whether log output is on. :vartype do_output: bool :ivar do_save: Parameter indicating whether log save commands is on. :vartype do_save: bool """ def __init__(self): self.do_output = False self.do_save = False
[docs] def execute(self, filename): """Execute commands listed in an ascii file with a .com extension. :param filename: Filename of the ASCII file with the SPEX commands (including .com extension). :type filename: str :rtype: int """ exists = os.path.isfile(filename) if exists: file = os.path.splitext(filename)[0] if os.path.splitext(filename)[1] != '.com': print("Error: Filename does not have extension .com.") return 1 command = 'log execute '+str(file) pyspex_f2py.api_command(command) return 0 else: print("Error: File {:s} does not exist.".format(filename)) return 1
[docs] def save(self, savefile, status=None): """Save the commands for a session in an ascii file. The status is optional and either overwrite or append. :param savefile: Filename of the output command file (including .com extension). :type savefile: str :param status: (Optional) Overwrite or append an existing file name. String can be 'overwrite' or 'append'. :type status: str """ file = os.path.splitext(savefile)[0] ext = os.path.splitext(savefile)[1] if ext != '.com': print("Error: File does not have the .com extension.") return 1 if status is not None: if status == 'overwrite': command = 'log save '+str(file)+' overwrite' elif status == 'append': command = 'log save '+str(file)+' append' else: print("Warning: Status not recognized. Trying to save commands anyway.") command = 'log save '+str(file) else: command = 'log save '+str(file) stat = pyspex_f2py.api_command(command) if stat == 0: self.do_save = True
[docs] def output(self, outfile, status=None): """Save the terminal output to ascii file. The status is optional and either overwrite or append. :param outfile: Filename of the output file (including .out extension). :type outfile: str :param status: (Optional) Overwrite or append an existing file name. String can be 'overwrite' or 'append'. :type status: str """ file = os.path.splitext(outfile)[0] ext = os.path.splitext(outfile)[1] if ext != '.out': print("Error: File does not have the .out extension.") return 1 if status is not None: if status == 'overwrite': command = 'log output '+str(file)+' overwrite' elif status == 'append': command = 'log output '+str(file)+' append' else: print("Warning: Status not recognized. Trying to save terminal output anyway.") command = 'log output '+str(file) else: command = 'log output '+str(file) stat = pyspex_f2py.api_command(command) if stat == 0: self.do_output = True
[docs] def close(self, logtype): """Close the save or output file. :param logtype: String indicating the type of log to close ('save' or 'out'). :type logtype: str """ if logtype == 'save': stat = pyspex_f2py.api_command('log close save') if stat == 0: self.do_save = False elif logtype == 'out': stat = pyspex_f2py.api_command('log close out') if stat == 0: self.do_output = False elif logtype == 'output': stat = pyspex_f2py.api_command('log close out') if stat == 0: self.do_output = False else: print("Error: Type {:s} not recognized.".format(type))