pycupra 0.1.11__py3-2ndver-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
pycupra/utilities.py ADDED
@@ -0,0 +1,116 @@
1
+ from datetime import date, datetime
2
+ from base64 import b64encode
3
+ from string import ascii_letters as letters, digits
4
+ from sys import argv
5
+ from os import environ as env
6
+ from os.path import join, dirname, expanduser
7
+ from itertools import product
8
+ import json
9
+ import logging
10
+ import re
11
+
12
+ _LOGGER = logging.getLogger(__name__)
13
+
14
+
15
+ def read_config():
16
+ """Read config from file."""
17
+ for directory, filename in product(
18
+ [
19
+ dirname(argv[0]),
20
+ expanduser("~"),
21
+ env.get("XDG_CONFIG_HOME", join(expanduser("~"), ".config")),
22
+ ],
23
+ ["seat.conf", ".seat.conf"],
24
+ ):
25
+ try:
26
+ config = join(directory, filename)
27
+ _LOGGER.debug("checking for config file %s", config)
28
+ with open(config) as config:
29
+ return dict(
30
+ x.split(": ")
31
+ for x in config.read().strip().splitlines()
32
+ if not x.startswith("#")
33
+ )
34
+ except (IOError, OSError):
35
+ continue
36
+ return {}
37
+
38
+
39
+ def json_loads(s):
40
+ return json.loads(s, object_hook=obj_parser)
41
+
42
+
43
+ def obj_parser(obj):
44
+ """Parse datetime."""
45
+ for key, val in obj.items():
46
+ try:
47
+ obj[key] = datetime.strptime(val, "%Y-%m-%dT%H:%M:%S%z")
48
+ except (TypeError, ValueError):
49
+ pass
50
+ return obj
51
+
52
+
53
+ def find_path(src, path):
54
+ """Simple navigation of a hierarchical dict structure using XPATH-like syntax.
55
+
56
+ >>> find_path(dict(a=1), 'a')
57
+ 1
58
+
59
+ >>> find_path(dict(a=1), '')
60
+ {'a': 1}
61
+
62
+ >>> find_path(dict(a=None), 'a')
63
+
64
+
65
+ >>> find_path(dict(a=1), 'b')
66
+ Traceback (most recent call last):
67
+ ...
68
+ KeyError: 'b'
69
+
70
+ >>> find_path(dict(a=dict(b=1)), 'a.b')
71
+ 1
72
+
73
+ >>> find_path(dict(a=dict(b=1)), 'a')
74
+ {'b': 1}
75
+
76
+ >>> find_path(dict(a=dict(b=1)), 'a.c')
77
+ Traceback (most recent call last):
78
+ ...
79
+ KeyError: 'c'
80
+
81
+ """
82
+ if not path:
83
+ return src
84
+ if isinstance(path, str):
85
+ path = path.split(".")
86
+ return find_path(src[path[0]], path[1:])
87
+
88
+
89
+ def is_valid_path(src, path):
90
+ """
91
+ >>> is_valid_path(dict(a=1), 'a')
92
+ True
93
+
94
+ >>> is_valid_path(dict(a=1), '')
95
+ True
96
+
97
+ >>> is_valid_path(dict(a=1), None)
98
+ True
99
+
100
+ >>> is_valid_path(dict(a=1), 'b')
101
+ False
102
+ """
103
+ try:
104
+ find_path(src, path)
105
+ return True
106
+ except KeyError:
107
+ return False
108
+
109
+
110
+ def camel2slug(s):
111
+ """Convert camelCase to camel_case.
112
+
113
+ >>> camel2slug('fooBar')
114
+ 'foo_bar'
115
+ """
116
+ return re.sub("([A-Z])", "_\\1", s).lower().lstrip("_")