Comments (14)
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.
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.
/usr/lib/python3.5/site-packages/pytz
Which is there, it just doesn't have a zoneinfo dir.
from pendulum.
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.
Which version of pytz
do you have?
from pendulum.
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.
re: version
>>> import pytz
>>> pytz.__version__
'2016.6.1'
from pendulum.
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.
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.
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.
Works for me. Is there any reason not to move the decode to Parser too and just deprecate Loader?
from pendulum.
Do you want me to make this change or are you making a pull request?
from pendulum.
I can submit a PR.
from pendulum.
Great! Thank you :-)
from pendulum.
Related Issues (20)
- to_iso8601_string
- instance() fails for datetimes with pytz.FixedOffsets
- "import pendulum" causes unwanted import of pytest (presence of pytest in sys.modules often used to control Python behaviour) HOT 2
- MMM is not case-insensitive like Java
- If you have the /etc/timezone file set to a timezone string, pendulum.UTC has that timezone's utcoffset instead of None HOT 1
- in_words() exclude week
- timedelta additions to pendulum.DateTime HOT 1
- extreme slow down in `in_tz` after upgrading from 2.1.2 to 3.0.0 HOT 3
- Day of week constants
- pendulum.instance & dateutil.tz compatibility
- Localized Format LT inconsistent with leading zeroes
- Default string representation is not iso anymore HOT 1
- Add DateTime to typing for diff()
- Cannot install version 2.x via pip because of dead link for get-poetry.py in build-wheels.sh HOT 1
- IsADirectoryError in datetime.py
- DeprecationWarning about datetime.utcfromtimestamp in Python-3.12
- Flexget install Raspberry PI ! HOT 3
- Bugs in `Duration` edge cases
- Remove deprecated datetime.datetime.utcfromtimestamp HOT 1
- v2.8.1 broke Pendulum math and timezone awareness HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pendulum.