Giter Site home page Giter Site logo

Comments (14)

sdispater avatar sdispater commented on August 15, 2024 1

Yeah. We should deprecate one or the other.

I think the best approach here is just to remove Parser and move its methods to the loader class, since the name Parser is not exactly clear about what its purpose is.

from pendulum.

sdispater avatar sdispater commented on August 15, 2024

That's odd. What is the output of the following?

>>> import pytz
>>> import os
>>> import inspect

>>> os.path.dirname(inspect.getfile(pytz))

More importantly, does it return /usr/lib/python3.5/site-packages/pytz 

from pendulum.

mayfield avatar mayfield commented on August 15, 2024

/usr/lib/python3.5/site-packages/pytz

Which is there, it just doesn't have a zoneinfo dir.

from pendulum.

mayfield avatar mayfield commented on August 15, 2024

So in my install the zoneinfo files are shared with the system...

>>> import pytz
>>> pytz.open_resource('adsf')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.5/site-packages/pytz/__init__.py", line 90, in open_resource
    return open(filename, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '/usr/share/zoneinfo/adsf'

That open_resource function from pytz might be your huckleberry.

from pendulum.

sdispater avatar sdispater commented on August 15, 2024

Which version of pytz do you have?

from pendulum.

sdispater avatar sdispater commented on August 15, 2024

Here is the open_resource function from pytz:

def open_resource(name):
    """Open a resource from the zoneinfo subdir for reading.

    Uses the pkg_resources module if available and no standard file
    found at the calculated location.
    """
    name_parts = name.lstrip('/').split('/')
    for part in name_parts:
        if part == os.path.pardir or os.path.sep in part:
            raise ValueError('Bad path segment: %r' % part)
    filename = os.path.join(os.path.dirname(__file__),
                            'zoneinfo', *name_parts)
    if not os.path.exists(filename):
        # http://bugs.launchpad.net/bugs/383171 - we avoid using this
        # unless absolutely necessary to help when a broken version of
        # pkg_resources is installed.
        try:
            from pkg_resources import resource_stream
        except ImportError:
            resource_stream = None

        if resource_stream is not None:
            return resource_stream(__name__, 'zoneinfo/' + name)
    return open(filename, 'rb')

So, it seems that some pkg_resources version are broken and the zoneinfo directory is missing.

Pendulum needs to use the open_resource from pytz to fix the problem.

from pendulum.

mayfield avatar mayfield commented on August 15, 2024

re: version

>>> import pytz
>>> pytz.__version__
'2016.6.1'

from pendulum.

mayfield avatar mayfield commented on August 15, 2024

Yeah, how about a patch like this (untested)...

:; git diff pendulum/tz/loader.py | cat
diff --git a/pendulum/tz/loader.py b/pendulum/tz/loader.py
index 296854a..38bebff 100644
--- a/pendulum/tz/loader.py
+++ b/pendulum/tz/loader.py
@@ -15,16 +15,8 @@ class Loader(object):
     @classmethod
     def load(cls, name):
         name = decode(name)
-
-        name_parts = name.lstrip('/').split('/')
-
-        for part in name_parts:
-            if part == os.path.pardir or os.path.sep in part:
-                raise ValueError('Bad path segment: %r' % part)
-
-        filepath = os.path.join(cls.path, *name_parts)
-
-        if not os.path.exists(filepath):
+        try:
+            with pytz.open_resource(name) as f:
+                return Parser._parse(f)
+        except OSError:
             raise ValueError('Unknown timezone [{}]'.format(name))
-
-        return Parser.parse(filepath)

from pendulum.

mayfield avatar mayfield commented on August 15, 2024

I tested that and added a couple tests to validate the loader contract (ie. return ValueError when the timezone is not found instead of a FileNotFoundError (OSError for pre py3k). I can submit a PR on that if you like.

from pendulum.

sdispater avatar sdispater commented on August 15, 2024

Thanks.

For more simplicity, it's possible to just do the with open_resource() in the public Parser.parse() method.

class Parser(object):

    @classmethod
    def parse(cls, name):
        try:
            with open_resource(name) as fp:
                return cls._parse(fp)
        except OSError:
             raise ValueError('Unknown timezone [{}]'.format(name))

And the Loader to become:

class Loader(object):

    @classmethod
    def load(cls, name):
        name = decode(name)

        return Parser.parse(name)

It would remove a lot of the purpose of the Loader class (except for decoding the name) but I think it would be more logical. 

What do you think?

from pendulum.

mayfield avatar mayfield commented on August 15, 2024

Works for me. Is there any reason not to move the decode to Parser too and just deprecate Loader?

from pendulum.

sdispater avatar sdispater commented on August 15, 2024

Do you want me to make this change or are you making a pull request?

from pendulum.

mayfield avatar mayfield commented on August 15, 2024

I can submit a PR.

from pendulum.

sdispater avatar sdispater commented on August 15, 2024

Great! Thank you :-)

from pendulum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.