Skip to content

Commit 0ef145b

Browse files
committed
Refactor FileOperation and FilterPostRoutingRule for improved functionality; enhance path handling, update message processing, and adjust PostMessage attributes for optional types.
1 parent abac334 commit 0ef145b

File tree

5 files changed

+41
-45
lines changed

5 files changed

+41
-45
lines changed

src/python/reddit/bo.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ class FileOperation(BusinessOperation):
1414
This operation receive a PostMessage and write down in the right company
1515
.txt all the important information and the time of the operation
1616
"""
17+
18+
path = "/tmp"
19+
1720
def on_init(self):
18-
if hasattr(self,'path'):
19-
os.chdir(self.path)
21+
"""
22+
This method is called when the operation is created.
23+
"""
24+
25+
if not os.path.exists(self.path):
26+
os.makedirs(self.path)
2027

2128
def on_post_message(self, request: PostMessage):
2229
"""
@@ -33,7 +40,7 @@ def on_post_message(self, request: PostMessage):
3340
ts = datetime.datetime.fromtimestamp(request.post.created_utc).__str__()
3441

3542
line = ts+" : "+title+" : "+author+" : "+url
36-
filename = request.found+".txt"
43+
filename = (request.found or "default")+".txt"
3744

3845

3946
self.put_line(filename, line)
@@ -56,6 +63,7 @@ class FileOperationWithIrisAdapter(BusinessOperation):
5663
.txt all the important information and the time of the operation using the iris
5764
adapter EnsLib.File.OutboundAdapter
5865
"""
66+
@staticmethod
5967
def get_adapter_type():
6068
"""
6169
Name of the registred Adapter

src/python/reddit/bp.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ def iris_to_python(self, request:'iris.dc.Demo.PostMessage'):
3030
return self.on_python_message(request)
3131

3232
def on_python_message(self, request: PostMessage):
33-
if 'dog'.upper() in request.post.selftext.upper():
34-
request.to_email_address = '[email protected]'
35-
request.found = 'Dog'
36-
if 'cat'.upper() in request.post.selftext.upper():
37-
request.to_email_address = '[email protected]'
38-
request.found = 'Cat'
33+
if request.post and request.post.selftext:
34+
if 'dog'.upper() in request.post.selftext.upper():
35+
request.to_email_address = '[email protected]'
36+
request.found = 'Dog'
37+
if 'cat'.upper() in request.post.selftext.upper():
38+
request.to_email_address = '[email protected]'
39+
request.found = 'Cat'
3940

4041
if request.found is not None:
4142
rsp = self.send_request_sync(self.target,request)

src/python/reddit/bs.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from iop import BusinessService
22

3+
from typing import Optional
34
import json
45
import requests
56

7+
68
from message import PostMessage
79
from obj import PostClass
810

@@ -12,6 +14,11 @@ class RedditService(BusinessService):
1214
seconds, use requests to fetch self.limit posts as data from the reddit
1315
API before calling the FilterPostRoutingRule process.
1416
"""
17+
feed = "/new/"
18+
limit = "5"
19+
target = "Python.FilterPostRoutingRule"
20+
21+
@staticmethod
1522
def get_adapter_type():
1623
"""
1724
Name of the registred Adapter
@@ -22,21 +29,12 @@ def on_init(self):
2229
"""
2330
This method is called when the service is created.
2431
"""
25-
26-
if not hasattr(self,'feed'):
27-
self.feed = "/new/"
28-
29-
if not hasattr(self,'limit'):
30-
raise TypeError('no limit field')
31-
32-
if not hasattr(self,'target'):
33-
self.target = "Python.FilterPostRoutingRule"
34-
35-
self.last_post_name = ""
36-
37-
return 1
38-
39-
def on_process_input(self,request):
32+
if self.limit is None:
33+
self.limit = "5"
34+
35+
self.last_post_name = "t3_tjflyd" # This is the last post name to start from, it should be updated after the first fetch.
36+
37+
def on_process_input(self,message_input):
4038
"""
4139
This method is called every 5 seconds by the Ens.InboundAdapter.
4240
"""
@@ -47,7 +45,7 @@ def on_process_input(self,request):
4745
msg.post = post
4846
self.send_request_sync(self.target,msg)
4947

50-
def on_task(self) -> PostClass:
48+
def on_task(self) -> Optional[PostClass]:
5149
"""
5250
This method is called by on_process_input to fetch the data from the
5351
reddit API.

src/python/reddit/message.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from iop import Message
2-
2+
from typing import Optional
33
from dataclasses import dataclass
4-
54
from obj import PostClass
65

76
@dataclass
87
class PostMessage(Message):
98

10-
post:PostClass = None
11-
to_email_address:str = None
12-
found:str = None
9+
post:Optional[PostClass] = None
10+
to_email_address:Optional[str] = None
11+
found:Optional[str] = None

src/python/reddit/settings.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import os
2+
13
import bp
2-
from bo import *
3-
from bs import *
4+
from bo import FileOperation, FileOperationWithIrisAdapter
5+
from bs import RedditService
46

57
CLASSES = {
68
'Python.RedditService': RedditService,
@@ -29,11 +31,6 @@
2931
"@LogTraceEvents": "true",
3032
"@Schedule": "",
3133
"Setting": [
32-
{
33-
"@Target": "Host",
34-
"@Name": "%settings",
35-
"#text": "path=/tmp"
36-
},
3734
{
3835
"@Target": "Host",
3936
"@Name": "%enable",
@@ -55,14 +52,7 @@
5552
"@Foreground": "false",
5653
"@Comment": "",
5754
"@LogTraceEvents": "false",
58-
"@Schedule": "",
59-
"Setting": [
60-
{
61-
"@Target": "Host",
62-
"@Name": "%settings",
63-
"#text": "limit=10\nother<10"
64-
}
65-
]
55+
"@Schedule": ""
6656
},
6757
{
6858
"@Name": "Python.FilterPostRoutingRule",

0 commit comments

Comments
 (0)