Ejemplo n.º 1
0
def main():
    build = Builder("Toshiba")
    amount_of_goods = 15
    splitter = 60
    goods = build.create_goods(amount_of_goods)
    eldorado = Seller("Eldorado")
    eldorado.add_goods(goods)

    print(build.get_string_repr(eldorado.GoodsList), "\n", "=" * splitter)
    cons_petya = WorkerConsultant("Petya", salary=splitter, greedy_level=1.1)
    cons_vasya = WorkerConsultant("Vasya", salary=200, greedy_level=1.3)
    cons_andrea = WorkerConsultant("Andrea", salary=150, greedy_level=1.5)
    man_victor = WorkerManager("Victor", salary=1000)

    eldorado.add_worker(cons_petya)
    eldorado.add_worker(cons_vasya)
    eldorado.add_worker(cons_andrea)
    eldorado.add_worker(man_victor)

    for i in range(len(eldorado.GoodsList) - 10):
        eldorado.get_random_consultant().sell_one_good(eldorado.GoodsList)

    print("=" * splitter)
    eldorado.get_random_consultant().upscale_prices(eldorado.GoodsList)
    print("=" * splitter)

    for i in range(len(eldorado.GoodsList) - 5):
        eldorado.get_random_consultant().sell_one_good(eldorado.GoodsList)

    print(eldorado.get_workers_info())
    print("=" * splitter)
    man_victor.fire_worker(eldorado.find_worst_consultant(),
                           eldorado.WorkerList)
    print("=" * splitter)

    print("\n", eldorado.get_workers_info(), sep="")

    print(eldorado.get_random_consultant().get_sold_goods_info())
    print("Highest salary", eldorado.get_highest_salary_worker().get_info())
    print("=" * splitter)
    search_id = randint(0, amount_of_goods - 1)
    print("Search Results of id {0}: {1}".format(
        search_id, eldorado.find_good_by_id(search_id)))
    print("=" * splitter)

    print(build.get_string_repr(eldorado.GoodsList))

    print("=" * splitter)
    print("Cost of all goods in", eldorado, eldorado.calculate_prices())
    print("=" * splitter)

    print("Amount of WorkerConsultant class:", WorkerConsultant.get_amount())
    print("Amount of WorkerManager class:", WorkerManager.get_amount())
    print("Amount of Good class:", Good.get_amount())
    print("Amount of Builder class:", Builder.get_amount())
    print("Amount of Seller class:", Seller.get_amount())
    input("PressEnter")
Ejemplo n.º 2
0
	def __init__(self, address, allow_create=True):
		self.address = re.sub("[^A-Za-z0-9]", "", address) # Make sure its correct address
		self.address_short = "%s..%s" % (self.address[:6], self.address[-4:]) # Short address for logging
		self.log = logging.getLogger("Site:%s" % self.address_short)

		self.content = None # Load content.json
		self.peers = {} # Key: ip:port, Value: Peer.Peer
		self.peer_blacklist = SiteManager.peer_blacklist # Ignore this peers (eg. myself)
		self.last_announce = 0 # Last announce time to tracker
		self.worker_manager = WorkerManager(self) # Handle site download from other peers
		self.bad_files = {} # SHA512 check failed files, need to redownload {"inner.content": 1} (key: file, value: failed accept)
		self.content_updated = None # Content.js update time
		self.notifications = [] # Pending notifications displayed once on page load [error|ok|info, message, timeout]
		self.page_requested = False # Page viewed in browser

		self.storage = SiteStorage(self, allow_create=allow_create) # Save and load site files
		self.loadSettings() # Load settings from sites.json
		self.content_manager = ContentManager(self) # Load contents

		if not self.settings.get("auth_key"): # To auth user in site (Obsolete, will be removed)
			self.settings["auth_key"] = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(24))
			self.log.debug("New auth key: %s" % self.settings["auth_key"])
			self.saveSettings()

		if not self.settings.get("wrapper_key"): # To auth websocket permissions
			self.settings["wrapper_key"] = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(12)) 
			self.log.debug("New wrapper key: %s" % self.settings["wrapper_key"])
			self.saveSettings()

		self.websockets = [] # Active site websocket connections

		# Add event listeners
		self.addEventListeners()
Ejemplo n.º 3
0
    def __init__(self, address, allow_create=True, settings=None):
        self.address = re.sub("[^A-Za-z0-9]", "", address)  # Make sure its correct address
        self.address_short = "%s..%s" % (self.address[:6], self.address[-4:])  # Short address for logging
        self.log = logging.getLogger("Site:%s" % self.address_short)
        self.addEventListeners()

        self.content = None  # Load content.json
        self.peers = {}  # Key: ip:port, Value: Peer.Peer
        self.peer_blacklist = SiteManager.peer_blacklist  # Ignore this peers (eg. myself)
        self.time_announce = 0  # Last announce time to tracker
        self.last_tracker_id = random.randint(0, 10)  # Last announced tracker id
        self.worker_manager = WorkerManager(self)  # Handle site download from other peers
        self.bad_files = {}  # SHA check failed files, need to redownload {"inner.content": 1} (key: file, value: failed accept)
        self.content_updated = None  # Content.js update time
        self.notifications = []  # Pending notifications displayed once on page load [error|ok|info, message, timeout]
        self.page_requested = False  # Page viewed in browser
        self.websockets = []  # Active site websocket connections

        self.connection_server = None
        self.storage = SiteStorage(self, allow_create=allow_create)  # Save and load site files
        self.loadSettings(settings)  # Load settings from sites.json
        self.content_manager = ContentManager(self)
        self.content_manager.loadContents()  # Load content.json files
        if "main" in sys.modules and "file_server" in dir(sys.modules["main"]):  # Use global file server by default if possible
            self.connection_server = sys.modules["main"].file_server
        else:
            self.connection_server = None
        if not self.settings.get("auth_key"):  # To auth user in site (Obsolete, will be removed)
            self.settings["auth_key"] = CryptHash.random()
            self.log.debug("New auth key: %s" % self.settings["auth_key"])

        if not self.settings.get("wrapper_key"):  # To auth websocket permissions
            self.settings["wrapper_key"] = CryptHash.random()
            self.log.debug("New wrapper key: %s" % self.settings["wrapper_key"])
Ejemplo n.º 4
0
    def __init__(self, address, allow_create=True):

        self.address = re.sub("[^A-Za-z0-9]", "",
                              address)  # Make sure its correct address
        self.address_short = "%s..%s" % (self.address[:6], self.address[-4:]
                                         )  # Short address for logging
        self.directory = "data/%s" % self.address  # Site data diretory
        self.log = logging.getLogger("Site:%s" % self.address_short)

        if not os.path.isdir(self.directory):
            if allow_create:
                os.mkdir(self.directory)  # Create directory if not found
            else:
                raise Exception("Directory not exists: %s" % self.directory)
        self.content = None  # Load content.json
        self.peers = {}  # Key: ip:port, Value: Peer.Peer
        self.peer_blacklist = SiteManager.peer_blacklist  # Ignore this peers (eg. myself)
        self.last_announce = 0  # Last announce time to tracker
        self.worker_manager = WorkerManager(
            self)  # Handle site download from other peers
        self.bad_files = {}  # SHA512 check failed files, need to redownload
        self.content_updated = None  # Content.js update time
        self.last_downloads = []  # Files downloaded in run of self.download()
        self.notifications = [
        ]  # Pending notifications displayed once on page load [error|ok|info, message, timeout]
        self.page_requested = False  # Page viewed in browser

        self.loadContent(init=True)  # Load content.json
        self.loadSettings()  # Load settings from sites.json

        if not self.settings.get("auth_key"):  # To auth user in site
            self.settings["auth_key"] = ''.join(
                random.choice(string.ascii_uppercase + string.ascii_lowercase +
                              string.digits) for _ in range(24))
            self.log.debug("New auth key: %s" % self.settings["auth_key"])
            self.saveSettings()

        if not self.settings.get(
                "wrapper_key"):  # To auth websocket permissions
            self.settings["wrapper_key"] = ''.join(
                random.choice(string.ascii_uppercase + string.ascii_lowercase +
                              string.digits) for _ in range(12))
            self.log.debug("New wrapper key: %s" %
                           self.settings["wrapper_key"])
            self.saveSettings()

        self.websockets = []  # Active site websocket connections

        # Add event listeners
        self.addEventListeners()
Ejemplo n.º 5
0
	def __init__(self, address, allow_create=True, settings=None, public_key=None):
		if public_key:
			self.address = CryptArk.getAddress(public_key)
			self.publicKey = public_key
		else:
			self.address = address
			self.publicKey = SiteManager.getPublicKey(address)
		self.address_hash = hashlib.sha256(self.address).digest()
		self.address_short = "%s..%s" % (self.address[:6], self.address[-4:])  # Short address for logging
		self.log = logging.getLogger("Site:%s" % self.address_short)
		self.addEventListeners()

		self.content = None  # Load content.json
		self.peers = {}  # Key: ip:port, Value: Peer.Peer
		self.peers_recent = collections.deque(maxlen=100)
		self.peer_blacklist = SiteManager.peer_blacklist  # Ignore this peers (eg. myself)
		self.worker_manager = WorkerManager(self)  # Handle site download from other peers
		self.bad_files = {}  # SHA check failed files, need to redownload {"inner.content": 1} (key: file, value: failed accept)
		self.content_updated = None  # Content.js update time
		self.notifications = []  # Pending notifications displayed once on page load [error|ok|info, message, timeout]
		self.page_requested = False  # Page viewed in browser
		self.websockets = []  # Active site websocket connections

		self.connection_server = None
		self.loadSettings(settings)  # Load settings from sites.json
		self.storage = SiteStorage(self, allow_create=allow_create)  # Save and load site files
		self.content_manager = ContentManager(self)
		self.content_manager.loadContents()  # Load content.json files
		if "main" in sys.modules and "file_server" in dir(sys.modules["main"]):  # Use global file server by default if possible
			self.connection_server = sys.modules["main"].file_server
		else:
			self.log.debug("Creating connection server")   # remove
			self.connection_server = FileServer()

		self.announcer = SiteAnnouncer(self)  # Announce and get peer list from other nodes

		if not self.settings.get("auth_key"):  # To auth user in site (Obsolete, will be removed)
			self.settings["auth_key"] = CryptHash.random()
			self.log.debug("New auth key: %s" % self.settings["auth_key"])

		if not self.settings.get("wrapper_key"):  # To auth websocket permissions
			self.settings["wrapper_key"] = CryptHash.random()
			self.log.debug("New wrapper key: %s" % self.settings["wrapper_key"])

		if not self.settings.get("ajax_key"):  # To auth websocket permissions
			self.settings["ajax_key"] = CryptHash.random()
			self.log.debug("New ajax key: %s" % self.settings["ajax_key"])