mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-05-14 05:28:45 +02:00
style: Refactoring imports, adapting MongoConnector to different connection_strings
This commit is contained in:
parent
3cd8860312
commit
e44385ce3a
@ -1,6 +1,6 @@
|
|||||||
from abc import ABC
|
from abc import ABC
|
||||||
|
|
||||||
from models.News import News
|
from News.models.News import News
|
||||||
|
|
||||||
|
|
||||||
class NewsServiceInterface(ABC):
|
class NewsServiceInterface(ABC):
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import pymongo
|
import pymongo
|
||||||
from models.News import News
|
from News.models.News import News
|
||||||
from utils.NewsServiceInterface import NewsServiceInterface
|
from News.utils.NewsServiceInterface import NewsServiceInterface
|
||||||
|
|
||||||
|
|
||||||
class MongoConnector:
|
class MongoConnector:
|
||||||
@ -8,7 +8,7 @@ class MongoConnector:
|
|||||||
self,
|
self,
|
||||||
hostname,
|
hostname,
|
||||||
database: str,
|
database: str,
|
||||||
port: int = 27017,
|
port: int | None,
|
||||||
username: str | None = None,
|
username: str | None = None,
|
||||||
password: str | None = None,
|
password: str | None = None,
|
||||||
):
|
):
|
||||||
@ -20,10 +20,13 @@ class MongoConnector:
|
|||||||
|
|
||||||
def connect(self, hostname, port, username, password) -> pymongo.MongoClient:
|
def connect(self, hostname, port, username, password) -> pymongo.MongoClient:
|
||||||
if username is not None and password is not None:
|
if username is not None and password is not None:
|
||||||
connection_string = f"mongodb://{username}:{password}@{hostname}:{port}"
|
connection_string = f"mongodb+srv://{username}:{password}@{hostname}"
|
||||||
else:
|
else:
|
||||||
connection_string = f"mongodb://{hostname}:{port}"
|
connection_string = f"mongodb+srv://{hostname}"
|
||||||
|
if port is not None:
|
||||||
|
connection_string += f":{port}"
|
||||||
|
connection_string = connection_string.replace("mongodb+srv", "mongodb")
|
||||||
|
print(connection_string)
|
||||||
return pymongo.MongoClient(connection_string)
|
return pymongo.MongoClient(connection_string)
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
from abc import ABC
|
from abc import ABC
|
||||||
from dataclasses import dataclass
|
from dataclasses import asdict, dataclass
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
|
|
||||||
@ -35,3 +35,6 @@ class Company:
|
|||||||
name: str
|
name: str
|
||||||
last_update: str
|
last_update: str
|
||||||
relationships: list[CompanyRelationship]
|
relationships: list[CompanyRelationship]
|
||||||
|
|
||||||
|
def dict(self):
|
||||||
|
return asdict(self)
|
||||||
|
@ -4199,6 +4199,102 @@
|
|||||||
" ) as export_file:\n",
|
" ) as export_file:\n",
|
||||||
" json.dump(dataclasses.asdict(company), export_file, ensure_ascii=False)"
|
" json.dump(dataclasses.asdict(company), export_file, ensure_ascii=False)"
|
||||||
]
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import os\n",
|
||||||
|
"import sys\n",
|
||||||
|
"\n",
|
||||||
|
"module_path = os.path.abspath(os.path.join(\"..\"))\n",
|
||||||
|
"if module_path not in sys.path:\n",
|
||||||
|
" sys.path.append(module_path)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 6,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"mongodb://root:pR0R0v2e2@trisnol.tech:27017\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from News.utils.mongodb.mongo import MongoConnector\n",
|
||||||
|
"from Unternehmensregister.utils.CompanyMongoService import CompanyMongoService\n",
|
||||||
|
"\n",
|
||||||
|
"connector = MongoConnector(\n",
|
||||||
|
" hostname=\"trisnol.tech\",\n",
|
||||||
|
" database=\"transparenzregister\",\n",
|
||||||
|
" username=\"root\",\n",
|
||||||
|
" password=\"pR0R0v2e2\",\n",
|
||||||
|
" port=27017,\n",
|
||||||
|
")\n",
|
||||||
|
"\n",
|
||||||
|
"service = CompanyMongoService(connector)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 15,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
" 0%| | 0/3147 [00:00<?, ?it/s]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"100%|██████████| 3147/3147 [00:30<00:00, 102.30it/s]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Inserted documents: 0\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stderr",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from tqdm import tqdm\n",
|
||||||
|
"import glob\n",
|
||||||
|
"import json\n",
|
||||||
|
"from Unternehmensregister.models.Company import Company\n",
|
||||||
|
"\n",
|
||||||
|
"num_inserted = 0\n",
|
||||||
|
"for file in tqdm(glob.glob1(\"./data/Unternehmensregister/transformed\", \"*.json\")):\n",
|
||||||
|
" path = os.path.join(\"./data/Unternehmensregister/transformed\", file)\n",
|
||||||
|
" with open(path, \"r\", encoding=\"utf-8\") as file_object:\n",
|
||||||
|
" data = json.loads(file_object.read())\n",
|
||||||
|
" company: Company = Company(**data)\n",
|
||||||
|
"\n",
|
||||||
|
" company_db = service.get_by_id(company.id)\n",
|
||||||
|
" if company_db is None:\n",
|
||||||
|
" service.insert(company)\n",
|
||||||
|
" num_inserted += 1\n",
|
||||||
|
"print(f\"Inserted documents: {num_inserted}\")"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
from News.utils.mongodb.mongo import MongoConnector
|
||||||
|
from Unternehmensregister.models.Company import Company
|
||||||
|
from Unternehmensregister.utils.CompanyServiceInterface import CompanyServiceInterface
|
||||||
|
|
||||||
|
|
||||||
|
class CompanyMongoService(CompanyServiceInterface):
|
||||||
|
def __init__(self, connector: MongoConnector):
|
||||||
|
self.collection = connector.database["companies"]
|
||||||
|
|
||||||
|
def get_all(self) -> list[Company]:
|
||||||
|
result = self.collection.find()
|
||||||
|
return list(result)
|
||||||
|
|
||||||
|
def get_by_id(self, id: str) -> Company | None:
|
||||||
|
result = list(self.collection.find({"id": id}))
|
||||||
|
if len(result) == 1:
|
||||||
|
return result[0]
|
||||||
|
return None
|
||||||
|
|
||||||
|
def insert(self, company: Company):
|
||||||
|
return self.collection.insert_one(company.dict())
|
@ -0,0 +1,14 @@
|
|||||||
|
from abc import ABC
|
||||||
|
|
||||||
|
from models import Company
|
||||||
|
|
||||||
|
|
||||||
|
class CompanyServiceInterface(ABC):
|
||||||
|
def get_all(self) -> list[Company.Company]:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def get_by_id(self, id: Company.CompayID) -> Company.Company | None:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def insert(self, company: Company.Company):
|
||||||
|
raise NotImplementedError()
|
Loading…
x
Reference in New Issue
Block a user