PK! 9##tinydb_constraint/__init__.pyfrom .table import ConstraintTable PK!'<00tinydb_constraint/exception.pyclass ConstraintException(ValueError): pass PK!hAAtinydb_constraint/table.pyfrom tinydb.database import Table import os import dateutil.parser from datetime import datetime from .util import remove_control_chars from .exception import ConstraintException class ConstraintTable(Table): view_dict = None _schema = None def insert(self, element): return super().insert(element) def insert_multiple(self, elements): return super().insert_multiple(elements) def update(self, fields, cond=None, doc_ids=None, eids=None): if doc_ids is None: doc_ids = list() if callable(fields): _update = lambda data, eid: self._sanitize_one(fields(data[eid])) else: _update = lambda data, eid: data[eid].update(self._sanitize_one(fields)) return self.process_elements(_update, cond, doc_ids, eids) def _sanitize_multiple(self, records): """Sanitizes records, e.g. from Excel spreadsheet Arguments: records {iterable} -- Iterable of records Keyword Arguments: schema {dict} -- Dictionary of schemas (default: {None}) table_name {str} -- Table name to get from schema (default: {None}) Returns: list -- List of records """ def _records(): for record in records: record_schema = tuple(self._parse_record(record)) for _k, _v in record_schema: if _v is not table_schema[_k]: raise ConstraintException('{} not in table schema {}'.format(_v, table_schema)) table_schema.update(record_schema) yield dict(self._parse_record(record, yield_type=False)) if bool(int(os.getenv('TINYDB_SANITIZE', '0'))): return records else: table_schema = self.schema for v in table_schema.values(): assert not isinstance(v, (list, tuple, set)) return list(_records()) def _sanitize_one(self, record): return self._sanitize_multiple([record])[0] @property def schema(self): if self._schema is None: self._schema = dict() for record in self.all(): for k, v in self._parse_record(record): self._schema.setdefault(k, set()).add(v) for k, v in self._schema.items(): if len(v) == 1: self._schema[k] = v.pop() else: self._schema[k] = list(v) return self._schema @schema.setter def schema(self, constraint): if self._schema is None: self._schema = dict() self._schema.update(constraint) @staticmethod def _parse_record(record, yield_type=True): def _yield_switch(x): if yield_type: return type(x) else: if isinstance(x, datetime): return x.isoformat() else: return x for k, v in record.items(): if bool(int(os.getenv('TINYDB_DATETIME', '1'))): if isinstance(v, str): v = remove_control_chars(v.strip()) if v.isdigit(): v = int(v) elif '.' in v and v.replace('.', '', 1).isdigit(): v = float(v) elif v in {'', '-'}: continue else: try: v = dateutil.parser.parse(v) except ValueError: pass yield k, _yield_switch(v) PK!GP~>>tinydb_constraint/util.pyimport unicodedata, re all_chars = (chr(i) for i in range(0x110000)) control_chars = ''.join(c for c in all_chars if unicodedata.category(c) in {'Cc'}) control_char_re = re.compile('[%s]' % re.escape(control_chars)) def remove_control_chars(s): return unicodedata.normalize("NFKD", control_char_re.sub('', s)) PK! ::)tinydb_constraint-0.1.0.dist-info/LICENSEMIT License Copyright (c) 2018 Pacharapol Withayasakpunt Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!H WX'tinydb_constraint-0.1.0.dist-info/WHEEL A н#Z."jm)Afb~ڠO68oF04UhoAf f4=4h0k::wXPK!HNA*tinydb_constraint-0.1.0.dist-info/METADATAUmo6_q@,:Nڠ4'HE1ĴtHFRa}w` + *SAEymM 'r,f6uRk|pJ @r,]U1e^aK( ɠ*/ Ex S2/ť-0*ՊH!>V:Lm1*)?JoFߧVH$1\M+w kbQ麍(pz(+roJ1^\y)Nq wSGN[W.7ή* 2dMM̆O'=Ng{|/{<{B@k s蟽 _V; 93\O.:XЄ(KV?F$nG{A_݋#s)qTPX|N+(@ %fd~BLrkTKg xe 7\[L{t4h"Z将r\zvܞOgɼ7UCgggmݠtQZ 8y%Qw AD/;~/[ޚޠ3S)1ȧkz?Rzu֋Sa6cų)6NpVQTXl2 x/GO4BU&mIMU4 4)#^mҶh?-AsTJCE,Jq[jKB06bz.а.SVY*G_$C3*[mĻk5xԸAwk=РktC-z@\IQL44z9PK!H[)(tinydb_constraint-0.1.0.dist-info/RECORDr@}1``q!Т¦ dIh4weUq |9 щdw0 E? _g6›i[Nv4Q7N}"iQ~cT SByPZ*&v5SŗV̢xneYޕL1ʬDZ\*Zm]TCa/^ĕ>!GqtZ \::c 4I{ZMt~ qZ 响X.EaRwM_)௶IYLh筺nc ҵ}=5շ?a "P8|{^frS(BDSwAWf`{QC@yeg4Ci@n5w"]KrIMRxe?5pϿr 2 2xPK! 9##tinydb_constraint/__init__.pyPK!'<00^tinydb_constraint/exception.pyPK!hAAtinydb_constraint/table.pyPK!GP~>>Ctinydb_constraint/util.pyPK! ::)tinydb_constraint-0.1.0.dist-info/LICENSEPK!H WX'9tinydb_constraint-0.1.0.dist-info/WHEELPK!HNA*tinydb_constraint-0.1.0.dist-info/METADATAPK!H[)(^tinydb_constraint-0.1.0.dist-info/RECORDPK/