-
Notifications
You must be signed in to change notification settings - Fork 1
/
reviews.py
executable file
·140 lines (123 loc) · 4.44 KB
/
reviews.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/python
# Copyright 2011 Tim Hartman <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
version='0.0.1'
import argparse
import numpy as np
import re
from collections import *
import datetime
import sys
import tempfile
# argument parsing definitions
parser = argparse.ArgumentParser(prog='abaqus2dyna',
description='Translate Abaqus to LS-DYNA')
parser.add_argument('input',
metavar='INPUT',
help='Abaqus keyword file (limit one)',
nargs=1,
type=argparse.FileType('r'))
parser.add_argument('-o, --output',
dest='output',
metavar='OUTPUT',
help='LS-DYNA keyword file output location',
type=argparse.FileType('w'))
parser.add_argument('-v, --version',
action='version',
version='%(prog)s ' + version)
#args = parser.parse_args()
bib = open('reviews.bib','r')
try:
bib_lines = bib.readlines()
except UnicodeDecodeError:
print('Oops, I don\'t do Unicode characters\n')
exit(1)
madebyjabref = False
regex_comment = re.compile('^ *%')
for i in range(len(bib_lines)-1,-1,-1):
if regex_comment.search(bib_lines[i]):
if re.search('This file was created with JabRef',bib_lines[i]):
madebyjabref = True
bib_lines.pop(i)
bib_lines = str.join('',bib_lines)
bib_split = bib_lines.split('@')
types = Counter()
entry = {}
regex = {}
regex['type'] = re.compile('^[a-zA-Z]*{')
regex['key'] = re.compile('^{[a-zA-Z0-9\-_+=()\[\] ]*,')
regex['entry_name'] = re.compile('^[_\-a-zA-Z0-9]* = {')
for i in bib_split:
i.strip()
search_for_type = regex['type'].search(i)
if not search_for_type:
continue
current_type = search_for_type.group(0)[:-1]
if re.match('comment',current_type,flags=re.IGNORECASE):
continue
types[current_type] += 1
i = i[len(current_type):]
key = regex['key'].search(i)
if not key:
import pdb; pdb.set_trace()
raise Exception('Couldn\'t find BiBTeX key')
i = i[len(key.group(0)):-1]
# next four lines to get rid of last two }'s
i = i.strip()
i = i[:-1]
i = i.strip()
i = i[:-1]
key = key.group(0)[1:-1]
entry[key] = {}
entry[key]['type'] = current_type
isplit = i.split('},\n')
for j in isplit:
j = j.strip()
field = regex['entry_name'].search(j)
if not field:
import pdb; pdb.set_trace()
raise Exception('Cannot find field/value pair')
j = j[len(field.group(0)):]
field = field.group(0)[:-4].lower()
value = j
entry[key][field] = value
#import pdb; pdb.set_trace()
output = open('reviews.tex','w+')
output.write('% LaTeX document generated from BibTeX reviews\n')
output.write('% ' + datetime.datetime.utcnow().strftime("%y-%m-%d %H:%M:%S UTC") + '\n')
output.write('\\documentclass{bibtex2review}\n')
output.write('\\begin{document}\n\maketitle\n\\tableofcontents\n\\clearpage\n\n')
def eolsub(match):
match = match.group(0)
match = re.sub('\n',' ',match)
return match
alpha_keys = sorted(entry.keys())
for key in alpha_keys:
ref = entry[key]
if 'review' in ref and ref['review'] is not None:
output.write('% review for {:s}\n'.format(key))
output.write('\\subsection{{{0:s}}}\n\\LARGE\n{1:s}\n\\cite{{{0:s}}}\n\\normalsize\n'.format(key,ref['title']))
# prepare review section
review = ref['review']
if madebyjabref:
# strip tabs
review = re.sub('\t','',review)
# strip unintended eols
review = re.sub('.\n.',eolsub,review)
# condense good eols
review = re.sub('\n\n','\n',review)
#import pdb; pdb.set_trace()
output.write('\n\n{{\\large\n{:s}\n}}\n\\clearpage\n\n'.format(review))
output.write('%bibliography\n\\bibliography{\\finkbase}\n\\end{document}')
output.close()