Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace paths with resources #281

Merged
merged 12 commits into from
Nov 5, 2020
29 changes: 13 additions & 16 deletions src/gourmet/GourmetRecipeManager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import os.path
from pkgutil import get_data as _get_data
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do

Suggested change
from pkgutil import get_data as _get_data
from pkgutil import get_data

I remember your point of hiding the imports on import, yet it's not quite idiomatic Python.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would disagree. It may not be common, but in addition to being specifically mentioned in PEP 8, it's used in Python's own standard library:
https://stackoverflow.com/questions/47138594/why-are-python-imports-renamed-with-leading-underscores
https://stackoverflow.com/questions/20757763/why-are-modules-imported-as-name-in-another-module

But besides just stylistic issues, there's an important pragmatic reason as underscored in PEP 8:

Imported names should always be considered an implementation detail. Other modules must not rely on indirect access to such imported names unless they are an explicitly documented part of the containing module's API

Note that imported names become part of a module's symbol table just like any other name, so from foo import * or from foo import get_data will work identically as if get_data was defined in the module itself. The best way to enforce this is by prefixing them with an underscore. While __all__ provides partial mitigation in cases of import *, it doesn't guard against explicit imports:

from foo import get_data  # this must be ok?
from foo import _get_data  # this looks like a problem

And as noted in the PEP:

Even with __all__ set appropriately, internal interfaces ... should still be prefixed with a single leading underscore.

import re
import threading
from gettext import gettext as _
Expand All @@ -15,8 +14,7 @@
from gourmet.exporters.exportManager import ExportManager
from gourmet.exporters.printer import PrintManager
from gourmet.gdebug import debug
from gourmet.gglobals import (DEFAULT_HIDDEN_COLUMNS, REC_ATTRS, doc_base,
icondir, uibase)
from gourmet.gglobals import DEFAULT_HIDDEN_COLUMNS, REC_ATTRS
from gourmet.gtk_extras import WidgetSaver
from gourmet.gtk_extras import dialog_extras as de
from gourmet.gtk_extras import (fix_action_group_importance, mnemonic_manager,
Expand All @@ -28,6 +26,9 @@
get_thread_manager_gui)
from gourmet.timer import show_timer

from .image_utils import load_pixbuf_from_resource as _load_pixbuf_from_resource


UNDO = 1
SHOW_TRASH = 2

Expand Down Expand Up @@ -347,15 +348,11 @@ def show_about (self, *args):
else:
translator = defaults.CREDITS

logo=GdkPixbuf.Pixbuf.new_from_file(os.path.join(icondir,"gourmet.png"))
logo = _load_pixbuf_from_resource('gourmet.svg')

# load LICENSE text file
try:
license_text = open(os.path.join(doc_base,'LICENSE'),'r').read()
except IOError as err:
print("IO Error %s" % err)
except:
print("Unexpexted error")
license_text = _get_data('gourmet', 'data/LICENSE').decode()
assert license_text

paypal_link = """https://www.paypal.com/cgi-bin/webscr?cmd=_donations
&business=Thomas_Hinkle%40alumni%2ebrown%2eedu
Expand All @@ -364,7 +361,7 @@ def show_about (self, *args):
gratipay_link = "https://gratipay.com/on/github/thinkle/"
flattr_link = "http://flattr.com/profile/Thomas_Hinkle/things"

about = Gtk.AboutDialog()
about = Gtk.AboutDialog(parent=self.window)
about.set_artists(version.artists)
about.set_authors(version.authors)
about.set_comments(version.description)
Expand Down Expand Up @@ -404,7 +401,7 @@ def show_about (self, *args):
about.destroy()

def show_help (self, *args):
de.show_faq(os.path.join(doc_base,'FAQ'))
de.show_faq(parent=self.window)

def save (self, file=None, db=None, xml=None):
debug("save (self, file=None, db=None, xml=None):",5)
Expand Down Expand Up @@ -504,7 +501,7 @@ def __init__ (self, rg):
self.rg = rg
self.rmodel = self.rg.rmodel
self.ui=Gtk.Builder()
self.ui.add_from_file(os.path.join(uibase,'recipe_index.ui'))
self.ui.add_from_string(_get_data('gourmet', 'ui/recipe_index.ui').decode())
RecIndex.__init__(self, self.ui, self.rg.rd, self.rg)
self.setup_main_window()

Expand Down Expand Up @@ -871,7 +868,7 @@ def __init__(self):
self.setup_index_columns()
self.setup_hacks()
self.ui=Gtk.Builder()
self.ui.add_from_file(os.path.join(uibase,'recipe_index.ui'))
self.ui.add_from_string(_get_data('gourmet', 'ui/recipe_index.ui').decode())
self.setup_actions()
RecIndex.__init__(self,
ui=self.ui,
Expand Down Expand Up @@ -955,7 +952,7 @@ def selection_changed (self, selected=False):

def setup_main_window(self):
self.window = self.app = Gtk.Window()
self.window.set_icon_from_file(os.path.join(icondir, 'gourmet.png'))
self.window.set_icon(_load_pixbuf_from_resource('gourmet.svg'))
saver = WidgetSaver.WindowSaver(
self.window,
self.prefs.get('app_window', {'window_size': (800, 600)})
Expand Down
4 changes: 2 additions & 2 deletions src/gourmet/backends/DatabaseChooser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os.path
from gettext import gettext as _
from pkgutil import get_data as _get_data

from gi.repository import Gtk

Expand All @@ -20,9 +21,8 @@ def __init__ (self, okcb=lambda x: debug(x,0), modal=True):
self.default_file_directory = gglobals.gourmetdir
self.default_files = {'sqlite':'recipes.db'
}
uifile = os.path.join(gglobals.uibase,'databaseChooser.ui')
self.ui = Gtk.Builder()
self.ui.add_from_file(uifile)
self.ui.add_from_string(_get_data('gourmet', 'ui/databaseChooser.ui').decode())
self.connection_widgets = ['hostEntry','userEntry','pwEntry','dbEntry',
'hostLabel','userLabel','pwLabel','dbLabel',
'pwCheckButton']
Expand Down
4 changes: 2 additions & 2 deletions src/gourmet/batchEditor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import os
from pkgutil import get_data as _get_data

from gi.repository import Gtk

Expand All @@ -14,7 +14,7 @@ def __init__ (self, rg):

def setup_ui (self):
self.ui = Gtk.Builder()
self.ui.add_from_file(os.path.join(gglobals.uibase,'batchEditor.ui'))
self.ui.add_from_string(_get_data('gourmet', 'ui/batchEditor.ui').decode())
self.dialog = self.ui.get_object('batchEditorDialog')
self.setFieldWhereBlankButton = self.ui.get_object('setFieldWhereBlankButton')
self.setup_boxes()
Expand Down
Loading