Skip to content
This repository was archived by the owner on Jan 28, 2019. It is now read-only.

Conversation

anthrotype
Copy link
Member

The reason I haven't tagged ufoLib 3.0 yet is because I was working on allowing to use the built-in xml.etree module when lxml is not installed.
The reason is, as much as I love to add third-party dependencies (:grin) and avoid to re-invent the wheel, I believe it makes sense to keep ufoLib working in a simple, pure-python setup, like it was before.
Especially since we are also planning to eventually merge ufoLib into fonttools, where third-party dependencies are even more difficult to add.

this PR adds a ufoLib.etree module which exports the same API as the (built-in) xml.etree.ElementTree or lxml.etree, and uses the latter whenever it is available otherwise falls back on the built-in module.

In order to produce the same output (with control over the order of the attributes, etc.), I needed to subclass Element and ElementTree objects and basically adapt the serialization part of the built-in ElementTree library.

The current tests pass, which is good. Of course, it is a bit slower than lxml (havent' run benchmarks yet), but the idea is to have it as a reasonable default or fallback, and lxml being the opt-in extra for those who want faster output.

I would like to add some more tests before merging this.

works with both lxml and xml.etree backends

adds some missing things from built-in etree, such as the ability to
use an OrderedDict for attributes, support for pretty_print argument
to add indentation, etc.
one can always do `pytest -v`, or through `tox -- -v`
this is conventional among python packages to have an 'testing' extras_require
so developers can bootstrap a testing environment easily with something like:

pip install -e .[testing]

The pytest-cov is a pluging to integrate coverage.py with pytest.

pytest-randomly is to shuffle tests in a random order to check against inter-test
dependencies
…decov.io

and remove 3.5, we don't need to test that one any more now that there's 3.7
and skip_branch_with_pr, to avoid running CI twice both on the PR branch and
on merged master.
@codecov-io
Copy link

codecov-io commented Jul 14, 2018

Codecov Report

❗ No coverage uploaded for pull request base (master@9735cdc). Click here to learn what that means.
The diff coverage is 66.56%.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #161   +/-   ##
=========================================
  Coverage          ?   88.73%           
=========================================
  Files             ?       20           
  Lines             ?    10157           
  Branches          ?     1106           
=========================================
  Hits              ?     9013           
  Misses            ?      773           
  Partials          ?      371
Impacted Files Coverage Δ
Lib/ufoLib/glifLib.py 74.51% <100%> (ø)
Lib/ufoLib/plistlib.py 98.03% <100%> (ø)
Lib/ufoLib/test/test_plistlib.py 100% <100%> (ø)
Lib/ufoLib/etree.py 60.22% <60.22%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9735cdc...d4f10f1. Read the comment docs.

@miguelsousa
Copy link
Member

@anthrotype strings are still saved as data in layercontents.plist files. I think it's this line https://github.com/anthrotype/ufoLib/blob/optional-lxml/Lib/ufoLib/__init__.py#L1118

All of these string vs. data issues make me think that the tests have some glaring holes. What's your opinion?

@anthrotype
Copy link
Member Author

tests have some glaring holes

Yeah, that’s why I’m adding coverage support in this pr among other things.
I’m about to take a flight now, I’ll take a look at that layercontents issue you pointed to. Thanks Miguel for checking

looks like defcon LayerSet.newLayer does not ensure the 'name' argument is a unicode
string, so serializing the layercontents.plist with the new ufoLib.plistlib may
lead to the layer name being encoded as a <data> element instead of a <string>.

Thanks Miguel for noticing this.
@miguelsousa
Copy link
Member

42a3428 fixes layerName (the UI string) but not self.layerContents[layerName] (the folder name).

@anthrotype
Copy link
Member Author

Right.. will fix tomorrow

@anthrotype
Copy link
Member Author

42a3428 fixes layerName (the UI string) but not self.layerContents[layerName] (the folder name).

hm, I don't think it's possible for the self.layerContents values (the directory name) to not be unicode, because the directory name is generated by the ufoLib.filenames.userNameToFileName function, which always returns unicode: it uses unicode_literals, asserts that input is unicode, etc.
Also, the ufoLib.plistlib.load will always return unicode strings for plist <string> elements, never bytes. So I don't think it's possible to introduce a bytes string in that dictionary. It was different for the layerName, the key in that dictionary, because the writeLayerContents function accepts an optional layerOrder list of strings, and the user may unwittingly pass bytes instead of unicodes.

@miguelsousa did you actually manage to reproduce the directory value in layerContents being a bytes string instead of unicode string?

@anthrotype
Copy link
Member Author

btw @miguelsousa sorry I didn't make it in time for the pyup.io bot weekly update-all-the-repos.. Let's not rush this, and make sure we don't introduce any further regressions.

@anthrotype
Copy link
Member Author

ok i'm gonna merge and release 2.3.0. I don't think this deserves 3.0.0. We are not breaking the API, the lxml dependency is optional, though recommended for better speeds.

@anthrotype anthrotype merged commit 25c1060 into unified-font-object:master Jul 16, 2018
@anthrotype anthrotype deleted the optional-lxml branch July 16, 2018 12:59
@typemytype
Copy link
Collaborator

super, thanks!!

@benkiel
Copy link
Collaborator

benkiel commented Jul 16, 2018

Thanks! Does this then close #140?

@miguelsousa
Copy link
Member

did you actually manage to reproduce the directory value in layerContents being a bytes string instead of unicode string?

yes. I patched mutatorMath locally, updated ufoLib to 42a3428, and ran makeinstancesufo_test.py from the afdko repo.

Let's not rush this, and make sure we don't introduce any further regressions.

agree. No rush on our end.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants