def main(): '''enter''' init() args = parse_args(sys.argv[1:]) Config.monkey_patch(args.config) app = make_app() # 开启 log 线程 start_thread(log_worker, daemon = True) app.run( port=args.port )
def make_config(): ''' 根据配置文件组装 config ''' _config = Config() config = _config print(config) workname = os.path.dirname(config._path) config.path = os.path.join(workname, config.path) return config
def delete(self, path): config = Config() full_path = os.path.join(config.path or '', path) try: os.remove(full_path) dir_, _ = os.path.dirname(full_path) os.removedirs(dir_) except: pass return {'status':'success.'}
def put(self, name): config = Config() args = request.json if 'value' in args: config.set(name, args['value']) config.save() return {'status': 'successful'} else: return {'status': 'error.'}
def patch(self, path): ''' 部分更新''' args = request.json config = Config() path = urllib.parse.unquote(path) # full_path = os.path.join(config.path or '', path) article = article_factory(path) meta = args.get('meta', {}) for k, v in meta.items(): article.update_meta(k, v) if args.get('text', None): article.text = text article.save() return {'msg': 'update success.'}
def post(self): args = request.json if not isinstance(args, dict): return {'errCode': 301, 'msg': 'body 值错误!'} config = Config() for key, value in args.items(): config.set(key, value) config.save() return {'status': 'successful.'}
def get(self, path): config = Config() # print(path) # full_path = os.path.join(config.path or '', path) # article = article_factory(full_path) article = article_factory(path) if article is None: return {'err_code': 101, 'msg': '此格式暂不支持'} if os.path.exists(article.full_path): data = { 'meta': article.meta, 'path': article.path, 'text': article.text } return data else: return {'err_code': 100, 'msg': '此文章不存在', 'full_path': article.full_path}
def put(self, path): ''' 更新指定的文章 目录不存在就抛出异常 ''' args = request.json config = Config() text = args.get('text', '') path = urllib.parse.unquote(path) article = article_factory(path) if article.exists() is not True: return {'error_msg':'文章不存在'} meta = args.get('meta', {}) # 更新修改时间 if 'modified' not in meta: meta['modified'] = datetime.datetime.now().strftime(config.date_format) article.meta = meta article.text = args.get('text', '') print(article.meta) article.save() return {'msg': '更新成功!'}
def parse_article_args(): config = Config() parser = reqparse.RequestParser() parser.add_argument('path', type=str, location='json', default=None, help='文章保存地址') parser.add_argument('meta', type=dict, location='json', default=None, help='文章元数据') parser.add_argument('text', type=str, location='json', default='', help='文章不含元数据的正文') parser.add_argument('type', type=str, location='json', default='markdown', help='文章类型(markdown, rst)') args = parser.parse_args() if args['meta'] is None: args['meta'] = {} meta = args['meta'] if meta.get('slug', None) is None: title = meta.get('title', '') meta['slug'] = slugify(title) meta['date'] = meta.get('date', datetime.datetime.now().strftime(config.default_date_format)) meta['modified'] = meta.get('modified', datetime.datetime.now().strftime(config.default_date_format)) ext = 'md' if args['type'] == 'markdown' else 'rst' args['location'] = args.get('path', '{}.{}'.format(meta.get('title', ''), ext)) args['path'] = args['location'] args['meta'] = meta return args
def delete(self, name): config = Config() config.delete(name) return {'message': '删除 {} 成功!'.format(name)}
def get(self, name): config = Config() value = config.get(name) return {'status': '200', 'value': value}
def get(self): config = Config() data = {k.lower(): v for k, v in config._pelicanconf.items()} add_task(['ping', '-c', '4', 'baidu.com']) return data