How to create an importable file

Asked by Mike Strickland on 2015-01-02

Many years ago, I wrote a recipe program in BASIC, which I have been using via dosemu in Ubuntu. Needless to say, Gourmet is a MUCH better application.

I have exported the recipes to a text file, with tags between recipes, but need to format that file in such a way that Gourmet can import it. I have looked at the native Gourmet export file format (I exported recipes I have in Gourmet currently), but it seems to have record numbers for the database, or at least that is what it looks like, and I don't want to overwrite the recipes that are already in Gourmet.

I'm dealing with over 600 recipes, so I want to automate the import, instead of recipe by recipe. I'd appreciate pointers on the layout for a file that I can put all recipes into for Gourmet to import. I know a bit about PHP, so I should be able to write a script to read the text file I created and write it back out in whatever format is needed.

Question information

Language:
English Edit question
Status:
Solved
For:
Gourmet Edit question
Assignee:
No assignee Edit question
Solved by:
Mike Strickland
Solved:
2015-01-30
Last query:
2015-01-30
Last reply:
2015-01-29
Mike Strickland (livinwell) said : #1

If I leave out the record number, will Gourmet import the data into new records? I'm assuming that what appears to be record numbers in the export file are, in fact, record numbers. An example of what I'm referring to:

<recipe id="21">

Launchpad Janitor (janitor) said : #2

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Mike Strickland (livinwell) said : #3

I would still appreciate some information on this topic. I have many recipes that I'd like to import, but do not want to corrupt my current Gourmet database.

Thomas Hinkle (tmhinkle) said : #4

Yes just leave out the record numbers and you should be fine. If you want
to pay it safe you can back up your recipes.db file (in ~/.gourmet on linux
or search for it on Windows).

I don't recall what gourmet does with duplicate record numbers but no
numbers should work easily enough. It certainly wouldn't rely on id numbers
to create bee database entries though there's an outside chance it uses
them to update if they match existing recipes.

Tom

Tom
On Jan 28, 2015 9:26 AM, "Mike Strickland" <
<email address hidden>> wrote:

> Question #259988 on Gourmet changed:
> https://answers.launchpad.net/gourmet/+question/259988
>
> Status: Expired => Open
>
> Mike Strickland is still having a problem:
> I would still appreciate some information on this topic. I have many
> recipes that I'd like to import, but do not want to corrupt my current
> Gourmet database.
>
> --
> You received this question notification because you are a member of
> Gourmet Recipe Manager, which is an answer contact for Gourmet.
>

Mike Strickland (livinwell) said : #5

Thanks so much for the response.

I will create the import file with <recipe> tag, with no 'id=' part in it. I hope to get the script completed, that will write out the file, using your information, today. I did not break up ingredients, when I exported from the DOS app, into the 3 parts needed by the Gourmet format, so I am having to figure out how to handle breaking them up into their components - no 'unit' is causing me grief (like 2 eggs), my script interprets that as amount = 2 and the unit = eggs, which won't be right in Gourmet.

I will take your advice and back up the recipes.db file, prior to the import attempt.

Will post results, once I have attempted to import the file.

Mike Strickland (livinwell) said : #6

OK, got the import file to look right, but import failed. Below is the result of clicking the Details button in the error strip in Gourmet. At this point, Gourmet will not let me cancel the import - have to shut down the program to get out. Re-opening, it appears that the database is OK.

Any thoughts? Maybe id number is required, or at least id number = NULL?

Error 1: Error during GXML2 Importer

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/gourmet/threadManager.py", line 97, in run
    self.do_run()
  File "/usr/lib/python2.7/dist-packages/gourmet/importers/xml_importer.py", line 70, in do_run
    self.parse = xml.sax.parse(self.fn, self.rh)
  File "/usr/lib/python2.7/xml/sax/__init__.py", line 33, in parse
    parser.parse(source)
  File "/usr/lib/python2.7/xml/sax/expatreader.py", line 107, in parse
    xmlreader.IncrementalParser.parse(self, source)
  File "/usr/lib/python2.7/xml/sax/xmlreader.py", line 123, in parse
    self.feed(buffer)
  File "/usr/lib/python2.7/xml/sax/expatreader.py", line 210, in feed
    self._parser.Parse(data, isFinal)
  File "/usr/lib/python2.7/xml/sax/expatreader.py", line 304, in start_element
    self._cont_handler.startElement(name, AttributesImpl(attrs))
  File "/usr/lib/python2.7/dist-packages/gourmet/plugins/import_export/gxml_plugin/gxml2_importer.py", line 32, in startElement
    self.start_ing(recipe_id=self.rec['id'])
KeyError: 'id'

Mike Strickland (livinwell) said : #7

Just attempted to import the file with <recipe id="">. Got same result.

Mike Strickland (livinwell) said : #8

On Thu, 2015-01-29 at 12:31 +0000, Thomas Hinkle wrote:
> Yes just leave out the record numbers and you should be fine.

Looked at the Python script in the error (don't know anything about
Python). To me, it looks like the script might be choking on no record
number.

Any idea of an easy way to get the last used record number in the
Gourmet database?
--
Mike Strickland
<email address hidden>
---------------------------
Homepage: A Rescuer's Guide to Georgia Native Plants
        GeorgiaNatives.net

Mike Strickland (livinwell) said : #9

On Thu, 2015-01-29 at 12:31 +0000, Thomas Hinkle wrote:
> Yes just leave out the record numbers and you should be fine.

Leaving out the record numbers didn't work, so I did some experimenting.
I have managed to get the import to work. Below is the procedure I
followed, notes on that procedure, along with the format I used for the
Gourmet import file.

Might be of use to others.

Important info I learned:
 ID number is required
 Tags must have a value between them or omit the tag set
 Ampersand must be converted to &amp;

Procedure:
Export recipes from old application
Export all recipes from Gourmet (to find largest ID number)
Use text editor to find largest id number in Gourmet export
Write script to parse old application export file to Gourmet format file

 use counter to create unique id number for each recipe
 change all ampersand to &amp;
 omit tags if there is no value for that tag

Gourmet format is (encoding may be different):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE gourmetDoc>
<gourmetDoc>
 <recipe id="NumberHere">
  <title>RecipeName</title>
  <category>Category</category>
  <cuisine>Cuisine</cuisine>
  <source>Source</source>
  <yields>NumberOfServings</yields>
  <ingredient-list>
   <ingredient>
    <amount>Quantity</amount>
    <unit>UnitOfMeasure</unit>
    <item>IngredientDescription</item>
   </ingredient>
  </ingredient-list>
  <instructions>PreparationInstructions</instructions>
 </recipe>
</gourmetDoc>

There are other tags, but, these are the ones I used. You can find
others in a Gourmet export file.

If a tag has no value, leave it out. For example '1 egg' would have an
'amount' and 'item', but no 'unit'.

Repeat 'ingredient' blocks, within the 'ingredient-list' tag, as needed
to cover all ingredients in a recipe.

Instructions can have multiple lines. Use newline to end a line and
newline on a line by itself to create a blank line.

Repeat 'recipe' blocks as needed to cover all recipes to be imported.

WARNING: I do not know if using an ID number, that exists in a Gourmet
database, will overwrite an existing record in the database. I found the
largest number in my database by exporting it and searching for the
largest ID number. My import file started with the next number as the ID
for the first recipe to import.

Mike,

thank you very much for posting this here, and sorry that I couldn't be of any help in this case!
(It's currently one of those phases where I'm quite busy with other things than Gourmet, so I can't unfortunately quite keep pace with the incoming questions. Sorry about that.)

Mike Strickland (livinwell) said : #11

My main concern was corrupting the original Gourmet information. Once I found out that it was only one file, and not a bunch of things that I'd have to replace, if I messed up, I was a lot more willing to take chances with the data - after backing it up, of course.

I understand about 'busy time', mine is coming up around March-April.

I still have one question. I will be sharing recipes with my daughter, and I'd like to use Gourmet export files. If I do an all recipe export, can she import it and have the program just overwrite the recipes that have matching record ID? Or will it add the imported recipes to new records, regardless?

Thomas Hinkle (tmhinkle) said : #12

I'm fairly confident it won't overwrite, but that's because I think it
would be a stupid way to design it and I don't think I was stupid when I
wrote it. That said I too am busy and don't have time to peek at the code.
And off course there's the possibility I did design it wrong.

A final thought: I do have a memory of writing code to map old IDs to new
IDs. I think that code must have been how I made sure that references in
the import file would work in the new file. If that memory is accurate,
then my code must handle ideas right and you had don't have to worry about
import overriding your file.

Again though, I'm busy and lazy so we're all going based on my memory and
not me actually looking at the code.

Best of luck.

Tom
On Jan 30, 2015 12:06 PM, "Mike Strickland" <
<email address hidden>> wrote:

> Question #259988 on Gourmet changed:
> https://answers.launchpad.net/gourmet/+question/259988
>
> Status: Open => Solved
>
> Mike Strickland confirmed that the question is solved:
> My main concern was corrupting the original Gourmet information. Once I
> found out that it was only one file, and not a bunch of things that I'd
> have to replace, if I messed up, I was a lot more willing to take
> chances with the data - after backing it up, of course.
>
> I understand about 'busy time', mine is coming up around March-April.
>
> I still have one question. I will be sharing recipes with my daughter,
> and I'd like to use Gourmet export files. If I do an all recipe export,
> can she import it and have the program just overwrite the recipes that
> have matching record ID? Or will it add the imported recipes to new
> records, regardless?
>
> --
> You received this question notification because you are a member of
> Gourmet Recipe Manager, which is an answer contact for Gourmet.
>

Mike Strickland (livinwell) said : #13

On Fri, 2015-01-30 at 20:36 +0000, Thomas Hinkle wrote:
> A final thought: I do have a memory of writing code to map old IDs to
> new
> IDs. I think that code must have been how I made sure that references
> in
> the import file would work in the new file. If that memory is
> accurate,
> then my code must handle ideas right and you had don't have to worry
> about
> import overriding your file.

That would be the logical thing to do.

> Again though, I'm busy and lazy so we're all going based on my memory
> and
> not me actually looking at the code.

<chuckle> I fully understand - I'd probably do the same. If you do ever
work on that bit of code, might add a note to the question I asked to
let others know the fact of the matter. For me, I'm good.

Thanks.