scribble 1.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
scribble/__init__.py ADDED
@@ -0,0 +1,68 @@
1
+ """
2
+ Scibble is a mechanism to store dictionary information to a persistent
3
+ state. This allows the data to be easily retrieved and edited, making it
4
+ a good option when trying to store settings data.
5
+
6
+ Note: Data saved within the ScribbleDictionary must be json serialisable.
7
+
8
+ An example of creating a persisten data block would be:
9
+
10
+ ..code-block:: python
11
+
12
+ import os
13
+ import scribble
14
+
15
+
16
+ # -- Instance a scribble dictionary. We can treat this exactly
17
+ # -- as we treat a dictionary
18
+ data = scribble.get('foobar')
19
+
20
+ # -- Set some key/value pairs in the ScribbleDictionary
21
+ data['option_a'] = True
22
+ data['option_b'] = 123
23
+
24
+ # -- Calling .save() will trigger the dictionary
25
+ # -- to store its current state to a persistent state
26
+ data.save()
27
+
28
+ # -- We can see the location the data is saved to, and we can
29
+ # -- see that it does indeed exist
30
+ print(data.location())
31
+ print(os.path.exists(data.location()))
32
+
33
+
34
+ Equally, we can re-retrieve that data in a completely new instance of python
35
+ using the following code:
36
+
37
+ ..code-block:: python
38
+
39
+ # -- Instance a scribble dictionary with the same identifier
40
+ data = scribble.get('foobar')
41
+
42
+ # -- Print the fact that we have retrieved the same
43
+ # -- infromation
44
+ print(data)
45
+ # {'option_a': True, 'option_b': 123}
46
+
47
+ # -- We can then further edit the data
48
+ data['option_a'] = False
49
+
50
+ # -- Calling .save() will trigger the dictionary
51
+ # -- to store its current state to a persistent state
52
+ data.save()
53
+
54
+ By default scribble will store its data in the follow platform specific
55
+ locations:
56
+
57
+ * Windows: %APPDATA%/pyscribble
58
+
59
+ * Linux: %XDG_CONFIG_HOME%/pyscribble if the environment variable exists, otherwise %HOME%/.config/pyscribble
60
+
61
+ * OSX: Not yet supported
62
+
63
+ However, you can override these paths by setting an environment variable
64
+ PYSCRIBBLE_STORAGE_DIR, if this is set then the path defined by that variable
65
+ will always be used over the default behaviour.
66
+ """
67
+ from .core import get
68
+ from .core import STORAGE_DIRECTORY
scribble/core.py ADDED
@@ -0,0 +1,144 @@
1
+ import os
2
+ import sys
3
+ import json
4
+
5
+
6
+ # -- This is a special environment variable which the user can set in
7
+ # -- order to tailor exactly where scribble will write out its data
8
+ ENVIRONMENT_VARIABLE = 'PYSCRIBBLE_STORAGE_DIR'
9
+
10
+ # -- Determine where we should store our data files. This is dependent
11
+ # -- upon a couple of factors. Firstly, we allow for hte user to define
12
+ # -- an environment variable which specifies the storage location
13
+ STORAGE_DIRECTORY = os.environ.get(ENVIRONMENT_VARIABLE, None)
14
+
15
+ # -- If we have not been provided with a specific storage location
16
+ # -- then we need to resolve to a default location, but this is platform
17
+ # -- dependent
18
+ if not STORAGE_DIRECTORY:
19
+ if sys.platform == 'linux' or sys.platform == 'linux2':
20
+ if 'XDG_CONFIG_HOME' in os.environ:
21
+ STORAGE_DIRECTORY = os.path.join(
22
+ os.environ['XDG_CONFIG_HOME'],
23
+ 'pyscribble',
24
+ )
25
+
26
+ else:
27
+ STORAGE_DIRECTORY = os.path.join(
28
+ os.environ['HOME'],
29
+ '.config',
30
+ 'pyscribble',
31
+ )
32
+
33
+ elif sys.platform == 'win32':
34
+ STORAGE_DIRECTORY = os.path.join(
35
+ os.environ.get('APPDATA'),
36
+ 'pyscribble',
37
+ )
38
+
39
+ elif sys.platform == 'darwin':
40
+ STORAGE_DIRECTORY = os.path.join(
41
+ os.environ['HOME'],
42
+ 'Documents',
43
+ 'pyscribble',
44
+ )
45
+
46
+ else:
47
+ raise Exception(
48
+ (
49
+ '%s is not supported by default. In order to utilise this module '
50
+ 'you must define an environment variable (%s) specifying the '
51
+ 'storage path'
52
+ ) % (
53
+ sys.platform,
54
+ ENVIRONMENT_VARIABLE,
55
+ )
56
+ )
57
+
58
+
59
+ # ------------------------------------------------------------------------------
60
+ def get(identifier, *args, **kwargs):
61
+ """
62
+ This will attempt to retrieve a Scribble Dictionary from the
63
+ STORAGE_DIRECTORY. If it does not exist, an empty Scribble
64
+ Dictionary will be created which will be savable with the given
65
+ identifier.
66
+
67
+ :param identifier: Unique identifier to access a specific scribble
68
+ dictionary.
69
+ :type identifier: str
70
+
71
+ :return: ScribbleDictionary
72
+ """
73
+ return ScribbleDictionary(identifier, *args, **kwargs)
74
+
75
+
76
+ # ------------------------------------------------------------------------------
77
+ class ScribbleDictionary(dict):
78
+
79
+ # --------------------------------------------------------------------------
80
+ def __init__(self, identifier, *args, **kwargs):
81
+ super(ScribbleDictionary, self).__init__(*args, **kwargs)
82
+
83
+ # -- Store the identifier, as this is used whenever the
84
+ # -- scribble dictionary is stored.
85
+ self.identifier = identifier
86
+
87
+ # -- Initiate a load of any persistent data for the given
88
+ # -- identifier
89
+ self.load()
90
+
91
+ # --------------------------------------------------------------------------
92
+ def load(self):
93
+ """
94
+ Loads the data from the disk if it exists and updates this
95
+ dictionary.
96
+ """
97
+ if os.path.exists(self.location()):
98
+ with open(self.location(), 'r') as f:
99
+ self.update(json.load(f))
100
+
101
+ # --------------------------------------------------------------------------
102
+ def save(self):
103
+ """
104
+ Saves the ScribbleDictionary data to a persistent state
105
+ """
106
+ # -- Ensure the location to save to exists, otherwise the
107
+ # -- write will fail
108
+ if not os.path.exists(STORAGE_DIRECTORY):
109
+ os.makedirs(STORAGE_DIRECTORY)
110
+
111
+ # -- Serialise to json - we wrap this in a try/except in case
112
+ # -- the data is not json serialisable.
113
+ try:
114
+ data = json.dumps(
115
+ self,
116
+ indent=4,
117
+ sort_keys=True,
118
+ )
119
+
120
+ except BaseException:
121
+ raise Exception(
122
+ 'Could not encode the data within the Scribble Dictionary '
123
+ 'to JSON. Please ensure any stored data can be serialised '
124
+ 'to JSON.'
125
+ )
126
+
127
+ # -- Write the data out
128
+ with open(self.location(), 'w') as f:
129
+ f.write(data)
130
+
131
+ # --------------------------------------------------------------------------
132
+ def location(self):
133
+ """
134
+ Returns the location of the persistent file. This is regardless of
135
+ whether the file exists or not.
136
+
137
+ :return: str
138
+ """
139
+ return os.path.join(
140
+ STORAGE_DIRECTORY,
141
+ '%s.json' % (
142
+ self.identifier,
143
+ ),
144
+ )
@@ -0,0 +1,93 @@
1
+ Metadata-Version: 2.4
2
+ Name: scribble
3
+ Version: 1.0.1
4
+ Summary: Allows for persistent storage of dictionary data
5
+ Home-page: https://github.com/mikemalinowski/scribble
6
+ Author: Mike Malinowski
7
+ Author-email: mike@twisted.space
8
+ Keywords: scribble dictionary appdata persistent
9
+ Classifier: Programming Language :: Python
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Dynamic: author
15
+ Dynamic: author-email
16
+ Dynamic: classifier
17
+ Dynamic: description
18
+ Dynamic: description-content-type
19
+ Dynamic: home-page
20
+ Dynamic: keywords
21
+ Dynamic: license-file
22
+ Dynamic: summary
23
+
24
+ # Overview
25
+ Scibble is a mechanism to store dictionary information to a persistent
26
+ state. This allows the data to be easily retrieved and edited, making it
27
+ a good option when trying to store settings data.
28
+
29
+ Note: Data saved within the ScribbleDictionary must be json serialisable.
30
+
31
+ An example of creating a persisten data block would be:
32
+ ```python
33
+ import os
34
+ import scribble
35
+
36
+
37
+ # -- Instance a scribble dictionary. We can treat this exactly
38
+ # -- as we treat a dictionary
39
+ data = scribble.get('foobar')
40
+
41
+ # -- Set some key/value pairs in the ScribbleDictionary
42
+ data['option_a'] = True
43
+ data['option_b'] = 123
44
+
45
+ # -- Calling .save() will trigger the dictionary
46
+ # -- to store its current state to a persistent state
47
+ data.save()
48
+
49
+ # -- We can see the location the data is saved to, and we can
50
+ # -- see that it does indeed exist
51
+ print(data.location())
52
+ print(os.path.exists(data.location()))
53
+ ```
54
+
55
+
56
+ Equally, we can re-retrieve that data in a completely new instance of python
57
+ using the following code:
58
+ ```python
59
+ # -- Instance a scribble dictionary with the same identifier
60
+ data = scribble.get('foobar')
61
+
62
+ # -- Print the fact that we have retrieved the same
63
+ # -- infromation
64
+ print(data)
65
+ # {'option_a': True, 'option_b': 123}
66
+
67
+ # -- We can then further edit the data
68
+ data['option_a'] = False
69
+
70
+ # -- Calling .save() will trigger the dictionary
71
+ # -- to store its current state to a persistent state
72
+ data.save()
73
+ ```
74
+ By default scribble will store its data in the follow platform specific
75
+ locations:
76
+
77
+ * Windows: %APPDATA%/pyscribble
78
+
79
+ * Linux: %XDG_CONFIG_HOME%/pyscribble if the environment variable exists, otherwise %HOME%/.config/pyscribble
80
+
81
+ * OSX: ËœHOME/Documents/pyscribble
82
+
83
+ However, you can override these paths by setting an environment variable
84
+ PYSCRIBBLE_STORAGE_DIR, if this is set then the path defined by that variable
85
+ will always be used over the default behaviour.
86
+
87
+ ## Compatability
88
+
89
+ This has been tested under Python 2.7.13 and Python 3.6.6 on both Ubuntu and Windows.
90
+
91
+ ## Contribute
92
+
93
+ If you would like to contribute thoughts, ideas, fixes or features please get in touch! mike@twisted.space
@@ -0,0 +1,7 @@
1
+ scribble/__init__.py,sha256=tm9-QXONvmJUCdqB8m24ik1GBjf_fIOwOwXD3obQO3A,2160
2
+ scribble/core.py,sha256=oFvnTJQ7BxfU3nvzUd7g-CTQWwqX5iqT8G1-xFLuptY,4808
3
+ scribble-1.0.1.dist-info/licenses/LICENSE,sha256=RBhg5hIyHCQyj9Hd6pdx4QVzRdIfplLdnbuNB0fm2H4,1093
4
+ scribble-1.0.1.dist-info/METADATA,sha256=lioTEtcW7t6MQ21L1q4fItXHGI4z_-HD56MRnZs-mBc,2988
5
+ scribble-1.0.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
6
+ scribble-1.0.1.dist-info/top_level.txt,sha256=6I5a3Lk0uFVFFBlCyocku3xmL-w01LD3HQraNk7u0Qg,9
7
+ scribble-1.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2018 The Python Packaging Authority
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ scribble