├── README.md ├── odata1c ├── __init__.py ├── odata1c.py └── odata1cparser.py └── test.py /README.md: -------------------------------------------------------------------------------- 1 | # 1c_odata_python 2 | 3 | Библиотека работы с odata протоколом 1С. 4 | 5 | https://gitter.im/surge-/1c_odata_python 6 | 7 | url = 'http://localhost/odata/standard.odata/' 8 | 9 | service = odata1c.service(url=url, login='admin', password='') 10 | 11 | brands = service.Catalog['Brands'] 12 | 13 | for i in brands['entry']['content']: 14 | print i['Description'] 15 | -------------------------------------------------------------------------------- /odata1c/__init__.py: -------------------------------------------------------------------------------- 1 | from odata1c import service 2 | -------------------------------------------------------------------------------- /odata1c/odata1c.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import urllib2 4 | import feedparser 5 | import requests 6 | import xml.etree.ElementTree as etree 7 | 8 | from odata1cparser import Odata1cObj 9 | 10 | 11 | 12 | 13 | class service(object): 14 | 15 | def __init__(self, url, login, password): 16 | self.url = url 17 | self.login = login 18 | self.password = password 19 | self.workspace = self.get_workspace() 20 | 21 | # for key in self.workspace.keys(): 22 | # setattr(self, key, Odata1cObj( self, key) ) 23 | # statr = getattr(self, key) 24 | # statr.append_items(self.workspace[key]) 25 | 26 | 27 | #Четение всех таблиц из 1C Workspace только для таких как Catalog и Document 28 | # for item in self.workspace[name]: 29 | # print item 30 | # setattr(self, item, Odata1cObj( self, name) ) 31 | 32 | 33 | 34 | 35 | def read (self, collection='', entry=''): 36 | if collection not in self.workspace.keys(): 37 | print '[ERROR] Workspace has no ', collection 38 | return 39 | 40 | if entry not in self.workspace[collection]: 41 | print '[ERROR] %s has no '%(collection,), entry 42 | return 43 | 44 | query_url = self.url + collection+'_'+ entry 45 | req = requests.get(query_url, auth=(self.login, self.password)) 46 | root = etree.fromstring(req.text.encode('utf-8')) 47 | return root 48 | 49 | 50 | 51 | 52 | def get_workspace(self): 53 | catalog_dict = list() 54 | document_dict = list() 55 | 56 | #if element.find('...') is not None. 57 | 58 | req = requests.get(self.url, auth=(self.login, self.password)) 59 | root = etree.fromstring(req.text.encode('utf-8')) 60 | 61 | #print root[0][1] 62 | #print root[0][1][0].text 63 | 64 | collections = root[0].findall('{http://www.w3.org/2007/app}collection') 65 | 66 | for collection in collections: 67 | txt = collection[0].text 68 | splitted = txt.split('_') 69 | 70 | if 'Catalog' in splitted: 71 | catalog_dict.append( txt.replace('Catalog_','') ) 72 | elif 'Document' in splitted: 73 | document_dict.append( txt.replace('Document_','') ) 74 | 75 | workspace = {'Catalog' : catalog_dict, 'Document':document_dict} 76 | return workspace 77 | 78 | def __getattribute__(self, name): 79 | 80 | try: 81 | ret_res = super(service, self).__getattribute__(name) 82 | except AttributeError: 83 | workspace = super(service, self).__getattribute__('workspace') 84 | if name in workspace.keys(): 85 | db1c = Odata1cObj( self, name) 86 | #db1c.append_items(workspace[name]) 87 | #db1c.load() 88 | setattr(self, name, db1c) 89 | 90 | #ret_res = db1c.load() 91 | ret_res = db1c 92 | 93 | return ret_res 94 | 95 | 96 | #object.__getattribute__(self, 'workspace') 97 | # if name in workspace: 98 | # db1c = object.__getattribute__(self, name) 99 | # print 'Downloading from 1C..' 100 | # return db1c.load() 101 | # else: 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | -------------------------------------------------------------------------------- /odata1c/odata1cparser.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | 4 | 5 | 6 | class Obj1C(object): 7 | 8 | def __init__(self, service, name): 9 | self.service = service 10 | self.name = name 11 | self.items=dict() 12 | #self.load() 13 | 14 | 15 | def load(self): 16 | #http://95.79.53.147:9080/bit_nn123/odata/standard.odata/Document_Order 17 | if self.name in self.service.workspace.keys(): 18 | print 'Downloading ',self.name, '...' 19 | #self.items = self.service.get_workspace()[self.name] 20 | for i in self.items: 21 | print self.name, i 22 | root = self.service.read(self.name, i) 23 | self.parse_xml(root,i) 24 | 25 | return self.items 26 | 27 | 28 | def parse_xml(self,root,table): 29 | entrys = root.findall('{http://www.w3.org/2005/Atom}entry') 30 | if len(entrys) > 0: 31 | #print len(entrys) 32 | 33 | self.items[table]={'entry' : { 'content' : [] } } 34 | 35 | 36 | for entry in entrys: 37 | 38 | contents = entry.findall('{http://www.w3.org/2005/Atom}content') 39 | for content in contents: 40 | properties = content.findall('{http://schemas.microsoft.com/ado/2007/08/dataservices/metadata}properties') 41 | prop_dict = dict() 42 | for prop in properties[0]: 43 | key = prop.tag.split('}')[-1] 44 | prop_dict[key]=prop.text 45 | 46 | self.items[table]['entry']['content'].append(prop_dict) 47 | 48 | 49 | 50 | def append_items(self, items): 51 | for i in items: 52 | self.items[i]=dict() 53 | 54 | def __str__(self): 55 | return str(self.items) 56 | 57 | def __getitem__(self,index): 58 | 59 | if index in self.service.workspace[self.name]: 60 | print self.name+'_'+index 61 | root = self.service.read(self.name, index) 62 | print root.tag 63 | self.parse_xml(root,index) 64 | res = self.items[index] 65 | else: 66 | print index,' Not in ', self.name 67 | res = {} 68 | # if index in : 69 | # print self.name+'_'+index 70 | # root = self.service.read(self.name, index) 71 | # print root.tag 72 | 73 | return res 74 | 75 | 76 | 77 | 78 | 79 | class Odata1cObj(Obj1C): 80 | pass -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | from odata1c import odata1c 2 | import xml.etree.ElementTree as etree 3 | import requests 4 | 5 | 6 | url = 'http://localhost/odata/standard.odata/' 7 | 8 | service = odata1c.service(url=url, login='admin', password='') 9 | 10 | #print service.workspace['Catalog'] 11 | 12 | catalogs = service.Catalog 13 | 14 | for i in catalogs['SKUGroup']['entry']['content']: 15 | print i 16 | #print service.Document['Order'] 17 | 18 | 19 | 20 | 21 | 22 | 23 | --------------------------------------------------------------------------------