Example #1
0
 def save(self, attributes=[]):
     target = {}
     if attributes:
         for key in attributes: target[key] = self[key]
     else: target = cpy(self)
     targetfile = getdatadir() + os.sep + "containers" + os.sep + str(self.createtime) + "_" + stripname(self.origin)
     p = Persist(targetfile)
     p.data = getjson().dumps(target)
     p.save()
Example #2
0
def savealiases(ddir=None):
    """ return global aliases. """
    global aliases
    if aliases:
        logging.warn("saving aliases")
        from jsb.lib.persist import Persist
        from jsb.utils.lazydict import LazyDict
        d = ddir or getdatadir()
        p = Persist(d + os.sep + "run" + os.sep + "aliases")
        p.data = aliases
        p.save()
    return aliases
Example #3
0
 def save(self, attributes=[]):
     target = {}
     if attributes:
         for key in attributes:
             target[key] = self[key]
     else:
         target = cpy(self)
     targetfile = getdatadir() + os.sep + "containers" + os.sep + str(
         self.createtime) + "_" + stripname(self.origin)
     p = Persist(targetfile)
     p.data = getjson().dumps(target)
     p.save()
Example #4
0
def savealiases(ddir=None):
    """ return global aliases. """
    global aliases
    if aliases:
        logging.warn("saving aliases")
        from jsb.lib.persist import Persist
        from jsb.utils.lazydict import LazyDict

        d = ddir or getdatadir()
        p = Persist(d + os.sep + "run" + os.sep + "aliases")
        p.data = aliases
        p.save()
    return aliases
Example #5
0
def convertpickle(src, target):
    import gozerbot.compat.persist
    p = gozerbot.compat.persist.Persist(src)    
    if p and p.data:
        pers = Persist(target)
        if not pers.data:
            pers.data = {}
        pers.data.update(p.data)
        try:
            pers.save()
        except TypeError:
            pers2 = Persist(target)
            if not pers2.data:
                pers2.data = {}
            for item, value in p.data.iteritems():
                pers2.data[jsonstring(item)] = value
            pers2.save()
Example #6
0
 def save(self):
     """ save the hubbub items data. """
     Persist.save(self)
     self.itemslists.save()
     self.markup.save()
Example #7
0
class Config(LazyDict):

    """ 
        config class is a dict containing json strings. is writable to file 
        and human editable.

    """

    def __init__(self, filename, verbose=False, input={}, ddir=None, nolog=False, *args, **kw):
        assert filename
        LazyDict.__init__(self, input, *args, **kw)
        a = "%s%s" % (os.sep, os.sep)
        filename = filename.replace(a, os.sep)
        self.origname = filename
        self.origdir = ddir or getdatadir()
        self.setcfile(ddir or getdatadir(), filename)
        self.jsondb = None
        if not self._comments: self._comments = {}
        logging.info("fromfile - %s from %s" % (self.origname, whichmodule(2)))
        self.fromfile(self.cfile)
        self.origdir = ddir or getdatadir()
        self.setcfile(ddir or getdatadir(), filename)
        self.init()
        if self.owner: logging.info("owner is %s" % self.owner)
        if not self.has_key("uuid"): self.setuuid()
        if not self.has_key("cfile"): self.cfile = self.setcfile(self.origdir, self.origname) 
        assert self.cfile
        if not self.origdir in self.cfile: raise WrongFileName("%s not in %s" % (self.origdir, self.cfile))

    def setcfile(self, ddir, filename):
        self.filename = filename or 'mainconfig'
        self.datadir = ddir or getdatadir()
        self.dir = self.datadir + os.sep + 'config'
        self.cfile = self.dir + os.sep + filename

    def setuuid(self, save=True):
        logging.debug("setting uuid")
        self.uuid = str(uuid.uuid4())
        if save: self.save()

    def __deepcopy__(self, a):
        """ accessor function. """
        cfg = Config(self.filename, input=self, nolog=True)
        return cfg

    def __getitem__(self, item):
        """ accessor function. """
        if not self.has_key(item): return None
        else: return LazyDict.__getitem__(self, item)

    def merge(self, cfg):
        """ merge in another cfg. """
        self.update(cfg)

    def set(self, item, value):
        """ set item to value. """
        LazyDict.__setitem__(self, item, value)

    def fromdb(self):
        """ read config from database. """
        from jsb.lib.persist import Persist
        tmp = Persist(self.cfile)
        logging.debug("fromdb - %s - %s" % (self.cfile, tmp.data.tojson()))
        self.update(tmp.data)

    def todb(self):
        """ save config to database. """
        cp = dict(self)
        del cp['jsondb']
        if not self.jsondb:
            from jsb.lib.persist import Persist
            self.jsondb = Persist(self.cfile)
        self.jsondb.data = cp
        self.jsondb.save()

    def fromfile(self, filename=None):
        """ read config object from filename. """
        curline = ""
        fname = filename or self.cfile
        if not fname: raise Exception(" %s - %s" % (self.cfile, self.dump()))
        if not os.path.exists(fname): logging.warn("config file %s doesn't exist yet" % fname) ; return False 
        comment = ""
        for line in open(fname, 'r'):
            curline = line
            curline = curline.strip()
            if curline == "": continue
            if curline.startswith('#'): comment = curline; continue
            if True:
                try:
                    key, value = curline.split('=', 1)
                    kkey = key.strip()
                    self[kkey] = json.loads(unicode(value.strip()))
                    if comment: self._comments[kkey] = comment 
                    comment = ""
                except ValueError: logging.error("skipping line - unable to parse: %s" % line)
        #self.cfile = fname
        return

    def tofile(self, filename=None, stdout=False):
        """ save config object to file. """
        if not filename: filename = self.cfile
        if not filename: raise Exception("no cfile found  - %s" % whichmodule(3))
        logging.warn("saving %s" %  self.origname)
        if filename.startswith(os.sep): d = [os.sep,]
        else: d = []
        for p in filename.split(os.sep)[:-1]:
            if not p: continue
            d.append(p)
            ddir = os.sep.join(d)
            if not os.path.isdir(ddir):
                logging.debug("persist - creating %s dir" % ddir)
                try: os.mkdir(ddir)
                except OSError, ex:
                    logging.error("persist - not saving - failed to make %s - %s" % (ddir, str(ex)))
                    return
        written = []
        curitem = None
        later = []
        try:
            if stdout: configtmp = sys.stdout
            else: configtmp = open(filename + '.tmp', 'w')
            configtmp.write('# ===========================================================\n#\n')
            configtmp.write("# JSONBOT CONFIGURATION FILE - %s\n" % filename)
            configtmp.write("#\n")
            configtmp.write('# last changed on %s\n#\n' % time.ctime(time.time()))
            configtmp.write("# This file contains configration data for the JSONBOT.\n")
            configtmp.write('# Variables are defined by "name = json value" pairs.\n')
            configtmp.write('# Make sure to use " in strings.\n#\n')
            configtmp.write('# The bot can edit this file!.\n#\n')
            configtmp.write('# ===========================================================\n\n')
            teller = 0
            keywords = self.keys()
            keywords.sort()
            for keyword in keywords:
                value = self[keyword]
                if keyword in written: continue
                if keyword in ['origdir', 'origname', 'issaved', 'blacklist', 'whitelist', 'followlist', 'uuid', 'whitelist', 'datadir', 'name', 'createdfrom', 'cfile', 'filename', 'dir', 'isdb']: later.append(keyword) ; continue
                if keyword == 'jsondb': continue
                if keyword == 'optionslist': continue
                if keyword == 'gatekeeper': continue
                if keyword == "_comments": continue
                if self._comments and self._comments.has_key(keyword):
                    configtmp.write(self._comments[keyword] + u"\n")
                curitem = keyword
                try: configtmp.write('%s = %s\n' % (keyword, json.dumps(value)))
                except TypeError: logging.error("%s - can't serialize %s" % (filename, keyword)) ; continue
                teller += 1
                #configtmp.write("\n")
            configtmp.write('\n\n# ============================================================\n#\n')
            configtmp.write("# bot generated stuff.\n#\n")
            configtmp.write('# ============================================================\n\n')
            for keyword in later:
                if self._comments and self._comments.has_key(keyword):
                    configtmp.write(self._comments[keyword] + u"\n")
                curitem = keyword
                value = self[keyword]
                try: configtmp.write(keyword + " = " + json.dumps(value) + "\n")
                except TypeError: logging.error("%s - can't serialize %s" % (filename, keyword)) ; continue
                teller += 1
                #configtmp.write("\n")
            if not "mainconfig" in filename and self._comments:
                try:
                    configtmp.write('\n\n# ============================================================\n#\n')
                    configtmp.write("# possible other config variables.\n#\n")
                    configtmp.write('# ============================================================\n\n')
                    items = self._comments.keys()
                    keys = self.keys()
                    do = []
                    for var in items:
                        if var not in keys: do.append(var)
                    do.sort()
                    for var in do:
                         configtmp.write(u"# %s -=- %s\n" % (var, self._comments[var]))
                    configtmp.write("\n\n")
                except Exception, ex: handle_exception()
            else: configtmp.write("\n\n# jsonbot can run multiple bots at once. see %s/config/fleet for their configurations.\n\n" % self.origdir)
            if not stdout: 
                configtmp.close()
                os.rename(filename + '.tmp', filename)
            return teller

        except Exception, ex:
            handle_exception()
            logging.error("ERROR WRITING %s CONFIG FILE: %s .. %s" % (self.cfile, str(ex), curitem))
Example #8
0
 def save(self):
     Persist.save(self)
Example #9
0
 def save(self):
     Persist.save(self)