-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask.py
More file actions
194 lines (166 loc) · 6.27 KB
/
Copy pathtask.py
File metadata and controls
194 lines (166 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python3
# coding=utf-8
import os
import subprocess
import requests
import yaml
import json
import sys
from pathlib import Path
from loguru import logger
from yuque import YuQue
from lake2md import lake_to_md
from dotenv import dotenv_values
user_config = dotenv_values(".env")
class Config:
def __init__(self, prefix):
try:
pwd = Path(__file__).absolute()
file_path = Path(pwd).parent / 'config.json'
logger.info(file_path)
with open(file_path, 'r') as f:
config = json.load(f)
if prefix in config.keys():
self.token = config[prefix]['token']
# self.namespace = config[prefix]['token']
self.basedir = config[prefix]['basedir']
self.desdir = Path(self.basedir, config[prefix]['desdir'])
self.workdir = Path(self.basedir, config[prefix]['workdir'])
self.cmd = config[prefix]['cmd']
self.conf = config[prefix]['hugo']
else:
logger.debug("配置不正确")
except OSError as e:
logger.exception(e)
def deploy(self):
if self.cmd != '':
os.chdir(self.workdir)
return subprocess.Popen(self.cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding="utf-8")
else:
logger.debug("命令为空")
def init_doc(prefix, namespace):
config = Config(prefix)
yq = YuQue(config.token)
trees = yq.get_info(namespace)
logger.debug("文档树的内容:{}", trees)
# 把目录写到本地,以便于更新和删除
tree_path = Path(config.desdir, prefix + '.json')
with open(tree_path, 'w+') as f:
json.dump(trees, f, indent=6)
# 遍历目录树
for tree in trees:
path = Path(config.desdir, tree['path'])
if Path(path).exists():
pass
else:
Path(path).mkdir(parents=True, exist_ok=True)
doc = yq.get_doc(namespace, tree['slug'])
if doc['format'] == 'lake':
file = Path(config.desdir, Path(tree['path']), tree['title'] + '.md')
with open(file, 'w') as f:
md_doc = lake_to_md(doc['body'], doc['title'])
f.writelines(md_doc)
logger.info("{}---已经下载", doc['title'])
else:
logger.info("{doc_title}的格式为{doc_format}---格式不支持跳过", doc_title=doc['title'], doc_format=doc['format'])
def init_config(token, prefix):
pwd = Path(__file__).absolute()
file_path = Path(pwd).parent / 'config.json'
with open(file_path, 'r') as f:
config = json.load(f)
basedir = user_config.get('BASEDIR', Path.home())
desdir = Path(basedir, prefix, user_config.get('DESDIR', 'content'))
workdir = Path(basedir, prefix)
if workdir.exists():
os.chdir(workdir)
else:
workdir.mkdir(parents=True, exist_ok=True)
os.chdir(workdir)
subprocess.call("hugo new site .",shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding="utf-8")
desdir.mkdir(parents=True, exist_ok=True)
conf = {
prefix: {
"token": token,
"basedir": str(basedir),
"desdir": str(desdir),
"workdir": str(workdir),
"cmd": "hugo",
"hugo": {
"html": True,
"shortcode": False
}
}
}
if prefix not in config:
config.update(conf)
logger.debug(config)
with open(file_path, 'w') as f:
json.dump(config, f, indent = 6)
logger.debug("{}已经添加到配置", prefix)
else:
logger.debug("{}已经存在了", prefix)
def publish_doc(slug, doc, title, prefix, namespace):
config = Config(prefix)
# 获取目录列表
yq = YuQue(config.token)
trees = yq.get_info(namespace)
tree_path = Path(config.desdir, prefix + '.json')
with open(tree_path, 'w+') as f:
json.dump(trees, f, indent=6)
for tree in trees:
if tree['slug'] == slug:
path = Path(config.desdir, tree['path'])
if Path(path).exists():
pass
else:
Path(path).mkdir(parents=True)
doc = yq.get_doc(namespace, tree['slug'])
file = Path(config.desdir, Path(tree['path']), tree['title'] + '.md')
with open(file, 'w') as f:
# md_doc = hugo_lake_to_md(doc, tree['title'], html=config.html)
md_doc = lake_to_md(doc['body'], tree['title'])
f.writelines(md_doc)
title = tree['title']
else:
pass
config.deploy()
logger.info("知识库{}发布了一遍名为<<{}>>的文章并已部署!", namespace, title)
def delete_doc(slug, prefix, namespace):
config = Config(prefix)
tree_path = Path(config.desdir, prefix + '.json')
with open(tree_path, 'r') as f:
trees = json.load(f)
for tree in trees:
if tree['slug'] == slug:
path = Path(config.desdir, tree['path'], tree['title'] + '.md')
Path(path).unlink()
title = tree['title']
config.deploy()
logger.info("知识库{}删除了一篇名为<<{}>>的文章!", namespace, title)
def update_doc(slug, doc, title, prefix, namespace):
config = Config(prefix)
yq = YuQue(config.token)
tree_path = Path(config.desdir, prefix + '.json')
with open(tree_path, 'r') as f:
trees = json.load(f)
for tree in trees:
if tree['slug'] == slug:
path = Path(config.desdir, tree['path'])
if Path(path).exists():
pass
else:
Path(path).mkdir(parents=True)
logger.debug("文档已被修改或移动,直接覆盖")
file = Path(path, tree['title'] + '.md')
doc = yq.get_doc(namespace, tree['slug'])
with open(file, 'w') as f:
md_doc = lake_to_md(doc['body'], tree['title'])
f.writelines(md_doc)
title = tree['title']
else:
pass
config.deploy()
logger.info("知识库{}更新了一篇<<{}>>的文章!", namespace, title)
if __name__ == '__main__':
# init_doc('zjan-bwcmnq')
init_config('cccc', 'abcd')