From 58f9be5f9a1c3d33ca1d7e20f8492c89bbd69649 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sun, 21 Sep 2025 11:19:00 -0400 Subject: [PATCH] fix: refactor --- lua/cp/health.lua | 8 ++++---- lua/cp/pickers/init.lua | 8 ++++---- lua/cp/scrape.lua | 43 +++++++--------------------------------- lua/cp/utils.lua | 44 +++++++++++++++++++++++++++++++++++++++++ lua/cp/version.lua | 5 +++-- 5 files changed, 62 insertions(+), 46 deletions(-) create mode 100644 lua/cp/utils.lua diff --git a/lua/cp/health.lua b/lua/cp/health.lua index af89cc8..563c065 100644 --- a/lua/cp/health.lua +++ b/lua/cp/health.lua @@ -1,5 +1,7 @@ local M = {} +local utils = require('cp.utils') + local function check_nvim_version() if vim.fn.has('nvim-0.10.0') == 1 then vim.health.ok('Neovim 0.10.0+ detected') @@ -22,8 +24,7 @@ local function check_uv() end local function check_python_env() - local plugin_path = debug.getinfo(1, 'S').source:sub(2) - plugin_path = vim.fn.fnamemodify(plugin_path, ':h:h:h') + local plugin_path = utils.get_plugin_path() local venv_dir = plugin_path .. '/.venv' if vim.fn.isdirectory(venv_dir) == 1 then @@ -34,8 +35,7 @@ local function check_python_env() end local function check_scrapers() - local plugin_path = debug.getinfo(1, 'S').source:sub(2) - plugin_path = vim.fn.fnamemodify(plugin_path, ':h:h:h') + local plugin_path = utils.get_plugin_path() local scrapers = { 'atcoder.py', 'codeforces.py', 'cses.py' } for _, scraper in ipairs(scrapers) do diff --git a/lua/cp/pickers/init.lua b/lua/cp/pickers/init.lua index a90cd8d..e2920ef 100644 --- a/lua/cp/pickers/init.lua +++ b/lua/cp/pickers/init.lua @@ -3,6 +3,7 @@ local M = {} local cache = require('cp.cache') local logger = require('cp.log') local scrape = require('cp.scrape') +local utils = require('cp.utils') ---@class cp.PlatformItem ---@field id string Platform identifier (e.g. "codeforces", "atcoder", "cses") @@ -36,12 +37,11 @@ end local function get_contests_for_platform(platform) local contests = {} - local function get_plugin_path() - local plugin_path = debug.getinfo(1, 'S').source:sub(2) - return vim.fn.fnamemodify(plugin_path, ':h:h:h') + if not utils.setup_python_env() then + return contests end - local plugin_path = get_plugin_path() + local plugin_path = utils.get_plugin_path() local cmd = { 'uv', 'run', diff --git a/lua/cp/scrape.lua b/lua/cp/scrape.lua index f8a5e31..88ab166 100644 --- a/lua/cp/scrape.lua +++ b/lua/cp/scrape.lua @@ -15,42 +15,13 @@ local M = {} local cache = require('cp.cache') local logger = require('cp.log') local problem = require('cp.problem') - -local function get_plugin_path() - local plugin_path = debug.getinfo(1, 'S').source:sub(2) - return vim.fn.fnamemodify(plugin_path, ':h:h:h') -end +local utils = require('cp.utils') local function check_internet_connectivity() local result = vim.system({ 'ping', '-c', '1', '-W', '3', '8.8.8.8' }, { text = true }):wait() return result.code == 0 end -local function setup_python_env() - local plugin_path = get_plugin_path() - local venv_dir = plugin_path .. '/.venv' - - if vim.fn.executable('uv') == 0 then - logger.log( - 'uv is not installed. Install it to enable problem scraping: https://docs.astral.sh/uv/', - vim.log.levels.WARN - ) - return false - end - - if vim.fn.isdirectory(venv_dir) == 0 then - logger.log('setting up Python environment for scrapers...') - local result = vim.system({ 'uv', 'sync' }, { cwd = plugin_path, text = true }):wait() - if result.code ~= 0 then - logger.log('failed to setup Python environment: ' .. result.stderr, vim.log.levels.ERROR) - return false - end - logger.log('python environment setup complete') - end - - return true -end - ---@param platform string ---@param contest_id string ---@return {success: boolean, problems?: table[], error?: string} @@ -77,14 +48,14 @@ function M.scrape_contest_metadata(platform, contest_id) } end - if not setup_python_env() then + if not utils.setup_python_env() then return { success = false, error = 'Python environment setup failed', } end - local plugin_path = get_plugin_path() + local plugin_path = utils.get_plugin_path() local args = { 'uv', @@ -182,7 +153,7 @@ function M.scrape_problem(ctx) } end - if not setup_python_env() then + if not utils.setup_python_env() then return { success = false, problem_id = ctx.problem_name, @@ -190,7 +161,7 @@ function M.scrape_problem(ctx) } end - local plugin_path = get_plugin_path() + local plugin_path = utils.get_plugin_path() local args if ctx.contest == 'cses' then @@ -308,11 +279,11 @@ function M.scrape_problems_parallel(platform, contest_id, problems, config) return {} end - if not setup_python_env() then + if not utils.setup_python_env() then return {} end - local plugin_path = get_plugin_path() + local plugin_path = utils.get_plugin_path() local jobs = {} for _, prob in ipairs(problems) do diff --git a/lua/cp/utils.lua b/lua/cp/utils.lua new file mode 100644 index 0000000..36414d7 --- /dev/null +++ b/lua/cp/utils.lua @@ -0,0 +1,44 @@ +local M = {} + +local logger = require('cp.log') + +---@return string +function M.get_plugin_path() + local plugin_path = debug.getinfo(1, 'S').source:sub(2) + return vim.fn.fnamemodify(plugin_path, ':h:h:h') +end + +local python_env_setup = false + +---@return boolean success +function M.setup_python_env() + if python_env_setup then + return true + end + + local plugin_path = M.get_plugin_path() + local venv_dir = plugin_path .. '/.venv' + + if vim.fn.executable('uv') == 0 then + logger.log( + 'uv is not installed. Install it to enable problem scraping: https://docs.astral.sh/uv/', + vim.log.levels.WARN + ) + return false + end + + if vim.fn.isdirectory(venv_dir) == 0 then + logger.log('setting up Python environment for scrapers...') + local result = vim.system({ 'uv', 'sync' }, { cwd = plugin_path, text = true }):wait() + if result.code ~= 0 then + logger.log('failed to setup Python environment: ' .. result.stderr, vim.log.levels.ERROR) + return false + end + logger.log('python environment setup complete') + end + + python_env_setup = true + return true +end + +return M diff --git a/lua/cp/version.lua b/lua/cp/version.lua index b454a07..308fe01 100644 --- a/lua/cp/version.lua +++ b/lua/cp/version.lua @@ -1,8 +1,9 @@ local M = {} +local utils = require('cp.utils') + local function get_git_version() - local plugin_path = debug.getinfo(1, 'S').source:sub(2) - local plugin_root = vim.fn.fnamemodify(plugin_path, ':h:h:h') + local plugin_root = utils.get_plugin_path() local result = vim .system({ 'git', 'describe', '--tags', '--always', '--dirty' }, {