Skip to content

Commit

Permalink
added massive logger and fixed #13
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Spasov authored and Martin Spasov committed Sep 24, 2017
1 parent 2146ad0 commit 8fe309a
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 120 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ Noted contributors
* **[Tiax](https://bitbucket.org/tiax)**

* Undo/redo functionality

* **[Mirko Brombin](https://github.com/mirkobrombin)

* Fixed #13 and updated the Screenshot
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The Trello board can be found here : https://trello.com/b/mc6cU0Mn

Any help is welcomed :)

![noted](https://github.com/mirkobrombin/Noted/raw/master/screenshot.png)
![noted](https://raw.githubusercontent.com/suburbanfilth/Noted/master/screenshot.png)

# Installation

Expand Down
2 changes: 1 addition & 1 deletion data/com.github.suburbanfilth.noted.appdata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</description>
<screenshots>
<screenshot type="default">
<image>https://user-images.githubusercontent.com/7277719/30231519-30520d98-94a0-11e7-86df-1a486324e287.png</image>
<image>https://raw.githubusercontent.com/suburbanfilth/Noted/master/screenshot.png</image>
</screenshot>
</screenshots>
<developer_name>Martin Spasov</developer_name>
Expand Down
16 changes: 15 additions & 1 deletion noted/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
from database_tables import Notebook, Note, Base
import os
import subprocess
from logger import logger as lg

class Database(object):

logger = lg.Logger()

@lg.logging_decorator(logger)
def start_database(self):
path = GLib.get_user_data_dir()
db_path = "{}/Noted/sqlitedatabase.db".format(path)
Expand All @@ -21,9 +25,11 @@ def start_database(self):
DBSession = sessionmaker(bind=self.engine)
self.session = DBSession()

@lg.logging_decorator(logger)
def close_database(self):
self.session.close()

@lg.logging_decorator(logger)
def create_note(self,name,content,idd,notebook_id):
try:
note = Note(name=unicode(name,'iso-8859-1'),content=unicode(content,'iso-8859-1'),idd = idd,notebook_id = notebook_id)
Expand All @@ -32,11 +38,13 @@ def create_note(self,name,content,idd,notebook_id):
self.session.add(note)
self.session.commit()

@lg.logging_decorator(logger)
def create_notebook(self,name,idd):
notebook = Notebook(name=name, idd = idd)
self.session.add(notebook)
self.session.commit()

@lg.logging_decorator(logger)
def delete_notebook(self,idd):
notes = self.session.query(Note).filter_by(notebook_id=idd).all()
notebook = self.session.query(Notebook).filter_by(idd=idd).one()
Expand All @@ -54,6 +62,7 @@ def delete_notebook(self,idd):
self.session.delete(notebook)
self.session.commit()

@lg.logging_decorator(logger)
def delete_note(self,idd):
note = self.session.query(Note).filter_by(idd=idd).one()
notebook = self.session.query(Notebook).filter_by(idd=note.notebook_id).one()
Expand All @@ -66,6 +75,7 @@ def delete_note(self,idd):
note.deleted_notebook_id = notebook.idd
self.session.commit()

@lg.logging_decorator(logger)
def restore_note(self,idd):
note = self.session.query(Note).filter_by(idd=idd).one()
notebook_name = note.deleted_notebook_name
Expand All @@ -83,21 +93,25 @@ def restore_note(self,idd):
result = True
self.session.commit()
return note.notebook_id,notebook_name,result


@lg.logging_decorator(logger)
def modify_note(self,name,content,idd):
note = self.session.query(Note).filter_by(idd=idd).one()
note.name = unicode(name,'iso-8859-1')
note.content = unicode(content,'iso-8859-1')
self.session.commit()

@lg.logging_decorator(logger)
def get_notebooks(self):
notebooks = self.session.query(Notebook).all()
return notebooks

@lg.logging_decorator(logger)
def get_notes_from_notebook(self,notebook_id):
notes = self.session.query(Note).filter_by(notebook_id = notebook_id).all()
return notes

@lg.logging_decorator(logger)
def get_note(self,idd):
result = self.session.query(Note).filter_by(idd=idd).one()
return result
38 changes: 34 additions & 4 deletions noted/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
from gi.repository import Gtk, Gdk, Pango, GdkPixbuf
import format_toolbar as ft
import subprocess
from logger import logger as lg

WHITESPACE = ('\r', '\n', '\t', ' ')

class UndoableInsert(object):

logger = lg.Logger()

@lg.logging_decorator(logger)
def __init__(self,text_iter,text,text_len):

self.offset = text_iter.get_offset()
Expand All @@ -22,6 +26,9 @@ def __init__(self,text_iter,text,text_len):

class UndoableDelete(object):

logger = lg.Logger()

@lg.logging_decorator(logger)
def __init__(self,buf, start_iter,end_iter):

self.text = buf.get_text(start_iter, end_iter,False)
Expand All @@ -39,7 +46,10 @@ def __init__(self,buf, start_iter,end_iter):
self.mergeable = True

class UndoableInsertTag(object):

logger = lg.Logger()

@lg.logging_decorator(logger)
def __init__(self, tag, start, end):
self.tag = tag
self.start = start.get_offset()
Expand All @@ -48,6 +58,9 @@ def __init__(self, tag, start, end):

class UndoableDeleteTag(object):

logger = lg.Logger()

@lg.logging_decorator(logger)
def __init__(self,tag,start,end):
self.tag = tag
self.start = start.get_offset()
Expand All @@ -57,6 +70,9 @@ def __init__(self,tag,start,end):

class Editor(Gtk.Grid):

logger = lg.Logger()

@lg.logging_decorator(logger)
def __init__(self,parent):

Gtk.Grid.__init__(self, row_spacing=5, column_spacing=2)
Expand Down Expand Up @@ -169,6 +185,7 @@ def __init__(self,parent):
self.attach(self.scrolled_window, 0, 0, 2, 1)
self.attach(self.format_toolbar, 0, 1, 2, 1)

@lg.logging_decorator(logger)
def get_text(self,start=None,end=None):
if not start:
start = self.textbuffer.get_start_iter()
Expand All @@ -179,11 +196,13 @@ def get_text(self,start=None,end=None):
start,
end)

@lg.logging_decorator(logger)
def get_clean_text(self):

return self.textbuffer.get_text(self.textbuffer.get_start_iter(),
self.textbuffer.get_end_iter(), False)

@lg.logging_decorator(logger)
def set_text(self, content):
self.textbuffer.set_text("")
if content != "":
Expand All @@ -197,6 +216,7 @@ def set_text(self, content):
else:
pass

@lg.logging_decorator(logger)
def toggle_tag(self, widget, tag):
#
# Toggles tag on selection, it does not do anything if there is no selection
Expand Down Expand Up @@ -230,6 +250,7 @@ def toggle_tag(self, widget, tag):
self.undo_stack.append(undo_action)
self.textview.grab_focus()

@lg.logging_decorator(logger)
def apply_tag(self, widget, tag):
#
# Applyes tag on selection, this is currently used
Expand All @@ -255,6 +276,7 @@ def apply_tag(self, widget, tag):
self.undo_stack.append(undo_action)
self.textview.grab_focus()

@lg.logging_decorator(logger)
def apply_just(self,widget,tag):
# gets an itter at the current offset and then gets the current line from it.
# if there is only one char,then the start iter and the end iter are the same.
Expand All @@ -277,7 +299,7 @@ def apply_just(self,widget,tag):
self.textbuffer.apply_tag(self.tags[tag],start_iter,end_iter)
self.textview.grab_focus()


@lg.logging_decorator(logger)
def insert_with_tags(self, buf, start_iter, data, data_len):
end = self.textbuffer.props.cursor_position
#creating new start iter because the provided one
Expand Down Expand Up @@ -369,6 +391,7 @@ def can_be_merged(prev,cur):
self.current_indent_level += 1
self.textview.grab_focus()

@lg.logging_decorator(logger)
def delete(self,buff, start,end):
if buff.get_text(start,end,False) == '\t' and \
start.get_line_offset() <= self.current_indent_level and \
Expand Down Expand Up @@ -419,12 +442,14 @@ def can_be_merged(prev,cur):
self.undo_stack.append(prev_delete)
self.undo_stack.append(undo_action)

@lg.logging_decorator(logger)
def delete_after(self,buff,start,end):
if self.offset_after_tab_deletion:
self.textbuffer.insert(buff.get_iter_at_offset(self.offset_after_tab_deletion),'-',1)
self.textbuffer.insert(buff.get_iter_at_offset(self.offset_after_tab_deletion+1),' ',1)
self.offset_after_tab_deletion = None


@lg.logging_decorator(logger)
def undo(self,widget):
if not self.undo_stack:
return
Expand Down Expand Up @@ -458,7 +483,8 @@ def undo(self,widget):
self.textview.grab_focus()
self.not_undoable_action = False
self.undo_in_progress = False


@lg.logging_decorator(logger)
def redo(self,widget):
if not self.redo_stack:
return
Expand Down Expand Up @@ -492,6 +518,7 @@ def redo(self,widget):
self.not_undoable_action = False
self.undo_in_progress = False

@lg.logging_decorator(logger)
def add_image(self, widget):
dialog = Gtk.FileChooserDialog("Pick a file",
None,
Expand Down Expand Up @@ -525,20 +552,23 @@ def add_image(self, widget):

dialog.destroy()

@lg.logging_decorator(logger)
def add_list(self,widget):
if self.format_toolbar.list.get_active():
current_position = self.textbuffer.get_iter_at_offset(self.textbuffer.props.cursor_position)
self.textbuffer.insert(current_position, '\n\t- ')
else:
self.current_indent_level = 1

@lg.logging_decorator(logger)
def send_feedback(self, widget):
try:
result = subprocess.call(
["pantheon-mail", "mailto:[email protected]"])
except OSError:
pass


@lg.logging_decorator(logger)
def activate_shortcuts(self,widget,event):
keyval = event.keyval
keyval_name = Gdk.keyval_name(keyval)
Expand Down
Empty file added noted/logger/__init__.py
Empty file.
28 changes: 28 additions & 0 deletions noted/logger/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import logging
from gi.repository import GLib

class Logger(object):

def __init__(self):
self.logger = logging.getLogger('Main')
self.logger.setLevel(logging.DEBUG)
logging_path = "{}/Noted/Main".format(GLib.get_user_data_dir())
self.handler = logging.FileHandler(logging_path)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(message)s')
self.handler.setFormatter(formatter)
self.logger.addHandler(self.handler)


def logging_decorator(logger):
def decorator(function):
def wrapper(*args,**kwargs):
try:
result = function(*args,**kwargs)
logger.logger.debug('Succesfully completed {}'.format(function.__name__))
return result
except Exception as e:
logger.logger.debug('There was a problem running the function {} ; ERROR MESSAGE > {}'.format(function.__name__,e))
#NOTE figure out how to close on exception
raise e
return wrapper
return decorator
Loading

0 comments on commit 8fe309a

Please sign in to comment.