1+ """
2+ Copyright 2011 Stonepaw & Helmic
3+
4+ This file is part of Webcomic Helper.
5+
6+ Webcomic Helper is free software: you can redistribute it and/or modify
7+ it under the terms of the GNU General Public License as published by
8+ the Free Software Foundation, either version 3 of the License, or
9+ (at your option) any later version.
10+
11+ Webcomic Helper is distributed in the hope that it will be useful,
12+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+ GNU General Public License for more details.
15+
16+ You should have received a copy of the GNU General Public License
17+ along with Webcomic Helper. If not, see <http://www.gnu.org/licenses/>.
18+ """
19+
20+ import clr
21+
22+ import System
23+
24+ clr .AddReference ("System.Xml" )
25+ clr .AddReference ("System.Windows.Forms" )
26+
27+ from System .Xml import XmlWriter , XmlWriterSettings
28+
29+ from System .Windows .Forms import MessageBox , MessageBoxButtons , MessageBoxIcon
30+
31+ from System .IO import FileInfo , Path
32+
33+ SCIRPT_DIRECTORY = FileInfo (__file__ ).DirectoryName
34+
35+ REGEX_FILE = Path .Combine (SCIRPT_DIRECTORY , "regex.dat" )
36+
37+ ICON = Path .Combine (SCIRPT_DIRECTORY , "webcomichelper.ico" )
38+
39+ HEADER_IMAGE = Path .Combine (SCIRPT_DIRECTORY , "Webcomic Helper.png" )
40+
41+
42+ class WebComicHelperResult (object ):
43+
44+ def __init__ (self , result , imageregex = None , linkregex = None ):
45+
46+ self ._result = result
47+ self ._image_regex = imageregex
48+ self ._link_regex = linkregex
49+
50+
51+ class WebComicHelperResultEnum (object ):
52+ NoImages = 1
53+ NoLinks = 2
54+ Site = 3
55+ Success = 4
56+
57+
58+ class LinkRegex (object ):
59+
60+ def __init__ (self , regex ):
61+ self ._regex = regex
62+ self ._matches = 0
63+
64+
65+ class ImageRegex (object ):
66+
67+ def __init__ (self , regex ):
68+ self ._regex = regex
69+ self ._matches = 0
70+
71+
72+ class SiteRegex (object ):
73+
74+ def __init__ (self , imageregex , linkregex , domain ):
75+ self ._image_regex = imageregex
76+ self ._link_regex = linkregex
77+ self ._domain = domain
78+
79+
80+ class NextPageLinkFormResult (object ):
81+ def __init__ (self , text , isimage ):
82+ self .is_image = isimage
83+ self .text = text
84+
85+
86+ class WebComic (object ):
87+
88+ def __init__ (self , info , starturl , helper_result ):
89+ """Info should be a dict of all the info"""
90+ self ._info = info
91+ self ._start_url = starturl
92+ self ._image_regex = helper_result ._image_regex
93+ self ._link_regex = helper_result ._link_regex
94+
95+ def SaveToXml (self , filepath ):
96+ """Saves the cbw file to the filepath
97+ filepath should be the complete path to a cbw file
98+ """
99+ xsettings = XmlWriterSettings ()
100+ xsettings .Indent = True
101+ try :
102+ xmlwriter = XmlWriter .Create (filepath , xsettings )
103+ # \\TODO: Do something
104+ except (System .UnauthorizedAccessException , System .IO .IOException ), ex :
105+ MessageBox .Show ("An error occured trying to create the cbw file. The error was:\n \n " + ex .Message + "\n \n Try again with a different file path" ,
106+ "Could not create cbw" , MessageBoxButtons .OK , MessageBoxIcon .Error )
107+ return False
108+
109+ with xmlwriter :
110+ xmlwriter .WriteStartElement ("WebComic" )
111+ self ._write_info (xmlwriter )
112+ self ._write_regex (xmlwriter )
113+ xmlwriter .WriteEndElement ()
114+
115+ return True
116+
117+ def _write_info (self , xmlwriter ):
118+ """Writes the info portion of the cbw using the passed xmlwriter"""
119+ xmlwriter .WriteStartElement ("Info" )
120+ for item in self ._info :
121+ xmlwriter .WriteElementString (item , self ._info [item ])
122+ xmlwriter .WriteEndElement ()
123+
124+
125+ def _write_regex (self , xmlwriter ):
126+ """Writes the regex portion of the cbw using the passed xmlwriter"""
127+
128+ xmlwriter .WriteStartElement ("Images" )
129+ xmlwriter .WriteStartElement ("Image" )
130+ xmlwriter .WriteAttributeString ("Url" , "?" + self ._start_url )
131+ xmlwriter .WriteStartElement ("Parts" )
132+ xmlwriter .WriteElementString ("Part" , self ._image_regex )
133+ xmlwriter .WriteElementString ("Part" , self ._link_regex )
134+ xmlwriter .WriteEndElement ()
135+ xmlwriter .WriteEndElement ()
136+ xmlwriter .WriteEndElement ()
0 commit comments