Skip to content

Commit a411fa4

Browse files
committed
add files
0 parents  commit a411fa4

File tree

8 files changed

+398
-0
lines changed

8 files changed

+398
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pylibconfig.egg-info
2+
.hg
3+
*.bak
4+
*.swp
5+
*.pyc

.hgignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#syntax: glob
2+
3+
syntax: regexp
4+
\.bak$
5+
\.pyc$
6+
pylibconfig.egg-info
7+
\.*\.swp$
8+
\.git\/*

README

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
python-libconfig
2+
3+
Python bindings to the C library libconfig
4+
5+
6+
INSTALLATION
7+
8+
First install libconfig and its development files.
9+
10+
Redhat: yum install libconfig libconfig-devel -y
11+
Debian: apt-get libconfig libconfig-devel
12+
openSUSE: zypper install libconfig8 libconfig-devel
13+
(available in the Packman repository)
14+
15+
On other platforms, you can compile libconfig from source:
16+
17+
http://www.hyperrealm.com/libconfig/
18+
19+
Instructions:
20+
21+
wget http://www.hyperrealm.com/libconfig/libconfig-1.3.2.tar.gz
22+
tar -zxf libconfig-1.3.2.tar.gz
23+
cd libconfig-1.3.2
24+
export MYPREFIX=/usr
25+
# or if you lack privileges for making system-wide changes:
26+
# export MYPREFIX=$HOME/local
27+
./configure --prefix=$MYPREFIX
28+
make
29+
make install
30+
31+
To install this module, run the following commands:
32+
33+
python setup.py build
34+
python setup.py install
35+
36+
37+
SEE ALSO
38+
39+
You can also look for Perl bindings information at:
40+
41+
RT, CPAN's request tracker
42+
http://rt.cpan.org/NoAuth/Bugs.html?Dist=Conf-Libconfig
43+
44+
AnnoCPAN, Annotated CPAN documentation
45+
http://annocpan.org/dist/Conf-Libconfig
46+
47+
CPAN Ratings
48+
http://cpanratings.perl.org/d/Conf-Libconfig
49+
50+
Search CPAN
51+
http://search.cpan.org/dist/Conf-Libconfig/
52+
53+
54+
COPYRIGHT AND LICENCE
55+
56+
Copyright (C) 2010 cnangel
57+
58+
This program is released under the following license: bsd

python-libconfig.spec

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Summary: A python interface to libconfig
2+
Name: python-libconfig
3+
Version: 0.0.1
4+
Release: 1
5+
License: bsd
6+
Group: Development/Libraries
7+
Source0: %{name}-%{version}.tar.gz
8+
URL: http://code.google.com/r/cnangel-python-libconfig/
9+
10+
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
11+
BuildRequires: libconfig libconfig-devel
12+
Requires: libconfig
13+
14+
%description
15+
Python interface to libconfig
16+
17+
python-libconfig is an interface to the popular config for Python.
18+
19+
%prep
20+
%setup -q -n %{name}-%{version}c1
21+
22+
%build
23+
rm -f doc/*~
24+
export libdirname=%{_lib}
25+
CFLAGS="$RPM_OPT_FLAGS" python setup.py build
26+
27+
%install
28+
rm -rf $RPM_BUILD_ROOT
29+
30+
export libdirname=%{_lib}
31+
python setup.py install --root=$RPM_BUILD_ROOT --record=INSTALLED_FILES
32+
33+
%clean
34+
rm -rf $RPM_BUILD_ROOT
35+
36+
%files -f INSTALLED_FILES
37+
%defattr(-,root,root)
38+
%doc README
39+
%{_libdir}/python?.?/site-packages/*.so
40+
41+
%changelog
42+
* Fri Apr 16 2010 Cnangel <junliang.li@alibaba-inc.com> 1.0.0-1
43+
- build the first spec file

setup.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from setuptools import setup, find_packages, Extension
4+
import sys, os
5+
6+
setup(
7+
name = 'pylibconfig',
8+
description = "libconfig bindings for Python",
9+
version = "0.0.1",
10+
author = "Sergey S. Gogin",
11+
author_email = "d-x@bk.ru",
12+
maintainer ="cnangel",
13+
maintainer_email = "cnangel@gmail.com",
14+
keywords = "libconfig libconfig++ boost python config configuration",
15+
test_suite = "tests",
16+
license = "bsd",
17+
url = "",
18+
ext_modules = [
19+
Extension(
20+
"pylibconfig",
21+
["src/pylibconfig.cc"],
22+
libraries=["boost_python", "config++"]
23+
#include_dirs=includes,
24+
#extra_link_args=lflags
25+
)
26+
]
27+
)

src/pylibconfig.cc

Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
#include <boost/python.hpp>
2+
#include <libconfig.h++>
3+
#include <string>
4+
5+
using namespace boost::python;
6+
using namespace libconfig;
7+
8+
class pyConfig
9+
{
10+
public:
11+
pyConfig ()
12+
{
13+
config = new Config ();
14+
}
15+
16+
~pyConfig ()
17+
{
18+
delete config;
19+
}
20+
21+
void read ( FILE * stream )
22+
{ config->read ( stream ); }
23+
24+
void write ( FILE * stream )
25+
{ config->write ( stream ); }
26+
27+
void readFile ( const char * filename )
28+
{ config->readFile ( filename ); }
29+
30+
void writeFile ( const char * filename )
31+
{ config->writeFile ( filename ); }
32+
33+
bool getAutoConvert ()
34+
{ return config->getAutoConvert (); }
35+
36+
void setAutoConvert ( bool flag )
37+
{ config->setAutoConvert ( flag ); }
38+
39+
bool exists ( const char * path )
40+
{ return config->exists ( path ); }
41+
42+
tuple value ( const char * path )
43+
{
44+
std::string v_string;
45+
if ( config->lookupValue ( path, v_string ) )
46+
return make_tuple ( v_string.c_str(), true );
47+
48+
unsigned int v_uint;
49+
if ( config->lookupValue ( path, v_uint ) )
50+
return make_tuple ( v_uint, true );
51+
52+
int v_int;
53+
if ( config->lookupValue ( path, v_int ) )
54+
return make_tuple ( v_int, true );
55+
56+
bool v_bool;
57+
if ( config->lookupValue ( path, v_bool ) )
58+
return make_tuple ( v_bool, true );
59+
60+
unsigned long long v_ulonglong;
61+
if ( config->lookupValue ( path, v_ulonglong ) )
62+
return make_tuple ( v_ulonglong, true );
63+
64+
long long v_longlong;
65+
if ( config->lookupValue ( path, v_longlong ) )
66+
return make_tuple ( v_longlong, true );
67+
68+
float v_float;
69+
if ( config->lookupValue ( path, v_float ) )
70+
return make_tuple ( v_float, true );
71+
72+
double v_double;
73+
if ( config->lookupValue ( path, v_double ) )
74+
return make_tuple ( v_double, true );
75+
76+
return make_tuple ( "", false );
77+
}
78+
79+
list children_root ()
80+
{
81+
list result;
82+
int length = config->getRoot ().getLength ();
83+
for ( int index = 0; index < length; index++ )
84+
{
85+
result.append ( config->getRoot () [ index ].getPath ().c_str () );
86+
}
87+
return result;
88+
}
89+
90+
list children ( const char * path )
91+
{
92+
list result;
93+
try {
94+
list result;
95+
int length = config->lookup ( path ).getLength ();
96+
for ( int index = 0; index < length; index++ )
97+
{
98+
result.append ( config->lookup ( path ) [ index ].getPath ().c_str () );
99+
}
100+
return result;
101+
}
102+
catch ( SettingNotFoundException & e )
103+
{
104+
return result;
105+
}
106+
}
107+
108+
void remove ( const char * path, const char * name )
109+
{
110+
config->lookup ( path ).remove ( name );
111+
}
112+
113+
void addBoolean ( const char * path, const char * name )
114+
{
115+
config->lookup ( path ).add ( name, libconfig::Setting::TypeBoolean );
116+
}
117+
118+
void addBigInteger ( const char * path, const char * name )
119+
{
120+
config->lookup ( path ).add ( name, libconfig::Setting::TypeInt64 );
121+
}
122+
123+
void addInteger ( const char * path, const char * name )
124+
{
125+
config->lookup ( path ).add ( name, libconfig::Setting::TypeInt );
126+
}
127+
128+
void addFloat ( const char * path, const char * name )
129+
{
130+
config->lookup ( path ).add ( name, libconfig::Setting::TypeFloat );
131+
}
132+
133+
void addString ( const char * path, const char * name )
134+
{
135+
config->lookup ( path ).add ( name, libconfig::Setting::TypeString );
136+
}
137+
138+
void addGroup ( const char * path, const char * name )
139+
{
140+
config->lookup ( path ).add ( name, libconfig::Setting::TypeGroup );
141+
}
142+
143+
void addList ( const char * path, const char * name )
144+
{
145+
config->lookup ( path ).add ( name, libconfig::Setting::TypeList );
146+
}
147+
148+
void addArray ( const char * path, const char * name )
149+
{
150+
config->lookup ( path ).add ( name, libconfig::Setting::TypeArray );
151+
}
152+
153+
void appendToList ( const char * path, const char * value )
154+
{
155+
config->lookup ( path ).add ( libconfig::Setting::TypeString ) = value;
156+
}
157+
158+
void setValue_bool ( const char * path, bool value )
159+
{
160+
config->lookup ( path ) = value;
161+
}
162+
163+
void setValue_int ( const char * path, int value )
164+
{
165+
config->lookup ( path ) = value;
166+
}
167+
168+
void setValue_long ( const char * path, long value )
169+
{
170+
config->lookup ( path ) = value;
171+
}
172+
173+
void setValue_longlong ( const char * path, long long value )
174+
{
175+
config->lookup ( path ) = value;
176+
}
177+
178+
void setValue_float ( const char * path, float value )
179+
{
180+
config->lookup ( path ) = value;
181+
}
182+
183+
void setValue_double ( const char * path, double value )
184+
{
185+
config->lookup ( path ) = value;
186+
}
187+
188+
void setValue_str ( const char * path, std::string value )
189+
{
190+
config->lookup ( path ) = value;
191+
}
192+
193+
private:
194+
Config * config;
195+
};
196+
197+
BOOST_PYTHON_MODULE ( pylibconfig )
198+
{
199+
class_<pyConfig>("Config")
200+
.def("read", &pyConfig::read )
201+
.def("write", &pyConfig::write )
202+
.def("readFile", &pyConfig::readFile )
203+
.def("writeFile", &pyConfig::writeFile )
204+
.def("getAutoConvert", &pyConfig::getAutoConvert )
205+
.def("setAutoConvert", &pyConfig::setAutoConvert )
206+
.def("exists", &pyConfig::exists )
207+
.def("children", &pyConfig::children )
208+
.def("children", &pyConfig::children_root )
209+
.def("value", &pyConfig::value )
210+
.def("remove", &pyConfig::remove )
211+
.def("addString", &pyConfig::addString )
212+
.def("addBoolean", &pyConfig::addBoolean )
213+
.def("addBigInteger", &pyConfig::addBigInteger )
214+
.def("addInteger", &pyConfig::addInteger )
215+
.def("addFloat", &pyConfig::addFloat )
216+
.def("addGroup", &pyConfig::addGroup )
217+
.def("addList", &pyConfig::addList )
218+
.def("addArray", &pyConfig::addArray )
219+
.def("setValue", &pyConfig::setValue_bool )
220+
.def("setValue", &pyConfig::setValue_float )
221+
.def("setValue", &pyConfig::setValue_double )
222+
.def("setValue", &pyConfig::setValue_longlong )
223+
.def("setValue", &pyConfig::setValue_long )
224+
.def("setValue", &pyConfig::setValue_int )
225+
.def("setValue", &pyConfig::setValue_str )
226+
.def("appendToList", &pyConfig::appendToList )
227+
;
228+
}

tests/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# -*- coding: utf-8 -*-
2+
all = ["test"]

0 commit comments

Comments
 (0)