From f6deda77ff3446d3868e317639fd328171547165 Mon Sep 17 00:00:00 2001 From: Cmatrix1 Date: Tue, 19 Sep 2023 18:06:46 +0330 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20abstraction=20for=20the=20pro?= =?UTF-8?q?cess=20would=20be=20unaware=20of=20what=20component=20is=20Spid?= =?UTF-8?q?er?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastcrawler/core/contracts.py | 24 ++++++++++++++++++++++++ fastcrawler/core/spider.py | 3 ++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 fastcrawler/core/contracts.py diff --git a/fastcrawler/core/contracts.py b/fastcrawler/core/contracts.py new file mode 100644 index 0000000..5e48130 --- /dev/null +++ b/fastcrawler/core/contracts.py @@ -0,0 +1,24 @@ +from abc import ABC, abstractmethod + + +class ProcessItem(ABC): + + @property + def instances(self) -> list["ProcessItem"]: + if not hasattr(self, "_instances"): + self._instances = [self] + return self._instances + + def __rshift__(self, other: "ProcessItem") -> "ProcessItem": + """ + leveraged RSHIFT method for magic in flow >> + objA >> objB >> objC >> objD + """ + self.instances.append(other) + setattr(other, "instances", self.instances) + return other + + @abstractmethod + async def start(self): + ... + diff --git a/fastcrawler/core/spider.py b/fastcrawler/core/spider.py index 41b6254..dcc3ac7 100644 --- a/fastcrawler/core/spider.py +++ b/fastcrawler/core/spider.py @@ -13,8 +13,9 @@ from fastcrawler.parsers.schema import BaseModel from fastcrawler.utils.injection import _Depends +from .contracts import ProcessItem -class Spider: +class Spider(ProcessItem): """ Spider class to create the actual spider interface so that configuration of each spider can be given