Refactor config parsing code in main.d, rename esv.d -> esvapi.d, rename class EsvAPI to ESVApi, makefile changes
This commit is contained in:
parent
7f61d7bfa6
commit
5dc2a12f1e
3 changed files with 24 additions and 30 deletions
14
Makefile
14
Makefile
|
@ -2,9 +2,9 @@ IMPORT = import
|
||||||
PREFIX = /usr/local
|
PREFIX = /usr/local
|
||||||
MANPREFIX = /usr/share/man
|
MANPREFIX = /usr/share/man
|
||||||
|
|
||||||
DC = ldc2
|
DC = ldc2
|
||||||
CFLAGS = -O -I${IMPORT} -w
|
CFLAGS = -O -I${IMPORT} -release -w
|
||||||
OBJS = main.o esv.o ini.o
|
OBJS = main.o esvapi.o ini.o
|
||||||
|
|
||||||
all: esv
|
all: esv
|
||||||
|
|
||||||
|
@ -12,17 +12,17 @@ esv: ${OBJS}
|
||||||
${DC} ${CFLAGS} -of$@ ${OBJS}
|
${DC} ${CFLAGS} -of$@ ${OBJS}
|
||||||
|
|
||||||
# main executable
|
# main executable
|
||||||
main.o: main.d esv.o
|
main.o: main.d esvapi.o
|
||||||
${DC} ${CFLAGS} -c main.d -of$@
|
${DC} ${CFLAGS} -c main.d -of$@
|
||||||
|
|
||||||
esv.o: esv.d
|
esvapi.o: esvapi.d
|
||||||
${DC} ${CFLAGS} -c esv.d -of$@
|
${DC} ${CFLAGS} -c esvapi.d -of$@
|
||||||
|
|
||||||
ini.o: ${IMPORT}/dini/*.d
|
ini.o: ${IMPORT}/dini/*.d
|
||||||
${DC} ${CFLAGS} -c ${IMPORT}/dini/*.d -of$@
|
${DC} ${CFLAGS} -c ${IMPORT}/dini/*.d -of$@
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f ${PROG} ${OBJS}
|
rm -f esv ${OBJS}
|
||||||
|
|
||||||
install: esv
|
install: esv
|
||||||
install -m755 esv ${DESTDIR}${PREFIX}/bin/esv
|
install -m755 esv ${DESTDIR}${PREFIX}/bin/esv
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
* along with esv. If not, see <http://www.gnu.org/licenses/>.
|
* along with esv. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
module esvapi;
|
||||||
|
|
||||||
import std.algorithm : filter, map;
|
import std.algorithm : filter, map;
|
||||||
import std.array : appender;
|
import std.array : appender;
|
||||||
import std.ascii : isAlphaNum;
|
import std.ascii : isAlphaNum;
|
||||||
|
@ -106,12 +108,12 @@ const string[] BIBLE_BOOKS = [
|
||||||
"Revelation"
|
"Revelation"
|
||||||
];
|
];
|
||||||
|
|
||||||
class EsvAPI
|
class ESVApi
|
||||||
{
|
{
|
||||||
private string _key;
|
private string _key;
|
||||||
private string _url;
|
private string _url;
|
||||||
private string _mode;
|
private string _mode;
|
||||||
EsvAPIOptions opts;
|
ESVApiOptions opts;
|
||||||
string extraParameters;
|
string extraParameters;
|
||||||
int delegate(size_t dlTotal, size_t dlNow, size_t ulTotal, size_t ulNow) onProgress;
|
int delegate(size_t dlTotal, size_t dlNow, size_t ulTotal, size_t ulNow) onProgress;
|
||||||
string tmpDir;
|
string tmpDir;
|
||||||
|
@ -312,7 +314,7 @@ class EsvAPI
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct EsvAPIOptions
|
struct ESVApiOptions
|
||||||
{
|
{
|
||||||
bool[string] boolOpts;
|
bool[string] boolOpts;
|
||||||
int[string] intOpts;
|
int[string] intOpts;
|
||||||
|
@ -337,7 +339,7 @@ struct EsvAPIOptions
|
||||||
intOpts["indent_declares"] = 40;
|
intOpts["indent_declares"] = 40;
|
||||||
intOpts["indent_psalm_doxology"] = 30;
|
intOpts["indent_psalm_doxology"] = 30;
|
||||||
intOpts["line_length"] = 0;
|
intOpts["line_length"] = 0;
|
||||||
indent_using = "space";
|
indent_using = "space";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
30
main.d
30
main.d
|
@ -20,6 +20,7 @@
|
||||||
|
|
||||||
import std.conv : to, ConvException;
|
import std.conv : to, ConvException;
|
||||||
import std.file : exists, write, FileException;
|
import std.file : exists, write, FileException;
|
||||||
|
import std.format : format;
|
||||||
import std.getopt : getopt, GetOptException, config;
|
import std.getopt : getopt, GetOptException, config;
|
||||||
import std.path : baseName, expandTilde, isValidPath;
|
import std.path : baseName, expandTilde, isValidPath;
|
||||||
import std.process : environment, executeShell;
|
import std.process : environment, executeShell;
|
||||||
|
@ -27,7 +28,7 @@ import std.regex : regex, matchFirst, replaceAll, replaceFirst;
|
||||||
import std.stdio : writef, writeln, writefln, stderr;
|
import std.stdio : writef, writeln, writefln, stderr;
|
||||||
import std.string : splitLines;
|
import std.string : splitLines;
|
||||||
|
|
||||||
import esv;
|
import esvapi;
|
||||||
import dini;
|
import dini;
|
||||||
|
|
||||||
enum VERSION = "0.2.0";
|
enum VERSION = "0.2.0";
|
||||||
|
@ -164,7 +165,7 @@ key = " ~ DEFAULT_APIKEY ~ "
|
||||||
panic("API key not present in configuration file; cannot proceed");
|
panic("API key not present in configuration file; cannot proceed");
|
||||||
|
|
||||||
// Initialise API object and validate the book and verse
|
// Initialise API object and validate the book and verse
|
||||||
EsvAPI esv = new EsvAPI(apiKey);
|
ESVApi esv = new ESVApi(apiKey);
|
||||||
if (!esv.validateBook(args[1].extractBook()))
|
if (!esv.validateBook(args[1].extractBook()))
|
||||||
panic("book '" ~ args[1] ~ "' does not exist");
|
panic("book '" ~ args[1] ~ "' does not exist");
|
||||||
if (!esv.validateVerse(args[2]))
|
if (!esv.validateVerse(args[2]))
|
||||||
|
@ -208,17 +209,17 @@ key = " ~ DEFAULT_APIKEY ~ "
|
||||||
foreach (string key; ["footnotes", "headings", "passage_references", "verse_numbers"]) {
|
foreach (string key; ["footnotes", "headings", "passage_references", "verse_numbers"]) {
|
||||||
try {
|
try {
|
||||||
esv.opts.boolOpts["include_" ~ key] =
|
esv.opts.boolOpts["include_" ~ key] =
|
||||||
returnValid("true", iniData["passage"].getKey(key)).catchConvException(
|
returnValid("true", iniData["passage"].getKey(key)).to!bool();
|
||||||
(in ConvException ex, in char[] str)
|
} catch (ConvException e) {
|
||||||
{
|
panic(
|
||||||
panic(configPath ~ ": value '" ~ cast(string)str ~
|
format!"%s: key '%s' of section 'passage' is not a boolean value (must be either 'true' or 'false')"(
|
||||||
"' is not convertible to a boolean value; must be either 'true' or 'false'");
|
configPath, key
|
||||||
}
|
)
|
||||||
);
|
);
|
||||||
} catch (IniException e) {} // just do nothing; use the default settings
|
} catch (IniException e) {} // just do nothing; use the default settings
|
||||||
}
|
}
|
||||||
// Get line_length ([passage])
|
// Get line_length ([passage])
|
||||||
try esv.opts.intOpts["line_length"] = returnValid("0", iniData["passage"].getKey("line_length")).to!int();
|
try esv.opts.intOpts["line_length"] = returnValid("0", iniData["passage"].getKey("line_length")).to!int();
|
||||||
catch (ConvException e) {
|
catch (ConvException e) {
|
||||||
panic(configPath ~ ": value '" ~ iniData["passage"].getKey("line_length")
|
panic(configPath ~ ": value '" ~ iniData["passage"].getKey("line_length")
|
||||||
~ "' is not convertible to an integer value; must be a non-decimal number");
|
~ "' is not convertible to an integer value; must be a non-decimal number");
|
||||||
|
@ -274,12 +275,3 @@ string extractBook(in string book) @safe
|
||||||
{
|
{
|
||||||
return book.replaceAll(regex("[-_]"), " ");
|
return book.replaceAll(regex("[-_]"), " ");
|
||||||
}
|
}
|
||||||
|
|
||||||
bool catchConvException(in char[] sb, void delegate(in ConvException ex, in char[] str) @system catchNet)
|
|
||||||
{
|
|
||||||
try return sb.to!bool();
|
|
||||||
catch (ConvException e) {
|
|
||||||
catchNet(e, sb);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue