|
1 |
| -# MySQL Connector/Python - MySQL driver written in Python. |
2 |
| -# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
3 |
| - |
4 |
| -# MySQL Connector/Python is licensed under the terms of the GPLv2 |
5 |
| -# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most |
6 |
| -# MySQL Connectors. There are special exceptions to the terms and |
7 |
| -# conditions of the GPLv2 as it is applied to this software, see the |
8 |
| -# FOSS License Exception |
9 |
| -# <http://www.mysql.com/about/legal/licensing/foss-exception.html>. |
10 |
| -# |
11 |
| -# This program is free software; you can redistribute it and/or modify |
12 |
| -# it under the terms of the GNU General Public License as published by |
13 |
| -# the Free Software Foundation. |
14 |
| -# |
15 |
| -# This program is distributed in the hope that it will be useful, |
16 |
| -# but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
| -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
| -# GNU General Public License for more details. |
19 |
| -# |
20 |
| -# You should have received a copy of the GNU General Public License |
21 |
| -# along with this program; if not, write to the Free Software |
22 |
| -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
23 |
| - |
24 |
| -import json |
25 |
| -import uuid |
26 |
| - |
27 |
| -class DbDoc(object): |
28 |
| - """Represents a generic document in JSON format. |
29 |
| -
|
30 |
| - Args: |
31 |
| - value (object): The value can be a JSON string or a dict. |
32 |
| -
|
33 |
| - Raises: |
34 |
| - Exception: If ``value`` type is not a basestring or dict. |
35 |
| - """ |
36 |
| - def __init__(self, value): |
37 |
| - # TODO: handle exceptions. What happens if it doesn't load properly |
38 |
| - if isinstance(value, dict): |
39 |
| - self.__dict__ = value |
40 |
| - elif isinstance(value, basestring): |
41 |
| - self.__dict__ = json.loads(value) |
42 |
| - else: |
43 |
| - raise Exception("Unable to handle type: ".format(type(value))) |
44 |
| - |
45 |
| - def __getitem__(self, index): |
46 |
| - return self.__dict__[index] |
47 |
| - |
48 |
| - def keys(self): |
49 |
| - return self.__dict__.keys() |
50 |
| - |
51 |
| - def ensure_id(self): |
52 |
| - if "_id" not in self.__dict__: |
53 |
| - self.__dict__["_id"] = str(uuid.uuid4()) |
54 |
| - |
55 |
| - def __str__(self): |
56 |
| - return json.dumps(self.__dict__) |
57 |
| - #return str(self.__dict__) |
| 1 | +# MySQL Connector/Python - MySQL driver written in Python. |
| 2 | +# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
| 3 | + |
| 4 | +# MySQL Connector/Python is licensed under the terms of the GPLv2 |
| 5 | +# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most |
| 6 | +# MySQL Connectors. There are special exceptions to the terms and |
| 7 | +# conditions of the GPLv2 as it is applied to this software, see the |
| 8 | +# FOSS License Exception |
| 9 | +# <http://www.mysql.com/about/legal/licensing/foss-exception.html>. |
| 10 | +# |
| 11 | +# This program is free software; you can redistribute it and/or modify |
| 12 | +# it under the terms of the GNU General Public License as published by |
| 13 | +# the Free Software Foundation. |
| 14 | +# |
| 15 | +# This program is distributed in the hope that it will be useful, |
| 16 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +# GNU General Public License for more details. |
| 19 | +# |
| 20 | +# You should have received a copy of the GNU General Public License |
| 21 | +# along with this program; if not, write to the Free Software |
| 22 | +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | + |
| 24 | +import json |
| 25 | +import uuid |
| 26 | + |
| 27 | + |
| 28 | +class DbDoc(object): |
| 29 | + """Represents a generic document in JSON format. |
| 30 | +
|
| 31 | + Args: |
| 32 | + value (object): The value can be a JSON string or a dict. |
| 33 | +
|
| 34 | + Raises: |
| 35 | + Exception: If ``value`` type is not a basestring or dict. |
| 36 | + """ |
| 37 | + def __init__(self, value): |
| 38 | + # TODO: Handle exceptions. What happens if it doesn't load properly? |
| 39 | + if isinstance(value, dict): |
| 40 | + self.__dict__ = value |
| 41 | + elif isinstance(value, basestring): |
| 42 | + self.__dict__ = json.loads(value) |
| 43 | + else: |
| 44 | + raise Exception("Unable to handle type: ".format(type(value))) |
| 45 | + |
| 46 | + def __getitem__(self, index): |
| 47 | + return self.__dict__[index] |
| 48 | + |
| 49 | + def keys(self): |
| 50 | + return self.__dict__.keys() |
| 51 | + |
| 52 | + def ensure_id(self): |
| 53 | + if "_id" not in self.__dict__: |
| 54 | + self.__dict__["_id"] = str(uuid.uuid4()) |
| 55 | + |
| 56 | + def __str__(self): |
| 57 | + return json.dumps(self.__dict__) |
0 commit comments