Skip to content

Commit

Permalink
Refactor mastercook text file check (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
cydanil authored Jun 26, 2021
2 parents 4f98bdf + baa1c05 commit 6b5c119
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 31 deletions.
12 changes: 1 addition & 11 deletions src/gourmet/gtk_extras/dialog_extras.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fnmatch
import os.path
import re
import traceback
Expand All @@ -24,14 +23,6 @@ class UserCancelledError(Exception):
pass


def is_markup(s):
try:
Pango.parse_markup(s, '0')
return True
except:
return False


class ModalDialog (Gtk.Dialog):
def __init__(self, default=None, title="", okay=True, label=False,
sublabel=False, parent=None, cancel=True,
Expand Down Expand Up @@ -178,8 +169,7 @@ def setup_dialog(self, *args, **kwargs):
self.set_title(title)

def setup_label(self, label: str):
if not is_markup(label):
label = xml.sax.saxutils.escape(label)
label = xml.sax.saxutils.escape(label)
label = f'<span weight="bold" size="larger">{label}</span>'
self.set_markup(label)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,30 +225,26 @@ def commit_rec(self):


class Tester(importer.Tester):
"""Check whether the given file is a Mastercook export."""

def __init__(self):
"""Initialize the tester with Mastercook regexps.
For this type, there is also another check to ensure that unsupported
look alike are not accepted.
"""
importer.Tester.__init__(self, regexp=MASTERCOOK_START_REGEXP)
self.not_me = "<[?]?(xml|mx2|RcpE|RTxt)[^>]*>"

def test(self, filename):
def test(self, filename: str) -> bool:
"""Test that the provided file is a Mastercook file."""
if not hasattr(self, 'matcher'):
self.matcher = re.compile(self.regexp)
self.not_matcher = re.compile(self.not_me)
if isinstance(filename, str):
self.ofi = open(filename, 'r')
CLOSE = True
else:
self.ofi = filename
CLOSE = False
l = self.ofi.readline()
while l:
if self.not_matcher.match(l):
self.ofi.close()
return False
if self.matcher.match(l):
self.ofi.close()
return True
l = self.ofi.readline()
if CLOSE:
self.ofi.close()
else:
self.ofi.seek(0)

with open(filename) as fin:
for line in fin.readlines():
if self.not_matcher.match(line):
return False
if self.matcher.match(line):
return True
27 changes: 27 additions & 0 deletions tests/recipe_files/mastercook_text_export.mxp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
* Exported from MasterCook *

4th Of July

Recipe By :
Serving Size : 1 Preparation Time :0:00
Categories : Mixed Drinks Specialty Drinks

Amount Measure Ingredient -- Preparation Method
-------- ------------ --------------------------------
1 1/2 ounces Vodka
1/2 ounce Triple sec
1/2 ounce Sweet and sour mix
1/2 ounce Curacao
1 dash Grenadine

Mix all ingredients except grenadine in shaker and chill. Serve in martini glass. Add grenadine and it will sink to bottom. This is a great specialty drink for Independance Day.

Recipe Source:
THE ALL DRINKS LIST compiled by Andy Premaza
<http://cutter.ship.edu/~ap4269/alldrink.html>

Formatted for MasterCook by Joe Comiskey, aka MR MAD - [email protected] -or- [email protected]

06-17-1998

- - - - - - - - - - - - - - - - - -
7 changes: 7 additions & 0 deletions tests/test_imports_from_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

from gourmet.importers.importManager import ImportFileList, ImportManager
from gourmet.plugins.import_export.mastercook_import_plugin.mastercook_plaintext_importer import Tester as MCTester # noqa
from gourmet.recipeManager import RecipeManager

TEST_FILE_DIRECTORY = Path(__file__).parent / 'recipe_files'
Expand Down Expand Up @@ -153,3 +154,9 @@ def test_mycookbook():
'link': 'https://www.allrecipes.com/recipe/10549/best-brownies/', # noqa
}
})


def test_mastercook_file_tester():
filename = TEST_FILE_DIRECTORY / 'mastercook_text_export.mxp'
tester = MCTester()
assert tester.test(filename)

0 comments on commit 6b5c119

Please sign in to comment.