245e1e1069889f6a605af380c430f3c3e0cdcb99
2 # -*- coding: utf-8 -*-
3 #######################################################################################
4 # Copyright © 2009 Benoît Pin <pin@cri.ensmp.fr> #
5 # Plinn - http://plinn.org #
8 # This program is free software; you can redistribute it and/or #
9 # modify it under the terms of the GNU General Public License #
10 # as published by the Free Software Foundation; either version 2 #
11 # of the License, or (at your option) any later version. #
13 # This program is distributed in the hope that it will be useful, #
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16 # GNU General Public License for more details. #
18 # You should have received a copy of the GNU General Public License #
19 # along with this program; if not, write to the Free Software #
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
21 #######################################################################################
23 Downloads RSS based order description and make a local human readable file tree
24 to facilitate printing tasks.
29 from urllib2
import HTTPBasicAuthHandler
30 from urllib2
import build_opener
31 from urllib2
import urlopen
32 from xml
.dom
.minidom
import parseString
33 from xml
.dom
import Node
34 from os
import mkdir
, chdir
35 from os
.path
import abspath
, join
, expanduser
, exists
36 from getpass
import getpass
38 ELEMENT_NODE
= Node
.ELEMENT_NODE
40 def getHttpOpener(url
, login
, password
) :
41 auth_handler
= HTTPBasicAuthHandler()
42 host
= '/'.join(url
.split('/', 3)[:3])
43 auth_handler
.add_password('Zope', host
, login
, password
)
44 opener
= build_opener(auth_handler
)
47 def getXml(url
, opener
) :
48 url
= '%s?disable_cookie_login__=1' % url
49 xml
= opener
.open(url
).read()
52 def genFileTree(url
, login
, password
, dest
) :
53 opener
= getHttpOpener(url
, login
, password
)
54 xml
= getXml(url
, opener
)
56 doc
= d
.documentElement
58 channel
= doc
.getElementsByTagName('channel')[0]
59 orderName
= getContentOf(channel
, 'title')
64 for item
in iterElementChildsByTagName(d
.documentElement
, 'item') :
65 ppTitle
= getContentOf(item
, 'pp:title')
66 ppQuantity
= getContentOf(item
, 'pp:quantity')
68 printTypePath
= join(orderName
, ppTitle
)
69 printQuantityPath
= join(orderName
, ppTitle
, ppQuantity
)
71 if not exists(printTypePath
) :
73 infoFile
= open(join(printTypePath
, 'info.txt'), 'w')
74 infoFile
.write(getContentOf(item
, 'pp:title'))
75 infoFile
.write('\n\n')
76 infoFile
.write(getContentOf(item
, 'pp:description'))
79 if not exists(printQuantityPath
) :
80 mkdir(printQuantityPath
)
82 hdUrl
= '%s?disable_cookie_login__=1' % getContentOf(item
, 'link')
83 localFileName
= getContentOf(item
, 'title')
85 localFile
= open(join(printQuantityPath
, localFileName
), 'w')
86 localFile
.write(opener
.open(hdUrl
).read())
89 def iterElementChildsByTagName(parent
, tagName
) :
90 child
= parent
.firstChild
92 if child
.nodeType
== ELEMENT_NODE
and child
.tagName
== tagName
:
94 child
= child
.nextSibling
96 def getContentOf(parent
, tagName
) :
97 child
= parent
.firstChild
99 if child
.nodeType
== ELEMENT_NODE
and child
.tagName
== tagName
:
100 return child
.firstChild
.nodeValue
.encode('utf-8')
101 child
= child
.nextSibling
103 raise ValueError("%r tag not found" % tagName
)
107 url
= raw_input('url flux xml de la commande : ')
108 login
= raw_input('login : ')
109 password
= getpass('mot de passe : ')
110 dest
= raw_input('cible [~/Desktop]')
112 dest
= expanduser('~/Desktop')
114 genFileTree(url
, login
, password
, dest
)
116 if __name__
== '__main__' :