[pybliographer/wip/gtk3] format: Encapsulate module variable



commit 57de3e995fe0781e8eb12b3091eb9b6dc9e02d63
Author: Germán Poo-Caamaño <gpoo gnome org>
Date:   Wed Oct 11 15:48:34 2017 -0300

    format: Encapsulate module variable
    
    Initialize the global variables inside methods/functions,
    to avoid loading twice the same data when the module is
    loaded multiple times.

 Pyblio/Format/Medline.py      |   18 +++++++++++-------
 Pyblio/Format/test_medline.py |   12 ++++++------
 2 files changed, 17 insertions(+), 13 deletions(-)
---
diff --git a/Pyblio/Format/Medline.py b/Pyblio/Format/Medline.py
index aeaf3b0..36807b4 100644
--- a/Pyblio/Format/Medline.py
+++ b/Pyblio/Format/Medline.py
@@ -22,25 +22,25 @@
 
 # Extension module for Medline files
 
-from Pyblio import Base, Fields, Types, Autoload, Open, Iterator, Utils, Config
-
 import re, string
 
+from Pyblio import Base, Fields, Types, Autoload, Open, Iterator, Utils, Config
 
 header = re.compile ('^(\w\w[\w ][\w ])- (.*)$')
 contin = re.compile ('^      (.*)$')
 
-one_to_one = Config.get ('medline/mapping').data
+one_to_one = None
 
 medurl = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?cmd=prlinks&dbfrom=pubmed&retmode=ref&id='
 
 
-class MedlineIterator (Iterator.Iterator):
+class MedlineIterator(Iterator.Iterator):
+    def __init__(self, file):
+        global one_to_one
+        if one_to_one is None:
+            one_to_one = Config.get ('medline/mapping').data
 
-    def __init__ (self, file):
         self.file = file
-        return
-
 
     def first (self):
         # rewind the file
@@ -217,6 +217,9 @@ class Medline (Base.DataBase):
 
 
 def writer (iter, output, **argh):
+    global one_to_one
+    if one_to_one is None:
+        one_to_one = Config.get ('medline/mapping').data
 
     entry = iter.first ()
     while entry:
@@ -305,4 +308,5 @@ Autoload.register ('format', 'Medline', {'open'  : opener,
 ### Local Variables:
 ### Mode: python
 ### py-master-file : "test_medline.py"
+### encoding: utf-8
 ### End:
diff --git a/Pyblio/Format/test_medline.py b/Pyblio/Format/test_medline.py
index 743c282..f328ca1 100644
--- a/Pyblio/Format/test_medline.py
+++ b/Pyblio/Format/test_medline.py
@@ -8,16 +8,12 @@ locale.setlocale (locale.LC_ALL, '')
 import gettext
 gettext.install ('pybliographer', '/usr/local/share/locale', unicode = True)
 
-sys.path.append (os.path.abspath('../..'))
+basedir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
+sys.path.append (basedir)
 
 from Pyblio import Base, Config, Fields
-
-Config.parse_directory (os.path.abspath('../ConfDir'))
-Config.load_user ()
-
 from Pyblio.Format import Medline
 
-
 example_1 = """PMID- 15985842
 OWN - NLM
 STAT- MEDLINE
@@ -154,9 +150,13 @@ comparison = {'Holmes': 'W. C.',
 
 class ReaderCase (unittest.TestCase):
     def setUp (self):
+        Config.parse_directory(os.path.join(basedir, 'ConfDir'))
+        Config.load_user()
         self.db = Base.DataBase ('//localhost/Internal')
         self.output = cStringIO.StringIO()
 
+    def teardown(self):
+        Config.forget_changes()
 
     def test01(self):
        """Test that all fields are Instances, as


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]