sciveo 0.1.21__tar.gz → 0.1.23__tar.gz

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.
Files changed (60) hide show
  1. {sciveo-0.1.21 → sciveo-0.1.23}/PKG-INFO +5 -1
  2. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/__init__.py +2 -2
  3. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/api/base.py +2 -2
  4. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/api/upload.py +1 -1
  5. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/cli.py +7 -4
  6. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/common/configuration.py +1 -1
  7. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/common/model.py +1 -1
  8. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/common/optimizers.py +1 -1
  9. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/common/sampling.py +1 -1
  10. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/content/dataset.py +1 -1
  11. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/content/experiment.py +4 -4
  12. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/content/project.py +1 -1
  13. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/content/runner.py +1 -1
  14. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/monitoring/monitor.py +4 -4
  15. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/monitoring/start.py +1 -1
  16. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/network/camera.py +2 -2
  17. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/network/sniffer.py +2 -2
  18. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/network/tools.py +2 -2
  19. sciveo-0.1.23/sciveo/tools/array.py +149 -0
  20. sciveo-0.1.23/sciveo/tools/common.py +98 -0
  21. {sciveo-0.1.21/sciveo/common → sciveo-0.1.23/sciveo}/tools/compress.py +12 -1
  22. {sciveo-0.1.21/sciveo/common → sciveo-0.1.23/sciveo}/tools/crypto.py +12 -1
  23. {sciveo-0.1.21/sciveo/common → sciveo-0.1.23/sciveo}/tools/daemon.py +2 -2
  24. {sciveo-0.1.21/sciveo/common → sciveo-0.1.23/sciveo}/tools/hardware.py +2 -2
  25. sciveo-0.1.23/sciveo/tools/http.py +134 -0
  26. {sciveo-0.1.21/sciveo/common → sciveo-0.1.23/sciveo}/tools/logger.py +1 -1
  27. sciveo-0.1.23/sciveo/tools/os.py +67 -0
  28. {sciveo-0.1.21/sciveo/common → sciveo-0.1.23/sciveo}/tools/remote.py +3 -3
  29. sciveo-0.1.23/sciveo/tools/simple_counter.py +112 -0
  30. sciveo-0.1.23/sciveo/tools/synchronized.py +160 -0
  31. {sciveo-0.1.21/sciveo/common → sciveo-0.1.23/sciveo}/tools/timers.py +1 -1
  32. sciveo-0.1.23/sciveo/version.py +2 -0
  33. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo.egg-info/PKG-INFO +5 -1
  34. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo.egg-info/SOURCES.txt +17 -13
  35. sciveo-0.1.23/sciveo.egg-info/requires.txt +79 -0
  36. {sciveo-0.1.21 → sciveo-0.1.23}/setup.py +23 -15
  37. {sciveo-0.1.21 → sciveo-0.1.23}/test/test_compress.py +1 -1
  38. {sciveo-0.1.21 → sciveo-0.1.23}/test/test_crypto.py +1 -1
  39. {sciveo-0.1.21 → sciveo-0.1.23}/test/test_monitoring.py +1 -1
  40. {sciveo-0.1.21 → sciveo-0.1.23}/test/test_sampling.py +1 -1
  41. sciveo-0.1.21/sciveo/common/tools/synchronized.py +0 -59
  42. sciveo-0.1.21/sciveo/network/tunnel.py +0 -17
  43. sciveo-0.1.21/sciveo/version.py +0 -2
  44. sciveo-0.1.21/sciveo.egg-info/requires.txt +0 -15
  45. {sciveo-0.1.21 → sciveo-0.1.23}/README.md +0 -0
  46. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/api/__init__.py +0 -0
  47. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo/common/__init__.py +0 -0
  48. {sciveo-0.1.21/sciveo/common/tools → sciveo-0.1.23/sciveo/content}/__init__.py +0 -0
  49. {sciveo-0.1.21/sciveo/content → sciveo-0.1.23/sciveo/monitoring}/__init__.py +0 -0
  50. {sciveo-0.1.21/sciveo/monitoring → sciveo-0.1.23/sciveo/network}/__init__.py +0 -0
  51. {sciveo-0.1.21/sciveo/network → sciveo-0.1.23/sciveo/tools}/__init__.py +0 -0
  52. {sciveo-0.1.21/sciveo/common → sciveo-0.1.23/sciveo}/tools/configuration.py +0 -0
  53. {sciveo-0.1.21/sciveo/common → sciveo-0.1.23/sciveo}/tools/formating.py +0 -0
  54. {sciveo-0.1.21/sciveo/common → sciveo-0.1.23/sciveo}/tools/random.py +0 -0
  55. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo.egg-info/dependency_links.txt +0 -0
  56. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo.egg-info/entry_points.txt +0 -0
  57. {sciveo-0.1.21 → sciveo-0.1.23}/sciveo.egg-info/top_level.txt +0 -0
  58. {sciveo-0.1.21 → sciveo-0.1.23}/setup.cfg +0 -0
  59. {sciveo-0.1.21 → sciveo-0.1.23}/test/test_configuration.py +0 -0
  60. {sciveo-0.1.21 → sciveo-0.1.23}/test/test_runner.py +0 -0
@@ -1,10 +1,14 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sciveo
3
- Version: 0.1.21
3
+ Version: 0.1.23
4
4
  Description-Content-Type: text/markdown
5
5
  Provides-Extra: mon
6
6
  Provides-Extra: net
7
+ Provides-Extra: media
8
+ Provides-Extra: media-server
9
+ Provides-Extra: media-ml
7
10
  Provides-Extra: all
11
+ Provides-Extra: media-all
8
12
 
9
13
  # SCIVEO - ML/AI and Scientific tools
10
14
 
@@ -16,8 +16,8 @@ try:
16
16
  import os
17
17
  import time
18
18
 
19
- from sciveo.common.tools.logger import *
20
- from sciveo.common.tools.daemon import TasksDaemon, __upload_content__
19
+ from sciveo.tools.logger import *
20
+ from sciveo.tools.daemon import TasksDaemon, __upload_content__
21
21
  from sciveo.content.runner import ProjectRunner
22
22
  from sciveo.content.dataset import Dataset
23
23
  from sciveo.monitoring.start import MonitorStart
@@ -14,8 +14,8 @@ import json
14
14
  from urllib import request, parse
15
15
  from urllib.error import HTTPError
16
16
 
17
- from sciveo.common.tools.logger import *
18
- from sciveo.common.tools.configuration import GlobalConfiguration
17
+ from sciveo.tools.logger import *
18
+ from sciveo.tools.configuration import GlobalConfiguration
19
19
 
20
20
 
21
21
  class APIRemoteClient:
@@ -15,7 +15,7 @@ from urllib import request, parse
15
15
  from urllib.error import HTTPError
16
16
  import requests
17
17
 
18
- from sciveo.common.tools.logger import *
18
+ from sciveo.tools.logger import *
19
19
  from sciveo.api.base import *
20
20
 
21
21
 
@@ -13,18 +13,18 @@
13
13
  import os
14
14
  import argparse
15
15
 
16
- from sciveo.common.tools.logger import *
17
- from sciveo.common.tools.timers import Timer
16
+ from sciveo.tools.logger import *
17
+ from sciveo.tools.timers import Timer
18
18
  from sciveo.monitoring.start import MonitorStart
19
19
  from sciveo.network.tools import NetworkTools
20
- from sciveo.common.tools.configuration import GlobalConfiguration
20
+ from sciveo.tools.configuration import GlobalConfiguration
21
21
 
22
22
 
23
23
  def main():
24
24
  config = GlobalConfiguration.get()
25
25
 
26
26
  parser = argparse.ArgumentParser(description='sciveo CLI')
27
- parser.add_argument('command', choices=['init', 'monitor', 'scan'], help='Command to execute')
27
+ parser.add_argument('command', choices=['init', 'monitor', 'scan', 'media-server'], help='Command to execute')
28
28
  parser.add_argument('--period', type=int, default=120, help='Period in seconds')
29
29
  parser.add_argument('--block', type=bool, default=True, help='Block flag')
30
30
  parser.add_argument('--auth', type=str, default=config['secret_access_key'], help='Auth secret access key')
@@ -53,6 +53,9 @@ def main():
53
53
  fp.write(line + '\n')
54
54
  else:
55
55
  info(f"init, [{base_path}] already there")
56
+ elif args.command == 'media-server':
57
+ from sciveo.media.pipelines.server import __START_SCIVEO_MEDIA_SERVER__
58
+ __START_SCIVEO_MEDIA_SERVER__()
56
59
  else:
57
60
  warning(args.command, "not implemented")
58
61
 
@@ -11,7 +11,7 @@
11
11
 
12
12
  import json
13
13
 
14
- from sciveo.common.tools.logger import *
14
+ from sciveo.tools.logger import *
15
15
 
16
16
 
17
17
  list_hyperparameters = list(set([
@@ -15,7 +15,7 @@ import datetime
15
15
 
16
16
  import numpy as np
17
17
 
18
- from sciveo.common.tools.logger import *
18
+ from sciveo.tools.logger import *
19
19
 
20
20
 
21
21
  class ModelInfo:
@@ -11,7 +11,7 @@
11
11
 
12
12
  import numpy as np
13
13
 
14
- from sciveo.common.tools.logger import *
14
+ from sciveo.tools.logger import *
15
15
 
16
16
 
17
17
  class BaseOptimizer:
@@ -12,7 +12,7 @@
12
12
  from itertools import product
13
13
  import numpy as np
14
14
 
15
- from sciveo.common.tools.logger import *
15
+ from sciveo.tools.logger import *
16
16
  from sciveo.common.configuration import Configuration
17
17
  from sciveo.common.optimizers import BaseOptimizer, AdamOptimizer
18
18
 
@@ -14,7 +14,7 @@ import time
14
14
 
15
15
  import numpy as np
16
16
 
17
- from sciveo.common.tools.logger import *
17
+ from sciveo.tools.logger import *
18
18
 
19
19
 
20
20
  class Dataset:
@@ -15,10 +15,10 @@ import datetime
15
15
 
16
16
  import numpy as np
17
17
 
18
- from sciveo.common.tools.logger import *
19
- from sciveo.common.tools.daemon import __upload_content__ as UPLOAD
20
- from sciveo.common.tools.formating import format_elapsed_time
21
- from sciveo.common.tools.hardware import HardwareInfo
18
+ from sciveo.tools.logger import *
19
+ from sciveo.tools.daemon import __upload_content__ as UPLOAD
20
+ from sciveo.tools.formating import format_elapsed_time
21
+ from sciveo.tools.hardware import HardwareInfo
22
22
  from sciveo.common.configuration import Configuration
23
23
  from sciveo.common.model import ModelInfo
24
24
  from sciveo.content.dataset import Dataset
@@ -14,7 +14,7 @@ import time
14
14
 
15
15
  import numpy as np
16
16
 
17
- from sciveo.common.tools.logger import *
17
+ from sciveo.tools.logger import *
18
18
  from sciveo.api.base import *
19
19
  from sciveo.common.configuration import Configuration
20
20
  from sciveo.content.experiment import Experiment, RemoteExperiment
@@ -11,7 +11,7 @@
11
11
 
12
12
  import os
13
13
 
14
- from sciveo.common.tools.logger import *
14
+ from sciveo.tools.logger import *
15
15
  from sciveo.common.sampling import RandomSampler, GridSampler, AutoSampler
16
16
  from sciveo.content.project import RemoteProject, LocalProject
17
17
 
@@ -20,10 +20,10 @@ import re
20
20
  import uuid
21
21
  import numpy as np
22
22
 
23
- from sciveo.common.tools.logger import *
24
- from sciveo.common.tools.daemon import DaemonBase
25
- from sciveo.common.tools.hardware import *
26
- from sciveo.common.tools.formating import format_memory_size
23
+ from sciveo.tools.logger import *
24
+ from sciveo.tools.daemon import DaemonBase
25
+ from sciveo.tools.hardware import *
26
+ from sciveo.tools.formating import format_memory_size
27
27
  from sciveo.api.base import APIRemoteClient
28
28
 
29
29
 
@@ -12,7 +12,7 @@
12
12
  import os
13
13
  import time
14
14
 
15
- from sciveo.common.tools.logger import *
15
+ from sciveo.tools.logger import *
16
16
  from sciveo.monitoring.monitor import BaseMonitor
17
17
 
18
18
 
@@ -15,8 +15,8 @@ import cv2
15
15
  import numpy as np
16
16
  from scapy.all import sniff, IP, TCP, UDP
17
17
 
18
- from sciveo.common.tools.logger import *
19
- from sciveo.common.tools.timers import FPSCounter
18
+ from sciveo.tools.logger import *
19
+ from sciveo.tools.timers import FPSCounter
20
20
  from sciveo.network.tools import StreamSniffer
21
21
 
22
22
 
@@ -13,8 +13,8 @@ import socket
13
13
  import threading
14
14
  from scapy.all import sniff, IP, TCP
15
15
 
16
- from sciveo.common.tools.logger import *
17
- from sciveo.common.tools.timers import Timer
16
+ from sciveo.tools.logger import *
17
+ from sciveo.tools.timers import Timer
18
18
 
19
19
 
20
20
  class StreamSniffer:
@@ -13,8 +13,8 @@ import socket
13
13
  import ipaddress
14
14
  import threading
15
15
 
16
- from sciveo.common.tools.logger import *
17
- from sciveo.common.tools.timers import Timer
16
+ from sciveo.tools.logger import *
17
+ from sciveo.tools.timers import Timer
18
18
 
19
19
 
20
20
  class NetworkTools:
@@ -0,0 +1,149 @@
1
+ #
2
+ # Pavlin Georgiev, Softel Labs
3
+ #
4
+ # This is a proprietary file and may not be copied,
5
+ # distributed, or modified without express permission
6
+ # from the owner. For licensing inquiries, please
7
+ # contact pavlin@softel.bg.
8
+ #
9
+ # 2024
10
+ #
11
+
12
+ import numpy as np
13
+ import copy
14
+
15
+
16
+ def split_sized(arr, chunk_size, residual=True):
17
+ result = []
18
+ num_sets = int(len(arr) / chunk_size)
19
+ from_idx = 0
20
+ to_idx = 0
21
+ for i in range(num_sets):
22
+ to_idx = from_idx + chunk_size
23
+ result.append(arr[from_idx:to_idx])
24
+ from_idx = to_idx
25
+
26
+ if residual:
27
+ result.append(arr[from_idx:])
28
+
29
+ return result
30
+
31
+
32
+ def split_ratios(arr, ratios=[0.75, 0.20, 0.05]):
33
+ if sum(ratios) != 1.0:
34
+ raise Exception("invalid ratios sum: {}".format(sum(ratios)))
35
+
36
+ ratios_idx = []
37
+ current_id = 0
38
+ for r in ratios:
39
+ ratios_idx.append(current_id)
40
+ current_id += int(len(arr) * r)
41
+
42
+ result = []
43
+ for i in range(1, len(ratios_idx)):
44
+ result.append(arr[ratios_idx[i - 1] : ratios_idx[i]])
45
+ result.append(arr[ratios_idx[i]:])
46
+
47
+ return result
48
+
49
+
50
+ def split_dataset(arr, ratios={"train": 0.85, "val": 0.10, "test": 0.05}):
51
+ ratios_keys = []
52
+ ratios_values = []
53
+ for k, v in ratios.items():
54
+ ratios_keys.append(k)
55
+ ratios_values.append(v)
56
+
57
+ split_keys = split_ratios(arr, ratios_values)
58
+
59
+ dataset = {}
60
+ for i, k in enumerate(ratios_keys):
61
+ dataset[k] = split_keys[i]
62
+ return dataset
63
+
64
+
65
+ def copy_dict_values(src, dst, keys):
66
+ for k in keys:
67
+ if k in src:
68
+ dst[k] = src[k]
69
+
70
+
71
+ """
72
+ Search d2 keys into d1, when found, merge these keys recursive into the section found.
73
+ """
74
+ def merge_dicts(d1, d2):
75
+ for k, v in d1.items():
76
+ if k in d2:
77
+ if isinstance(v, dict):
78
+ merge_dicts(v, d2[k])
79
+ else:
80
+ d1[k] = d2[k]
81
+ else:
82
+ if isinstance(v, dict):
83
+ merge_dicts(v, d2)
84
+ return d1
85
+
86
+ def merge_dicts2(d1, d2, r={}):
87
+ for k, v in d1.items():
88
+ r[k] = copy.deepcopy(v)
89
+ if k in d2:
90
+ if isinstance(v, dict):
91
+ merge_dicts2(v, d2[k], r[k])
92
+ else:
93
+ d1[k] = d2[k]
94
+ r[k] = copy.deepcopy(d2[k])
95
+ else:
96
+ if isinstance(v, dict):
97
+ merge_dicts2(v, d2, r[k])
98
+ else:
99
+ for k2, v2 in d2.items():
100
+ if k2 not in d1:
101
+ r[k2] = copy.deepcopy(v2)
102
+ return r
103
+
104
+
105
+ def split_sized(arr, chunk_size, residual=True):
106
+ result = []
107
+ num_sets = int(len(arr) / chunk_size)
108
+ from_idx = 0
109
+ to_idx = 0
110
+ for i in range(num_sets):
111
+ to_idx = from_idx + chunk_size
112
+ result.append(arr[from_idx:to_idx])
113
+ from_idx = to_idx
114
+
115
+ if residual:
116
+ result.append(arr[from_idx:])
117
+
118
+ return result
119
+
120
+ def split_ratios(arr, ratios=[0.75, 0.20, 0.05]):
121
+ if sum(ratios) != 1.0:
122
+ raise Exception("invalid ratios sum: {}".format(sum(ratios)))
123
+
124
+ ratios_idx = []
125
+ current_id = 0
126
+ for r in ratios:
127
+ ratios_idx.append(current_id)
128
+ current_id += int(len(arr) * r)
129
+
130
+ result = []
131
+ for i in range(1, len(ratios_idx)):
132
+ result.append(arr[ratios_idx[i - 1] : ratios_idx[i]])
133
+ result.append(arr[ratios_idx[i]:])
134
+
135
+ return result
136
+
137
+ def split_dataset(arr, ratios={"train": 0.85, "val": 0.10, "test": 0.05}):
138
+ ratios_keys = []
139
+ ratios_values = []
140
+ for k, v in ratios.items():
141
+ ratios_keys.append(k)
142
+ ratios_values.append(v)
143
+
144
+ split_keys = split_ratios(arr, ratios_values)
145
+
146
+ dataset = {}
147
+ for i, k in enumerate(ratios_keys):
148
+ dataset[k] = split_keys[i]
149
+ return dataset
@@ -0,0 +1,98 @@
1
+ #
2
+ # Pavlin Georgiev, Softel Labs
3
+ #
4
+ # This is a proprietary file and may not be copied,
5
+ # distributed, or modified without express permission
6
+ # from the owner. For licensing inquiries, please
7
+ # contact pavlin@softel.bg.
8
+ #
9
+ # 2024
10
+ #
11
+
12
+ import re
13
+ import datetime
14
+ import os
15
+ import os.path
16
+ import json
17
+ import cv2
18
+ import skvideo.io
19
+
20
+ from sciveo.tools.logger import *
21
+
22
+
23
+ def get_resize_resoulution(FH, FW, max_dim):
24
+ if FH <= max_dim:
25
+ debug("get_resize_resoulution", [FH, FW], max_dim)
26
+ return FH, FW
27
+ h = max_dim
28
+ w = int(FW * h / FH)
29
+ if w % 2 == 1:
30
+ w += 1
31
+ return h, w
32
+
33
+ def get_frame_resolution_max_dim(frame, max_dim):
34
+ FH = frame.shape[0]
35
+ FW = frame.shape[1]
36
+
37
+ if FH > FW:
38
+ return get_resize_resoulution(FH, FW, max_dim)
39
+ else:
40
+ w, h = get_resize_resoulution(FW, FH, max_dim)
41
+ return h, w
42
+
43
+ def get_frame_resolution(frame, h):
44
+ FH = frame.shape[0]
45
+ FW = frame.shape[1]
46
+ return get_resize_resoulution(FH, FW, h)
47
+
48
+
49
+ class VideoWriterBase:
50
+ def __init__(self, local_video_path, w=640):
51
+ self.local_video_path = local_video_path
52
+ self.w = w
53
+ self.draw_frame = True
54
+
55
+ def use_frame_draw(self):
56
+ return self.draw_frame
57
+
58
+ def write(self, frame, detections=None):
59
+ pass
60
+
61
+ def close(self):
62
+ pass
63
+
64
+ class VideoWriterDummy(VideoWriterBase):
65
+ def __init__(self, local_video_path, w=640):
66
+ super().__init__(local_video_path, w)
67
+ debug("init", self.local_video_path)
68
+
69
+ def write(self, frame, detections=None):
70
+ pass
71
+
72
+ def close(self):
73
+ debug("close", self.local_video_path)
74
+ pass
75
+
76
+ class VideoWriterFFMPEG(VideoWriterBase):
77
+ def __init__(self, local_video_path, w=640, crf='35', aux_params={}):
78
+ super().__init__(local_video_path, w)
79
+
80
+ output_params = {'-vcodec':'libx264', '-crf':crf, '-preset':'medium', '-vf':'format=yuv420p', '-profile:v':'baseline', '-level':'3.0'}
81
+ output_params = {**output_params, **aux_params}
82
+ info(self.local_video_path, output_params)
83
+ self.out = skvideo.io.FFmpegWriter(self.local_video_path, outputdict=output_params)
84
+
85
+ def write(self, frame, detections=None):
86
+ self.out.writeFrame(frame[:,:,::-1])
87
+
88
+ def close(self):
89
+ self.out.close()
90
+ debug("closed", self.local_video_path)
91
+
92
+ @staticmethod
93
+ def new(local_video_path, w=640, crf='35'):
94
+ if int(os.environ.setdefault('PRODUCTION_MODE', "1")):
95
+ result = VideoWriterFFMPEG(local_video_path, w, crf)
96
+ else:
97
+ result = VideoWriterDummy(local_video_path, w)
98
+ return result
@@ -1,8 +1,19 @@
1
+ #
2
+ # Pavlin Georgiev, Softel Labs
3
+ #
4
+ # This is a proprietary file and may not be copied,
5
+ # distributed, or modified without express permission
6
+ # from the owner. For licensing inquiries, please
7
+ # contact pavlin@softel.bg.
8
+ #
9
+ # 2024
10
+ #
11
+
1
12
  import json
2
13
  import gzip
3
14
  import base64
4
15
 
5
- from sciveo.common.tools.logger import *
16
+ from sciveo.tools.logger import *
6
17
 
7
18
 
8
19
  class CompressJsonData:
@@ -1,9 +1,20 @@
1
+ #
2
+ # Pavlin Georgiev, Softel Labs
3
+ #
4
+ # This is a proprietary file and may not be copied,
5
+ # distributed, or modified without express permission
6
+ # from the owner. For licensing inquiries, please
7
+ # contact pavlin@softel.bg.
8
+ #
9
+ # 2024
10
+ #
11
+
1
12
  import json
2
13
  import base64
3
14
  from Crypto.Cipher import AES
4
15
  from Crypto.Random import get_random_bytes
5
16
 
6
- from sciveo.common.tools.logger import *
17
+ from sciveo.tools.logger import *
7
18
 
8
19
 
9
20
  class CryptoJsonData:
@@ -13,8 +13,8 @@ import threading
13
13
  import signal
14
14
  import time
15
15
 
16
- from sciveo.common.tools.logger import *
17
- from sciveo.common.tools.synchronized import ListQueue
16
+ from sciveo.tools.logger import *
17
+ from sciveo.tools.synchronized import ListQueue
18
18
  from sciveo.api.upload import APIFileUploader
19
19
 
20
20
 
@@ -15,8 +15,8 @@ import uuid
15
15
  import random
16
16
  import string
17
17
 
18
- from sciveo.common.tools.logger import *
19
- from sciveo.common.tools.formating import format_memory_size
18
+ from sciveo.tools.logger import *
19
+ from sciveo.tools.formating import format_memory_size
20
20
 
21
21
 
22
22
  def new_guid_uuid():
@@ -0,0 +1,134 @@
1
+ #
2
+ # Pavlin Georgiev, Softel Labs
3
+ #
4
+ # This is a proprietary file and may not be copied,
5
+ # distributed, or modified without express permission
6
+ # from the owner. For licensing inquiries, please
7
+ # contact pavlin@softel.bg.
8
+ #
9
+ # 2024
10
+ #
11
+
12
+ import json
13
+ from urllib import request, parse
14
+ from urllib.error import HTTPError
15
+ import requests
16
+
17
+ from sciveo.tools.logger import *
18
+ from sciveo.tools.synchronized import ListQueue
19
+
20
+
21
+ def POST_JSON(url, data_json, headers={}, timeout=30):
22
+ result = False
23
+ try:
24
+ #debug("POST", url, data_json, headers)
25
+ response = requests.post(url, json=data_json, headers=headers, timeout=timeout)
26
+ result = response.json()
27
+ debug("POST", result)
28
+ except HTTPError as e:
29
+ error(e, data_json)
30
+ except Exception as e:
31
+ error(e, data_json)
32
+ return result
33
+
34
+ def POST(url, data, headers={}, timeout=30):
35
+ result = False
36
+ try:
37
+ debug("POST", url, data, headers)
38
+ data = parse.urlencode(data).encode("utf-8")
39
+ resp = request.urlopen(request.Request(url, data=data, headers=headers), timeout=timeout)
40
+ result = json.loads(resp.read())
41
+ debug("POST", resp.status, result)
42
+ except HTTPError as e:
43
+ error(e, data)
44
+ except Exception as e:
45
+ error(e, data)
46
+ return result
47
+
48
+ def GET(url, headers={}, timeout=30):
49
+ result = False
50
+ try:
51
+ # debug("GET", url)
52
+ response = requests.get(url, headers=headers, timeout=timeout)
53
+ result = response.json()
54
+ # debug("GET", result)
55
+ except HTTPError as e:
56
+ error(e)
57
+ except Exception as e:
58
+ error(e)
59
+ return result
60
+
61
+
62
+ class HTTPQueueSynchronized:
63
+ _queue = None
64
+
65
+ @staticmethod
66
+ def queue():
67
+ if HTTPQueueSynchronized._queue is None:
68
+ HTTPQueueSynchronized._queue = ListQueue("HTTPQueue")
69
+ return HTTPQueueSynchronized._queue
70
+
71
+ @staticmethod
72
+ def size():
73
+ return HTTPQueueSynchronized.queue().size()
74
+
75
+ @staticmethod
76
+ def get(url,
77
+ url_prefix="{}/api/v1".format(os.environ.get('API_BASE_URL', 'api.smiveo.com')),
78
+ access_token=os.environ.get('AUTH_KEY_SENSORS_API', "none")
79
+ ):
80
+ debug("HTTPQueueSynchronized::GET", url)
81
+ try:
82
+ url = url_prefix + url
83
+ url = HTTPQueueSynchronized.url_append(url, {"access_token": access_token})
84
+ HTTPQueueSynchronized.queue().push({"method": "GET", "url": url, "data": None})
85
+ except Exception as e:
86
+ error(e, "HTTPQueueSynchronized::GET", url)
87
+
88
+ @staticmethod
89
+ def post(url, data,
90
+ url_prefix="{}/api/v1".format(os.environ.get('API_BASE_URL', 'api.smiveo.com')),
91
+ access_token=os.environ.get('AUTH_KEY_SENSORS_API', "none")
92
+ ):
93
+ debug("HTTPQueueSynchronized::POST", url, data)
94
+ try:
95
+ url = url_prefix + url
96
+ data["access_token"] = access_token
97
+ HTTPQueueSynchronized.queue().push({"method": "POST", "url": url, "data": parse.urlencode(data).encode("utf-8")})
98
+ except Exception as e:
99
+ error(e, "HTTPQueueSynchronized::POST", url, data)
100
+
101
+ @staticmethod
102
+ def pop(block=True, timeout=None, retries=5):
103
+ req = HTTPQueueSynchronized.queue().pop(timeout=timeout)
104
+ try:
105
+ while(retries > 0):
106
+ if HTTPQueueSynchronized.save(req["method"], req["url"], req["data"]):
107
+ break
108
+ retries -= 1
109
+ except Exception as e:
110
+ error(e, "HTTPQueueSynchronized::POP")
111
+
112
+ @staticmethod
113
+ def url_append(url, url_args):
114
+ for k, v in url_args.items():
115
+ if "?" in url:
116
+ and_token = "&"
117
+ else:
118
+ and_token = "?"
119
+ url += "{}{}={}".format(and_token, k, v)
120
+ return url
121
+
122
+ @staticmethod
123
+ def save(method, url, data, timeout=10):
124
+ try:
125
+ debug("HTTPQueueSynchronized", method, url, data)
126
+ resp = request.urlopen(request.Request(url, data=data), timeout=timeout)
127
+ debug("HTTPQueueSynchronized", resp.status, resp.read())
128
+ except HTTPError as e:
129
+ error(e, "HTTPQueueSynchronized::HTTPError", method, url, data)
130
+ return False
131
+ except Exception as e:
132
+ error(e, "HTTPQueueSynchronized::Exception", method, url, data)
133
+ return False
134
+ return True