Added a way to specify the type of a macro on files

This feature is mainly useful for command line arguments.
If you run KLayout with

  klayout -b -r myscript

it will not be able to determine the type of macro without
a suffix. You can explicitly specify a certain type by
giving the suffix implicitly:

  klayout -b -r myscript[rb]

This will read "myscript" but pretend it was "myscript.rb"
and execute it as Ruby script.

This feature is handy if you need to run a file with a
specific interpreter but cannot modify the file name.
This commit is contained in:
Matthias Koefferlein 2018-07-12 21:51:51 +02:00
parent f8ffcd14f1
commit 05f8d223ac
2 changed files with 29 additions and 6 deletions

View File

@ -32,6 +32,7 @@
#include "tlClassRegistry.h"
#include "tlLog.h"
#include "tlXMLParser.h"
#include "tlGlobPattern.h"
#include "rba.h"
#include "pya.h"
@ -213,15 +214,18 @@ void Macro::save_to (const std::string &path)
}
}
void Macro::load_from (const std::string &path)
void Macro::load_from (const std::string &fn)
{
m_format = NoFormat;
if (tl::verbosity () >= 20) {
tl::log << "Loading macro from " << path;
}
std::pair<bool, std::string> f = format_from_filename (fn, m_interpreter, m_dsl_interpreter, m_autorun_default, m_format);
if (f.first) {
if (format_from_suffix (path, m_interpreter, m_dsl_interpreter, m_autorun_default, m_format)) {
const std::string &path = f.second;
if (tl::verbosity () >= 20) {
tl::log << "Loading macro from " << path;
}
m_autorun = m_autorun_default;
@ -242,7 +246,7 @@ void Macro::load_from (const std::string &path)
}
} else {
throw tl::Exception (tl::to_string (QObject::tr ("Unable to determine format for file from suffix ")) + path);
throw tl::Exception (tl::to_string (QObject::tr ("Unable to determine format for file from suffix or format spec ")) + fn);
}
m_modified = true;
@ -297,7 +301,24 @@ bool
Macro::format_from_suffix (const std::string &fn, Macro::Interpreter &interpreter, std::string &dsl_name, bool &autorun_pref, Macro::Format &format)
{
std::string suffix = tl::to_string (QFileInfo (tl::to_qstring (fn)).suffix ());
return format_from_suffix_string (suffix, interpreter, dsl_name, autorun_pref, format);
}
std::pair<bool, std::string>
Macro::format_from_filename (const std::string &fn, Macro::Interpreter &interpreter, std::string &dsl_name, bool &autorun_pref, Macro::Format &format)
{
tl::GlobPattern pat ("(*)\\[(*)\\]");
std::vector<std::string> pat_parts;
if (pat.match (fn, pat_parts) && pat_parts.size () == 2) {
return std::make_pair (format_from_suffix_string (pat_parts[1], interpreter, dsl_name, autorun_pref, format), pat_parts[0]);
} else {
return std::make_pair (format_from_suffix (fn, interpreter, dsl_name, autorun_pref, format), fn);
}
}
bool
Macro::format_from_suffix_string (const std::string &suffix, Macro::Interpreter &interpreter, std::string &dsl_name, bool &autorun_pref, Macro::Format &format)
{
interpreter = None;
dsl_name = std::string ();
format = NoFormat;

View File

@ -602,6 +602,8 @@ private:
void on_menu_needs_update ();
void on_changed ();
void save_to (const std::string &path);
static bool format_from_suffix_string (const std::string &suffix, Macro::Interpreter &interpreter, std::string &dsl_name, bool &autorun_pref, Macro::Format &format);
static std::pair<bool, std::string> format_from_filename (const std::string &fn, Macro::Interpreter &interpreter, std::string &dsl_name, bool &autorun_pref, Macro::Format &format);
void set_autorun_default (bool f)
{