1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
|
%global _empty_manifest_terminate_build 0
Name: python-redgrease
Version: 0.3.30
Release: 1
Summary: RedisGears helper package
License: MIT License
URL: https://github.com/lyngon/redgrease
Source0: https://mirrors.nju.edu.cn/pypi/web/packages/43/b2/1c498e30bb2f73ba8916174b0ae4b7e8a40818d0d3bf04e64d2744d425bc/redgrease-0.3.30.tar.gz
BuildArch: noarch
Requires: python3-attrs
Requires: python3-redis
Requires: python3-cloudpickle
Requires: python3-packaging
Requires: python3-wrapt
Requires: python3-typing-extensions
Requires: python3-redis-py-cluster
Requires: python3-watchdog
Requires: python3-ConfigArgParse
Requires: python3-pyyaml
Requires: python3-attrs
Requires: python3-redis
Requires: python3-cloudpickle
Requires: python3-packaging
Requires: python3-wrapt
Requires: python3-typing-extensions
Requires: python3-redis-py-cluster
Requires: python3-watchdog
Requires: python3-ConfigArgParse
Requires: python3-pyyaml
Requires: python3-attrs
Requires: python3-redis
Requires: python3-cloudpickle
Requires: python3-packaging
Requires: python3-wrapt
Requires: python3-typing-extensions
Requires: python3-redis-py-cluster
Requires: python3-attrs
Requires: python3-redis
Requires: python3-cloudpickle
Requires: python3-packaging
Requires: python3-wrapt
%description
[](https://redislabs.com/modules/redis-gears/)
[](https://mit-license.org/)
[](https://pypi.org/project/redgrease/#history)
[](https://pypi.org/project/redgrease)
[](https://pypi.org/project/redgrease)
[](https://www.python.org/)
[](https://lgtm.com/projects/g/lyngon/redgrease/context:python)
[](https://lgtm.com/projects/g/lyngon/redgrease/alerts/)
[](https://github.com/lyngon/redgrease/actions/workflows/full_tests.yml)
[](https://redgrease.readthedocs.io/en/latest/?badge=latest)
[](https://codecov.io/gh/lyngon/redgrease)
[](https://libraries.io/pypi/redgrease)
[](https://github.com/lyngon/redgrease/pulls?q=is%3Apr+is%3Aclosed)
[](https://github.com/lyngon/redgrease/issues?q=is%3Aissue+is%3Aopen+label%3Abug)
[]()
[](https://github.com/lyngon/redgrease/pulse)
[](https://github.com/psf/black)
[](https://hub.docker.com/r/lyngon/redgrease)
[](https://www.youtube.com/watch?v=hGlyFc79BUE)
[](https://www.python.org/)
<!--
Author: Anders Åström
Contact: anders@lyngon.com
-->
<!--
The MIT License
Copyright © 2021 Lyngon Pte. Ltd.
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.
-->
# RedGrease
RedGrease is a Python package and set of tools to facilitate development against [Redis](https://redis.io/) in general and [Redis Gears](https://redislabs.com/modules/redis-gears/) in particular.
It may help you create:
- Advanced analytical queries,
- Event based and streaming data processing,
- Custom Redis commands and interactions,
- And much, much more...
... all written in Python and running distributed ON your Redis nodes.
## Read the [Documentation](https://redgrease.readthedocs.io)
## Installation
- [Client](#client-installation) - I.e. Package used by application and/or during dev.
- [Runtime](#runtime-installation) - I.e. Package installed on the Redis engine.
## Quick Example:
```python
import redgrease as rdgr
conn = rdgr.RedisGears(host='localhost', port=30001)
# count for each genre how many times it appears
results = (
rdgr.KeysReader()
.keys(type='hash')
.map(lambda key: rdgr.cmd.hget(key, 'genres'))
.filter(lambda x:x != '\\N')
.flatmap(lambda x: x.split(','))
.map(str.strip)
.countby()
.run(on=conn)
)
for res in results:
print(f"{res['key']::<15s}{res['value']}")
```
## RedGrease consists of the followinig, components:
1. [A Redis / Redis Gears client](https://redgrease.readthedocs.io), which is an extended version of the [redis](https://pypi.org/project/redis/) client, but with additional pythonic functions, mapping closely (1-to-1) to the Redis Gears command set (e.g. `RG.PYEXECUTE`, `RG.GETRESULT`, `RG.TRIGGER`, `RG.DUMPREGISTRATIONS` etc), outlined [here](https://oss.redislabs.com/redisgears/commands.html)
```python
import redgrease
gear_script = ... # Some vanilla Gear script string, a GearFunction object or a script file path.
rg = redgrease.RedisGears()
rg.gears.pyexecute(gear_script) # <--
```
2. Wrappers for the [runtime functions](https://redgrease.readthedocs.io) (e.g. `GearsBuilder`, `GB`, `atomic`, `execute`, `log` etc) that are automatically loaded into the server [runtime environment](https://oss.redislabs.com/redisgears/runtime.html). These placeholder versions provide **docstrings**, **auto completion** and **type hints** during development, and does not clash with the actual runtime, i.e does not require redgrease to be installed on the server.

3. [Server side Redis commands](https://redgrease.readthedocs.io), allowing for **all** Redis (v.6) commands to be executed on serverside as if using a Redis 'client' class, instead of 'manually' invoking the `execute()`. It is basically the [redis](https://pypi.org/project/redis/) client, but with `execute_command()` rewired to use the Gears-native `execute()` instead under the hood.
```python
import redgrease
def download_image(annotation):
img_id = annotation["image_id"]
img_key = f"image:{img_id}"
if redgrease.cmd.hexists(img_key, "image_data"): # <- hexists
# image already downloaded
return img_key
redgrease.log(f"Downloadign image for annotation: {annotation}")
image_url = redgrease.cmd.hget(img_key, "url") # <- hget
response = requests.get(image_url)
redgrease.cmd.hset(img_key, "image_data", bytes(response.content)) # <- hset
return img_key
# Redis connection (with Gears)
connection = redgrease.RedisGears()
# Automatically download corresponding image, whenever an annotation is created.
image_keys = (
redgrease.KeysReader()
.values(type="hash", event="hset")
.foreach(download_image, requirements=["requests"])
.register("annotation:*", on=connection)
)
```
4. ['First class'](https://en.wikipedia.org/wiki/First-class_citizen) [GearFunction objects](https://redgrease.readthedocs.io), inspired by the remote builders of the official [redisgears-py](https://github.com/RedisGears/redisgears-py) client, but with some differences.
```python
records = redgrease.KeysReader().records(type="hash") # <- fun 1 : hash-recods
record_listener = ( # <- fun 2, "extents" fun 1
records.foreach(schedule)
.register(key_pattern, eventTypes=["hset"])
)
count_by_status = ( # <- fun 3, also "extends" fun 1
records.countby(lambda r: r.value.get("status", "unknown"))
.map(lambda r: {r["key"]: r["value"]})
.aggregate({}, lambda a, r: dict(a, **r))
.run(key_pattern)
)
process_records = ( # <- fun 4
redgrease.StreamReader()
.values() # <- Sugar
.foreach(process, requirements=["numpy"])
.register("to_be_processed")
)
# Instantiate client objects
server = redgrease.RedisGears()
# Different ways of executing
server.gears.pyexecute(record_listener)
process_records.on(server)
count = count_by_status.on(server)
```
:warning: As with the official package, this require that the server runtime Python version matches the client runtime that defined the function. As of writing this is Only for Python 3.7. But don't worry, both "vanilla" and RedGrease flavoured Gear functions can still be excuted by file.
5. [CLI tool](https://redgrease.readthedocs.io) running and or loading of Gears scripts onto a Redis Gears cluster. Particularls useful for "trigger-based" CommandReader Gears.
It also provides a simple form of 'hot-reloading' of Redis Gears scripts, by continously monitoring directories containing Redis Gears scripts and automatically 'pyexecute' them on a Redis Gear instance if it detects modifications.
The purpose is mainly to streamline development of 'trigger-style' Gear scripts by providing a form of hot-reloading functionality.
```
redgrease --server 10.0.2.21 --watch scripts/
```
This will 'pyexecute' the gears scripts in the 'scripts' directory against the server. It will also watch the directors for changes and re-execute the scripts if they have been modified.
6. Other boilerplate or otherwise functions and utilities (`redgrease.utils`), that are commonly used in gears. e.g:
- A record `record` function that can be used to transform the default `KeysReader` dict to an `Records` object with the appropriate attributes.
- CommandReader + Trigger function decorator, that makes custom Redis Commands really easy.
- Helpers to aid debugging and/ or testing of gears.
- ...
Additional feature suggestions appriciated.
7. Docker images with redgrease pre-installed as well as, hopefully, variants for all python versions 3.6 or later (Work-in-Progress).
## Example usage:
```python
import redgrease
import redgrease.utils
relevant_usr_fields = {
"active": bool,
"permissions": redgrease.utils.list_parser(str),
}
# # "Open" Gear function
# Extracting a dict for every 'active' user
active_users = (
redgrease.KeysOnlyReader()
.map(lambda key: redgrease.cmd.hmget(key, *relevant_usr_fields.keys()))
.map(
lambda udata: redgrease.utils.to_dict(
udata, keys=relevant_usr_fields.keys(), val_transform=relevant_usr_fields
)
)
.filter(lambda usr: usr["active"])
)
# # "Open" Gear function re-use
# Count the number of active users
active_user_count = active_users.count()
# Get all the distinct user permissions
all_issued_permissions = active_users.flatmap(lambda usr: usr["permissions"]).distinct()
# # Redis Client w. Gears-features can be created separately
r = redgrease.RedisGears()
# # Two ways of running:
# With 'pyexecute' ...
count = r.gears.pyexecute(active_user_count.run("user:*"))
# ... or using the 'on' method
permissions = all_issued_permissions.run("user:*").on(r)
# Result values are directly accessible
print(f"Count: {count}")
if count > 100:
print("So many users!")
print(permissions)
if "root" in permissions:
print("Someone has root permissions")
# Errors can be accessed too
if count.errors:
print(f"Errors counting users: {count.errors}")
if permissions.errors:
print(f"Errors collecting permissions: {permissions.errors}")
```
# Installation
Redgrease may be installed either as a developmet tool only, a client library and/or as a runtime library on the Redis Gears server.
It can be installed with different 'extras' dependencies depending on preferred usage.
## Client Installation
Installaton for development and/or client application environment.
In the environment where you develop your Gears scripts, simply install 'redgrease' with pip3, as usual:
```
python3 -m pip install redgrease[all]
```
This installs all the dependencies, allowing for the full features set.
`reagrease[cli]` : Installs dependencies for the CLI
## Runtime Installation
Installation for the Redis Gears Runtime / Server.
### Server Requirements
Firstly, the Redis Engine must be running the Redis Gears module.
The easiest way to get started is to run published Docker images:
1) Single engine setup
```
docker run -p 6379:6379 redislabs/redisgears
```
2) Three master node cluster setup
```
docker run -p 30001:30001 -p 30002:30002 -p 30003:30003 redislabs/rgcluster
```
For more advanced deployments of Redis with Gears, please refer to the [official Gears installation documentation](https://docs.redislabs.com/latest/modules/redisgears/installing-redisgears/).
### Redgrease Runtime Package
As for RedGrease, it is recommended to use the `redgrease[runtime]` package as a server side dependency.
This installs dependencies for the all the server side features such as server side Redis commands and the runtime for gears constructed with the Remote Gears Builder.
The easiest way of installing the latest stable runtime package is by simply setting the `enforce_redgrease` argument of `pyexecute` to `True`.
Redgrease will also be automatically enforced when executing any dynamically constructed GearFunction objects.
```python
import redgrease
rg = redgrease.RedisGears()
rg.gears.pyexecute(enforce_redgrease=True)
```
**NOTE:** Its possible to pin the versions inside `enforce_redgrese`, see `redgrease.requirements.PackageOption` for more details.
## Extras
The current 3rd party packages in the `runtime` extras are:
- [attrs](https://pypi.org/project/attrs/) - For parsing composite Gears response structures into attrs objects
- [redis](https://pypi.org/project/redis/) - Needed for server-side Redis commands
- [cludpickle](https://pypi.org/project/cloudpickle/) - Needed for the "Remote" gears (similar to the official [redisgears-py](https://github.com/RedisGears/redisgears-py) client)
### Note on the runtime environment
If the only thing you need is the `redgrease` client, documentation, type hints and the loader CLI for development of conventional Gears scripts (i.e that only use the standard commands and loaded as strings), **then the RedGrease package is not strictly required to be installed** in the Redis Gears Python Runtime environment (i.e. on the server).
You can in this case, simply remove the `redgrease` import clause from your script, after development, but before `pyexecuting` them as per the [Redis Gears documentation](https://oss.redislabs.com/redisgears/intro.html). Such scripts will still run perfectly fine without `redgrease` in Redis Gears Environment.
A minimal install, without any 3rd party dependencies, which is pretty much only the syntactic sugar and runtime placeholders, can be installed using the bare `redgrease` package.
This might be useful if you really don't want the 3rd party packages in the server runtime but still want to use the `redgrease` sugar.
You can also use the RedGrease watcher or loader CLI to automate loading your scripts as well as requirements from a normal `requirements.txt` files, as outlined [here](https://github.com/lyngon/redgrease)
# Usage / Documentation
The documention is work-in-progress, but the latest and greatest version is available here:
### https://redgrease.readthedocs.io
Go read the docs!
# Testing
Tests are separate from the package, but are available in the [GitHub repo](https://github.com/lyngon/redgrease).
```
git clone https://github.com/lyngon/redgrease
```
In order to run the tests, [Docker](https://docs.docker.com/get-docker/) is required to be installed in order to spin up fresh Redis instances, on demand for the tests.
Pytest and quite a number of its add-ons, as well as `tox`, is also needed to run the tests properly. All tests, including development, requirements are best installed through:
```
cd redgrease/
python3 -m venv .venv
source .venv/bin/activate
pip install -r src/requirements-dev.txt
```
Then the test can be run with:
```
pytest
```
**NOTE:** Running the tests takes very long, because a new fresh Redis instance is spun up **for each test**, just to ensure no risk of cross-contamination. This may be optimized, as many tests are actually independent, but it's left like this for now.
# Why This?
The need for this arose from wanting to prototype the concepts for a new Redis module, using Gears CommandReaders and triggers instead of having to write a full fledged module in C.
Initially RedGrease has been just a very simple module, with placeholders for the default Redis gears runtime functions, with type hints and docstrings, just to make it more convenient and less error prone to write Gears functions.
Then the loader CLI was created, in order to further speed up the rapid development cycle.
Then the server-side Redis `client` commands function was added to minimize errors (e.g. misspelled command strings).
Then the client was added ... and before long it started to get a life of its own.
Note that this means RedGrease package is primarily intended to be an aid for **development** of Gears scripts, and was not originally intended to be used in any "production" software.
This intent has now changed, and the new goal is now to make Redgrease a production grade package for Redis Gears.
Granted, there is still quite some way to go to get there, so your support and feedback is greatly appreciated.
If you like this project, or want professional support, please consider [sponsoring](https://github.com/sponsors/lyngon).
%package -n python3-redgrease
Summary: RedisGears helper package
Provides: python-redgrease
BuildRequires: python3-devel
BuildRequires: python3-setuptools
BuildRequires: python3-pip
%description -n python3-redgrease
[](https://redislabs.com/modules/redis-gears/)
[](https://mit-license.org/)
[](https://pypi.org/project/redgrease/#history)
[](https://pypi.org/project/redgrease)
[](https://pypi.org/project/redgrease)
[](https://www.python.org/)
[](https://lgtm.com/projects/g/lyngon/redgrease/context:python)
[](https://lgtm.com/projects/g/lyngon/redgrease/alerts/)
[](https://github.com/lyngon/redgrease/actions/workflows/full_tests.yml)
[](https://redgrease.readthedocs.io/en/latest/?badge=latest)
[](https://codecov.io/gh/lyngon/redgrease)
[](https://libraries.io/pypi/redgrease)
[](https://github.com/lyngon/redgrease/pulls?q=is%3Apr+is%3Aclosed)
[](https://github.com/lyngon/redgrease/issues?q=is%3Aissue+is%3Aopen+label%3Abug)
[]()
[](https://github.com/lyngon/redgrease/pulse)
[](https://github.com/psf/black)
[](https://hub.docker.com/r/lyngon/redgrease)
[](https://www.youtube.com/watch?v=hGlyFc79BUE)
[](https://www.python.org/)
<!--
Author: Anders Åström
Contact: anders@lyngon.com
-->
<!--
The MIT License
Copyright © 2021 Lyngon Pte. Ltd.
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.
-->
# RedGrease
RedGrease is a Python package and set of tools to facilitate development against [Redis](https://redis.io/) in general and [Redis Gears](https://redislabs.com/modules/redis-gears/) in particular.
It may help you create:
- Advanced analytical queries,
- Event based and streaming data processing,
- Custom Redis commands and interactions,
- And much, much more...
... all written in Python and running distributed ON your Redis nodes.
## Read the [Documentation](https://redgrease.readthedocs.io)
## Installation
- [Client](#client-installation) - I.e. Package used by application and/or during dev.
- [Runtime](#runtime-installation) - I.e. Package installed on the Redis engine.
## Quick Example:
```python
import redgrease as rdgr
conn = rdgr.RedisGears(host='localhost', port=30001)
# count for each genre how many times it appears
results = (
rdgr.KeysReader()
.keys(type='hash')
.map(lambda key: rdgr.cmd.hget(key, 'genres'))
.filter(lambda x:x != '\\N')
.flatmap(lambda x: x.split(','))
.map(str.strip)
.countby()
.run(on=conn)
)
for res in results:
print(f"{res['key']::<15s}{res['value']}")
```
## RedGrease consists of the followinig, components:
1. [A Redis / Redis Gears client](https://redgrease.readthedocs.io), which is an extended version of the [redis](https://pypi.org/project/redis/) client, but with additional pythonic functions, mapping closely (1-to-1) to the Redis Gears command set (e.g. `RG.PYEXECUTE`, `RG.GETRESULT`, `RG.TRIGGER`, `RG.DUMPREGISTRATIONS` etc), outlined [here](https://oss.redislabs.com/redisgears/commands.html)
```python
import redgrease
gear_script = ... # Some vanilla Gear script string, a GearFunction object or a script file path.
rg = redgrease.RedisGears()
rg.gears.pyexecute(gear_script) # <--
```
2. Wrappers for the [runtime functions](https://redgrease.readthedocs.io) (e.g. `GearsBuilder`, `GB`, `atomic`, `execute`, `log` etc) that are automatically loaded into the server [runtime environment](https://oss.redislabs.com/redisgears/runtime.html). These placeholder versions provide **docstrings**, **auto completion** and **type hints** during development, and does not clash with the actual runtime, i.e does not require redgrease to be installed on the server.

3. [Server side Redis commands](https://redgrease.readthedocs.io), allowing for **all** Redis (v.6) commands to be executed on serverside as if using a Redis 'client' class, instead of 'manually' invoking the `execute()`. It is basically the [redis](https://pypi.org/project/redis/) client, but with `execute_command()` rewired to use the Gears-native `execute()` instead under the hood.
```python
import redgrease
def download_image(annotation):
img_id = annotation["image_id"]
img_key = f"image:{img_id}"
if redgrease.cmd.hexists(img_key, "image_data"): # <- hexists
# image already downloaded
return img_key
redgrease.log(f"Downloadign image for annotation: {annotation}")
image_url = redgrease.cmd.hget(img_key, "url") # <- hget
response = requests.get(image_url)
redgrease.cmd.hset(img_key, "image_data", bytes(response.content)) # <- hset
return img_key
# Redis connection (with Gears)
connection = redgrease.RedisGears()
# Automatically download corresponding image, whenever an annotation is created.
image_keys = (
redgrease.KeysReader()
.values(type="hash", event="hset")
.foreach(download_image, requirements=["requests"])
.register("annotation:*", on=connection)
)
```
4. ['First class'](https://en.wikipedia.org/wiki/First-class_citizen) [GearFunction objects](https://redgrease.readthedocs.io), inspired by the remote builders of the official [redisgears-py](https://github.com/RedisGears/redisgears-py) client, but with some differences.
```python
records = redgrease.KeysReader().records(type="hash") # <- fun 1 : hash-recods
record_listener = ( # <- fun 2, "extents" fun 1
records.foreach(schedule)
.register(key_pattern, eventTypes=["hset"])
)
count_by_status = ( # <- fun 3, also "extends" fun 1
records.countby(lambda r: r.value.get("status", "unknown"))
.map(lambda r: {r["key"]: r["value"]})
.aggregate({}, lambda a, r: dict(a, **r))
.run(key_pattern)
)
process_records = ( # <- fun 4
redgrease.StreamReader()
.values() # <- Sugar
.foreach(process, requirements=["numpy"])
.register("to_be_processed")
)
# Instantiate client objects
server = redgrease.RedisGears()
# Different ways of executing
server.gears.pyexecute(record_listener)
process_records.on(server)
count = count_by_status.on(server)
```
:warning: As with the official package, this require that the server runtime Python version matches the client runtime that defined the function. As of writing this is Only for Python 3.7. But don't worry, both "vanilla" and RedGrease flavoured Gear functions can still be excuted by file.
5. [CLI tool](https://redgrease.readthedocs.io) running and or loading of Gears scripts onto a Redis Gears cluster. Particularls useful for "trigger-based" CommandReader Gears.
It also provides a simple form of 'hot-reloading' of Redis Gears scripts, by continously monitoring directories containing Redis Gears scripts and automatically 'pyexecute' them on a Redis Gear instance if it detects modifications.
The purpose is mainly to streamline development of 'trigger-style' Gear scripts by providing a form of hot-reloading functionality.
```
redgrease --server 10.0.2.21 --watch scripts/
```
This will 'pyexecute' the gears scripts in the 'scripts' directory against the server. It will also watch the directors for changes and re-execute the scripts if they have been modified.
6. Other boilerplate or otherwise functions and utilities (`redgrease.utils`), that are commonly used in gears. e.g:
- A record `record` function that can be used to transform the default `KeysReader` dict to an `Records` object with the appropriate attributes.
- CommandReader + Trigger function decorator, that makes custom Redis Commands really easy.
- Helpers to aid debugging and/ or testing of gears.
- ...
Additional feature suggestions appriciated.
7. Docker images with redgrease pre-installed as well as, hopefully, variants for all python versions 3.6 or later (Work-in-Progress).
## Example usage:
```python
import redgrease
import redgrease.utils
relevant_usr_fields = {
"active": bool,
"permissions": redgrease.utils.list_parser(str),
}
# # "Open" Gear function
# Extracting a dict for every 'active' user
active_users = (
redgrease.KeysOnlyReader()
.map(lambda key: redgrease.cmd.hmget(key, *relevant_usr_fields.keys()))
.map(
lambda udata: redgrease.utils.to_dict(
udata, keys=relevant_usr_fields.keys(), val_transform=relevant_usr_fields
)
)
.filter(lambda usr: usr["active"])
)
# # "Open" Gear function re-use
# Count the number of active users
active_user_count = active_users.count()
# Get all the distinct user permissions
all_issued_permissions = active_users.flatmap(lambda usr: usr["permissions"]).distinct()
# # Redis Client w. Gears-features can be created separately
r = redgrease.RedisGears()
# # Two ways of running:
# With 'pyexecute' ...
count = r.gears.pyexecute(active_user_count.run("user:*"))
# ... or using the 'on' method
permissions = all_issued_permissions.run("user:*").on(r)
# Result values are directly accessible
print(f"Count: {count}")
if count > 100:
print("So many users!")
print(permissions)
if "root" in permissions:
print("Someone has root permissions")
# Errors can be accessed too
if count.errors:
print(f"Errors counting users: {count.errors}")
if permissions.errors:
print(f"Errors collecting permissions: {permissions.errors}")
```
# Installation
Redgrease may be installed either as a developmet tool only, a client library and/or as a runtime library on the Redis Gears server.
It can be installed with different 'extras' dependencies depending on preferred usage.
## Client Installation
Installaton for development and/or client application environment.
In the environment where you develop your Gears scripts, simply install 'redgrease' with pip3, as usual:
```
python3 -m pip install redgrease[all]
```
This installs all the dependencies, allowing for the full features set.
`reagrease[cli]` : Installs dependencies for the CLI
## Runtime Installation
Installation for the Redis Gears Runtime / Server.
### Server Requirements
Firstly, the Redis Engine must be running the Redis Gears module.
The easiest way to get started is to run published Docker images:
1) Single engine setup
```
docker run -p 6379:6379 redislabs/redisgears
```
2) Three master node cluster setup
```
docker run -p 30001:30001 -p 30002:30002 -p 30003:30003 redislabs/rgcluster
```
For more advanced deployments of Redis with Gears, please refer to the [official Gears installation documentation](https://docs.redislabs.com/latest/modules/redisgears/installing-redisgears/).
### Redgrease Runtime Package
As for RedGrease, it is recommended to use the `redgrease[runtime]` package as a server side dependency.
This installs dependencies for the all the server side features such as server side Redis commands and the runtime for gears constructed with the Remote Gears Builder.
The easiest way of installing the latest stable runtime package is by simply setting the `enforce_redgrease` argument of `pyexecute` to `True`.
Redgrease will also be automatically enforced when executing any dynamically constructed GearFunction objects.
```python
import redgrease
rg = redgrease.RedisGears()
rg.gears.pyexecute(enforce_redgrease=True)
```
**NOTE:** Its possible to pin the versions inside `enforce_redgrese`, see `redgrease.requirements.PackageOption` for more details.
## Extras
The current 3rd party packages in the `runtime` extras are:
- [attrs](https://pypi.org/project/attrs/) - For parsing composite Gears response structures into attrs objects
- [redis](https://pypi.org/project/redis/) - Needed for server-side Redis commands
- [cludpickle](https://pypi.org/project/cloudpickle/) - Needed for the "Remote" gears (similar to the official [redisgears-py](https://github.com/RedisGears/redisgears-py) client)
### Note on the runtime environment
If the only thing you need is the `redgrease` client, documentation, type hints and the loader CLI for development of conventional Gears scripts (i.e that only use the standard commands and loaded as strings), **then the RedGrease package is not strictly required to be installed** in the Redis Gears Python Runtime environment (i.e. on the server).
You can in this case, simply remove the `redgrease` import clause from your script, after development, but before `pyexecuting` them as per the [Redis Gears documentation](https://oss.redislabs.com/redisgears/intro.html). Such scripts will still run perfectly fine without `redgrease` in Redis Gears Environment.
A minimal install, without any 3rd party dependencies, which is pretty much only the syntactic sugar and runtime placeholders, can be installed using the bare `redgrease` package.
This might be useful if you really don't want the 3rd party packages in the server runtime but still want to use the `redgrease` sugar.
You can also use the RedGrease watcher or loader CLI to automate loading your scripts as well as requirements from a normal `requirements.txt` files, as outlined [here](https://github.com/lyngon/redgrease)
# Usage / Documentation
The documention is work-in-progress, but the latest and greatest version is available here:
### https://redgrease.readthedocs.io
Go read the docs!
# Testing
Tests are separate from the package, but are available in the [GitHub repo](https://github.com/lyngon/redgrease).
```
git clone https://github.com/lyngon/redgrease
```
In order to run the tests, [Docker](https://docs.docker.com/get-docker/) is required to be installed in order to spin up fresh Redis instances, on demand for the tests.
Pytest and quite a number of its add-ons, as well as `tox`, is also needed to run the tests properly. All tests, including development, requirements are best installed through:
```
cd redgrease/
python3 -m venv .venv
source .venv/bin/activate
pip install -r src/requirements-dev.txt
```
Then the test can be run with:
```
pytest
```
**NOTE:** Running the tests takes very long, because a new fresh Redis instance is spun up **for each test**, just to ensure no risk of cross-contamination. This may be optimized, as many tests are actually independent, but it's left like this for now.
# Why This?
The need for this arose from wanting to prototype the concepts for a new Redis module, using Gears CommandReaders and triggers instead of having to write a full fledged module in C.
Initially RedGrease has been just a very simple module, with placeholders for the default Redis gears runtime functions, with type hints and docstrings, just to make it more convenient and less error prone to write Gears functions.
Then the loader CLI was created, in order to further speed up the rapid development cycle.
Then the server-side Redis `client` commands function was added to minimize errors (e.g. misspelled command strings).
Then the client was added ... and before long it started to get a life of its own.
Note that this means RedGrease package is primarily intended to be an aid for **development** of Gears scripts, and was not originally intended to be used in any "production" software.
This intent has now changed, and the new goal is now to make Redgrease a production grade package for Redis Gears.
Granted, there is still quite some way to go to get there, so your support and feedback is greatly appreciated.
If you like this project, or want professional support, please consider [sponsoring](https://github.com/sponsors/lyngon).
%package help
Summary: Development documents and examples for redgrease
Provides: python3-redgrease-doc
%description help
[](https://redislabs.com/modules/redis-gears/)
[](https://mit-license.org/)
[](https://pypi.org/project/redgrease/#history)
[](https://pypi.org/project/redgrease)
[](https://pypi.org/project/redgrease)
[](https://www.python.org/)
[](https://lgtm.com/projects/g/lyngon/redgrease/context:python)
[](https://lgtm.com/projects/g/lyngon/redgrease/alerts/)
[](https://github.com/lyngon/redgrease/actions/workflows/full_tests.yml)
[](https://redgrease.readthedocs.io/en/latest/?badge=latest)
[](https://codecov.io/gh/lyngon/redgrease)
[](https://libraries.io/pypi/redgrease)
[](https://github.com/lyngon/redgrease/pulls?q=is%3Apr+is%3Aclosed)
[](https://github.com/lyngon/redgrease/issues?q=is%3Aissue+is%3Aopen+label%3Abug)
[]()
[](https://github.com/lyngon/redgrease/pulse)
[](https://github.com/psf/black)
[](https://hub.docker.com/r/lyngon/redgrease)
[](https://www.youtube.com/watch?v=hGlyFc79BUE)
[](https://www.python.org/)
<!--
Author: Anders Åström
Contact: anders@lyngon.com
-->
<!--
The MIT License
Copyright © 2021 Lyngon Pte. Ltd.
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.
-->
# RedGrease
RedGrease is a Python package and set of tools to facilitate development against [Redis](https://redis.io/) in general and [Redis Gears](https://redislabs.com/modules/redis-gears/) in particular.
It may help you create:
- Advanced analytical queries,
- Event based and streaming data processing,
- Custom Redis commands and interactions,
- And much, much more...
... all written in Python and running distributed ON your Redis nodes.
## Read the [Documentation](https://redgrease.readthedocs.io)
## Installation
- [Client](#client-installation) - I.e. Package used by application and/or during dev.
- [Runtime](#runtime-installation) - I.e. Package installed on the Redis engine.
## Quick Example:
```python
import redgrease as rdgr
conn = rdgr.RedisGears(host='localhost', port=30001)
# count for each genre how many times it appears
results = (
rdgr.KeysReader()
.keys(type='hash')
.map(lambda key: rdgr.cmd.hget(key, 'genres'))
.filter(lambda x:x != '\\N')
.flatmap(lambda x: x.split(','))
.map(str.strip)
.countby()
.run(on=conn)
)
for res in results:
print(f"{res['key']::<15s}{res['value']}")
```
## RedGrease consists of the followinig, components:
1. [A Redis / Redis Gears client](https://redgrease.readthedocs.io), which is an extended version of the [redis](https://pypi.org/project/redis/) client, but with additional pythonic functions, mapping closely (1-to-1) to the Redis Gears command set (e.g. `RG.PYEXECUTE`, `RG.GETRESULT`, `RG.TRIGGER`, `RG.DUMPREGISTRATIONS` etc), outlined [here](https://oss.redislabs.com/redisgears/commands.html)
```python
import redgrease
gear_script = ... # Some vanilla Gear script string, a GearFunction object or a script file path.
rg = redgrease.RedisGears()
rg.gears.pyexecute(gear_script) # <--
```
2. Wrappers for the [runtime functions](https://redgrease.readthedocs.io) (e.g. `GearsBuilder`, `GB`, `atomic`, `execute`, `log` etc) that are automatically loaded into the server [runtime environment](https://oss.redislabs.com/redisgears/runtime.html). These placeholder versions provide **docstrings**, **auto completion** and **type hints** during development, and does not clash with the actual runtime, i.e does not require redgrease to be installed on the server.

3. [Server side Redis commands](https://redgrease.readthedocs.io), allowing for **all** Redis (v.6) commands to be executed on serverside as if using a Redis 'client' class, instead of 'manually' invoking the `execute()`. It is basically the [redis](https://pypi.org/project/redis/) client, but with `execute_command()` rewired to use the Gears-native `execute()` instead under the hood.
```python
import redgrease
def download_image(annotation):
img_id = annotation["image_id"]
img_key = f"image:{img_id}"
if redgrease.cmd.hexists(img_key, "image_data"): # <- hexists
# image already downloaded
return img_key
redgrease.log(f"Downloadign image for annotation: {annotation}")
image_url = redgrease.cmd.hget(img_key, "url") # <- hget
response = requests.get(image_url)
redgrease.cmd.hset(img_key, "image_data", bytes(response.content)) # <- hset
return img_key
# Redis connection (with Gears)
connection = redgrease.RedisGears()
# Automatically download corresponding image, whenever an annotation is created.
image_keys = (
redgrease.KeysReader()
.values(type="hash", event="hset")
.foreach(download_image, requirements=["requests"])
.register("annotation:*", on=connection)
)
```
4. ['First class'](https://en.wikipedia.org/wiki/First-class_citizen) [GearFunction objects](https://redgrease.readthedocs.io), inspired by the remote builders of the official [redisgears-py](https://github.com/RedisGears/redisgears-py) client, but with some differences.
```python
records = redgrease.KeysReader().records(type="hash") # <- fun 1 : hash-recods
record_listener = ( # <- fun 2, "extents" fun 1
records.foreach(schedule)
.register(key_pattern, eventTypes=["hset"])
)
count_by_status = ( # <- fun 3, also "extends" fun 1
records.countby(lambda r: r.value.get("status", "unknown"))
.map(lambda r: {r["key"]: r["value"]})
.aggregate({}, lambda a, r: dict(a, **r))
.run(key_pattern)
)
process_records = ( # <- fun 4
redgrease.StreamReader()
.values() # <- Sugar
.foreach(process, requirements=["numpy"])
.register("to_be_processed")
)
# Instantiate client objects
server = redgrease.RedisGears()
# Different ways of executing
server.gears.pyexecute(record_listener)
process_records.on(server)
count = count_by_status.on(server)
```
:warning: As with the official package, this require that the server runtime Python version matches the client runtime that defined the function. As of writing this is Only for Python 3.7. But don't worry, both "vanilla" and RedGrease flavoured Gear functions can still be excuted by file.
5. [CLI tool](https://redgrease.readthedocs.io) running and or loading of Gears scripts onto a Redis Gears cluster. Particularls useful for "trigger-based" CommandReader Gears.
It also provides a simple form of 'hot-reloading' of Redis Gears scripts, by continously monitoring directories containing Redis Gears scripts and automatically 'pyexecute' them on a Redis Gear instance if it detects modifications.
The purpose is mainly to streamline development of 'trigger-style' Gear scripts by providing a form of hot-reloading functionality.
```
redgrease --server 10.0.2.21 --watch scripts/
```
This will 'pyexecute' the gears scripts in the 'scripts' directory against the server. It will also watch the directors for changes and re-execute the scripts if they have been modified.
6. Other boilerplate or otherwise functions and utilities (`redgrease.utils`), that are commonly used in gears. e.g:
- A record `record` function that can be used to transform the default `KeysReader` dict to an `Records` object with the appropriate attributes.
- CommandReader + Trigger function decorator, that makes custom Redis Commands really easy.
- Helpers to aid debugging and/ or testing of gears.
- ...
Additional feature suggestions appriciated.
7. Docker images with redgrease pre-installed as well as, hopefully, variants for all python versions 3.6 or later (Work-in-Progress).
## Example usage:
```python
import redgrease
import redgrease.utils
relevant_usr_fields = {
"active": bool,
"permissions": redgrease.utils.list_parser(str),
}
# # "Open" Gear function
# Extracting a dict for every 'active' user
active_users = (
redgrease.KeysOnlyReader()
.map(lambda key: redgrease.cmd.hmget(key, *relevant_usr_fields.keys()))
.map(
lambda udata: redgrease.utils.to_dict(
udata, keys=relevant_usr_fields.keys(), val_transform=relevant_usr_fields
)
)
.filter(lambda usr: usr["active"])
)
# # "Open" Gear function re-use
# Count the number of active users
active_user_count = active_users.count()
# Get all the distinct user permissions
all_issued_permissions = active_users.flatmap(lambda usr: usr["permissions"]).distinct()
# # Redis Client w. Gears-features can be created separately
r = redgrease.RedisGears()
# # Two ways of running:
# With 'pyexecute' ...
count = r.gears.pyexecute(active_user_count.run("user:*"))
# ... or using the 'on' method
permissions = all_issued_permissions.run("user:*").on(r)
# Result values are directly accessible
print(f"Count: {count}")
if count > 100:
print("So many users!")
print(permissions)
if "root" in permissions:
print("Someone has root permissions")
# Errors can be accessed too
if count.errors:
print(f"Errors counting users: {count.errors}")
if permissions.errors:
print(f"Errors collecting permissions: {permissions.errors}")
```
# Installation
Redgrease may be installed either as a developmet tool only, a client library and/or as a runtime library on the Redis Gears server.
It can be installed with different 'extras' dependencies depending on preferred usage.
## Client Installation
Installaton for development and/or client application environment.
In the environment where you develop your Gears scripts, simply install 'redgrease' with pip3, as usual:
```
python3 -m pip install redgrease[all]
```
This installs all the dependencies, allowing for the full features set.
`reagrease[cli]` : Installs dependencies for the CLI
## Runtime Installation
Installation for the Redis Gears Runtime / Server.
### Server Requirements
Firstly, the Redis Engine must be running the Redis Gears module.
The easiest way to get started is to run published Docker images:
1) Single engine setup
```
docker run -p 6379:6379 redislabs/redisgears
```
2) Three master node cluster setup
```
docker run -p 30001:30001 -p 30002:30002 -p 30003:30003 redislabs/rgcluster
```
For more advanced deployments of Redis with Gears, please refer to the [official Gears installation documentation](https://docs.redislabs.com/latest/modules/redisgears/installing-redisgears/).
### Redgrease Runtime Package
As for RedGrease, it is recommended to use the `redgrease[runtime]` package as a server side dependency.
This installs dependencies for the all the server side features such as server side Redis commands and the runtime for gears constructed with the Remote Gears Builder.
The easiest way of installing the latest stable runtime package is by simply setting the `enforce_redgrease` argument of `pyexecute` to `True`.
Redgrease will also be automatically enforced when executing any dynamically constructed GearFunction objects.
```python
import redgrease
rg = redgrease.RedisGears()
rg.gears.pyexecute(enforce_redgrease=True)
```
**NOTE:** Its possible to pin the versions inside `enforce_redgrese`, see `redgrease.requirements.PackageOption` for more details.
## Extras
The current 3rd party packages in the `runtime` extras are:
- [attrs](https://pypi.org/project/attrs/) - For parsing composite Gears response structures into attrs objects
- [redis](https://pypi.org/project/redis/) - Needed for server-side Redis commands
- [cludpickle](https://pypi.org/project/cloudpickle/) - Needed for the "Remote" gears (similar to the official [redisgears-py](https://github.com/RedisGears/redisgears-py) client)
### Note on the runtime environment
If the only thing you need is the `redgrease` client, documentation, type hints and the loader CLI for development of conventional Gears scripts (i.e that only use the standard commands and loaded as strings), **then the RedGrease package is not strictly required to be installed** in the Redis Gears Python Runtime environment (i.e. on the server).
You can in this case, simply remove the `redgrease` import clause from your script, after development, but before `pyexecuting` them as per the [Redis Gears documentation](https://oss.redislabs.com/redisgears/intro.html). Such scripts will still run perfectly fine without `redgrease` in Redis Gears Environment.
A minimal install, without any 3rd party dependencies, which is pretty much only the syntactic sugar and runtime placeholders, can be installed using the bare `redgrease` package.
This might be useful if you really don't want the 3rd party packages in the server runtime but still want to use the `redgrease` sugar.
You can also use the RedGrease watcher or loader CLI to automate loading your scripts as well as requirements from a normal `requirements.txt` files, as outlined [here](https://github.com/lyngon/redgrease)
# Usage / Documentation
The documention is work-in-progress, but the latest and greatest version is available here:
### https://redgrease.readthedocs.io
Go read the docs!
# Testing
Tests are separate from the package, but are available in the [GitHub repo](https://github.com/lyngon/redgrease).
```
git clone https://github.com/lyngon/redgrease
```
In order to run the tests, [Docker](https://docs.docker.com/get-docker/) is required to be installed in order to spin up fresh Redis instances, on demand for the tests.
Pytest and quite a number of its add-ons, as well as `tox`, is also needed to run the tests properly. All tests, including development, requirements are best installed through:
```
cd redgrease/
python3 -m venv .venv
source .venv/bin/activate
pip install -r src/requirements-dev.txt
```
Then the test can be run with:
```
pytest
```
**NOTE:** Running the tests takes very long, because a new fresh Redis instance is spun up **for each test**, just to ensure no risk of cross-contamination. This may be optimized, as many tests are actually independent, but it's left like this for now.
# Why This?
The need for this arose from wanting to prototype the concepts for a new Redis module, using Gears CommandReaders and triggers instead of having to write a full fledged module in C.
Initially RedGrease has been just a very simple module, with placeholders for the default Redis gears runtime functions, with type hints and docstrings, just to make it more convenient and less error prone to write Gears functions.
Then the loader CLI was created, in order to further speed up the rapid development cycle.
Then the server-side Redis `client` commands function was added to minimize errors (e.g. misspelled command strings).
Then the client was added ... and before long it started to get a life of its own.
Note that this means RedGrease package is primarily intended to be an aid for **development** of Gears scripts, and was not originally intended to be used in any "production" software.
This intent has now changed, and the new goal is now to make Redgrease a production grade package for Redis Gears.
Granted, there is still quite some way to go to get there, so your support and feedback is greatly appreciated.
If you like this project, or want professional support, please consider [sponsoring](https://github.com/sponsors/lyngon).
%prep
%autosetup -n redgrease-0.3.30
%build
%py3_build
%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
find usr/lib -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
find usr/lib64 -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
find usr/bin -type f -printf "/%h/%f\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
find usr/sbin -type f -printf "/%h/%f\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
find usr/share/man -type f -printf "/%h/%f.gz\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .
%files -n python3-redgrease -f filelist.lst
%dir %{python3_sitelib}/*
%files help -f doclist.lst
%{_docdir}/*
%changelog
* Fri May 05 2023 Python_Bot <Python_Bot@openeuler.org> - 0.3.30-1
- Package Spec generated
|