開發PlainTasks與JSON的插件

PlainTasks 是款很有名的任務管理插件,具體的介紹在這裡

我最近的工作作務,是開發一款插件,能實現 JSON 文件到 todo 類文件的轉換。

JSON 的格式是這樣的

   1:  {
   2:     "project2":[
   3:        {
   4:           "finish_time":"",
   5:           "status":0,
   6:           "name":"marking a  sublime demo"
   7:        },
   8:        {
   9:           "finish_time":"",
  10:           "status":0,
  11:           "name":"testing"
  12:        },
  13:        {
  14:           "finish_time":"",
  15:           "status":0,
  16:           "name":"programing"
  17:        }
  18:     ],
  19:     "project1":[
  20:        {
  21:           "finish_time":"",
  22:           "status":0,
  23:           "name":"writing a  blog"
  24:        },
  25:        {
  26:           "finish_time":"",
  27:           "status":0,
  28:           "name":"reading a book"
  29:        },
  30:        {
  31:           "finish_time":"",
  32:           "status":0,
  33:           "name":"go to home"
  34:        }
  35:     ]
  36:  }

以下是部分代碼片斷,很喜歡 python 的語法,但我不夠深入,希望高手指正下

   1:   def conver_todo_json(self):
   2:          file_name = self.window.active_view().file_name()
   3:          if not ".todo" in file_name:
   4:              return
   5:   
   6:          rom = '^s*☐s*(.*)$'
   7:          rdm = 's*✔s*(.+?)s*@done?([()dw,.:-/ ]*)s*'
   8:          rpm = '([^(]*?)(?=:)'
   9:   
  10:          json_data = {}
  11:          json_file = re.sub(".todo", '.json', file_name)
  12:          project = 'other'
  13:   
  14:          with open(file_name, "r+", encoding="utf-8") as f:
  15:              for line in f:
  16:                  prj = re.match(rpm, line)
  17:                  if prj:
  18:                      project = prj.groups()[0]
  19:                      json_data[project] = []
  20:                  task_open = re.match(rom, line)
  21:                  if task_open:
  22:                      task_item = {"name": task_open.groups()[0] ,"status":0,"finish_time":""}
  23:                      json_data[project].append(task_item)
  24:                  task_done = re.match(rdm, line)
  25:                  if task_done:
  26:                      task_item = {"name":task_done.groups()[0],"status":1,"finish_time":task_done.groups()[1] }
  27:                      json_data[project].append(task_item)
  28:   
  29:          with open(json_file, "w+", encoding="utf-8") as f:
  30:              json.dump(json_data, f)
  31:   
  32:          self.window.open_file(json_file)

具體的詳細信息在這裡

原文地址:https://www.cnblogs.com/ms_config/p/3173687.html