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

feature: Added support for opting out endpoint translation #56

Merged
merged 3 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions lib/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ cache.init = function() {
file.mkdir(file.cacheDir());
};

cache.deleteAll = function () {
cache.list().forEach(value => {
cache.del(value.name);
})
};

cache.get = function(k) {
const fullpath = file.cacheFile(k);
if (!file.exist(fullpath)) return null;
Expand Down
6 changes: 6 additions & 0 deletions lib/commands/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ const cmd = {
default: false,
describe: 'Show extra details: category, companies, tags.'
})
.option('T', {
alias: 'dontTranslate',
type: 'boolean',
default: false,
describe: 'Set to true to disable endpoint\'s translation',
})
.positional('keyword', {
type: 'string',
default: '',
Expand Down
10 changes: 8 additions & 2 deletions lib/commands/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ const cmd = {
default: false,
describe: 'Show extra question details in source code'
})
.option('T', {
alias: 'dontTranslate',
type: 'boolean',
default: false,
describe: 'Set to true to disable endpoint\'s translation',
})
.positional('keyword', {
type: 'string',
default: '',
Expand Down Expand Up @@ -175,7 +181,7 @@ cmd.handler = function(argv) {
session.argv = argv;
if (argv.keyword.length > 0) {
// show specific one
core.getProblem(argv.keyword, function(e, problem) {
core.getProblem(argv.keyword, !argv.dontTranslate, function(e, problem) {
if (e) return log.fail(e);
showProblem(problem, argv);
});
Expand All @@ -194,7 +200,7 @@ cmd.handler = function(argv) {
if (problems.length === 0) return log.fail('Problem not found!');

const problem = _.sample(problems);
core.getProblem(problem, function(e, problem) {
core.getProblem(problem, !argv.dontTranslate, function(e, problem) {
if (e) return log.fail(e);
showProblem(problem, argv);
});
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/star.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ const cmd = {

cmd.handler = function(argv) {
session.argv = argv;
core.getProblem(argv.keyword, function(e, problem) {
// translation doesn't affect question lookup
core.getProblem(argv.keyword, true, function(e, problem) {
if (e) return log.fail(e);

core.starProblem(problem, !argv.delete, function(e, starred) {
Expand Down
10 changes: 8 additions & 2 deletions lib/commands/submission.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ const cmd = {
default: false,
describe: 'Show extra question details in submission code'
})
.option('T', {
alias: 'dontTranslate',
type: 'boolean',
default: false,
describe: 'Set to true to disable endpoint\'s translation',
})
.positional('keyword', {
type: 'string',
default: '',
Expand Down Expand Up @@ -69,7 +75,7 @@ function doTask(problem, queue, cb) {

if (argv.extra) {
// have to get problem details, e.g. problem description.
core.getProblem(problem.fid, function(e, problem) {
core.getProblem(problem.fid, !argv.dontTranslate, function(e, problem) {
if (e) return cb(e);
exportSubmission(problem, argv, onTaskDone);
});
Expand Down Expand Up @@ -135,7 +141,7 @@ cmd.handler = function(argv) {
if (!argv.keyword)
return log.fail('missing keyword?');

core.getProblem(argv.keyword, function(e, problem) {
core.getProblem(argv.keyword, !argv.dontTranslate, function(e, problem) {
if (e) return log.fail(e);
q.addTask(problem).run();
});
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ cmd.handler = function(argv) {

const meta = file.meta(argv.filename);

core.getProblem(meta.id, function(e, problem) {
// translation doesn't affect problem lookup
core.getProblem(meta.id, true, function(e, problem) {
if (e) return log.fail(e);

problem.file = argv.filename;
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function runTest(argv) {

const meta = file.meta(argv.filename);

core.getProblem(meta.id, function(e, problem) {
core.getProblem(meta.id, true, function(e, problem) {
if (e) return log.fail(e);

if (!problem.testable)
Expand Down
10 changes: 5 additions & 5 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const QUERY_HANDLERS = {
};

core.filterProblems = function(opts, cb) {
this.getProblems(function(e, problems) {
this.getProblems(!opts.dontTranslate, function(e, problems) {
if (e) return cb(e);

for (let q of (opts.query || '').split('')) {
Expand All @@ -82,11 +82,11 @@ core.filterProblems = function(opts, cb) {
});
};

core.getProblem = function(keyword, cb) {
core.getProblem = function(keyword, needTranslation, cb) {
if (keyword.id)
return core.next.getProblem(keyword, cb);
return core.next.getProblem(keyword, needTranslation, cb);

this.getProblems(function(e, problems) {
this.getProblems(needTranslation, function(e, problems) {
if (e) return cb(e);

keyword = Number(keyword) || keyword;
Expand All @@ -95,7 +95,7 @@ core.getProblem = function(keyword, cb) {
return x.fid + '' === keyword + '' || x.fid + '' === metaFid + '' || x.name === keyword || x.slug === keyword;
});
if (!problem) return cb('Problem not found!');
core.next.getProblem(problem, cb);
core.next.getProblem(problem, needTranslation, cb);
});
};

Expand Down
11 changes: 6 additions & 5 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ const LANGS = [
const h = {};

h.KEYS = {
user: '../user',
stat: '../stat',
plugins: '../../plugins',
problems: 'problems',
problem: p => p.fid + '.' + p.slug + '.' + p.category
user: '../user',
stat: '../stat',
plugins: '../../plugins',
problems: 'problems',
translation: 'translationConfig',
problem: p => p.fid + '.' + p.slug + '.' + p.category
};

h.prettyState = function(state) {
Expand Down
23 changes: 19 additions & 4 deletions lib/plugins/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,37 @@ var session = require('../session');

const plugin = new Plugin(50, 'cache', '', 'Plugin to provide local cache.');

plugin.getProblems = function(cb) {
// this function will clear all caches if needTranslation is different than stored
// it will also store the new needTranslation into cache automatically
function clearCacheIfTchanged(needTranslation) {
const translationConfig = cache.get(h.KEYS.translation);
if (!translationConfig || translationConfig['useEndpointTranslation'] != needTranslation) {
// cache doesn't have the key => old cache version, need to update
// or cache does have the key but it contains a different value
cache.deleteAll();
cache.set(h.KEYS.translation, { useEndpointTranslation: needTranslation });
log.debug('cache cleared: -T option changed');
}
}

plugin.getProblems = function (needTranslation, cb) {
clearCacheIfTchanged(needTranslation);
const problems = cache.get(h.KEYS.problems);
if (problems) {
log.debug('cache hit: problems.json');
return cb(null, problems);
}

plugin.next.getProblems(function(e, problems) {
plugin.next.getProblems(needTranslation, function(e, problems) {
if (e) return cb(e);

cache.set(h.KEYS.problems, problems);
return cb(null, problems);
});
};

plugin.getProblem = function(problem, cb) {
plugin.getProblem = function (problem, needTranslation, cb) {
clearCacheIfTchanged(needTranslation);
const k = h.KEYS.problem(problem);
const _problem = cache.get(k);
if (_problem) {
Expand All @@ -42,7 +57,7 @@ plugin.getProblem = function(problem, cb) {
}
}

plugin.next.getProblem(problem, function(e, _problem) {
plugin.next.getProblem(problem, needTranslation, function(e, _problem) {
if (e) return cb(e);

plugin.saveProblem(_problem);
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/company.js
Original file line number Diff line number Diff line change
Expand Up @@ -1511,8 +1511,8 @@ var TAGS = {
'1148': ['math']
};

plugin.getProblems = function(cb) {
plugin.next.getProblems(function(e, problems) {
plugin.getProblems = function(needTranslation, cb) {
plugin.next.getProblems(needTranslation, function(e, problems) {
if (e) return cb(e);

problems.forEach(function(problem) {
Expand Down
28 changes: 18 additions & 10 deletions lib/plugins/leetcode.cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,29 @@ function checkError(e, resp, expectedStatus) {
return e;
}

plugin.getProblems = function(cb) {
plugin.next.getProblems(function(e, problems) {
// overloading getProblems here to make sure everything related
// to listing out problems can have a chance to be translated.
// NOTE: Details of the problem is translated inside leetcode.js
plugin.getProblems = function (needTranslation, cb) {
plugin.next.getProblems(needTranslation, function(e, problems) {
if (e) return cb(e);

plugin.getProblemsTitle(function(e, titles) {
if (e) return cb(e);
if (needTranslation) {
// only translate titles of the list if user requested
plugin.getProblemsTitle(function (e, titles) {
if (e) return cb(e);

problems.forEach(function(problem) {
const title = titles[problem.id];
if (title)
problem.name = title;
});
problems.forEach(function (problem) {
const title = titles[problem.id];
if (title)
problem.name = title;
});

return cb(null, problems);
});
} else {
return cb(null, problems);
});
}
});
};

Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ plugin.init = function() {
config.app = 'leetcode';
};

plugin.getProblems = function(cb) {
plugin.getProblems = function (needTranslation, cb) {
log.debug('running leetcode.getProblems');
let problems = [];
const getCategory = function(category, queue, cb) {
Expand Down Expand Up @@ -117,7 +117,7 @@ plugin.getCategoryProblems = function(category, cb) {
});
};

plugin.getProblem = function(problem, cb) {
plugin.getProblem = function(problem, needTranslation, cb) {
log.debug('running leetcode.getProblem');
const user = session.getUser();
if (problem.locked && !user.paid) return cb('failed to load locked problem!');
Expand Down Expand Up @@ -161,7 +161,7 @@ plugin.getProblem = function(problem, cb) {
problem.likes = q.likes;
problem.dislikes = q.dislikes;

problem.desc = q.translatedContent ? q.translatedContent : q.content;
problem.desc = (q.translatedContent && needTranslation) ? q.translatedContent : q.content;

problem.templates = JSON.parse(q.codeDefinition);
problem.testcase = q.sampleTestCase;
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/solution.discuss.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ function getSolution(problem, lang, cb) {
});
}

plugin.getProblem = function(problem, cb) {
plugin.next.getProblem(problem, function(e, problem) {
plugin.getProblem = function(problem, needTranslation, cb) {
plugin.next.getProblem(problem, needTranslation, function(e, problem) {
if (e || !session.argv.solution) return cb(e, problem);

var lang = session.argv.lang;
Expand Down
Loading