Skip to content

Commit acdcde7

Browse files
committed
Merge pull request #33 from mikrosimage/swig_FrameRange_clean_SequenceRetrieveInfosFromPattern
Swig FrameRangesSubView iterable + clean Sequence::retrieveInfosFromPattern
2 parents 5014a48 + 090478d commit acdcde7

10 files changed

Lines changed: 210 additions & 54 deletions

File tree

src/FrameRange.i

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,15 @@
4747
}
4848
}
4949

50+
%extend sequenceParser::FrameRangesSubView
51+
{
52+
%pythoncode
53+
{
54+
def __iter__(self):
55+
return PyFrameRangesConstIterator(self.begin().previous(), self.end())
56+
def __str__(self):
57+
return self.string()
58+
}
59+
}
60+
5061
#endif

src/Item.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,25 @@ std::vector<Item> Item::explode()
4242
EType getTypeFromPath( const boost::filesystem::path& path )
4343
{
4444
if( bfs::is_symlink( path ) )
45-
{
46-
return eTypeLink;
47-
}
48-
if( bfs::is_regular_file( path ) )
4945
{
50-
return eTypeFile;
51-
}
46+
return eTypeLink;
47+
}
48+
if( bfs::is_regular_file( path ) )
49+
{
50+
return eTypeFile;
51+
}
5252
if( bfs::is_directory( path ) )
53-
{
54-
return eTypeFolder;
55-
}
56-
return eTypeUndefined;
53+
{
54+
return eTypeFolder;
55+
}
56+
return eTypeUndefined;
57+
}
58+
59+
60+
EType getTypeFromPath( const std::string& pathStr )
61+
{
62+
const boost::filesystem::path path( pathStr );
63+
return getTypeFromPath(path);
5764
}
5865

5966

src/Item.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ class Item
6060
_path /= sequence.getStandardPattern();
6161
}
6262

63+
Item( const Sequence& sequence, const std::string& folder )
64+
: _type(eTypeSequence)
65+
, _path(folder)
66+
, _sequence(sequence)
67+
{
68+
_path /= sequence.getStandardPattern();
69+
}
70+
6371
EType getType() const { return _type; }
6472

6573
std::string getAbsoluteFilepath() const { return _path.string(); }
@@ -92,7 +100,10 @@ class Item
92100
};
93101

94102

103+
#ifndef SWIG
95104
EType getTypeFromPath( const boost::filesystem::path& path );
105+
#endif
106+
EType getTypeFromPath( const std::string& pathStr );
96107

97108

98109
std::ostream& operator<<( std::ostream& os, const Item& item );

src/ItemStat.cpp

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include "ItemStat.hpp"
2-
#include "system.hpp"
32

43
#include <boost/filesystem/operations.hpp>
54

6-
#include <sys/types.h>
75
#include <sys/stat.h>
6+
#include <pwd.h>
7+
#include <grp.h>
88

99

1010
namespace bfs = boost::filesystem;
@@ -65,6 +65,45 @@ ItemStat::ItemStat( const Item& item, const bool approximative )
6565
}
6666
}
6767

68+
std::string ItemStat::getUserName() const
69+
{
70+
#ifdef __UNIX__
71+
passwd* user = getpwuid(userId);
72+
if(user == NULL)
73+
return std::string("unknown");
74+
return std::string(user->pw_name ? user->pw_name : "unknown");
75+
#else
76+
return std::string("not implemented");
77+
#endif
78+
}
79+
80+
std::string ItemStat::getGroupName() const
81+
{
82+
#ifdef __UNIX__
83+
group* group = getgrgid(groupId);
84+
if(group == NULL)
85+
return std::string("unknown");
86+
return std::string(group->gr_name ? group->gr_name : "unknown");
87+
#else
88+
return std::string("not implemented");
89+
#endif
90+
}
91+
92+
#ifdef __UNIX__
93+
void ItemStat::setPermissions( const mode_t& protection )
94+
{
95+
ownerCanRead = protection & S_IRUSR;
96+
ownerCanWrite = protection & S_IWUSR;
97+
ownerCanExecute = protection & S_IXUSR;
98+
groupCanRead = protection & S_IRGRP;
99+
groupCanWrite = protection & S_IWGRP;
100+
groupCanExecute = protection & S_IXGRP;
101+
otherCanRead = protection & S_IROTH;
102+
otherCanWrite = protection & S_IWOTH;
103+
otherCanExecute = protection & S_IXOTH;
104+
}
105+
#endif
106+
68107
void ItemStat::statLink( const boost::filesystem::path& path )
69108
{
70109
boost::system::error_code errorCode;
@@ -87,8 +126,11 @@ void ItemStat::statLink( const boost::filesystem::path& path )
87126
accessTime = statInfos.st_atime;
88127
creationTime = statInfos.st_ctime;
89128
size = statInfos.st_size;
129+
minSize = size;
130+
maxSize = size;
90131
// size on hard-drive (takes hardlinks into account)
91132
sizeOnDisk = (statInfos.st_blocks / nbHardLinks) * 512;
133+
setPermissions(statInfos.st_mode);
92134
#else
93135
fullNbHardLinks = nbHardLinks = 1;
94136
deviceId = 0;
@@ -112,9 +154,20 @@ void ItemStat::setDefaultValues(){
112154
creationTime = 0;
113155
sizeOnDisk = 0;
114156
size = 0;
157+
minSize = 0;
158+
maxSize = 0;
115159
realSize = 0;
116160
fullNbHardLinks = 0;
117161
modificationTime = -1;
162+
ownerCanRead = false;
163+
ownerCanWrite = false;
164+
ownerCanExecute = false;
165+
groupCanRead = false;
166+
groupCanWrite = false;
167+
groupCanExecute = false;
168+
otherCanRead = false;
169+
otherCanWrite = false;
170+
otherCanExecute = false;
118171
}
119172

120173
void ItemStat::statFolder( const boost::filesystem::path& path )
@@ -141,8 +194,11 @@ void ItemStat::statFolder( const boost::filesystem::path& path )
141194
accessTime = statInfos.st_atime;
142195
creationTime = statInfos.st_ctime;
143196
size = statInfos.st_size;
197+
minSize = size;
198+
maxSize = size;
144199
// size on hard-drive (takes hardlinks into account)
145200
sizeOnDisk = statInfos.st_blocks * 512;
201+
setPermissions(statInfos.st_mode);
146202
#else
147203
deviceId = 0;
148204
inodeId = 0;
@@ -164,6 +220,8 @@ void ItemStat::statFile( const boost::filesystem::path& path )
164220
boost::system::error_code errorCode;
165221
fullNbHardLinks = nbHardLinks = bfs::hard_link_count( path, errorCode );
166222
size = bfs::file_size( path, errorCode );
223+
minSize = size;
224+
maxSize = size;
167225
modificationTime = bfs::last_write_time( path, errorCode );
168226
int stat_status = -1;
169227

@@ -183,8 +241,8 @@ void ItemStat::statFile( const boost::filesystem::path& path )
183241
accessTime = statInfos.st_atime;
184242
creationTime = statInfos.st_ctime;
185243
// size on hard-drive (takes hardlinks into account)
186-
sizeOnDisk = (statInfos.st_blocks / nbHardLinks) * 512;
187-
244+
sizeOnDisk = (statInfos.st_blocks / nbHardLinks) * 512;
245+
setPermissions(statInfos.st_mode);
188246
#else
189247
deviceId = 0;
190248
inodeId = 0;
@@ -219,6 +277,7 @@ void ItemStat::statSequence( const Item& item, const bool approximative )
219277
userId = statInfos.st_uid;
220278
groupId = statInfos.st_gid;
221279
accessTime = statInfos.st_atime;
280+
setPermissions(statInfos.st_mode);
222281
#else
223282
deviceId = 0;
224283
inodeId = 0;
@@ -230,6 +289,8 @@ void ItemStat::statSequence( const Item& item, const bool approximative )
230289
modificationTime = 0;
231290
fullNbHardLinks = 0;
232291
size = 0;
292+
minSize = 0;
293+
maxSize = 0;
233294
realSize = 0;
234295
sizeOnDisk = 0;
235296
creationTime = 0;
@@ -250,14 +311,44 @@ void ItemStat::statSequence( const Item& item, const bool approximative )
250311

251312
ItemStat fileStat(type, filepath);
252313

314+
// use the most restrictive permissions in the sequence
315+
#ifdef __UNIX__
316+
// user
317+
if( ownerCanRead && ! fileStat.ownerCanRead)
318+
ownerCanRead = false;
319+
if( ownerCanWrite && ! fileStat.ownerCanWrite)
320+
ownerCanWrite = false;
321+
if( ownerCanExecute && ! fileStat.ownerCanExecute)
322+
ownerCanExecute = false;
323+
// group
324+
if( groupCanRead && ! fileStat.groupCanRead)
325+
groupCanRead = false;
326+
if( groupCanWrite && ! fileStat.groupCanWrite)
327+
groupCanWrite = false;
328+
if( groupCanExecute && ! fileStat.groupCanExecute)
329+
groupCanExecute = false;
330+
// other
331+
if( otherCanRead && ! fileStat.otherCanRead)
332+
otherCanRead = false;
333+
if( otherCanWrite && ! fileStat.otherCanWrite)
334+
otherCanWrite = false;
335+
if( otherCanExecute && ! fileStat.otherCanExecute)
336+
otherCanExecute = false;
337+
#endif
338+
253339
// use the latest modification date in the sequence
254340
if( fileStat.modificationTime > modificationTime )
255341
modificationTime = fileStat.modificationTime;
256342
if( creationTime == 0 || creationTime > fileStat.creationTime )
257343
creationTime = fileStat.creationTime;
258344

345+
// compute sizes
259346
fullNbHardLinks += fileStat.fullNbHardLinks;
260347
size += fileStat.size;
348+
if( minSize == 0 || minSize > fileStat.size )
349+
minSize = fileStat.size;
350+
if( maxSize == 0 || maxSize < fileStat.size )
351+
maxSize = fileStat.size;
261352
realSize += fileStat.realSize;
262353
sizeOnDisk += fileStat.sizeOnDisk;
263354
}

src/ItemStat.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
#include "Item.hpp"
55
#include "common.hpp"
6+
#include "system.hpp"
7+
8+
#ifdef __UNIX__
9+
#include <sys/types.h>
10+
#endif
611

712

813
namespace sequenceParser {
@@ -13,12 +18,18 @@ class ItemStat
1318
ItemStat( const Item& item, const bool approximative=true );
1419
ItemStat( const EType& type, const boost::filesystem::path& path, const bool approximative=true );
1520

21+
std::string getUserName() const;
22+
std::string getGroupName() const;
23+
1624
private:
1725
void statFolder( const boost::filesystem::path& path );
1826
void statFile( const boost::filesystem::path& path );
1927
void statSequence( const Item& item, const bool approximative );
2028
void statLink( const boost::filesystem::path& path );
2129
void setDefaultValues();
30+
#ifdef __UNIX__
31+
void setPermissions( const mode_t& protection );
32+
#endif
2233

2334
public:
2435
long long deviceId;
@@ -28,11 +39,22 @@ class ItemStat
2839
long long userId;
2940
long long groupId;
3041
long long size;
42+
long long minSize; /// size of the smallest file in the sequence (otherwise, same value as size)
43+
long long maxSize; /// size of the biggest file in the sequence (otherwise, same value as size)
3144
long long realSize; /// size (takes hardlinks into account)
3245
long long sizeOnDisk; /// size on hard-drive (takes hardlinks into account)
3346
long long accessTime;
3447
long long modificationTime;
3548
long long creationTime;
49+
bool ownerCanRead;
50+
bool ownerCanWrite;
51+
bool ownerCanExecute;
52+
bool groupCanRead;
53+
bool groupCanWrite;
54+
bool groupCanExecute;
55+
bool otherCanRead;
56+
bool otherCanWrite;
57+
bool otherCanExecute;
3658
};
3759

3860
}

src/Sequence.cpp

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -318,48 +318,44 @@ EPattern Sequence::checkPattern( const std::string& pattern, const EDetection de
318318
* and init internal values.
319319
* @param[in] pattern
320320
* @param[in] accept
321-
* @param[out] prefix
322-
* @param[out] suffix
323-
* @param[out] padding
324-
* @param[out] strictPadding
325321
*/
326-
bool Sequence::retrieveInfosFromPattern( const std::string& filePattern, const EPattern& accept, std::string& prefix, std::string& suffix, std::size_t& padding, bool& strictPadding ) const
322+
bool Sequence::initFromPattern( const std::string& filePattern, const EPattern& accept )
327323
{
328324
boost::cmatch matches;
329-
//std::cout << filePattern << " / " << prefix << " + " << padding << " + " << suffix << std::endl;
325+
//std::cout << filePattern << " / " << _prefix << " + " << _padding << " + " << _suffix << std::endl;
330326
if( ( accept & ePatternStandard ) && regex_match( filePattern.c_str(), matches, regexPatternStandard ) )
331327
{
332328
std::string paddingStr( matches[2].first, matches[2].second );
333-
padding = paddingStr.size();
334-
strictPadding = ( paddingStr[0] == '#' );
329+
_padding = paddingStr.size();
330+
_strictPadding = ( paddingStr[0] == '#' );
335331
}
336332
else if( ( accept & ePatternCStyle ) && regex_match( filePattern.c_str(), matches, regexPatternCStyle ) )
337333
{
338334
std::string paddingStr( matches[2].first, matches[2].second );
339-
padding = paddingStr.size() == 0 ? 0 : boost::lexical_cast<std::size_t > ( paddingStr ); // if no padding value: %d -> padding = 0
340-
strictPadding = false;
335+
_padding = paddingStr.size() == 0 ? 0 : boost::lexical_cast<std::size_t > ( paddingStr ); // if no _padding value: %d -> _padding = 0
336+
_strictPadding = false;
341337
}
342338
else if( ( accept & ePatternFrame ) && regex_match( filePattern.c_str(), matches, regexPatternFrame ) )
343339
{
344340
std::string frame( matches[2].first, matches[2].second );
345341
// Time t = boost::lexical_cast<Time>( frame );
346-
padding = frame.size();
347-
strictPadding = false;
342+
_padding = frame.size();
343+
_strictPadding = false;
348344
}
349345
else if( ( accept & ePatternFrameNeg ) && regex_match( filePattern.c_str(), matches, regexPatternFrameNeg ) )
350346
{
351347
std::string frame( matches[2].first, matches[2].second );
352348
// Time t = boost::lexical_cast<Time>( frame );
353-
padding = frame.size();
354-
strictPadding = false;
349+
_padding = frame.size();
350+
_strictPadding = false;
355351
}
356352
else
357353
{
358354
// this is a file, not a sequence
359355
return false;
360356
}
361-
prefix = std::string( matches[1].first, matches[1].second );
362-
suffix = std::string( matches[3].first, matches[3].second );
357+
_prefix = std::string( matches[1].first, matches[1].second );
358+
_suffix = std::string( matches[3].first, matches[3].second );
363359
return true;
364360
}
365361

@@ -374,17 +370,6 @@ void Sequence::init( const std::string& prefix, const std::size_t padding, const
374370
_strictPadding = strictPadding;
375371
}
376372

377-
378-
bool Sequence::initFromPattern( const std::string& pattern, const std::vector<FrameRange>& frameRanges, const EPattern accept )
379-
{
380-
if( !retrieveInfosFromPattern( pattern, accept, _prefix, _suffix, _padding, _strictPadding ) )
381-
return false; // not regognize as a pattern, maybe a still file
382-
_ranges.clear();
383-
_ranges = frameRanges;
384-
return true;
385-
}
386-
387-
388373
std::vector<boost::filesystem::path> Sequence::getFiles() const
389374
{
390375
std::vector<boost::filesystem::path> allPaths;

0 commit comments

Comments
 (0)