Ejemplo n.º 1
0
 def post(self):
     link = Link()
     if users.get_current_user():
         link.user = users.get_current_user()
         link.url = self.request.get('url')
         link.tags = self.get_tags(random.randint(1,7))
         link.notes = self.request.get('notes')
         link.is_privat = True if self.request.get('is_privat') else False
         link.put()
         self.redirect('/')
Ejemplo n.º 2
0
 def __init__(self,
              node_id=0,
              name='',
              links=None,
              sum_cost=0,
              msg_count=0):
     self.node_id = node_id
     self.name = name
     self.links = links  # All links
     self.root_id = node_id  # ID of the root
     self.sum_cost = sum_cost  # Sum cost from this node to the root node
     self.next_link = Link(name, 'Root')  # Next link to the root
     self.msg_count = msg_count  # Broadcast message count
    def import_file(self, import_path, start_node: Node, dest_node: Node):
        """
        Read the csv file from the given import path.
        :param start_node: Start node of the graph.
        :param dest_node: Destination node of the graph.
        :param import_path: Path to the csv file.
        :return: Links and nodes from the csv file.
        """
        # If the import path is not valid, exit
        if not self.is_path_valid(import_path):
            sys.exit(2)

        # Add the start and dest nodes to the nodes list
        self.nodes.append(start_node)
        self.nodes.append(dest_node)

        # Starting the import
        with open(import_path) as csv_file:
            # Iterate through each row
            reader = csv.reader(csv_file, delimiter=';')
            for i, row in enumerate(reader):
                if i == 0:
                    continue
                # Get the nodes
                node1 = Node(
                    (int(row[0] or '0'), int(row[1] or '0'), int(row[2]
                                                                 or '0')))
                node2 = Node(
                    (int(row[3] or '0'), int(row[4] or '0'), int(row[5]
                                                                 or '0')))
                # Add the nodes, if they haven't been added yet
                found_node = next(
                    (node for node in self.nodes if node1 == node), None)
                if found_node:
                    node1 = found_node
                else:
                    self.nodes.append(node1)
                found_node = next(
                    (node for node in self.nodes if node2 == node), None)
                if found_node:
                    node2 = found_node
                else:
                    self.nodes.append(node2)
                # Add the link
                self.links.append(
                    Link(node1, node2, int(row[6] or '0'), int(row[7] or '0'),
                         int(row[8] or '0'), int(row[9] or '0')))

        # Print the file if debug is enabled
        self.print_file()
        return self.links, self.nodes
Ejemplo n.º 4
0
	def get(self):
		user = users.get_current_user()
		links_query = Link.all().order('-date')

		if user:
			links_query.filter('user ='******'index.html' )
			tmplt_vals = {
				'include': render_basic_includes(
					tmplt_vals_nav = { 'active': NAV.HOME }
				),
				'user': user.nickname(),
				'last_100_links': last_100_links,
				'logout_url': users.create_logout_url('/')
			}
			rendered_index = template.render( path_index, tmplt_vals )
			self.response.out.write( rendered_index )
		else:
			self.redirect(users.create_login_url(self.request.uri))
Ejemplo n.º 5
0
def read_file():
    # Get inputfile from command line
    inputfile = ''
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'i:', ['ifile='])
    except getopt.GetoptError:
        print('You need to pass at least one argument:')
        print('__main__.py -i <inputfile>')
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-i", "--ifile"):
            inputfile = arg
    if len(sys.argv) < 3:
        print('You need to pass at least one argument:')
        print('__main__.py -i <inputfile>')
        sys.exit(2)
    # Read file
    with open(
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         inputfile), 'r') as file:
        nodes = []
        links = []
        counter = 0
        print('Input:')
        for line in file:
            counter += 1
            # Check if the maximum number of items has been reached
            if counter > MAX_ITEMS:
                break
            # Check if line is node
            result = re.search(
                '^\\s*([a-zA-Z][a-zA-Z0-9]*)\\s?=\\s?([0-9]*);.*', line)
            if result:
                # Check if name exceeds max length
                if len(result.group(1)) > MAX_IDENT:
                    continue
                # Check if node id exceeds max number
                if int(result.group(2)) > MAX_NODE_ID:
                    continue
                # Check if name is root (reserved)
                if result.group(1) is 'Root':
                    continue
                nodes.append(Node(int(result.group(2)), result.group(1)))
                print(nodes[-1].name, ':', nodes[-1].node_id)
                continue
            # Check if line is link
            result = re.search(
                '^\\s*([a-zA-Z][a-zA-Z0-9]*)\\s?-\\s?([a-zA-Z][a-zA-Z0-9]*)\\s?:\\s?([0-9]*);.*',
                line)
            if result:
                # Check if name exceeds max length
                if len(result.group(1)) > MAX_IDENT or len(
                        result.group(2)) > MAX_IDENT:
                    continue
                # Check if link cost exceeds max cost
                if int(result.group(3)) > MAX_COST:
                    continue
                # Check if name is root (reserved)
                if result.group(1) is 'Root' or result.group(2) is 'Root':
                    continue
                links.append(
                    Link(result.group(1), result.group(2),
                         int(result.group(3))))
                print(links[-1].node_id_1, '-', links[-1].node_id_2, ':',
                      links[-1].cost)
                continue
            # Check if line is comment
            result = re.search('//.*', line)
            if result:
                continue
            # Check if line is empty
            result = re.search('^\\s*$', line)
            if result:
                continue
            print('Error in line (', counter, '): ', line)

        return Graph(nodes, links)