def make_app(conf_path = None): ''' Args: 从命令行指定 config 的文件位置 ''' if conf_path: Config.monkey_patch(conf_path) config = Config() app = Flask(__name__) app.config['SECRET_KEY'] = 'Security' app.debug = config.server_debug or True from pelican_manager.views import admin_bp, article_bp, setting_bp app.register_blueprint(admin_bp) app.register_blueprint(article_bp) app.register_blueprint(setting_bp) from pelican_manager.api import article_api, setting_api app.register_blueprint(article_api) app.register_blueprint(setting_api) # CORS cors = CORS(app, resources={r'/api/*': {'origins': '*'} }) return app
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): config = Config() paths = [config.path] if config.article_paths: for path in config.article_paths: paths = list(map(lambda p: os.path.join(p, path), paths)) articles = [] for path in paths: for full_path in iterdir(path): article = article_factory(full_path) if article and article.meta.get('title', None): data = { 'meta': article.meta, 'path': article.path, 'text': article.text, } # print(data) yield data
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 print(args) return args
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 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 main(): '''enter''' args = parse_args(sys.argv[1:]) app = make_app(args.config) config = Config() app.run(port=args.port or config.server_port, )
def config(request): config = Config('tests/pelicanconf.py') return config
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()} return data
import markdown import codecs import re, os from pelican_manager.utils import handle_duplicate_name from pelican_manager.config import Config config = Config() class InterfaceNotImpleteException(Exception): pass class NotSupportFormat(Exception): pass def article_factory(path): markdown = ['.md', '.markdown'] rst = ['.rst'] _, ext = os.path.splitext(path) if ext in markdown: return MarkdownArticle(path) else: return None class Article(object): ''' article model 传入一片文章, 读取出 metadata
from pelican_manager.config import Config Config.monkey_patch('tests/pelicanconf.py')