sourcing/sourcing/cli.py

88 lines
2.8 KiB
Python

from . import create_app, model, database, edl
from pprint import pprint
import click
app = create_app('config.default')
@app.cli.command()
@click.argument('user_or_email')
@click.option('--password', prompt=True, hide_input=True)
def reset_password(user_or_email, password):
user = model.User.lookup_user_or_email(user_or_email)
user.set_password(password)
database.session.commit()
print(f'password updated for {user.username} ({user.email})')
@app.cli.command()
@click.argument('hashid')
def parse_link(hashid):
home = 'http://localhost:5000/'
item = model.Item.get_by_hashid(hashid)
pprint(item.parse())
print(item.item_and_title(home))
@app.cli.command()
def all_titles():
home = 'http://localhost:5000/'
titles = model.XanaLink.get_all_titles(home=home)
print(titles.values())
@app.cli.command()
@click.argument('hashid')
def delete_item(hashid):
item = model.Item.get_by_hashid(hashid)
database.session.delete(item)
database.session.commit()
@app.cli.command()
def populate_references():
home = 'http://localhost:5000/'
seen = set()
for ref in model.Reference.query:
seen.add((ref.subject_id, ref.object_id))
for link_obj in model.XanaLink.query:
link = link_obj.parse()
for items in link['facets']:
for i in items:
k, _, v = i.partition(': ')
if k == 'span' and ',' in v:
v = v.partition(',')[0]
item = model.Item.from_external(v, home=home)
if item:
print(link_obj.id, '->', item.id)
as_tuple = (link_obj.id, item.id)
if as_tuple in seen:
continue
ref = model.Reference(subject_id=link_obj.id, object_id=item.id)
database.session.add(ref)
seen.add(as_tuple)
for xanapage in model.XanaPage.query:
doc_edl = edl.parse_edl(xanapage.text)
if 'spans' not in doc_edl or not doc_edl['spans']:
continue
for url, start, length in doc_edl['spans']:
src_doc = model.Item.from_external(url, home=home)
if not src_doc.id:
continue
print(xanapage.id, '->', src_doc.id)
as_tuple = (xanapage.id, src_doc.id)
if as_tuple in seen:
continue
ref = model.Reference(subject_id=xanapage.id, object_id=src_doc.id)
database.session.add(ref)
seen.add(as_tuple)
database.session.commit()
@app.cli.command()
@click.argument('hashid')
def show_references(hashid):
item = model.Item.get_by_hashid(hashid)
print('item_id:', item.id)
print('subjects:', [i.id for i in item.subjects])
print('objects:', [i.id for i in item.objects])