Пример #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
 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
 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
    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()