From e5de56f799ae859d23dfb3974594ffd747f003c9 Mon Sep 17 00:00:00 2001 From: Python3pkg Date: Wed, 17 May 2017 21:50:38 -0700 Subject: [PATCH] Convert to python3 --- odata1c/__init__.py | 2 +- odata1c/__init__.py.bak | 1 + odata1c/odata1c.py | 12 ++-- odata1c/odata1c.py.bak | 110 +++++++++++++++++++++++++++++++++++ odata1c/odata1cparser.py | 12 ++-- odata1c/odata1cparser.py.bak | 80 +++++++++++++++++++++++++ test.py | 2 +- test.py.bak | 22 +++++++ 8 files changed, 227 insertions(+), 14 deletions(-) create mode 100644 odata1c/__init__.py.bak create mode 100644 odata1c/odata1c.py.bak create mode 100644 odata1c/odata1cparser.py.bak create mode 100644 test.py.bak diff --git a/odata1c/__init__.py b/odata1c/__init__.py index 48e50a8..07034cd 100644 --- a/odata1c/__init__.py +++ b/odata1c/__init__.py @@ -1 +1 @@ -from odata1c import service +from .odata1c import service diff --git a/odata1c/__init__.py.bak b/odata1c/__init__.py.bak new file mode 100644 index 0000000..48e50a8 --- /dev/null +++ b/odata1c/__init__.py.bak @@ -0,0 +1 @@ +from odata1c import service diff --git a/odata1c/odata1c.py b/odata1c/odata1c.py index 2cf2da8..05a2215 100644 --- a/odata1c/odata1c.py +++ b/odata1c/odata1c.py @@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- -import urllib2 +import urllib.request, urllib.error, urllib.parse import feedparser import requests import xml.etree.ElementTree as etree -from odata1cparser import Odata1cObj +from .odata1cparser import Odata1cObj @@ -33,12 +33,12 @@ def __init__(self, url, login, password): def read (self, collection='', entry=''): - if collection not in self.workspace.keys(): - print '[ERROR] Workspace has no ', collection + if collection not in list(self.workspace.keys()): + print(('[ERROR] Workspace has no ', collection)) return if entry not in self.workspace[collection]: - print '[ERROR] %s has no '%(collection,), entry + print(('[ERROR] %s has no '%(collection,), entry)) return query_url = self.url + collection+'_'+ entry @@ -81,7 +81,7 @@ def __getattribute__(self, name): ret_res = super(service, self).__getattribute__(name) except AttributeError: workspace = super(service, self).__getattribute__('workspace') - if name in workspace.keys(): + if name in list(workspace.keys()): db1c = Odata1cObj( self, name) #db1c.append_items(workspace[name]) #db1c.load() diff --git a/odata1c/odata1c.py.bak b/odata1c/odata1c.py.bak new file mode 100644 index 0000000..f6b2c36 --- /dev/null +++ b/odata1c/odata1c.py.bak @@ -0,0 +1,110 @@ +# -*- coding: utf-8 -*- + +import urllib.request, urllib.error, urllib.parse +import feedparser +import requests +import xml.etree.ElementTree as etree + +from .odata1cparser import Odata1cObj + + + + +class service(object): + + def __init__(self, url, login, password): + self.url = url + self.login = login + self.password = password + self.workspace = self.get_workspace() + + # for key in self.workspace.keys(): + # setattr(self, key, Odata1cObj( self, key) ) + # statr = getattr(self, key) + # statr.append_items(self.workspace[key]) + + + #Четение всех таблиц из 1C Workspace только для таких как Catalog и Document + # for item in self.workspace[name]: + # print item + # setattr(self, item, Odata1cObj( self, name) ) + + + + + def read (self, collection='', entry=''): + if collection not in list(self.workspace.keys()): + print('[ERROR] Workspace has no ', collection) + return + + if entry not in self.workspace[collection]: + print('[ERROR] %s has no '%(collection,), entry) + return + + query_url = self.url + collection+'_'+ entry + req = requests.get(query_url, auth=(self.login, self.password)) + root = etree.fromstring(req.text.encode('utf-8')) + return root + + + + + def get_workspace(self): + catalog_dict = list() + document_dict = list() + + #if element.find('...') is not None. + + req = requests.get(self.url, auth=(self.login, self.password)) + root = etree.fromstring(req.text.encode('utf-8')) + + #print root[0][1] + #print root[0][1][0].text + + collections = root[0].findall('{http://www.w3.org/2007/app}collection') + + for collection in collections: + txt = collection[0].text + splitted = txt.split('_') + + if 'Catalog' in splitted: + catalog_dict.append( txt.replace('Catalog_','') ) + elif 'Document' in splitted: + document_dict.append( txt.replace('Document_','') ) + + workspace = {'Catalog' : catalog_dict, 'Document':document_dict} + return workspace + + def __getattribute__(self, name): + + try: + ret_res = super(service, self).__getattribute__(name) + except AttributeError: + workspace = super(service, self).__getattribute__('workspace') + if name in list(workspace.keys()): + db1c = Odata1cObj( self, name) + #db1c.append_items(workspace[name]) + #db1c.load() + setattr(self, name, db1c) + + #ret_res = db1c.load() + ret_res = db1c + + return ret_res + + + #object.__getattribute__(self, 'workspace') + # if name in workspace: + # db1c = object.__getattribute__(self, name) + # print 'Downloading from 1C..' + # return db1c.load() + # else: + + + + + + + + + diff --git a/odata1c/odata1cparser.py b/odata1c/odata1cparser.py index 8dc55cb..e158731 100644 --- a/odata1c/odata1cparser.py +++ b/odata1c/odata1cparser.py @@ -14,11 +14,11 @@ def __init__(self, service, name): def load(self): #http://95.79.53.147:9080/bit_nn123/odata/standard.odata/Document_Order - if self.name in self.service.workspace.keys(): - print 'Downloading ',self.name, '...' + if self.name in list(self.service.workspace.keys()): + print(('Downloading ',self.name, '...')) #self.items = self.service.get_workspace()[self.name] for i in self.items: - print self.name, i + print((self.name, i)) root = self.service.read(self.name, i) self.parse_xml(root,i) @@ -57,13 +57,13 @@ def __str__(self): def __getitem__(self,index): if index in self.service.workspace[self.name]: - print self.name+'_'+index + print((self.name+'_'+index)) root = self.service.read(self.name, index) - print root.tag + print((root.tag)) self.parse_xml(root,index) res = self.items[index] else: - print index,' Not in ', self.name + print((index,' Not in ', self.name)) res = {} # if index in : # print self.name+'_'+index diff --git a/odata1c/odata1cparser.py.bak b/odata1c/odata1cparser.py.bak new file mode 100644 index 0000000..888db2b --- /dev/null +++ b/odata1c/odata1cparser.py.bak @@ -0,0 +1,80 @@ +# -*- coding: utf-8 -*- + + + + +class Obj1C(object): + + def __init__(self, service, name): + self.service = service + self.name = name + self.items=dict() + #self.load() + + + def load(self): + #http://95.79.53.147:9080/bit_nn123/odata/standard.odata/Document_Order + if self.name in list(self.service.workspace.keys()): + print('Downloading ',self.name, '...') + #self.items = self.service.get_workspace()[self.name] + for i in self.items: + print(self.name, i) + root = self.service.read(self.name, i) + self.parse_xml(root,i) + + return self.items + + + def parse_xml(self,root,table): + entrys = root.findall('{http://www.w3.org/2005/Atom}entry') + if len(entrys) > 0: + #print len(entrys) + + self.items[table]={'entry' : { 'content' : [] } } + + + for entry in entrys: + + contents = entry.findall('{http://www.w3.org/2005/Atom}content') + for content in contents: + properties = content.findall('{http://schemas.microsoft.com/ado/2007/08/dataservices/metadata}properties') + prop_dict = dict() + for prop in properties[0]: + key = prop.tag.split('}')[-1] + prop_dict[key]=prop.text + + self.items[table]['entry']['content'].append(prop_dict) + + + + def append_items(self, items): + for i in items: + self.items[i]=dict() + + def __str__(self): + return str(self.items) + + def __getitem__(self,index): + + if index in self.service.workspace[self.name]: + print(self.name+'_'+index) + root = self.service.read(self.name, index) + print(root.tag) + self.parse_xml(root,index) + res = self.items[index] + else: + print(index,' Not in ', self.name) + res = {} + # if index in : + # print self.name+'_'+index + # root = self.service.read(self.name, index) + # print root.tag + + return res + + + + + +class Odata1cObj(Obj1C): + pass \ No newline at end of file diff --git a/test.py b/test.py index b85bdcc..3035f64 100644 --- a/test.py +++ b/test.py @@ -12,7 +12,7 @@ catalogs = service.Catalog for i in catalogs['SKUGroup']['entry']['content']: - print i + print(i) #print service.Document['Order'] diff --git a/test.py.bak b/test.py.bak new file mode 100644 index 0000000..b85bdcc --- /dev/null +++ b/test.py.bak @@ -0,0 +1,22 @@ +from odata1c import odata1c +import xml.etree.ElementTree as etree +import requests + + +url = 'http://localhost/odata/standard.odata/' + +service = odata1c.service(url=url, login='admin', password='') + +#print service.workspace['Catalog'] + +catalogs = service.Catalog + +for i in catalogs['SKUGroup']['entry']['content']: + print i +#print service.Document['Order'] + + + + + +