Example #1
0
def month(year, month):
    """
        Display available days in selected month.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    path = "%s/%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year, month)

    try:
        tree = ft.generate_tree(path)
    except OSError:
        abort(404)

    if not tree:
        abort(404)

    kwargs = {
        "year": year,
        "month": {
            "value": month,
            "name": datetime(int(year), int(month), 1).strftime("%B"),
        },
        "days": tree.keys(),
    }
    return render_theme_template(app.config["DEFAULT_THEME"], "month.html",
                                 **kwargs)
Example #2
0
def index():
    """
        Display available years and current day's images.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    path = app.config["IMAGE_LOCATION_DIR"]
    tree = ft.generate_tree(path)

    utc_time = pytz.utc.localize(datetime.utcnow())
    tz_time = utc_time.astimezone(app.config["TIMEZONE"]) - \
            timedelta(hours=app.config["OFFSET_HOURS"])

    year = str(tz_time.year)
    month = "0%s" % tz_time.month if tz_time.month < 10 else str(tz_time.month)
    day = "0%s" % tz_time.day if tz_time.day < 10 else str(tz_time.day)

    current_day_images = None
    if tree.get(year, {}).get(month, {}).get(day):
        current_day_images = []
        for hour, image in tree[year][month][day].iteritems():
            hour = int(hour)
            date_hour = datetime(int(year), tz_time.month, tz_time.day, hour)
            current_day_images.append({
                "path": image.replace(path, app.config["IMAGE_LOCATION_URL"]),
                "name": date_hour.strftime("%I:00 %p"),
            })

    kwargs = {
        "years": tree,
        "current_day": current_day_images,
    }
    return render_theme_template(app.config["DEFAULT_THEME"], "home.html",
            **kwargs)
Example #3
0
 def __init__(self, numReplicate=3, idCnt=0):
     self.dataServers = []
     self.fileTree = FileTree()
     self.meta = {}
     self.numReplicate = numReplicate
     self.idCnt = idCnt
     self.cv = threading.Condition()
Example #4
0
def year(year):
    """
        Display available months in selected year.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    path = "%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year)

    try:
        tree = ft.generate_tree(path)
    except OSError:
        abort(404)

    if not tree:
        abort(404)

    months = tree.keys()
    month_data = []
    for month in months:
        month_data.append({
            "value": month,
            "name": datetime(year, int(month), 1).strftime("%B"),
        })

    kwargs = {
        "year": year,
        "months": month_data,
    }
    return render_theme_template(app.config["DEFAULT_THEME"], "year.html",
                                 **kwargs)
Example #5
0
def month(year, month):
    """
        Display available days in selected month.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    path = "%s/%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year, month)

    try:
        tree = ft.generate_tree(path)
    except OSError:
        abort(404)

    if not tree:
        abort(404)

    kwargs = {
        "year": year,
        "month": {
            "value": month,
            "name": datetime(int(year), int(month), 1).strftime("%B"),
        },
        "days": tree.keys(),
    }
    return render_theme_template(app.config["DEFAULT_THEME"], "month.html",
            **kwargs)
Example #6
0
def rss_daily():
    """
        RSS feed of images, with a new feed item for each available day.
    """
    domain = app.config["SITE_DOMAIN"]
    path = app.config["IMAGE_LOCATION_DIR"]
    url = app.config["IMAGE_LOCATION_URL"]
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    tree = ft.generate_tree(path)

    rss_items= []
    for years, year_data in tree.iteritems():
        for months, month_data in year_data.iteritems():
            for days, day_data in month_data.iteritems():
                images = []
                for hour, image_path in day_data.iteritems():
                    image = {
                        "path": "http://%s%s" % (domain,
                            image_path.replace(path, url)),
                        "name": "",
                    }
                    images.append(image)
                images = sorted(images)
                rss_items.append(images)

    rss_items = sorted(rss_items, key=lambda day: day[0]["path"], reverse=True)

    dir_re = re.compile(r'/([0-9]{1,4})/([0-9]{2,2})/([0-9]{2,2})/')
    rss_data = []

    for item in rss_items:
        match = dir_re.search(item[0]["path"])
        year = match.group(1),
        month = match.group(2),
        day = match.group(3),

        year = year[0]
        month = month[0]
        day = day[0]

        date = datetime(int(year), int(month), int(day))
        date_name = date.strftime("%A, %B %d, %Y")
        pub_date = date.strftime("%a, %d %b %Y, 00:00:00")

        rss_data.append({
            "images": item,
            "url": "http://%s/%s/%s/%s/" % (app.config["SITE_DOMAIN"], year,
                month, day),
            "date_name": date_name,
            "pub_date": pub_date,
        })

    kwargs = {
        "rss": rss_data,
        "pub_date": rss_data[0]["pub_date"],
        "link": "http://%s/" % app.config["SITE_DOMAIN"],
    }
    response = make_response(render_template("rss_daily.xml", **kwargs))
    response.headers["Content-type"] = "application/xml"
    return response
Example #7
0
def year(year):
    """
        Display available months in selected year.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    path = "%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year)

    try:
        tree = ft.generate_tree(path)
    except OSError:
        abort(404)

    if not tree:
        abort(404)

    months = tree.keys()
    month_data = []
    for month in months:
        month_data.append({
            "value": month,
            "name": datetime(year, int(month), 1).strftime("%B"),
        })

    kwargs = {
        "year": year,
        "months": month_data,
    }
    return render_theme_template(app.config["DEFAULT_THEME"], "year.html",
            **kwargs)
Example #8
0
def hour(year, month, day, hour):
    """
        Display image for selected hour if it exists.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    path = "%s/%s/%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year, month, day)

    try:
        image_files = ft.generate_tree(path)
    except OSError:
        abort(404)

    if not image_files:
        abort(404)

    kwargs = {
        "year":
        year,
        "month": {
            "value": month,
            "name": datetime(int(year), int(month), int(day)).strftime("%B")
        },
        "day":
        day,
        "hour":
        hour,
        "hour_name":
        datetime(int(year), int(month), int(day),
                 int(hour)).strftime("%I:00 %p"),
        "image":
        image_files[hour].replace(app.config["IMAGE_LOCATION_DIR"],
                                  app.config["IMAGE_LOCATION_URL"]),
    }
    return render_theme_template(app.config["DEFAULT_THEME"], "hour.html",
                                 **kwargs)
Example #9
0
def index():
    """
        Display available years and current day's images.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    path = app.config["IMAGE_LOCATION_DIR"]
    tree = ft.generate_tree(path)

    utc_time = pytz.utc.localize(datetime.utcnow())
    tz_time = utc_time.astimezone(app.config["TIMEZONE"]) - \
            timedelta(hours=app.config["OFFSET_HOURS"])

    year = str(tz_time.year)
    month = "0%s" % tz_time.month if tz_time.month < 10 else str(tz_time.month)
    day = "0%s" % tz_time.day if tz_time.day < 10 else str(tz_time.day)

    current_day_images = None
    if tree.get(year, {}).get(month, {}).get(day):
        current_day_images = []
        for hour, image in tree[year][month][day].iteritems():
            hour = int(hour)
            date_hour = datetime(int(year), tz_time.month, tz_time.day, hour)
            current_day_images.append({
                "path":
                image.replace(path, app.config["IMAGE_LOCATION_URL"]),
                "name":
                date_hour.strftime("%I:00 %p"),
            })

    kwargs = {
        "years": tree,
        "current_day": current_day_images,
    }
    return render_theme_template(app.config["DEFAULT_THEME"], "home.html",
                                 **kwargs)
Example #10
0
def hour(year, month, day, hour):
    """
        Display image for selected hour if it exists.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    path = "%s/%s/%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year, month, day)

    try:
        image_files = ft.generate_tree(path)
    except OSError:
        abort(404)

    if not image_files:
        abort(404)

    kwargs = {
        "year": year,
        "month": {
            "value": month,
            "name": datetime(int(year), int(month), int(day)).strftime("%B")
        },
        "day": day,
        "hour": hour,
        "hour_name": datetime(int(year), int(month), int(day),
                int(hour)).strftime("%I:00 %p"),
        "image": image_files[hour].replace(app.config["IMAGE_LOCATION_DIR"],
                app.config["IMAGE_LOCATION_URL"]),
    }
    return render_theme_template(app.config["DEFAULT_THEME"], "hour.html",
            **kwargs)
Example #11
0
 def __init__(self, dir=None, filetree_label=None, texteditor_factory=None):
     if not dir: dir = os.getcwd()
     self._filetree = FileTree(dir=dir, label=filetree_label)
     self._payloadview = PayloadView(texteditor_factory=texteditor_factory)
     self.this = JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                            self._filetree.this, self._payloadview.this)
     self.this.setOneTouchExpandable(True)
     self._filetree.add_tree_selection_listener(self._tree_listener)
     self.this.getRightComponent().setVisible(False)
Example #12
0
def rss_hourly():
    """
        RSS feed of images, with a new feed item for each hourly image.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    tree = ft.generate_tree(app.config["IMAGE_LOCATION_DIR"])

    rss_items = []
    for years, year_data in tree.iteritems():
        for months, month_data in year_data.iteritems():
            for days, day_data in month_data.iteritems():
                for hours, hour_file in day_data.iteritems():
                    rss_items.append(hour_file)

    dir_re = re.compile(
        r'/([0-9]{1,4})/([0-9]{2,2})/([0-9]{2,2})/([0-9]{2,2})\.')
    rss_items = sorted(rss_items, reverse=True)[0:50]
    rss_data = []
    for item in rss_items:
        match = dir_re.search(item)
        year = match.group(1),
        month = match.group(2),
        day = match.group(3),
        hr = match.group(4),

        year = year[0]
        month = month[0]
        day = day[0]
        hr = hr[0]

        date = datetime(int(year), int(month), int(day), int(hr))
        date_name = date.strftime("%A, %B %d, %Y, %I:00 %p")
        pub_date = date.strftime("%a, %d %b %Y, %H:00:00")

        rss_data.append({
            "path":
            "http://%s%s" % (app.config["SITE_DOMAIN"],
                             item.replace(app.config["IMAGE_LOCATION_DIR"],
                                          app.config["IMAGE_LOCATION_URL"])),
            "url":
            "http://%s/%s/%s/%s/%s/" %
            (app.config["SITE_DOMAIN"], year, month, day, hr),
            "date_name":
            date_name,
            "pub_date":
            pub_date,
        })

    kwargs = {
        "rss": rss_data,
        "pub_date": rss_data[0]["pub_date"],
        "link": "http://%s/" % app.config["SITE_DOMAIN"],
    }
    response = make_response(render_template("rss_hourly.xml", **kwargs))
    response.headers["Content-type"] = "application/xml"
    return response
Example #13
0
def rss_hourly():
    """
        RSS feed of images, with a new feed item for each hourly image.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    tree = ft.generate_tree(app.config["IMAGE_LOCATION_DIR"])

    rss_items= []
    for years, year_data in tree.iteritems():
        for months, month_data in year_data.iteritems():
            for days, day_data in month_data.iteritems():
                for hours, hour_file in day_data.iteritems():
                    rss_items.append(hour_file)

    dir_re = re.compile(r'/([0-9]{1,4})/([0-9]{2,2})/([0-9]{2,2})/([0-9]{2,2})\.')
    rss_items = sorted(rss_items, reverse=True)[0:50]
    rss_data = []
    for item in rss_items:
        match = dir_re.search(item)
        year = match.group(1),
        month = match.group(2),
        day = match.group(3),
        hr = match.group(4),

        year = year[0]
        month = month[0]
        day = day[0]
        hr = hr[0]

        date = datetime(int(year), int(month), int(day), int(hr))
        date_name = date.strftime("%A, %B %d, %Y, %I:00 %p")
        pub_date = date.strftime("%a, %d %b %Y, %H:00:00")

        rss_data.append({
            "path": "http://%s%s" % (app.config["SITE_DOMAIN"],
                item.replace(app.config["IMAGE_LOCATION_DIR"],
                app.config["IMAGE_LOCATION_URL"])),
            "url": "http://%s/%s/%s/%s/%s/" % (app.config["SITE_DOMAIN"], year,
                month, day, hr),
            "date_name": date_name,
            "pub_date": pub_date,
        })

    kwargs = {
        "rss": rss_data,
        "pub_date": rss_data[0]["pub_date"],
        "link": "http://%s/" % app.config["SITE_DOMAIN"],
    }
    response = make_response(render_template("rss_hourly.xml", **kwargs))
    response.headers["Content-type"] = "application/xml"
    return response
Example #14
0
class FileGraph(object):
    __metaclass__ = SingletonType

    ## The constructor.
    #
    # @param self The object pointer.
    def __init__(self, file):
        self._graph = FileTree(FileContainer(file))

    ## Adds an (directed) edge between nodes.
    #
    # @param self The object pointer.
    # @param edgeFrom The node from which the edge connected.
    # @param edgeTo The edge to the node to be connected.
    def add_edge(self, edgeFrom, edgeTo, line=0):
        self._graph.addLink(edgeFrom, edgeTo, line)

    ## Getter for the graph to be used
    #
    # @param self The object pointer.
    def getGraph(self):
        return self._graph
Example #15
0
class FileView:
    """
    SplitPane containing an editoresque (Sublime-alike) filetree+editor widget
    """
    def __init__(self, dir=None, filetree_label=None, texteditor_factory=None):
        if not dir: dir = os.getcwd()
        self._filetree = FileTree(dir=dir, label=filetree_label)
        self._payloadview = PayloadView(texteditor_factory=texteditor_factory)
        self.this = JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                               self._filetree.this, self._payloadview.this)
        self.this.setOneTouchExpandable(True)
        self._filetree.add_tree_selection_listener(self._tree_listener)
        self.this.getRightComponent().setVisible(False)

    def _tree_listener(self, e):
        """
        Listen for tree selection and fill the payloadview

        :param e: unused
        :return: None
        """
        try:
            fpath = os.path.join(*[str(p) for p in e.getPath().getPath()][1:])

            if fpath.endswith('.html'):
                self.this.getRightComponent().setVisible(False)
                return

            with open(fpath, 'r') as f:
                payload = f.read()
                self._payloadview.set_editable(True)
                self._payloadview.refresh(payload)
                self.this.getRightComponent().setVisible(True)
                self.this.setDividerLocation(0.25)
        except IOError:
            pass

    def addTreeListener(self, action):
        """
        Add a new Tree ActionListener

        :param action: actionListener lambda
        :return:
        """
        self._filetree.add_tree_selection_listener(action)

    def addPayloadListener(self, action):
        """
        Add a new PayloadView Listener

        :param action: actionListener lambda
        :return:
        """
        self._payloadview.add_listener(action)

    def refresh(self):
        self._filetree.refresh()
Example #16
0
    def createWidgets(self):
        self.selectLabel = ttk.Label(self, text = "Select:")
        self.selectLabel.grid(column = 0, row = 0, padx = 10, pady = 5)
        self.select = Select(self, textvariable = self.selectVar)
        self.select.grid(column = 1, row = 0, sticky = (E, W))

        self.searchLabel = ttk.Label(self, text = "Search:")
        self.searchLabel.grid(column = 2, row = 0, padx = 10, pady = 5)
        self.search = Search(self, textvariable = self.searchVar)
        self.search.grid(column = 3, row = 0, sticky = (E, W))

        self.filestorage = FileStorage(self)
        self.filetree = FileTree(self)
        self.filetree.grid(column = 0, row = 1, rowspan = 4, sticky = (N, S, E, W), columnspan = 4)

        self.scrollbar = ttk.Scrollbar(self, orient = VERTICAL, command = self.filetree.yview)
        self.scrollbar.grid(column = 4, row = 1, rowspan = 4, sticky = (N, S, E))
        self.filetree.configure(yscrollcommand = self.scrollbar.set)

        self.tags = Tags(self)
        self.tags.grid(column = 5, row = 2, sticky = (E, W), padx = 5)

        self.tagsLab = ttk.Label(text = "Tags")
        self.tagsLab.grid(column = 5, row = 1, padx = 5, pady = 2)

        self.notes = Notes(self)
        self.notes.grid(column = 5, row = 4, sticky = (N, S, E, W), padx = 5)

        self.notesLab = ttk.Label(text = "Notes")
        self.notesLab.grid(column = 5, row = 3, padx = 5, pady = 2)

        self.scrollNotes = ttk.Scrollbar(self, orient = VERTICAL, command = self.notes.yview)
        self.scrollNotes.grid(column = 6, row = 4, sticky = (N, S, W))
        self.notes.configure(yscrollcommand = self.scrollNotes.set)

        self.buttons = Buttons(self)
        self.buttons.grid(row = 5, column = 0, columnspan = 5, pady = 5, sticky = (E, W))

        self.statusBar = StatusBar(self)
        self.statusBar.grid(row = 6, column = 0, columnspan = 5, padx = 5, pady = 5, sticky = (E, W))        
Example #17
0
class GUI(Tk):
    "represents GUI"
    def __init__(self):
        super().__init__()
   
        self.option_add("*tearOff", FALSE)
        self.initialized = False
        self.title("Papyer")
        
        x, y = 1500, 500
        self.minsize(x, y)
        placeWindow(self, x, y)
        
        self.options = Options(self)
        self["menu"] = TopMenu(self)        

        self.protocol("WM_DELETE_WINDOW", self.closeFun)

        self.base = os.getcwd()

        self.selectVar = StringVar()
        self.searchVar = StringVar()

        self.createWidgets()

        self.columnconfigure(1, weight = 1)
        self.columnconfigure(3, weight = 1)
        self.columnconfigure(5, weight = 1)
        self.rowconfigure(4, weight = 1)

        self.bind("<Control-d>", lambda e: self.filetree.keepDuplicates())
        self.bind("<Control-a>", lambda e: self.filetree.selectAll())
        
        self.mainloop()



    def refresh(self):
        # do in a smarter way - check changes in the files
        self.filestorage.save()
        self.filetree.saveSettings()
        selected = self.filetree.selection()
        self.createWidgets()
        self.filetree.selection_set(selected)


    def createWidgets(self):
        self.selectLabel = ttk.Label(self, text = "Select:")
        self.selectLabel.grid(column = 0, row = 0, padx = 10, pady = 5)
        self.select = Select(self, textvariable = self.selectVar)
        self.select.grid(column = 1, row = 0, sticky = (E, W))

        self.searchLabel = ttk.Label(self, text = "Search:")
        self.searchLabel.grid(column = 2, row = 0, padx = 10, pady = 5)
        self.search = Search(self, textvariable = self.searchVar)
        self.search.grid(column = 3, row = 0, sticky = (E, W))

        self.filestorage = FileStorage(self)
        self.filetree = FileTree(self)
        self.filetree.grid(column = 0, row = 1, rowspan = 4, sticky = (N, S, E, W), columnspan = 4)

        self.scrollbar = ttk.Scrollbar(self, orient = VERTICAL, command = self.filetree.yview)
        self.scrollbar.grid(column = 4, row = 1, rowspan = 4, sticky = (N, S, E))
        self.filetree.configure(yscrollcommand = self.scrollbar.set)

        self.tags = Tags(self)
        self.tags.grid(column = 5, row = 2, sticky = (E, W), padx = 5)

        self.tagsLab = ttk.Label(text = "Tags")
        self.tagsLab.grid(column = 5, row = 1, padx = 5, pady = 2)

        self.notes = Notes(self)
        self.notes.grid(column = 5, row = 4, sticky = (N, S, E, W), padx = 5)

        self.notesLab = ttk.Label(text = "Notes")
        self.notesLab.grid(column = 5, row = 3, padx = 5, pady = 2)

        self.scrollNotes = ttk.Scrollbar(self, orient = VERTICAL, command = self.notes.yview)
        self.scrollNotes.grid(column = 6, row = 4, sticky = (N, S, W))
        self.notes.configure(yscrollcommand = self.scrollNotes.set)

        self.buttons = Buttons(self)
        self.buttons.grid(row = 5, column = 0, columnspan = 5, pady = 5, sticky = (E, W))

        self.statusBar = StatusBar(self)
        self.statusBar.grid(row = 6, column = 0, columnspan = 5, padx = 5, pady = 5, sticky = (E, W))        
        

    def closeFun(self):
        "ask for saving files on exit"
        self.filetree.saveSettings()
        self.filestorage.save()
        self.options.save()
        self.destroy()
Example #18
0
def rss_daily():
    """
        RSS feed of images, with a new feed item for each available day.
    """
    domain = app.config["SITE_DOMAIN"]
    path = app.config["IMAGE_LOCATION_DIR"]
    url = app.config["IMAGE_LOCATION_URL"]
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    tree = ft.generate_tree(path)

    rss_items = []
    for years, year_data in tree.iteritems():
        for months, month_data in year_data.iteritems():
            for days, day_data in month_data.iteritems():
                images = []
                for hour, image_path in day_data.iteritems():
                    image = {
                        "path":
                        "http://%s%s" %
                        (domain, image_path.replace(path, url)),
                        "name":
                        "",
                    }
                    images.append(image)
                images = sorted(images)
                rss_items.append(images)

    rss_items = sorted(rss_items, key=lambda day: day[0]["path"], reverse=True)

    dir_re = re.compile(r'/([0-9]{1,4})/([0-9]{2,2})/([0-9]{2,2})/')
    rss_data = []

    for item in rss_items:
        match = dir_re.search(item[0]["path"])
        year = match.group(1),
        month = match.group(2),
        day = match.group(3),

        year = year[0]
        month = month[0]
        day = day[0]

        date = datetime(int(year), int(month), int(day))
        date_name = date.strftime("%A, %B %d, %Y")
        pub_date = date.strftime("%a, %d %b %Y, 00:00:00")

        rss_data.append({
            "images":
            item,
            "url":
            "http://%s/%s/%s/%s/" %
            (app.config["SITE_DOMAIN"], year, month, day),
            "date_name":
            date_name,
            "pub_date":
            pub_date,
        })

    kwargs = {
        "rss": rss_data,
        "pub_date": rss_data[0]["pub_date"],
        "link": "http://%s/" % app.config["SITE_DOMAIN"],
    }
    response = make_response(render_template("rss_daily.xml", **kwargs))
    response.headers["Content-type"] = "application/xml"
    return response
Example #19
0
def day(year, month, day):
    """
        Display available images in selected day.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    path = "%s/%s/%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year, month, day)

    try:
        image_files = ft.generate_tree(path)
    except OSError:
        abort(404)

    if not image_files:
        abort(404)

    images = []
    for hour, image in image_files.iteritems():
        images.append({
            "url": image.replace(app.config["IMAGE_LOCATION_DIR"],
                    app.config["IMAGE_LOCATION_URL"]),
            "hour": hour,
            "hour_name": datetime(int(year), int(month), int(day),
                int(hour)).strftime("%I:00 %p"),
        })

    # find next/previous days
    days_tree = ft.generate_tree(app.config["IMAGE_LOCATION_DIR"])
    days = []
    for iter_year, year_data in days_tree.iteritems():
        for iter_month, month_data in year_data.iteritems():
            for iter_day, day_data in month_data.iteritems():
                days.append((iter_year, iter_month, iter_day))
    days = sorted(days)

    prev_day = None
    next_day = None
    for index, a_day in enumerate(days):
        if a_day == (year, month, day):
            if index - 1 >= 0:
                prev_day = days[index - 1]
                prev_day = {
                    "year": prev_day[0],
                    "month": prev_day[1],
                    "day": prev_day[2]
                }
            if index + 1 < len(days):
                next_day = days[index + 1]
                next_day = {
                    "year": next_day[0],
                    "month": next_day[1],
                    "day": next_day[2]
                }
            break

    kwargs = {
        "year": year,
        "month": {
            "value": month,
            "name": datetime(int(year), int(month), int(day)).strftime("%B")
        },
        "day": day,
        "images": images,
        "prev_day": prev_day,
        "next_day": next_day,
    }
    return render_theme_template(app.config["DEFAULT_THEME"], "day.html",
            **kwargs)
Example #20
0
class NameServer:
    def __init__(self, numReplicate=3, idCnt=0):
        self.dataServers = []
        self.fileTree = FileTree()
        self.meta = {}
        self.numReplicate = numReplicate
        self.idCnt = idCnt
        self.cv = threading.Condition()

    def add(self, server):
        self.dataServers.append(server)

    def parse_cmd(self):
        cmd = input("MiniDFS> ")
        parameters = cmd.split(' ')
        return parameters

    def operator(self):
        while True:
            param = self.parse_cmd()
            l = len(param)
            if l == 0:
                print("input a blank line")
                continue

            if param[0] == "quit" or param[0] == "exit":
                print("quit")
                break
            # list all the files in name server.
            elif param[0] == "list":
                if l != 1:
                    print("usage: list (list all the files in name server)")
                else:
                    print("file\tFileID\tChunkNumber")
                    self.fileTree.list_(self.meta)
                continue
            # upload file to miniDFS
            elif param[0] == "put":
                if l != 3:
                    print("usage: put source_file_path des_file_path")
                    continue
                try:
                    f = open(param[1], 'rb')
                except IOError:
                    print("open file error: file %s" % param[1])
                    continue
                if self.fileTree.insert_node(param[2], True):
                    print("create file error \n. maybe the file : %s exists" %
                          param[2])
                    continue
                else:
                    totalSize = os.path.getsize(param[1])
                    f.seek(0, 1)  # whence 0: current, 1: head, 2: tail
                    buf = f.read(totalSize)
                    serverSize = np.array([s.size for s in self.dataServers])
                    idx = np.argsort(serverSize)
                    self.idCnt += 1
                    for i in range(self.numReplicate):
                        self.dataServers[idx[i]].cv.acquire()
                        self.meta[param[2]] = (self.idCnt, totalSize)
                        self.dataServers[idx[i]].cmd = "put"
                        self.dataServers[idx[i]].fid = self.idCnt
                        self.dataServers[idx[i]].bufSize = totalSize
                        self.dataServers[idx[i]].buf = buf
                        self.dataServers[idx[i]].finish = False
                        self.dataServers[idx[i]].cv.notify_all()
                        self.dataServers[idx[i]].cv.release()
                f.close()
            # fetch file from miniDFS
            elif param[0] == "read" or param[0] == "fetch":
                if l != 3 and l != 4:
                    print("usage: read source_file_path dest_file_path")
                    print("usage: fetch FileID Offset dest_file_path")
                    continue
                else:
                    if param[0] == "read" and param[1] not in self.meta:
                        print("error: no such file in miniDFS.")
                        continue
                    for i in range(4):
                        self.dataServers[i].cv.acquire()
                        self.dataServers[i].cmd = param[0]
                        if param[0] == "read":
                            self.dataServers[i].fid, self.dataServers[
                                i].bufSize = meta[param[1]]
                        else:
                            self.dataServers[i].fid, self.dataServers[
                                i].bufSize = int(param[1]), int(param[2])
                        self.dataServers[i].finish = False
                        self.dataServers[i].cv.notify_all()
                        self.dataServers[i].cv.release()
            # locate the data server given file ID and Offset.
            elif param == "locate":
                if l != 3:
                    print("usage: locate fileID Offset")
                    continue
                else:
                    for i in range(4):
                        self.dataServers[i].cv.acquire()
                        self.dataServers[i].cmd = "locate"
                        self.dataServers[i].fid = int(param[1])
                        self.dataServers[i].offset = int(param[2])
                        self.dataServers[i].finish = False
                        self.dataServers[i].cv.release()
                        self.dataServers[i].cv.notify_all()
            else:
                print("wrong command.")

            # waiting for the finish of data server.
            for server in self.dataServers:
                server.cv.acquire()
                while not server.finish:
                    server.cv.wait()
                server.cv.notify_all()
                server.cv.release()

            # work after processing of data server
            if param[0] == "read" or param == "fetch":
                md5 = hashlib.md5()
                pre_checksum = ""
                for i in range(4):
                    if self.dataServers[i].bufSize:
                        if param[0] == "read":
                            try:
                                f = open(param[2], 'wb')
                            except IOError:
                                print(
                                    "create file failed. maybe wrong directory."
                                )
                        elif param[0] == "fetch":
                            try:
                                f = open(param[3], 'wb')
                            except IOError:
                                print(
                                    "create file failed. maybe wrong directory."
                                )
                        f.write(self.dataServers[i].buf)
                        f.close()
                        md5.update(self.dataServers[i].buf)
                        md5_checksum = md5.digest()
                        if pre_checksum and pre_checksum != md5_checksum:
                            raise ValueError(
                                "error: unequal checksum for files from different dataServers. File got may be wrong."
                            )
                        pre_checksum = md5_checksum
                        self.dataServers[i].buf = ""
            elif param[0] == "put":
                print("Upload success. The file ID is %d." % self.idCnt)
            elif param[0] == "locate" or param[0] == "ls":
                notFound = True
                for i in range(4):
                    if self.dataServers[i].bufSize:
                        notFound = False
                        print("found FileID %s offset %s at %s." %
                              (param[1], param[2],
                               self.dataServers[i].get_name()))
                if notFound:
                    print("not found FileID %s offset %s." %
                          (param[1], param[2]))
Example #21
0
 def __init__(self, file):
     self._graph = FileTree(FileContainer(file))
Example #22
0
def day(year, month, day):
    """
        Display available images in selected day.
    """
    ft = FileTree(app.config["TIMEZONE"], app.config["OFFSET_HOURS"])
    path = "%s/%s/%s/%s" % (app.config["IMAGE_LOCATION_DIR"], year, month, day)

    try:
        image_files = ft.generate_tree(path)
    except OSError:
        abort(404)

    if not image_files:
        abort(404)

    images = []
    for hour, image in image_files.iteritems():
        images.append({
            "url":
            image.replace(app.config["IMAGE_LOCATION_DIR"],
                          app.config["IMAGE_LOCATION_URL"]),
            "hour":
            hour,
            "hour_name":
            datetime(int(year), int(month), int(day),
                     int(hour)).strftime("%I:00 %p"),
        })

    # find next/previous days
    days_tree = ft.generate_tree(app.config["IMAGE_LOCATION_DIR"])
    days = []
    for iter_year, year_data in days_tree.iteritems():
        for iter_month, month_data in year_data.iteritems():
            for iter_day, day_data in month_data.iteritems():
                days.append((iter_year, iter_month, iter_day))
    days = sorted(days)

    prev_day = None
    next_day = None
    for index, a_day in enumerate(days):
        if a_day == (year, month, day):
            if index - 1 >= 0:
                prev_day = days[index - 1]
                prev_day = {
                    "year": prev_day[0],
                    "month": prev_day[1],
                    "day": prev_day[2]
                }
            if index + 1 < len(days):
                next_day = days[index + 1]
                next_day = {
                    "year": next_day[0],
                    "month": next_day[1],
                    "day": next_day[2]
                }
            break

    kwargs = {
        "year": year,
        "month": {
            "value": month,
            "name": datetime(int(year), int(month), int(day)).strftime("%B")
        },
        "day": day,
        "images": images,
        "prev_day": prev_day,
        "next_day": next_day,
    }
    return render_theme_template(app.config["DEFAULT_THEME"], "day.html",
                                 **kwargs)