class Settings(XMLObject): """ Class for storing and retrieving ALL information about PyQLogger """ Dialogs = ItemNode('DialogSettings', optional=True) UI = ItemNode('UISettings') # GUI settings Accounts = ListNode('Account') # list of accounts AutoLogin = TextNode(optional=True) # which account to autologin Speller = ItemNode("SpellerSettings") # speller settings StyleSheet = TextNode(optional=True) # file with css DebugLevel = IntegerAttribute(default=10) # level of logging 10,20,30 def accountByName(self, name): """ searches the list of accouns, and returns one by it's .Name """ for a in self.Accounts: if a.Name == name: return a raise Exception("Inavlid Account name: " + name) def load(): """ static method to create new settings class from xml file """ fname = os.path.expanduser("~") + "/.pyqlogger/settings.xml" if os.path.exists(fname): try: return XMLObject.instanceFromXml(open(fname).read()) except Exception, e: print "Couldn't read settings. Either empty of borken! (%s)" % e import traceback traceback.print_exc() return Settings(UI=UISettings(), Speller=SpellerSettings())
class Option(XMLObject): Type = TextNode() # one of : 'Integer','String','Boolean','List', 'DoubleList' Name = TextNode() Value = RawNode(default='',optional=True) def getListOptionValue(self): tmp = self.Value.split(';') if len(tmp) > 2: sel = int(tmp[0]) return tmp[1:][sel] def getDoubleListOptionValue(self): if self.Value: hash = pickle.loads( str(self.Value) ) else: hash = {} return hash
class Post(XMLObject): """ Basic class for post in a blog """ ID = TextNode(optional=True ) #post's id (optional because we use it for drafts aswell) Title = TextNode() #post's title Content = RawNode() #body of the post Created = TextNode() #date of post's publication (or last update) Data = ItemNode('PostData', optional=True) def __str__(self): """ return string representation of the post """ return "ID: %s\nTitle: %s\nDate: %s\nBody:\n%s" % \ (self.ID, self.Title, self.Created, self.Content)
class PluginData(XMLObject): # Indication of plugin's status (0 - disable, 1 - enable) Enabled = IntegerAttribute(default=0) Options = ListNode('Option') Class = TextNode() def optionByName(self, name): for o in self.Options: if o.Name == name: return o
class Account(XMLObject): """ Base class for blog account providers """ Name = StringAttribute() # Display name Username = TextNode() # Login name Password = TextNode(optional=True) # Top Secret! Host = TextNode() # Account's host SelectedBlog = IntegerAttribute(default=0) # id of currently selected blog Service = StringAttribute() #What Blogging API Is used Blogs = ListNode('Blog') def serviceByName(self, name): assert name, "Name cannot be empty" for b in BlogServices: (s_module, s_class) = b.split('.') if name == s_class: return getattr(globals()[s_module], s_class) raise Exception("Invalid Blog Service name : " + name) def blogByName(self, name): """ searches the list of accouns, and returns one by it's .Name """ assert name, "Name cannot be empty" for a in self.Blogs: if a.Name == name: return a raise Exception("Invalid Blog name: " + name) def blogById(self, blogId): assert blogId, "Id cannot be empty" cnt = 0 for blog in self.Blogs: if blog.ID == blogId: return cnt cnt += 1 raise Exception("Invalid blog number %s !" % blogId) def init(self): if not hasattr(self, "inited"): svc = self.serviceByName(self.Service) self.BlogService = svc(self.Host, self.Username, self.Password) for blog in self.Blogs: blog.Service = self.BlogService self.inited = True def login(self): """ Perform login to server (if needed) """ if hasattr(self.BlogService, 'login'): return self.BlogService.login() return True def fetchBlogs(self): """ get the list of blogs for this account """ blogs = self.BlogService.getBlogs() # fetch blogs from server for blog in blogs: here = [bb for bb in self.Blogs if bb.Name == blog.Name] if not here: self.Blogs += [blog] def __len__(self): """ Return number of blogs in account """ return len(self.Blogs) def __getitem__(self, blogId): """ Get a blog in account by it's id """ return self.Blogs[self.blogById(blogId)]
class DialogSettings(XMLObject): UrlDialogTarget = TextNode(optional=True)
class SpellerSettings(XMLObject): Enabled = IntegerAttribute(default=0) Language = TextNode(default="en_US") Prefix = TextNode(default="/usr/bin")