-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconsole.py
118 lines (110 loc) · 3.53 KB
/
console.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
# file: console.py
# vim:fileencoding=utf-8:ft=python
# lamprop - main program.
#
# Copyright © 2011-2021 R.F. Smith <[email protected]>. All rights reserved.
# Created: 2011-03-26 14:54:24 +0100
# Last modified: 2022-01-28T09:52:27+0100
#
# SPDX-License-Identifier: BSD-2-Clause
import argparse
import logging
import os
import sys
import lp
class LicenseAction(argparse.Action):
"""Action class to print the license."""
def __call__(self, parser, namespace, values, option_string=None):
print(lp.__license__)
sys.exit()
def main():
"""Entry point for lamprop console application."""
# Process the command-line arguments
doc = (
"Calculate the elastic properties of a fibrous composite laminate. "
"See the manual (lamprop-manual.pdf) for more in-depth information."
)
opts = argparse.ArgumentParser(prog="lamprop", description=doc)
group = opts.add_mutually_exclusive_group()
group.add_argument(
"-i",
"--info",
action="store_true",
help="show information about source file (the default is not to)",
)
group.add_argument(
"-l",
"--latex",
action="store_true",
help="generate LaTeX output (the default is plain text)",
)
group.add_argument("-H", "--html", action="store_true", help="generate HTML output")
opts.add_argument(
"-e",
"--eng",
action="store_true",
help="output only the engineering properties",
)
opts.add_argument(
"-m",
"--mat",
action="store_true",
help="output only the ABD matrix and stiffness tensor",
)
opts.add_argument(
"-f", "--fea", action="store_true", help="output only material data for FEA"
)
group = opts.add_mutually_exclusive_group()
group.add_argument(
"-L", "--license", action=LicenseAction, nargs=0, help="print the license"
)
group.add_argument("-v", "--version", action="version", version=lp.__version__)
opts.add_argument(
"--log",
default="warning",
choices=["debug", "info", "warning", "error"],
help="logging level (defaults to 'warning')",
)
opts.add_argument(
"files", metavar="file", nargs="*", help="one or more files to process"
)
args = opts.parse_args(sys.argv[1:])
logging.basicConfig(
level=getattr(logging, args.log.upper(), None),
format="%(levelname)s: %(message)s",
)
del opts, group
if args.mat is False and args.eng is False and args.fea is False:
args.eng = True
args.mat = True
args.fea = True
# No files given to process.
if len(args.files) == 0:
sys.exit(1)
# Set the output method.
out = lp.text_output
if args.latex:
out = lp.latex_output
elif args.html:
out = lp.html_output
# Force utf-8 encoding for stdout on ms-windows.
# Because redirected output uses cp1252 by default.
if os.name == "nt":
sys.stdout.reconfigure(encoding="utf-8")
for f in args.files:
logging.info("processing file '{}'".format(f))
laminates = lp.parse(f)
if args.info and lp.info:
print(f'Information for "{f}":')
for ln in lp.info:
print(ln)
print()
if lp.warn:
print(f'Warnings for "{f}":')
for ln in lp.warn:
print(ln)
print()
for curlam in laminates:
print(*out(curlam, args.eng, args.mat, args.fea), sep="\n")
if __name__ == "__main__":
main()