Coverage for /media/ldata/code/tendril/tendril/dox/docstore.py: 0%

# Coverage for /media/ldata/code/tendril/tendril/dox/docstore.py : 0%

## 127 statements 0 run 127 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | # Copyright (C) 2015 Chintalagiri Shashank # # This file is part of Tendril. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. """ This file is part of tendril See the COPYING, README, and INSTALL files for more information """ import os from fs.opener import fsopendir from fs.utils import copyfile from fs import path from tendril.utils.db import with_db from tendril.entityhub import serialnos from tendril.utils.config import DOCSTORE_ROOT from tendril.utils.config import INSTANCE_ROOT from tendril.utils.config import REFDOC_ROOT from tendril.utils.config import DOCUMENT_WALLET_PREFIX from tendril.utils.config import REFDOC_PREFIX from tendril.utils.config import DOCSTORE_PREFIX from db import controller from wallet import wallet_fs from tendril.utils import log logger = log.get_logger(__name__, log.INFO) docstore_fs = fsopendir(DOCSTORE_ROOT, create_dir=True) workspace_fs = fsopendir(os.path.join(INSTANCE_ROOT, 'scratch'), create_dir=True) refdoc_fs = fsopendir(REFDOC_ROOT) local_fs = fsopendir('/') class ExposedDocument(object): def __init__(self, desc, fspath, fs, ts=None, efield=None): self.desc = desc self.path = fspath self.fs = fs self.ts = ts self.efield = efield self._get_fs_prefix() def _get_fs_prefix(self): if self.fs == refdoc_fs: self._prefix = os.path.join('/expose', REFDOC_PREFIX) elif self.fs == docstore_fs: self._prefix = os.path.join('/expose', DOCSTORE_PREFIX) elif self.fs == wallet_fs: self._prefix = os.path.join('/expose', DOCUMENT_WALLET_PREFIX) @property def exposed_url(self): return os.path.join(self._prefix, self.path) @property def filetype(self): ext = os.path.splitext(self.path)[1] if not ext: return None return ext.strip('.').lower() @property def filename(self): return path.splitext(path.split(self.path)[1])[0] @with_db def list_sno_documents(serialno=None, session=None): if serialno is None: raise AttributeError("sno cannot be None") results = controller.get_sno_documents(serialno=serialno, session=session) print ("Documents for Serial No. : " + serialno) for result in results: print result def get_docs_list_for_serialno(serialno): # TODO This function can be deprecated documents = controller.get_sno_documents(serialno=serialno) rval = [] for document in documents: rval.append(ExposedDocument(document.doctype, document.docpath, docstore_fs, document.created_at, document.efield)) return rval def get_docs_list_for_doctype(doctype, limit=None): # TODO This function can be deprecated documents = controller.get_doctype_documents(doctype=doctype, limit=limit) rval = [] for document in documents: rval.append(ExposedDocument(document.doctype, document.docpath, docstore_fs, document.created_at, document.efield)) return rval def get_docs_list_for_sno_doctype(serialno, doctype, limit=None): documents = controller.get_serialno_doctype_documents(serialno=serialno, doctype=doctype, limit=limit) rval = [] for document in documents: rval.append(ExposedDocument(document.doctype, document.docpath, docstore_fs, document.created_at, document.efield)) return rval @with_db def copy_docs_to_workspace(serialno=None, workspace=None, clearws=False, setwsno=True, fs=None, session=None): if serialno is None: raise AttributeError('serialno cannot be None') if fs is None: fs = workspace_fs if workspace is None: workspace = fs.makeopendir('workspace', recursive=True) elif workspace.startswith('/'): raise ValueError('workspace should be a relative path') else: workspace = fs.makeopendir(workspace, recursive=True) if clearws is True: for p in workspace.listdir(dirs_only=True): workspace.removedir(p, force=True) for p in workspace.listdir(files_only=True): workspace.remove(p) if setwsno is True: with workspace.open('wsno', 'wb') as f: f.write(serialno) for doc in controller.get_sno_documents(serialno=serialno, session=session): docname = os.path.split(doc.docpath)[1] if docname.startswith(serialno): if not os.path.splitext(docname)[0] == serialno: docname = docname[len(serialno) + 1:] copyfile(docstore_fs, doc.docpath, workspace, docname) @with_db def delete_document(docpath, session=None): deregister_document(docpath=docpath, session=session) docstore_fs.remove(docpath) @with_db def deregister_document(docpath=None, session=None): if docpath is None: raise AttributeError('docpath cannot be None') controller.deregister_document(docpath=docpath, session=session) def insert_document(sno, docpath, series): fname = os.path.split(docpath)[1] if not fname.startswith(sno) and \ not os.path.splitext(fname)[0].endswith(sno): fname = sno + '-' + fname if series is None: series = serialnos.get_series(sno) storepath = path.join(series, fname) if not docstore_fs.exists(path.dirname(storepath)): docstore_fs.makedir(path.dirname(storepath), recursive=True) copyfile(local_fs, docpath, docstore_fs, storepath) return storepath @with_db def register_document(serialno=None, docpath=None, doctype=None, efield=None, series=None, session=None): if serialno is None: raise AttributeError("serialno cannot be None") if docpath is None: raise AttributeError('docpath cannot be None') if doctype is None: raise AttributeError('doctype cannot be None') logger.info("Registering document for sno " + str(serialno) + " : " + str(docpath)) # WARNING : This writes the file before actually checking that all is ok. # This may not be a very safe approach. storepath = insert_document(serialno, docpath, series) controller.register_document(serialno=serialno, docpath=storepath, doctype=doctype, efield=efield, session=session) # def clean_docindex(): # pass # # # def clean_docstore(): # pass