cluedin-magic 0.1.0__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.
@@ -0,0 +1,5 @@
1
+ from .cluedin_magic import CluedInMagics
2
+
3
+
4
+ def load_ipython_extension(ipython):
5
+ ipython.register_magics(CluedInMagics)
@@ -0,0 +1,106 @@
1
+ # pylint: disable=missing-module-docstring missing-class-docstring missing-function-docstring
2
+ import base64
3
+ import itertools
4
+ import json
5
+ from urllib.parse import urlparse
6
+
7
+ import cluedin
8
+ import pandas as pd
9
+ from IPython.core.magic import Magics, line_magic, magics_class
10
+
11
+
12
+ @magics_class
13
+ class CluedInMagics(Magics):
14
+
15
+ @line_magic
16
+ def cluedin(self, line):
17
+ command, _, args = line.partition(' ')
18
+ command_handlers = {
19
+ 'get-context': self._handle_get_context,
20
+ 'search': self._handle_search,
21
+ }
22
+
23
+ if command in command_handlers:
24
+ return command_handlers[command](args.strip())
25
+
26
+ base_help = f'Available commands: {", ".join(command_handlers.keys())}\n' + \
27
+ 'Usage:\n' + \
28
+ '%cluedin get-context --jwt <jwt>\n' + \
29
+ '%cluedin search --context <context> --query <query> [--limit <limit>]'
30
+
31
+ if not command or command == '--help':
32
+ print(base_help)
33
+ return
34
+
35
+ print(f'Unknown command: "{command}".\n{base_help}')
36
+
37
+ def _handle_get_context(self, line):
38
+ params = self.parse_params(line)
39
+
40
+ if 'jwt' not in params:
41
+ print(
42
+ 'Missing required parameter: --jwt.\n' +
43
+ 'Usage: %cluedin get-context --jwt <jwt>.')
44
+ return None
45
+
46
+ try:
47
+ return self._get_context(params['jwt'])
48
+ # pylint: disable=broad-except
49
+ except Exception as e:
50
+ print(e)
51
+ return
52
+
53
+ def _get_context(self, jwt):
54
+ _, payload, _ = jwt.split('.')
55
+ decoded_payload = base64.b64decode(
56
+ payload + '=' * (4 - len(payload) % 4))
57
+ jwt_json = json.loads(decoded_payload.decode('utf-8'))
58
+
59
+ url = urlparse(jwt_json['iss'])
60
+ context = cluedin.Context.from_dict({
61
+ 'protocol': url.scheme,
62
+ 'domain': '.'.join(url.hostname.split('.')[1:]),
63
+ 'org_name': jwt_json['ClientId'],
64
+ 'access_token': jwt
65
+ })
66
+
67
+ return context
68
+
69
+ def _handle_search(self, args):
70
+ params = self.parse_params(args)
71
+
72
+ if 'context' not in params:
73
+ print(
74
+ 'Missing required parameter: --context.\n' +
75
+ 'Usage: %cluedin search --context <context> --query <query> [--limit <limit>].')
76
+ return None
77
+
78
+ if 'query' not in params:
79
+ print(
80
+ 'Missing required parameter: --query.\n' +
81
+ 'Usage: %cluedin search --context <context> --query <query> [--limit <limit>].')
82
+ return None
83
+
84
+ ctx = self.shell.user_ns[params['context']]
85
+
86
+ if 'limit' not in params:
87
+ return pd.DataFrame(cluedin.gql.search(ctx, params['query']))
88
+ return pd.DataFrame(
89
+ itertools.islice(
90
+ cluedin.gql.search(ctx,
91
+ params['query'],
92
+ min(int(params['limit']), 10_000)),
93
+ int(params['limit'])))
94
+
95
+ @staticmethod
96
+ def parse_params(params):
97
+ parsed_params = {}
98
+ tokens = params.split('--')[1:]
99
+
100
+ for token in tokens:
101
+ key_value = token.split(None, 1)
102
+ key = key_value[0]
103
+ value = key_value[1] if len(key_value) > 1 else ''
104
+ parsed_params[key] = value.strip()
105
+
106
+ return parsed_params
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Roman Klimenko roman@klimenko.dk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,57 @@
1
+ Metadata-Version: 2.1
2
+ Name: cluedin-magic
3
+ Version: 0.1.0
4
+ Summary: IPython magic commands for CluedIn
5
+ License: MIT
6
+ Author: Roman Klimenko
7
+ Author-email: roman@klimenko.dk
8
+ Requires-Python: >=3.10,<4.0
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Requires-Dist: cluedin (>=2.5.0,<3.0.0)
15
+ Requires-Dist: ipython (>=8.26.0,<9.0.0)
16
+ Requires-Dist: pandas (>=2.2.2,<3.0.0)
17
+ Description-Content-Type: text/markdown
18
+
19
+ # CluedIn IPython magic commands
20
+
21
+ This module provides IPython [magic](https://ipython.readthedocs.io/en/stable/interactive/python-ipython-diff.html#magics) commands for interacting with the CluedIn API.
22
+
23
+ ```python
24
+ # Install the package.
25
+ %pip install cluedin-magic
26
+ ```
27
+
28
+ ```python
29
+ # Load the extension.
30
+ %load_ext cluedin_magic
31
+ ```
32
+
33
+ ```python
34
+ # Create a new CluedIn context from a JWT token.
35
+ api_token = '<your_token_here>'
36
+ ctx = %cluedin get-context --jwt %api_token
37
+ ```
38
+
39
+ ```python
40
+ # Find all entities with a specific entityType.
41
+ %cluedin search --context ctx --query +entityType:/Infrastructure/User
42
+ ```
43
+
44
+ ```python
45
+ # Find all entities with a specific entityType and limit the results.
46
+ %cluedin search --context ctx --query +entityType:/Infrastructure/User --limit 10
47
+ ```
48
+
49
+ ```python
50
+ # Complex query with multiple properties and limit the results.
51
+ %cluedin search --context ctx --query +entityType:/IMDb/Name -properties.imdb.name.deathYear:"\\\\N" --limit 10
52
+ ```
53
+
54
+ ```python
55
+ # Save the results of a query to a pandas DataFrame.
56
+ pd = %cluedin search --context ctx --query +entityType:/IMDb/Name +properties.imdb.name.birthYear:1981
57
+ ```
@@ -0,0 +1,6 @@
1
+ cluedin_magic/__init__.py,sha256=zbzi_I8HULyogYaSJD_rW9vWa7856qnOgSXXwR_Gckk,123
2
+ cluedin_magic/cluedin_magic.py,sha256=puaWw264ZDgdIz84p707S1EX0jcGdzTl1oUf6VHtru8,3369
3
+ cluedin_magic-0.1.0.dist-info/LICENSE,sha256=TyM87zEN0rPZ1o2fY96aTOIe4uLs4wPl-QcQdVnohwA,1088
4
+ cluedin_magic-0.1.0.dist-info/METADATA,sha256=1Se8-KfFV7a0JCcZHwJ-axqKAS8r9LEGTPL0dw3hCj8,1744
5
+ cluedin_magic-0.1.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
6
+ cluedin_magic-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 1.9.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any