Skip to content

Commit

Permalink
Removed wrappers. (CycodeLabs#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-pticha authored Sep 20, 2023
1 parent 1608f5c commit 7651cdb
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 60 deletions.
37 changes: 0 additions & 37 deletions exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,6 @@
ServiceUnavailable
)

def catch_redis_exception(f):
@functools.wraps(f)
def func(*args, **kwargs):
try:
return f(*args, **kwargs)

except ConnectionError as e:
print(f"[x] Redis connection error, make sure Redis is running by executing: make setup")

except TimeoutError as e:
print(f"[x] Redis timeout error, make sure Redis is running by executing: make setup")

except RedisError:
print(f"[x] Redis error: {e}")

sys.exit(1)

return func


def catch_neo_exception(f):
@functools.wraps(f)
def func(*args, **kwargs):
try:
return f(*args, **kwargs)

except ServiceUnavailable as e:
print(f"[x] Neo4j connection error, make sure Neo4j is running by executing: make setup")

except Neo4jError as e:
print(f"[x] Neo4j error: {e}")

sys.exit(1)

return func


def catch_exit() -> None:
from config import Config
if Config.github_token:
Expand Down
9 changes: 1 addition & 8 deletions graph_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
from py2neo.ogm import GraphObject
from py2neo.data import Node
from typing import Tuple, Optional
from exceptions import catch_neo_exception


class GraphDb(object):
@catch_neo_exception
def __init__(self, uri, user, password):
self.graph = Graph(uri, auth=(user, password))

@catch_neo_exception
def push_object(self, obj: GraphObject):
self.graph.merge(obj)

@catch_neo_exception
def get_object(self, obj: GraphObject) -> Optional[GraphObject]:
"""Tries to find an object in the graph.
Returns None if wasn't found.
Expand All @@ -24,8 +20,7 @@ def get_object(self, obj: GraphObject) -> Optional[GraphObject]:
return None
else:
return matched_obj.first()

@catch_neo_exception

def get_or_create(self, obj: GraphObject) -> Tuple[GraphObject, bool]:
"""Tries to find a similar object using given object _id.
If found one, returns it, together with True value.
Expand All @@ -41,7 +36,6 @@ def get_or_create(self, obj: GraphObject) -> Tuple[GraphObject, bool]:
else:
return matched_obj.first(), True

@catch_neo_exception
def get_all(self, node_type: str) -> list[Node]:
"""
Returns all nodeTypes nodes in the graph.
Expand All @@ -53,6 +47,5 @@ def get_all(self, node_type: str) -> list[Node]:
"""
return list(self.graph.nodes.match(node_type))

@catch_neo_exception
def clean_graph(self):
self.graph.delete_all()
7 changes: 4 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ def main() -> None:
if __name__ == "__main__":
try:
main()
exceptions.catch_exit()
except KeyboardInterrupt:
pass
finally:
exceptions.catch_exit()
exceptions.catch_exit()
except Exception as e:
print(f"Exception: {e}")
12 changes: 0 additions & 12 deletions redis_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import redis
from config import Config
from exceptions import catch_redis_exception


class RedisConnection:
Expand All @@ -25,55 +24,44 @@ def __exit__(self, exc_type, exc_value, traceback):
if self.redis_client:
self.redis_client.close()

@catch_redis_exception
def insert_to_hash(self, hash: str, field: str, value: str) -> None:
try:
self.redis_client.hset(hash, field, value)
except redis.exceptions.ResponseError as e:
print(f"Failed to set value: {e}")

@catch_redis_exception
def insert_to_string(self, key: str, value: str) -> None:
try:
self.redis_client.set(key, value)
except redis.exceptions.ResponseError as e:
print(f"Failed to set value: {e}")

@catch_redis_exception
def get_string(self, key: str) -> str:
return self.redis_client.get(key)

@catch_redis_exception
def insert_to_set(self, set: str, value: str) -> str:
try:
self.redis_client.sadd(set, value)
except redis.exceptions.ResponseError as e:
print(f"Failed to set value: {e}")

@catch_redis_exception
def get_value_from_hash(self, hash: str, field: str) -> str or None:
return self.redis_client.hget(hash, field)

@catch_redis_exception
def exists_in_set(self, set: str, value: str) -> bool:
return bool(self.redis_client.sismember(set, value))

@catch_redis_exception
def get_set_length(self, set: str) -> int:
return self.redis_client.scard(set)

@catch_redis_exception
def get_set_values(self, set: str) -> set:
return self.redis_client.smembers(set)

@catch_redis_exception
def delete_key(self, key: str) -> None:
self.redis_client.delete(key)

@catch_redis_exception
def flush_db(self) -> None:
self.redis_client.flushdb()

@catch_redis_exception
def get_all_keys(self) -> list:
return self.redis_client.keys()

0 comments on commit 7651cdb

Please sign in to comment.