Hi Genis,

In data giovedì 14 marzo 2013 11:28:43, Samuele Kaplun ha scritto:
> In data giovedì 14 marzo 2013 11:22:53, Samuele Kaplun ha scritto:
> > In data giovedì 14 marzo 2013 13:49:30, Genis Musulmanbekov ha scritto:
> > > * 2013-03-14 12:47:58 -> NameError: name 'mechanize' is not defined
> > > (invenio_connector.py:459:<module>)
> 
> could you please paste in the email at least the Traceback part? That would
> ease a lot our debugging activity :-)

Thanks! I was able to find the culprit and fix it.

Here’s a patch to fix the bug. It will be included in the next minor release 
of Invenio 1.1.x and higher.

Best regards,
        Samuele
-- 
Samuele Kaplun
Invenio Developer ** <http://invenio-software.org/>
>From c118da89c5dd77441ac30f23f5785a4c5588ef3d Mon Sep 17 00:00:00 2001
From: Samuele Kaplun <[email protected]>
Date: Thu, 14 Mar 2013 15:46:18 +0100
Subject: [PATCH] InvenioConnector: mechanize dependency

* python-mechanize package is an optional Invenio dependency.
  However _SGMLParserFactory and CDSInvenioConnector classes
  in invenio_connector.py were assuming its existence.
  This is now fixed.

Reported-by: Genis Musulmanbekov <[email protected]>
---
 modules/miscutil/lib/invenio_connector.py |   88 ++++++++++++++---------------
 1 file changed, 44 insertions(+), 44 deletions(-)

diff --git a/modules/miscutil/lib/invenio_connector.py b/modules/miscutil/lib/invenio_connector.py
index 49d8a89..7bb1c42 100644
--- a/modules/miscutil/lib/invenio_connector.py
+++ b/modules/miscutil/lib/invenio_connector.py
@@ -455,52 +455,52 @@ class Record(dict):
             return self.server_url + "/"+ CFG_SITE_RECORD +"/" + str(self.recid)
         else:
             return None
-
-class _SGMLParserFactory(mechanize.DefaultFactory):
-    """
-    Black magic to be able to interact with CERN SSO forms.
-    """
-    def __init__(self, i_want_broken_xhtml_support=False):
-        if OLD_MECHANIZE_VERSION:
-            forms_factory = mechanize.FormsFactory(
-                form_parser_class=ClientForm.XHTMLCompatibleFormParser)
-        else:
-            forms_factory = mechanize.FormsFactory(
-                form_parser_class=mechanize.XHTMLCompatibleFormParser)
-        mechanize.Factory.__init__(
-            self,
-            forms_factory=forms_factory,
-            links_factory=mechanize.LinksFactory(),
-            title_factory=mechanize.TitleFactory(),
-            response_type_finder=mechanize._html.ResponseTypeFinder(
-                allow_xhtml=i_want_broken_xhtml_support),
-            )
-
-class CDSInvenioConnector(InvenioConnector):
-    def __init__(self, user="", password="", local_import_path="invenio"):
-        """
-        This is a specialized InvenioConnector class suitable to connect
-        to the CERN Document Server (CDS), which uses centralized SSO.
-        """
-        cds_url = CFG_CDS_URL
-        if user:
-            cds_url = cds_url.replace('http', 'https')
-        super(CDSInvenioConnector, self).__init__(cds_url, user, password, local_import_path=local_import_path)
-
-    def _init_browser(self):
+if MECHANIZE_AVAILABLE:
+    class _SGMLParserFactory(mechanize.DefaultFactory):
         """
-        @note: update this everytime the CERN SSO login form is refactored.
+        Black magic to be able to interact with CERN SSO forms.
         """
-        self.browser = mechanize.Browser(factory=_SGMLParserFactory(i_want_broken_xhtml_support=True))
-        self.browser.set_handle_robots(False)
-        self.browser.open(self.server_url)
-        self.browser.follow_link(text_regex="Sign in")
-        self.browser.select_form(nr=0)
-        self.browser.form['ctl00$ctl00$NICEMasterPageBodyContent$SiteContentPlaceholder$txtFormsLogin'] = self.user
-        self.browser.form['ctl00$ctl00$NICEMasterPageBodyContent$SiteContentPlaceholder$txtFormsPassword'] = self.password
-        self.browser.submit()
-        self.browser.select_form(nr=0)
-        self.browser.submit()
+        def __init__(self, i_want_broken_xhtml_support=False):
+            if OLD_MECHANIZE_VERSION:
+                forms_factory = mechanize.FormsFactory(
+                    form_parser_class=ClientForm.XHTMLCompatibleFormParser)
+            else:
+                forms_factory = mechanize.FormsFactory(
+                    form_parser_class=mechanize.XHTMLCompatibleFormParser)
+            mechanize.Factory.__init__(
+                self,
+                forms_factory=forms_factory,
+                links_factory=mechanize.LinksFactory(),
+                title_factory=mechanize.TitleFactory(),
+                response_type_finder=mechanize._html.ResponseTypeFinder(
+                    allow_xhtml=i_want_broken_xhtml_support),
+                )
+
+    class CDSInvenioConnector(InvenioConnector):
+        def __init__(self, user="", password="", local_import_path="invenio"):
+            """
+            This is a specialized InvenioConnector class suitable to connect
+            to the CERN Document Server (CDS), which uses centralized SSO.
+            """
+            cds_url = CFG_CDS_URL
+            if user:
+                cds_url = cds_url.replace('http', 'https')
+            super(CDSInvenioConnector, self).__init__(cds_url, user, password, local_import_path=local_import_path)
+
+        def _init_browser(self):
+            """
+            @note: update this everytime the CERN SSO login form is refactored.
+            """
+            self.browser = mechanize.Browser(factory=_SGMLParserFactory(i_want_broken_xhtml_support=True))
+            self.browser.set_handle_robots(False)
+            self.browser.open(self.server_url)
+            self.browser.follow_link(text_regex="Sign in")
+            self.browser.select_form(nr=0)
+            self.browser.form['ctl00$ctl00$NICEMasterPageBodyContent$SiteContentPlaceholder$txtFormsLogin'] = self.user
+            self.browser.form['ctl00$ctl00$NICEMasterPageBodyContent$SiteContentPlaceholder$txtFormsPassword'] = self.password
+            self.browser.submit()
+            self.browser.select_form(nr=0)
+            self.browser.submit()
 
 class RecordsHandler(xml.sax.handler.ContentHandler):
     "MARCXML Parser"
-- 
1.7.10.4

Reply via email to