base.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2021/05/17/
  3. # @Author : ricky
  4. # @File : base.py
  5. # @Software: vscode
  6. """
  7. 关键字修改类基类
  8. """
  9. import os
  10. from shutil import move
  11. from loguru import logger
  12. class BaseCore:
  13. """
  14. 初始化参数:
  15. context (object): 上下文对象
  16. targetdir (str): 目标路径
  17. sitename (str): 新站点名称,
  18. projectdirname (str): 新目录名称
  19. packagename (str): 新包名
  20. projectname (str): 新项目名
  21. artifactid (str): 新artifactId
  22. groupid (str): 新groupId
  23. configdict (dict): 配置字典
  24. callback (function): 回调方法
  25. """
  26. def __init__(self, context, targetdir, sitename, projectdirname,
  27. packagename, projectname, artifactid, groupid, configdict,
  28. callback):
  29. self.context = context
  30. self.targetdir = targetdir
  31. self.sitename = sitename
  32. self.packagename = packagename
  33. self.projectdirname = projectdirname
  34. self.projectname = projectname
  35. self.artifactid = artifactid
  36. self.groupid = groupid
  37. self.configdict = configdict
  38. self.callback = callback
  39. self.exceptions = []
  40. def start(self):
  41. """开始方法"""
  42. pass
  43. def messagehandle(self, message):
  44. """
  45. 消息处理
  46. 参数:
  47. message (str): 消息
  48. """
  49. if hasattr(self.callback, '__call__'):
  50. self.callback(self.context, message)
  51. def exceptionhandle(self, message):
  52. """
  53. 异常处理
  54. 参数:
  55. message (str): 消息
  56. """
  57. self.exceptions.append(str(len(self.exceptions) + 1) + "." + message)
  58. logger.error(message)
  59. def move_dir(self, old_dir, new_dir):
  60. """
  61. 移动目录
  62. 参数:
  63. old_dir (str): 旧目录
  64. new_dir (str): 新目录
  65. """
  66. for temp_path in os.listdir(old_dir):
  67. filepath = new_dir + os.path.sep + temp_path
  68. oldpath = old_dir + os.path.sep + temp_path
  69. if os.path.isdir(oldpath):
  70. os.mkdir(filepath)
  71. self.move_dir(oldpath, filepath)
  72. if os.path.isfile(oldpath):
  73. move(oldpath, filepath)