;ς )δHEc@sNdZdkZdkZdklZdkTdkZdkl Z ydk l Z Wne j onXdklZdklZlZdklZd klZd klZeaad „Zd „Zd ed„Zd ed„Zd„Z d„Z!d„Z"d„Z#d„Z$d„Z%d„Z&d„Z'e(djo e'ƒndS(sμ zwikiimport.py - import files/directories into a zwiki. (c) 2004-2005 SKWM, GNU GPL. Usage: cd directorycontainingfiles zopectl run .../ZWiki/tools/zwikiimport.py [opts] /path/to/wikipageorfolder Options: -h, --help show this help message and exit -n, --dry-run Don't actually import anything. May be inaccurate. -v, --verbose Be more verbose. --debug Print additional debug information. --replace When objects already exist, replace them. --delete Delete existing objects instead of importing. Notes/stories/todos: -zope root folder or root page is specified as an argument -we walk the current directory -each text file becomes a wiki page by the same name -a subdirectory becomes a page. the contents are imported and parented under it -a file named after its directory or index.html? is merged with the directory page -images become images, files become files -relative links and image paths are adjusted -allow ignoring/replacing/deleting old pages (as long wiki is anon-writable) todo: suffixes influence the page type: .html, .stx, .rst, .txt etc. an id collision creates a page with modified name a front page may be selected from the imported pages text patterns may be removed from the pages file(s) may be specified on the command line use cmf or plone image/files when appropriate images and files have safe ids assigned if needed use visitor pattern smarter dry run get around anon-writable requirement of zwiki web api apply html tidy automatically ? N(s OptionParser(s*(sguess_content_type(sget(senviron(sstdinsstdout(s HTTPRequest(s HTTPResponse(sRequestContainerc Cs±tƒ}|iddddddddƒ|id d dddd ƒ|id dddd ƒ|idddddƒ|idddddƒ|iƒ\aatt_dS(sParse command-line options.s-ns --dry-runsactions store_truesdestsdryrunshelps2Don't actually import anything (may be inaccurate)s-vs --verbosesBe more verboses--debugs"Print additional debug informations --replaces(When objects already exist, replace thems--deletes,Delete existing objects instead of importingN(s OptionParsersparsers add_options parse_argssoptionssargssNonesrequest(sparser((sA/mnt/gmirror/ports/www/zope-zwiki/work/ZWiki/tools/zwikiimport.pys parseArgs=s       cCswtdtƒ}dtd]*>(.*)iN(sressearchstsmsgroupsNone(stsm((sA/mnt/gmirror/ports/www/zope-zwiki/work/ZWiki/tools/zwikiimport.pys bodyFromHtmlws cCs2tidd|ƒ}tidd|ƒ}|SdS(s^Fix up relative hyperlinks and image paths for the wiki. NB doesn't work after html tidy.s8(?i)( (href|src)=")(?!http:)([^"/]+/)+?(?P[^"/]+)"s \1\g"s2(?i)( (href|src)=")(?!http:)(?P[^"]+).html?"s \1\g"N(sressubst(st((sA/mnt/gmirror/ports/www/zope-zwiki/work/ZWiki/tools/zwikiimport.pys fixLinksIn}s    cCsOtd||fƒtiotdƒtSn|i|ƒ}|oti o2|i dti ƒt ƒi ƒtdƒtSnΚ|otioGt|ƒ}|i|||dti ƒt ƒi ƒtdƒtSnryGt|ƒ}|i|||dti ƒt ƒi ƒtdƒtSWn'tj o}td|ƒtSnXdS( s”Create, modify or delete the specified wiki page under the parent page. Prints a status message and returns a boolean for success/failure. sdoFile(%s,...,%s)s : dry runsREQUESTs : deleteds : replaceds : createds: failed *** (%s)N(sdlogsnamestypesoptionssdryrunsvlogsTruesparents pageWithNamesexistingsdeletesrequestsget_transactionscommitsreplaces fixLinksInstextseditscreates BadRequestsesFalse(sparentsnamestextstypesesexisting((sA/mnt/gmirror/ports/www/zope-zwiki/work/ZWiki/tools/zwikiimport.pysdoPageŒs4          cCs”td|ƒtiotdƒtSn|iƒ}t||t ƒ}|oti o,|i |ƒt ƒiƒtdƒtSn |otio5|i|ƒi|ƒt ƒiƒtdƒtSnΖy›t|ƒddd!djo&|i|tii||dƒƒn#|i|tii||dƒƒ|i|ƒi|ƒt ƒiƒtd ƒtSWn'tj o}td |ƒtSnXd S( s»Create, modify or delete the specified file or image. An Image is created if the file suffix indicates it. Prints a status message and returns a boolean for success/failure. sdoFile(%s,...)s : dry runs : deleteds : replacediisimagess : createds: failed *** (%s)N(sdlogsfilenamesoptionssdryrunsvlogsTruescontextsfoldersgetattrsNonesexistingsdeletes _delObjectsget_transactionscommitsreplaces_getObs manage_uploadsdatasguess_content_types _setObjectsOFSsImagesFiles BadRequestsesFalse(scontextsfilenamesdatasesfoldersexisting((sA/mnt/gmirror/ports/www/zope-zwiki/work/ZWiki/tools/zwikiimport.pysdoFile―s8        &"  c CsFtd| |fƒt|dtƒtii|ƒoΙtii|ƒ}tii tii|ƒƒ\}} t id| ƒoPtt|ƒiƒƒ}|tjo't|dtƒt|||dƒqύqBt|dtƒt||t|ƒiƒƒnBt|dtƒt|ƒ}|djpt||ddƒo|i|ƒp|}d}x―ti|ƒD]ž}tii tii|ƒƒ\}} t id| ƒo ||jpt id|ƒo%ttii||ƒƒiƒ}qlt|tii||ƒƒqlW|o)t|ƒ}|i|ƒi d|ƒqBndS( s@Import a file or directory tree as wiki page/file/image objects.simportFile(%s,%s)snewlines(?i).htmshtmlss (?i)index$stextN(!sdlogscontextsfilepathsvlogsFalsesosspathsisfilesbasenamesfilenamessplitextspagenamesextsresmatchs bodyFromHtmlsopensreadstextsNonesdoPagesdoFilespageNameFromPaths dirpagenames pageWithNamesdirpages dirpagetextslistdirsfsjoins importFiles fixLinksInsedit( scontextsfilepathstexts dirpagetextsdirpagesfilenames dirpagenamespagenamesfsext((sA/mnt/gmirror/ports/www/zope-zwiki/work/ZWiki/tools/zwikiimport.pys importFileΦs6$   #$3%  cCs$tiitii|ƒƒdSdS(s8Derive a suitable wiki page name from a filesystem path.iN(sosspathssplitextsbasename(spath((sA/mnt/gmirror/ports/www/zope-zwiki/work/ZWiki/tools/zwikiimport.pyspageNameFromPathόscCs}ti|tƒ}| otSnd|ijo+|iddƒ}|o|d}q]n|idjo|SntSdS(spGet the wiki page object indicated by a ZODB path. If it's a folder, return the first page. Otherwise None.sFoldersspecs ZWiki PageiN(sappsrestrictedTraversespathsNonesobjs meta_types objectValuesspages(spathsobjspages((sA/mnt/gmirror/ports/www/zope-zwiki/work/ZWiki/tools/zwikiimport.pys pageFromPathscCs/tƒtttdƒdƒtƒiƒdS(sMain procedure.is.N(s parseArgss importFiles pageFromPathsargssget_transactionscommit(((sA/mnt/gmirror/ports/www/zope-zwiki/work/ZWiki/tools/zwikiimport.pysmainss__main__()s__doc__sossresoptparses OptionParsers zExceptionss OFS.ImagesOFSsOFS.content_typessguess_content_types transactionsgetsget_transactions ImportErrorsenvironssyssstdinsstdoutsZPublisher.HTTPRequests HTTPRequestsZPublisher.HTTPResponses HTTPResponsesZPublisher.BaseRequestsRequestContainersNonesoptionssargss parseArgss makerequestsTruesdlogsvlogs bodyFromHtmls fixLinksInsdoPagesdoFiles importFilespageNameFromPaths pageFromPathsmains__name__(s fixLinksInsstdoutsRequestContainersdoFilespageNameFromPaths parseArgssdlogsres bodyFromHtmls HTTPResponsesmains OptionParsers HTTPRequestsOFSsget_transactions importFiles makerequestsdoPages pageFromPathsguess_content_typesstdinsvlogsenvironsos((sA/mnt/gmirror/ports/www/zope-zwiki/work/ZWiki/tools/zwikiimport.pys?*s:             # ' &