def merge_chars(dst_path, char_path, column_path): for fn in listdir(dst_path): dst_file = path.join(dst_path, fn) if path.isdir(dst_file): merge_chars(dst_file, char_path, column_path) elif fn.endswith('.json'): char = load_json(path.join(char_path, fn)) column = load_json(path.join(column_path, fn)) assert char and column assert char.get('blocks') assert char.get('chars') assert column.get('columns') assert len(char['blocks']) == len(column['blocks']) and len( char['blocks']) == 1 for i, (a, b) in enumerate(zip(char['blocks'], column['blocks'])): if not (a['x'] == b['x'] and a['y'] == b['y'] and a['w'] == b['w'] and a['h'] == b['h']): print('\t'.join([fn, str(i + 1), str(a), str(b)])) char['blocks'][i].update( dict(x=b['x'], y=b['y'], w=b['w'], h=b['h'], changed=True)) char['columns'] = sorted(column['columns'], key=itemgetter('x'), reverse=True) for i, c in enumerate(char['columns']): c['no'] = c['column_id'] = 'b1c%d' % (i + 1) save_json(char, dst_file)
def load_render(p): if not p.startswith(kind): p = kind + '_' + p filename = path.join(BASE_DIR, 'static', 'pos', pos, *p.split('_')[:-1], p + '.json') page = load_json(filename) if not page: return self.write('error: {0} 页面不存在'.format(p)) if pos in 'char|column|block': if pos + 's' in page: page[pos + 's'] = json.dumps(page[pos + 's']) else: page[pos + 's'] = [] readonly = test or self.get_query_argument( 'readonly', None) or self.lock_page(self, pos, p, False) != p r = self.do_render(p, self.html_files[pos], pos_type=pos_types[pos], readonly=readonly, test=test, messages=messages, page=page, pos=pos, kind=kind, **page, get_img=get_img, txt=get_txt(p)) if isinstance(r, dict): if self.get_query_argument('fix', None): page = load_json(filename) page['chars'] = r['chars'] save_json(page, filename + '~') elif 'force_layout_type' in r: page = load_json(filename) page['layout_type'] = r['force_layout_type'] save_json(page, filename) elif export: mismatch = r.get('mismatch_lines') if mismatch: msg = '跳过 %s\t有%d个不匹配\t%s' % (p, len(mismatch), ','.join(mismatch)) messages.append(msg) logging.warning(msg) else: page = load_json(filename) page['chars'] = r['chars'] logging.info('export ' + p) export[0] += 1 arc.writestr('/'.join([*p.split('_')[:-1], p + '.json']), json.dumps(page, ensure_ascii=False))
def save(self, kind, pos, name, boxes, field=None, layout_type=0): filename = path.join(BASE_DIR, 'static', 'pos', pos, *name.split('_')[:-1], name + '.json') page = load_json(filename) assert page and isinstance(boxes, list) field = field or ('chars' if pos == 'proof' else pos + 's') saved = page.get('layout_type', 0) != layout_type or (page[field] != boxes and boxes) if layout_type: page['layout_type'] = layout_type if page[field] != boxes and boxes: page[field] = boxes if saved: save_json(page, filename) logging.info('%d boxes saved: %s' % (len(boxes), name)) lock_file = PagesHandler.get_lock_file(pos, name) text = [] if path.exists(lock_file): with open(lock_file) as f: text = f.read() if 'saved' in text: text = text.split('\n') else: text = [] with open(lock_file, 'w') as f: text += [ self.get_ip(), self.current_user, get_date_time(), 'saved' ] f.write('\n'.join(text))
def merge_columns(dst_path, char_path): indexes = load_json(path.join(static_path, 'index.json')) indexes['column'] = {} for fn in listdir(char_path): src_file = path.join(char_path, fn) if fn.endswith('.json') and fn[:2] in ['GL', 'QL', 'YB']: dst_file = dst_path for folder in fn.split('_')[:-1]: dst_file = path.join(dst_file, folder) if not path.exists(dst_file): mkdir(dst_file) dst_file = path.join(dst_file, fn) column = load_json(src_file) assert column and column.get('columns') save_json(column, dst_file) indexes['column'][fn[:2]] = indexes['column'].get(fn[:2], []) + [fn[:-5]] save_json(indexes, path.join(static_path, 'index.json'))
def get(self, pos, kind, username=None): def get_icon(p): if options.debug: return '/static/' + '/'.join( ['img', *p.split('_')[:-1], p + '.jpg']) base_url = 'http://tripitaka-img.oss-cn-beijing.aliyuncs.com/pages' url = '/'.join([ base_url, *p.split('_')[:-1], p + '_' + page_codes.get(p) + '.jpg' ]) return url + '?x-oss-process=image/resize,m_lfit,h_300,w_300' def get_info(p): filename = path.join(BASE_DIR, 'static', 'pos', pos, *p.split('_')[:-1], p + '.json') return load_json(filename) page_codes = load_json(path.join(BASE_DIR, 'static/pagecode_hash.json')) pos_type = pos_types[pos] cur_user = self.current_user or self.get_ip() username = username or cur_user me = '\n' + username + '\n' self.unlock_timeout(pos, me) if kind == 'me': pages = self.get_my_pages(pos, username) return self.render('my_pages.html', kinds=kinds, pages=pages, count=len(pages), username=username, pos_type=pos_type, pos=pos, kind=kind, get_icon=get_icon, get_info=get_info) field = pos + ('_invalid' if self.get_query_argument('invalid', 0) == '1' else '') pages, count = self.pick_pages(pos, indexes[field].get(kind, []), 12) html = 'block_pages.html' if pos == 'block' else 'char_pages.html' if pos == 'block': [CutHandler.lock_page(self, pos, name) for name in pages] self.render(html, kinds=kinds, pages=pages, count=count, username=username, invalids={}, pos_type=pos_type, pos=pos, kind=kind, get_icon=get_icon, get_info=get_info)
def get_info(p): filename = path.join(BASE_DIR, 'static', 'pos', pos, *p.split('_')[:-1], p + '.json') return load_json(filename)
# -*- coding: utf-8 -*- from controller.base import load_json, save_json, get_date_time, BaseHandler from tornado.options import options import logging from os import path, listdir, remove import json import random import time import codecs import zipfile BASE_DIR = path.dirname(path.dirname(__file__)) pos_types = dict(block='切栏', column='切列', char='切字', proof='文字') kind_types = {'GL': '高丽藏', 'JX': '嘉兴藏', 'QL': '乾隆藏', 'YB': '永乐北藏'} indexes = load_json(path.join('static', 'index.json')) kinds = {k: {t: kind_types[t] for t in v} for k, v in indexes.items()} class MainHandler(BaseHandler): URL = r'/' def get(self): if 0: with open('page_codes.txt') as f: lines = f.read().split('\n') proof = {"QL": [], "YB": []} for t in lines: proof[t[:2]].append(t) save_json(proof, 'proof.json') self.render('index.html', kinds=kinds, index=indexes, pos='char')