Skip to content

Commit 90ff9ff

Browse files
config changes
1 parent d1e1e9a commit 90ff9ff

File tree

5 files changed

+114
-50
lines changed

5 files changed

+114
-50
lines changed

contentstack/config.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,57 @@
2222
* SOFTWARE.
2323
2424
"""
25+
import logging
2526

2627

27-
class Config:
28+
logging.basicConfig(filename='contentstack.log', format='%(asctime)s - %(message)s', level=logging.INFO)
29+
logging.getLogger("Config")
30+
31+
32+
class Config():
2833

2934
def __init__(self):
30-
self.default_config = dict(protocol="https", host="cdn.contentstack.io", port=443, version="v3", urls={
31-
"sync": "/stacks/sync",
32-
"content_types": "/content_types/",
33-
"entries": "/entries/",
34-
"assets": "/assets/",
35-
"environments": "/environments/"
35+
self.defaultConfig = dict(protocol="https://", host="cdn.contentstack.io", port=443, version="v3", path={
36+
"stacks": "stacks",
37+
"sync": "stacks/sync",
38+
"content_types": "content_types",
39+
"entries": "entries",
40+
"assets": "assets",
41+
"environments": "environments"
3642
})
3743

3844
def host(self, host_url=None):
3945
if host_url is not None:
40-
self.default_config["host"] = host_url
41-
return self.default_config["host"]
42-
43-
def version(self):
44-
return self.default_config["host"]
46+
self.defaultConfig["host"] = host_url
47+
return self.defaultConfig["host"]
4548

46-
def protocol(self):
47-
return self.default_config["protocol"]
49+
def version(self, version: str = None):
50+
if version is not None and isinstance(version, str):
51+
self.defaultConfig['version'] = version
52+
return self.defaultConfig['version']
53+
else:
54+
return self.defaultConfig['version']
4855

49-
def get_endpoint(self, url_path):
50-
config_url = "{0}{1}/{2}/{3}".format(self.protocol(), self.host(), self.version(), url_path)
51-
return config_url
56+
def path(self, path):
57+
url_section = self.defaultConfig['path']
58+
if path in url_section:
59+
return url_section[path]
60+
else:
61+
logging.error("{0} is invalid endpoint path".format(path))
62+
raise ValueError('Invalid endpoint!!, {0} is invalid endpoint path, '
63+
'Path can be found among {1}'
64+
.format(path, url_section.keys()))
5265

53-
def hiShailesh(self):
54-
print("Something")
66+
def endpoint(self, path):
67+
url = self.path(path)
68+
if url is not None and isinstance(url, str):
69+
url = "{0}{1}/{2}/{3}".format(self.defaultConfig["protocol"], self.host(), self.version(), url)
70+
logging.info('{0} endpoint'.format(path))
71+
return url
5572

5673

5774
config = Config()
58-
host = config.host("stag-cdn.contentstack.io")
59-
print(host)
75+
config.host("cdn.contentstack.io")
76+
result_url = config.endpoint('assets')
77+
print(result_url)
6078

contentstack/http_request.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import platform
12
import urllib.parse
23
from urllib import parse
34

@@ -17,7 +18,7 @@ def __init__(self, url_path, query=dict, local_headers=dict):
1718
self._local_headers = local_headers
1819
self._local_headers['X-User-Agent'] = self._contentstack_user_agent()
1920
self._local_headers['Content-Type'] = 'application/json'
20-
self.url_path = config.Config().get_endpoint(self.url_path)
21+
self.url_path = config.Config().endpoint(self.url_path)
2122
if 'environment' in self._local_headers:
2223
self._query_prams['environment'] = self._local_headers['environment']
2324

@@ -43,20 +44,17 @@ def _contentstack_user_agent() -> str:
4344
'name': 'contentstack.python',
4445
'version': "1.0.0"
4546
}}
46-
# from contentstack import __version__
47-
# from sys import platform as cs_plateforom
48-
# os_name = cs_plateforom.system()
49-
# if os_name == 'Darwin':
50-
# os_name = 'macOS'
51-
# elif not os_name or os_name == 'Java':
52-
# os_name = None
53-
# elif os_name and os_name not in ['macOS', 'Windows']:
54-
# os_name = 'Linux'
55-
# header['os'] = {
56-
# 'name': os_name,
57-
# 'version': cs_plateforom.release()
58-
# }
59-
47+
os_name = platform.system()
48+
if os_name == 'Darwin':
49+
os_name = 'macOS'
50+
elif not os_name or os_name == 'Java':
51+
os_name = None
52+
elif os_name and os_name not in ['macOS', 'Windows']:
53+
os_name = 'Linux'
54+
header['os'] = {
55+
'name': os_name,
56+
'version': platform.release()
57+
}
6058
return header.__str__()
6159

6260
def set_entry_model(self):

contentstack/resource.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
contentstack.resource
3+
~~~~~~~~~~~~~~~~~~~
4+
"""
5+
6+
7+
class Resource(object):
8+
9+
def __init__(self, default_locale='en-us', **item):
10+
self.raw = item
11+
self.default_locale = default_locale
12+
13+
def fields(self, locale=None):
14+
if locale is None:
15+
locale = self._locale()
16+
17+
@property
18+
def locale(self):
19+
pass

contentstack/utils.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""
22
* MIT License
3-
*
43
* Copyright (c) 2012 - 2019 Contentstack
54
*
65
* Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -13,17 +12,20 @@
1312
* The above copyright notice and this permission notice shall be included in all
1413
* copies or substantial portions of the Software.
1514
*
16-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* [ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1716
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1817
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1918
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2019
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2120
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22-
* SOFTWARE.
21+
* SOFTWARE ]
2322
*
2423
"""
2524

2625
import logging
26+
import os
27+
28+
import requests
2729

2830
try:
2931
from logging import NullHandler
@@ -32,20 +34,40 @@ class NullHandler(logging.Handler):
3234
def emit(self, record):
3335
pass
3436

35-
logging.getLogger(__name__).addHandler(NullHandler())
36-
log = logging.getLogger(__name__)
37+
logging.basicConfig(filename='contentstack.log', format='%(asctime)s - %(message)s', level=logging.INFO)
38+
logging.getLogger("Config")
3739

3840

39-
class utils:
41+
class Util:
4042

4143
def __init__(self):
42-
log.error('logging called in utils')
44+
logging.error('logging called in utils')
4345
pass
4446

45-
'''def is_internet_on():
46-
try: urllib.request.urlopen('http://216.58.192.142', timeout=1)
47-
#return True
48-
except urllib2.URLError as err:
49-
return False'''
47+
@staticmethod
48+
def log(message: str):
49+
logging.debug(message)
50+
51+
@classmethod
52+
def is_connected(cls):
53+
import socket
54+
try:
55+
host = socket.gethostbyname('cdn.contentstack.io')
56+
s = socket.create_connection((host, 80), 2)
57+
s.close()
58+
return True
59+
except:
60+
pass
61+
return False
62+
63+
def findUserAgent(self):
64+
url = 'https://www.contentful.com'
65+
headers = {
66+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'}
67+
response = requests.get(url, headers=headers)
68+
print(response.content)
5069

5170

71+
utils = Util()
72+
status = utils.findUserAgent()
73+
print(status)

setup.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def get_version():
1818

1919

2020
def read(fname):
21-
"""Read description from local file."""
2221
return open(os.path.join(os.path.dirname(__file__), fname)).read()
2322

2423

@@ -47,7 +46,15 @@ def read(fname):
4746
"Operating System :: OS Independent",
4847
'Intended Audience :: Developers',
4948
'Natural Language :: English',
50-
'Programming Language :: Python',
49+
'Programming Language :: Python :: 3',
50+
'Programming Language :: Python :: 3.1',
51+
'Programming Language :: Python :: 3.2',
52+
'Programming Language :: Python :: 3.3',
53+
'Programming Language :: Python :: 3.4',
54+
'Programming Language :: Python :: 3.5',
55+
'Programming Language :: Python :: 3.6',
5156
'Programming Language :: Python :: 3.7',
5257
],
58+
test_suite='tests',
59+
5360
)

0 commit comments

Comments
 (0)