Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typing for some methods #144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion odf/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def __init__(self, attributes=None, text=None, cdata=None, qname=None, qattribut
allowed_attrs = self.allowed_attributes()
if allowed_attrs is not None:
allowed_args = [ a[1].lower().replace('-','') for a in allowed_attrs]
self.attributes={}
self.attributes={} # type: dict[tuple[str, str], ...]
# Load the attributes from the 'attributes' argument
if attributes:
for attr, value in attributes.items():
Expand All @@ -391,6 +391,7 @@ def __init__(self, attributes=None, text=None, cdata=None, qname=None, qattribut
raise AttributeError( "Required attribute missing: %s in <%s>" % (r[1].lower().replace('-',''), self.tagName))

def get_knownns(self, prefix):
# type: (Element, str) -> str | None
""" Odfpy maintains a list of known namespaces. In some cases a prefix is used, and
we need to know which namespace it resolves to.
"""
Expand All @@ -410,6 +411,7 @@ def get_nsprefix(self, namespace):
return prefix

def allowed_attributes(self):
# type: (Element) -> tuple[tuple[str, str], ...]
return grammar.allowed_attributes.get(self.qname)

def _setOwnerDoc(self, element):
Expand All @@ -418,6 +420,7 @@ def _setOwnerDoc(self, element):
self._setOwnerDoc(child)

def addElement(self, element, check_grammar=True):
# type: (Element, Element, bool) -> None
""" adds an element to an Element

Element.addElement(Element)
Expand Down Expand Up @@ -450,6 +453,7 @@ def addCDATA(self, cdata, check_grammar=True):
self.appendChild(CDATASection(cdata))

def removeAttribute(self, attr, check_grammar=True):
# type: (Element, str | tuple[str, str], bool) -> None
""" Removes an attribute by name. """
allowed_attrs = self.allowed_attributes()
if allowed_attrs is None:
Expand All @@ -467,6 +471,7 @@ def removeAttribute(self, attr, check_grammar=True):
self.removeAttrNS(allowed_attrs[i][0], allowed_attrs[i][1])

def setAttribute(self, attr, value, check_grammar=True):
# type: (Element, str | tuple[str, str], Any, bool) -> None
""" Add an attribute to the element
This is sort of a convenience method. All attributes in ODF have
namespaces. The library knows what attributes are legal and then allows
Expand All @@ -493,6 +498,7 @@ def setAttribute(self, attr, value, check_grammar=True):
self.setAttrNS(allowed_attrs[i][0], allowed_attrs[i][1], value)

def setAttrNS(self, namespace, localpart, value):
# type: (Element, str, str, Any) -> None
""" Add an attribute to the element
In case you need to add an attribute the library doesn't know about
then you must provide the full qualified name
Expand All @@ -507,6 +513,7 @@ def setAttrNS(self, namespace, localpart, value):
self.attributes[(namespace, localpart)] = c.convert((namespace, localpart), value, self)

def getAttrNS(self, namespace, localpart):
# type: (Element, str, str) -> Any
"""
gets an attribute, given a namespace and a key
@param namespace a unicode string or a bytes: the namespace
Expand All @@ -529,9 +536,11 @@ def getAttrNS(self, namespace, localpart):
return result

def removeAttrNS(self, namespace, localpart):
# type: (Element, str, str) -> None
del self.attributes[(namespace, localpart)]

def getAttribute(self, attr):
# type: (str | tuple[str, str]) -> Any
""" Get an attribute value. The method knows which namespace the attribute is in
"""
allowed_attrs = self.allowed_attributes()
Expand Down Expand Up @@ -582,6 +591,7 @@ def toXml(self, level, f):
f.write(u'/>')

def _getElementsByObj(self, obj, accumulator):
# type: (Element, Element, list[Element]) -> list[Element]
if self.qname == obj.qname:
accumulator.append(self)
for e in self.childNodes:
Expand All @@ -590,11 +600,13 @@ def _getElementsByObj(self, obj, accumulator):
return accumulator

def getElementsByType(self, element):
# type: (Element, ...) -> list[Element]
""" Gets elements based on the type, which is function from text.py, draw.py etc. """
obj = element(check_grammar=False)
return self._getElementsByObj(obj,[])

def isInstanceOf(self, element):
# type: (Element, ...) -> bool
""" This is a check to see if the object is an instance of a type """
obj = element(check_grammar=False)
return self.qname == obj.qname
2 changes: 2 additions & 0 deletions odf/opendocument.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ def getMediaType(self):
return self.mimetype

def getStyleByName(self, name):
# type: (str) -> element.Element
"""
Finds a style object based on the name
@param name unicode string the name of style to search
Expand All @@ -770,6 +771,7 @@ def getStyleByName(self, name):
return result

def getElementsByType(self, elt):
# type: (...) -> list[element.Element]
"""
Gets elements based on the type, which is function from
text.py, draw.py etc.
Expand Down