Beispiel #1
0
 def get_id(self):
     ftitle = os.path.join(self.docdir, 'title')
     fid = os.path.join(self.docdir, 'tile')
     if os.path.exists(ftitle):
         title = utils.read_file(ftitle).split("\n")[0].strip()
         if title:
             return slugify(title)
     elif os.path.exists(fid):
         docid = utils.read_file(idfile).split("\n")[0].strip()
         if docid:
             return docid
     return os.path.split(self.docdir)[1]
Beispiel #2
0
 def get_id(self):
     ftitle = os.path.join(self.docdir, 'title')
     fid = os.path.join(self.docdir, 'tile')
     if os.path.exists(ftitle):
         title = utils.read_file(ftitle).split("\n")[0].strip()
         if title:
             return slugify(title)
     elif os.path.exists(fid):
         docid = utils.read_file(idfile).split("\n")[0].strip()
         if docid:
             return docid
     return os.path.split(self.docdir)[1]
Beispiel #3
0
 def rjson(mo):
     if mo.group(2).startswith('_attachments'): 
         # someone  want to include from attachments
         path = os.path.join(app_dir, mo.group(2).strip())
         filenum = 0
         for filename in glob.iglob(path):
             logger.debug("process json macro: %s" % filename)
             library = ''
             try:
                 if filename.endswith('.json'):
                     library = read_json(filename)
                 else:
                     library = read_file(filename)
             except IOError, e:
                 raise MacroError(str(e))
             filenum += 1
             current_file = filename.split(app_dir)[1]
             fields = current_file.split('/')
             count = len(fields)
             include_to = included
             for i, field in enumerate(fields):
                 if i+1 < count:
                     include_to[field] = {}
                     include_to = include_to[field]
                 else:
                     include_to[field] = library
         if not filenum:
             raise MacroError(
             "Processing code: No file matching '%s'" % mo.group(2))
Beispiel #4
0
 def rjson(mo):
     if mo.group(2).startswith('_attachments'): 
         # someone  want to include from attachments
        path = os.path.join(app_dir, mo.group(2).strip())
        filenum = 0
        for filename in glob.iglob(path):
            library = ''
            try:
                if filename.endswith('.json'):
                    library = read_json(filename)
                else:
                    library = read_file(filename)
            except IOError, e:
                print >>sys.stderr, e
                sys.exit(1)
            filenum += 1
            current_file = filename.split(app_dir)[1]
            fields = current_file.split('/')
            count = len(fields)
            include_to = included
            for i, field in enumerate(fields):
                if i+1 < count:
                    include_to[field] = {}
                    include_to = include_to[field]
                else:
                    include_to[field] = library
        if not filenum:
            print >>sys.stderr, "Processing code: No file matching '%s'" % mo.group(2)
            sys.exit(-1)
Beispiel #5
0
 def rjson(mo):
     if mo.group(2).startswith('_attachments'):
         # someone  want to include from attachments
         path = os.path.join(app_dir, mo.group(2).strip())
         filenum = 0
         for filename in glob.iglob(path):
             library = ''
             try:
                 if filename.endswith('.json'):
                     library = read_json(filename)
                 else:
                     library = read_file(filename)
             except IOError, e:
                 print >> sys.stderr, e
                 sys.exit(1)
             filenum += 1
             current_file = filename.split(app_dir)[1]
             fields = current_file.split('/')
             count = len(fields)
             include_to = included
             for i, field in enumerate(fields):
                 if i + 1 < count:
                     include_to[field] = {}
                     include_to = include_to[field]
                 else:
                     include_to[field] = library
         if not filenum:
             print >> sys.stderr, "Processing code: No file matching '%s'" % mo.group(
                 2)
             sys.exit(-1)
Beispiel #6
0
 def get_id(self):
     """
     if there is an _id file, docid is extracted from it,
     else we take the current folder name.
     """
     idfile = os.path.join(self.docdir, '_id')
     if os.path.exists(idfile):
         docid = utils.read_file(idfile).split("\n")[0].strip()
         if docid: return docid
     if self.is_ddoc:
         return "_design/%s" % os.path.split(self.docdir)[1]
     else:
         return os.path.split(self.docdir)[1]
Beispiel #7
0
 def rreq(mo):
     # just read the file and return it
     path = os.path.join(app_dir, mo.group(2).strip())
     library = ''
     filenum = 0
     for filename in glob.iglob(path):            
         if verbose>=2:
            print "process code macro: %s" % filename
         try:
            library += read_file(filename)
         except IOError, e:
            print >>sys.stderr, e
            sys.exit(-1)
         filenum += 1
Beispiel #8
0
 def rreq(mo):
     # just read the file and return it
     path = os.path.join(app_dir, mo.group(2).strip())
     library = ''
     filenum = 0
     for filename in glob.iglob(path):
         if verbose >= 2:
             print "process code macro: %s" % filename
         try:
             library += read_file(filename)
         except IOError, e:
             print >> sys.stderr, e
             sys.exit(-1)
         filenum += 1
Beispiel #9
0
 def rreq(mo):
     # just read the file and return it
     path = os.path.join(app_dir, mo.group(2).strip())
     library = ''
     filenum = 0
     for filename in glob.iglob(path):            
         logger.debug("process code macro: %s" % filename)
         try:
             cnt = read_file(filename)
             if cnt.find("!code") >= 0:
                 cnt = run_code_macros(cnt, app_dir)
             library += cnt
         except IOError, e:
             raise MacroError(str(e))
         filenum += 1
Beispiel #10
0
    def dir_to_fields(self, current_dir='', depth=0,
                manifest=[]):
        """ process a directory and get all members """        
        
        fields={}
        if not current_dir:
            current_dir = self.docdir
        for name in os.listdir(current_dir):
            current_path = os.path.join(current_dir, name)
            rel_path = _replace_backslash(utils.relpath(current_path, self.docdir))
            if name.startswith("."):
                continue
            elif self.check_ignore(name):
                continue
            elif depth == 0 and name.startswith('_'):
                # files starting with "_" are always "special"
                continue
            elif name == '_attachments':
                continue
            elif depth == 0 and (name == 'couchapp' or name == 'couchapp.json'):
                # we are in app_meta
                if name == "couchapp":
                    manifest.append('%s/' % rel_path)
                    content = self.dir_to_fields(current_path,
                        depth=depth+1, manifest=manifest)
                else:
                    manifest.append(rel_path)
                    content = utils.read_json(current_path)
                    if not isinstance(content, dict):
                        content = { "meta": content }
                if 'signatures' in content:
                    del content['signatures']

                if 'manifest' in content:
                    del content['manifest']

                if 'objects' in content:
                    del content['objects']
                
                if 'length' in content:
                    del content['length']

                if 'couchapp' in fields:
                    fields['couchapp'].update(content)
                else:
                    fields['couchapp'] = content
            elif os.path.isdir(current_path):
                manifest.append('%s/' % rel_path)
                fields[name] = self.dir_to_fields(current_path,
                        depth=depth+1, manifest=manifest)
            else:
                logger.debug("push %s" % rel_path)
                  
                content = ''  
                if name.endswith('.json'):
                    try:
                        content = utils.read_json(current_path)
                    except ValueError:
                        logger.error("Json invalid in %s" % current_path)           
                else:
                    try:
                        content = utils.read_file(current_path).strip()
                    except UnicodeDecodeError:
                        logger.warning("%s isn't encoded in utf8" % current_path)
                        content = utils.read_file(current_path, utf8=False)
                        try:
                            content.encode('utf-8')
                        except UnicodeError:
                            logger.warning(
                            "plan B didn't work, %s is a binary" % current_path)
                            logger.warning("use plan C: encode to base64")   
                            content = "base64-encoded;%s" % base64.b64encode(
                                                                        content)

                
                # remove extension
                name, ext = os.path.splitext(name)
                if name in fields:
                    logger.warning(
        "%(name)s is already in properties. Can't add (%(fqn)s)" % {
                            "name": name, "fqn": rel_path })
                else:
                    manifest.append(rel_path)
                    fields[name] = content
        return fields