The personal URL shortener
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.8 KiB

import typing
import time
import logging
from hashlib import sha256
from cloudant.document import Document
class Data:
Data object.
def __init__(self, data_db: object,
identifier: str = None,
data: str = None) -> typing.NoReturn:
Initialize the Data object.
:param data_db: The Data database object.
:param identifier: A uniquely generated ID identifying the data object.
:param data: The data to save.
self.logger = logging.getLogger(self.__class__.__name__)
self.data_db = data_db
self.identifier = identifier = data
self.timestamp = time.time()
self.pointers = []
self.data_found = None
def generate_identifier(self) -> typing.NoReturn:
Method to generate and save a new unique ID as the Data object identifier.
hash_object = sha256('utf-8'))
self.identifier = hash_object.hexdigest()
def populate(self, pointer: str = None) -> typing.NoReturn:
Method to populate the Data object fields with proper data and save it in the database.
:param pointer: The unique ID of the pointer object to save with the data.
if self.identifier:
self.logger.debug("The identifier is set, retrieving data...")
self.logger.debug("The data is set, generating an identifier...")
self.logger.debug("Attempting to get the data with "
"the identifier generated...")
if not self.data_found:
self.logger.debug("The data generated is not found, "
def get_data(self) -> typing.NoReturn:
Method to retrieve the Data ojbect from the database.
with Document(self.data_db, self.identifier) as data:
try: = data['value']
self.timestamp = data['timestamp']
self.pointers = data['pointers']
self.data_found = True
except KeyError:
self.data_found = False
def set_data(self, pointer: str) -> typing.NoReturn:
Method to save Data object to the database.
with Document(self.data_db, self.identifier) as data:
data['value'] =
data['timestamp'] = self.timestamp
except KeyError:
data['pointers'] = [pointer]