예제 #1
0
 def __init__(self, fn=None, text=None, encoding='UTF-8', **args):
     File.__init__(self, fn=fn, encoding=encoding, **args)
     if text is not None:
         self.text = text
     elif fn is not None and os.path.exists(fn):
         self.text = String(self.read().decode(encoding))
     else:
         self.text = String("")
예제 #2
0
파일: text.py 프로젝트: BlackEarth/bl
 def __init__(self, fn=None, text=None, encoding='UTF-8', **args):
     File.__init__(self, fn=fn, encoding=encoding, **args)
     if text is not None:
         self.text = text
     elif fn is not None and os.path.exists(fn):
         self.text = String(self.read().decode(encoding))
     else:
         self.text = String("")
예제 #3
0
파일: css.py 프로젝트: BlackEarth/bf
 def __init__(self, fn=None, styles=None, text=None, encoding='UTF-8', **args):
     File.__init__(self, fn=fn, encoding=encoding, **args)
     if styles is not None:
         self.styles = styles
     elif text is not None:
         self.styles = Styles.from_css(text)
     elif fn is not None and os.path.exists(fn):
         self.styles = Styles.from_css(open(fn, 'rb').read().decode(encoding))
     else:
         self.styles = Styles()
예제 #4
0
파일: xml.py 프로젝트: BlackEarth/bxml
    def __init__(
        self,
        fn=None,
        root=None,
        tree=None,
        parser=None,
        encoding='UTF-8',
        schemas=None,
        NS=None,
        **args
    ):

        # parent init of course
        File.__init__(self, fn=fn, root=root, parser=parser, schemas=schemas, **args)

        # assign the current class's values to self, because XML's values might have been overridden in the child class.
        self.ROOT_TAG = self.__class__.ROOT_TAG
        self.DEFAULT_NS = self.__class__.DEFAULT_NS
        if NS is not None:
            self.NS = NS
        else:
            self.NS = self.__class__.NS

        # set up the root element
        if root is None and tree is not None:
            self.root = self.tree.getroot()
        elif isinstance(root, str) or isinstance(root, bytes):
            self.root = XML.fromstring(root, parser=parser)
        elif isinstance(root, dict):
            self.root = self.from_dict(root)
        elif root is not None:
            self.root = root
        elif type(fn) in [str, bytes] and os.path.isfile(fn):  # read from fn
            tree = etree.parse(fn, parser=parser)
            self.root = tree.getroot()
            self.info = self.get_info(tree=tree)
        elif self.ROOT_TAG is not None:
            from .builder import Builder

            B = Builder(default=self.DEFAULT_NS, **self.NS)
            tag = self.tag_name(self.ROOT_TAG)
            tagns = self.tag_namespace(self.ROOT_TAG)  # None if no namespace
            if tagns is not None:
                nstag = list(self.NS.keys())[list(self.NS.values()).index(tagns)]
            else:
                nstag = '_'  # this is the "default" nstag for the Builder
            self.root = B[nstag](tag)
        else:
            self.root = etree.Element(
                String(self.__class__.__name__).identifier(camelsplit=True).lower(), nsmap=self.NS
            )

        # set up document info (based on tree.docinfo)
        if self.info is None:
            self.info = self.get_info(tree=tree)
예제 #5
0
 def __init__(self,
              fn=None,
              styles=None,
              text=None,
              encoding='UTF-8',
              **args):
     File.__init__(self, fn=fn, encoding=encoding, **args)
     if styles is not None:
         self.styles = styles
     elif text is not None:
         self.styles = Styles.from_css(text)
     elif fn is not None and os.path.exists(fn):
         self.styles = Styles.from_css(
             open(fn, 'rb').read().decode(encoding))
     else:
         self.styles = Styles()
예제 #6
0
 def convert(self, outfn=None, **params):
     if outfn is not None and File(fn=outfn).fn != self.fn:
         self.write(fn=outfn)
     else:
         outfn = self.fn
     res = Image(fn=outfn).mogrify(**params)
     return outfn + ': ' + res
예제 #7
0
파일: xml.py 프로젝트: BlackEarth/bxml
 def write(
     self,
     fn=None,
     root=None,
     encoding='UTF-8',
     doctype=None,
     canonicalized=False,
     xml_declaration=True,
     pretty_print=True,
     with_comments=True,
 ):
     data = self.tobytes(
         root=root or self.root,
         xml_declaration=xml_declaration,
         pretty_print=pretty_print,
         encoding=encoding or self.info.encoding,
         doctype=doctype or self.info.doctype,
         canonicalized=canonicalized,
         with_comments=with_comments,
     )
     if canonicalized == True and xml_declaration == True:  # add the xml declaration
         data = ("<?xml version='1.0' encoding='%s'?>\n" % encoding).encode(encoding) + data
     File.write(self, fn=fn or self.fn, data=data)
예제 #8
0
 def insert(self,
            text=None,
            data=None,
            prefix="",
            suffix="",
            ext='.txt',
            duplicate=False):
     """insert the given queue entry into the queue.
     text    = the text of the queue entry (REQUIRED)
     prefix  = a prefix for each queue entry filename (default '')
     suffix  = a suffix for each queue entry filename (before extension, default '')
     ext     = the extension of the queue entry filename (default '.json') 
     """
     # only insert the entry if there is no other entry containing the same data
     # -- no need to duplicate an existing entry.
     if duplicate == False:
         qfns = self.list()
         for qfn in qfns:
             try:
                 with open(qfn, 'rb') as f:
                     qft = f.read().decode('utf-8')
                     if qft == text:
                         return qfn
             except:
                 # queue managed to get to the file before we read it -- no need to keep reading,
                 # since they are processed in the same order by self.list()
                 break
     qfn = os.path.join(
         self.path,
         "%s%s-%.5f%s%s" % (prefix, time.strftime("%Y%m%d.%H%M%S"),
                            random.random(), suffix, ext))
     if text is not None:
         qf = Text(fn=qfn, text=text)
     elif data is not None:
         qf = File(fn=qfn, data=data)
     qf.write()
     return qfn
예제 #9
0
파일: text.py 프로젝트: BlackEarth/bl
 def write(self, fn=None, text=None, encoding='UTF-8', **args):
     try:
         data = (text or self.text or '').encode(encoding)
     except:
         data = (text or self.text or '').encode()
     File.write(self, fn=fn, data=data, **args)
예제 #10
0
 def write(self, fn=None, text=None, encoding='UTF-8', **args):
     try:
         data = (text or self.text or '').encode(encoding)
     except:
         data = (text or self.text or '').encode()
     File.write(self, fn=fn, data=data, **args)
예제 #11
0
 def write(self, fn=None, encoding='UTF-8', **args):
     text = self.render_styles()
     File.write(self, fn=fn, data=text.encode(encoding))
예제 #12
0
    def gswrite(self,
                fn=None,
                device='jpeg',
                res=600,
                alpha=4,
                quality=90,
                gs=None):
        "use ghostscript to create output file(s) from the PDF"
        gs = (gs or self.gs or os.environ.get('gs') or 'gs')
        # count the number of pages
        if fn is None:
            fn = os.path.splitext(self.fn)[0] + DEVICE_EXTENSIONS[device]
        fn = File(fn=fn).fn  # normalize path
        if not os.path.exists(os.path.dirname(fn)):
            os.makedirs(os.path.dirname(fn))
        log.debug("PDF.gswrite():\n\tself.fn = %s\n\tout fn = %s" %
                  (self.fn, fn))
        if os.path.splitext(self.fn)[-1].lower() == '.pdf':
            cmd = [
                gs, '-q', '-dNODISPLAY', '-c',
                "(%s) (r) file runpdfbegin pdfpagecount = quit" % self.fn
            ]
            log.debug(cmd)
            out = subprocess.check_output(cmd).decode('utf-8').strip()
            if out == '': out = '1'
            pages = int(out)
            log.debug("%d page(s)" % pages)
        else:
            pages = 1
        if pages > 1:
            # add a counter to the filename, which tells gs to create a file for every page in the input
            fb, ext = os.path.splitext(fn)
            n = len(re.split('.', str(pages))) - 1
            counter = "-%%0%dd" % n
            fn = fb + counter + ext

            # remove any existing output filenames that match the pattern
            for existingfn in glob(fb + '*' + ext):
                log.debug("REMOVING %s" % existingfn)
                os.remove(existingfn)

        callargs = [
            gs, '-q', '-dSAFER', '-dBATCH', '-dNOPAUSE', '-dUseCropBox',
            '-sDEVICE=%s' % device,
            '-r%d' % res
        ]
        if device == 'jpeg':
            callargs += ['-dJPEGQ=%d' % quality]
        if 'png' in device or 'jpeg' in device or 'tiff' in device:
            callargs += [
                '-dTextAlphaBits=%d' % alpha,
                '-dGraphicsAlphaBits=%d' % alpha
            ]
        callargs += ['-sOutputFile=%s' % fn, self.fn]
        try:
            log.debug(callargs)
            subprocess.check_output(callargs)
        except subprocess.CalledProcessError as e:
            log.error(callargs)
            log.error(str(e.output, 'utf-8'))
        fns = sorted(glob(re.sub(r'%\d+d', '*', fn)))
        log.debug('\n\t' + '\n\t'.join(fns))
        return fns
예제 #13
0
파일: css.py 프로젝트: BlackEarth/bf
 def write(self, fn=None, encoding='UTF-8', **args):
     text = self.render_styles()
     File.write(self, fn=fn, data=text.encode(encoding))