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

Added parameter '--throw-exception', which can not be specified togheter... #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions ribosome.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ def rethrow(e, rnafile, linemap):
parser = argparse.ArgumentParser(
version = "ribosome code generator, version 1.14")
parser.add_argument('dna', type=file)
parser.add_argument('--rna', action='store_true')
group = parser.add_mutually_exclusive_group()
group.add_argument('--rna', action='store_true')
group.add_argument('--throw-exception', action='store_true')

# Pwd
__dir__ = os.path.dirname(os.path.realpath(__file__))
Expand Down Expand Up @@ -353,7 +355,7 @@ def rnawrite(s):

# Generate RNA Prologue code
rnawrite(PROLOGUE)
if not args.rna:
if not args.rna and not args.throw_exception:
rnawrite('try:\n')

# Process the DNA file.
Expand Down Expand Up @@ -388,7 +390,7 @@ def rnawrite(s):
# Lets save the left and right spaces, if any
lspace = line.replace(line.lstrip(), '')
# Add 4 spaces to accommodate our try except clause if not rna
if not args.rna:
if not args.rna and not args.throw_exception:
lspace = lspace + ' ' * 4
rspace = line.replace(line.rstrip(), '')
# followed by stripping the line
Expand Down Expand Up @@ -477,7 +479,7 @@ def rnawrite(s):
# Generate RNA epilogue code.
dnastack = [[None, 'ribosome', __line__() + 1]]
rna.write('\n')
if not args.rna:
if not args.rna and not args.throw_exception:
rna.write('except Exception as e:\n')
rna.write(' LINEMAP = [\n')
last = None
Expand All @@ -501,7 +503,17 @@ def rnawrite(s):
if not args.rna:
import subprocess
# Execute the RNA file. Pass it any arguments not used by ribosome.
subprocess.call(['python', rnafile] + sys.argv[2:])
pipes = subprocess.Popen(['python', rnafile] + sys.argv[2:], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = pipes.communicate()

# Print generated RNA file contents
sys.stdout.write(stdout)

# Delete the RNA file.
os.remove(rnafile)

# If there was an error in the RNA file, print the error and exit with a non-zero status
if args.throw_exception and stderr is not None and len(stderr) > 0:
sys.stderr.write(stderr)
sys.exit(1)