def run(project=None, args=[], **kwargs): """Show status of current project. Keep checking this often! status [item] Args: item: Item from the project you wish to view in more detail. Possible values: - docs: View all docs currently being worked on. Shorthand of `list_docs` command. - context: View the detail of context currently used in the project. """ if project is None: message = "" else: active_doc = "" if "last_loaded_doc" in app.session: active_doc = app.session["last_loaded_doc"] path = docs_path() with ZipFile(path, "r") as zipfile: docs = len(zipfile.namelist()) current_context = project.context["name"] dfcount = mongo.db.data_fields.count({"project_id": project._id}) message = ( "Project name: %s\n" "Last loaded document: %s\n" "Total documents: %d\n" "# data fields: %d\n" "Context: %s" % (project.name, active_doc, docs, dfcount, current_context) ) instruction = ClientInstruction({"message": message}) return [project, instruction]
def list_docs(project): path = docs_path() del project.active_doc project.active_doc = None save_project(project) with ZipFile(path, 'r') as zipfile: instruction = ClientInstruction({ 'pass_project': True, 'pass_docs': True, 'docs': get_doc_infos(project, zipfile), 'project': project.to_dict(), 'page': '#doc-list', 'message': "Listing documents of project %s" % project.name }) return [project, instruction]
def load_doc(name, doctype=None, project=None): """Load document Args: name: Name of document (file must exist in :/username/project/docs.zip: file). doctype: Type of document. 'pdf', 'docx', etc. project: reference to ArthurProject object. Returns: list: List of two objects, ArthurProject and ClientInstruction instance. """ if project is None: instruction = ClientInstruction({"message": "Please load a project."}) else: try: path = docs_path() with ZipFile(path, "r") as docs: text = docs.read(name) doc = ArthurDocument(text=text, doctype=doctype, name=name) project.active_doc = doc save_project(project) instruction = ClientInstruction( { "pass_project": True, "data_fields": get_docblocks(project, name), "page": "#doc-view", "message": 'Document "%s" loaded' % name, } ) except OSError as e: if e.errno == errno.EEXIST: instruction = ClientInstruction({"message": "File %s does not exist." % name}) return (project, instruction) else: instruction = ClientInstruction({"message": "OSError: " + e[1]}) return (project, instruction) except IOError as e: instruction = ClientInstruction({"message": "IOError: " + e[1]}) return (project, instruction) except KeyError as e: # Usually for when active_doc not found. instruction = ClientInstruction({"message": "KeyError: " + str(e)}) return (project, instruction) return (project, instruction)
def run(project = None, args = [], **kwargs): """Load all documents inside a zip file in server into currently active project. Runs 'upload_zip' instead if file does not exist. usage: load_zip [--keep] [--nuke] name Args: name: Zip file to load. Optional arguments: --keep, -k By default, load_zip will remove uploaded zip file. Add this option to keep that file. --nuke, -n ☠ Destroy all previous documents in this project and recreate them. ☠ --overwrite_corpus, -o Overwrite corpus as they are created. """ if project is None: instruction = ClientInstruction({'message': 'Please load a project.'}) else: if len(args) == 0: docs = get_docs('load_zip') instruction = ClientInstruction({ 'message': "\n".join(docs) }) else: parser = ArgumentParser(add_help=False) parser.add_argument('name') parser.add_argument('--keep', '-k', action='store_true') parser.add_argument('--nuke', '-n', action='store_true') parser.add_argument('--overwrite_corpus', '-o', action='store_true') parsed_args = parser.parse_args(args) name = parsed_args.name keep = parsed_args.keep nuke = parsed_args.nuke frompath = os.path.join(uploaded_path(), name) project, instruction = load_zip(project, docs_path(), frompath, keep=keep, nuke=nuke, connection=kwargs['connection'], mongo=mongo) save_project(project) return [project, instruction]